1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
load(
"@rules_erlang//:erlang_app_info.bzl",
"ErlangAppInfo",
"flat_deps",
)
load(
"@rules_erlang//:util.bzl",
"path_join",
)
load(
"@rules_erlang//private:util.bzl",
"additional_file_dest_relative_path",
)
load(
"//bazel/elixir:elixir_toolchain.bzl",
"elixir_dirs",
"erlang_dirs",
"maybe_install_erlang",
)
def deps_dir_contents(ctx, deps, dir):
files = []
for dep in deps:
lib_info = dep[ErlangAppInfo]
for src in lib_info.include + lib_info.beam + lib_info.srcs:
rp = additional_file_dest_relative_path(dep.label, src)
f = ctx.actions.declare_file(path_join(
dir,
lib_info.app_name,
rp,
))
ctx.actions.symlink(
output = f,
target_file = src,
)
files.append(f)
return files
def _impl(ctx):
(erlang_home, _, erlang_runfiles) = erlang_dirs(ctx)
(elixir_home, elixir_runfiles) = elixir_dirs(ctx)
escript = ctx.actions.declare_file(path_join("escript", "rabbitmqctl"))
ebin = ctx.actions.declare_directory("ebin")
mix_invocation_dir = ctx.actions.declare_directory("{}_mix".format(ctx.label.name))
fetched_srcs = ctx.actions.declare_file("deps.tar")
deps = flat_deps(ctx.attr.deps)
deps_dir = ctx.label.name + "_deps"
deps_dir_files = deps_dir_contents(ctx, deps, deps_dir)
package_dir = path_join(
ctx.label.workspace_root,
ctx.label.package,
)
script = """set -euo pipefail
{maybe_install_erlang}
if [[ "{elixir_home}" == /* ]]; then
ABS_ELIXIR_HOME="{elixir_home}"
else
ABS_ELIXIR_HOME=$PWD/{elixir_home}
fi
ABS_EBIN_DIR=$PWD/{ebin_dir}
ABS_ESCRIPT_PATH=$PWD/{escript_path}
ABS_FETCHED_SRCS=$PWD/{fetched_srcs}
export PATH="$ABS_ELIXIR_HOME"/bin:"{erlang_home}"/bin:${{PATH}}
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
MIX_INVOCATION_DIR="{mix_invocation_dir}"
cp -R {package_dir}/config ${{MIX_INVOCATION_DIR}}/config
cp -R {package_dir}/lib ${{MIX_INVOCATION_DIR}}/lib
cp {package_dir}/mix.exs ${{MIX_INVOCATION_DIR}}/mix.exs
cd ${{MIX_INVOCATION_DIR}}
export IS_BAZEL=true
export HOME=${{PWD}}
export DEPS_DIR=$(dirname $ABS_EBIN_DIR)/{deps_dir}
export MIX_ENV=prod
export ERL_COMPILER_OPTIONS=deterministic
"${{ABS_ELIXIR_HOME}}"/bin/mix local.hex --force
"${{ABS_ELIXIR_HOME}}"/bin/mix local.rebar --force
"${{ABS_ELIXIR_HOME}}"/bin/mix deps.get
if [ ! -d _build/${{MIX_ENV}}/lib/rabbit_common ]; then
cp -r ${{DEPS_DIR}}/* _build/${{MIX_ENV}}/lib
fi
"${{ABS_ELIXIR_HOME}}"/bin/mix deps.compile
"${{ABS_ELIXIR_HOME}}"/bin/mix compile
"${{ABS_ELIXIR_HOME}}"/bin/mix escript.build
cp escript/rabbitmqctl ${{ABS_ESCRIPT_PATH}}
cp _build/${{MIX_ENV}}/lib/rabbitmqctl/ebin/* ${{ABS_EBIN_DIR}}
cp _build/${{MIX_ENV}}/lib/rabbitmqctl/consolidated/* ${{ABS_EBIN_DIR}}
tar --file ${{ABS_FETCHED_SRCS}} \\
--create deps
# remove symlinks from the _build directory since it
# is not used, and bazel does not allow them
find . -type l -delete
""".format(
maybe_install_erlang = maybe_install_erlang(ctx),
erlang_home = erlang_home,
elixir_home = elixir_home,
mix_invocation_dir = mix_invocation_dir.path,
package_dir = package_dir,
deps_dir = deps_dir,
escript_path = escript.path,
ebin_dir = ebin.path,
fetched_srcs = fetched_srcs.path,
)
inputs = depset(
direct = ctx.files.srcs,
transitive = [
erlang_runfiles.files,
elixir_runfiles.files,
depset(deps_dir_files),
],
)
ctx.actions.run_shell(
inputs = inputs,
outputs = [escript, ebin, mix_invocation_dir, fetched_srcs],
command = script,
mnemonic = "MIX",
)
runfiles = ctx.runfiles([ebin]).merge_all([
erlang_runfiles,
elixir_runfiles,
] + [
dep[DefaultInfo].default_runfiles
for dep in deps
])
return [
DefaultInfo(
executable = escript,
files = depset([ebin, fetched_srcs]),
runfiles = runfiles,
),
ErlangAppInfo(
app_name = "rabbitmq_cli",
include = [],
beam = [ebin],
priv = [],
license_files = ctx.files.license_files,
srcs = ctx.files.srcs,
deps = deps,
),
]
rabbitmqctl_private = rule(
implementation = _impl,
attrs = {
"is_windows": attr.bool(
mandatory = True,
),
"srcs": attr.label_list(
mandatory = True,
allow_files = True,
),
"license_files": attr.label_list(
allow_files = True,
),
"deps": attr.label_list(
providers = [ErlangAppInfo],
),
},
toolchains = [
"//bazel/elixir:toolchain_type",
],
provides = [ErlangAppInfo],
executable = True,
)
def rabbitmqctl(**kwargs):
rabbitmqctl_private(
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs
)
|