From 243d5ea91900d7acc385cea7399ec6534d1f72d4 Mon Sep 17 00:00:00 2001 From: Bertrand Coconnier Date: Sun, 24 Jan 2021 00:19:29 +0100 Subject: [PATCH] Don't compile C files with -std=c++1x flag on MacOSX. This makes the build fail when building the Python module from source. For some reason, MacOSX compilers seem to be picky about this flag. The solution is a bit involved since it requires to overload distutils compiler classes to select the flag that is relevant to each source file. --- python/setup.py.in | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/python/setup.py.in b/python/setup.py.in index 876ea2a7cf..20fae50fb2 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -1,6 +1,6 @@ # This script has been automatically built from ${CMAKE_SOURCE_DIR}/python/setup.py.in # -# Copyright (c) 2014-2019 Bertrand Coconnier +# Copyright (c) 2014-2021 Bertrand Coconnier # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software @@ -25,6 +25,7 @@ from setuptools.command.build_ext import build_ext from setuptools.command.install_lib import install_lib from distutils import log from distutils.ccompiler import new_compiler +from distutils.unixccompiler import UnixCCompiler from distutils.dist import Distribution @@ -90,6 +91,36 @@ class InstallJSBSimModule(install_lib): 'msvcp140.dll')) break +# distutils assumes that all the files must be compiled with the same compiler +# flags regardless of their file extension .c or .cpp. +# JSBSim C++ files must be compiled with -std=c++1x but compilation on MacOSX +# fails when trying to compile C files with -std=c++1x. +# The class C_CxxCompiler adds C++ flags to compilation flags for C++ files only. +class C_CxxCompiler(UnixCCompiler): + def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): + _cc_args = cc_args + # Add the C++ flags to the compile flags if we are dealing with a C++ + # source file. + if os.path.splitext(src)[-1] in ('.cpp', '.cxx', '.cc'): + _cc_args = cpp_compile_flag + cc_args + + UnixCCompiler._compile(self, obj, src, ext, _cc_args, + extra_postargs, pp_opts) + +# The class BuildC_CxxExtension intercepts the build of the Python module and +# replaces the compiler by an instance of C_CxxCompiler that select the compile +# flags depending on whether it is a C++ source file or not. +class BuildC_CxxExtension(build_ext): + def build_extension(self, ext): + if self.compiler.compiler_type == 'unix': + + old_compiler = self.compiler + self.compiler = C_CxxCompiler() + # Copy the attributes to the new compiler instance + for attr, value in old_compiler.__dict__.items(): + setattr(self.compiler, attr, value) + return build_ext.build_extension(self, ext) + # Process the path to the JSBSim library. library_path = os.path.join('${BUILD_ROOT_PATH}', 'src') @@ -118,9 +149,9 @@ else: compiler = new_compiler(compiler=compiler_name) if compiler.compiler_type == 'unix': - cpp11_compile_flag = ['-std=c++11'] + cpp_compile_flag = ['-std=c++11'] else: - cpp11_compile_flag = [] + cpp_compile_flag = [] # Update the JSBSim library path according to the --config option if args.config: @@ -138,7 +169,7 @@ if 'sdist' not in dist.commands and compiler.find_library_file([library_path], 'include_dirs': ['src'], 'libraries': ['JSBSim', ${JSBSIM_LINK_LIBRARIES}], 'library_dirs': [library_path], - 'extra_compile_args': cpp11_compile_flag } + 'extra_compile_args': cpp_compile_flag } setup_kwargs = { 'cmdclass' : {'build_ext': QuietBuild, 'install_lib': InstallJSBSimModule}} else: @@ -153,9 +184,10 @@ else: ext_kwargs = { 'sources': ['jsbsim.pyx', ${JSBSIM_SOURCE_FILES}], 'libraries': [${JSBSIM_LINK_LIBRARIES}], 'include_dirs': ['src', 'src/simgear/xml'], - 'extra_compile_args': compile_flags + cpp11_compile_flag} + 'extra_compile_args': compile_flags } # List of required modules to build the JSBSim module from the sources. - setup_kwargs = {'setup_requires': ["setuptools>=18.0", "cython>=0.25"]} + setup_kwargs = {'cmdclass' : {'build_ext': BuildC_CxxExtension,}, + 'setup_requires': ["setuptools>=18.0", "cython>=0.25"]} # Build & installation process for the JSBSim Python module setup(