Build cli deps as .ez archives

This provides an elixir/erlang agnostic way of providing them other
erlang rules
This commit is contained in:
Rin Kuryloski 2023-05-04 08:31:54 +02:00
parent 45520436e8
commit 19f4abd55b
4 changed files with 68 additions and 26 deletions

View File

@ -1,7 +1,4 @@
load(
"@rules_erlang//:util.bzl",
"path_join",
)
load("@bazel_skylib//lib:shell.bzl", "shell")
load(
"@rules_erlang//private:util.bzl",
"additional_file_dest_relative_path",
@ -20,24 +17,20 @@ def _impl(ctx):
out = ctx.actions.declare_file(ctx.attr.out.name)
mix_invocation_dir = ctx.actions.declare_directory("{}_mix".format(ctx.label.name))
package_dir = path_join(
ctx.label.workspace_root,
ctx.label.package,
)
copy_srcs_commands = []
for src in ctx.files.srcs:
dest = additional_file_dest_relative_path(ctx.label, src)
copy_srcs_commands.extend([
'mkdir -p "$(dirname ${{MIX_INVOCATION_DIR}}/{dest})"'.format(
dest = dest,
),
'cp {flags}"{src}" "${{MIX_INVOCATION_DIR}}/{dest}"'.format(
flags = "-r " if src.is_directory else "",
src = src.path,
dest = dest,
),
])
for src in ctx.attr.srcs:
for src_file in src[DefaultInfo].files.to_list():
dest = additional_file_dest_relative_path(src.label, src_file)
copy_srcs_commands.extend([
'mkdir -p "$(dirname ${{MIX_INVOCATION_DIR}}/{dest})"'.format(
dest = dest,
),
'cp {flags}"{src}" "${{MIX_INVOCATION_DIR}}/{dest}"'.format(
flags = "-r " if src_file.is_directory else "",
src = src_file.path,
dest = dest,
),
])
script = """set -euo pipefail
@ -60,11 +53,23 @@ MIX_INVOCATION_DIR="{mix_invocation_dir}"
{copy_srcs_commands}
ORIGINAL_DIR=$PWD
cd "${{MIX_INVOCATION_DIR}}"
export HOME="${{PWD}}"
export MIX_ENV=prod
export ERL_COMPILER_OPTIONS=deterministic
"${{ABS_ELIXIR_HOME}}"/bin/mix archive.build -o "${{ABS_OUT_PATH}}"
for archive in {archives}; do
"${{ABS_ELIXIR_HOME}}"/bin/mix archive.install --force $ORIGINAL_DIR/$archive
done
if [[ -n "{ez_deps}" ]]; then
mkdir -p _build/${{MIX_ENV}}/lib
for ez_dep in {ez_deps}; do
unzip -q $ORIGINAL_DIR/$ez_dep -d _build/${{MIX_ENV}}/lib
done
fi
"${{ABS_ELIXIR_HOME}}"/bin/mix archive.build \\
--no-deps-check \\
-o "${{ABS_OUT_PATH}}"
# remove symlinks from the _build directory since it
# is an unused output, and bazel does not allow them
@ -75,7 +80,8 @@ find . -type l -delete
elixir_home = elixir_home,
mix_invocation_dir = mix_invocation_dir.path,
copy_srcs_commands = "\n".join(copy_srcs_commands),
package_dir = package_dir,
archives = " ".join([shell.quote(a.path) for a in ctx.files.archives]),
ez_deps = " ".join([shell.quote(a.path) for a in ctx.files.ez_deps]),
out = out.path,
)
@ -84,6 +90,8 @@ find . -type l -delete
transitive = [
erlang_runfiles.files,
elixir_runfiles.files,
depset(ctx.files.archives),
depset(ctx.files.ez_deps),
],
)
@ -110,6 +118,12 @@ mix_archive_build = rule(
mandatory = True,
allow_files = True,
),
"archives": attr.label_list(
allow_files = [".ez"],
),
"ez_deps": attr.label_list(
allow_files = [".ez"],
),
"out": attr.output(),
},
toolchains = [

View File

@ -5,6 +5,34 @@ load(":rabbitmqctl_test.bzl", "rabbitmqctl_test")
load("//:rabbitmq_home.bzl", "rabbitmq_home")
load("//:rabbitmq_run.bzl", "rabbitmq_run")
load("//:rabbitmq.bzl", "RABBITMQ_DIALYZER_OPTS", "STARTS_BACKGROUND_BROKER_TAG", "without")
load(
"@rabbitmq-server//bazel/elixir:mix_archive_build.bzl",
"mix_archive_build",
)
mix_archive_build(
name = "parallel_stream_ez",
srcs = ["@parallel_stream//:sources"],
archives = ["@hex//:archive"],
out = "parallel_stream.ez",
)
mix_archive_build(
name = "csv_ez",
srcs = ["@csv//:sources"],
archives = ["@hex//:archive"],
ez_deps = [
":parallel_stream_ez",
],
out = "csv.ez",
)
mix_archive_build(
name = "json_ez",
srcs = ["@json//:sources"],
archives = ["@hex//:archive"],
out = "json.ez",
)
# Note: All the various rabbitmq-* scripts are just copies of rabbitmqctl
rabbitmqctl(

View File

@ -149,7 +149,7 @@ find . -type l -delete
escript_path = escript.path,
ebin_dir = ebin.path,
consolidated_dir = consolidated.path,
archives = "".join([shell.quote(a.path) for a in ctx.files.archives]),
archives = " ".join([shell.quote(a.path) for a in ctx.files.archives]),
precompiled_deps = " ".join([
dep[ErlangAppInfo].app_name
for dep in ctx.attr.deps

View File

@ -139,7 +139,7 @@ set -x
elixir_home = elixir_home,
package_dir = package_dir,
deps_dir = deps_dir,
archives = "".join([shell.quote(a.short_path) for a in ctx.files.archives]),
archives = " ".join([shell.quote(a.short_path) for a in ctx.files.archives]),
precompiled_deps = precompiled_deps,
rabbitmq_run_cmd = ctx.attr.rabbitmq_run[DefaultInfo].files_to_run.executable.short_path,
)
@ -196,7 +196,7 @@ exit /b 1
elixir_home = windows_path(elixir_home),
package_dir = windows_path(ctx.label.package),
deps_dir = deps_dir,
archives = "".join([shell.quote(a.short_path) for a in ctx.files.archives]),
archives = " ".join([shell.quote(a.short_path) for a in ctx.files.archives]),
precompiled_deps = precompiled_deps,
rabbitmq_run_cmd = ctx.attr.rabbitmq_run[DefaultInfo].files_to_run.executable.short_path,
)