Debian Codegraph @

unittests/linuxcrosstests.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2016-2021 The Meson development team
3
4 import os
5 import shutil
6 import unittest
7 import platform
8
9 from mesonbuild.mesonlib import (
10 is_windows, is_cygwin
11 )
12 from mesonbuild.mesonlib import MesonException
13
14
15
16 from .baseplatformtests import BasePlatformTests
17 from .helpers import *
18
19 class BaseLinuxCrossTests(BasePlatformTests):
20 # Don't pass --libdir when cross-compiling. We have tests that
21 # check whether meson auto-detects it correctly.
22 libdir = None
23
24
25 def should_run_cross_arm_tests():
26 return shutil.which('armv7l-unknown-linux-gnueabihf-gcc') and not platform.machine().lower().startswith('arm')
27
28 @unittest.skipUnless(not is_windows() and should_run_cross_arm_tests(), "requires ability to cross compile to ARM")
29 class LinuxCrossArmTests(BaseLinuxCrossTests):
30 '''
31 Tests that cross-compilation to Linux/ARM works
32 '''
33
34 def setUp(self):
35 super().setUp()
36 self.meson_cross_files = [os.path.join(self.src_root, 'cross', 'nixos-armhf.ini')]
37
38 def test_nested_for_build_subprojects(self) -> None:
39 """Test that when cross compiled nested native subprojects report
40 themselves as for the build machine.
41
42 This cannot be done without a unit test because in a host == build
43 configuration this wont be reported at all
44 """
45 testdir = os.path.join(self.src_root, 'test cases', 'native', '10 native subproject')
46
47 with self.subTest('configuring'):
48 out = self.init(testdir)
49
50 with self.subTest('nested build-only subproject reports correct machine'):
51 self.assertIn('Executing subproject buildtool:hostp for machine: build', out)
52
53 with self.subTest('summary reports correct host subprojects'):
54 expected = '\n'.join([
55 'Subprojects (for host machine)',
56 ' both : YES',
57 ' recursive-both : YES',
58 ' recursive-host-only : YES',
59 ])
60 self.assertIn(expected, out)
61
62 with self.subTest('summary reports correct build subprojects'):
63 expected = '\n'.join([
64 'Subprojects (for build machine)',
65 ' both : YES',
66 ' buildtool : YES',
67 ' hostp : YES',
68 ' recursive-both : YES',
69 ' recursive-build-only: YES',
70 ' test installs : YES',
71 ])
72 self.assertIn(expected, out)
73
74 def test_cflags_cross_environment_pollution(self):
75 '''
76 Test that the CFLAGS environment variable does not pollute the cross
77 environment. This can't be an ordinary test case because we need to
78 inspect the compiler database.
79 '''
80 testdir = os.path.join(self.common_test_dir, '3 static')
81 self.init(testdir, override_envvars={'CFLAGS': '-DBUILD_ENVIRONMENT_ONLY'})
82 compdb = self.get_compdb()
83 self.assertNotIn('-DBUILD_ENVIRONMENT_ONLY', compdb[0]['command'])
84
85 def test_cross_file_overrides_always_args(self):
86 '''
87 Test that $lang_args in cross files always override get_always_args().
88 Needed for overriding the default -D_FILE_OFFSET_BITS=64 on some
89 architectures such as some Android versions and Raspbian.
90 https://github.com/mesonbuild/meson/issues/3049
91 https://github.com/mesonbuild/meson/issues/3089
92 '''
93 testdir = os.path.join(self.unit_test_dir, '33 cross file overrides always args')
94 self.meson_cross_files = [os.path.join(testdir, 'ubuntu-armhf-overrides.txt')]
95 self.init(testdir)
96 compdb = self.get_compdb()
97 self.assertRegex(compdb[0]['command'], '-D_FILE_OFFSET_BITS=64.*-U_FILE_OFFSET_BITS')
98 self.build()
99
100 def test_cross_libdir(self):
101 # When cross compiling "libdir" should default to "lib"
102 # rather than "lib/x86_64-linux-gnu" or something like that.
103 testdir = os.path.join(self.common_test_dir, '1 trivial')
104 self.init(testdir)
105 for i in self.introspect('--buildoptions'):
106 if i['name'] == 'libdir':
107 self.assertEqual(i['value'], 'lib')
108 return
109 self.assertTrue(False, 'Option libdir not in introspect data.')
110
111 def test_cross_libdir_subproject(self):
112 # Guard against a regression where calling "subproject"
113 # would reset the value of libdir to its default value.
114 testdir = os.path.join(self.unit_test_dir, '75 subdir libdir')
115 self.init(testdir, extra_args=['--libdir=fuf'])
116 for i in self.introspect('--buildoptions'):
117 if i['name'] == 'libdir':
118 self.assertEqual(i['value'], 'fuf')
119 return
120 self.assertTrue(False, 'Libdir specified on command line gets reset.')
121
122 def test_std_remains(self):
123 # C_std defined in project options must be in effect also when cross compiling.
124 testdir = os.path.join(self.unit_test_dir, '50 noncross options')
125 self.init(testdir)
126 compdb = self.get_compdb()
127 self.assertRegex(compdb[0]['command'], '-std=c99')
128 self.build()
129
130 @skipIfNoPkgconfig
131 @skipIfNoExecutable('arm-linux-gnueabihf-pkg-config')
132 def test_pkg_config_option(self):
133 testdir = os.path.join(self.unit_test_dir, '57 pkg_config_path option')
134 self.init(testdir, extra_args=[
135 '-Dbuild.pkg_config_path=' + os.path.join(testdir, 'build_extra_path'),
136 '-Dpkg_config_path=' + os.path.join(testdir, 'host_extra_path'),
137 ])
138
139 def test_run_native_test(self):
140 '''
141 https://github.com/mesonbuild/meson/issues/7997
142 check run native test in crossbuild without exe wrapper
143 '''
144 testdir = os.path.join(self.unit_test_dir, '87 run native test')
145 stamp_file = os.path.join(self.builddir, 'native_test_has_run.stamp')
146 self.init(testdir)
147 self.build()
148 self.assertPathDoesNotExist(stamp_file)
149 self.run_tests()
150 self.assertPathExists(stamp_file)
151
152
153 def should_run_cross_mingw_tests():
154 return shutil.which('x86_64-w64-mingw32-gcc') and not (is_windows() or is_cygwin())
155
156 @unittest.skipUnless(not is_windows() and should_run_cross_mingw_tests(), "requires ability to cross compile with MinGW")
157 class LinuxCrossMingwTests(BaseLinuxCrossTests):
158 '''
159 Tests that cross-compilation to Windows/MinGW works
160 '''
161
162 def setUp(self):
163 super().setUp()
164 self.meson_cross_files = [os.path.join(self.src_root, 'cross', 'linux-mingw-w64-64bit.txt')]
165
166 def test_exe_wrapper_behaviour(self):
167 '''
168 Test that an exe wrapper that isn't found doesn't cause compiler sanity
169 checks and compiler checks to fail, but causes configure to fail if it
170 requires running a cross-built executable (custom_target or run_target)
171 and causes the tests to be skipped if they are run.
172 '''
173 testdir = os.path.join(self.unit_test_dir, '36 exe_wrapper behaviour')
174 # Configures, builds, and tests fine by default
175 self.init(testdir)
176 self.build()
177 self.run_tests()
178 self.wipe()
179 os.mkdir(self.builddir)
180 # Change cross file to use a non-existing exe_wrapper and it should fail
181 self.meson_cross_files = [os.path.join(testdir, 'broken-cross.txt')]
182 # Force tracebacks so we can detect them properly
183 env = {'MESON_FORCE_BACKTRACE': '1'}
184 error_message = "An exe_wrapper is needed for " + self.builddir + "/prog.exe but was not found. Please define one in cross file and check the command and/or add it to PATH."
185
186 with self.assertRaises(MesonException) as cm:
187 # Must run in-process or we'll get a generic CalledProcessError
188 self.init(testdir, extra_args='-Drun-target=false',
189 inprocess=True,
190 override_envvars=env)
191 self.assertEqual(str(cm.exception), error_message)
192
193 with self.assertRaises(MesonException) as cm:
194 # Must run in-process or we'll get a generic CalledProcessError
195 self.init(testdir, extra_args='-Dcustom-target=false',
196 inprocess=True,
197 override_envvars=env)
198 self.assertEqual(str(cm.exception), error_message)
199
200 self.init(testdir, extra_args=['-Dcustom-target=false', '-Drun-target=false'],
201 override_envvars=env)
202 self.build()
203
204 with self.assertRaises(MesonException) as cm:
205 # Must run in-process or we'll get a generic CalledProcessError
206 self.run_tests(inprocess=True, override_envvars=env)
207 self.assertEqual(str(cm.exception),
208 "The exe_wrapper defined in the cross file 'broken' was not found. Please check the command and/or add it to PATH.")
209
210 @skipIfNoPkgconfig
211 def test_cross_pkg_config_option(self):
212 testdir = os.path.join(self.unit_test_dir, '57 pkg_config_path option')
213 self.init(testdir, extra_args=[
214 '-Dbuild.pkg_config_path=' + os.path.join(testdir, 'build_extra_path'),
215 '-Dpkg_config_path=' + os.path.join(testdir, 'host_extra_path'),
216 ])