Skip to content

Commit 166b2fd

Browse files
HoloRinmergify[bot]
authored andcommitted
Build cli deps as .ez archives
This provides an elixir/erlang agnostic way of providing them other erlang rules (cherry picked from commit 19f4abd)
1 parent d0a9c9f commit 166b2fd

File tree

4 files changed

+68
-26
lines changed

4 files changed

+68
-26
lines changed

bazel/elixir/mix_archive_build.bzl

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
load(
2-
"@rules_erlang//:util.bzl",
3-
"path_join",
4-
)
1+
load("@bazel_skylib//lib:shell.bzl", "shell")
52
load(
63
"@rules_erlang//private:util.bzl",
74
"additional_file_dest_relative_path",
@@ -20,24 +17,20 @@ def _impl(ctx):
2017
out = ctx.actions.declare_file(ctx.attr.out.name)
2118
mix_invocation_dir = ctx.actions.declare_directory("{}_mix".format(ctx.label.name))
2219

23-
package_dir = path_join(
24-
ctx.label.workspace_root,
25-
ctx.label.package,
26-
)
27-
2820
copy_srcs_commands = []
29-
for src in ctx.files.srcs:
30-
dest = additional_file_dest_relative_path(ctx.label, src)
31-
copy_srcs_commands.extend([
32-
'mkdir -p "$(dirname ${{MIX_INVOCATION_DIR}}/{dest})"'.format(
33-
dest = dest,
34-
),
35-
'cp {flags}"{src}" "${{MIX_INVOCATION_DIR}}/{dest}"'.format(
36-
flags = "-r " if src.is_directory else "",
37-
src = src.path,
38-
dest = dest,
39-
),
40-
])
21+
for src in ctx.attr.srcs:
22+
for src_file in src[DefaultInfo].files.to_list():
23+
dest = additional_file_dest_relative_path(src.label, src_file)
24+
copy_srcs_commands.extend([
25+
'mkdir -p "$(dirname ${{MIX_INVOCATION_DIR}}/{dest})"'.format(
26+
dest = dest,
27+
),
28+
'cp {flags}"{src}" "${{MIX_INVOCATION_DIR}}/{dest}"'.format(
29+
flags = "-r " if src_file.is_directory else "",
30+
src = src_file.path,
31+
dest = dest,
32+
),
33+
])
4134

4235
script = """set -euo pipefail
4336
@@ -60,11 +53,23 @@ MIX_INVOCATION_DIR="{mix_invocation_dir}"
6053
6154
{copy_srcs_commands}
6255
56+
ORIGINAL_DIR=$PWD
6357
cd "${{MIX_INVOCATION_DIR}}"
6458
export HOME="${{PWD}}"
6559
export MIX_ENV=prod
6660
export ERL_COMPILER_OPTIONS=deterministic
67-
"${{ABS_ELIXIR_HOME}}"/bin/mix archive.build -o "${{ABS_OUT_PATH}}"
61+
for archive in {archives}; do
62+
"${{ABS_ELIXIR_HOME}}"/bin/mix archive.install --force $ORIGINAL_DIR/$archive
63+
done
64+
if [[ -n "{ez_deps}" ]]; then
65+
mkdir -p _build/${{MIX_ENV}}/lib
66+
for ez_dep in {ez_deps}; do
67+
unzip -q $ORIGINAL_DIR/$ez_dep -d _build/${{MIX_ENV}}/lib
68+
done
69+
fi
70+
"${{ABS_ELIXIR_HOME}}"/bin/mix archive.build \\
71+
--no-deps-check \\
72+
-o "${{ABS_OUT_PATH}}"
6873
6974
# remove symlinks from the _build directory since it
7075
# is an unused output, and bazel does not allow them
@@ -75,7 +80,8 @@ find . -type l -delete
7580
elixir_home = elixir_home,
7681
mix_invocation_dir = mix_invocation_dir.path,
7782
copy_srcs_commands = "\n".join(copy_srcs_commands),
78-
package_dir = package_dir,
83+
archives = " ".join([shell.quote(a.path) for a in ctx.files.archives]),
84+
ez_deps = " ".join([shell.quote(a.path) for a in ctx.files.ez_deps]),
7985
out = out.path,
8086
)
8187

@@ -84,6 +90,8 @@ find . -type l -delete
8490
transitive = [
8591
erlang_runfiles.files,
8692
elixir_runfiles.files,
93+
depset(ctx.files.archives),
94+
depset(ctx.files.ez_deps),
8795
],
8896
)
8997

@@ -110,6 +118,12 @@ mix_archive_build = rule(
110118
mandatory = True,
111119
allow_files = True,
112120
),
121+
"archives": attr.label_list(
122+
allow_files = [".ez"],
123+
),
124+
"ez_deps": attr.label_list(
125+
allow_files = [".ez"],
126+
),
113127
"out": attr.output(),
114128
},
115129
toolchains = [

deps/rabbitmq_cli/BUILD.bazel

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ load(":rabbitmqctl_test.bzl", "rabbitmqctl_test")
55
load("//:rabbitmq_home.bzl", "rabbitmq_home")
66
load("//:rabbitmq_run.bzl", "rabbitmq_run")
77
load("//:rabbitmq.bzl", "RABBITMQ_DIALYZER_OPTS", "STARTS_BACKGROUND_BROKER_TAG", "without")
8+
load(
9+
"@rabbitmq-server//bazel/elixir:mix_archive_build.bzl",
10+
"mix_archive_build",
11+
)
12+
13+
mix_archive_build(
14+
name = "parallel_stream_ez",
15+
srcs = ["@parallel_stream//:sources"],
16+
archives = ["@hex//:archive"],
17+
out = "parallel_stream.ez",
18+
)
19+
20+
mix_archive_build(
21+
name = "csv_ez",
22+
srcs = ["@csv//:sources"],
23+
archives = ["@hex//:archive"],
24+
ez_deps = [
25+
":parallel_stream_ez",
26+
],
27+
out = "csv.ez",
28+
)
29+
30+
mix_archive_build(
31+
name = "json_ez",
32+
srcs = ["@json//:sources"],
33+
archives = ["@hex//:archive"],
34+
out = "json.ez",
35+
)
836

937
# Note: All the various rabbitmq-* scripts are just copies of rabbitmqctl
1038
rabbitmqctl(

deps/rabbitmq_cli/rabbitmqctl.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ find . -type l -delete
149149
escript_path = escript.path,
150150
ebin_dir = ebin.path,
151151
consolidated_dir = consolidated.path,
152-
archives = "".join([shell.quote(a.path) for a in ctx.files.archives]),
152+
archives = " ".join([shell.quote(a.path) for a in ctx.files.archives]),
153153
precompiled_deps = " ".join([
154154
dep[ErlangAppInfo].app_name
155155
for dep in ctx.attr.deps

deps/rabbitmq_cli/rabbitmqctl_test.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ set -x
139139
elixir_home = elixir_home,
140140
package_dir = package_dir,
141141
deps_dir = deps_dir,
142-
archives = "".join([shell.quote(a.short_path) for a in ctx.files.archives]),
142+
archives = " ".join([shell.quote(a.short_path) for a in ctx.files.archives]),
143143
precompiled_deps = precompiled_deps,
144144
rabbitmq_run_cmd = ctx.attr.rabbitmq_run[DefaultInfo].files_to_run.executable.short_path,
145145
)
@@ -196,7 +196,7 @@ exit /b 1
196196
elixir_home = windows_path(elixir_home),
197197
package_dir = windows_path(ctx.label.package),
198198
deps_dir = deps_dir,
199-
archives = "".join([shell.quote(a.short_path) for a in ctx.files.archives]),
199+
archives = " ".join([shell.quote(a.short_path) for a in ctx.files.archives]),
200200
precompiled_deps = precompiled_deps,
201201
rabbitmq_run_cmd = ctx.attr.rabbitmq_run[DefaultInfo].files_to_run.executable.short_path,
202202
)

0 commit comments

Comments
 (0)