blob: 242bad340be8877a71662362ecba8b00e4e41dc5 [file] [log] [blame]
Chris Frantz6da32ea2021-11-22 13:12:08 -08001# Copyright lowRISC contributors.
2# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3# SPDX-License-Identifier: Apache-2.0
4
Chris Frantz74f90952022-04-18 20:57:01 -07005load("@nonhermetic//:env.bzl", "ENV")
Alexander Williamsdb7e7442022-12-14 14:47:19 -08006load("@ot_python_deps//:requirements.bzl", "entry_point")
Chris Frantz74f90952022-04-18 20:57:01 -07007
Chris Frantz6da32ea2021-11-22 13:12:08 -08008"""Rules for running FuseSoC.
9
10FuseSoC is a package manager and set of build tools for HDL code.
11
12Because we want the output of some FuseSoC built resources to be
13available to bazel (such as the verilated chip model for running
14tests), the `fusesoc_build` rule allows bazel to delegate certain
15targets to FuseSoC.
16
17This rule is not sandboxed, as our current configuration depends
18on FuseSoC and its dependencies (verible, verilator, etc) already
19having been installed. In the future, we will try to rework our
20dependencies so the FuseSoC rules can be sandboxed.
21"""
22
Miles Dai99f50442022-05-09 18:44:19 -040023load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
24
Drew Macrae18c77fa2022-10-18 10:57:08 -040025def _corefiles2rootarg(core):
26 return core.dirname
27
Chris Frantz6da32ea2021-11-22 13:12:08 -080028def _fusesoc_build_impl(ctx):
Chris Frantz74f90952022-04-18 20:57:01 -070029 dirname = "build.{}".format(ctx.label.name)
30 out_dir = ctx.actions.declare_directory(dirname)
31 flags = [ctx.expand_location(f, ctx.attr.srcs) for f in ctx.attr.flags]
32 outputs = [out_dir]
33 groups = {}
Drew Macrae18c77fa2022-10-18 10:57:08 -040034 args = ctx.actions.args()
Drew Macraeacfd5d02022-10-31 22:39:44 -040035
Chris Frantz74f90952022-04-18 20:57:01 -070036 for group, files in ctx.attr.output_groups.items():
37 deps = [ctx.actions.declare_file("{}/{}".format(dirname, f)) for f in files]
38 outputs.extend(deps)
39 groups[group] = depset(deps)
40
Miguel Osorio3f0e5392022-06-09 09:26:53 -070041 if ctx.attr.verilator_options:
42 verilator_options = ctx.attr.verilator_options[BuildSettingInfo].value
43 flags.append("--verilator_options={}".format(" ".join(verilator_options)))
Miles Dai99f50442022-05-09 18:44:19 -040044
Drew Macraeacfd5d02022-10-31 22:39:44 -040045 if ctx.attr.make_options:
46 make_options = ctx.attr.make_options[BuildSettingInfo].value
47 flags.append("--make_options={}".format(" ".join(make_options)))
48
Drew Macrae18c77fa2022-10-18 10:57:08 -040049 args.add_all(
50 ctx.files.cores,
51 uniquify = True,
52 map_each = _corefiles2rootarg,
53 format_each = "--cores-root=%s",
54 )
55
56 args.add_all([
57 "run",
58 "--flag=fileset_top",
59 ])
60 args.add(ctx.attr.target, format = "--target=%s")
61 args.add_all([
62 "--setup",
63 "--build",
64 ])
65 args.add(out_dir.path, format = "--build-root=%s")
66
67 args.add_all(ctx.attr.systems)
68 args.add_all(flags)
Drew Macrae0e5292a2022-10-12 15:20:00 -040069
Yenkai Wangd7cf4dc2023-03-15 13:13:40 -070070 _inputs = ctx.files.srcs + ctx.files.cores
71
72 # For some reason, the sanboxed fusesoc would call vivado twice. Add an
73 # option to use the system installed fusesoc.
74 if ctx.attr.use_system_fusesoc:
75 _exec = "fusesoc"
76 else:
77 _exec = ctx.executable._fusesoc
78 _inputs += ctx.files._fusesoc
79
Timothy Trippelaa870692022-06-09 16:38:23 -070080 # Note: the `fileset_top` flag used above is specific to the OpenTitan
81 # project to select the correct RTL fileset.
Chris Frantz6da32ea2021-11-22 13:12:08 -080082 ctx.actions.run(
83 mnemonic = "FuseSoC",
Chris Frantz74f90952022-04-18 20:57:01 -070084 outputs = outputs,
Yenkai Wangd7cf4dc2023-03-15 13:13:40 -070085 inputs = _inputs,
Drew Macrae18c77fa2022-10-18 10:57:08 -040086 arguments = [args],
Yenkai Wangd7cf4dc2023-03-15 13:13:40 -070087 executable = _exec,
Chris Frantz74f90952022-04-18 20:57:01 -070088 use_default_shell_env = False,
Chris Frantz6da32ea2021-11-22 13:12:08 -080089 execution_requirements = {
90 "no-sandbox": "",
91 },
Chris Frantz74f90952022-04-18 20:57:01 -070092 env = ENV,
Chris Frantz6da32ea2021-11-22 13:12:08 -080093 )
Chris Frantz74f90952022-04-18 20:57:01 -070094 return [
95 DefaultInfo(
96 files = depset(outputs),
97 data_runfiles = ctx.runfiles(files = outputs + ctx.files.data),
98 ),
99 OutputGroupInfo(**groups),
100 ]
Chris Frantz6da32ea2021-11-22 13:12:08 -0800101
102fusesoc_build = rule(
103 implementation = _fusesoc_build_impl,
104 attrs = {
105 "cores": attr.label_list(allow_files = True, doc = "FuseSoC core specification files"),
106 "srcs": attr.label_list(allow_files = True, doc = "Source files"),
Chris Frantz9b34e4a2021-11-24 17:03:12 -0800107 "data": attr.label_list(allow_files = True, doc = "Files needed at runtime"),
Chris Frantz6da32ea2021-11-22 13:12:08 -0800108 "target": attr.string(mandatory = True, doc = "Target name (e.g. 'sim')"),
109 "systems": attr.string_list(mandatory = True, doc = "Systems to build"),
Chris Frantz74f90952022-04-18 20:57:01 -0700110 "flags": attr.string_list(doc = "Flags controlling the FuseSOC system build"),
111 "output_groups": attr.string_list_dict(
112 allow_empty = True,
113 doc = "Mapping of group name to lists of files in that named group",
114 ),
Miles Dai99f50442022-05-09 18:44:19 -0400115 "verilator_options": attr.label(),
Drew Macraeacfd5d02022-10-31 22:39:44 -0400116 "make_options": attr.label(),
Yenkai Wangd7cf4dc2023-03-15 13:13:40 -0700117 "use_system_fusesoc": attr.bool(
118 default = False,
119 doc = "Use non-sanboxed fusesoc (for FPGA vivado build)",
120 ),
Alexander Williamsdb7e7442022-12-14 14:47:19 -0800121 "_fusesoc": attr.label(
122 default = entry_point("fusesoc"),
123 executable = True,
124 cfg = "exec",
125 ),
Chris Frantz6da32ea2021-11-22 13:12:08 -0800126 },
127)