fix(build): keep compile-only flags and their operands off the link line

AR
16h ago
+267-95

wally does not build at all if CXXFLAGS carries a compile-only sysroot.

export SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
export CXXFLAGS="-nostdinc++ -isystem $SDKROOT/usr/include/c++/v1 --sysroot=$SDKROOT"
cargo test
ld: file cannot be mmap()ed, errno=22 (Invalid argument)
    path=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1
clang: error: linker command failed with exit code 1

Every artifact fails. wally, wally-cxx, harness_child, fake_llm, all the integration tests.

Why

CMake folds CXXFLAGS into CMAKE_CXX_FLAGS, and CMake puts that variable on the link line too, not just the compile line. build.rs reads the kit's link line back through the CMake file API and filters it, because those flags are fatal handed to ld. That filter is why is_compile_only exists.

The filter works one argument at a time. split_fragment returns -isystem and its directory as two separate arguments, so -isystem got dropped and the bare directory stayed. A bare directory argument is a link input. The linker tries to mmap it.

Two spellings were also missing from the predicate:

FlagWhy it leaked
--sysroot=<dir>the value lives inside the argument, so nothing pairs with it
-nostdinc / -nostdinc++no operand at all, so nothing pairs with it either

Fix

link_args_from walks a fragment. For any flag whose value arrives as the next argument, it drops both. Everything else goes through is_compile_only, which now also knows --sysroot and -nostdinc.

The filters moved to build_link_args.rs. They cannot stay in build.rs and be tested, because cargo test never runs a build script's own #[cfg(test)] module. Tests written there would be dead code dressed up as coverage. build.rs includes the file and the test compiles it as a module, so one implementation feeds both.

Tests

Seven cases in tests/test_wally_build_link_args.rs:

  • the exact CXXFLAGS above, which must reduce to nothing
  • one case per operand-taking spelling: -isystem, -I, -F, -iframework, -iquote, -idirafter. A missing spelling is the bug, so each one is pinned.
  • --sysroot=<dir> dropped on its own
  • -nostdinc and -nostdinc++
  • link arguments that must survive: -Wl,-rpath,, static archives, -framework, -lc++, -Wl,-force_load,
  • a quoted include path arriving through split_fragment whole
  • MSVC's linker /D switches not mistaken for defines

Verified

$ cargo test            # with CXXFLAGS still exported
test result: ok. 630 passed; 0 failed
test result: ok. ...    # every integration suite, 0 failed

$ cargo clippy --all-targets -- -D warnings
Finished `dev` profile, no warnings

$ python3 scripts/test/test-ledger.py
147/149 C++ cases ported, 2 excepted, 0 missing

$ bash scripts/ci/check-agents-sync.sh    # ok
$ python3 scripts/ci/check-versions.py    # versions.toml consistent

Scope

One file moved, one predicate tightened, tests. A build whose CMAKE_CXX_FLAGS is clean behaves exactly as before. The linker was already getting none of these flags; this stops it getting their operands too.


Summary by cubic

Fixes broken builds when CXXFLAGS carries a compile-only sysroot, such as -nostdinc++ -isystem <sdk>/usr/include/c++/v1 --sysroot=<sdk>.

CMake folds CXXFLAGS into CMAKE_CXX_FLAGS, which ends up on the link line too. The build.rs filter dropped -isystem but kept its directory, which the linker reads as an input file and dies on with ld: file cannot be mmap()ed, errno=22.

Bug Fixes

  • Operand-taking flags (-isystem, -I, -F, -iframework, -iquote, -idirafter) now drop their next argument as well.
  • --sysroot=<dir> and -nostdinc/-nostdinc++ are now recognized as compile-only.
  • The filter moved to build_link_args.rs so cargo test can exercise it — build scripts' own test modules never run under cargo test.

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of build configuration flags across GCC- and MSVC-based environments, helping ensure linker options are retained while compiler-only options are excluded.
    • Corrected parsing of quoted paths and options that take a separate value, improving builds that use these arguments.
  • Tests

    • Added regression coverage for compiler and linker flag handling across supported formats.