Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bazel] Define rules-based CC lowRISC toolchain #25810

Merged
merged 6 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ build --conlyopt='-std=gnu11'
# disassemblies when compilation mode is fastbuild.
build --strip='never'

# Override default enablement of flags from @crt//common to control C compiler
# warnings.
build --features=-pedantic_warnings
build --host_features=-pedantic_warnings

# Enable toolchain hardening features.
# `guards` adds `unimp` guard instructions after unconditional jumps.
build --features=guards
build --host_features=guards

# Use --config=disable_hardening to disable hardening to measure the
# impact of the hardened sequences on code size.
build:disable_hardening --features=-guards --copt=-DOT_DISABLE_HARDENING=1
Expand Down
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ use_repo(open_dice, "open-dice")
hyperdebug = use_extension("//third_party/hyperdebug:extensions.bzl", "hyperdebug")
use_repo(hyperdebug, "hyperdebug_firmware")

lowrisc_rv32imcb_toolchain = use_extension("//third_party/lowrisc:extensions.bzl", "lowrisc_rv32imcb_toolchain")
use_repo(lowrisc_rv32imcb_toolchain, "lowrisc_rv32imcb_toolchain")

# Extension for linking in externally managed test and provisioning customizations
# for both secure/non-secure manufacturer domains.
hooks = use_extension("//rules:extensions.bzl", "hooks")
Expand All @@ -140,3 +143,5 @@ use_repo(
)

register_toolchains("//rules/opentitan:localtools")

register_toolchains("//toolchain:cc_toolchain_opentitan")
28 changes: 28 additions & 0 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@

- [Build Software](./sw/doc/build_software.md)
- [External dependencies](./third_party/README.md)
- [RISC-V toolchain](./toolchain/README.md)

- [Device Software](./sw/device/README.md)
- [Build & Test Rules](./rules/opentitan/README.md)
Expand Down
11 changes: 0 additions & 11 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,3 @@
# //third_party/README.md first.

workspace(name = "lowrisc_opentitan")

# CRT is the Compiler Repository Toolkit. It contains the configuration for
# the windows compiler.
load("//third_party/crt:repos.bzl", "crt_repos")
crt_repos()
load("@crt//:repos.bzl", "crt_repos")
crt_repos()
load("@crt//:deps.bzl", "crt_deps")
crt_deps()
load("@crt//config:registration.bzl", "crt_register_toolchains")
crt_register_toolchains(riscv32 = True)
8 changes: 4 additions & 4 deletions hw/ip/otbn/dv/uvm/get-toolchain-paths.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ if [[ -n ${BAZEL_CACHE} ]]; then
${BAZEL_CMD} fetch \
--distdir="${BAZEL_DISTDIR}" \
--repository_cache="${BAZEL_CACHE}" \
@lowrisc_rv32imcb_files//...
@lowrisc_rv32imcb_toolchain//...
else
BAZEL_CMD="./bazelisk.sh"
${BAZEL_CMD} fetch @lowrisc_rv32imcb_files//...
${BAZEL_CMD} fetch @lowrisc_rv32imcb_toolchain//...
fi

# Set environment variables for the RV32 linker and assembler.
RV32_TOOL_LD=$(${BAZEL_CMD} query \
'deps(@lowrisc_rv32imcb_files//:bin/riscv32-unknown-elf-ld)' \
'deps(@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ld)' \
--output location | cut -f1 -d:)
RV32_TOOL_AS=$(${BAZEL_CMD} query \
'deps(@lowrisc_rv32imcb_files//:bin/riscv32-unknown-elf-as)' \
'deps(@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as)' \
--output location | cut -f1 -d:)
export RV32_TOOL_LD
export RV32_TOOL_AS
2 changes: 1 addition & 1 deletion third_party/crt/BUILD.bazel → rules/actions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

package(default_visibility = ["//visibility:public"])
OT_ACTION_OBJDUMP = "objdump"
2 changes: 1 addition & 1 deletion rules/certificates.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ certificate_codegen = rule(
attrs = {
"template": attr.label(allow_single_file = True, doc = "path to the hjson template file"),
"clang_format": attr.label(
default = "@lowrisc_rv32imcb_files//:bin/clang-format",
default = "@lowrisc_rv32imcb_toolchain//:bin/clang-format",
allow_single_file = True,
cfg = "host",
executable = True,
Expand Down
1 change: 1 addition & 0 deletions rules/opentitan/legacy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ obj_transform = rv_rule(
"format": attr.string(default = "binary"),
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
},
fragments = ["cpp"],
toolchains = ["@rules_cc//cc:toolchain_type"],
)

Expand Down
15 changes: 12 additions & 3 deletions rules/opentitan/static_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Inspired by:
# https://gist.github.com/shareefj/4e314b16148fded3a8ec874e71b07143

load("@rules_cc//cc:action_names.bzl", "CPP_LINK_STATIC_LIBRARY_ACTION_NAME")
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
load("@lowrisc_opentitan//rules:rv.bzl", "rv_rule")

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

cc_toolchain = find_cc_toolchain(ctx)
feature_config = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
archiver = cc_common.get_tool_for_action(
feature_configuration = feature_config,
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
)

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

lib_paths = [lib.path for lib in libs]

ar_path = cc_toolchain.ar_executable

ctx.actions.run_shell(
command = "\"{0}\" rcT {1} {2} && echo -e 'create {1}\naddlib {1}\nsave\nend' | \"{0}\" -M".format(
ar_path,
archiver,
output_lib.path,
" ".join(lib_paths),
),
Expand Down
28 changes: 26 additions & 2 deletions rules/opentitan/transform.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
# SPDX-License-Identifier: Apache-2.0

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_cc//cc:action_names.bzl", "OBJ_COPY_ACTION_NAME")
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@lowrisc_opentitan//rules/opentitan:util.bzl", "get_override")
load("//rules:actions.bzl", "OT_ACTION_OBJDUMP")

def obj_transform(ctx, **kwargs):
"""Transform an object file via objcopy.
Expand All @@ -21,6 +23,17 @@ def obj_transform(ctx, **kwargs):
The transformed File.
"""
cc_toolchain = find_cc_toolchain(ctx)
feature_config = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
objcopy = cc_common.get_tool_for_action(
feature_configuration = feature_config,
action_name = OBJ_COPY_ACTION_NAME,
)

output = kwargs.get("output")
if not output:
name = get_override(ctx, "attr.name", kwargs)
Expand All @@ -40,7 +53,7 @@ def obj_transform(ctx, **kwargs):
src.path,
output.path,
],
executable = cc_toolchain.objcopy_executable,
executable = objcopy,
)
return output

Expand All @@ -57,6 +70,17 @@ def obj_disassemble(ctx, **kwargs):
The disassembled File.
"""
cc_toolchain = find_cc_toolchain(ctx)
feature_config = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
objdump = cc_common.get_tool_for_action(
feature_configuration = feature_config,
action_name = OT_ACTION_OBJDUMP,
)

output = kwargs.get("output")
if not output:
name = get_override(ctx, "attr.name", kwargs)
Expand All @@ -69,7 +93,7 @@ def obj_disassemble(ctx, **kwargs):
outputs = [output],
inputs = [src] + cc_toolchain.all_files.to_list(),
arguments = [
cc_toolchain.objdump_executable,
objdump,
src.path,
output.path,
],
Expand Down
Loading
Loading