bazelbuild / bazelbuild/rules_cc
Link-Time Substitution: Native Support
- Dominant language
- Starlark
- Stars
- 247
- Forks
- 196
- PR merge metrics
- No merged PRs in 30d
Description
### Description of the problem / feature request:
I'm using [link-time substitution](https://bramtertoolen.medium.com/link-time-substitution-c-c-test-hardware-dependent-code-91ffd4ef8687) 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:
- Forget to depend on a target which defines an implementation of an interface, resulting in a linker error about missing functions.
- Accidentally depend on more than one implementation of an interface, resulting in an ODR violation.
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:
```python
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.
Contributor guide
Assessment
This issue has not been assessed yet.