Skip to content

Commit

Permalink
Fix cross environment pollution.
Browse files Browse the repository at this point in the history
Environment variables like CFLAGS and LDFLAGS should not affect the
cross environment.

Fixes #1772
  • Loading branch information
oleavr committed May 21, 2017
1 parent abd12b6 commit 1b311f0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 24 deletions.
7 changes: 4 additions & 3 deletions mesonbuild/backend/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,10 @@ def generate_basic_compiler_args(self, target, compiler, no_warn_args=False):
# Add compile args added using add_global_arguments()
# These override per-project arguments
commands += self.build.get_global_args(compiler)
# Compile args added from the env: CFLAGS/CXXFLAGS, etc. We want these
# to override all the defaults, but not the per-target compile args.
commands += self.environment.coredata.external_args[compiler.get_language()]
if not target.is_cross:
# Compile args added from the env: CFLAGS/CXXFLAGS, etc. We want these
# to override all the defaults, but not the per-target compile args.
commands += self.environment.coredata.external_args[compiler.get_language()]
# Always set -fPIC for shared libraries
if isinstance(target, build.SharedLibrary):
commands += compiler.get_pic_args()
Expand Down
7 changes: 4 additions & 3 deletions mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2266,9 +2266,10 @@ def generate_link(self, target, outfile, outname, obj_list, linker, extra_args=[
# Add link args added using add_global_link_arguments()
# These override per-project link arguments
commands += self.build.get_global_link_args(linker)
# Link args added from the env: LDFLAGS. We want these to
# override all the defaults but not the per-target link args.
commands += self.environment.coredata.external_link_args[linker.get_language()]
if not target.is_cross:
# Link args added from the env: LDFLAGS. We want these to
# override all the defaults but not the per-target link args.
commands += self.environment.coredata.external_link_args[linker.get_language()]

# Now we will add libraries and library paths from various sources

Expand Down
18 changes: 10 additions & 8 deletions mesonbuild/backend/vs2010backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,12 @@ def gen_vcxproj(self, target, ofname, guid):
for l, args in self.build.global_args.items():
if l in file_args:
file_args[l] += args
# Compile args added from the env: CFLAGS/CXXFLAGS, etc. We want these
# to override all the defaults, but not the per-target compile args.
for l, args in self.environment.coredata.external_args.items():
if l in file_args:
file_args[l] += args
if not target.is_cross:
# Compile args added from the env: CFLAGS/CXXFLAGS, etc. We want these
# to override all the defaults, but not the per-target compile args.
for l, args in self.environment.coredata.external_args.items():
if l in file_args:
file_args[l] += args
for args in file_args.values():
# This is where Visual Studio will insert target_args, target_defines,
# etc, which are added later from external deps (see below).
Expand Down Expand Up @@ -901,9 +902,10 @@ def gen_vcxproj(self, target, ofname, guid):
# Add link args added using add_global_link_arguments()
# These override per-project link arguments
extra_link_args += self.build.get_global_link_args(compiler)
# Link args added from the env: LDFLAGS. We want these to
# override all the defaults but not the per-target link args.
extra_link_args += self.environment.coredata.external_link_args[compiler.get_language()]
if not target.is_cross:
# Link args added from the env: LDFLAGS. We want these to
# override all the defaults but not the per-target link args.
extra_link_args += self.environment.coredata.external_link_args[compiler.get_language()]
# Only non-static built targets need link args and link dependencies
extra_link_args += target.link_args
# External deps must be last because target link libraries may depend on them.
Expand Down
19 changes: 10 additions & 9 deletions mesonbuild/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,15 +957,16 @@ def _get_compiler_check_args(self, env, extra_args, dependencies, mode='compile'
args += self.get_linker_debug_crt_args()
# Read c_args/cpp_args/etc from the cross-info file (if needed)
args += self.get_cross_extra_flags(env, link=(mode == 'link'))
if mode == 'preprocess':
# Add CPPFLAGS from the env.
args += env.coredata.external_preprocess_args[self.language]
elif mode == 'compile':
# Add CFLAGS/CXXFLAGS/OBJCFLAGS/OBJCXXFLAGS from the env
args += env.coredata.external_args[self.language]
elif mode == 'link':
# Add LDFLAGS from the env
args += env.coredata.external_link_args[self.language]
if not self.is_cross:
if mode == 'preprocess':
# Add CPPFLAGS from the env.
args += env.coredata.external_preprocess_args[self.language]
elif mode == 'compile':
# Add CFLAGS/CXXFLAGS/OBJCFLAGS/OBJCXXFLAGS from the env
args += env.coredata.external_args[self.language]
elif mode == 'link':
# Add LDFLAGS from the env
args += env.coredata.external_link_args[self.language]
args += self.get_compiler_check_args()
# extra_args must override all other arguments, so we add them last
args += extra_args
Expand Down
2 changes: 1 addition & 1 deletion run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def is_cross_build(self):
print('Running unittests.\n')
units = ['InternalTests', 'AllPlatformTests']
if mesonlib.is_linux():
units += ['LinuxlikeTests']
units += ['LinuxlikeTests', 'LinuxArmCrossCompileTests']
elif mesonlib.is_windows():
units += ['WindowsTests']
# Can't pass arguments to unit tests, so set the backend to use in the environment
Expand Down
21 changes: 21 additions & 0 deletions run_unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,27 @@ def test_run_installed(self):
env['LD_LIBRARY_PATH'] = installed_libdir
self.assertEqual(subprocess.call(installed_exe, env=env), 0)

class LinuxArmCrossCompileTests(BasePlatformTests):
'''
Tests that verify cross-compilation to Linux/ARM
'''
def setUp(self):
super().setUp()
src_root = os.path.dirname(__file__)
self.meson_command += ['--cross=' + os.path.join(src_root, 'cross', 'ubuntu-armhf.txt')]

def test_cross_environment_pollution(self):
'''
Test that CFLAGS and the like don't pollute the cross environment. This
can't be an ordinary test case because we need to inspect the compiler
database.
'''
testdir = os.path.join(self.common_test_dir, '3 static')
os.environ['CFLAGS'] = '-DBUILD_ENVIRONMENT_ONLY'
self.init(testdir)
compdb = self.get_compdb()
self.assertNotIn('-DBUILD_ENVIRONMENT_ONLY', compdb[0]['command'])

class RewriterTests(unittest.TestCase):

def setUp(self):
Expand Down

0 comments on commit 1b311f0

Please sign in to comment.