Skip to content

Commit

Permalink
ensure that csv and json elixir deps are embedded in the cli escript
Browse files Browse the repository at this point in the history
also set 'cfg =' appropriately
  • Loading branch information
HoloRin committed Jun 18, 2024
1 parent 5fc8a8c commit 46250dc
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 23 deletions.
14 changes: 6 additions & 8 deletions bazel/elixir/elixir_bytecode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ set -x
erl_libs_path = erl_libs_path,
env = env,
setup = ctx.attr.setup,
elixirc = ctx.executable._compiler.path,
elixirc = ctx.executable._elixirc.path,
out_dir = ebin.path,
elixirc_opts = " ".join([shell.quote(opt) for opt in ctx.attr.elixirc_opts]),
srcs = " ".join([f.path for f in ctx.files.srcs]),
)

compiler_runfiles = ctx.attr._compiler[DefaultInfo].default_runfiles
compiler_runfiles = ctx.attr._elixirc[DefaultInfo].default_runfiles

inputs = depset(
direct = ctx.files.srcs + erl_libs_files,
Expand All @@ -70,7 +70,7 @@ set -x
outputs = [ebin],
command = script,
mnemonic = "ELIXIRC",
tools = [ctx.executable._compiler],
tools = [ctx.executable._elixirc],
)

return [
Expand All @@ -97,12 +97,10 @@ elixir_bytecode = rule(
mandatory = True,
),
"setup": attr.string(),
"_compiler": attr.label(
default = Label("@rules_elixir//tools:elixirc"),
allow_single_file = True,
"_elixirc": attr.label(
default = Label("@rules_elixir//tools:elixirc_wrapper"),
executable = True,
cfg = "exec",
cfg = "target",
),
},
# toolchains = ["@rules_elixir//:toolchain_type"],
)
67 changes: 67 additions & 0 deletions bazel/elixir/mix_archive_extract.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
load(
"@rules_erlang//:erlang_app_info.bzl",
"ErlangAppInfo",
"flat_deps",
)
load(
"@rules_erlang//:util.bzl",
"path_join",
)

def _impl(ctx):
ebin = ctx.actions.declare_directory(path_join(ctx.attr.app_name, "ebin"))

script = """set -euo pipefail
DEST="$(mktemp -d)"
unzip -q -d "$DEST" {archive}
cp "$DEST"/{app_name}/ebin/* {ebin}
""".format(
archive = ctx.file.archive.path,
app_name = ctx.attr.app_name,
ebin = ebin.path,
)

ctx.actions.run_shell(
inputs = ctx.files.archive,
outputs = [ebin],
command = script,
mnemonic = "MixArchiveExtract",
)

deps = flat_deps(ctx.attr.deps)

runfiles = ctx.runfiles([ebin])
for dep in ctx.attr.deps:
runfiles = runfiles.merge(dep[DefaultInfo].default_runfiles)

return [
DefaultInfo(
files = depset([ebin]),
runfiles = runfiles,
),
ErlangAppInfo(
app_name = ctx.attr.app_name,
extra_apps = ctx.attr.extra_apps,
include = [],
beam = [ebin],
priv = [],
license_files = [],
srcs = ctx.files.srcs,
deps = deps,
)
]

mix_archive_extract = rule(
implementation = _impl,
attrs = {
"app_name": attr.string(mandatory = True),
"extra_apps": attr.string_list(),
"deps": attr.label_list(providers = [ErlangAppInfo]),
"archive": attr.label(
allow_single_file = [".ez"],
),
"srcs": attr.label_list(),
},
provides = [ErlangAppInfo],
)
47 changes: 32 additions & 15 deletions deps/rabbitmq_cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ load(
"//bazel/elixir:mix_archive_build.bzl",
"mix_archive_build",
)
load(
"//bazel/elixir:mix_archive_extract.bzl",
"mix_archive_extract",
)

mix_archive_build(
name = "csv_ez",
Expand All @@ -40,13 +44,34 @@ mix_archive_build(
archives = ["@hex//:archive"],
)

mix_archive_extract(
name = "csv",
srcs = ["@csv//:sources"],
app_name = "csv",
archive = ":csv_ez",
deps = [
"@rules_elixir//elixir",
],
)

mix_archive_build(
name = "json_ez",
srcs = ["@json//:sources"],
out = "json.ez",
archives = ["@hex//:archive"],
)

mix_archive_extract(
name = "json",
srcs = ["@json//:sources"],
app_name = "json",
archive = ":json_ez",
deps = [
"@rules_elixir//elixir",
"@rules_elixir//elixir:logger",
],
)

mix_archive_build(
name = "amqp_ez",
testonly = True,
Expand Down Expand Up @@ -93,6 +118,8 @@ SRCS = glob([
])

DEPS = [
":csv",
":json",
"//deps/rabbit_common:erlang_app",
"@observer_cli//:erlang_app",
"@stdout_formatter//:erlang_app",
Expand All @@ -114,10 +141,6 @@ elixir_bytecode(
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US.UTF-8",
},
ez_deps = [
":csv.ez",
":json.ez",
],
setup = """\
mkdir -p _build/$MIX_ENV/lib/csv
cp -RL $ERL_LIBS/csv/ebin _build/$MIX_ENV/lib/csv
Expand All @@ -133,6 +156,7 @@ app_file(
app_name = APP_NAME,
app_version = APP_VERSION,
modules = [":beam_files"],
# mix escripts do not include dependencies in the applications key
deps = [
"@rules_elixir//elixir",
"@rules_elixir//elixir:logger",
Expand Down Expand Up @@ -225,6 +249,7 @@ app_file(
"Elixir." + name
for name in _TEST_MODULES
],
# mix escripts do not include dependencies in the applications key
deps = [
"@rules_elixir//elixir",
"@rules_elixir//elixir:logger",
Expand Down Expand Up @@ -285,13 +310,11 @@ plt(
"public_key",
"runtime_tools",
],
ez_deps = [
":csv_ez",
":json_ez",
],
ignore_warnings = True,
libs = ["@rules_elixir//elixir:elixir"],
deps = [
":csv",
":json",
"//deps/rabbit:erlang_app",
"//deps/rabbit_common:erlang_app",
"@rules_elixir//elixir",
Expand Down Expand Up @@ -328,8 +351,6 @@ ex_unit_test(
},
ez_deps = [
":amqp.ez",
":csv.ez",
":json.ez",
":temp.ez",
":x509.ez",
],
Expand Down Expand Up @@ -388,16 +409,12 @@ elixir_bytecode(
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US.UTF-8",
},
ez_deps = [
":csv.ez",
":json.ez",
],
setup = """\
mkdir -p _build/$MIX_ENV/lib/csv
cp -RL $ERL_LIBS/csv/ebin _build/$MIX_ENV/lib/csv
""",
tags = ["manual"],
deps = DEPS + [
"//deps/rabbit:erlang_app",
],
tags = ["manual"],
)

0 comments on commit 46250dc

Please sign in to comment.