bazel icon indicating copy to clipboard operation
bazel copied to clipboard

includes and defines should not propagate further than direct dependents

Open hhclam opened this issue 9 years ago • 7 comments

Description of feature request:

cc_library's defines and includes attributes propagate to all transitive dependents. This leads to unwanted includes and defines. There should be finer control that affects only the direct dependents.

Have you found anything relevant by searching the web?

An opposite example of the existing behavior in Bazel is GN from the Chromium project. Their default behavior of public_configs only affect direct dependents: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/reference.md#public_configs They define the all_dependent_configs to affect all dependents transitively but it is strongly discouraged. https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/reference.md#all_dependent_configs With Bazel I have only one option.

hhclam avatar Jun 27 '17 03:06 hhclam

We have #2670 for includes, would that solve your problems? Do you have an usecase for doing the same for defines?

hlopko avatar Jun 27 '17 09:06 hlopko

Also, as Ming mentioned, you can use copts. That does not propagate. That's actually what we recommend: https://bazel.build/versions/master/docs/best-practices.html#include-paths.

kchodorow avatar Jun 27 '17 14:06 kchodorow

+1 what Kristina said, although we're aware that copts are not platform independent and require using select to make them so.

hlopko avatar Jun 27 '17 14:06 hlopko

The recommendation of "All include paths should be relative to the workspace directory." requires changing existing code which try to avoid for migrating existing code. copts does not propagate but it requires all the dependents (in case they need the header) to add the copts.

Our code are modularized into separate directories, e.g.

moduleA/a.c moduleA/a.h

moduleB/b.c moduleB/b.h

moduleC/c.c moduleC/c.h

And each .c file doesn't use the path from workspace root. Dependency of moduleB on moduleA will put the flag -ImoduleA in compilation arguments. As you can imagine the list of -I that we'll need in copts will get long down the dependency graph. The alternative route that Bazel provides (i.e. includes) affect all transitive dependents which is not desirable.

We may have a scenario that happens to defines as well where we want -D to be passed down to direct dependents but not further.

I think a feature like "direct_dependent_copts" will work great for these cases. This allows a rule to affect the copts to its direct dependents only. This is similar to "direct_dependent_settings" in GYP: https://gyp.gsrc.io/docs/InputFormatReference.md#Dependent-Settings that the Chromium project used before. The tricky bit is when a rule wants to re-export copts from its parent, GYP solved this problem with "export_dependent_settings": https://chromium.googlesource.com/experimental/chromium/src/+/refs/wip/bajones/webvr/tools/gn/docs/cookbook.md#Dependent-settings. GN does it with "public_deps". And in Bazel this could be solved by "reexport_deps".

It seems to me this is a missing feature to fine tune the propagation of includes, copts and defines.

hhclam avatar Jun 27 '17 16:06 hhclam

Any more comments for this FR? The fact is I do want the copts and includes to be propogated, but only the direct dependents. Right now I have to do this copts = ["-I%s/something" % PACKAGE_NAME] for all direct dependents which is very terse. includes and defines however are very intrusive that affects all transitive dependents.

hhclam avatar Aug 07 '17 20:08 hhclam

Can you please elaborate on the use case more? Right now we have behaviors that only affect srcs (such as copts), or is fully transitive (like defines). Not having much experience with GN/GYP builds, nor with the chromium build, is this 'only-direct-dependants' thing something that needs to be done often in practice? Isn't it more future-proof to allow fully transitive behavior (or maybe the dependants should actually be srcs)? I guess I need more convincing of the importance of this feature in the wild.

hlopko avatar Aug 16 '17 09:08 hlopko

Duplicate of #11369

oquenchil avatar May 12 '20 11:05 oquenchil