| # Copyright lowRISC contributors. |
| # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| # SPDX-License-Identifier: Apache-2.0 |
| |
| load("@nonhermetic//:env.bzl", "ENV") |
| load("@ot_python_deps//:requirements.bzl", "entry_point") |
| |
| """Rules for running FuseSoC. |
| |
| FuseSoC is a package manager and set of build tools for HDL code. |
| |
| Because we want the output of some FuseSoC built resources to be |
| available to bazel (such as the verilated chip model for running |
| tests), the `fusesoc_build` rule allows bazel to delegate certain |
| targets to FuseSoC. |
| |
| This rule is not sandboxed, as our current configuration depends |
| on FuseSoC and its dependencies (verible, verilator, etc) already |
| having been installed. In the future, we will try to rework our |
| dependencies so the FuseSoC rules can be sandboxed. |
| """ |
| |
| load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") |
| |
| def _corefiles2rootarg(core): |
| return core.dirname |
| |
| def _fusesoc_build_impl(ctx): |
| dirname = "build.{}".format(ctx.label.name) |
| out_dir = ctx.actions.declare_directory(dirname) |
| flags = [ctx.expand_location(f, ctx.attr.srcs) for f in ctx.attr.flags] |
| outputs = [out_dir] |
| groups = {} |
| args = ctx.actions.args() |
| |
| for group, files in ctx.attr.output_groups.items(): |
| deps = [ctx.actions.declare_file("{}/{}".format(dirname, f)) for f in files] |
| outputs.extend(deps) |
| groups[group] = depset(deps) |
| |
| if ctx.attr.verilator_options: |
| verilator_options = ctx.attr.verilator_options[BuildSettingInfo].value |
| flags.append("--verilator_options={}".format(" ".join(verilator_options))) |
| |
| if ctx.attr.make_options: |
| make_options = ctx.attr.make_options[BuildSettingInfo].value |
| flags.append("--make_options={}".format(" ".join(make_options))) |
| |
| args.add_all( |
| ctx.files.cores, |
| uniquify = True, |
| map_each = _corefiles2rootarg, |
| format_each = "--cores-root=%s", |
| ) |
| |
| args.add_all([ |
| "run", |
| "--flag=fileset_top", |
| ]) |
| args.add(ctx.attr.target, format = "--target=%s") |
| args.add_all([ |
| "--setup", |
| "--build", |
| ]) |
| args.add(out_dir.path, format = "--build-root=%s") |
| |
| args.add_all(ctx.attr.systems) |
| args.add_all(flags) |
| |
| # Note: the `fileset_top` flag used above is specific to the OpenTitan |
| # project to select the correct RTL fileset. |
| ctx.actions.run( |
| mnemonic = "FuseSoC", |
| outputs = outputs, |
| inputs = ctx.files.srcs + ctx.files.cores + ctx.files._fusesoc, |
| arguments = [args], |
| executable = ctx.executable._fusesoc, |
| use_default_shell_env = False, |
| execution_requirements = { |
| "no-sandbox": "", |
| }, |
| env = ENV, |
| ) |
| return [ |
| DefaultInfo( |
| files = depset(outputs), |
| data_runfiles = ctx.runfiles(files = outputs + ctx.files.data), |
| ), |
| OutputGroupInfo(**groups), |
| ] |
| |
| fusesoc_build = rule( |
| implementation = _fusesoc_build_impl, |
| attrs = { |
| "cores": attr.label_list(allow_files = True, doc = "FuseSoC core specification files"), |
| "srcs": attr.label_list(allow_files = True, doc = "Source files"), |
| "data": attr.label_list(allow_files = True, doc = "Files needed at runtime"), |
| "target": attr.string(mandatory = True, doc = "Target name (e.g. 'sim')"), |
| "systems": attr.string_list(mandatory = True, doc = "Systems to build"), |
| "flags": attr.string_list(doc = "Flags controlling the FuseSOC system build"), |
| "output_groups": attr.string_list_dict( |
| allow_empty = True, |
| doc = "Mapping of group name to lists of files in that named group", |
| ), |
| "verilator_options": attr.label(), |
| "make_options": attr.label(), |
| "_fusesoc": attr.label( |
| default = entry_point("fusesoc"), |
| executable = True, |
| cfg = "exec", |
| ), |
| }, |
| ) |