Skip to content

Commit 7516416

Browse files
committed
SCons: Fix get_compiler_version() to return ints
Otherwise comparisons would fail for compiler versions above 10. Also simplified code somewhat to avoid using subprocess too much needlessly. (cherry picked from commits c7dc514 and df7ecfc)
1 parent c34b351 commit 7516416

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

SConstruct

+4-4
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,13 @@ if selected_platform in platform_list:
350350
# Force to use Unicode encoding
351351
env.Append(MSVC_FLAGS=['/utf8'])
352352
else: # Rest of the world
353+
version = methods.get_compiler_version(env) or [-1, -1]
354+
353355
shadow_local_warning = []
354356
all_plus_warnings = ['-Wwrite-strings']
355357

356358
if methods.using_gcc(env):
357-
version = methods.get_compiler_version(env)
358-
if version != None and version[0] >= '7':
359+
if version[0] >= 7:
359360
shadow_local_warning = ['-Wshadow-local']
360361

361362
if (env["warnings"] == 'extra'):
@@ -369,8 +370,7 @@ if selected_platform in platform_list:
369370
'-Wduplicated-branches', '-Wduplicated-cond',
370371
'-Wstringop-overflow=4', '-Wlogical-op'])
371372
env.Append(CXXFLAGS=['-Wnoexcept', '-Wplacement-new=1'])
372-
version = methods.get_compiler_version(env)
373-
if version != None and version[0] >= '9':
373+
if version[0] >= 9:
374374
env.Append(CCFLAGS=['-Wattribute-alias=2'])
375375
elif (env["warnings"] == 'all'):
376376
env.Append(CCFLAGS=['-Wall'] + shadow_local_warning)

methods.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import os.path
32
import re
43
import glob
54
import subprocess
@@ -626,14 +625,23 @@ def detect_darwin_sdk_path(platform, env):
626625
raise
627626

628627
def get_compiler_version(env):
629-
# Not using this method on clang because it returns 4.2.1 # https://reviews.llvm.org/D56803
630-
if using_gcc(env):
631-
version = decode_utf8(subprocess.check_output([env['CXX'], '-dumpversion']).strip())
632-
else:
633-
version = decode_utf8(subprocess.check_output([env['CXX'], '--version']).strip())
634-
match = re.search('[0-9][0-9.]*', version)
628+
"""
629+
Returns an array of version numbers as ints: [major, minor, patch].
630+
The return array should have at least two values (major, minor).
631+
"""
632+
if not env.msvc:
633+
# Not using -dumpversion as some GCC distros only return major, and
634+
# Clang used to return hardcoded 4.2.1: # https://reviews.llvm.org/D56803
635+
try:
636+
version = decode_utf8(subprocess.check_output([env.subst(env['CXX']), '--version']).strip())
637+
except (subprocess.CalledProcessError, OSError):
638+
print("Couldn't parse CXX environment variable to infer compiler version.")
639+
return None
640+
else: # TODO: Implement for MSVC
641+
return None
642+
match = re.search('[0-9]+\.[0-9.]+', version)
635643
if match is not None:
636-
return match.group().split('.')
644+
return list(map(int, match.group().split('.')))
637645
else:
638646
return None
639647

platform/x11/detect.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,14 @@ def configure(env):
180180
env.Append(LINKFLAGS=['-pipe'])
181181

182182
# Check for gcc version >= 6 before adding -no-pie
183+
version = get_compiler_version(env) or [-1, -1]
183184
if using_gcc(env):
184-
version = get_compiler_version(env)
185-
if version != None and version[0] >= '6':
185+
if version[0] >= 6:
186186
env.Append(CCFLAGS=['-fpie'])
187187
env.Append(LINKFLAGS=['-no-pie'])
188188
# Do the same for clang should be fine with Clang 4 and higher
189189
if using_clang(env):
190-
version = get_compiler_version(env)
191-
if version != None and version[0] >= '4':
190+
if version[0] >= 4:
192191
env.Append(CCFLAGS=['-fpie'])
193192
env.Append(LINKFLAGS=['-no-pie'])
194193

0 commit comments

Comments
 (0)