bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Feature Request: local_includes for cc_library, making an easy path for good cross-platform hygiene

Open cpsauer opened this issue 3 years ago • 2 comments

Hi wonderful Bazelers,

This is a small-scope cc feature request.

As background: cc_library lets you specify include directories for its headers, automatically handling the package. This is very nice for interface headers but bad problematic for implementation headers you don't want to propagate to dependents. The docs steer you to add -I flags to copts to handle that case.

The problem is that this leads people to write code that won't work with toolchains where the flags are different. And needing to figure out that you have to use package_name() (which doesn't handle the external workspace case) tends to lead users to just make all includes public, propagating them to all dependencies. Bad times.

A fairly simple new parameter, local_includes could solve this well, I'd propose. What do you think?

Thanks, Chris (ex-Googler) P.S. Edited from the template for clarity, brevity, and friendliness.

cpsauer avatar Oct 14 '22 01:10 cpsauer

This same idea was also mentioned here: https://github.com/bazelbuild/bazel/issues/13803#issuecomment-899920510

cameron-martin avatar Jun 23 '23 10:06 cameron-martin

+1 for local_includes.

I think this can be achived with implementation_deps

def custom_cc_library(
        name,
        local_includes = [],
        implementation_deps = [],
        **kwargs):
    native.cc_library(
        name = name + "_local_includes",
        includes = local_includes,
        visibility = ["//visibility:private"],
    )

    native.cc_library(
        name = name,
        implementation_deps = implementation_deps + [name + "_local_includes"],
        **kwargs
    )

kon72 avatar Feb 02 '24 14:02 kon72