From 0b54b81222f092bd859b08af993e2700bddd81be Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 23 Mar 2021 20:52:49 -0700 Subject: Split environment variable and command line cflags They are supposed to have different behavior. The environment variables apply to both the compiler and linker when the compiler acts as a linker, but the command line ones do not. Fixes #8345 --- run_unittests.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'run_unittests.py') diff --git a/run_unittests.py b/run_unittests.py index fd75c8c9c..1eba4dfd6 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -5634,6 +5634,28 @@ class AllPlatformTests(BasePlatformTests): matches += 1 self.assertEqual(matches, 1) + def test_env_flags_to_linker(self) -> None: + # Compilers that act as drivers should add their compiler flags to the + # linker, those that do not shouldn't + with mock.patch.dict(os.environ, {'CFLAGS': '-DCFLAG', 'LDFLAGS': '-flto'}): + env = get_fake_env() + + # Get the compiler so we know which compiler class to mock. + cc = env.detect_compiler_for('c', MachineChoice.HOST) + cc_type = type(cc) + + # Test a compiler that acts as a linker + with mock.patch.object(cc_type, 'INVOKES_LINKER', True): + cc = env.detect_compiler_for('c', MachineChoice.HOST) + link_args = env.coredata.get_external_link_args(cc.for_machine, cc.language) + self.assertEqual(sorted(link_args), sorted(['-DCFLAG', '-flto'])) + + # And one that doesn't + with mock.patch.object(cc_type, 'INVOKES_LINKER', False): + cc = env.detect_compiler_for('c', MachineChoice.HOST) + link_args = env.coredata.get_external_link_args(cc.for_machine, cc.language) + self.assertEqual(sorted(link_args), sorted(['-flto'])) + class FailureTests(BasePlatformTests): ''' Tests that test failure conditions. Build files here should be dynamically -- cgit v1.2.1