Skip to content

Commit

Permalink
[bazel,top] Rework top selection mecanism to use string_flag
Browse files Browse the repository at this point in the history
Signed-off-by: Amaury Pouly <[email protected]>
  • Loading branch information
pamaury committed Nov 25, 2024
1 parent 7fdb846 commit e017fb5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
51 changes: 33 additions & 18 deletions hw/top/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load(
"//rules:autogen.bzl",
"opentitan_ip_c_header",
Expand All @@ -14,42 +15,56 @@ load("//hw/top:defs.bzl", IPS = "OPENTITAN_ALL_IPS")

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

# FIXME rework that properly with platforms
constraint_setting(
name = "top_setting",
default_constraint_value = ":top_earlgrey",
)
ALL_TOPS = [
"earlgrey",
"darjeeling",
]

constraint_value(
name = "top_earlgrey",
constraint_setting = ":top_setting",
# Use this flag to select the top.
string_flag(
name = "top",
build_setting_default = "earlgrey",
values = ALL_TOPS,
)

# Config settings to test against tops.
[
config_setting(
name = "is_{}".format(top),
flag_values = {
":top": top,
},
)
for top in ALL_TOPS
]

alias(
name = "top",
name = "top_desc",
actual = select(
{
":top_earlgrey": "//hw/top_earlgrey",
":is_earlgrey": "//hw/top_earlgrey",
":is_darjeeling": "//hw/top_darjeeling",
},
no_match_error = "no top selected",
no_match_error = "unknown top",
),
)

alias(
name = "top_lib",
actual = select(
{
":top_earlgrey": "//hw/top_earlgrey/sw/autogen:top_earlgrey",
":is_earlgrey": "//hw/top_earlgrey/sw/autogen:top_earlgrey",
":is_darjeeling": "//hw/top_darjeeling/sw/autogen:top_darjeeling",
},
no_match_error = "no top selected",
no_match_error = "unknown top",
),
)

[
opentitan_ip_c_header(
name = "{}_c_regs".format(ip),
ip = ip,
top = ":top",
top = ":top_desc",
)
for ip in IPS
]
Expand All @@ -58,22 +73,22 @@ alias(
opentitan_ip_rust_header(
name = "{}_rust_regs".format(ip),
ip = ip,
top = ":top",
top = ":top_desc",
)
for ip in IPS
]

opentitan_top_dt_api(
name = "dt_api",
top = ":top",
top = ":top_desc",
deps = [":top_lib"],
)

[
opentitan_ip_dt_header(
name = "dt_{}".format(ip),
ip = ip,
top = ":top",
top = ":top_desc",
deps = [":dt_api"],
)
for ip in IPS
Expand All @@ -87,6 +102,6 @@ cc_library(

opentitan_top_devicetables(
name = "devicetables",
top = ":top",
top = ":top_desc",
deps = [":dt_headers"],
)
18 changes: 18 additions & 0 deletions hw/top/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,21 @@ OPENTITAN_ALL_IPS = [
"uart",
"usbdev",
]

def opentitan_require_top(top_name):
"""
Use this macro in the `target_compatible_with` attribute of
a rule to express the requirement that this target should only
be considered for a particular top.
Example:
cc_library(
name = "bla",
...
target_compatible_with = opentitan_require_top("earlgrey"),
)
"""
return select({
"//hw/top:is_{}".format(top_name): [],
"//conditions:default": ["@platforms//:incompatible"],
})
2 changes: 1 addition & 1 deletion sw/device/lib/dif/autogen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package(default_visibility = ["//visibility:public"])
opentitan_autogen_dif(
name = ip,
ip = ip,
top = "//hw/top",
top = "//hw/top:top_desc",
)
for ip in OPENTITAN_ALL_IPS
]

0 comments on commit e017fb5

Please sign in to comment.