An example repo can be found at https://github.com/versemonger/bazel-test
Asked
Active
Viewed 429 times
1 Answers
4
Bazel simply does not implement any header privacy checking by default. However, under clang and a recent version of Bazel, the so-called "layering check" validation can be enabled with the layering_check
feature. For instance, with the OP's example repository and Bazel 4.0.0, I see:
$ CC=clang bazel build //sub --features layering_check
INFO: Analyzed target //sub:sub (16 packages loaded, 56 targets configured).
INFO: Found 1 target...
ERROR: sub/BUILD:3:10: Compiling sub/greet.cpp failed: (Exit 1): clang failed: error executing command /usr/bin/clang -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 28 argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox clang failed: error executing command /usr/bin/clang -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 28 argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox
sub/greet.cpp:1:10: error: use of private header from outside its module: 'f.h' [-Wprivate-header]
#include "f.h"
^
layering_check
may also be enabled on a per-target basis by setting features = ['layering_check']
in the BUILD
file.

Benjamin Peterson
- 19,297
- 6
- 32
- 39
-
Thank you for the explanation! one more question though: is there any pattern about when a target can access headers in srcs section of its deps? Or is it always?(so hdrs and srcs make no difference without layering_check?) – Zhu Li Apr 20 '21 at 19:33
-
1`srcs` vs. `hdrs` doesn't affect visibility checking, no. (There are some other differences like `strip_include_prefix` only applies to `hdrs`.) – Benjamin Peterson Apr 20 '21 at 19:39