| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2016-2021 The Meson development team |
| 3 |
|
| 4 |
import subprocess |
| 5 |
import re |
| 6 |
import os |
| 7 |
import platform |
| 8 |
import unittest |
| 9 |
|
| 10 |
from mesonbuild.mesonlib import ( |
| 11 |
MachineChoice, is_osx, version_compare |
| 12 |
) |
| 13 |
from mesonbuild.compilers import ( |
| 14 |
detect_c_compiler |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
from run_tests import ( |
| 19 |
get_fake_env |
| 20 |
) |
| 21 |
|
| 22 |
from .baseplatformtests import BasePlatformTests |
| 23 |
from .helpers import * |
| 24 |
|
| 25 |
@unittest.skipUnless(is_osx(), "requires Darwin") |
| 26 |
class DarwinTests(BasePlatformTests): |
| 27 |
''' |
| 28 |
Tests that should run on macOS |
| 29 |
''' |
| 30 |
|
| 31 |
def setUp(self): |
| 32 |
super().setUp() |
| 33 |
self.platform_test_dir = os.path.join(self.src_root, 'test cases/osx') |
| 34 |
|
| 35 |
def test_apple_bitcode(self): |
| 36 |
''' |
| 37 |
Test that -fembed-bitcode is correctly added while compiling and |
| 38 |
-bitcode_bundle is added while linking when b_bitcode is true and not |
| 39 |
when it is false. This can't be an ordinary test case because we need |
| 40 |
to inspect the compiler database. |
| 41 |
''' |
| 42 |
testdir = os.path.join(self.platform_test_dir, '7 bitcode') |
| 43 |
env = get_fake_env(testdir, self.builddir, self.prefix) |
| 44 |
cc = detect_c_compiler(env, MachineChoice.HOST) |
| 45 |
if cc.id != 'clang': |
| 46 |
raise unittest.SkipTest('Not using Clang on OSX') |
| 47 |
# Try with bitcode enabled |
| 48 |
out = self.init(testdir, extra_args='-Db_bitcode=true') |
| 49 |
# Warning was printed |
| 50 |
self.assertRegex(out, 'WARNING:.*b_bitcode') |
| 51 |
# Compiler options were added |
| 52 |
for compdb in self.get_compdb(): |
| 53 |
if 'module' in compdb['file']: |
| 54 |
self.assertNotIn('-fembed-bitcode', compdb['command']) |
| 55 |
else: |
| 56 |
self.assertIn('-fembed-bitcode', compdb['command']) |
| 57 |
build_ninja = os.path.join(self.builddir, 'build.ninja') |
| 58 |
# Linker options were added |
| 59 |
with open(build_ninja, encoding='utf-8') as f: |
| 60 |
contents = f.read() |
| 61 |
m = re.search('LINK_ARGS =.*-bitcode_bundle', contents) |
| 62 |
self.assertIsNotNone(m, msg=contents) |
| 63 |
# Try with bitcode disabled |
| 64 |
self.setconf('-Db_bitcode=false') |
| 65 |
# Regenerate build |
| 66 |
self.build() |
| 67 |
for compdb in self.get_compdb(): |
| 68 |
self.assertNotIn('-fembed-bitcode', compdb['command']) |
| 69 |
build_ninja = os.path.join(self.builddir, 'build.ninja') |
| 70 |
with open(build_ninja, encoding='utf-8') as f: |
| 71 |
contents = f.read() |
| 72 |
m = re.search('LINK_ARGS =.*-bitcode_bundle', contents) |
| 73 |
self.assertIsNone(m, msg=contents) |
| 74 |
|
| 75 |
def test_apple_bitcode_modules(self): |
| 76 |
''' |
| 77 |
Same as above, just for shared_module() |
| 78 |
''' |
| 79 |
testdir = os.path.join(self.common_test_dir, '148 shared module resolving symbol in executable') |
| 80 |
# Ensure that it builds even with bitcode enabled |
| 81 |
self.init(testdir, extra_args='-Db_bitcode=true') |
| 82 |
self.build() |
| 83 |
self.run_tests() |
| 84 |
|
| 85 |
@unittest.skipIf(version_compare(platform.mac_ver()[0], '<10.7'), '-export_dynamic was added in 10.7') |
| 86 |
def test_apple_lto_export_dynamic(self): |
| 87 |
''' |
| 88 |
Tests that -Wl,-export_dynamic is correctly added, when export_dynamic: true is set. |
| 89 |
On macOS, this is relevant for LTO builds only. |
| 90 |
''' |
| 91 |
testdir = os.path.join(self.common_test_dir, '148 shared module resolving symbol in executable') |
| 92 |
# Ensure that it builds even with LTO enabled |
| 93 |
env = {'CFLAGS': '-flto'} |
| 94 |
self.init(testdir, override_envvars=env) |
| 95 |
self.build() |
| 96 |
self.run_tests() |
| 97 |
|
| 98 |
def _get_darwin_versions(self, fname): |
| 99 |
fname = os.path.join(self.builddir, fname) |
| 100 |
out = subprocess.check_output(['otool', '-L', fname], universal_newlines=True) |
| 101 |
m = re.match(r'.*version (.*), current version (.*)\)', out.split('\n')[1]) |
| 102 |
self.assertIsNotNone(m, msg=out) |
| 103 |
return m.groups() |
| 104 |
|
| 105 |
def _get_darwin_rpaths(self, fname: str) -> T.List[str]: |
| 106 |
out = subprocess.check_output(['otool', '-l', fname], universal_newlines=True) |
| 107 |
pattern = re.compile(r'path (.*) \(offset \d+\)') |
| 108 |
rpaths = pattern.findall(out) |
| 109 |
return rpaths |
| 110 |
|
| 111 |
def _get_darwin_rpath_libraries(self, fname: str) -> T.List[str]: |
| 112 |
out = subprocess.check_output(['otool', '-L', fname], universal_newlines=True) |
| 113 |
pattern = re.compile(r'@rpath/\S+') |
| 114 |
libs = pattern.findall(out) |
| 115 |
return libs |
| 116 |
|
| 117 |
@skipIfNoPkgconfig |
| 118 |
def test_library_versioning(self): |
| 119 |
''' |
| 120 |
Ensure that compatibility_version and current_version are set correctly |
| 121 |
''' |
| 122 |
testdir = os.path.join(self.platform_test_dir, '2 library versions') |
| 123 |
self.init(testdir) |
| 124 |
self.build() |
| 125 |
targets = {} |
| 126 |
for t in self.introspect('--targets'): |
| 127 |
targets[t['name']] = t['filename'][0] if isinstance(t['filename'], list) else t['filename'] |
| 128 |
self.assertEqual(self._get_darwin_versions(targets['some']), ('7.0.0', '7.0.0')) |
| 129 |
self.assertEqual(self._get_darwin_versions(targets['noversion']), ('0.0.0', '0.0.0')) |
| 130 |
self.assertEqual(self._get_darwin_versions(targets['onlyversion']), ('1.0.0', '1.0.0')) |
| 131 |
self.assertEqual(self._get_darwin_versions(targets['onlysoversion']), ('5.0.0', '5.0.0')) |
| 132 |
self.assertEqual(self._get_darwin_versions(targets['intver']), ('2.0.0', '2.0.0')) |
| 133 |
self.assertEqual(self._get_darwin_versions(targets['stringver']), ('2.3.0', '2.3.0')) |
| 134 |
self.assertEqual(self._get_darwin_versions(targets['stringlistver']), ('2.4.0', '2.4.0')) |
| 135 |
self.assertEqual(self._get_darwin_versions(targets['intstringver']), ('1111.0.0', '2.5.0')) |
| 136 |
self.assertEqual(self._get_darwin_versions(targets['stringlistvers']), ('2.6.0', '2.6.1')) |
| 137 |
|
| 138 |
def test_duplicate_rpath(self): |
| 139 |
testdir = os.path.join(self.unit_test_dir, '10 build_rpath') |
| 140 |
# We purposely pass a duplicate rpath to Meson, in order |
| 141 |
# to ascertain that Meson does not call install_name_tool |
| 142 |
# with duplicate -delete_rpath arguments, which would |
| 143 |
# lead to erroring out on installation |
| 144 |
env = {"LDFLAGS": "-Wl,-rpath,/foo/bar"} |
| 145 |
self.init(testdir, override_envvars=env) |
| 146 |
self.build() |
| 147 |
self.install() |
| 148 |
|
| 149 |
def test_removing_unused_linker_args(self): |
| 150 |
testdir = os.path.join(self.common_test_dir, '104 has arg') |
| 151 |
env = {'CFLAGS': '-L/tmp -L /var/tmp -headerpad_max_install_names -Wl,-export_dynamic -framework Foundation'} |
| 152 |
self.init(testdir, override_envvars=env) |
| 153 |
|
| 154 |
def test_objc_versions(self): |
| 155 |
# Objective-C always uses the C standard version. |
| 156 |
# Objective-C++ always uses the C++ standard version. |
| 157 |
# This is what most people seem to want and in addition |
| 158 |
# it is the only setup supported by Xcode. |
| 159 |
testdir = os.path.join(self.objc_test_dir, '1 simple') |
| 160 |
self.init(testdir) |
| 161 |
self.assertIn('-std=c99', self.get_compdb()[0]['command']) |
| 162 |
self.wipe() |
| 163 |
testdir = os.path.join(self.objcpp_test_dir, '1 simple') |
| 164 |
self.init(testdir) |
| 165 |
self.assertIn('-std=c++14', self.get_compdb()[0]['command']) |
| 166 |
|
| 167 |
def test_darwin_get_object_archs(self): |
| 168 |
from mesonbuild.mesonlib import darwin_get_object_archs |
| 169 |
archs = darwin_get_object_archs('/bin/cat') |
| 170 |
self.assertEqual(archs, ['x86_64', 'aarch64']) |
| 171 |
|
| 172 |
def test_darwin_meson_rpaths_removed_on_install(self): |
| 173 |
testdir = os.path.join(self.darwin_test_dir, '1 rpath removal on install') |
| 174 |
self.init(testdir) |
| 175 |
self.build() |
| 176 |
# Meson-created RPATHs are usually only valid in the build directory |
| 177 |
rpaths = self._get_darwin_rpaths(os.path.join(self.builddir, 'libbar.dylib')) |
| 178 |
self.assertListEqual(rpaths, ['@loader_path/foo']) |
| 179 |
self.install() |
| 180 |
# Those RPATHs are no longer valid and should not be present after installation |
| 181 |
rpaths = self._get_darwin_rpaths(os.path.join(self.installdir, 'usr/lib/libbar.dylib')) |
| 182 |
self.assertListEqual(rpaths, []) |
| 183 |
libs = self._get_darwin_rpath_libraries(os.path.join(self.installdir, 'usr/bin/main')) |
| 184 |
self.assertListEqual(libs, []) |
| 185 |
libs = self._get_darwin_rpath_libraries(os.path.join(self.installdir, 'usr/bin/main-whole')) |
| 186 |
self.assertListEqual(libs, []) |
| 187 |
|
| 188 |
@skip_if_not_language('rust') |
| 189 |
def test_rust_apple_framework_rlib(self): |
| 190 |
''' |
| 191 |
Test that Rust rlibs properly record Apple framework dependencies, |
| 192 |
so that external tools (like cargo) can link against them without |
| 193 |
meson's help. |
| 194 |
''' |
| 195 |
testdir = os.path.join(self.rust_test_dir, '37 apple framework') |
| 196 |
self.init(testdir) |
| 197 |
# Build only the library, not the executable |
| 198 |
self.build(target='timelib') |
| 199 |
# Manually invoke rustc to build the executable, using the rlib. |
| 200 |
# This simulates what cargo or another build system would do. |
| 201 |
rlib = os.path.join(self.builddir, 'libtimelib.rlib') |
| 202 |
main_rs = os.path.join(testdir, 'main.rs') |
| 203 |
out_exe = os.path.join(self.builddir, 'manual_main') |
| 204 |
subprocess.check_call(['rustc', '--extern', f'timelib={rlib}', main_rs, '-o', out_exe]) |
| 205 |
# Run the executable to verify it works |
| 206 |
subprocess.check_call([out_exe]) |