Skip to content

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

Open
0xull wants to merge 1 commit into
mystor:masterfrom
0xull:cpp-build-track-headers
Open

0xull wants to merge 1 commit 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 (after compiling), cpp_build asks the compiler which files the generated cpp_closures.cpp and every Config::file source read, and prints cargo:rerun-if-changed for each one. how it asks depends on the compiler:

  • gcc and clang run again with the same flags plus -MM -MT cpp_build_deps, and cpp_build parses the make-style output, which leaves out system headers because of -MM.
  • cl.exe runs 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 with /Zs /showIncludes:user, which leaves out system headers by itself. cpp_build recognises clang-cl by its file name, because cc's is_like_clang_cl isn't in the older cc versions cpp_build still supports.

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:

  • i went with a separate pass instead of reading dependency files from the real compile, because finding those files depends on cc's object naming, which changed in cc 1.0.85, and cpp_build supports cc back to 1.0.38. with gcc and clang the extra pass took about 40ms per source in my tests, compared with about 430ms for the compile.
  • i tried msvc's /sourceDependencies first, but it lowercases every path, which breaks the relative paths and the OUT_DIR check, while /showIncludes keeps paths as they are.
  • sources added to a cc::Build before converting it with From aren't tracked, because cc only lists them through get_files, which needs cc 1.0.89. the docs now say to add them with Config::file instead.
  • apple clang lists the sdk's SDKSettings.json as a dependency even with -MM, and i left it in, since an sdk update should rerun the build.

for testing, there are unit tests for both output formats and for splitting INCLUDE. 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, 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 all three systems, and 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.

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