Skip to content

Commit 354757a

Browse files
authored
Merge pull request tweag#6 from Skydio/danny/split-build-action
Split build action
2 parents d2d6143 + 9af6fc7 commit 354757a

File tree

5 files changed

+182
-153
lines changed

5 files changed

+182
-153
lines changed

private/actions.bzl

+70-66
Original file line numberDiff line numberDiff line change
@@ -32,62 +32,62 @@ def nix_wrapper(ctx, derivation, deps, nixpkgs, out):
3232
),
3333
)
3434

35-
def _nix_debug_build(ctx, out_symlink):
35+
def _nix_debug_build(ctx, out_tree):
3636
toolchain = ctx.toolchains["@io_tweag_rules_nixpkgs//:toolchain_type"]
3737

3838
# <name>.path_info generates a file with the referenced nix store paths
3939
ctx.actions.run_shell(
40-
inputs = [out_symlink],
40+
inputs = [out_tree],
4141
outputs = [ctx.outputs.path_info],
4242
command = "{} path-info -r {}/result > {}".format(
4343
toolchain.nixinfo.nix_bin_path,
44-
out_symlink.path,
44+
out_tree.path,
4545
ctx.outputs.path_info.path,
4646
),
4747
)
4848

4949
# <name>.log generates a file with the nix build log for this target
5050
ctx.actions.run_shell(
51-
inputs = [out_symlink],
51+
inputs = [out_tree],
5252
outputs = [ctx.outputs.log],
5353
command = "{} log {}/result > {}".format(
5454
toolchain.nixinfo.nix_bin_path,
55-
out_symlink.path,
55+
out_tree.path,
5656
ctx.outputs.log.path,
5757
),
5858
)
5959

6060
# <name>.derivation shows the derivation for this target
6161
ctx.actions.run_shell(
62-
inputs = [out_symlink],
62+
inputs = [out_tree],
6363
outputs = [ctx.outputs.derivation],
6464
command = "{} show-derivation {}/result > {}".format(
6565
toolchain.nixinfo.nix_bin_path,
66-
out_symlink.path,
66+
out_tree.path,
6767
ctx.outputs.derivation.path,
6868
),
6969
)
7070

7171
ctx.actions.run_shell(
72-
inputs = [out_symlink],
72+
inputs = [out_tree],
7373
outputs = [ctx.outputs.lib_list],
7474
command = "find {}/result/lib > {}".format(
75-
out_symlink.path,
75+
out_tree.path,
7676
ctx.outputs.lib_list.path,
7777
),
7878
)
7979

80-
def _nix_docker_helper(ctx, out_symlink):
80+
def _nix_docker_helper(ctx, out_tree):
8181
toolchain = ctx.toolchains["@io_tweag_rules_nixpkgs//:toolchain_type"]
8282

8383
# Create a tarball with runtime dependencies of built package.
8484
ctx.actions.run_shell(
85-
inputs = [out_symlink],
85+
inputs = [out_tree],
8686
outputs = [ctx.outputs.tar],
8787
command = " ".join([
8888
toolchain.nixinfo.nix_store_bin_path,
8989
"-q -R --include-outputs",
90-
"{}/result".format(out_symlink.path),
90+
"{}/result".format(out_tree.path),
9191
"|",
9292
"xargs tar c",
9393
">",
@@ -101,60 +101,14 @@ def nix_build(
101101
srcs,
102102
deps,
103103
repo, # nix repo
104-
out_symlink,
105-
out_include_dir,
106-
out_include_dir_name,
107-
out_lib_dir_name,
108-
out_shared_libs,
109-
out_static_libs):
104+
out_tree):
110105
""" runs nix-build on a set of sources """
111106
toolchain = ctx.toolchains["@io_tweag_rules_nixpkgs//:toolchain_type"]
112107

113-
_nix_debug_build(ctx, out_symlink) # add optional debug outputs
114-
_nix_docker_helper(ctx, out_symlink)
108+
_nix_debug_build(ctx, out_tree) # add optional debug outputs
109+
_nix_docker_helper(ctx, out_tree)
115110

116-
if out_include_dir:
117-
ctx.actions.run_shell(
118-
inputs = [out_symlink],
119-
outputs = [out_include_dir],
120-
command = "cp -R {}/{}/* {}".format(
121-
out_symlink.path,
122-
out_include_dir_name,
123-
out_include_dir.path,
124-
),
125-
)
126-
127-
if out_shared_libs:
128-
ctx.actions.run_shell(
129-
inputs = [out_symlink],
130-
outputs = out_shared_libs.values(),
131-
command = "\n".join([
132-
"cp -R {}/{}/{} {}".format(
133-
out_symlink.path,
134-
out_lib_dir_name,
135-
lib_name,
136-
out_shared_libs[lib_name].path,
137-
)
138-
for lib_name in out_shared_libs
139-
]),
140-
)
141-
142-
if out_static_libs:
143-
ctx.actions.run_shell(
144-
inputs = [out_symlink],
145-
outputs = out_static_libs.values(),
146-
command = "\n".join([
147-
"cp -R {}/{}/{} {}".format(
148-
out_symlink.path,
149-
out_lib_dir_name,
150-
lib_name,
151-
out_static_libs[lib_name].path,
152-
)
153-
for lib_name in out_static_libs
154-
]),
155-
)
156-
157-
input_nix_out_symlinks = []
111+
input_nix_out_trees = []
158112
nix_file_deps = []
159113
nixpath_entries = []
160114
for dep in deps:
@@ -163,7 +117,7 @@ def nix_build(
163117
nixpath_entries += ["-I", "{}={}".format(info.name, info.nix_import.path)]
164118
nix_file_deps.append(info.nix_import)
165119
nix_file_deps += info.extra_nix_files
166-
input_nix_out_symlinks.append(info.out_symlink)
120+
input_nix_out_trees.append(info.out_tree)
167121

168122
maybe_build_attr = []
169123
if ctx.attr.build_attribute:
@@ -173,8 +127,8 @@ def nix_build(
173127
]
174128

175129
ctx.actions.run(
176-
outputs = [out_symlink],
177-
inputs = srcs + input_nix_out_symlinks + nix_file_deps + [repo[NixPkgsInfo].nix_import],
130+
outputs = [out_tree],
131+
inputs = srcs + input_nix_out_trees + nix_file_deps + [repo[NixPkgsInfo].nix_import],
178132
executable = toolchain.nixinfo.nix_build_bin_path,
179133
env = {
180134
# nix tries to update a database entry for it's cache state
@@ -191,7 +145,7 @@ def nix_build(
191145
"--keep-failed", # TODO(danny): when should we enable this?
192146
"--show-trace",
193147
"--out-link",
194-
"{}/result".format(out_symlink.path),
148+
"{}/result".format(out_tree.path),
195149
],
196150
mnemonic = "NixBuild",
197151
execution_requirements = {
@@ -207,6 +161,56 @@ def nix_build(
207161
# },
208162
)
209163

164+
def nix_collect_cc(
165+
ctx,
166+
out_tree, # input
167+
out_include_dir,
168+
out_include_dir_name,
169+
out_lib_dir_name,
170+
out_shared_libs,
171+
out_static_libs):
172+
"""nix_collect_cc takes the output symlink and collects cc artifacts"""
173+
if out_include_dir:
174+
ctx.actions.run_shell(
175+
inputs = [out_tree],
176+
outputs = [out_include_dir],
177+
command = "cp -R {}/{}/* {}".format(
178+
out_tree.path,
179+
out_include_dir_name,
180+
out_include_dir.path,
181+
),
182+
)
183+
184+
if out_shared_libs:
185+
ctx.actions.run_shell(
186+
inputs = [out_tree],
187+
outputs = out_shared_libs.values(),
188+
command = "\n".join([
189+
"cp -R {}/{}/{} {}".format(
190+
out_tree.path,
191+
out_lib_dir_name,
192+
lib_name,
193+
out_shared_libs[lib_name].path,
194+
)
195+
for lib_name in out_shared_libs
196+
]),
197+
)
198+
199+
if out_static_libs:
200+
ctx.actions.run_shell(
201+
inputs = [out_tree],
202+
outputs = out_static_libs.values(),
203+
command = "\n".join([
204+
"cp -R {}/{}/{} {}".format(
205+
out_tree.path,
206+
out_lib_dir_name,
207+
lib_name,
208+
out_static_libs[lib_name].path,
209+
)
210+
for lib_name in out_static_libs
211+
]),
212+
)
213+
210214
GENERATE_NIX_MANIFEST = """
211215
set -euo pipefail
212216
store_paths=()

private/aspects.bzl

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ load(":providers.bzl", "NixDepsInfo", "NixDerivationInfo")
22

33
def _nix_deps_aspect_impl(target, ctx):
44
store_paths = []
5-
out_symlinks = []
5+
out_trees = []
66

77
# Collect direct store paths and bazel-support symlinks in data + deps attrs.
88
if NixDerivationInfo in target:
99
store_paths.append(target[NixDerivationInfo].store_path)
1010

1111
# The bazel-support symlink is only passed by nix_cc rules.
12-
out_symlink = target[NixDerivationInfo].out_symlink
13-
if out_symlink != None:
14-
out_symlinks.append(out_symlink)
12+
out_tree = target[NixDerivationInfo].out_tree
13+
if out_tree != None:
14+
out_trees.append(out_tree)
1515

1616
# Forward all store paths and bazel-support symlinks in deps
1717
direct_deps = []
1818

1919
indirect_store_paths = []
20-
indirect_out_symlinks = []
20+
indirect_out_trees = []
2121
if hasattr(ctx.rule.attr, "data"):
2222
direct_deps += [dep for dep in ctx.rule.attr.data]
2323
if hasattr(ctx.rule.attr, "deps"):
@@ -26,14 +26,14 @@ def _nix_deps_aspect_impl(target, ctx):
2626
if NixDepsInfo not in dep:
2727
continue
2828
indirect_store_paths.append(dep[NixDepsInfo].store_paths)
29-
indirect_out_symlinks.append(dep[NixDepsInfo].out_symlinks)
29+
indirect_out_trees.append(dep[NixDepsInfo].out_trees)
3030

3131
return [NixDepsInfo(store_paths = depset(
3232
direct = store_paths,
3333
transitive = indirect_store_paths,
34-
), out_symlinks = depset(
35-
direct = out_symlinks,
36-
transitive = indirect_out_symlinks,
34+
), out_trees = depset(
35+
direct = out_trees,
36+
transitive = indirect_out_trees,
3737
))]
3838

3939
nix_deps_aspect = aspect(

private/providers.bzl

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ NixBuildInfo = provider(
1717
"name": "Name of nix build, same as rule name attribute",
1818
"nix_import": "File import for this .nix file",
1919
"extra_nix_files": "other .nix Files needed by build",
20-
"out_symlink": "File of symlink output created by nix-build",
20+
"out_tree": "File of symlink output created by nix-build",
2121
},
2222
)
2323

@@ -33,7 +33,7 @@ NixDerivationInfo = provider(
3333
doc = "Produced by nix rules to signal to nix_deps_aspect that a " +
3434
"nix store path is depended on by this rule.",
3535
fields = {
36-
"out_symlink": "File of bazel support TreeArtifact, or None",
36+
"out_tree": "File of bazel support TreeArtifact, or None",
3737
"store_path": "String, derivation's store path or symlink thereto",
3838
},
3939
)
@@ -43,7 +43,7 @@ NixDepsInfo = provider(
4343
"Accumulated from NixDerivationInfo." +
4444
"Can be used to build docker containers with appropriate nix dependencies.",
4545
fields = {
46-
"out_symlinks": "Depset of Files, bazel support TreeArtifacts needed to reference store_paths",
46+
"out_trees": "Depset of Files, bazel support TreeArtifacts needed to reference store_paths",
4747
"store_paths": "Depset of store paths needed at runtime",
4848
},
4949
)

0 commit comments

Comments
 (0)