Skip to content

Commit 5f05990

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. They want you to use `cc_common.get_tool_for_action` instead, but unfortunately that only allows accessing the tools used by particular actions. Some of our rules need to access specific tools even if they're not used by the presented actions. Examples: 1. We use `clang` for assembly actions, but OTBN needs `riscv32-unknown-elf-as` directly. 2. We need `objdump` which can be registered to the `cc_toolchain`, but since there is no action in Bazel that uses it there's no way to access it with this function. To solve this, I've repurposed this Bazel function to pull tools out of the data for the C toolchain. Signed-off-by: James Wainwright <james.wainwright@lowrisc.org>
1 parent 9a919d8 commit 5f05990

File tree

6 files changed

+25
-21
lines changed

6 files changed

+25
-21
lines changed

rules/opentitan/static_library.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
1111
load("@lowrisc_opentitan//rules:rv.bzl", "rv_rule")
12+
load("@lowrisc_opentitan//rules/opentitan:toolchain.bzl", "get_tool")
1213

1314
def _ot_static_library_impl(ctx):
1415
output_lib = ctx.actions.declare_file("lib{}.a".format(ctx.attr.name))
@@ -42,7 +43,7 @@ def _ot_static_library_impl(ctx):
4243

4344
lib_paths = [lib.path for lib in libs]
4445

45-
ar_path = cc_toolchain.ar_executable
46+
ar_path = get_tool(cc_toolchain, "riscv32-unknown-elf-ar").path
4647

4748
ctx.actions.run_shell(
4849
command = "\"{0}\" rcT {1} {2} && echo -e 'create {1}\naddlib {1}\nsave\nend' | \"{0}\" -M".format(

rules/opentitan/toolchain.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ LocalToolInfo = provider(fields = [
1515
"update_manifest_json",
1616
])
1717

18+
def get_tool(cc_toolchain, name):
19+
"""Find the path to a tool within the C toolchain."""
20+
21+
# Note: we should really be using `cc.get_tool_for_action`, but that only exposes
22+
# tools with corresponding C compilation actions. This does not include tools like
23+
# `objdump` despite them being in the C toolchain. It also doesn't allow for
24+
# accessing specific tools such as `riscv32-unknown-elf-as` when the toolchain is
25+
# configured to use a wrapper like `clang` for assembly actions instead.
26+
return [f for f in cc_toolchain.all_files.to_list() if f.basename == name][0]
27+
1828
def _localtools_toolchain(ctx):
1929
tools = LocalToolInfo(
2030
opentitantool = ctx.attr.opentitantool[0].files_to_run,

rules/opentitan/transform.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
66
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
77
load("@bazel_skylib//lib:paths.bzl", "paths")
88
load("@lowrisc_opentitan//rules/opentitan:util.bzl", "get_override")
9+
load("@lowrisc_opentitan//rules/opentitan:toolchain.bzl", "get_tool")
910

1011
def obj_transform(ctx, **kwargs):
1112
"""Transform an object file via objcopy.
@@ -40,7 +41,7 @@ def obj_transform(ctx, **kwargs):
4041
src.path,
4142
output.path,
4243
],
43-
executable = cc_toolchain.objcopy_executable,
44+
executable = get_tool(cc_toolchain, "riscv32-unknown-elf-objcopy"),
4445
)
4546
return output
4647

@@ -69,7 +70,7 @@ def obj_disassemble(ctx, **kwargs):
6970
outputs = [output],
7071
inputs = [src] + cc_toolchain.all_files.to_list(),
7172
arguments = [
72-
cc_toolchain.objdump_executable,
73+
get_tool(cc_toolchain, "riscv32-unknown-elf-objdump").path,
7374
src.path,
7475
output.path,
7576
],

rules/otbn.bzl

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,16 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
load("//rules:rv.bzl", "rv_rule")
6+
load("//rules/opentitan:toolchain.bzl", "get_tool")
67
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
78

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-
179
def _otbn_assemble_sources(ctx, additional_srcs = []):
1810
"""Helper function that, for each source file in the provided context, adds
1911
an action to the context that invokes the otbn assember (otbn_as.py),
2012
producing a corresponding object file. Returns a list of all object files
2113
that will be generated by these actions.
2214
"""
2315
cc_toolchain = find_cc_toolchain(ctx)
24-
assembler = _get_assembler(cc_toolchain)
2516

2617
objs = []
2718
for src in ctx.files.srcs + additional_srcs:
@@ -33,7 +24,7 @@ def _otbn_assemble_sources(ctx, additional_srcs = []):
3324
cc_toolchain.all_files.to_list() +
3425
[ctx.executable._otbn_as]),
3526
env = {
36-
"RV32_TOOL_AS": assembler.path,
27+
"RV32_TOOL_AS": get_tool(cc_toolchain, "riscv32-unknown-elf-as").path,
3728
},
3829
arguments = ["-o", obj.path, src.path] + ctx.attr.args,
3930
executable = ctx.executable._otbn_as,
@@ -78,7 +69,6 @@ def _otbn_binary(ctx, additional_srcs = []):
7869
that other rules can depend on in their `deps`.
7970
"""
8071
cc_toolchain = find_cc_toolchain(ctx)
81-
assembler = _get_assembler(cc_toolchain)
8272

8373
# Run the otbn assembler on source files to produce object (.o) files.
8474
objs = _otbn_assemble_sources(ctx, additional_srcs)
@@ -100,10 +90,10 @@ def _otbn_binary(ctx, additional_srcs = []):
10090
ctx.files._otbn_data +
10191
[ctx.executable._wrapper]),
10292
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,
93+
"RV32_TOOL_AS": get_tool(cc_toolchain, "riscv32-unknown-elf-as").path,
94+
"RV32_TOOL_AR": get_tool(cc_toolchain, "riscv32-unknown-elf-ar").path,
95+
"RV32_TOOL_LD": get_tool(cc_toolchain, "riscv32-unknown-elf-ld").path,
96+
"RV32_TOOL_OBJCOPY": get_tool(cc_toolchain, "riscv32-unknown-elf-objcopy").path,
10797
},
10898
arguments = [
10999
"--app-name={}".format(ctx.attr.name),

rules/tock.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
9+
load("//rules/opentitan:toolchain.bzl", "get_tool")
910
load(
1011
"//rules:rv.bzl",
1112
"rv_rule",
@@ -110,7 +111,7 @@ def _tock_image_impl(ctx):
110111
ctx.file.kernel.path,
111112
kernel_binary.path,
112113
],
113-
executable = cc_toolchain.objcopy_executable,
114+
executable = get_tool(cc_toolchain, "riscv32-unknown-elf-objcopy").path,
114115
)
115116

116117
ctx.actions.run(

sw/device/silicon_creator/imm_rom_ext/utils.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
66
load("@lowrisc_opentitan//rules:rv.bzl", "rv_rule")
7+
load("@lowrisc_opentitan//rules/opentitan:toolchain.bzl", "get_tool")
78

89
def _bin_to_imm_rom_ext_object_impl(ctx):
910
cc_toolchain = find_cc_toolchain(ctx)
@@ -30,7 +31,7 @@ def _bin_to_imm_rom_ext_object_impl(ctx):
3031
src.path,
3132
object.path,
3233
],
33-
executable = cc_toolchain.objcopy_executable,
34+
executable = get_tool(cc_toolchain, "riscv32-unknown-elf-objcopy").path,
3435
)
3536
outputs.append(object)
3637
return [DefaultInfo(files = depset(outputs), runfiles = ctx.runfiles(files = outputs))]

0 commit comments

Comments
 (0)