Skip to content

Using dep files in "default" C++ toolchains - #1005

Closed
Overhatted wants to merge 1 commit into
meta:mainfrom
Overhatted:depFilesInDefaultCxxToolchain
Closed

Overhatted wants to merge 1 commit into
meta:mainfrom
Overhatted:depFilesInDefaultCxxToolchain

Conversation

@Overhatted

@Overhatted Overhatted commented Jun 18, 2025 •

Copy link
Copy Markdown
Contributor

Since it's such an important feature, I think the default should be true. Else some users might be unimpressed by Buck2's performance.
Let me know what you think.
I'm still testing to make sure it works:

  • Linux default Clang
  • Linux (compiler_type = "gcc", compiler = "gcc", cxx_compiler = "g++")
  • Windows default MSVC
  • Windows Clang (compiler_type = "clang", compiler = "clang.exe", cxx_compiler = "clang.exe", linker = "lld-link.exe", link_flags = ["libcmt.lib"])
  • Windows Clang (compiler_type = "clang_windows", compiler = "clang.exe", cxx_compiler = "clang.exe", linker = "lld-link.exe", link_flags = ["libcmt.lib"])
  • Windows Clang CL (compiler = "clang-cl.exe", cxx_compiler = "clang-cl.exe", linker = "lld-link.exe")

I don't have any apple devices.

I tested using the hello_world example. I added another .hpp, built :main, changed the new .hpp, built :main and then checked that nothing was built using buck2 log what-ran.

I didn't test using the buck2 audit dep-files TARGET CATEGORY IDENTIFIER command mentioned in https://buck2.build/docs/rule_authors/dep_files/ because I wasn't able to figure out what should be the CATEGORY or IDENTIFIER.

…olchain

Since it's such an important feature, I think the default should be true. Else some users might be unimpressed by Buck2's performance.
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 18, 2025
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. (Because this pull request was imported automatically, there will not be any future comments.)

@Overhatted
Overhatted marked this pull request as ready for review June 21, 2025 15:28
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@alexlian merged this pull request in b179908.

facebook-github-bot pushed a commit to meta/buck2-prelude that referenced this pull request Jul 1, 2025
Summary:
Since it's such an important feature, I think the default should be true. Else some users might be unimpressed by Buck2's performance.
Let me know what you think.
I'm still testing to make sure it works:
- [ ] Linux GCC
- [ ] Linux compiler_type=clang
- [ ] Windows MSVC
- [ ] Windows compiler_type=clang
I don't have any apple devices.

X-link: meta/buck2#1005

Reviewed By: JakobDegen

Differential Revision: D76913821

Pulled By: alexlian

fbshipit-source-id: ab6ad59e5de19dc25f66843da21443b62d3f56bb
@Overhatted
Overhatted deleted the depFilesInDefaultCxxToolchain branch November 27, 2026 13:45
meta-codesync Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 28, 2026
Summary:
`system_cxx_toolchain` (and `cxx_tools_info_toolchain`) with `compiler_type = "clang"` tracks C/C++ dependencies with the `show_headers` mode, `clang -H`. clang does not print the headers it reads through `-include` or anything they include, and it does not print the files of `#embed`. The rules pass `prefix_header`, and a `precompiled_header` that is not compiled, as `-include <header>` tagged with the headers tag. The dep file leaves these headers out, so Buck2 treats them as unused, and an edit to one of them is a dep file cache hit that keeps the old object.

This PR switches `clang` to the `makefile` mode, as gcc already uses: `clang -MD` lists all of them.

## Affected version

origin/main 00c2ee4f74. The mapping in `prelude/toolchains/cxx.bzl` has been in place since b179908fbd (meta/buck2#1005).

## Reproduction

```
mkdir inc
printf '#pragma once\nint inner;\n' > inc/inner.h
printf '#pragma once\n#include "inner.h"\nint p;\n' > inc/prefix.h
printf '#pragma once\nint q;\n' > inc/normal.h
printf '#include "normal.h"\nint main(){return q+p+inner;}\n' > b.c
python3 prelude/cxx/tools/dep_file_processor.py show_headers b.c dep.txt clang -Iinc -include inc/prefix.h -H -fno-color-diagnostics -c b.c -o b.o
cat dep.txt
```

Through Buck2, with the e2e fixture of this PR and the prelude of origin/main: build `root//:lib[objects][r.c.o]`, edit `pi.h` (included by the prefix header `p.h`), and build again.

## Expected

The dep file lists `inc/prefix.h` and `inc/inner.h`, as `dep_file_processor.py makefile` does for the same compile with `-MD`. The second build compiles again.

## Actual

```
inc/normal.h
b.c
```

`buck2 audit dep-files root//:lib c_compile r.c` lists neither `p.h` nor `pi.h`, and `buck2 log what-ran` shows the second build as `LocalDepFileCache`. With this PR, both headers are listed and the compile runs again (`Local`).

## Notes

- `clang_cl` and `clang_windows` keep their modes. meta/buck2#1656 changes the `clang_cl` line of the same block, so whichever lands second needs a trivial rebase.
- With `makefile` mode, clang compiles also pick up the `\#` / `$$` unescaping of meta/buck2#1657.

X-link: meta/buck2#1665

Reviewed By: dtolnay

Differential Revision: D134817891

fbshipit-source-id: 7a5415dc4ca4385c3291fb61d0d7e48921610f9d
meta-codesync Bot pushed a commit that referenced this pull request Nov 28, 2026
Summary:
`system_cxx_toolchain` (and `cxx_tools_info_toolchain`) with `compiler_type = "clang"` tracks C/C++ dependencies with the `show_headers` mode, `clang -H`. clang does not print the headers it reads through `-include` or anything they include, and it does not print the files of `#embed`. The rules pass `prefix_header`, and a `precompiled_header` that is not compiled, as `-include <header>` tagged with the headers tag. The dep file leaves these headers out, so Buck2 treats them as unused, and an edit to one of them is a dep file cache hit that keeps the old object.

This PR switches `clang` to the `makefile` mode, as gcc already uses: `clang -MD` lists all of them.

## Affected version

origin/main 00c2ee4. The mapping in `prelude/toolchains/cxx.bzl` has been in place since b179908 (#1005).

## Reproduction

```
mkdir inc
printf '#pragma once\nint inner;\n' > inc/inner.h
printf '#pragma once\n#include "inner.h"\nint p;\n' > inc/prefix.h
printf '#pragma once\nint q;\n' > inc/normal.h
printf '#include "normal.h"\nint main(){return q+p+inner;}\n' > b.c
python3 prelude/cxx/tools/dep_file_processor.py show_headers b.c dep.txt clang -Iinc -include inc/prefix.h -H -fno-color-diagnostics -c b.c -o b.o
cat dep.txt
```

Through Buck2, with the e2e fixture of this PR and the prelude of origin/main: build `root//:lib[objects][r.c.o]`, edit `pi.h` (included by the prefix header `p.h`), and build again.

## Expected

The dep file lists `inc/prefix.h` and `inc/inner.h`, as `dep_file_processor.py makefile` does for the same compile with `-MD`. The second build compiles again.

## Actual

```
inc/normal.h
b.c
```

`buck2 audit dep-files root//:lib c_compile r.c` lists neither `p.h` nor `pi.h`, and `buck2 log what-ran` shows the second build as `LocalDepFileCache`. With this PR, both headers are listed and the compile runs again (`Local`).

## Notes

- `clang_cl` and `clang_windows` keep their modes. #1656 changes the `clang_cl` line of the same block, so whichever lands second needs a trivial rebase.
- With `makefile` mode, clang compiles also pick up the `\#` / `$$` unescaping of #1657.

Pull Request resolved: #1665

Test Plan:
- `tests/e2e/test_prelude_tools.rs`: the fixture library gets `prefix_header = "p.h"`, which includes `pi.h`. `test_dep_file_keeps_the_headers_found_next_to_the_source` expects both in the dep file. The new `test_edit_to_a_header_of_the_prefix_header_rebuilds_the_object` edits only `pi.h` and expects a `Local` compile.
- Checked manually on macOS (the e2e test is skipped there) with a locally built buck2 against the fixture: origin/main gives `LocalDepFileCache`, this branch gives `Local`.

Reviewed By: dtolnay

Differential Revision: D134817891

fbshipit-source-id: 7a5415dc4ca4385c3291fb61d0d7e48921610f9d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants