Skip to content

Commit 4295909

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 4295909

File tree

12 files changed

+393
-1
lines changed

12 files changed

+393
-1
lines changed

cc/private/toolchain/unix_cc_toolchain_config.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ 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/toolchains:cc_toolchain_info.bzl", RulesBasedFeatureInfo = "FeatureInfo")
35+
load("@rules_cc//cc/toolchains/impl:legacy_converter.bzl", "convert_feature")
3436

3537
def _target_os_version(ctx):
3638
platform_type = ctx.fragments.apple.single_arch_platform.platform_type
@@ -1931,6 +1933,8 @@ def _impl(ctx):
19311933
if symbol_check:
19321934
features.append(symbol_check)
19331935

1936+
features.extend([convert_feature(extra_feature[RulesBasedFeatureInfo]) for extra_feature in ctx.attr.extra_features])
1937+
19341938
return cc_common.create_cc_toolchain_config_info(
19351939
ctx = ctx,
19361940
features = features,
@@ -1965,6 +1969,7 @@ cc_toolchain_config = rule(
19651969
"cxx_builtin_include_directories": attr.string_list(),
19661970
"cxx_flags": attr.string_list(),
19671971
"dbg_compile_flags": attr.string_list(),
1972+
"extra_features": attr.label_list(providers = [RulesBasedFeatureInfo], default = []),
19681973
"extra_flags_per_feature": attr.string_list_dict(),
19691974
"fastbuild_compile_flags": attr.string_list(),
19701975
"host_system_name": attr.string(mandatory = True),

cc/private/toolchain/windows_cc_toolchain_config.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ 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/toolchains:cc_toolchain_info.bzl", RulesBasedFeatureInfo = "FeatureInfo")
35+
load("@rules_cc//cc/toolchains/impl:legacy_converter.bzl", "convert_feature")
3436

3537
all_compile_actions = [
3638
ACTION_NAMES.c_compile,
@@ -1640,9 +1642,12 @@ def _impl(ctx):
16401642
],
16411643
enabled = True,
16421644
)
1645+
features.extend([cpp_modules_feature, cpp_module_modmap_file_feature, cpp20_module_compile_flags_feature])
1646+
features.extend([convert_feature(extra_feature[RulesBasedFeatureInfo]) for extra_feature in ctx.attr.extra_features])
1647+
16431648
return cc_common.create_cc_toolchain_config_info(
16441649
ctx = ctx,
1645-
features = features + [cpp_modules_feature, cpp_module_modmap_file_feature, cpp20_module_compile_flags_feature],
1650+
features = features,
16461651
action_configs = action_configs,
16471652
artifact_name_patterns = artifact_name_patterns,
16481653
cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories,
@@ -1670,6 +1675,7 @@ cc_toolchain_config = rule(
16701675
"dbg_mode_debug_flag": attr.string(default = ""),
16711676
"default_compile_flags": attr.string_list(default = []),
16721677
"default_link_flags": attr.string_list(default = []),
1678+
"extra_features": attr.label_list(providers = [RulesBasedFeatureInfo], default = []),
16731679
"fastbuild_mode_debug_flag": attr.string(default = ""),
16741680
"host_system_name": attr.string(),
16751681
"msvc_cl_path": attr.string(default = "vc_installation_error.bat"),

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+
)

0 commit comments

Comments
 (0)