Skip to content

Commit e295715

Browse files
committed
[devbundle] Statically link opentitantool & hsmtool
Opentitantool depends on libudev which can cause portability issues between different runtime environments. 1. Add a flag which permits statically linking host tools. 2. Supply `libudev-zero` which is a no-dependencies replacement for libudev. 3. Add some flags machinery for controlling whether opentitantool is statically linked (default: no). 4. Deliver statically linked opentitantool and hsmtool in the devbundle. Signed-off-by: Chris Frantz <[email protected]>
1 parent 4e0ccfc commit e295715

File tree

10 files changed

+189
-2
lines changed

10 files changed

+189
-2
lines changed

MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ use_repo(xkcp, "xkcp")
282282
hsm = use_extension("//third_party/hsm:extensions.bzl", "hsm")
283283
use_repo(hsm, "cloud_kms_hsm", "opensc", "sc_hsm", "softhsm2")
284284

285+
system_libs = use_extension("//third_party/system_libs:extensions.bzl", "system_libs")
286+
use_repo(system_libs, "libudev_zero")
287+
285288
nist_cavp = use_extension("//third_party/nist_cavp_testvectors:extensions.bzl", "nist_cavp")
286289
use_repo(
287290
nist_cavp,

MODULE.bazel.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

release/devbundle/BUILD

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files")
66
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
77
load("//rules/opentitan:splice.bzl", "bitstream_splice")
8+
load("//rules:flags.bzl", "build_with_flags")
89

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

@@ -62,6 +63,22 @@ pkg_files(
6263
},
6364
)
6465

66+
build_with_flags(
67+
name = "opentitantool",
68+
flags = {
69+
"//rules:static_link_host_tools": "True",
70+
},
71+
target = "//sw/host/opentitantool:package",
72+
)
73+
74+
build_with_flags(
75+
name = "hsmtool",
76+
flags = {
77+
"//rules:static_link_host_tools": "True",
78+
},
79+
target = "//sw/host/hsmtool:package",
80+
)
81+
6582
# TODO(cfrantz): Implement release automation so we don't have to publish the artifact manually.
6683
# Upload this to the GCS bucket `artifacts.opentitan.org` and name the file
6784
# with a date tag (e.g. devbundle-YYYYMMDD.tar.xz).
@@ -77,12 +94,13 @@ pkg_tar(
7794
srcs = [
7895
":bazel",
7996
":bitstreams_pkg",
97+
":hsmtool",
98+
":opentitantool",
8099
"//hw:package",
81100
"//hw/ip/otp_ctrl/data:package",
82101
"//sw/device/lib/testing/test_rom:package",
83102
"//sw/device/silicon_creator/lib/ownership/keys/fake:fpga_dev_pkg",
84103
"//sw/device/silicon_creator/rom_ext:fpga_dev_pkg",
85-
"//sw/host/opentitantool:package",
86104
],
87105
extension = "tar.xz",
88106
tags = ["manual"],

rules/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
load("//rules:autogen.bzl", "autogen_stamp_include")
66
load("//rules:stamp.bzl", "stamp_flag")
77
load("//rules/opentitan:defs.bzl", "OPENTITAN_PLATFORM")
8+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
89

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

@@ -28,3 +29,15 @@ stamp_flag(name = "stamp_flag")
2829
autogen_stamp_include(
2930
name = "autogen_stamp_include",
3031
)
32+
33+
bool_flag(
34+
name = "static_link_host_tools",
35+
build_setting_default = False,
36+
)
37+
38+
config_setting(
39+
name = "static_link_host_tools_enabled",
40+
flag_values = {
41+
":static_link_host_tools": "True",
42+
},
43+
)

rules/flags.bzl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright lowRISC contributors (OpenTitan project).
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
load("@rules_pkg//pkg:providers.bzl", "PackageDirsInfo", "PackageFilegroupInfo", "PackageFilesInfo", "PackageSymlinkInfo")
6+
7+
_KNOWN_PROVIDERS = [
8+
PackageDirsInfo,
9+
PackageFilegroupInfo,
10+
PackageFilesInfo,
11+
PackageSymlinkInfo,
12+
OutputGroupInfo,
13+
]
14+
15+
def _bool(v):
16+
if v in ("True", "true"):
17+
return True
18+
if v in ("False", "false"):
19+
return False
20+
fail("Boolean value must be 'True' or 'False'")
21+
22+
_FLAG_CONVERSIONS = {
23+
# TODO: this needs to be the superset of all flags you might want to
24+
# modify during the build and an appropriate type conversion function.
25+
"@lowrisc_opentitan//rules:static_link_host_tools": _bool,
26+
}
27+
28+
def _flags_transition_impl(settings, attr):
29+
result = {}
30+
for label, value in attr.flags.items():
31+
label = str(label)
32+
if label.startswith("@@//"):
33+
label = "@lowrisc_opentitan" + label[2:]
34+
result[label] = _FLAG_CONVERSIONS[label](value)
35+
return result
36+
37+
flags_transition = transition(
38+
implementation = _flags_transition_impl,
39+
inputs = [],
40+
outputs = _FLAG_CONVERSIONS.keys(),
41+
)
42+
43+
def _build_with_flags_impl(ctx):
44+
# Start with DefaultInfo and then forward on any other providers we know about.
45+
result = [ctx.attr.target[DefaultInfo]]
46+
for p in _KNOWN_PROVIDERS:
47+
if p in ctx.attr.target:
48+
result.append(ctx.attr.target[p])
49+
return result
50+
51+
build_with_flags = rule(
52+
implementation = _build_with_flags_impl,
53+
cfg = flags_transition,
54+
attrs = {
55+
"target": attr.label(doc = "Target to build with flags"),
56+
"flags": attr.label_keyed_string_dict(doc = "Mapping of flag labels to values"),
57+
"_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
58+
},
59+
)

sw/host/hsmtool/BUILD

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

55
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc", "rust_library", "rust_test")
6+
load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files")
67

78
package(default_visibility = ["//visibility:public"])
89

@@ -116,6 +117,10 @@ rust_library(
116117
rust_binary(
117118
name = "hsmtool",
118119
srcs = ["src/hsmtool.rs"],
120+
rustc_flags = select({
121+
"//rules:static_link_host_tools_enabled": ["--codegen=target-feature=+crt-static"],
122+
"//conditions:default": [],
123+
}),
119124
deps = [
120125
":hsmlib",
121126
"@crate_index//:anyhow",
@@ -142,3 +147,17 @@ rust_doc(
142147
name = "hsmlib_doc",
143148
crate = ":hsmlib",
144149
)
150+
151+
pkg_files(
152+
name = "binary",
153+
srcs = [":hsmtool"],
154+
attributes = pkg_attributes(mode = "0755"),
155+
)
156+
157+
pkg_filegroup(
158+
name = "package",
159+
srcs = [
160+
":binary",
161+
],
162+
prefix = "hsmtool",
163+
)

sw/host/opentitantool/BUILD

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ rust_binary(
4848
rustc_env_files = [
4949
"stamp-env.txt",
5050
],
51+
rustc_flags = select({
52+
"//rules:static_link_host_tools_enabled": ["--codegen=target-feature=+crt-static"],
53+
"//conditions:default": [],
54+
}),
5155
# stamping is necessary because opentitantool builds version.rs that needs it
5256
stamp = -1,
5357
deps = [
@@ -72,7 +76,10 @@ rust_binary(
7276
"@crate_index//:shellwords",
7377
"@crate_index//:thiserror",
7478
"@lowrisc_serde_annotate//serde_annotate",
75-
],
79+
] + select({
80+
"//rules:static_link_host_tools_enabled": ["@libudev_zero"],
81+
"//conditions:default": [],
82+
}),
7683
)
7784

7885
pkg_files(

third_party/system_libs/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright lowRISC contributors (OpenTitan project).
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
package(default_visibility = ["//visibility:public"])
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright lowRISC contributors (OpenTitan project).
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
load("@rules_foreign_cc//foreign_cc:make.bzl", "make")
6+
7+
package(default_visibility = ["//visibility:public"])
8+
9+
filegroup(
10+
name = "all_srcs",
11+
srcs = glob(["**"]),
12+
)
13+
14+
make(
15+
name = "libudev_zero",
16+
lib_source = ":all_srcs",
17+
out_static_libs = ["libudev.a"],
18+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright lowRISC contributors (OpenTitan project).
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
6+
7+
system_libs = module_extension(
8+
implementation = lambda _: _system_libs_repos(),
9+
)
10+
11+
def _system_libs_repos():
12+
http_archive(
13+
name = "libudev_zero",
14+
build_file = Label("//third_party/system_libs:BUILD.libudev_zero.bazel"),
15+
url = "https://github.com/illiliti/libudev-zero/archive/refs/tags/1.0.3.tar.gz",
16+
strip_prefix = "libudev-zero-1.0.3",
17+
sha256 = "0bd89b657d62d019598e6c7ed726ff8fed80e8ba092a83b484d66afb80b77da5",
18+
)

0 commit comments

Comments
 (0)