Skip to content

Commit 3a73257

Browse files
committed
Get os based args working in toolchains
1 parent 31ec28a commit 3a73257

File tree

7 files changed

+188
-49
lines changed

7 files changed

+188
-49
lines changed

BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")
33

44
cc_toolchain(
55
name = "toolchains_cc_toolchain",
6-
args = ["//runtimes:args"],
6+
args = [
7+
"//toolchain:args",
8+
"//runtimes:args",
9+
],
710
enabled_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"],
811
known_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"],
912
tool_map = "//toolchain:all_tools",

repo_gen/generate_repo.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ def create_single_label(label, num_spaces):
9494
9595
""".lstrip()
9696

97+
cc_arg_os_tpl = """
98+
cc_args(
99+
name = "llvm-{action}",
100+
actions = [
101+
"@rules_cc//cc/toolchains/actions:{action}",
102+
],
103+
args = select({{
104+
{args}
105+
}}),
106+
)
107+
108+
""".lstrip()
109+
97110
# ==================
98111
# || MODULE.bazel ||
99112
# ==================
@@ -143,7 +156,7 @@ def generate_module():
143156
def create_latest(row, dir):
144157
name = row["name"]
145158

146-
if "configurations" not in row:
159+
if "configurations" not in row or "all" not in row["configurations"]:
147160
return config_setting_tpl.format(
148161
name=f"{name}-latest",
149162
value=f"{name}",
@@ -171,6 +184,9 @@ def create_latest_with_configurations(row, dir):
171184
if "configurations" not in row:
172185
return ""
173186

187+
if "all" not in row["configurations"]:
188+
return ""
189+
174190
name = row["name"]
175191
default_config = row["default-configuration"]
176192

@@ -198,7 +214,7 @@ def create_version(row):
198214
settings = ""
199215
for version_item in row["versions"]:
200216
version = version_item["version"]
201-
if "configurations" in row:
217+
if "configurations" in row and "all" in row["configurations"]:
202218
configs = []
203219
for config_item in row["configurations"]["all"]:
204220
config = config_item["name"]
@@ -219,7 +235,7 @@ def create_version(row):
219235
def create_version_with_configurations(row, dir):
220236
name = row["name"]
221237

222-
if "configurations" not in row:
238+
if "configurations" not in row or "all" not in row["configurations"]:
223239
settings = ""
224240
for version_item in row["versions"]:
225241
version = version_item["version"]
@@ -256,6 +272,9 @@ def create_version_configs(row):
256272
if "configurations" not in row:
257273
return ""
258274

275+
if "all" not in row["configurations"]:
276+
return ""
277+
259278
name = row["name"]
260279
settings = ""
261280
for config_item in row["configurations"]["all"]:
@@ -415,6 +434,9 @@ def create_platform_aliases(name, version_item, actions):
415434
def create_configuration_args(row):
416435
if "configurations" not in row:
417436
return ""
437+
438+
if "all" not in row["configurations"]:
439+
return ""
418440

419441
action_to_args = {}
420442

@@ -442,6 +464,39 @@ def create_configuration_args(row):
442464

443465
return cc_args
444466

467+
def create_os_configuration_args(row):
468+
if "configurations" not in row:
469+
return ""
470+
471+
if "os" not in row["configurations"]:
472+
return ""
473+
474+
action_to_args = {}
475+
476+
name = row["name"]
477+
for config_item in row["configurations"]["os"]:
478+
config = config_item["name"]
479+
for action_item in config_item["actions"]:
480+
action = action_item["name"]
481+
args = ""
482+
args += f" \"@platforms//os:{config}\": [\n"
483+
for arg in action_item["args"]:
484+
args += f" \"{arg}\",\n"
485+
args += " ],\n"
486+
487+
if action not in action_to_args:
488+
action_to_args[action] = ""
489+
action_to_args[action] += args
490+
491+
cc_args = ""
492+
for action, args in action_to_args.items():
493+
cc_args += cc_arg_os_tpl.format(
494+
action=action,
495+
args=args.strip()
496+
)
497+
498+
return cc_args
499+
445500
def generate_build_files(dir, actions):
446501
with open(f'repo_gen/{dir}/BUILD.tpl', 'r') as file:
447502
build_tpl = file.read()
@@ -454,6 +509,7 @@ def generate_build_files(dir, actions):
454509
with open(f"{dir}/{name}/BUILD", 'w') as file:
455510
build = build_tpl.format(name = name)
456511
build += create_configuration_args(config)
512+
build += create_os_configuration_args(config)
457513
build += create_version_aliases(config, dir, actions)
458514
build += create_top_level_config_settings_group(config)
459515
build += create_version_configs(config)

repo_gen/toolchain.json

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,43 @@
1818
}
1919
]
2020
}
21-
]
21+
],
22+
"configurations": {
23+
"os": [
24+
{
25+
"name": "linux",
26+
"actions": [
27+
{
28+
"name": "c_compile",
29+
"args": [
30+
"-nostdinc",
31+
"-nostdlib",
32+
"-nodefaultlibs"
33+
]
34+
},
35+
{
36+
"name": "cpp_compile_actions",
37+
"args": [
38+
"-nostdinc",
39+
"-nostdlib",
40+
"-nodefaultlibs",
41+
"-nostdinc++",
42+
"-nostdlib++"
43+
]
44+
},
45+
{
46+
"name": "link_actions",
47+
"args": [
48+
"-nostdinc",
49+
"-nostdlib",
50+
"-nodefaultlibs",
51+
"-nostdinc++",
52+
"-nostdlib++"
53+
]
54+
}
55+
]
56+
}
57+
]
58+
}
2259
}
2360
]

repo_gen/toolchain/BUILD.tpl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
load("@bazel_skylib//lib:selects.bzl", "selects")
2+
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
3+
load("@rules_cc//cc/toolchains:args_list.bzl", "cc_args_list")
24

35
package(default_visibility = ["//toolchain:__subpackages__"])
46

7+
cc_args_list(
8+
name = "args",
9+
args =
10+
select({{
11+
":{name}": [
12+
":llvm-c_compile",
13+
":llvm-cpp_compile_actions",
14+
":llvm-link_actions",
15+
],
16+
"//conditions:default": [],
17+
}}),
18+
visibility = ["//toolchain:__pkg__"],
19+
)
20+

runtimes/BUILD

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,7 @@ load("@rules_cc//cc/toolchains:args_list.bzl", "cc_args_list")
1010
cc_args_list(
1111
name = "args",
1212
args = [
13-
":nostd",
14-
":nostd++",
15-
":nostd_link",
1613
"//runtimes/musl:args",
1714
],
1815
visibility = ["//:__pkg__"],
1916
)
20-
21-
cc_args(
22-
name = "nostd",
23-
actions = [
24-
"@rules_cc//cc/toolchains/actions:c_compile",
25-
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
26-
],
27-
args = [
28-
"-nostdinc",
29-
"-nostdlib",
30-
"-nodefaultlibs",
31-
],
32-
visibility = ["//visibility:public"],
33-
)
34-
35-
cc_args(
36-
name = "nostd++",
37-
actions = [
38-
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
39-
],
40-
args = [
41-
"-nostdinc++",
42-
"-nostdlib++",
43-
],
44-
visibility = ["//visibility:public"],
45-
)
46-
47-
cc_args(
48-
name = "nostd_link",
49-
actions = [
50-
"@rules_cc//cc/toolchains/actions:link_actions",
51-
],
52-
args = [
53-
"-nostdinc",
54-
"-nostdlib",
55-
"-nodefaultlibs",
56-
"-nostdinc++",
57-
"-nostdlib++",
58-
],
59-
visibility = ["//visibility:public"],
60-
)

toolchain/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
load("@rules_cc//cc/toolchains:args_list.bzl", "cc_args_list")
12
load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool")
23
load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map")
34

5+
cc_args_list(
6+
name = "args",
7+
args = [
8+
"//toolchain/llvm:args",
9+
],
10+
visibility = ["//:__pkg__"],
11+
)
12+
413
cc_tool_map(
514
name = "all_tools",
615
tools = {

toolchain/llvm/BUILD

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,69 @@
11
load("@bazel_skylib//lib:selects.bzl", "selects")
2+
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
3+
load("@rules_cc//cc/toolchains:args_list.bzl", "cc_args_list")
24

35
package(default_visibility = ["//toolchain:__subpackages__"])
46

7+
cc_args_list(
8+
name = "args",
9+
args =
10+
select({
11+
":llvm": [
12+
":llvm-c_compile",
13+
":llvm-cpp_compile_actions",
14+
":llvm-link_actions",
15+
],
16+
"//conditions:default": [],
17+
}),
18+
visibility = ["//toolchain:__pkg__"],
19+
)
20+
21+
cc_args(
22+
name = "llvm-c_compile",
23+
actions = [
24+
"@rules_cc//cc/toolchains/actions:c_compile",
25+
],
26+
args = select({
27+
"@platforms//os:linux": [
28+
"-nostdinc",
29+
"-nostdlib",
30+
"-nodefaultlibs",
31+
],
32+
}),
33+
)
34+
35+
cc_args(
36+
name = "llvm-cpp_compile_actions",
37+
actions = [
38+
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
39+
],
40+
args = select({
41+
"@platforms//os:linux": [
42+
"-nostdinc",
43+
"-nostdlib",
44+
"-nodefaultlibs",
45+
"-nostdinc++",
46+
"-nostdlib++",
47+
],
48+
}),
49+
)
50+
51+
cc_args(
52+
name = "llvm-link_actions",
53+
actions = [
54+
"@rules_cc//cc/toolchains/actions:link_actions",
55+
],
56+
args = select({
57+
"@platforms//os:linux": [
58+
"-nostdinc",
59+
"-nostdlib",
60+
"-nodefaultlibs",
61+
"-nostdinc++",
62+
"-nostdlib++",
63+
],
64+
}),
65+
)
66+
567
alias(
668
name = "ar_actions",
769
actual = select({

0 commit comments

Comments
 (0)