From 82a8c72187f844713618526ed3890d7b313b2065 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Thu, 28 Apr 2022 16:08:24 -0400 Subject: c_std, cpp_std: Change to a list of desired versions in preference order Projects that prefer GNU C but can fallback to ISO C can now set for example `default_options: 'c_std=gnu11,c11'` and it will use gnu11 when available, fallback to c11 otherwise. It is an error only if none of the values are supported by the current compiler. This allows to deprecate gnuXX values from MSVC compiler, that means that `default_options: 'c_std=gnu11'` will now print warning with MSVC but still fallback to 'c11' value. No warning is printed if at least one of the values is valid, i.e. `default_options: 'c_std=gnu11,c11'`. In the future that deprecation warning will become an hard error because `c_std=gnu11` should mean GNU is required, for projects that cannot be built with MSVC for example. --- unittests/allplatformstests.py | 33 +++++++++++++++++++++++++++++++++ unittests/baseplatformtests.py | 7 +++++++ 2 files changed, 40 insertions(+) (limited to 'unittests') diff --git a/unittests/allplatformstests.py b/unittests/allplatformstests.py index 7f38d4b..1a34f5d 100644 --- a/unittests/allplatformstests.py +++ b/unittests/allplatformstests.py @@ -4776,3 +4776,36 @@ class AllPlatformTests(BasePlatformTests): self.assertNotEqual(olddata, newdata) olddata = newdata oldmtime = newmtime + + def test_c_cpp_stds(self): + testdir = os.path.join(self.unit_test_dir, '114 c cpp stds') + self.init(testdir) + # Invalid values should fail whatever compiler we have + with self.assertRaises(subprocess.CalledProcessError): + self.setconf('-Dc_std=invalid') + with self.assertRaises(subprocess.CalledProcessError): + self.setconf('-Dc_std=c89,invalid') + with self.assertRaises(subprocess.CalledProcessError): + self.setconf('-Dc_std=c++11') + env = get_fake_env() + cc = detect_c_compiler(env, MachineChoice.HOST) + if cc.get_id() == 'msvc': + # default_option should have selected those + self.assertEqual(self.getconf('c_std'), 'c89') + self.assertEqual(self.getconf('cpp_std'), 'vc++11') + # This is deprecated but works for C + self.setconf('-Dc_std=gnu99') + self.assertEqual(self.getconf('c_std'), 'c99') + # C++ however never accepted that fallback + with self.assertRaises(subprocess.CalledProcessError): + self.setconf('-Dcpp_std=gnu++11') + # The first supported std should be selected + self.setconf('-Dcpp_std=gnu++11,vc++11,c++11') + self.assertEqual(self.getconf('cpp_std'), 'vc++11') + elif cc.get_id() == 'gcc': + # default_option should have selected those + self.assertEqual(self.getconf('c_std'), 'gnu89') + self.assertEqual(self.getconf('cpp_std'), 'gnu++98') + # The first supported std should be selected + self.setconf('-Dcpp_std=c++11,gnu++11,vc++11') + self.assertEqual(self.getconf('cpp_std'), 'c++11') diff --git a/unittests/baseplatformtests.py b/unittests/baseplatformtests.py index 3008eb7..b7deda5 100644 --- a/unittests/baseplatformtests.py +++ b/unittests/baseplatformtests.py @@ -303,6 +303,13 @@ class BasePlatformTests(TestCase): ensure_backend_detects_changes(self.backend) self._run(self.mconf_command + arg + [self.builddir]) + def getconf(self, optname: str): + opts = self.introspect('--buildoptions') + for x in opts: + if x.get('name') == optname: + return x.get('value') + self.fail(f'Option {optname} not found') + def wipe(self): windows_proof_rmtree(self.builddir) -- cgit v1.1