-
Notifications
You must be signed in to change notification settings - Fork 4
Configuring a cc_binary differently than a cc_test
Greg edited this page May 7, 2019
·
3 revisions
# This is an example of using a Starlark (user-defined) transition to make
# two native rules build with different settings in the same build.
cat tt/BUILD
load("//tt:defs.bzl", "transitioning_rule")
cc_test(
name = "test",
srcs = ["test.cc"],
data = [":transitioner"],
deps = ["//testing:gunit"],
)
transitioning_rule(
name = "transitioner",
actual_binary = ":bin",
)
cc_binary(
name = "bin",
srcs = ["bin.cc"],
copts = ["-DCOMPILATION_MODE=\\\"$(COMPILATION_MODE)\\\""],
)
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
$ cat tt/defs.bzl
def _impl(ctx):
return [
DefaultInfo(
data_runfiles =
ctx.attr.actual_binary[0][DefaultInfo].data_runfiles,
),
]
def _transition_impl(settings):
return {"//command_line_option:compilation_mode": "opt"}
_comp_mode_transition = transition(
implementation = _transition_impl,
inputs = [],
outputs = ["//command_line_option:compilation_mode"],
)
transitioning_rule = rule(
implementation = _impl,
attrs = {
"actual_binary": attr.label(cfg = _comp_mode_transition),
},
)
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
$ cat tt/bin.cc
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Binary on compilation mode=" << COMPILATION_MODE << "\n";
}
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
$ bazel build //tt:test --compilation_mode=fastbuild
INFO: Analysed target //tt:test (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //tt:test up-to-date:
bazel-bin/tt/test
INFO: Build completed successfully, 2 total actions
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
$ bazel-bin/tt/test.runfiles/tt/bin
Binary on compilation mode=opt