Skip to content

Commit 0b369e2

Browse files
committed
[bazel] Change how tools are pulled from cc_toolchains
Bazel is moving towards deprecating the `tool_path` style of CC toolchain which allowed executables to be taken using the `*_executable` parameters. The new method is `cc_common.get_tool_for_action` which works with both the old and new toolchain style. This commit changes all usages except for `objdump` which has no associated action. A future commit will create a custom objdump action for our toolchain and change this usage then. The OTBN toolchain actually needs exact tools. Instead of pulling these from the `cc_toolchain` it makes more sense to access the binaries directly from our toolchain repo. Signed-off-by: James Wainwright <james.wainwright@lowrisc.org>
1 parent 2481893 commit 0b369e2

File tree

5 files changed

+84
-25
lines changed

5 files changed

+84
-25
lines changed

rules/opentitan/static_library.bzl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Inspired by:
88
# https://gist.github.com/shareefj/4e314b16148fded3a8ec874e71b07143
99

10+
load("@rules_cc//cc:action_names.bzl", "CPP_LINK_STATIC_LIBRARY_ACTION_NAME")
1011
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
1112
load("@lowrisc_opentitan//rules:rv.bzl", "rv_rule")
1213

@@ -15,6 +16,16 @@ def _ot_static_library_impl(ctx):
1516
output_flags = ctx.actions.declare_file("lib{}.link".format(ctx.attr.name))
1617

1718
cc_toolchain = find_cc_toolchain(ctx)
19+
feature_config = cc_common.configure_features(
20+
ctx = ctx,
21+
cc_toolchain = cc_toolchain,
22+
requested_features = ctx.features,
23+
unsupported_features = ctx.disabled_features,
24+
)
25+
archiver = cc_common.get_tool_for_action(
26+
feature_configuration = feature_config,
27+
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
28+
)
1829

1930
# Aggregate linker inputs of all dependencies
2031
lib_sets = []
@@ -42,11 +53,9 @@ def _ot_static_library_impl(ctx):
4253

4354
lib_paths = [lib.path for lib in libs]
4455

45-
ar_path = cc_toolchain.ar_executable
46-
4756
ctx.actions.run_shell(
4857
command = "\"{0}\" rcT {1} {2} && echo -e 'create {1}\naddlib {1}\nsave\nend' | \"{0}\" -M".format(
49-
ar_path,
58+
archiver,
5059
output_lib.path,
5160
" ".join(lib_paths),
5261
),

rules/opentitan/transform.bzl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
6+
load("@rules_cc//cc:action_names.bzl", "OBJ_COPY_ACTION_NAME")
67
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
78
load("@bazel_skylib//lib:paths.bzl", "paths")
89
load("@lowrisc_opentitan//rules/opentitan:util.bzl", "get_override")
@@ -21,6 +22,17 @@ def obj_transform(ctx, **kwargs):
2122
The transformed File.
2223
"""
2324
cc_toolchain = find_cc_toolchain(ctx)
25+
feature_config = cc_common.configure_features(
26+
ctx = ctx,
27+
cc_toolchain = cc_toolchain,
28+
requested_features = ctx.features,
29+
unsupported_features = ctx.disabled_features,
30+
)
31+
objcopy = cc_common.get_tool_for_action(
32+
feature_configuration = feature_config,
33+
action_name = OBJ_COPY_ACTION_NAME,
34+
)
35+
2436
output = kwargs.get("output")
2537
if not output:
2638
name = get_override(ctx, "attr.name", kwargs)
@@ -40,7 +52,7 @@ def obj_transform(ctx, **kwargs):
4052
src.path,
4153
output.path,
4254
],
43-
executable = cc_toolchain.objcopy_executable,
55+
executable = objcopy,
4456
)
4557
return output
4658

rules/otbn.bzl

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,13 @@
55
load("//rules:rv.bzl", "rv_rule")
66
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
77

8-
def _get_assembler(cc_toolchain):
9-
"""Find the path to riscv-unknown-elf-as."""
10-
11-
# Note: the toolchain config doesn"t appear to have a good way to get
12-
# access to the assembler. We should be able to access it via the
13-
# the compiler, but I had trouble with //hw/ip/otbn/util/otbn_as.py invoking
14-
# the compiler as assembler.
15-
return [f for f in cc_toolchain.all_files.to_list() if f.basename.endswith("as")][0]
16-
178
def _otbn_assemble_sources(ctx, additional_srcs = []):
189
"""Helper function that, for each source file in the provided context, adds
1910
an action to the context that invokes the otbn assember (otbn_as.py),
2011
producing a corresponding object file. Returns a list of all object files
2112
that will be generated by these actions.
2213
"""
2314
cc_toolchain = find_cc_toolchain(ctx)
24-
assembler = _get_assembler(cc_toolchain)
2515

2616
objs = []
2717
for src in ctx.files.srcs + additional_srcs:
@@ -33,7 +23,7 @@ def _otbn_assemble_sources(ctx, additional_srcs = []):
3323
cc_toolchain.all_files.to_list() +
3424
[ctx.executable._otbn_as]),
3525
env = {
36-
"RV32_TOOL_AS": assembler.path,
26+
"RV32_TOOL_AS": ctx.executable._riscv32_as.path,
3727
},
3828
arguments = ["-o", obj.path, src.path] + ctx.attr.args,
3929
executable = ctx.executable._otbn_as,
@@ -78,7 +68,6 @@ def _otbn_binary(ctx, additional_srcs = []):
7868
that other rules can depend on in their `deps`.
7969
"""
8070
cc_toolchain = find_cc_toolchain(ctx)
81-
assembler = _get_assembler(cc_toolchain)
8271

8372
# Run the otbn assembler on source files to produce object (.o) files.
8473
objs = _otbn_assemble_sources(ctx, additional_srcs)
@@ -100,10 +89,10 @@ def _otbn_binary(ctx, additional_srcs = []):
10089
ctx.files._otbn_data +
10190
[ctx.executable._wrapper]),
10291
env = {
103-
"RV32_TOOL_AS": assembler.path,
104-
"RV32_TOOL_AR": cc_toolchain.ar_executable,
105-
"RV32_TOOL_LD": cc_toolchain.ld_executable,
106-
"RV32_TOOL_OBJCOPY": cc_toolchain.objcopy_executable,
92+
"RV32_TOOL_AS": ctx.executable._riscv32_as.path,
93+
"RV32_TOOL_AR": ctx.executable._riscv32_ar.path,
94+
"RV32_TOOL_LD": ctx.executable._riscv32_ld.path,
95+
"RV32_TOOL_OBJCOPY": ctx.executable._riscv32_objcopy.path,
10796
},
10897
arguments = [
10998
"--app-name={}".format(ctx.attr.name),
@@ -286,8 +275,11 @@ otbn_library = rv_rule(
286275
attrs = {
287276
"srcs": attr.label_list(allow_files = True),
288277
"args": attr.string_list(),
289-
"_cc_toolchain": attr.label(
290-
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
278+
"_riscv32_as": attr.label(
279+
default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as"),
280+
allow_single_file = True,
281+
executable = True,
282+
cfg = "exec",
291283
),
292284
"_otbn_as": attr.label(
293285
default = "//hw/ip/otbn/util:otbn_as",
@@ -306,7 +298,30 @@ otbn_binary = rv_rule(
306298
"srcs": attr.label_list(allow_files = True),
307299
"deps": attr.label_list(providers = [DefaultInfo]),
308300
"args": attr.string_list(),
309-
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
301+
"_riscv32_ar": attr.label(
302+
default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ar"),
303+
allow_single_file = True,
304+
executable = True,
305+
cfg = "exec",
306+
),
307+
"_riscv32_as": attr.label(
308+
default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as"),
309+
allow_single_file = True,
310+
executable = True,
311+
cfg = "exec",
312+
),
313+
"_riscv32_ld": attr.label(
314+
default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ld"),
315+
allow_single_file = True,
316+
executable = True,
317+
cfg = "exec",
318+
),
319+
"_riscv32_objcopy": attr.label(
320+
default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-objcopy"),
321+
allow_single_file = True,
322+
executable = True,
323+
cfg = "exec",
324+
),
310325
"_otbn_as": attr.label(
311326
default = "//hw/ip/otbn/util:otbn_as",
312327
executable = True,

rules/tock.bzl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""Rules for assembling Tock binaries.
66
"""
77

8+
load("@rules_cc//cc/action_names.bzl", "OBJ_COPY_ACTION_NAME")
89
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
910
load(
1011
"//rules:rv.bzl",
@@ -98,6 +99,16 @@ opt_mode = transition(
9899

99100
def _tock_image_impl(ctx):
100101
cc_toolchain = find_cc_toolchain(ctx)
102+
feature_config = cc_common.configure_features(
103+
ctx = ctx,
104+
cc_toolchain = cc_toolchain,
105+
requested_features = ctx.features,
106+
unsupported_features = ctx.disabled_features,
107+
)
108+
objcopy = cc_common.get_tool_for_action(
109+
feature_configuration = feature_config,
110+
action_name = OBJ_COPY_ACTION_NAME,
111+
)
101112

102113
kernel_binary = ctx.actions.declare_file("{}_kernel.bin".format(ctx.attr.name))
103114
images = [ctx.actions.declare_file("{}0.bin".format(ctx.attr.name))]
@@ -110,7 +121,7 @@ def _tock_image_impl(ctx):
110121
ctx.file.kernel.path,
111122
kernel_binary.path,
112123
],
113-
executable = cc_toolchain.objcopy_executable,
124+
executable = objcopy,
114125
)
115126

116127
ctx.actions.run(

sw/device/silicon_creator/imm_rom_ext/utils.bzl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@
22
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
# SPDX-License-Identifier: Apache-2.0
44

5+
load("@rules_cc//cc/action_names.bzl", "OBJ_COPY_ACTION_NAME")
56
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
67
load("@lowrisc_opentitan//rules:rv.bzl", "rv_rule")
78

89
def _bin_to_imm_rom_ext_object_impl(ctx):
910
cc_toolchain = find_cc_toolchain(ctx)
11+
feature_config = cc_common.configure_features(
12+
ctx = ctx,
13+
cc_toolchain = cc_toolchain,
14+
requested_features = ctx.features,
15+
unsupported_features = ctx.disabled_features,
16+
)
17+
objcopy = cc_common.get_tool_for_action(
18+
feature_configuration = feature_config,
19+
action_name = OBJ_COPY_ACTION_NAME,
20+
)
21+
1022
outputs = []
1123
for src in ctx.files.srcs:
1224
if src.extension != "bin":
@@ -30,7 +42,7 @@ def _bin_to_imm_rom_ext_object_impl(ctx):
3042
src.path,
3143
object.path,
3244
],
33-
executable = cc_toolchain.objcopy_executable,
45+
executable = objcopy,
3446
)
3547
outputs.append(object)
3648
return [DefaultInfo(files = depset(outputs), runfiles = ctx.runfiles(files = outputs))]

0 commit comments

Comments
 (0)