Skip to content

cpp_build: track C++ headers and Config::file sources - #119

Merged
ogoffart merged 2 commits into
mystor:masterfrom
0xull:cpp-build-track-headers
Nov 30, 2026
Merged

ogoffart merged 2 commits into
mystor:masterfrom
0xull:cpp-build-track-headers

Conversation

@0xull

@0xull 0xull commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

this is a follow-up to #118, which made cargo rerun the build script when a rust file with cpp! macros changes. headers included from those macros and sources added with Config::file still aren't tracked, though, so editing one of them keeps the old generated library until something else triggers a rerun. that's why the docs told users to print cargo:rerun-if-changed for them by hand.

with this change, cpp_build finds out which files the generated cpp_closures.cpp and every other source it compiles read, and prints cargo:rerun-if-changed for each one. how it finds out depends on the compiler:

  • gcc and clang compile with -MMD -MT cpp_build_deps added, which writes a make-style .d file next to each object, and cpp_build parses those after the build. -MMD leaves out system headers.
  • cl.exe runs a second time with /Zs /showIncludes, which only checks the syntax and prints a Note: including file: line for each header. since it can't leave out system headers on its own, cpp_build skips the ones under the INCLUDE directories. it also sets VSLANG=1033 so that output stays in english, as rustc and cc already do, and passes sources other than .c with -Tp, like cc does for .cc, so cl.exe doesn't mistake them for object files.
  • clang-cl runs a second time with /Zs /showIncludes:user, which leaves out system headers by itself.

files in OUT_DIR are skipped, paths inside the package stay relative, and a file that several sources include is only printed once. paths that don't exist are never printed, because cargo would rerun the build script on every build for them. and as with #118, nothing is printed when cargo_metadata(false) is set, and if the compiler can't list the files, the behaviour is the same as today.

a few notes on my work here:

  • to find the .d files, cpp_build needs the object paths. cc 1.6 added try_create_archive and try_emit_link_directives, which is all try_compile does after compiling the objects, so cpp_build calls try_compile_intermediates and those two itself and keeps the paths. that raises the minimum cc from 1.0.38 to 1.6, which still builds on rust 1.65, so cpp's msrv stays at 1.71.
  • i tried msvc's /sourceDependencies first, but it lowercases the header paths, which breaks the relative paths and the OUT_DIR check, while /showIncludes keeps paths as they are. clang-cl ignores /sourceDependencies anyway, so both msvc compilers share the /showIncludes pass.
  • sources added to a cc::Build before converting it with From are tracked now too, so the docs no longer ask users to add them with Config::file.

for testing, there are unit tests for both output formats and for splitting INCLUDE, and a test in test/ with a cpp! block that includes a nested header. i also wrote a script that builds a throwaway crate with a nested header, a header outside the crate, a header in a non-ascii folder, a Config::file source that shares a header, a source added to the cc::Build before the conversion, and a header behind #ifdef, and then edits each one. i ran it in a workflow on my fork on linux (gcc, clang), macos (apple clang) and windows (cl.exe, visual studio's clang-cl, mingw gcc), and every edit rebuilt with the new value, no-op builds stayed fresh, and editing the #ifdef-ed header didn't rerun anything. for comparison, the released 0.5.12 from crates.io kept printing the old values after every edit, because its build script never reran. the same workflow also passed the test suite on stable and 1.71 on linux and windows, but github never gave the macos test jobs a runner. ccache g++ worked locally too.

Signed-off-by: Ikeh Chukwuka <mrikehchukwuka@gmail.com>
@ogoffart

Copy link
Copy Markdown
Collaborator

Thanks!

My main concern is that this runs the compiler a second time just to get the dependencies. It would be better to get them from the real compile: -MMD for GCC/Clang (the .d files land next to the objects), and /sourceDependencies for cl.exe, keeping /showIncludes as a fallback for clang-cl.

A test in test/ that includes a header from a cpp! block would be nice.

@0xull

0xull commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

thank you, that makes sense, so i'll rework it the way you suggested. since /sourceDependencies lowercases every path, i'll also make the OUT_DIR and package checks case-insensitive for cl.exe.

the part i still need to work out is finding the .d files, because they're named after cc's object files and cc changed that naming in 1.0.85. i'll try looking for them in OUT_DIR first, so it keeps working with every cc version cpp_build supports, and if asking cc for the object paths turns out cleaner, i'll check with you before raising the minimum cc version.

i'll add the test too.

Signed-off-by: Ikeh Chukwuka <mrikehchukwuka@gmail.com>
@0xull

0xull commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

i pushed the rework. gcc and clang now compile with -MMD -MT cpp_build_deps, writing a .d file next to each object, and cpp_build reads those after the build instead of running the compiler a second time.

i did try scanning OUT_DIR for .d files first, like i said i would, but cc never cleans up stale objects, so if a source file gets renamed or dropped from Config::file, its .d file stays behind and cpp_build would keep tracking its headers. getting the object paths directly from cc turned out cleaner. cc 1.6 added try_create_archive and try_emit_link_directives, which is all try_compile does after compiling the objects, so cpp_build now calls try_compile_intermediates and those two itself and holds onto the object paths.

that's the cc bump i mentioned i'd check with you on. the minimum goes from 1.0.38 to 1.6, and since cc 1.6 still builds on rust 1.65, cpp's msrv stays at 1.71. does that work for you?

for cl.exe, i kept the /showIncludes pass rather than switching to /sourceDependencies. clang-cl needs that pass anyway, so both msvc compilers share one code path. using /sourceDependencies would have meant adding a json parser, the case-insensitive checks i mentioned earlier, and a version check for older cl.exe releases since the flag only exists from vs 2019 16.7. if you'd still prefer /sourceDependencies for cl.exe, i can add it.

i also added a test in test/ with a cpp! block that includes a nested header (a header that includes another header), and on gcc and clang it verifies that both end up in the .d files.

the checks on my fork pass across linux, macos, and windows with g++, clang++, cl.exe, clang-cl, and mingw g++. the two macos test jobs never picked up a runner from github, though the macos scenario itself passed.

@ogoffart

Copy link
Copy Markdown
Collaborator

Bumping msrv is no problem

@ogoffart
ogoffart merged commit 8e92446 into mystor:master Nov 30, 2026
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants