diff --git a/.bazelrc b/.bazelrc index 00fbb683bbb20..ec57c239c4ef2 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 diff --git a/MODULE.bazel b/MODULE.bazel index 3b1b342b45967..761baab29827a 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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") @@ -140,3 +143,5 @@ use_repo( ) register_toolchains("//rules/opentitan:localtools") + +register_toolchains("//toolchain:cc_toolchain_opentitan") diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index ea1259d002476..ccc743ecc5717 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -439,6 +439,34 @@ ] } }, + "//third_party/lowrisc:extensions.bzl%lowrisc_rv32imcb_toolchain": { + "general": { + "bzlTransitiveDigest": "W9Z9C5PMascfbcutgU86P2kcAUi2PZ7c0ud3w1QEMPI=", + "usagesDigest": "hbpdCo9aSLLeAkIPhlXazY3BiqGQ4s6FXsWgmlfBrJQ=", + "recordedFileInputs": {}, + "recordedDirentsInputs": {}, + "envVariables": {}, + "generatedRepoSpecs": { + "lowrisc_rv32imcb_toolchain": { + "bzlFile": "@@bazel_tools//tools/build_defs/repo:http.bzl", + "ruleClassName": "http_archive", + "attributes": { + "url": "https://github.com/lowRISC/lowrisc-toolchains/releases/download/20240923-1/lowrisc-toolchain-rv32imcb-20240923-1.tar.xz", + "sha256": "aeea1983553f4c81c6409abcf0d6ca33b5ed4716b2b694e7ff030523cf13486a", + "strip_prefix": "lowrisc-toolchain-rv32imcb-20240923-1", + "build_file": "@@//third_party/lowrisc:BUILD.lowrisc_rv32imcb_toolchain.bazel" + } + } + }, + "recordedRepoMappingEntries": [ + [ + "", + "bazel_tools", + "bazel_tools" + ] + ] + } + }, "//third_party/lychee:extensions.bzl%lychee": { "general": { "bzlTransitiveDigest": "D5TqlXWMpvZUNeDDU9wbX49JxrqA4HMJCzvoaHk+33s=", diff --git a/SUMMARY.md b/SUMMARY.md index 912c167c6d1e4..84f632b2c1f40 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -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) diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod index 0d90a1c97edfe..a670850ba99e8 100644 --- a/WORKSPACE.bzlmod +++ b/WORKSPACE.bzlmod @@ -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) diff --git a/hw/ip/otbn/dv/uvm/get-toolchain-paths.sh b/hw/ip/otbn/dv/uvm/get-toolchain-paths.sh index 647d2879fbe15..dca8a497d4b55 100755 --- a/hw/ip/otbn/dv/uvm/get-toolchain-paths.sh +++ b/hw/ip/otbn/dv/uvm/get-toolchain-paths.sh @@ -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 diff --git a/third_party/crt/BUILD.bazel b/rules/actions.bzl similarity index 75% rename from third_party/crt/BUILD.bazel rename to rules/actions.bzl index c85882e64fe2c..f430ea7eb95af 100644 --- a/third_party/crt/BUILD.bazel +++ b/rules/actions.bzl @@ -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" diff --git a/rules/certificates.bzl b/rules/certificates.bzl index ce71a6af1f68d..6658892dcad84 100644 --- a/rules/certificates.bzl +++ b/rules/certificates.bzl @@ -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, diff --git a/rules/opentitan/legacy.bzl b/rules/opentitan/legacy.bzl index 6458665d4bec1..03409861e1ab1 100644 --- a/rules/opentitan/legacy.bzl +++ b/rules/opentitan/legacy.bzl @@ -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"], ) diff --git a/rules/opentitan/static_library.bzl b/rules/opentitan/static_library.bzl index f4b10ca2d2ebb..fb12a26e9d2ba 100644 --- a/rules/opentitan/static_library.bzl +++ b/rules/opentitan/static_library.bzl @@ -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") @@ -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 = [] @@ -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), ), diff --git a/rules/opentitan/transform.bzl b/rules/opentitan/transform.bzl index 7985a58354d0c..52010fe1925e0 100644 --- a/rules/opentitan/transform.bzl +++ b/rules/opentitan/transform.bzl @@ -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. @@ -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) @@ -40,7 +53,7 @@ def obj_transform(ctx, **kwargs): src.path, output.path, ], - executable = cc_toolchain.objcopy_executable, + executable = objcopy, ) return output @@ -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) @@ -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, ], diff --git a/rules/otbn.bzl b/rules/otbn.bzl index 81b249eafbf88..9c78e40f2bc8a 100644 --- a/rules/otbn.bzl +++ b/rules/otbn.bzl @@ -5,15 +5,6 @@ load("//rules:rv.bzl", "rv_rule") load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain") -def _get_assembler(cc_toolchain): - """Find the path to riscv-unknown-elf-as.""" - - # Note: the toolchain config doesn"t appear to have a good way to get - # access to the assembler. We should be able to access it via the - # the compiler, but I had trouble with //hw/ip/otbn/util/otbn_as.py invoking - # the compiler as assembler. - return [f for f in cc_toolchain.all_files.to_list() if f.basename.endswith("as")][0] - def _otbn_assemble_sources(ctx, additional_srcs = []): """Helper function that, for each source file in the provided context, adds an action to the context that invokes the otbn assember (otbn_as.py), @@ -21,7 +12,6 @@ def _otbn_assemble_sources(ctx, additional_srcs = []): that will be generated by these actions. """ cc_toolchain = find_cc_toolchain(ctx) - assembler = _get_assembler(cc_toolchain) objs = [] for src in ctx.files.srcs + additional_srcs: @@ -33,7 +23,7 @@ def _otbn_assemble_sources(ctx, additional_srcs = []): cc_toolchain.all_files.to_list() + [ctx.executable._otbn_as]), env = { - "RV32_TOOL_AS": assembler.path, + "RV32_TOOL_AS": ctx.executable._riscv32_as.path, }, arguments = ["-o", obj.path, src.path] + ctx.attr.args, executable = ctx.executable._otbn_as, @@ -78,7 +68,6 @@ def _otbn_binary(ctx, additional_srcs = []): that other rules can depend on in their `deps`. """ cc_toolchain = find_cc_toolchain(ctx) - assembler = _get_assembler(cc_toolchain) # Run the otbn assembler on source files to produce object (.o) files. objs = _otbn_assemble_sources(ctx, additional_srcs) @@ -100,10 +89,10 @@ def _otbn_binary(ctx, additional_srcs = []): ctx.files._otbn_data + [ctx.executable._wrapper]), env = { - "RV32_TOOL_AS": assembler.path, - "RV32_TOOL_AR": cc_toolchain.ar_executable, - "RV32_TOOL_LD": cc_toolchain.ld_executable, - "RV32_TOOL_OBJCOPY": cc_toolchain.objcopy_executable, + "RV32_TOOL_AS": ctx.executable._riscv32_as.path, + "RV32_TOOL_AR": ctx.executable._riscv32_ar.path, + "RV32_TOOL_LD": ctx.executable._riscv32_ld.path, + "RV32_TOOL_OBJCOPY": ctx.executable._riscv32_objcopy.path, }, arguments = [ "--app-name={}".format(ctx.attr.name), @@ -286,8 +275,11 @@ otbn_library = rv_rule( attrs = { "srcs": attr.label_list(allow_files = True), "args": attr.string_list(), - "_cc_toolchain": attr.label( - default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"), + "_riscv32_as": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as"), + allow_single_file = True, + executable = True, + cfg = "exec", ), "_otbn_as": attr.label( default = "//hw/ip/otbn/util:otbn_as", @@ -306,7 +298,30 @@ otbn_binary = rv_rule( "srcs": attr.label_list(allow_files = True), "deps": attr.label_list(providers = [DefaultInfo]), "args": attr.string_list(), - "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), + "_riscv32_ar": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ar"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_riscv32_as": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_riscv32_ld": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ld"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_riscv32_objcopy": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-objcopy"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), "_otbn_as": attr.label( default = "//hw/ip/otbn/util:otbn_as", executable = True, @@ -335,7 +350,30 @@ otbn_sim_test = rv_rule( "deps": attr.label_list(providers = [DefaultInfo]), "exp": attr.label(allow_single_file = True), "dexp": attr.label(allow_single_file = True), - "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), + "_riscv32_ar": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ar"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_riscv32_as": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_riscv32_ld": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ld"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_riscv32_objcopy": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-objcopy"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), "_otbn_as": attr.label( default = "//hw/ip/otbn/util:otbn_as", executable = True, @@ -372,7 +410,30 @@ otbn_autogen_sim_test = rv_rule( attrs = { "srcs": attr.label_list(allow_files = True), "deps": attr.label_list(providers = [DefaultInfo]), - "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), + "_riscv32_ar": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ar"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_riscv32_as": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_riscv32_ld": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ld"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), + "_riscv32_objcopy": attr.label( + default = Label("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-objcopy"), + allow_single_file = True, + executable = True, + cfg = "exec", + ), "testgen": attr.label( mandatory = True, executable = True, diff --git a/rules/quality.bzl b/rules/quality.bzl index 3da4903b9f7f6..3d99fadd1a504 100644 --- a/rules/quality.bzl +++ b/rules/quality.bzl @@ -64,7 +64,7 @@ clang_format_attrs = { doc = "Command to execute to display diffs", ), "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, @@ -192,7 +192,7 @@ def _cc_aspect_impl(target, ctx, action_callback): # To see which checks clang-tidy knows about, run this command: # -# ./bazelisk.sh run @lowrisc_rv32imcb_files//:bin/clang-tidy -- --checks='*' --list-checks +# ./bazelisk.sh run @lowrisc_rv32imcb_toolchain//:bin/clang-tidy -- --checks='*' --list-checks _CLANG_TIDY_CHECKS = [ "clang-analyzer-core.*", # Disable advice to replace `memcpy` with `mempcy_s`. @@ -265,7 +265,7 @@ def _make_clang_tidy_aspect(enable_fix): executable = True, ), "_clang_tidy": attr.label( - default = "@lowrisc_rv32imcb_files//:bin/clang-tidy", + default = "@lowrisc_rv32imcb_toolchain//:bin/clang-tidy", allow_single_file = True, cfg = "host", executable = True, diff --git a/rules/rv.bzl b/rules/rv.bzl index 48368231fb30b..9ea767f5321b7 100644 --- a/rules/rv.bzl +++ b/rules/rv.bzl @@ -5,7 +5,7 @@ """Helpers for transitioning to the RISC-V target.""" OPENTITAN_CPU = "@platforms//cpu:riscv32" -OPENTITAN_PLATFORM = "@crt//platforms/riscv32:opentitan" +OPENTITAN_PLATFORM = "//toolchain:opentitan_platform" # This constant holds a dictionary of per-device dependencies which are used to # generate slightly different binaries for each hardware target, including two diff --git a/rules/tock.bzl b/rules/tock.bzl index 6ff06bc662793..a1aa1ec7e1386 100644 --- a/rules/tock.bzl +++ b/rules/tock.bzl @@ -5,6 +5,7 @@ """Rules for assembling Tock binaries. """ +load("@rules_cc//cc:action_names.bzl", "OBJ_COPY_ACTION_NAME") load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain") load( "//rules:rv.bzl", @@ -98,6 +99,16 @@ opt_mode = transition( def _tock_image_impl(ctx): 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, + ) kernel_binary = ctx.actions.declare_file("{}_kernel.bin".format(ctx.attr.name)) images = [ctx.actions.declare_file("{}0.bin".format(ctx.attr.name))] @@ -110,7 +121,7 @@ def _tock_image_impl(ctx): ctx.file.kernel.path, kernel_binary.path, ], - executable = cc_toolchain.objcopy_executable, + executable = objcopy, ) ctx.actions.run( @@ -187,5 +198,6 @@ tock_image = rv_rule( cfg = "exec", ), }, + fragments = ["cpp"], toolchains = ["@rules_cc//cc:toolchain_type"], ) diff --git a/sw/device/silicon_creator/imm_rom_ext/utils.bzl b/sw/device/silicon_creator/imm_rom_ext/utils.bzl index e5a55923ce671..1c881c2860ae2 100644 --- a/sw/device/silicon_creator/imm_rom_ext/utils.bzl +++ b/sw/device/silicon_creator/imm_rom_ext/utils.bzl @@ -2,11 +2,23 @@ # Licensed under the Apache License, Version 2.0, see LICENSE for details. # SPDX-License-Identifier: Apache-2.0 +load("@rules_cc//cc:action_names.bzl", "OBJ_COPY_ACTION_NAME") load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain") load("@lowrisc_opentitan//rules:rv.bzl", "rv_rule") def _bin_to_imm_rom_ext_object_impl(ctx): 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, + ) + outputs = [] for src in ctx.files.srcs: if src.extension != "bin": @@ -30,7 +42,7 @@ def _bin_to_imm_rom_ext_object_impl(ctx): src.path, object.path, ], - executable = cc_toolchain.objcopy_executable, + executable = objcopy, ) outputs.append(object) return [DefaultInfo(files = depset(outputs), runfiles = ctx.runfiles(files = outputs))] @@ -41,6 +53,7 @@ bin_to_imm_rom_ext_object = rv_rule( "srcs": attr.label_list(allow_files = True), "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), }, + fragments = ["cpp"], toolchains = ["@rules_cc//cc:toolchain_type"], ) diff --git a/third_party/coremark/BUILD.coremark.bazel b/third_party/coremark/BUILD.coremark.bazel index bf306721651c2..a65cba8df203d 100644 --- a/third_party/coremark/BUILD.coremark.bazel +++ b/third_party/coremark/BUILD.coremark.bazel @@ -16,9 +16,12 @@ cc_library( hdrs = [ "coremark.h", ], - features = [ - "-implicit_conversion_warnings", - "-strict_prototypes_warnings", + copts = [ + "-Wno-implicit-fallthrough", + "-Wno-strict-prototypes", + "-Wno-implicit-int-conversion", + "-Wno-sign-conversion", + "-Wno-shorten-64-to-32", ], includes = ["."], deps = [ diff --git a/third_party/crt/repos.bzl b/third_party/crt/repos.bzl deleted file mode 100644 index df14dd639e60c..0000000000000 --- a/third_party/crt/repos.bzl +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright lowRISC contributors (OpenTitan project). -# Licensed under the Apache License, Version 2.0, see LICENSE for details. -# SPDX-License-Identifier: Apache-2.0 - -load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -def crt_repos(local = None): - if local: - native.local_repository( - name = "crt", - path = local, - ) - else: - maybe( - http_archive, - name = "crt", - url = "https://github.com/lowRISC/crt/archive/refs/tags/v0.4.13.tar.gz", - strip_prefix = "crt-0.4.13", - sha256 = "9813c8a3aeb72c3951d305e19a69dbbd17a3a6313bef938c20b1e214eb51e6e6", - ) diff --git a/third_party/lowrisc/BUILD b/third_party/lowrisc/BUILD new file mode 100644 index 0000000000000..3e8bb41965a28 --- /dev/null +++ b/third_party/lowrisc/BUILD @@ -0,0 +1,3 @@ +# Copyright lowRISC contributors (OpenTitan project). +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 diff --git a/third_party/lowrisc/BUILD.lowrisc_rv32imcb_toolchain.bazel b/third_party/lowrisc/BUILD.lowrisc_rv32imcb_toolchain.bazel new file mode 100644 index 0000000000000..ec3250dcdd303 --- /dev/null +++ b/third_party/lowrisc/BUILD.lowrisc_rv32imcb_toolchain.bazel @@ -0,0 +1,67 @@ +# Copyright lowRISC contributors (OpenTitan project). +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +load("@bazel_skylib//rules/directory:directory.bzl", "directory") +load("@bazel_skylib//rules/directory:subdirectory.bzl", "subdirectory") +load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool") + +package(default_visibility = ["//visibility:public"]) + +exports_files(glob(["**"])) + +# Define certain binaries as `cc_tool`s so they can be used in a toolchain. +# Each tool has access to all the other libraries and binaries in this repository +# because some tools execute one another and access various libraries. +[ + cc_tool( + name = tool, + src = ":bin/riscv32-unknown-elf-{}".format(tool), + data = [":root"], + ) + for tool in [ + "clang", + "clang++", + "ar", + "objcopy", + "objdump", + "strip", + ] +] + +directory( + name = "root", + srcs = glob(["**/*"]), +) + +# System library include directories (for `-isystem`): + +subdirectory( + name = "lib-clang-include", + parent = ":root", + path = "lib/clang/16/include", +) + +subdirectory( + name = "riscv32-unknown-elf-include", + parent = ":root", + path = "riscv32-unknown-elf/include", +) + +subdirectory( + name = "cxx-include", + parent = ":root", + path = "riscv32-unknown-elf/include/c++/10.2.0", +) + +subdirectory( + name = "cxx-backward", + parent = ":root", + path = "riscv32-unknown-elf/include/c++/10.2.0/backward", +) + +subdirectory( + name = "cxx-riscv32-unknown-elf", + parent = ":root", + path = "riscv32-unknown-elf/include/c++/10.2.0/riscv32-unknown-elf", +) diff --git a/third_party/lowrisc/extensions.bzl b/third_party/lowrisc/extensions.bzl new file mode 100644 index 0000000000000..c75bdc5a8f2d1 --- /dev/null +++ b/third_party/lowrisc/extensions.bzl @@ -0,0 +1,19 @@ +# Copyright lowRISC contributors (OpenTitan project). +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +def _lowrisc_repos(): + VERSION = "20240923-1" + http_archive( + name = "lowrisc_rv32imcb_toolchain", + url = "https://github.com/lowRISC/lowrisc-toolchains/releases/download/{v}/lowrisc-toolchain-rv32imcb-{v}.tar.xz".format(v = VERSION), + sha256 = "aeea1983553f4c81c6409abcf0d6ca33b5ed4716b2b694e7ff030523cf13486a", + strip_prefix = "lowrisc-toolchain-rv32imcb-{}".format(VERSION), + build_file = ":BUILD.lowrisc_rv32imcb_toolchain.bazel", + ) + +lowrisc_rv32imcb_toolchain = module_extension( + implementation = lambda _: _lowrisc_repos(), +) diff --git a/toolchain/BUILD b/toolchain/BUILD new file mode 100644 index 0000000000000..9ede21cd85d57 --- /dev/null +++ b/toolchain/BUILD @@ -0,0 +1,376 @@ +# Copyright lowRISC contributors (OpenTitan project). +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +load("@rules_cc//cc/toolchains:actions.bzl", "cc_action_type") +load("@rules_cc//cc/toolchains:args.bzl", "cc_args") +load("@rules_cc//cc/toolchains:args_list.bzl", "cc_args_list") +load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature") +load("@rules_cc//cc/toolchains:feature_set.bzl", "cc_feature_set") +load("@rules_cc//cc/toolchains:feature_constraint.bzl", "cc_feature_constraint") +load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain") +load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map") +load("//rules:actions.bzl", "OT_ACTION_OBJDUMP") + +platform( + name = "opentitan_platform", + constraint_values = [ + "@platforms//cpu:riscv32", + "@platforms//os:none", + ], +) + +toolchain( + name = "cc_toolchain_opentitan", + exec_compatible_with = ["@platforms//cpu:x86_64"], + target_compatible_with = [ + "@platforms//cpu:riscv32", + "@platforms//os:none", + ], + toolchain = ":opentitan_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + +# Collects a set of tools, arguments, and features into a C/C++ toolchain. +cc_toolchain( + name = "opentitan_toolchain", + args = [ + ":system_includes", + ":arch_rv32imc", + ":arch_rv32imcb", + ":abi", + ":sys_spec", + ":warnings", + ":reproducible", + ":symbol_garbage_collection", + ":constructor_destructor", + ":nostdinc", + ], + enabled_features = [ + "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features", + ":feat_rv32_bitmanip", + ":feat_warnings_as_errors", + ":feat_guards", + ], + known_features = [ + "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features", + ":feat_warnings_as_errors", + ":feat_guards", + ":feat_use_lld", + ":feat_lto", + ":feat_minsize", + ":feat_dbg", + ":feat_fastbuild", + ":feat_opt", + ":feat_rv32_bitmanip", + ], + tool_map = ":tool_map", +) + +# Map from C/C++ compilation actions to tools. +cc_tool_map( + name = "tool_map", + tools = { + "@rules_cc//cc/toolchains/actions:ar_actions": "@lowrisc_rv32imcb_toolchain//:ar", + "@rules_cc//cc/toolchains/actions:assembly_actions": "@lowrisc_rv32imcb_toolchain//:clang", + "@rules_cc//cc/toolchains/actions:c_compile": "@lowrisc_rv32imcb_toolchain//:clang", + "@rules_cc//cc/toolchains/actions:cpp_compile_actions": "@lowrisc_rv32imcb_toolchain//:clang++", + "@rules_cc//cc/toolchains/actions:link_actions": "@lowrisc_rv32imcb_toolchain//:clang", + "@rules_cc//cc/toolchains/actions:objcopy_embed_data": "@lowrisc_rv32imcb_toolchain//:objcopy", + "@rules_cc//cc/toolchains/actions:strip": "@lowrisc_rv32imcb_toolchain//:strip", + ":action_objdump": "@lowrisc_rv32imcb_toolchain//:objdump", + }, +) + +cc_action_type( + name = "action_objdump", + action_name = OT_ACTION_OBJDUMP, +) + +# Built-in Bazel feature enabled for optimized builds. +cc_feature( + name = "feat_opt", + args = [ + ":opt", + ":lto", + ], + overrides = "@rules_cc//cc/toolchains/features:opt", +) + +cc_args( + name = "opt", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + "-Os", + "-g", + "-gdwarf-4", + ], +) + +# Built-in Bazel feature enabled for debug builds. +cc_feature( + name = "feat_dbg", + args = [":dbg"], + overrides = "@rules_cc//cc/toolchains/features:dbg", +) + +cc_args( + name = "dbg", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + "-O0", + "-g3", + ], +) + +# Built-in Bazel feature for fast builds. +cc_feature( + name = "feat_fastbuild", + args = [":fastbuild"], + overrides = "@rules_cc//cc/toolchains/features:fastbuild", +) + +cc_args( + name = "fastbuild", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + "-Os", + "-g", + "-gdwarf-4", + ], +) + +cc_args( + name = "nostdinc", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = ["-nostdinc"], +) + +cc_args( + name = "system_includes", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + "-isystem", + "{clang-include}", + "-isystem", + "{riscv32-include}", + "-isystem", + "{cxx-include}", + "-isystem", + "{cxx-backward}", + "-isystem", + "{cxx-riscv32}", + ], + data = [ + "@lowrisc_rv32imcb_toolchain//:cxx-backward", + "@lowrisc_rv32imcb_toolchain//:cxx-include", + "@lowrisc_rv32imcb_toolchain//:cxx-riscv32-unknown-elf", + "@lowrisc_rv32imcb_toolchain//:lib-clang-include", + "@lowrisc_rv32imcb_toolchain//:riscv32-unknown-elf-include", + ], + format = { + "clang-include": "@lowrisc_rv32imcb_toolchain//:lib-clang-include", + "riscv32-include": "@lowrisc_rv32imcb_toolchain//:riscv32-unknown-elf-include", + "cxx-include": "@lowrisc_rv32imcb_toolchain//:cxx-include", + "cxx-backward": "@lowrisc_rv32imcb_toolchain//:cxx-backward", + "cxx-riscv32": "@lowrisc_rv32imcb_toolchain//:cxx-riscv32-unknown-elf", + }, +) + +# This feature can be disabled to remove the bitmanip extensions. +cc_feature( + name = "feat_rv32_bitmanip", + feature_name = "rv32_bitmanip", +) + +cc_feature_constraint( + name = "constraint_bitmanip", + all_of = [":feat_rv32_bitmanip"], +) + +cc_feature_constraint( + name = "constraint_no_bitmanip", + none_of = [":feat_rv32_bitmanip"], +) + +cc_args( + name = "arch_rv32imcb", + actions = [ + "@rules_cc//cc/toolchains/actions:compile_actions", + "@rules_cc//cc/toolchains/actions:link_actions", + ], + args = ["-march=rv32imc_zba_zbb_zbc_zbs"], + requires_any_of = [":constraint_bitmanip"], +) + +cc_args( + name = "arch_rv32imc", + actions = [ + "@rules_cc//cc/toolchains/actions:compile_actions", + "@rules_cc//cc/toolchains/actions:link_actions", + ], + args = ["-march=rv32imc"], + requires_any_of = [":constraint_no_bitmanip"], +) + +cc_args( + name = "abi", + actions = [ + "@rules_cc//cc/toolchains/actions:compile_actions", + "@rules_cc//cc/toolchains/actions:link_actions", + ], + args = [ + "-mabi=ilp32", + "-mcmodel=medany", + "-mlittle-endian", + ], +) + +cc_args( + name = "warnings", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + "-Wall", + "-Wconversion", + "-Wextra", + "-Wgnu", + "-Wimplicit-fallthrough", + "-Winvalid-pch", + "-Wno-covered-switch-default", + "-Wno-error=unused-function", + "-Wno-gnu-statement-expression-from-macro-expansion", + "-Wno-gnu-zero-variadic-macro-arguments", + "-Wno-missing-field-initializers", + "-Wno-sign-compare", + "-Wno-unused-parameter", + "-Wstrict-prototypes", + "-Wswitch-default", + ], +) + +# Feature which turns all warnings into fatal errors. +cc_feature( + name = "feat_warnings_as_errors", + args = [":warnings_as_errors"], + feature_name = "warnings_as_errors", +) + +cc_args( + name = "warnings_as_errors", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = ["-Werror"], +) + +cc_args( + name = "reproducible", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = ["-Werror=date-time"], +) + +cc_feature( + name = "feat_guards", + args = [":guards"], + feature_name = "guards", +) + +cc_args( + name = "guards", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = ["-mguards"], +) + +cc_feature( + name = "feat_use_lld", + args = [":use_lld"], + feature_name = "use_lld", +) + +cc_args( + name = "use_lld", + actions = ["@rules_cc//cc/toolchains/actions:link_actions"], + args = ["-fuse-ld=lld"], +) + +cc_feature( + name = "feat_lto", + args = [":lto"], + feature_name = "lto", +) + +cc_args( + name = "lto", + actions = [ + "@rules_cc//cc/toolchains/actions:compile_actions", + "@rules_cc//cc/toolchains/actions:link_actions", + ], + args = ["-flto"], +) + +cc_feature( + name = "feat_minsize", + args = [ + ":minsize_cc", + ":minsize_ld", + ], + feature_name = "minsize", +) + +cc_args( + name = "minsize_cc", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + "-Oz", + # Inline slightly more which is actually smaller. + "-mllvm", + "--inline-threshold=10", + ], +) + +cc_args( + name = "minsize_ld", + actions = ["@rules_cc//cc/toolchains/actions:link_actions"], + args = [ + "-Wl,-mllvm", + "-Wl,--inline-threshold=10", + ], +) + +cc_args( + name = "unique_sections", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + "-ffunction-sections", + "-fdata-sections", + ], +) + +cc_args( + name = "gc_sections", + actions = ["@rules_cc//cc/toolchains/actions:link_actions"], + args = ["-Wl,--gc-sections"], +) + +cc_args_list( + name = "symbol_garbage_collection", + args = [ + ":unique_sections", + ":gc_sections", + ], +) + +cc_args( + name = "sys_spec", + actions = ["@rules_cc//cc/toolchains/actions:link_actions"], + args = ["-lnosys"], +) + +cc_args( + name = "constructor_destructor", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = [ + # Indicate that this program may not neccesarily be able to use standard system calls. + "-ffreestanding", + # Instantiate global variables only once. + "-fno-common", + ], +) diff --git a/toolchain/README.md b/toolchain/README.md new file mode 100644 index 0000000000000..1336b4c1d50c4 --- /dev/null +++ b/toolchain/README.md @@ -0,0 +1,31 @@ +# OpenTitan RISC-V toolchain + +This directory contains the Bazel configuration for OpenTitan's RISC-V +toolchain. + +This LLVM toolchain comes from the [lowrisc-toolchains] repository. See +`third_party/lowrisc/BUILD.lowrisc_toolchain.bazel` for changing the toolchain +version. + +[lowrisc-toolchains]: https://github.com/lowRISC/lowrisc-toolchains + +## Configuration + +There are four rules used to configure the toolchain: + +1. `cc_toolchain`: groups flags, features, and tools into a toolchain. +2. `cc_tool_map`: assigns tools to actions. +3. `cc_args`: defines flags to add to tools based on actions. +3. `cc_feature`: allows `cc_args` flags to be conditionally enabled. + +To add new flags to a tool in the toolchain, define a new `cc_args` target +and assign it to some actions (e.g. compiling C code, linking, etc.). Add the +new flags to `cc_toolchain.args`. + +To make flags optional, define a new `cc_feature` for those `cc_args`. Features +can be enabled at the command line using `bazel --features=$feature_name`. Add +the flags to `cc_toolchain.known_features` and optionally to +`cc_toolchain.enabled_features`. + +Bazel has three built-in features called `dbg`, `fastbuild`, and `opt` that can +be used to enable and disable flags at different optimization levels. diff --git a/util/prep-bazel-airgapped-build.sh b/util/prep-bazel-airgapped-build.sh index ad495b5dea121..a93905e9a225c 100755 --- a/util/prep-bazel-airgapped-build.sh +++ b/util/prep-bazel-airgapped-build.sh @@ -141,13 +141,10 @@ if [[ ${AIRGAPPED_DIR_CONTENTS} == "ALL" || \ ${BAZELISK} fetch \ --repository_cache=${BAZEL_AIRGAPPED_DIR}/${BAZEL_CACHEDIR} \ //... \ - @lowrisc_rv32imcb_files//... \ + @lowrisc_rv32imcb_toolchain//... \ @local_config_platform//... \ - @python3_toolchains//... \ @riscv-compliance//... \ @rules_foreign_cc//toolchains/... \ - @ninja_1.11.0_linux//... \ - @cmake-3.23.2-linux-x86_64//... \ # We don't need all bitstreams in the cache, we just need the latest one so # that the cache is "initialized" and "offline" mode will work correctly. mkdir -p ${BAZEL_AIRGAPPED_DIR}/${BAZEL_BITSTREAMS_CACHEDIR}