@@ -30,6 +30,12 @@ cc_binary(
3030 deps = [":hello_lib"],
3131)
3232
33+ cc_library(
34+ name = 'hello_header_only',
35+ hdrs = ['hello_header_only.h'],
36+ deps = [":hello_lib"],
37+ )
38+
3339cc_library(
3440 name = "hello_lib",
3541 srcs = ["hello_private.h", "hellolib.cc"],
@@ -115,11 +121,28 @@ int main() {
115121 return hello() - 42;
116122}
117123#endif
124+ EOF
125+
126+ cat > hello/hello_header_only.h << EOF
127+ #ifdef private_header
128+ #include "hello_private.h"
129+ int func() {
130+ return helloPrivate() - 42;
131+ }
132+ #elif defined layering_violation
133+ #include "base.h"
134+ int func() {
135+ return base() - 42;
136+ }
137+ #else
138+ #include "hello.h"
139+ int func() {
140+ return hello() - 42;
141+ }
142+ #endif
118143EOF
119144}
120145
121- # TODO(hlopko): Add a test for a "toplevel" header-only library
122- # once we have parse_headers support in cc_configure.
123146function test_bazel_layering_check() {
124147 if is_darwin; then
125148 echo " This test doesn't run on Darwin. Skipping."
@@ -135,7 +158,7 @@ function test_bazel_layering_check() {
135158 write_files
136159
137160 CC=" ${clang_tool} " bazel build \
138- //hello:hello --linkopt=-fuse-ld=gold -- features=layering_check \
161+ //hello:hello --features=layering_check \
139162 & > " ${TEST_log} " || fail " Build with layering_check failed"
140163
141164 bazel-bin/hello/hello || fail " the built binary failed to run"
@@ -148,23 +171,65 @@ function test_bazel_layering_check() {
148171 fail " module map files were not generated"
149172 fi
150173
151- # Specifying -fuse-ld=gold explicitly to override -fuse-ld=/usr/bin/ld.gold
152- # passed in by cc_configure because Ubuntu-16.04 ships with an old
153- # clang version that doesn't accept that.
154174 CC=" ${clang_tool} " bazel build \
155175 --copt=-D=private_header \
156- //hello:hello --linkopt=-fuse-ld=gold -- features=layering_check \
176+ //hello:hello --features=layering_check \
157177 & > " ${TEST_log} " && fail " Build of private header violation with " \
158178 " layering_check should have failed"
159179 expect_log " use of private header from outside its module: 'hello_private.h'"
160180
161181 CC=" ${clang_tool} " bazel build \
162182 --copt=-D=layering_violation \
163- //hello:hello --linkopt=-fuse-ld=gold -- features=layering_check \
183+ //hello:hello --features=layering_check \
164184 & > " ${TEST_log} " && fail " Build of private header violation with " \
165185 " layering_check should have failed"
166186 expect_log " module //hello:hello does not depend on a module exporting " \
167187 " 'base.h'"
168188}
169189
190+ function test_bazel_layering_check_header_only() {
191+ if is_darwin; then
192+ echo " This test doesn't run on Darwin. Skipping."
193+ return
194+ fi
195+
196+ local -r clang_tool=$( which clang)
197+ if [[ ! -x ${clang_tool:-/ usr/ bin/ clang_tool} ]]; then
198+ echo " clang not installed. Skipping test."
199+ return
200+ fi
201+
202+ write_files
203+
204+ CC=" ${clang_tool} " bazel build \
205+ //hello:hello_header_only --features=layering_check --features=parse_headers \
206+ -s --process_headers_in_dependencies \
207+ & > " ${TEST_log} " || fail " Build with layering_check + parse_headers failed"
208+
209+ if [[ ! -e bazel-bin/hello/hello_header_only.cppmap ]]; then
210+ fail " module map file for hello_header_only was not generated"
211+ fi
212+
213+ if [[ ! -e bazel-bin/hello/hello_lib.cppmap ]]; then
214+ fail " module map file for hello_lib was not generated"
215+ fi
216+
217+ CC=" ${clang_tool} " bazel build \
218+ --copt=-D=private_header \
219+ //hello:hello_header_only --features=layering_check --features=parse_headers \
220+ --process_headers_in_dependencies \
221+ & > " ${TEST_log} " && fail " Build of private header violation with " \
222+ " layering_check + parse_headers should have failed"
223+ expect_log " use of private header from outside its module: 'hello_private.h'"
224+
225+ CC=" ${clang_tool} " bazel build \
226+ --copt=-D=layering_violation \
227+ //hello:hello_header_only --features=layering_check --features=parse_headers \
228+ --process_headers_in_dependencies \
229+ & > " ${TEST_log} " && fail " Build of private header violation with " \
230+ " layering_check + parse_headers should have failed"
231+ expect_log " module //hello:hello_header_only does not depend on a module exporting " \
232+ " 'base.h'"
233+ }
234+
170235run_suite " test layering_check"
0 commit comments