Updated compiler version detection

This fixes multiple issues/inconsistencies around  `get_compiler_version()`:
* With no shell allocated, launching the compiler could fail even
  with proper paths being set.
* The return value was described as "an array of version numbers as ints",
  but the function actually returned a `Dictionary` (or `None`).
* Not all calls were properly handling a `None` return value in case of errors.
  On Windows this broke compiling for me since #81869 with default settings.
* Some calls defined inconsistent defaults/fallbacks (`0` or `-1`).

(cherry picked from commit 426e18fd37)
This commit is contained in:
Mario Liebisch 2023-09-25 21:08:36 +02:00 committed by Yuri Sizov
parent 08bc3570ae
commit 5bb54d3184
4 changed files with 31 additions and 21 deletions

View File

@ -1002,19 +1002,32 @@ def is_vanilla_clang(env):
def get_compiler_version(env): def get_compiler_version(env):
""" """
Returns an array of version numbers as ints: [major, minor, patch]. Returns a dictionary with various version information:
The return array should have at least two values (major, minor).
- major, minor, patch: Version following semantic versioning system
- metadata1, metadata2: Extra information
- date: Date of the build
""" """
ret = {
"major": -1,
"minor": -1,
"patch": -1,
"metadata1": None,
"metadata2": None,
"date": None,
}
if not env.msvc: if not env.msvc:
# Not using -dumpversion as some GCC distros only return major, and # Not using -dumpversion as some GCC distros only return major, and
# Clang used to return hardcoded 4.2.1: # https://reviews.llvm.org/D56803 # Clang used to return hardcoded 4.2.1: # https://reviews.llvm.org/D56803
try: try:
version = subprocess.check_output([env.subst(env["CXX"]), "--version"]).strip().decode("utf-8") version = subprocess.check_output([env.subst(env["CXX"]), "--version"], shell=True).strip().decode("utf-8")
except (subprocess.CalledProcessError, OSError): except (subprocess.CalledProcessError, OSError):
print("Couldn't parse CXX environment variable to infer compiler version.") print("Couldn't parse CXX environment variable to infer compiler version.")
return None return ret
else: # TODO: Implement for MSVC else:
return None # TODO: Implement for MSVC
return ret
match = re.search( match = re.search(
r"(?:(?<=version )|(?<=\) )|(?<=^))" r"(?:(?<=version )|(?<=\) )|(?<=^))"
r"(?P<major>\d+)" r"(?P<major>\d+)"
@ -1026,9 +1039,13 @@ def get_compiler_version(env):
version, version,
) )
if match is not None: if match is not None:
return match.groupdict() for key, value in match.groupdict().items():
else: if value is not None:
return None ret[key] = value
# Transform semantic versioning to integers
for key in ["major", "minor", "patch"]:
ret[key] = int(ret[key] or -1)
return ret
def using_gcc(env): def using_gcc(env):

View File

@ -106,7 +106,7 @@ def configure(env: "Environment"):
print("Using linker program: " + env["linker"]) print("Using linker program: " + env["linker"])
if env["linker"] == "mold" and using_gcc(env): # GCC < 12.1 doesn't support -fuse-ld=mold. if env["linker"] == "mold" and using_gcc(env): # GCC < 12.1 doesn't support -fuse-ld=mold.
cc_version = get_compiler_version(env) cc_version = get_compiler_version(env)
cc_semver = (int(cc_version["major"]), int(cc_version["minor"])) cc_semver = (cc_version["major"], cc_version["minor"])
if cc_semver < (12, 1): if cc_semver < (12, 1):
found_wrapper = False found_wrapper = False
for path in ["/usr/libexec", "/usr/local/libexec", "/usr/lib", "/usr/local/lib"]: for path in ["/usr/libexec", "/usr/local/libexec", "/usr/lib", "/usr/local/lib"]:

View File

@ -119,16 +119,9 @@ def configure(env: "Environment"):
env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"]) env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"]) env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
cc_version = get_compiler_version(env) or { cc_version = get_compiler_version(env)
"major": None, cc_version_major = cc_version["major"]
"minor": None, cc_version_minor = cc_version["minor"]
"patch": None,
"metadata1": None,
"metadata2": None,
"date": None,
}
cc_version_major = int(cc_version["major"] or -1)
cc_version_minor = int(cc_version["minor"] or -1)
vanilla = is_vanilla_clang(env) vanilla = is_vanilla_clang(env)
# Workaround for Xcode 15 linker bug. # Workaround for Xcode 15 linker bug.

View File

@ -203,7 +203,7 @@ def configure(env: "Environment"):
# Get version info for checks below. # Get version info for checks below.
cc_version = get_compiler_version(env) cc_version = get_compiler_version(env)
cc_semver = (int(cc_version["major"]), int(cc_version["minor"]), int(cc_version["patch"])) cc_semver = (cc_version["major"], cc_version["minor"], cc_version["patch"])
if env["lto"] != "none": if env["lto"] != "none":
# Workaround https://github.com/emscripten-core/emscripten/issues/19781. # Workaround https://github.com/emscripten-core/emscripten/issues/19781.