From 19f4abd55b0d25b61feb75ff4447b75490fe0700 Mon Sep 17 00:00:00 2001 From: Rin Kuryloski Date: Thu, 4 May 2023 08:31:54 +0200 Subject: Build cli deps as .ez archives This provides an elixir/erlang agnostic way of providing them other erlang rules --- bazel/elixir/mix_archive_build.bzl | 60 +++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 23 deletions(-) (limited to 'bazel') diff --git a/bazel/elixir/mix_archive_build.bzl b/bazel/elixir/mix_archive_build.bzl index 9652e0f1de..adc3b15de6 100644 --- a/bazel/elixir/mix_archive_build.bzl +++ b/bazel/elixir/mix_archive_build.bzl @@ -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 = [ -- cgit v1.2.1