Skip to content

Commit be400ae

Browse files
committed
chore(link): return executable File from link_action
And move environment handling to callers.
1 parent e73e07a commit be400ae

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

d/private/rules/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bzl_library(
1515
deps = [
1616
"//d/private/rules:compile",
1717
"//d/private/rules:link",
18+
"@bazel_lib//lib:expand_make_vars",
1819
],
1920
)
2021

@@ -68,7 +69,6 @@ bzl_library(
6869
],
6970
deps = [
7071
"//d/private/rules:cc_toolchain",
71-
"@bazel_lib//lib:expand_make_vars",
7272
"@rules_cc//cc:defs_bzl",
7373
],
7474
)
@@ -83,6 +83,7 @@ bzl_library(
8383
deps = [
8484
"//d/private/rules:compile",
8585
"//d/private/rules:link",
86+
"@bazel_lib//lib:expand_make_vars",
8687
"@rules_cc//cc:find_cc_toolchain_bzl",
8788
],
8889
)

d/private/rules/binary.bzl

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

3+
load("@bazel_lib//lib:expand_make_vars.bzl", "expand_variables")
34
load("@rules_cc//cc:find_cc_toolchain.bzl", "use_cc_toolchain")
45
load("//d/private/rules:compile.bzl", "TARGET_TYPE", "compilation_action", "runnable_attrs")
56
load("//d/private/rules:link.bzl", "link_action")
67

78
def _d_binary_impl(ctx):
89
"""Implementation of d_binary rule."""
910
d_info = compilation_action(ctx, target_type = TARGET_TYPE.BINARY)
10-
return link_action(ctx, d_info)
11+
output = link_action(ctx, d_info)
12+
env_with_expansions = {
13+
k: expand_variables(ctx, ctx.expand_location(v, ctx.files.data), [output], "env")
14+
for k, v in ctx.attr.env.items()
15+
}
16+
return [
17+
DefaultInfo(
18+
executable = output,
19+
runfiles = ctx.runfiles(files = ctx.files.data),
20+
),
21+
RunEnvironmentInfo(environment = env_with_expansions),
22+
]
1123

1224
d_binary = rule(
1325
implementation = _d_binary_impl,

d/private/rules/link.bzl

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Linking action for D rules.
33
44
"""
55

6-
load("@bazel_lib//lib:expand_make_vars.bzl", "expand_variables")
76
load("@rules_cc//cc:defs.bzl", "cc_common")
87
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
98
load("//d/private/rules:cc_toolchain.bzl", "find_cc_toolchain_for_linking")
@@ -15,9 +14,7 @@ def link_action(ctx, d_info):
1514
ctx: The rule context.
1615
d_info: The DInfo provider containing the linking context.
1716
Returns:
18-
List of providers:
19-
- DefaultInfo: The linked binary.
20-
- RunEnvironmentInfo: The environment variables for the linked binary.
17+
A File for the linked binary.
2118
"""
2219
toolchain = ctx.toolchains["//d:toolchain_type"].d_toolchain_info
2320
cc_linker_info = find_cc_toolchain_for_linking(ctx)
@@ -28,24 +25,12 @@ def link_action(ctx, d_info):
2825
compilation_outputs = cc_common.create_compilation_outputs(
2926
objects = depset(direct = [d_info.compilation_output]),
3027
)
31-
res = cc_common.link(
28+
return cc_common.link(
3229
name = ctx.label.name,
3330
actions = ctx.actions,
3431
feature_configuration = cc_linker_info.feature_configuration,
3532
cc_toolchain = cc_linker_info.cc_toolchain,
3633
compilation_outputs = compilation_outputs,
3734
linking_contexts = linking_contexts,
3835
user_link_flags = toolchain.linker_flags + ctx.attr.linkopts + d_info.linker_flags.to_list(),
39-
)
40-
output = res.executable
41-
env_with_expansions = {
42-
k: expand_variables(ctx, ctx.expand_location(v, ctx.files.data), [output], "env")
43-
for k, v in ctx.attr.env.items()
44-
}
45-
return [
46-
DefaultInfo(
47-
executable = output,
48-
runfiles = ctx.runfiles(files = ctx.files.data),
49-
),
50-
RunEnvironmentInfo(environment = env_with_expansions),
51-
]
36+
).executable

d/private/rules/test.bzl

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

3+
load("@bazel_lib//lib:expand_make_vars.bzl", "expand_variables")
34
load("@rules_cc//cc:find_cc_toolchain.bzl", "use_cc_toolchain")
45
load("//d/private/rules:compile.bzl", "TARGET_TYPE", "compilation_action", "runnable_attrs")
56
load("//d/private/rules:link.bzl", "link_action")
67

78
def _d_test_impl(ctx):
89
"""Implementation of d_test rule."""
910
d_info = compilation_action(ctx, target_type = TARGET_TYPE.TEST)
10-
return link_action(ctx, d_info)
11+
output = link_action(ctx, d_info)
12+
env_with_expansions = {
13+
k: expand_variables(ctx, ctx.expand_location(v, ctx.files.data), [output], "env")
14+
for k, v in ctx.attr.env.items()
15+
}
16+
return [
17+
DefaultInfo(
18+
executable = output,
19+
runfiles = ctx.runfiles(files = ctx.files.data),
20+
),
21+
RunEnvironmentInfo(environment = env_with_expansions),
22+
]
1123

1224
d_test = rule(
1325
implementation = _d_test_impl,

0 commit comments

Comments
 (0)