1
Fork 0

Support bazel compilation_mode

Bazel has a special command line option (--compilation_mode) for specifying how
c artifacts should be compiled. Copy over the implementation without too much
thought on whether they make sense.

See https://docs.bazel.build/versions/main/user-manual.html#flag--compilation_mode.

!!! Due to a regression in zig-cc, debug symbols are available under --compilation_mode opt

```
laurynasl@laurynasl ~/bazel-zig-cc
 % bazel build -c opt //test/c:which_libc 2>/dev/null && bazel-bin/test/c/which_libc && gdb -batch --ex 'info line main' bazel-bin/test/c/which_libc
glibc_2.19
Line 4 of "test/c/main.c" starts at address 0x2014f0 <main> and ends at 0x2014f1 <main+1>.
laurynasl@laurynasl ~/bazel-zig-cc
 % bazel build -c dbg //test/c:which_libc 2>/dev/null && bazel-bin/test/c/which_libc && gdb -batch --ex 'info line main' bazel-bin/test/c/which_libc
glibc_2.19
Line 4 of "test/c/main.c" starts at address 0x201500 <main> and ends at 0x201504 <main+4>.
laurynasl@laurynasl ~/bazel-zig-cc
 % bazel build -c fastbuild //test/c:which_libc 2>/dev/null && bazel-bin/test/c/which_libc && gdb -batch --ex 'info line main' bazel-bin/test/c/which_libc
glibc_2.19
Function "main" not defined.
```
nix
laurynasl 2022-06-13 12:54:11 +00:00
parent ef5d2b5069
commit b73d167c6c
1 changed files with 53 additions and 1 deletions

View File

@ -2,6 +2,7 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load(
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
"feature",
"feature_set",
"flag_group",
"flag_set",
"tool",
@ -36,6 +37,57 @@ rest_compile_actions = [
ACTION_NAMES.preprocess_assemble,
]
def _compilation_mode_features(ctx):
actions = all_link_actions + compile_and_link_actions + rest_compile_actions
dbg_feature = feature(
name = "dbg",
flag_sets = [
flag_set(
actions = actions,
flag_groups = [
flag_group(
flags = ["-g"],
),
],
),
],
)
opt_feature = feature(
name = "opt",
flag_sets = [
flag_set(
actions = actions,
flag_groups = [
flag_group(
flags = ["-O2", "-DNDEBUG"],
),
],
),
],
)
fastbuild_feature = feature(
name = "fastbuild",
flag_sets = [
flag_set(
actions = actions,
flag_groups = [
flag_group(
flags = ["-gmlt", "-Wl,-S"],
),
],
),
],
)
return [
dbg_feature,
opt_feature,
fastbuild_feature,
]
def _zig_cc_toolchain_config_impl(ctx):
compiler_flags = [
"-I" + d
@ -109,7 +161,7 @@ def _zig_cc_toolchain_config_impl(ctx):
compile_and_link_flags,
rest_compile_flags,
default_linker_flags,
]
] + _compilation_mode_features(ctx)
return cc_common.create_cc_toolchain_config_info(
ctx = ctx,