bazelbuild / rules_cc

C++ Rules for Bazel
https://bazel.build
Apache License 2.0
176 stars 88 forks source link

Link-Time Substitution: Native Support #200

Open cramertj opened 10 months ago

cramertj commented 10 months ago

Description of the problem / feature request:

I'm using link-time substitution in order to provide different implementations of the same function in different binaries (e.g. test targets, or targets with different features enabled) within the same overall build.

When working a large codebase, it's easy to either:

I would like to be able to tag the targets which declare the interface with e.g. a declares_symbols = [ ... ] argument, and the targets which define it with e.g. defines_symbols = [ ... ] such that I get a bazel error if I try to build a target which depends on a symbol declaration which does not have exactly one definition in its dependency tree.

For example:

cc_library(
   name = "what_color_declaration"
   hdrs = [ "what_color_declaration.h" ]
   declares_symbols = [ "what_color" ]
)

cc_library(
   name = "what_color_returns_blue"
   srcs = [ "what_color_returns_blue.cc" ]
   defines_symbols = [ "what_color" ]
)

cc_binary(
   name = "binary_a"
   srcs = [ "binary_a.cc" ]
    // ERROR: must provide definition of `what_color`,
    // declared by dep `what_color_declaration`.
   deps = [ ":what_color_declaration" ]
)

Feature requests: what underlying problem are you trying to solve with this feature?

I want to swap out the implementation of certain functions in different link targets (e.g. binary, test, static library) without rebuilding all dependencies.