bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.23k stars 4.07k forks source link

Should `cc_import` support `srcs` for private header files? #22808

Open fergushenderson opened 4 months ago

fergushenderson commented 4 months ago

It is not uncommon for C/C++ libraries to contain both public API headers and also private headers that are included by the public API headers, but which should not be directly included by any clients of the library.

When using cc_import with such libraries, there seems to be no way to keep the private headers private; both public and private headers need to be listed in "hdrs". This contrasts with cc_library, which does support private headers by listing them in "srcs" rather than "hdrs". But cc_import doesn't support the "srcs" attribute.

A potential work-around that I tried was using another cc_library target for the private headers, and listing that one in the deps of the cc_import rule. However, that doesn't seem to work either, getting a "file not found" error for the private header files in the CppCompileHeader action generated from the cc_import rule.

comius commented 4 months ago

Why would you even need to provide private headers to cc_import?

fergushenderson commented 2 months ago

Why would you even need to provide private headers to cc_import?

To avoid compilation errors when building code that depends on the public headers that in turn depend on the private headers, e.g.

In file included from main.cc:1:
foo_public.h:4:10: fatal error: foo_private.h: No such file or directory
    4 | #include "foo_private.h"