Skip to content

Commit 07836c2

Browse files
committed
feat: add 'env' and 'data' attributes for 'd_binary' and 'd_test'
* feat: add 'env' (location and make variable expansion) and 'data' attributes * fix: typo in 'resolved_toolchain' definition * chore: improve curl downloader * chore: refactor compiler versions generator
1 parent 8f77e39 commit 07836c2

File tree

16 files changed

+174
-95
lines changed

16 files changed

+174
-95
lines changed

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ module(
66
compatibility_level = 1,
77
)
88

9+
bazel_dep(name = "aspect_bazel_lib", version = "2.9.1")
910
bazel_dep(name = "bazel_features", version = "1.32.0")
1011
bazel_dep(name = "bazel_skylib", version = "1.7.1")
1112
bazel_dep(name = "package_metadata", version = "0.0.5")
1213
bazel_dep(name = "platforms", version = "0.0.10")
1314
bazel_dep(name = "rules_cc", version = "0.0.10")
1415

15-
bazel_dep(name = "aspect_bazel_lib", version = "2.9.1", dev_dependency = True)
1616
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.7.1", dev_dependency = True)
1717
bazel_dep(name = "buildifier_prebuilt", version = "8.0.3", dev_dependency = True)
1818
bazel_dep(name = "gazelle", version = "0.42.0", dev_dependency = True, repo_name = "bazel_gazelle")

d/private/resolved_toolchain.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def _resolved_toolchain_impl(ctx):
1313
return [
1414
toolchain_info,
1515
toolchain_info.default,
16-
toolchain_info.dinfo,
16+
toolchain_info.d_toolchain_info,
1717
toolchain_info.template_variables,
1818
]
1919

d/private/rules/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ bzl_library(
2424
],
2525
deps = [
2626
"//d/private:providers",
27+
"@aspect_bazel_lib//lib:expand_make_vars",
28+
"@bazel_skylib//lib:dicts",
2729
"@bazel_skylib//lib:paths",
2830
"@rules_cc//cc/common",
2931
],

d/private/rules/binary.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""D test rule for compiling binaries."""
22

3-
load("//d/private/rules:common.bzl", "TARGET_TYPE", "common_attrs", "compilation_action")
3+
load("//d/private/rules:common.bzl", "TARGET_TYPE", "compilation_action", "runnable_attrs")
44

55
def _d_binary_impl(ctx):
66
"""Implementation of d_binary rule."""
77
return compilation_action(ctx, target_type = TARGET_TYPE.BINARY)
88

99
d_binary = rule(
1010
implementation = _d_binary_impl,
11-
attrs = common_attrs,
11+
attrs = runnable_attrs,
1212
toolchains = ["//d:toolchain_type"],
1313
executable = True,
1414
)

d/private/rules/common.bzl

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Common definitions for D rules."""
22

3+
load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_variables")
4+
load("@bazel_skylib//lib:dicts.bzl", "dicts")
35
load("@bazel_skylib//lib:paths.bzl", "paths")
46
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
57
load("//d/private:providers.bzl", "DInfo")
@@ -30,6 +32,21 @@ common_attrs = {
3032
"_windows_constraint": attr.label(default = "@platforms//os:windows", doc = "Windows platform constraint"),
3133
}
3234

35+
runnable_attrs = dicts.add(
36+
common_attrs,
37+
{
38+
"env": attr.string_dict(doc = "Environment variables for the binary at runtime. Subject of location and make variable expansion."),
39+
"data": attr.label_list(allow_files = True, doc = "List of files to be made available at runtime."),
40+
},
41+
)
42+
43+
library_attrs = dicts.add(
44+
common_attrs,
45+
{
46+
"source_only": attr.bool(doc = "If true, the source files are compiled, but not library is produced."),
47+
},
48+
)
49+
3350
def _get_os(ctx):
3451
if ctx.target_platform_has_constraint(ctx.attr._linux_constraint[platform_common.ConstraintValueInfo]):
3552
return "linux"
@@ -38,7 +55,7 @@ def _get_os(ctx):
3855
elif ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]):
3956
return "windows"
4057
else:
41-
fail("Unsupported OS: %s" % ctx.label)
58+
fail("OS not supported")
4259

4360
def _binary_name(ctx, name):
4461
"""Returns the name of the binary based on the OS.
@@ -104,13 +121,13 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
104121
d_deps = [d[DInfo] for d in ctx.attr.deps if DInfo in d]
105122
compiler_flags = depset(ctx.attr.dopts, transitive = [d.compiler_flags for d in d_deps])
106123
imports = depset(
107-
[paths.join(ctx.label.package, imp) for imp in ctx.attr.imports],
124+
[paths.join(ctx.label.workspace_root, ctx.label.package, imp) for imp in ctx.attr.imports],
108125
transitive = [d.imports for d in d_deps],
109126
)
110127
linker_flags = depset(ctx.attr.linkopts, transitive = [d.linker_flags for d in d_deps])
111128
string_imports = depset(
112-
([ctx.label_package] if ctx.files.string_srcs else []) +
113-
[paths.join(ctx.label.package, imp) for imp in ctx.attr.string_imports],
129+
([paths.join(ctx.label.workspace_root, ctx.label.package)] if ctx.files.string_srcs else []) +
130+
[paths.join(ctx.label.workspace_root, ctx.label.package, imp) for imp in ctx.attr.string_imports],
114131
transitive = [d.string_imports for d in d_deps],
115132
)
116133
versions = depset(ctx.attr.versions, transitive = [d.versions for d in d_deps])
@@ -162,8 +179,8 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
162179
DInfo(
163180
compiler_flags = compiler_flags,
164181
imports = depset(
165-
[ctx.label.package] +
166-
[paths.join(ctx.label.package, imp) for imp in ctx.attr.imports],
182+
[paths.join(ctx.label.workspace_root, ctx.label.package)] +
183+
[paths.join(ctx.label.workspace_root, ctx.label.package, imp) for imp in ctx.attr.imports],
167184
transitive = [d.imports for d in d_deps],
168185
),
169186
interface_srcs = depset(
@@ -178,12 +195,22 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
178195
),
179196
linker_flags = linker_flags,
180197
string_imports = depset(
181-
([ctx.label.package] if ctx.files.string_srcs else []) +
182-
[paths.join(ctx.label.package, imp) for imp in ctx.attr.string_imports],
198+
([paths.join(ctx.label.workspace_root, ctx.label.package)] if ctx.files.string_srcs else []) +
199+
[paths.join(ctx.label.workspace_root, ctx.label.package, imp) for imp in ctx.attr.string_imports],
183200
transitive = [d.string_imports for d in d_deps],
184201
),
185202
versions = versions,
186203
),
187204
]
188205
else:
189-
return [DefaultInfo(executable = output)]
206+
env_with_expansions = {
207+
k: expand_variables(ctx, ctx.expand_location(v, ctx.files.data), [output], "env")
208+
for k, v in ctx.attr.env.items()
209+
}
210+
return [
211+
DefaultInfo(
212+
executable = output,
213+
runfiles = ctx.runfiles(files = ctx.files.data),
214+
),
215+
RunEnvironmentInfo(environment = env_with_expansions),
216+
]

d/private/rules/library.bzl

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
"""Rule for compiling D libraries."""
22

33
load("//d/private:providers.bzl", "DInfo")
4-
load("//d/private/rules:common.bzl", "TARGET_TYPE", "common_attrs", "compilation_action")
4+
load("//d/private/rules:common.bzl", "TARGET_TYPE", "compilation_action", "library_attrs")
55

66
def _d_library_impl(ctx):
77
"""Implementation of d_library rule."""
88
return compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY)
99

1010
d_library = rule(
1111
implementation = _d_library_impl,
12-
attrs = dict(
13-
common_attrs.items() +
14-
{
15-
"source_only": attr.bool(
16-
doc = "If true, the source files are compiled, but not library is produced.",
17-
),
18-
}.items(),
19-
),
12+
attrs = library_attrs,
2013
toolchains = ["//d:toolchain_type"],
2114
provides = [DInfo],
2215
)

d/private/rules/test.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""D test rule for compiling and running D unit tests."""
22

3-
load("//d/private/rules:common.bzl", "TARGET_TYPE", "common_attrs", "compilation_action")
3+
load("//d/private/rules:common.bzl", "TARGET_TYPE", "compilation_action", "runnable_attrs")
44

55
def _d_test_impl(ctx):
66
"""Implementation of d_test rule."""
77
return compilation_action(ctx, target_type = TARGET_TYPE.TEST)
88

99
d_test = rule(
1010
implementation = _d_test_impl,
11-
attrs = common_attrs,
11+
attrs = runnable_attrs,
1212
toolchains = ["//d:toolchain_type"],
1313
test = True,
1414
)

docs/rules.md

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/smoke/BUILD.bazel

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
"""Provides a simple way to test your rules as an external workspace."""
22

33
load("@bazel_skylib//rules:build_test.bzl", "build_test")
4-
load("@rules_d//d:defs.bzl", "d_binary", "d_test")
5-
6-
d_binary(
7-
name = "fiber_thread_usage",
8-
srcs = ["fiber_thread_usage.d"],
9-
)
10-
11-
d_test(
12-
name = "fiber_thread_usage_test",
13-
srcs = ["fiber_thread_usage.d"],
14-
)
154

165
build_test(
176
name = "smoke_test",
187
targets = [
19-
":fiber_thread_usage",
8+
"//tests/std_lib:fiber_thread_usage",
209
],
2110
)

e2e/smoke/WORKSPACE.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ local_repository(
66

77
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
88

9+
http_archive(
10+
name = "aspect_bazel_lib",
11+
sha256 = "f93d386d8d0b0149031175e81df42a488be4267c3ca2249ba5321c23c60bc1f0",
12+
strip_prefix = "bazel-lib-2.9.1",
13+
url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v2.9.1/bazel-lib-v2.9.1.tar.gz",
14+
)
15+
16+
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "aspect_bazel_lib_register_toolchains")
17+
18+
aspect_bazel_lib_dependencies()
19+
20+
aspect_bazel_lib_register_toolchains()
21+
922
http_archive(
1023
name = "bazel_features",
1124
sha256 = "07bd2b18764cdee1e0d6ff42c9c0a6111ffcbd0c17f0de38e7f44f1519d1c0cd",

0 commit comments

Comments
 (0)