Skip to content

Commit

Permalink
Don't compile C files with -std=c++1x flag on MacOSX.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bcoconni committed Jan 24, 2021
1 parent e263a89 commit 243d5ea
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions python/setup.py.in
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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(
Expand Down

0 comments on commit 243d5ea

Please sign in to comment.