test_driver.py (26639B)
1 import hashlib 2 import io 3 import json 4 import os 5 from pathlib import Path 6 import platform 7 import shutil 8 import subprocess 9 import tarfile 10 import tempfile 11 import unittest 12 13 14 ROOT = Path(__file__).resolve().parent.parent 15 MAKE = shutil.which("make") 16 TOOLS = "clang clang++ ld.lld llvm-ar llvm-ranlib llvm-nm llvm-objcopy llvm-objdump llvm-readelf llvm-strip llvm-size llvm-strings llvm-addr2line llvm-cxxfilt" 17 FAKE_TOOL = r'''#!/usr/bin/env python3 18 import json 19 import os 20 from pathlib import Path 21 import shutil 22 import sys 23 24 name = Path(sys.argv[0]).name 25 args = sys.argv[1:] 26 with open(os.environ["FAKE_LOG"], "a") as log: 27 log.write(json.dumps({"tool": name, "args": args, 28 "sysroot": os.environ.get("MUSLLVM_SYSROOT")}) + "\n") 29 30 def write(path, value="fixture"): 31 path = Path(path) 32 path.parent.mkdir(parents=True, exist_ok=True) 33 path.write_text(value) 34 35 if name in ("clang", "clang++", "gcc", "g++"): 36 version = os.environ.get("FAKE_CLANG_VERSION", "21.1.8") 37 if "--version" in args: 38 print(("clang" if name.startswith("clang") else "gcc") + " version " + version + " (fixture)") 39 elif "-dumpversion" in args: 40 print(version) 41 elif "-print-resource-dir" in args: 42 print(os.environ["FAKE_RESOURCE"]) 43 elif "-print-target-triple" in args: 44 print(next(arg.split("=", 1)[1] for arg in args if arg.startswith("--target="))) 45 elif "-print-libgcc-file-name" in args: 46 resource = next(arg.split("=", 1)[1] for arg in args if arg.startswith("-resource-dir=")) 47 print(str(Path(resource) / "lib/linux" / ("libclang_rt.builtins-" + os.environ["ARCH"] + ".a"))) 48 else: 49 sys.exit("fixture refuses compilation") 50 elif name == "llvm-config": 51 print(os.environ["FAKE_LLVM_CMAKE"]) 52 elif name == "curl": 53 destination = Path(args[args.index("--output") + 1]) 54 archive = args[-1].rsplit("/", 1)[-1] 55 if os.environ.get("FAKE_CURL_BAD"): 56 destination.write_text("truncated download") 57 else: 58 shutil.copyfile(Path(os.environ["FAKE_DOWNLOADS"]) / archive, destination) 59 if os.environ.get("FAKE_CURL_FAIL"): 60 sys.exit(22) 61 elif name == "make": 62 if "headers_install" in args: 63 prefix = next(arg.split("=", 1)[1] for arg in args if arg.startswith("INSTALL_HDR_PATH=")) 64 write(Path(prefix) / "include/linux/version.h") 65 elif "install-headers" in args: 66 prefix = next(arg.split("=", 1)[1] for arg in args if arg.startswith("DESTDIR=")) 67 write(Path(prefix) / "usr/include/stdio.h") 68 elif "install" in args: 69 prefix = next(arg.split("=", 1)[1] for arg in args if arg.startswith("DESTDIR=")) 70 write(Path(prefix) / "usr/lib/libc.so") 71 (Path(prefix) / "lib").mkdir(exist_ok=True) 72 elif name == "cmake": 73 if "-B" in args: 74 directory = Path(args[args.index("-B") + 1]) 75 write(directory / "fixture-config.json", json.dumps(args)) 76 elif "--build" in args: 77 directory = Path(args[args.index("--build") + 1]) 78 if os.environ.get("FAKE_FAIL_STAGE") == directory.name: 79 sys.exit(1) 80 if "install-distribution" in args: 81 configuration = json.loads((directory / "fixture-config.json").read_text()) 82 prefix = next(arg.split("=", 1)[1] for arg in configuration if arg.startswith("-DCMAKE_INSTALL_PREFIX=")) 83 for tool in os.environ["FAKE_TOOLS"].split(): 84 path = Path(prefix) / "bin" / tool 85 path.parent.mkdir(parents=True, exist_ok=True) 86 shutil.copyfile(os.environ["FAKE_EXECUTABLE"], path) 87 path.chmod(0o755) 88 elif "--install" in args: 89 directory = Path(args[args.index("--install") + 1]) 90 configuration = json.loads((directory / "fixture-config.json").read_text()) 91 prefix = next(arg.split("=", 1)[1] for arg in configuration if arg.startswith("-DCMAKE_INSTALL_PREFIX=")) 92 if directory.name == "builtins": 93 write(Path(prefix) / "lib/linux" / ("libclang_rt.builtins-" + os.environ["ARCH"] + ".a")) 94 else: 95 write(Path(os.environ["DESTDIR"]) / "usr/lib/libc++.a") 96 ''' 97 98 CAPTURE = r'''#!/usr/bin/env python3 99 import json 100 import os 101 import sys 102 print(json.dumps({"args": sys.argv[1:], "env": dict(os.environ)})) 103 ''' 104 105 106 class DriverTests(unittest.TestCase): 107 def setUp(self): 108 self.temporary = tempfile.TemporaryDirectory(prefix="musllvm-test-") 109 self.addCleanup(self.temporary.cleanup) 110 self.root = Path(self.temporary.name) / "project" 111 self.root.mkdir() 112 for name in ("scripts", "files/toolchain"): 113 shutil.copytree(ROOT / name, self.root / name) 114 for name in ("Makefile", "config.mk"): 115 shutil.copyfile(ROOT / name, self.root / name) 116 self.tools = Path(self.temporary.name) / "tools" 117 self.tools.mkdir() 118 self.fake = self.tools / "fixture" 119 self.fake.write_text(FAKE_TOOL) 120 self.fake.chmod(0o755) 121 for name in TOOLS.split() + ["llvm-config", "curl", "make", "cmake", "ninja"]: 122 (self.tools / name).symlink_to(self.fake) 123 self.resource = Path(self.temporary.name) / "resource/21" 124 (self.resource / "include").mkdir(parents=True) 125 (self.resource / "include/unwind.h").write_text("fixture") 126 self.llvm_cmake = Path(self.temporary.name) / "llvm-cmake" 127 self.llvm_cmake.mkdir() 128 (self.llvm_cmake / "LLVMConfig.cmake").write_text("fixture") 129 self.log = Path(self.temporary.name) / "commands.jsonl" 130 self.downloads = Path(self.temporary.name) / "downloads" 131 self.downloads.mkdir() 132 manifest = [] 133 for component in ("musl", "linux", "llvm", "atomic"): 134 archive = self.downloads / (component + "-test.tar.gz") 135 with tarfile.open(archive, "w:gz") as stream: 136 relative = "configure" if component == "musl" else "placeholder" 137 content = b"#!/bin/sh\nset -eu\n\"$CC\" -print-target-triple\n" if component == "musl" else b"fixture" 138 info = tarfile.TarInfo(component + "-test/" + relative) 139 info.size = len(content) 140 info.mode = 0o755 141 stream.addfile(info, io.BytesIO(content)) 142 if component == "atomic": 143 for relative in ("Makefile", "atomic.c"): 144 info = tarfile.TarInfo(component + "-test/" + relative) 145 info.size = 7 146 stream.addfile(info, io.BytesIO(b"fixture")) 147 if component == "llvm": 148 headers = { 149 "unwind.h": b"#include <__libunwind_config.h>\n#include <unwind_itanium.h>\n", 150 "__libunwind_config.h": b"#define UNWIND_CONFIG_FIXTURE 1\n", 151 "unwind_itanium.h": b"#define UNWIND_ITANIUM_FIXTURE 1\n", 152 } 153 for name, content in headers.items(): 154 info = tarfile.TarInfo(component + "-test/libunwind/include/" + name) 155 info.size = len(content) 156 stream.addfile(info, io.BytesIO(content)) 157 digest = hashlib.sha256(archive.read_bytes()).hexdigest() 158 manifest.append(f"{component} {archive.name} {digest} https://example.invalid/{archive.name}\n") 159 (self.root / "sources.list").write_text("".join(manifest)) 160 self.env = os.environ.copy() 161 for key in ("ARCH", "TARGET", "PREFIX", "BUILD_DIR", "SOURCES_DIR", "SOURCES_FILE", "LLVM_MODE", "BOOTSTRAP_LLVM_MODE", "JOBS", "LINK_JOBS", "HOST_CC", "HOST_CXX", "TARGET_CFLAGS", "HOST_CFLAGS", "HOST_CXXFLAGS", "HOST_LDFLAGS", "RUNNER", "MAKEFLAGS", "MFLAGS", "MUSLLVM_SYSROOT"): 162 self.env.pop(key, None) 163 self.env.update({ 164 "PATH": str(self.tools) + os.pathsep + os.environ["PATH"], 165 "FAKE_LOG": str(self.log), "FAKE_RESOURCE": str(self.resource), 166 "FAKE_LLVM_CMAKE": str(self.llvm_cmake), 167 "FAKE_DOWNLOADS": str(self.downloads), "ARCH": "x86_64", 168 "FAKE_EXECUTABLE": str(self.fake), "FAKE_TOOLS": TOOLS, 169 }) 170 171 def run_make(self, *arguments, success=True, env=None): 172 result = subprocess.run([MAKE, *arguments], cwd=self.root, env=env or self.env, 173 text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 174 if success: 175 self.assertEqual(result.returncode, 0, result.stdout) 176 else: 177 self.assertNotEqual(result.returncode, 0, result.stdout) 178 return result 179 180 def commands(self): 181 return [json.loads(line) for line in self.log.read_text().splitlines()] 182 183 def install_capture(self, path): 184 path.write_text(CAPTURE) 185 path.chmod(0o755) 186 187 def source_patch(self, name, before="fixture", after="patched"): 188 path = self.root / "patches/llvm" / name 189 path.parent.mkdir(parents=True, exist_ok=True) 190 path.write_text("--- a/placeholder\n+++ b/placeholder\n@@ -1 +1 @@\n-" + before + 191 "\n\\ No newline at end of file\n+" + after + "\n\\ No newline at end of file\n") 192 return path 193 194 def mock_host_check(self): 195 (self.root / "scripts/package.py").write_text('''import json 196 import os 197 import sys 198 with open(os.environ["FAKE_LOG"], "a") as stream: 199 stream.write(json.dumps({"tool": "check-host", "args": sys.argv[1:]}) + "\\n") 200 sys.exit(1 if os.environ.get("FAKE_STATIC_FAIL") else 0) 201 ''') 202 203 def install_wrapper(self, name, template, prefix=None): 204 prefix = prefix or Path(self.temporary.name) / "relocated tools' path" 205 (prefix / "bin").mkdir(parents=True, exist_ok=True) 206 (prefix / "libexec").mkdir(exist_ok=True) 207 script = (ROOT / "files/toolchain" / template).read_text() 208 path = prefix / "bin" / name 209 path.write_text(script.replace("@TARGET@", "x86_64-linux-musl").replace("@RESOURCE_VERSION@", "21")) 210 path.chmod(0o755) 211 return path 212 213 def test_configuration_and_command_line_overrides(self): 214 result = self.run_make("show-config", "ARCH=aarch64", "JOBS=1") 215 self.assertIn("TARGET=aarch64-linux-musl", result.stdout) 216 self.assertIn("JOBS=1", result.stdout) 217 self.assertIn(f"PREFIX={self.root}/build/aarch64-linux-musl\n", result.stdout) 218 self.assertIn(f"BUILD_DIR={self.root}/build/work/aarch64-linux-musl\n", result.stdout) 219 self.assertFalse((self.root / "build").exists()) 220 221 def test_invalid_configuration_fails_before_work(self): 222 for arguments in (("ARCH=mips",), ("JOBS=00",), ("JOBS=-2",), ("LINK_JOBS=1:2",), 223 ("PREFIX=/",), ("BUILD_DIR=/tmp/../home",), ("LLVM_MODE=other",), 224 ("TARGET=aarch64-linux-musl",), ("PREFIX=/tmp/tools with spaces",), 225 ("PREFIX=/tmp/tools$$variable",)): 226 with self.subTest(arguments=arguments): 227 self.run_make("show-config", *arguments, success=False) 228 self.assertFalse((self.root / "build").exists()) 229 230 def test_invalid_static_bootstrap_mode_fails_before_work(self): 231 self.run_make("show-config", "LLVM_MODE=static", "BOOTSTRAP_LLVM_MODE=static", success=False) 232 self.assertFalse((self.root / "build").exists()) 233 234 def test_static_host_bootstrap_uses_the_native_architecture(self): 235 self.mock_host_check() 236 native = platform.machine() 237 target = "aarch64" if native == "x86_64" else "x86_64" 238 self.run_make("runtimes", "LLVM_MODE=static", "ARCH=" + target, "TARGET_CFLAGS=-Os", 239 env={**self.env, "MUSLLVM_SYSROOT": "/wrong/target/sysroot"}) 240 prefix = self.root / "build" / (target + "-linux-musl") 241 work = self.root / "build/work" / (target + "-linux-musl") 242 bootstrap = work / "bootstrap/prefix" 243 options = json.loads((work / "llvm/fixture-config.json").read_text()) 244 self.assertIn("-DCMAKE_C_COMPILER=" + str(bootstrap / "bin" / (native + "-linux-musl-cc")), options) 245 self.assertIn("-DCMAKE_SYSROOT=" + str(bootstrap / "sysroot"), options) 246 self.assertIn("-DLLVM_HOST_TRIPLE=" + native + "-linux-musl", options) 247 self.assertIn("-DBUILD_SHARED_LIBS=OFF", options) 248 self.assertIn("-DLLVM_LINK_LLVM_DYLIB=OFF", options) 249 self.assertIn("-DLLVM_ENABLE_PLUGINS=OFF", options) 250 self.assertIn("-static", next(value for value in options if value.startswith("-DCMAKE_EXE_LINKER_FLAGS="))) 251 native_options = json.loads((work / "bootstrap/work/runtimes/fixture-config.json").read_text()) 252 self.assertIn("-DCMAKE_C_COMPILER_TARGET=" + native + "-linux-musl", native_options) 253 self.assertNotIn("-Os", next(value for value in native_options if value.startswith("-DCMAKE_C_FLAGS="))) 254 target_options = json.loads((work / "runtimes/fixture-config.json").read_text()) 255 self.assertIn("-DCMAKE_C_COMPILER_TARGET=" + target + "-linux-musl", target_options) 256 self.assertEqual((prefix / "share/musllvm/llvm-mode").read_text(), "static\n") 257 self.assertTrue(any(item["tool"] == "check-host" for item in self.commands())) 258 self.assertEqual(os.readlink(prefix / "libexec/clang"), "../host/bin/clang") 259 self.assertTrue(all(item["sysroot"] is None for item in self.commands() if item["tool"] == "cmake")) 260 count = sum(item["tool"] == "cmake" for item in self.commands()) 261 self.run_make("runtimes", "LLVM_MODE=static", "ARCH=" + target, "TARGET_CFLAGS=-Os") 262 self.assertEqual(sum(item["tool"] == "cmake" for item in self.commands()), count) 263 self.run_make("runtimes", "LLVM_MODE=static", "ARCH=" + target, "TARGET_CFLAGS=-Os", 264 "BOOTSTRAP_LLVM_MODE=source", success=False) 265 266 def test_static_host_can_bootstrap_llvm_with_gcc(self): 267 self.mock_host_check() 268 for tool in ("gcc", "g++"): 269 (self.tools / tool).symlink_to(self.fake) 270 self.run_make("toolchain", "LLVM_MODE=static", "BOOTSTRAP_LLVM_MODE=source", "HOST_CC=gcc", "HOST_CXX=g++") 271 work = self.root / "build/work/x86_64-linux-musl" 272 options = json.loads((work / "bootstrap/work/llvm/fixture-config.json").read_text()) 273 self.assertIn("-DCMAKE_C_COMPILER=" + str(self.tools / "gcc"), options) 274 self.assertIn("-DCMAKE_CXX_COMPILER=" + str(self.tools / "g++"), options) 275 self.assertTrue((work / "stamps/llvm").exists()) 276 277 def test_failed_static_host_check_prevents_target_build(self): 278 self.mock_host_check() 279 self.run_make("runtimes", "LLVM_MODE=static", success=False, 280 env={**self.env, "FAKE_STATIC_FAIL": "1"}) 281 work = self.root / "build/work/x86_64-linux-musl" 282 self.assertTrue((work / "bootstrap/work/stamps/runtimes").exists()) 283 self.assertFalse((work / "stamps/llvm").exists()) 284 self.assertFalse((work / "stamps/headers").exists()) 285 self.run_make("runtimes", "LLVM_MODE=static") 286 self.assertTrue((work / "stamps/runtimes").exists()) 287 288 def test_old_system_clang_is_rejected_before_fetch(self): 289 result = self.run_make("runtimes", success=False, 290 env={**self.env, "FAKE_CLANG_VERSION": "20.1.8"}) 291 self.assertIn("requires Clang 21 or newer", result.stdout) 292 self.assertFalse((self.root / "sources").exists()) 293 294 def test_fetch_verifies_and_skips_downloaded_archives(self): 295 self.run_make("pull") 296 self.assertEqual(sum(item["tool"] == "curl" for item in self.commands()), 4) 297 self.run_make("pull", "verify-sources") 298 self.assertEqual(sum(item["tool"] == "curl" for item in self.commands()), 4) 299 300 def test_corrupt_cache_is_repaired(self): 301 self.run_make("pull") 302 (self.root / "sources/musl-test.tar.gz").write_text("corrupt") 303 self.run_make("verify-sources", success=False) 304 self.run_make("pull", "verify-sources") 305 self.assertEqual(sum(item["tool"] == "curl" for item in self.commands()), 5) 306 307 def test_bad_or_failed_download_is_never_published(self): 308 for setting in ("FAKE_CURL_BAD", "FAKE_CURL_FAIL"): 309 with self.subTest(setting=setting): 310 env = {**self.env, setting: "1"} 311 self.run_make("pull", success=False, env=env) 312 self.assertEqual(list((self.root / "sources").iterdir()), []) 313 314 def test_full_driver_resumes_and_rejects_changed_configuration(self): 315 self.run_make("runtimes") 316 first = len([item for item in self.commands() if item["tool"] in ("make", "cmake")]) 317 self.run_make("runtimes", "JOBS=1") 318 second = len([item for item in self.commands() if item["tool"] in ("make", "cmake")]) 319 self.assertEqual(first, second) 320 self.run_make("runtimes", "TARGET_CFLAGS=-Os", success=False) 321 self.run_make("clean") 322 self.assertFalse((self.root / "build/x86_64-linux-musl").exists()) 323 self.assertTrue((self.root / "sources/musl-test.tar.gz").exists()) 324 325 def test_failed_stage_resumes_without_rebuilding_headers(self): 326 self.run_make("runtimes", success=False, env={**self.env, "FAKE_FAIL_STAGE": "builtins"}) 327 self.assertTrue((self.root / "build/work/x86_64-linux-musl/stamps/headers").exists()) 328 self.assertFalse((self.root / "build/work/x86_64-linux-musl/stamps/builtins").exists()) 329 self.run_make("runtimes") 330 self.assertEqual(sum("headers_install" in item["args"] for item in self.commands()), 1) 331 332 def test_source_mode_uses_installed_host_tools(self): 333 self.run_make("runtimes", "LLVM_MODE=source") 334 prefix = self.root / "build/x86_64-linux-musl" 335 self.assertEqual(os.readlink(prefix / "libexec/clang"), "../host/bin/clang") 336 self.assertTrue((prefix / "bin/x86_64-linux-musl-c++").exists()) 337 338 def test_builtins_find_libunwind_headers_before_installation(self): 339 for arch in ("x86_64", "aarch64"): 340 for mode in ("system", "source"): 341 with self.subTest(arch=arch, mode=mode): 342 name = arch + "-" + mode 343 prefix = self.root / "build" / name 344 work = self.root / "build/work" / name 345 self.run_make("builtins", "ARCH=" + arch, "LLVM_MODE=" + mode, 346 "PREFIX=" + str(prefix), "BUILD_DIR=" + str(work), 347 "TARGET_CFLAGS=-Os") 348 self.assertFalse((prefix / "sysroot/usr/include/unwind.h").exists()) 349 options = json.loads((work / "builtins/fixture-config.json").read_text()) 350 directories = [option.split("=", 1)[1] for option in options 351 if option.startswith("-DCMAKE_C_STANDARD_INCLUDE_DIRECTORIES=")] 352 self.assertEqual(len(directories), 1) 353 include = Path(directories[0]) 354 self.assertEqual(include, self.root / "build/src/llvm-test/libunwind/include") 355 for line in (include / "unwind.h").read_text().splitlines(): 356 header = line.removeprefix("#include <").removesuffix(">") 357 self.assertTrue((include / header).is_file(), header) 358 flags = next(option for option in options if option.startswith("-DCMAKE_C_FLAGS=")) 359 self.assertIn("-Os", flags) 360 self.assertIn("--gcc-toolchain=", flags) 361 self.assertIn("-resource-dir=", flags) 362 363 def test_source_patches_apply_once_in_order_and_are_installed(self): 364 self.source_patch("10-second.patch", "first", "second") 365 self.source_patch("00-first.patch", "fixture", "first") 366 result = self.run_make("runtimes") 367 source = self.root / "build/src/llvm-test" 368 self.assertEqual((source / "placeholder").read_text(), "second") 369 self.assertLess(result.stdout.index("Applying 00-first.patch"), result.stdout.index("Applying 10-second.patch")) 370 installed = self.root / "build/x86_64-linux-musl/share/musllvm/patches/llvm/00-first.patch" 371 self.assertEqual(installed.read_bytes(), (self.root / "patches/llvm/00-first.patch").read_bytes()) 372 self.assertNotIn("Applying", self.run_make("runtimes", "JOBS=1").stdout) 373 374 def test_changed_or_removed_patch_rejects_cached_sources(self): 375 patch = self.source_patch("00-fixture.patch") 376 self.run_make("runtimes") 377 commands = sum(item["tool"] in ("make", "cmake") for item in self.commands()) 378 self.source_patch("00-fixture.patch", after="changed") 379 result = self.run_make("runtimes", success=False) 380 self.assertIn("source or patches changed", result.stdout) 381 patch.unlink() 382 result = self.run_make("runtimes", success=False) 383 self.assertIn("source or patches changed", result.stdout) 384 self.assertEqual(sum(item["tool"] in ("make", "cmake") for item in self.commands()), commands) 385 self.assertEqual((self.root / "build/src/llvm-test/placeholder").read_text(), "patched") 386 387 def test_failed_source_patch_does_not_publish_extraction(self): 388 self.source_patch("00-broken.patch", before="missing") 389 result = self.run_make("runtimes", success=False) 390 self.assertIn("could not apply", result.stdout) 391 self.assertFalse((self.root / "build/src/llvm-test").exists()) 392 self.assertFalse((self.root / "build/src/llvm-test.lock").exists()) 393 self.assertTrue((self.root / "sources/llvm-test.tar.gz").exists()) 394 self.assertFalse(any(item["tool"] in ("make", "cmake") for item in self.commands())) 395 396 def test_chimera_runtime_settings_preserve_target_flags(self): 397 for arch in ("x86_64", "aarch64"): 398 with self.subTest(arch=arch): 399 self.run_make("runtimes", "ARCH=" + arch, "TARGET_CFLAGS=-Os") 400 work = self.root / "build/work" / (arch + "-linux-musl") 401 options = json.loads((work / "runtimes/fixture-config.json").read_text()) 402 self.assertIn("-DLIBCXX_HARDENING_MODE=fast", options) 403 self.assertIn("-DLIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION=2", options) 404 calls = [item for item in self.commands() if item["tool"] == "make" and str(work / "atomic") in item["args"]] 405 self.assertEqual(len(calls), 2) 406 for call in calls: 407 flags = next(argument for argument in call["args"] if argument.startswith("CFLAGS=")) 408 self.assertEqual(flags, "CFLAGS=-Os" + (" -mno-outline-atomics" if arch == "aarch64" else "")) 409 410 def test_clean_preserves_unmarked_directory(self): 411 prefix = self.root / "unrelated" 412 prefix.mkdir() 413 sentinel = prefix / "keep" 414 sentinel.write_text("user data") 415 self.run_make("clean", f"PREFIX={prefix}", success=False) 416 self.assertEqual(sentinel.read_text(), "user data") 417 418 def test_concurrent_build_is_rejected(self): 419 self.run_make("runtimes") 420 (self.root / "build/work/x86_64-linux-musl/lock").mkdir() 421 self.run_make("runtimes", success=False) 422 self.run_make("clean", success=False) 423 424 def test_cxx_wrapper_relocation_and_host_isolation(self): 425 wrapper = self.install_wrapper("x86_64-linux-musl-c++", "cc.sh") 426 self.install_capture(wrapper.parent.parent / "libexec/clang") 427 env = {**self.env, "CPATH": "/host/headers", "LIBRARY_PATH": "/host/libraries"} 428 result = subprocess.check_output([str(wrapper), "source with spaces.cpp"], env=env, text=True) 429 invocation = json.loads(result) 430 self.assertIn("--driver-mode=g++", invocation["args"]) 431 self.assertIn(f"--config={wrapper.parent.parent}/share/musllvm/clang.cfg", invocation["args"]) 432 self.assertIn("source with spaces.cpp", invocation["args"]) 433 self.assertIn(f"--sysroot={wrapper.parent.parent}/sysroot", invocation["args"]) 434 self.assertNotIn("CPATH", invocation["env"]) 435 self.assertNotIn("LIBRARY_PATH", invocation["env"]) 436 437 def test_wrapper_omits_default_libraries_for_compile_only_and_freestanding(self): 438 wrapper = self.install_wrapper("x86_64-linux-musl-cc", "cc.sh") 439 self.install_capture(wrapper.parent.parent / "libexec/clang") 440 for option in ("-c", "-E", "-nostdlib", "-nodefaultlibs"): 441 with self.subTest(option=option): 442 invocation = json.loads(subprocess.check_output([str(wrapper), option, "source.c"], env=self.env, text=True)) 443 self.assertNotIn("-latomic", invocation["args"]) 444 445 def test_sysroot_override_applies_to_compiler_and_pkg_config(self): 446 wrapper = self.install_wrapper("x86_64-linux-musl-cc", "cc.sh") 447 self.install_capture(wrapper.parent.parent / "libexec/clang") 448 env = {**self.env, "MUSLLVM_SYSROOT": "/another/sysroot"} 449 invocation = json.loads(subprocess.check_output([str(wrapper), "source.c"], env=env, text=True)) 450 self.assertIn("--sysroot=/another/sysroot", invocation["args"]) 451 pkg_wrapper = self.install_wrapper("x86_64-linux-musl-pkg-config", "pkg-config.sh") 452 self.install_capture(self.tools / "pkg-config") 453 invocation = json.loads(subprocess.check_output([str(pkg_wrapper), "--libs", "target"], env=env, text=True)) 454 self.assertEqual(invocation["env"]["PKG_CONFIG_SYSROOT_DIR"], "/another/sysroot") 455 456 def test_environment_output_is_safe_to_evaluate(self): 457 wrapper = self.install_wrapper("x86_64-linux-musl-env", "env.sh") 458 result = subprocess.check_output(["sh", "-c", 'eval "$("$1")"; printf "%s\\n%s\\n" "$CC" "$CMAKE_TOOLCHAIN_FILE"', 459 "test", str(wrapper)], env=self.env, text=True) 460 self.assertEqual(result.splitlines(), [str(wrapper.parent / "x86_64-linux-musl-cc"), 461 str(wrapper.parent.parent / "share/musllvm/toolchain.cmake")]) 462 463 def test_pkg_config_cannot_see_host_packages(self): 464 wrapper = self.install_wrapper("x86_64-linux-musl-pkg-config", "pkg-config.sh") 465 self.install_capture(self.tools / "pkg-config") 466 env = {**self.env, "PKG_CONFIG_PATH": "/host/packages", "PKG_CONFIG_LIBDIR": "/host/packages"} 467 invocation = json.loads(subprocess.check_output([str(wrapper), "--libs", "target"], env=env, text=True)) 468 self.assertEqual(invocation["env"]["PKG_CONFIG_PATH"], "") 469 self.assertEqual(invocation["env"]["PKG_CONFIG_SYSROOT_DIR"], str(wrapper.parent.parent / "sysroot")) 470 self.assertNotIn("/host/packages", invocation["env"]["PKG_CONFIG_LIBDIR"]) 471 472 473 if __name__ == "__main__": 474 unittest.main()