diff options
author | Simon Warta <[email protected]> | 2017-07-30 00:12:48 +0200 |
---|---|---|
committer | Simon Warta <[email protected]> | 2017-07-30 00:12:48 +0200 |
commit | efc57e680682407fb21c14f16a8dbdff88bcbc57 (patch) | |
tree | ab2d53e1ccacd79a1abb4564896566ed8a84babf | |
parent | 40f399c920c3516d66cbea977f1d38e55a1f7fb1 (diff) |
Refactor and test compiler version detection
-rwxr-xr-x | configure.py | 80 | ||||
-rwxr-xr-x | src/scripts/python_uniitests.py | 67 |
2 files changed, 113 insertions, 34 deletions
diff --git a/configure.py b/configure.py index e2e4f0e96..311c04e5b 100755 --- a/configure.py +++ b/configure.py @@ -2571,38 +2571,49 @@ class AmalgamationGenerator(object): return amalgamation_sources -def detect_compiler_version(ccinfo, cc_bin, os_name): - # pylint: disable=too-many-locals - - cc_version_flag = { - 'msvc': ([], r'Compiler Version ([0-9]+).([0-9]+).[0-9\.]+ for'), - 'gcc': (['-v'], r'gcc version ([0-9]+.[0-9])+.[0-9]+'), - 'clang': (['-v'], r'clang version ([0-9]+.[0-9])[ \.]') +class CompilerDetector(object): + _version_flags = { + 'msvc': [], + 'gcc': ['-v'], + 'clang': ['-v'], + } + _version_patterns = { + 'msvc': r'Compiler Version ([0-9]+).([0-9]+).[0-9\.]+ for', + 'gcc': r'gcc version ([0-9]+.[0-9])+.[0-9]+', + 'clang': r'clang version ([0-9]+.[0-9])[ \.]', } - cc_name = ccinfo.basename - if cc_name not in cc_version_flag.keys(): - logging.info("No compiler version detection available for %s" % (cc_name)) - return None + def __init__(self, cc_name, cc_bin, os_name): + self._cc_name = cc_name + self._cc_bin = cc_bin + self._os_name = os_name - (flags, version_re_str) = cc_version_flag[cc_name] - cc_cmd = cc_bin.split(' ') + flags + def get_version(self): + try: + cmd = self._cc_bin.split(' ') + CompilerDetector._version_flags[self._cc_name] + except KeyError: + logging.info("No compiler version detection available for %s" % (self._cc_name)) + return None - try: - cc_version = None + try: + stdout, stderr = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True).communicate() + cc_output = stdout + "\n" + stderr + except OSError as e: + logging.warning('Could not execute %s for version check: %s' % (cmd, e)) + return None - version = re.compile(version_re_str) - stdout, stderr = subprocess.Popen( - cc_cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True).communicate() - cc_output = stdout + "\n" + stderr + return self.version_from_compiler_output(cc_output) - match = version.search(cc_output) + def version_from_compiler_output(self, cc_output): + cc_version = None + match = re.search(CompilerDetector._version_patterns[self._cc_name], cc_output) if match: - if cc_name == 'msvc': + if self._cc_name == 'msvc': cl_version_to_msvc_version = { '18.00': '2013', '19.00': '2015', @@ -2616,15 +2627,14 @@ def detect_compiler_version(ccinfo, cc_bin, os_name): return None else: cc_version = match.group(1) - elif match is None and cc_name == 'clang' and os_name in ['darwin', 'ios']: + elif match is None and self._cc_name == 'clang' and self._os_name in ['darwin', 'ios']: xcode_version_to_clang = { '703': '3.8', '800': '3.9', '802': '4.0' } - version = re.compile(r'Apple LLVM version [0-9.]+ \(clang-([0-9]{3})\.') - match = version.search(cc_output) + match = re.search(r'Apple LLVM version [0-9.]+ \(clang-([0-9]{3})\.', cc_output) if match: apple_clang_version = match.group(1) @@ -2638,15 +2648,13 @@ def detect_compiler_version(ccinfo, cc_bin, os_name): return '3.8' # safe default if cc_version is None: - logging.warning("Ran '%s' to get %s version, but output '%s' does not match expected version format" % ( - ' '.join(cc_cmd), cc_name, cc_output)) + logging.warning("Tried to get %s version, but output '%s' does not match expected version format" % ( + self._cc_name, cc_output)) return None - logging.info('Detected %s compiler version %s' % (cc_name, cc_version)) + logging.info('Detected %s compiler version %s' % (self._cc_name, cc_version)) return cc_version - except OSError as e: - logging.warning('Could not execute %s for version check: %s' % (cc_cmd, e)) - return None + def have_program(program): """ @@ -3013,7 +3021,11 @@ def main(argv): osinfo = info_os[options.os] module_policy = info_module_policies[options.module_policy] if options.module_policy else None - cc_version = detect_compiler_version(cc, options.compiler_binary or cc.binary_name, osinfo.basename) + cc_version = CompilerDetector( + cc.basename, + options.compiler_binary or cc.binary_name, + osinfo.basename + ).get_version() if options.build_shared_lib and not osinfo.building_shared_supported: logging.warning('Shared libs not supported on %s, disabling shared lib support' % (osinfo.basename)) diff --git a/src/scripts/python_uniitests.py b/src/scripts/python_uniitests.py index 10d465fea..63f8bfc93 100755 --- a/src/scripts/python_uniitests.py +++ b/src/scripts/python_uniitests.py @@ -14,9 +14,76 @@ import sys import unittest sys.path.append("../..") # Botan repo root +from configure import CompilerDetector # pylint: disable=wrong-import-position from configure import ModulesChooser # pylint: disable=wrong-import-position +class CompilerDetection(unittest.TestCase): + + def test_gcc_invalid(self): + detector = CompilerDetector("gcc", "g++", "linux") + + compiler_out = "" + self.assertEqual(detector.version_from_compiler_output(compiler_out), None) + compiler_out = "gcc version 20170406 (Ubuntu 6.3.0-12ubuntu2)" + self.assertEqual(detector.version_from_compiler_output(compiler_out), None) + + def test_clang_invalid(self): + detector = CompilerDetector("clang", "clang++", "linux") + + compiler_out = "" + self.assertEqual(detector.version_from_compiler_output(compiler_out), None) + compiler_out = "clang version 20170406." + self.assertEqual(detector.version_from_compiler_output(compiler_out), None) + + def test_gcc_version(self): + detector = CompilerDetector("gcc", "g++", "linux") + compiler_out = """Using built-in specs. +COLLECT_GCC=/usr/bin/gcc +COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper +Target: x86_64-linux-gnu +Configured with: ../src/configure -v --with-pkgversion='Ubuntu 6.3.0-12ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu +Thread model: posix +gcc version 6.3.0 20170406 (Ubuntu 6.3.0-12ubuntu2)""" + self.assertEqual(detector.version_from_compiler_output(compiler_out), "6.3") + + def test_clang_version(self): + detector = CompilerDetector("clang", "clang++", "linux") + compiler_out = """clang version 4.0.0-1ubuntu1 (tags/RELEASE_400/rc1) +Target: x86_64-pc-linux-gnu +Thread model: posix +InstalledDir: /usr/bin +Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/6 +Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/6.3.0 +Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9 +Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.4 +Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5 +Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.1 +Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6 +Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0 +Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6 +Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6.3.0 +Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9 +Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.4 +Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5 +Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.1 +Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6 +Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.3.0 +Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0 +Candidate multilib: .;@m64 +Selected multilib: .;@m64""" + self.assertEqual(detector.version_from_compiler_output(compiler_out), "4.0") + + def test_msvc_version(self): + detector = CompilerDetector("msvc", "cl.exe", "windows") + compiler_out = """Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24213.1 for x86 +Copyright (C) Microsoft Corporation. All rights reserved. + +usage: cl [ option... ] filename... [ /link linkoption... ] +""" + self.assertEqual(detector.version_from_compiler_output(compiler_out), "2015") + + class ModulesChooserResolveDependencies(unittest.TestCase): def test_base(self): available_modules = set(["A", "B"]) |