File size: 24,400 Bytes
5f25733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
"""Check native build preconditions and subprocess failure propagation."""

import json
import os
from pathlib import Path
import platform
import shlex
import shutil
import subprocess
import sys
import tempfile
from types import SimpleNamespace
import unittest
from unittest.mock import patch

from build_native import NativeBuild, validate_target


class NativeBuildTests(unittest.TestCase):
    def setUp(self):
        temporary = tempfile.TemporaryDirectory()
        self.addCleanup(temporary.cleanup)
        self.root = Path(temporary.name)
        machine = platform.machine().lower()
        architecture = {"arm64": "aarch64", "amd64": "x86_64"}.get(machine, machine)
        suffix = {
            "Darwin": "apple-darwin",
            "Linux": "unknown-linux-gnu",
            "Windows": "pc-windows-msvc",
        }[platform.system()]
        self.args = SimpleNamespace(
            target=f"{architecture}-{suffix}",
            deployment_target="11.0",
            output=self.root / "build output",
            archives=self.root / "archives",
            cc=Path(sys.executable),
            cxx=Path(sys.executable),
            cmake=Path(sys.executable),
            make=Path(sys.executable),
            pkg_config=Path(sys.executable),
            shell=Path(sys.executable),
            bootstrap_make=None,
            jobs=2,
        )
        self.environment = {
            **os.environ,
            "INCLUDE": "fixture include",
            "LIB": "fixture lib",
        }

    def test_cli_entrypoint_imports_its_sibling_with_safe_path_enabled(self):
        result = subprocess.run(
            [
                sys.executable,
                "-P",
                str(Path(__file__).with_name("build_native.py")),
                "--help",
            ],
            cwd=self.root,
            capture_output=True,
            text=True,
            check=False,
        )
        self.assertEqual(result.returncode, 0, result.stderr)
        self.assertIn("--archives", result.stdout)

    def test_rejects_cross_host_and_musl_builds(self):
        for target, system, machine, libc in [
            ("x86_64-pc-windows-msvc", "Darwin", "x86_64", ""),
            ("aarch64-apple-darwin", "Darwin", "x86_64", ""),
            ("x86_64-unknown-linux-gnu", "Linux", "x86_64", "musl"),
            ("x86_64-unknown-linux-musl", "Linux", "x86_64", "musl"),
        ]:
            with self.subTest(target=target, system=system):
                with self.assertRaises(ValueError):
                    validate_target(target, system, machine, libc, "11.0")

    def test_requires_explicit_macos_deployment_target(self):
        with self.assertRaisesRegex(ValueError, "Declare the macOS deployment target"):
            validate_target("aarch64-apple-darwin", "Darwin", "arm64", "", None)

    def test_missing_tool_does_not_create_output(self):
        self.args.cc = self.root / "missing compiler"
        with self.assertRaisesRegex(ValueError, "Missing build tool"):
            NativeBuild(self.args, self.environment)
        self.assertFalse(self.args.output.exists())

    @unittest.skipIf(os.name == "nt", "Exercises the Unix clang++ driver symlink")
    def test_bootstrap_links_cpp_runtime_through_compiler_symlink(self):
        tools = {name: shutil.which(name) for name in ("clang", "cmake", "make")}
        if not all(tools.values()):
            self.skipTest("Requires clang, CMake and make")
        compiler = self.root / "clang++"
        compiler.symlink_to(Path(tools["clang"]).resolve())
        self.args.cc = Path(tools["clang"])
        self.args.cxx = compiler
        self.args.cmake = Path(tools["cmake"])
        self.args.make = Path(tools["make"])
        headers = self.root / "declared headers"
        headers.mkdir()
        (headers / "build_flag.h").write_text(
            '#define BUILD_FLAG "linked with flags"\n'
        )
        self.args.cxx_flag = [f"-I{headers}"]
        for name in ("ar", "ranlib"):
            tool = shutil.which(name)
            if tool is None:
                self.skipTest(f"Requires {name}")
            wrapper = self.root / f"declared {name}"
            wrapper.write_text(
                "#!/bin/sh\n"
                f"touch {shlex.quote(str(self.root / (name + '.used')))}\n"
                f'exec {shlex.quote(tool)} "$@"\n'
            )
            wrapper.chmod(0o755)
            setattr(self.args, name, wrapper)
        source = self.root / "cpp-source"
        source.mkdir()
        (source / "CMakeLists.txt").write_text(
            "cmake_minimum_required(VERSION 3.15)\n"
            "project(cpp_driver LANGUAGES CXX)\n"
            "add_library(message STATIC message.cpp)\n"
            "add_executable(cpp_driver main.cpp)\n"
            "target_link_libraries(cpp_driver PRIVATE message)\n"
            "install(TARGETS cpp_driver DESTINATION bin)\n"
        )
        (source / "main.cpp").write_text(
            "#include <iostream>\nconst char* message();\nint main() { std::cout << message(); }\n"
        )
        (source / "message.cpp").write_text(
            '#include "build_flag.h"\nconst char* message() { return BUILD_FLAG; }\n'
        )
        build = NativeBuild(self.args, self.environment)
        build.output.mkdir()
        build.sources = {"cpp-driver": source}
        build.cmake("cpp-driver", [], bootstrap=True)
        result = subprocess.run(
            [build.tools / "bin" / "cpp_driver"],
            check=True,
            capture_output=True,
            text=True,
        )
        self.assertEqual(result.stdout, "linked with flags")
        self.assertTrue(
            all((self.root / (name + ".used")).is_file() for name in ("ar", "ranlib"))
        )

    @unittest.skipIf(os.name == "nt", "Exercises Autoconf's Unix tool expansion")
    def test_libffi_rejects_quoted_archive_tool_paths(self):
        for name in ("ar", "ranlib"):
            for index, path in enumerate(("declared tool", "declared'tool")):
                with self.subTest(name=name, path=path):
                    (self.root / path).touch()
                    setattr(self.args, name, self.root / path)
                    self.args.output = self.root / f"{name}-{index}"
                    build = NativeBuild(self.args, self.environment)
                    with (
                        patch(
                            "build_native.prepare_sources",
                            side_effect=lambda *args: (build.output / "build").mkdir(),
                        ),
                        patch.object(build, "cmake"),
                        patch.object(build, "meson"),
                        patch.object(build, "run"),
                        self.assertRaisesRegex(ValueError, "libffi .* path"),
                    ):
                        build.build()
                    setattr(self.args, name, None)

    @unittest.skipIf(os.name == "nt", "Exercises Autoconf's Unix flag expansion")
    def test_libffi_compiler_receives_literal_defines_without_shell_quotes(self):
        compiler = shutil.which("cc")
        if compiler is None:
            self.skipTest("Requires a C compiler")
        self.args.cc = Path(compiler)
        make = shutil.which("make")
        if make is None:
            self.skipTest("Requires Make")
        for index, (flag, expected) in enumerate(
            [
                ('-DBUILD_FLAG="redacted"', "redacted\n"),
                (r'-DBUILD_FLAG="C:\\voice"', "C:\\voice\n"),
                ('-DBUILD_FLAG="two words"', None),
            ]
        ):
            with self.subTest(flag=flag):
                self.args.c_flag = [flag]
                self.args.output = self.root / str(index)
                build = NativeBuild(self.args, self.environment)
                with (
                    patch(
                        "build_native.prepare_sources",
                        side_effect=lambda *args: (build.output / "build").mkdir(),
                    ),
                    patch.object(build, "cmake"),
                    patch.object(build, "meson"),
                    patch.object(build, "run") as run,
                ):
                    if " " in flag:
                        with self.assertRaisesRegex(ValueError, "libffi flags"):
                            build.build()
                        continue
                    build.build()
                environment = next(
                    call.kwargs["environment"]
                    for call in run.call_args_list
                    if call.args[0] == "libffi-configure"
                )
                (build.output / "probe.c").write_text(
                    "#include <stdio.h>\nint main(void) { puts(BUILD_FLAG); }\n"
                )
                subprocess.run(
                    ["/bin/sh", "-c", "$CC $CFLAGS probe.c -o probe"],
                    cwd=build.output,
                    env=environment,
                    check=True,
                )
                result = subprocess.run(
                    [build.output / "probe"], check=True, capture_output=True, text=True
                )
                self.assertEqual(result.stdout, expected)
                (build.output / "Makefile").write_text(
                    f"CC={compiler}\nCFLAGS={environment['CFLAGS']} -fexceptions\n"
                    "MAKEOVERRIDES=\nall:\n\t$(MAKE) nested\n"
                    "nested:\n\t$(CC) $(CFLAGS) probe.c -o probe\n\t./probe\n"
                )
                result = subprocess.run(
                    [make, "--silent"],
                    cwd=build.output,
                    check=True,
                    capture_output=True,
                    text=True,
                )
                self.assertEqual(result.stdout, expected)

    @unittest.skipIf(os.name == "nt", "Unix install paths; Windows layout is unchanged")
    def test_cmake_installs_relative_library_paths(self):
        for argument, tool in (
            ("cc", "cc"),
            ("cxx", "c++"),
            ("cmake", "cmake"),
            ("make", "make"),
        ):
            setattr(self.args, argument, Path(shutil.which(tool)))
        source = self.root / "library-source"
        source.mkdir()
        self.args.c_flag = ["-DBUILD_FLAG=42"]
        self.args.link_flag = ["-Wl,-rpath,declared-relative-path"]
        (source / "CMakeLists.txt").write_text(
            "cmake_minimum_required(VERSION 3.15)\nproject(relative_paths LANGUAGES C)\n"
            "add_library(fixture SHARED fixture.c)\ninstall(TARGETS fixture DESTINATION lib)\n"
        )
        (source / "fixture.c").write_text("int fixture(void) { return BUILD_FLAG; }\n")
        build = NativeBuild(self.args, self.environment)
        build.output.mkdir()
        build.sources = {"fixture": source}
        build.cmake("fixture", [], bootstrap=True)
        if sys.platform == "darwin":
            from macos_runtime import inspect

            metadata = inspect(build.tools / "lib/libfixture.dylib", self.args.target)
            self.assertEqual(
                (metadata.identity, metadata.rpaths),
                ("@rpath/libfixture.dylib", ("declared-relative-path", "@loader_path")),
            )
        else:
            from linux_runtime import inspect

            metadata = inspect(build.tools / "lib/libfixture.so", self.args.target)
            self.assertEqual(
                (metadata.identity, metadata.rpaths),
                ("libfixture.so", ("29=declared-relative-path:$ORIGIN",)),
            )

    def test_build_refuses_existing_output_before_reading_sources(self):
        self.args.output.mkdir()
        (self.args.output / "keep").write_text("untouched")
        with self.assertRaises(FileExistsError):
            NativeBuild(self.args, self.environment).build()
        self.assertEqual(
            {p.name: p.read_text() for p in self.args.output.iterdir()},
            {"keep": "untouched"},
        )

    def test_failure_retains_log_and_state_without_completion_marker(self):
        build = NativeBuild(self.args, self.environment)
        build.output.mkdir()
        command = [
            sys.executable,
            "-c",
            "print('synthetic failure'); raise SystemExit(23)",
        ]
        with self.assertRaises(subprocess.CalledProcessError) as error:
            build.run("failure", command)
        self.assertEqual(error.exception.returncode, 23)
        self.assertEqual(
            (build.output / "failure.log").read_text().strip(), "synthetic failure"
        )
        self.assertEqual(
            json.loads((build.output / "build-state.json").read_text()),
            {
                "target": self.args.target,
                "deployment_target": "11.0",
                "flags": {"CFLAGS": [], "CXXFLAGS": [], "LDFLAGS": []},
                "steps": [{"name": "failure", "command": command, "exit_code": 23}],
            },
        )
        self.assertFalse((build.output / "built.json").exists())

    def test_ambient_native_discovery_variables_are_not_inherited(self):
        inherited = {
            **self.environment,
            "PKG_CONFIG_PATH": "/ambient",
            "CMAKE_PREFIX_PATH": "/ambient",
            "CPATH": "/ambient",
            "CFLAGS": "-I/ambient",
            "LDFLAGS": "-L/ambient",
        }
        environment = NativeBuild(self.args, inherited).environment
        self.assertFalse(any("/ambient" in value for value in environment.values()))
        self.assertEqual(environment["PKG_CONFIG_PATH"], "")

    def test_meson_receives_private_link_inputs_with_spaces(self):
        if platform.system() == "Darwin":
            self.args.cc = Path(shutil.which("cc"))
        self.args.c_flag = [] if os.name == "nt" else ["-DDECLARED_C=1"]
        self.args.cxx_flag = [] if os.name == "nt" else ["-DDECLARED_CXX=1"]
        self.args.link_flag = (
            [] if os.name == "nt" else ["-Ldeclared input with spaces"]
        )
        build = NativeBuild(self.args, self.environment)
        self.assertEqual(
            build.record["flags"],
            {
                "CFLAGS": self.args.c_flag,
                "CXXFLAGS": self.args.cxx_flag,
                "LDFLAGS": self.args.link_flag,
            },
        )
        build.sources = {"meson": self.root / "meson", "glib": self.root / "glib"}
        with patch.object(build, "run"):
            build.meson("glib", [])
        if platform.system() == "Darwin":
            subprocess.run(
                [
                    build.environment["OBJC"],
                    *shlex.split(build.environment["OBJCFLAGS"]),
                    "-x",
                    "objective-c",
                    "-fsyntax-only",
                    "-",
                ],
                input="#if DECLARED_C != 1\n#error missing declared flags\n#endif\n",
                text=True,
                check=True,
            )
        quote = subprocess.list2cmdline if build.windows else shlex.join
        include = f"{'/I' if build.windows else '-I'}{build.prefix / 'include'}"
        self.assertEqual(
            {name: build.environment[name] for name in ("CFLAGS", "CXXFLAGS")},
            {
                "CFLAGS": quote([*self.args.c_flag, include]),
                "CXXFLAGS": quote([*self.args.cxx_flag, include]),
            },
        )
        if build.windows:
            self.assertEqual(
                build.environment["LDFLAGS"],
                quote([*self.args.link_flag, f"/LIBPATH:{build.prefix / 'lib'}"]),
            )
        else:
            self.assertEqual(
                shlex.split(build.environment["LDFLAGS"]),
                [
                    *self.args.link_flag,
                    f"-L{build.prefix / 'lib'}",
                    (
                        "-Wl,-rpath,$ORIGIN:$ORIGIN/.."
                        if build.args.target.endswith("unknown-linux-gnu")
                        else f"-Wl,-rpath,{build.prefix / 'lib'}"
                    ),
                ],
            )

    def test_windows_paths_use_cygpath_and_propagate_failure(self):
        build = NativeBuild(self.args, self.environment)
        build.windows = True
        path = Path("D:/build output/source")
        with patch(
            "build_native.subprocess.check_output",
            return_value="/cygdrive/d/build output/source\n",
        ) as convert:
            self.assertEqual(build.posix_path(path), "/cygdrive/d/build output/source")
        self.assertEqual(
            convert.call_args.args[0],
            [build.toolchain["shell"].with_name("cygpath.exe"), "-u", str(path)],
        )
        with patch(
            "build_native.subprocess.check_output",
            side_effect=subprocess.CalledProcessError(1, "cygpath"),
        ):
            with self.assertRaises(subprocess.CalledProcessError):
                build.posix_path(path)

    def test_windows_rejects_unix_overrides_before_creating_output(self):
        self.args.target = "x86_64-pc-windows-msvc"
        for name, value in (
            ("c_flag", [r"/IC:\SDK\include"]),
            ("ranlib", Path(sys.executable)),
        ):
            with patch("build_native.validate_target"):
                setattr(self.args, name, value)
                with self.assertRaisesRegex(ValueError, "Unix build host"):
                    NativeBuild(self.args, self.environment)
                delattr(self.args, name)
            self.assertFalse(self.args.output.exists())

    def test_windows_recipes_use_explicit_targets_and_posix_paths(self):
        self.environment["USERPROFILE"] = str(self.root / "user profile")
        for architecture, flag in (("x86_64", "-m64"), ("aarch64", "-marm64")):
            with self.subTest(architecture=architecture):
                self.args.target = f"{architecture}-pc-windows-msvc"
                self.args.output = self.root / architecture
                with (
                    patch("build_native.platform.system", return_value="Windows"),
                    patch("build_native.platform.machine", return_value=architecture),
                ):
                    build = NativeBuild(self.args, self.environment)
                self.assertEqual(
                    build.environment["USERPROFILE"], self.environment["USERPROFILE"]
                )
                calls = {}

                def record(name, command, **kwargs):
                    calls[name] = (command, kwargs)
                    (build.output / f"{name}.log").write_text(
                        "-ID:/private/include -LD:/private/lib -lffi\n"
                    )

                with (
                    patch(
                        "build_native.prepare_sources",
                        side_effect=lambda *args: (build.output / "build").mkdir(),
                    ),
                    patch.object(build, "cmake") as cmake,
                    patch.object(build, "meson"),
                    patch.object(build, "run", side_effect=record),
                    patch.object(
                        build,
                        "posix_path",
                        side_effect=lambda path: "/cygdrive/d/" + path.name,
                    ),
                    patch(
                        "build_native.subprocess.check_output",
                        return_value="/usr/share/automake-1.18\n",
                    ),
                ):
                    build.build()
                opus_options = next(
                    call.args[1]
                    for call in cmake.call_args_list
                    if call.args[0] == "opus"
                )
                self.assertEqual(
                    "-DOPUS_PRESUME_NEON=ON" in opus_options, architecture == "aarch64"
                )
                command, kwargs = calls["libffi-configure"]
                host = f"{architecture}-w64-mingw32"
                linker_flags = "-no-undefined -Wc,-link,/IMPLIB:.libs/libffi.lib"
                self.assertEqual(
                    command,
                    [
                        build.toolchain["shell"],
                        "/cygdrive/d/configure",
                        "--prefix=/cygdrive/d/prefix",
                        "--enable-shared",
                        "--disable-static",
                        "--disable-docs",
                        f"--build={host}",
                        f"--host={host}",
                    ],
                )
                expected = {
                    **build.environment,
                    "CC": f"/cygdrive/d/msvcc.sh {flag}",
                    "CXX": f"/cygdrive/d/msvcc.sh {flag}",
                    "AR": "/usr/share/automake-1.18/ar-lib lib",
                    "RANLIB": ":",
                    "LD": "link",
                    "NM": "dumpbin -symbols",
                    "STRIP": ":",
                    "LDFLAGS": "-no-undefined",
                    "AM_MAKEFLAGS": shlex.quote(f"LTLDFLAGS={linker_flags}"),
                    "CPP": "cl -nologo -EP",
                    "CXXCPP": "cl -nologo -EP",
                    "CPPFLAGS": "-DFFI_BUILDING_DLL",
                    "CONFIG_SHELL": "/cygdrive/d/" + build.toolchain["shell"].name,
                }
                self.assertEqual(
                    kwargs,
                    {"cwd": build.output / "build/libffi", "environment": expected},
                )
                self.assertEqual(calls["libffi-install"][1]["environment"], expected)
                if platform.system() == "Windows":
                    cygwin = os.environ.get("VOICE_CYGWIN_ROOT")
                    make = str(Path(cygwin) / "bin/make.exe") if cygwin else None
                else:
                    make = shutil.which("make")
                if make is None:
                    self.skipTest("GNU make is required for the recursive build check")
                # Mirror libffi's MAKEOVERRIDES reset and recursive hook, without
                # running a compiler or requiring the native source archives.
                directory = build.output / "build/libffi"
                (directory / "Makefile").write_text(
                    "MAKEOVERRIDES =\nLTLDFLAGS = default\n"
                    "all:\n\t@$(MAKE) --no-print-directory $(AM_MAKEFLAGS) nested\n"
                    "nested:\n\t@$(MAKE) --no-print-directory $(AM_MAKEFLAGS) observe\n"
                    "observe:\n\t@printf '%s\\n' \"$(LTLDFLAGS)\"\n"
                )
                result = subprocess.run(
                    [make, "--no-print-directory"],
                    cwd=directory,
                    env={
                        **os.environ,
                        "AM_MAKEFLAGS": kwargs["environment"]["AM_MAKEFLAGS"],
                        "PATH": os.pathsep.join(
                            [str(Path(make).parent), os.environ.get("PATH", "")]
                        ),
                    },
                    capture_output=True,
                    text=True,
                    check=True,
                )
                self.assertEqual(
                    result.stdout.strip(),
                    linker_flags,
                )

    def test_explicit_windows_inputs_replace_ambient_sdk_and_search_paths(self):
        self.args.target = "x86_64-pc-windows-msvc"
        self.args.windows_build_inputs = self.root / "inputs.json"
        selected = {
            "PATH": str(self.root / "selected tools"),
            "INCLUDE": str(self.root / "selected include"),
            "LIB": str(self.root / "selected lib"),
        }
        with (
            patch("build_native.validate_target"),
            patch(
                "build_native.build_environment",
                return_value=(selected, {"target": self.args.target}),
            ),
        ):
            build = NativeBuild(
                self.args,
                {"PATH": "/unrelated", "LIBPATH": "/unrelated"},
            )
        self.assertFalse(
            any("/unrelated" in value for value in build.environment.values())
        )
        self.assertEqual(
            {name: build.environment[name] for name in ("INCLUDE", "LIB")},
            {name: selected[name] for name in ("INCLUDE", "LIB")},
        )
        self.assertEqual(
            build.record["windows_build_inputs"], {"target": self.args.target}
        )
        self.assertFalse(build.output.exists())