Skip to content

Commit 32b36e5

Browse files
committed
[bazel] Move obj_transform rule to cc.bzl
Deduplicates the implementation. Signed-off-by: James Wainwright <[email protected]> (cherry picked from commit a329756)
1 parent a3172f4 commit 32b36e5

File tree

5 files changed

+33
-47
lines changed

5 files changed

+33
-47
lines changed

rules/opentitan.bzl

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,6 @@ PER_DEVICE_DEPS = {
4646
def flatten(l):
4747
return [item for ll in l for item in ll]
4848

49-
def _obj_transform_impl(ctx):
50-
cc_toolchain = find_cc_toolchain(ctx)
51-
outputs = []
52-
for src in ctx.files.srcs:
53-
binary = ctx.actions.declare_file(
54-
"{}.{}".format(
55-
src.basename.replace("." + src.extension, ""),
56-
ctx.attr.suffix,
57-
),
58-
)
59-
outputs.append(binary)
60-
ctx.actions.run(
61-
outputs = [binary],
62-
inputs = [src] + cc_toolchain.all_files.to_list(),
63-
arguments = [
64-
"--output-target",
65-
ctx.attr.format,
66-
src.path,
67-
binary.path,
68-
],
69-
executable = cc_toolchain.objcopy_executable,
70-
)
71-
return [DefaultInfo(files = depset(outputs), data_runfiles = ctx.runfiles(files = outputs))]
72-
73-
obj_transform = rv_rule(
74-
implementation = _obj_transform_impl,
75-
attrs = {
76-
"srcs": attr.label_list(allow_files = True),
77-
"suffix": attr.string(default = "bin"),
78-
"format": attr.string(default = "binary"),
79-
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
80-
},
81-
toolchains = ["@rules_cc//cc:toolchain_type"],
82-
)
83-
8449
# A provider for device-specific archive files that hold binaries of SRAM programs.
8550
ArchiveInfo = provider(fields = ["archive_infos"])
8651

@@ -557,7 +522,6 @@ def opentitan_binary(
557522
Emits rules:
558523
cc_binary named: <name>.elf
559524
cc_binary+transition named: <name>_elf_transition
560-
obj_transform named: <name>_bin
561525
elf_to_dissassembly named: <name>_dis
562526
Optionally:
563527
gen_sim_dv_logs_db named: <name>_logs_db
@@ -597,12 +561,6 @@ def opentitan_binary(
597561

598562
bin_name = "{}_{}".format(name, "bin")
599563
targets.append(":" + bin_name)
600-
obj_transform(
601-
name = bin_name,
602-
srcs = [native_binary_name],
603-
platform = platform,
604-
testonly = testonly,
605-
)
606564

607565
dis_name = "{}_{}".format(name, "dis")
608566
targets.append(":" + dis_name)

rules/opentitan/legacy.bzl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
# SPDX-License-Identifier: Apache-2.0
44

5+
load(
6+
"@lowrisc_opentitan//rules/opentitan:transform.bzl",
7+
_obj_transform = "obj_transform",
8+
)
9+
load("@lowrisc_opentitan//rules:rv.bzl", "rv_rule")
10+
511
def legacy_rom_targets(target, suffixes, testonly = False):
612
"""Create filegroups for legacy ROM rule target names.
713
@@ -27,3 +33,23 @@ def legacy_rom_targets(target, suffixes, testonly = False):
2733
actual = ":{}_{}".format(target, suffix),
2834
testonly = testonly,
2935
)
36+
37+
def _obj_transform_impl(ctx):
38+
outputs = [_obj_transform(ctx)]
39+
return [
40+
DefaultInfo(
41+
files = depset(outputs),
42+
data_runfiles = ctx.runfiles(files = outputs),
43+
),
44+
]
45+
46+
obj_transform = rv_rule(
47+
implementation = _obj_transform_impl,
48+
attrs = {
49+
"src": attr.label(allow_single_file = True),
50+
"suffix": attr.string(default = "bin"),
51+
"format": attr.string(default = "binary"),
52+
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
53+
},
54+
toolchains = ["@rules_cc//cc:toolchain_type"],
55+
)

rules/opentitan/transform.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def obj_transform(ctx, **kwargs):
2828
output = "{}.{}".format(name, suffix)
2929

3030
output = ctx.actions.declare_file(output)
31-
src = get_override(ctx, "attr.src", kwargs)
31+
src = get_override(ctx, "file.src", kwargs)
3232
out_format = get_override(ctx, "attr.format", kwargs)
3333

3434
ctx.actions.run(

sw/device/silicon_owner/tock/kernel/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
load("@rules_rust//rust:defs.bzl", "rust_binary")
66
load("//rules:linker.bzl", "ld_library")
7-
load("//rules:opentitan.bzl", "elf_to_disassembly", "obj_transform")
7+
load("//rules:opentitan.bzl", "elf_to_disassembly")
8+
load("//rules/opentitan:legacy.bzl", "obj_transform")
89
load("//rules/opentitan:defs.bzl", "OPENTITAN_CPU")
910

1011
package(default_visibility = ["//visibility:public"])
@@ -59,6 +60,6 @@ rust_binary(
5960

6061
obj_transform(
6162
name = "raw_kernel",
62-
srcs = [":kernel"],
63+
src = ":kernel",
6364
tags = ["manual"],
6465
)

sw/device/silicon_owner/tock/upstream_kernel/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
load("@rules_rust//rust:defs.bzl", "rust_binary")
66
load("//rules:linker.bzl", "ld_library")
7-
load("//rules:opentitan.bzl", "elf_to_disassembly", "obj_transform")
7+
load("//rules:opentitan.bzl", "elf_to_disassembly")
88
load("//rules/opentitan:defs.bzl", "OPENTITAN_CPU")
9+
load("//rules/opentitan:legacy.bzl", "obj_transform")
910

1011
package(default_visibility = ["//visibility:public"])
1112

@@ -58,6 +59,6 @@ rust_binary(
5859

5960
obj_transform(
6061
name = "raw_kernel",
61-
srcs = [":kernel"],
62+
src = ":kernel",
6263
tags = ["manual"],
6364
)

0 commit comments

Comments
 (0)