Skip to content

Commit 6a23f6d

Browse files
committed
[verilator] Statically link the verilator simulator
In order to run in foreign CI environments, we need to statically link verilator to avoid glibc versioning issues. Worse, the current debian/ubuntu package for `libelf` has an error that prevents static linking. - Include the latest upstream libelf which corrects the linking error. - Set the CFLAGS and LDFLAGS environment variables during the build of the simulator to statically link and include the upstream libelf. Signed-off-by: Chris Frantz <[email protected]>
1 parent e295715 commit 6a23f6d

File tree

7 files changed

+71
-16
lines changed

7 files changed

+71
-16
lines changed

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ hsm = use_extension("//third_party/hsm:extensions.bzl", "hsm")
283283
use_repo(hsm, "cloud_kms_hsm", "opensc", "sc_hsm", "softhsm2")
284284

285285
system_libs = use_extension("//third_party/system_libs:extensions.bzl", "system_libs")
286-
use_repo(system_libs, "libudev_zero")
286+
use_repo(system_libs, "libelf", "libudev_zero")
287287

288288
nist_cavp = use_extension("//third_party/nist_cavp_testvectors:extensions.bzl", "nist_cavp")
289289
use_repo(

MODULE.bazel.lock

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

hw/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ fusesoc_build(
4141
"//:cores",
4242
],
4343
data = ["//hw/ip/otbn:all_files"],
44+
env = {
45+
# Note: when `make` is executed to build the simulator, it executes inside of
46+
# a build directory 6 levels down from where the location directives will
47+
# evaluate to.
48+
"CFLAGS_FOR_BUILD": "-I../../../../../../$(location @libelf//:gen_dir)/include",
49+
"LDFLAGS_FOR_BUILD": "-static ../../../../../../$(location @libelf//:gen_dir)/lib/libelf.a",
50+
},
51+
extra_deps = [
52+
# Note: the current debian/ubuntu `libelf1` package will not statically link.
53+
# We supply the latest version which fixes the linking error.
54+
"@libelf//:gen_dir",
55+
],
4456
make_options = ":make_options",
4557
output_groups = {
4658
"binary": ["sim-verilator/Vchip_sim_tb"],

release/devbundle/BUILD

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files")
66
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
7-
load("//rules/opentitan:splice.bzl", "bitstream_splice")
87
load("//rules:flags.bzl", "build_with_flags")
8+
load("//rules/opentitan:splice.bzl", "bitstream_splice")
99

1010
package(default_visibility = ["//visibility:public"])
1111

@@ -71,20 +71,12 @@ build_with_flags(
7171
target = "//sw/host/opentitantool:package",
7272
)
7373

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-
8274
# TODO(cfrantz): Implement release automation so we don't have to publish the artifact manually.
8375
# Upload this to the GCS bucket `artifacts.opentitan.org` and name the file
8476
# with a date tag (e.g. devbundle-YYYYMMDD.tar.xz).
8577
#
8678
# For example:
87-
# $ gcloud --acount[email protected] \
79+
# $ gcloud --account[email protected] \
8880
# storage cp \
8981
# bazel-bin/release/devbundle/devbundle.tar.xz \
9082
# gs://artifacts.opentitan.org/dev_bundle/devbundle-20250718.tar.xz
@@ -94,13 +86,16 @@ pkg_tar(
9486
srcs = [
9587
":bazel",
9688
":bitstreams_pkg",
97-
":hsmtool",
9889
":opentitantool",
9990
"//hw:package",
10091
"//hw/ip/otp_ctrl/data:package",
10192
"//sw/device/lib/testing/test_rom:package",
10293
"//sw/device/silicon_creator/lib/ownership/keys/fake:fpga_dev_pkg",
10394
"//sw/device/silicon_creator/rom_ext:fpga_dev_pkg",
95+
# Note: a static build of hsmtool doesn't work because the PKCS#11
96+
# middleware libraries are typically dynamically linked and load
97+
# glibc and friends.
98+
"//sw/host/hsmtool:package",
10499
],
105100
extension = "tar.xz",
106101
tags = ["manual"],

rules/fusesoc.bzl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,20 @@ def _fusesoc_build_impl(ctx):
8080
args.add_all(ctx.attr.systems)
8181
args.add_all(flags)
8282

83+
env = {k: ctx.expand_location(v, ctx.attr.extra_deps) for k, v in ctx.attr.env.items()}
84+
8385
# Note: the `fileset_top` flag used above is specific to the OpenTitan
8486
# project to select the correct RTL fileset.
8587
ctx.actions.run(
8688
mnemonic = "FuseSoC",
8789
outputs = outputs,
88-
inputs = ctx.files.srcs + ctx.files.cores + ctx.files._fusesoc + [
90+
inputs = ctx.files.srcs + ctx.files.cores + ctx.files._fusesoc + ctx.files.extra_deps + [
8991
cfg_file,
9092
],
9193
arguments = [args],
9294
executable = ctx.executable._fusesoc,
9395
use_default_shell_env = False,
94-
env = ENV,
96+
env = ENV | env,
9597
)
9698
return [
9799
DefaultInfo(
@@ -107,6 +109,8 @@ fusesoc_build = rule(
107109
"cores": attr.label_list(allow_files = True, doc = "FuseSoC core specification files"),
108110
"srcs": attr.label_list(allow_files = True, doc = "Source files"),
109111
"data": attr.label_list(allow_files = True, doc = "Files needed at runtime"),
112+
"extra_deps": attr.label_list(allow_files = True, doc = "Extra deps needed to build the model"),
113+
"env": attr.string_dict(doc = "Environment variables to set during the build"),
110114
"target": attr.string(mandatory = True, doc = "Target name (e.g. 'sim')"),
111115
"systems": attr.string_list(mandatory = True, doc = "Systems to build"),
112116
"flags": attr.string_list(doc = "Flags controlling the FuseSOC system build"),
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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:configure.bzl", "configure_make")
6+
7+
package(default_visibility = ["//visibility:public"])
8+
9+
filegroup(
10+
name = "all_srcs",
11+
srcs = glob(["**"]),
12+
)
13+
14+
configure_make(
15+
name = "libelf",
16+
args = ["-j"],
17+
#autoreconf = True,
18+
#autoreconf_options = ["-fi"],
19+
configure_in_place = True,
20+
lib_source = ":all_srcs",
21+
out_static_libs = ["libelf.a"],
22+
)
23+
24+
filegroup(
25+
name = "gen_dir",
26+
srcs = [":libelf"],
27+
output_group = "gen_dir",
28+
)

third_party/system_libs/extensions.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ def _system_libs_repos():
1616
strip_prefix = "libudev-zero-1.0.3",
1717
sha256 = "0bd89b657d62d019598e6c7ed726ff8fed80e8ba092a83b484d66afb80b77da5",
1818
)
19+
http_archive(
20+
name = "libelf",
21+
build_file = Label("//third_party/system_libs:BUILD.libelf.bazel"),
22+
url = "https://sourceware.org/elfutils/ftp/0.193/elfutils-0.193.tar.bz2",
23+
strip_prefix = "elfutils-0.193",
24+
sha256 = "7857f44b624f4d8d421df851aaae7b1402cfe6bcdd2d8049f15fc07d3dde7635",
25+
)

0 commit comments

Comments
 (0)