Skip to content

Commit f8959db

Browse files
committed
Draft proposal to inject extra features (rule-based features)
This is a draft proposal to inject rules-based toolchain features into the legacy toolchain.
1 parent 494d04c commit f8959db

File tree

11 files changed

+387
-0
lines changed

11 files changed

+387
-0
lines changed

cc/private/toolchain/unix_cc_toolchain_config.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ load(
3131
)
3232
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
3333
load("@rules_cc//cc/toolchains:cc_toolchain_config_info.bzl", "CcToolchainConfigInfo")
34+
load("@rules_cc//cc:cc_toolchain_config_lib.bzl", LegacyFeatureInfo = "FeatureInfo")
35+
load("@rules_cc//cc/toolchains:cc_toolchain_info.bzl", RulesBasedFeatureInfo = "FeatureInfo")
36+
load("@rules_cc//cc/toolchains/impl:legacy_converter.bzl", "convert_feature")
3437

3538
def _target_os_version(ctx):
3639
platform_type = ctx.fragments.apple.single_arch_platform.platform_type
@@ -1931,6 +1934,8 @@ def _impl(ctx):
19311934
if symbol_check:
19321935
features.append(symbol_check)
19331936

1937+
features.extend([convert_feature(extra_feature[RulesBasedFeatureInfo]) for extra_feature in ctx.attr.extra_features])
1938+
19341939
return cc_common.create_cc_toolchain_config_info(
19351940
ctx = ctx,
19361941
features = features,
@@ -1955,6 +1960,7 @@ cc_toolchain_config = rule(
19551960
"abi_libc_version": attr.string(mandatory = True),
19561961
"abi_version": attr.string(mandatory = True),
19571962
"archive_flags": attr.string_list(),
1963+
"extra_features": attr.label_list(providers = [RulesBasedFeatureInfo], default = []),
19581964
"builtin_sysroot": attr.string(),
19591965
"compile_flags": attr.string_list(),
19601966
"compiler": attr.string(mandatory = True),

cc/toolchains/cc_toolchain_info.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# decide to just require users to use the public user-facing rules.
2020
visibility([
2121
"//cc/toolchains/...",
22+
"//cc/private/toolchain/...",
2223
"//tests/rule_based_toolchain/...",
2324
])
2425

cc/toolchains/impl/legacy_converter.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ load(
2929

3030
visibility([
3131
"//cc/toolchains/...",
32+
"//cc/private/toolchain/...",
3233
"//tests/rule_based_toolchain/...",
3334
])
3435

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
2+
3+
cc_binary(
4+
name = "main",
5+
srcs = ["main.cpp"],
6+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main()
2+
{
3+
return 32;
4+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Copyright 2016 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This becomes the BUILD file for @local_config_cc// under non-BSD unixes.
16+
17+
load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
18+
load("@rules_cc//cc/toolchains:cc_toolchain.bzl", "cc_toolchain")
19+
load(":cc_toolchain_config.bzl", "cc_toolchain_config")
20+
21+
package(default_visibility = ["//visibility:public"])
22+
23+
licenses(["notice"]) # Apache 2.0
24+
25+
filegroup(
26+
name = "empty",
27+
srcs = [],
28+
)
29+
30+
filegroup(
31+
name = "cc_wrapper",
32+
srcs = ["cc_wrapper.sh"],
33+
)
34+
35+
filegroup(
36+
name = "validate_static_library",
37+
srcs = ["validate_static_library.sh"],
38+
)
39+
40+
filegroup(
41+
name = "deps_scanner_wrapper",
42+
srcs = ["deps_scanner_wrapper.sh"],
43+
)
44+
45+
filegroup(
46+
name = "compiler_deps",
47+
srcs = glob(
48+
["extra_tools/**"],
49+
allow_empty = True,
50+
) + [
51+
":builtin_include_directory_paths",
52+
":cc_wrapper",
53+
":deps_scanner_wrapper",
54+
":validate_static_library",
55+
],
56+
)
57+
58+
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
59+
load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature")
60+
61+
cc_feature(
62+
name = "foo",
63+
args = [":flags"],
64+
feature_name = "foo",
65+
visibility = ["//visibility:public"],
66+
)
67+
68+
cc_args(
69+
name = "flags",
70+
actions = ["@rules_cc//cc/toolchains/actions:compile_actions"],
71+
args = ["-foo"],
72+
)
73+
74+
toolchain(
75+
name = "my_linux_toolchain",
76+
exec_compatible_with = [
77+
"@platforms//os:linux",
78+
"@platforms//cpu:x86_64",
79+
],
80+
target_compatible_with = [
81+
"@platforms//os:linux",
82+
"@platforms//cpu:x86_64",
83+
],
84+
toolchain = ":cc-compiler-k8",
85+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
86+
)
87+
88+
cc_toolchain(
89+
name = "cc-compiler-k8",
90+
all_files = ":compiler_deps",
91+
ar_files = ":compiler_deps",
92+
as_files = ":compiler_deps",
93+
compiler_files = ":compiler_deps",
94+
dwp_files = ":empty",
95+
linker_files = ":compiler_deps",
96+
module_map = None,
97+
objcopy_files = ":empty",
98+
strip_files = ":empty",
99+
supports_header_parsing = 1,
100+
supports_param_files = 1,
101+
toolchain_config = ":local",
102+
toolchain_identifier = "local",
103+
)
104+
105+
cc_toolchain_config(
106+
name = "local",
107+
abi_libc_version = "local",
108+
abi_version = "local",
109+
compile_flags = [
110+
"-fstack-protector",
111+
"-Wall",
112+
"-Wunused-but-set-parameter",
113+
"-Wno-free-nonheap-object",
114+
"-fno-omit-frame-pointer",
115+
],
116+
compiler = "gcc",
117+
conly_flags = [],
118+
coverage_compile_flags = ["--coverage"],
119+
coverage_link_flags = ["--coverage"],
120+
cpu = "k8",
121+
cxx_builtin_include_directories = [
122+
"/usr/lib/gcc/x86_64-linux-gnu/10/include",
123+
"/usr/local/include",
124+
"/usr/include/x86_64-linux-gnu",
125+
"/usr/include",
126+
"/usr/include/c++/10",
127+
"/usr/include/x86_64-linux-gnu/c++/10",
128+
"/usr/include/c++/10/backward",
129+
],
130+
cxx_flags = ["-std=c++17"],
131+
dbg_compile_flags = ["-g"],
132+
extra_features = [":foo"],
133+
host_system_name = "local",
134+
link_flags = [
135+
"-fuse-ld=gold",
136+
"-B/usr/bin",
137+
"-Wl,-no-as-needed",
138+
"-Wl,-z,relro,-z,now",
139+
"-pass-exit-codes",
140+
],
141+
link_libs = [
142+
"-Wl,--push-state,-as-needed",
143+
"-lstdc++",
144+
"-Wl,--pop-state",
145+
"-Wl,--push-state,-as-needed",
146+
"-lm",
147+
"-Wl,--pop-state",
148+
],
149+
opt_compile_flags = [
150+
"-g0",
151+
"-O2",
152+
"-D_FORTIFY_SOURCE=1",
153+
"-DNDEBUG",
154+
"-ffunction-sections",
155+
"-fdata-sections",
156+
],
157+
opt_link_flags = ["-Wl,--gc-sections"],
158+
supports_start_end_lib = True,
159+
target_libc = "local",
160+
target_system_name = "local",
161+
tool_paths = {
162+
"ar": "/usr/bin/ar",
163+
"ld": "/usr/bin/ld",
164+
"cpp": "/usr/bin/cpp-10",
165+
"gcc": "/usr/bin/gcc-10",
166+
"dwp": "/usr/bin/dwp",
167+
"gcov": "/usr/bin/gcov-10",
168+
"nm": "/usr/bin/nm",
169+
"objcopy": "/usr/bin/objcopy",
170+
"objdump": "/usr/bin/objdump",
171+
"strip": "/usr/bin/strip",
172+
"c++filt": "/usr/bin/c++filt",
173+
"cpp-module-deps-scanner": "deps_scanner_wrapper.sh",
174+
"parse_headers": "cc_wrapper.sh",
175+
"validate_static_library": "validate_static_library.sh",
176+
},
177+
toolchain_identifier = "local",
178+
unfiltered_compile_flags = [
179+
"-fno-canonical-system-headers",
180+
"-Wno-builtin-macro-redefined",
181+
"-D__DATE__=\"redacted\"",
182+
"-D__TIMESTAMP__=\"redacted\"",
183+
"-D__TIME__=\"redacted\"",
184+
],
185+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This file is generated by cc_configure and contains builtin include directories
2+
that /usr/bin/gcc reported. This file is a dependency of every compilation action and
3+
changes to it will be reflected in the action cache key. When some of these
4+
paths change, Bazel will make sure to rerun the action, even though none of
5+
declared action inputs or the action commandline changes.
6+
7+
/usr/lib/gcc/x86_64-linux-gnu/10/include
8+
/usr/local/include
9+
/usr/include/x86_64-linux-gnu
10+
/usr/include
11+
/usr/include/c++/10
12+
/usr/include/x86_64-linux-gnu/c++/10
13+
/usr/include/c++/10/backward
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# buildifier: disable=bzl-visibility
2+
load(
3+
"@rules_cc//cc/private/toolchain:unix_cc_toolchain_config.bzl",
4+
unix_cc_toolchain_config = "cc_toolchain_config",
5+
)
6+
7+
# Macro for calling cc_toolchain_config from @bazel_tools with setting the
8+
# right paths and flags for the tools.
9+
def cc_toolchain_config(
10+
name,
11+
abi_libc_version,
12+
abi_version,
13+
compile_flags,
14+
compiler,
15+
conly_flags,
16+
coverage_compile_flags,
17+
coverage_link_flags,
18+
cpu,
19+
cxx_builtin_include_directories,
20+
cxx_flags,
21+
dbg_compile_flags,
22+
host_system_name,
23+
link_flags,
24+
link_libs,
25+
opt_compile_flags,
26+
opt_link_flags,
27+
supports_start_end_lib,
28+
target_libc,
29+
target_system_name,
30+
tool_paths,
31+
toolchain_identifier,
32+
unfiltered_compile_flags,
33+
extra_features = []):
34+
unix_cc_toolchain_config(
35+
name = name,
36+
abi_libc_version = abi_libc_version,
37+
abi_version = abi_version,
38+
archive_flags = [],
39+
compile_flags = compile_flags,
40+
compiler = compiler,
41+
conly_flags = conly_flags,
42+
coverage_compile_flags = coverage_compile_flags,
43+
coverage_link_flags = coverage_link_flags,
44+
cpu = cpu,
45+
cxx_builtin_include_directories = cxx_builtin_include_directories,
46+
cxx_flags = cxx_flags,
47+
dbg_compile_flags = dbg_compile_flags,
48+
fastbuild_compile_flags = [],
49+
host_system_name = host_system_name,
50+
link_flags = link_flags,
51+
link_libs = link_libs,
52+
extra_features = extra_features,
53+
opt_compile_flags = opt_compile_flags,
54+
opt_link_flags = opt_link_flags,
55+
supports_start_end_lib = supports_start_end_lib,
56+
target_libc = target_libc,
57+
target_system_name = target_system_name,
58+
tool_paths = tool_paths,
59+
toolchain_identifier = toolchain_identifier,
60+
unfiltered_compile_flags = unfiltered_compile_flags,
61+
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright 2015 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# Ship the environment to the C++ action
18+
#
19+
set -eu
20+
21+
OUTPUT=
22+
23+
function parse_option() {
24+
local -r opt="$1"
25+
if [[ "${OUTPUT}" = "1" ]]; then
26+
OUTPUT=$opt
27+
elif [[ "$opt" = "-o" ]]; then
28+
# output is coming
29+
OUTPUT=1
30+
fi
31+
}
32+
33+
# let parse the option list
34+
for i in "$@"; do
35+
if [[ "$i" = @* && -r "${i:1}" ]]; then
36+
while IFS= read -r opt
37+
do
38+
parse_option "$opt"
39+
done < "${i:1}" || exit 1
40+
else
41+
parse_option "$i"
42+
fi
43+
done
44+
45+
# Set-up the environment
46+
47+
48+
# Call the C++ compiler
49+
/usr/bin/gcc-13 "$@"
50+
51+
# Generate an empty file if header processing succeeded.
52+
if [[ "${OUTPUT}" == *.h.processed ]]; then
53+
echo -n > "${OUTPUT}"
54+
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Ship the environment to the C++ action
4+
#
5+
set -eu
6+
7+
# Set-up the environment
8+
9+
10+
# Call the C++ compiler
11+
12+
/usr/bin/gcc-10 -E -x c++ -fmodules-ts -fdeps-file=out.tmp -fdeps-format=p1689r5 "$@" >"$DEPS_SCANNER_OUTPUT_FILE"

0 commit comments

Comments
 (0)