Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 15 additions & 31 deletions private/elixir_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ load(
"@rules_erlang//private:erlang_build.bzl",
"OtpInfo",
)
load(
"@rules_erlang//tools:erlang_toolchain.bzl",
"erlang_home",
"otp_rootdir_setup",
"otp_runfiles",
)

ElixirInfo = provider(
doc = "A Home directory of a built Elixir",
Expand All @@ -20,28 +26,6 @@ ElixirInfo = provider(
],
)

def otp_install_snippet(otp_info):
"""Generate shell snippet to extract OTP release tar, like maybe_install_erlang."""
if otp_info.release_dir_tar == None:
return ""
return """\
mkdir -p $(dirname "{install_path}")
if mkdir "{install_path}"; then
tar --extract --no-same-owner \\
--directory "{install_path}" \\
--file {release_tar}
fi""".format(
release_tar = otp_info.release_dir_tar.path,
install_path = otp_info.install_path,
)

def otp_runfiles(ctx, otp_info):
"""Build runfiles depset for OTP, like erlang_dirs."""
if otp_info.release_dir_tar != None:
return ctx.runfiles([otp_info.release_dir_tar, otp_info.version_file])
else:
return ctx.runfiles([otp_info.version_file])

def _elixir_build_impl(ctx):
otp_info = ctx.attr.otp[OtpInfo]
release_dir = ctx.actions.declare_directory("elixir_release")
Expand All @@ -57,7 +41,7 @@ def _elixir_build_impl(ctx):
outputs = [release_dir],
command = """set -euo pipefail

{maybe_install_erlang}
{erl_rootdir_setup}

export PATH="{erlang_home}"/bin:${{PATH}}

Expand Down Expand Up @@ -85,8 +69,8 @@ make
cp -r bin $ABS_RELEASE_DIR/
cp -r lib $ABS_RELEASE_DIR/
""".format(
maybe_install_erlang = otp_install_snippet(otp_info),
erlang_home = otp_info.erlang_home,
erl_rootdir_setup = otp_rootdir_setup(otp_info),
erlang_home = erlang_home(otp_info),
release_path = release_dir.path,
source_files = " ".join([f.path for f in ctx.files.srcs]),
first_source_file = ctx.files.srcs[0].path if ctx.files.srcs else "",
Expand All @@ -104,14 +88,14 @@ cp -r lib $ABS_RELEASE_DIR/
outputs = [version_file],
command = """set -euo pipefail

{maybe_install_erlang}
{erl_rootdir_setup}

export PATH="{erlang_home}"/bin:${{PATH}}

"{elixir_home}"/bin/iex --version > {version_file}
""".format(
maybe_install_erlang = otp_install_snippet(otp_info),
erlang_home = otp_info.erlang_home,
erl_rootdir_setup = otp_rootdir_setup(otp_info),
erlang_home = erlang_home(otp_info),
elixir_home = release_dir.path,
version_file = version_file.path,
),
Expand Down Expand Up @@ -157,14 +141,14 @@ def _elixir_external_impl(ctx):
outputs = [version_file],
command = """set -euo pipefail

{maybe_install_erlang}
{erl_rootdir_setup}

export PATH="{erlang_home}"/bin:${{PATH}}

"{elixir_home}"/bin/iex --version > {version_file}
""".format(
maybe_install_erlang = otp_install_snippet(otp_info),
erlang_home = otp_info.erlang_home,
erl_rootdir_setup = otp_rootdir_setup(otp_info),
erlang_home = erlang_home(otp_info),
elixir_home = elixir_home,
version_file = version_file.path,
),
Expand Down
6 changes: 3 additions & 3 deletions private/elixir_bytecode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load(
"//private:elixir_toolchain.bzl",
"elixir_dirs",
"erlang_dirs",
"maybe_install_erlang",
"erl_rootdir_setup",
)

def unique_dirnames(files):
Expand Down Expand Up @@ -57,7 +57,7 @@ def _impl(ctx):

script = """set -euo pipefail

{maybe_install_erlang}
{erl_rootdir_setup}
if [[ "{elixir_home}" == /* ]]; then
ABS_ELIXIR_HOME="{elixir_home}"
else
Expand All @@ -82,7 +82,7 @@ ${{ABS_ELIXIR_HOME}}/bin/elixirc \\
{elixirc_opts} \\
{srcs}
""".format(
maybe_install_erlang = maybe_install_erlang(ctx),
erl_rootdir_setup = erl_rootdir_setup(ctx),
erlang_home = erlang_home,
elixir_home = elixir_home,
erl_libs_path = erl_libs_path,
Expand Down
16 changes: 6 additions & 10 deletions private/elixir_release.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ load(
"//private:elixir_toolchain.bzl",
"elixir_dirs",
"erlang_dirs",
"maybe_install_erlang",
"erl_rootdir_setup",
)

def _elixir_release_impl(ctx):
Expand Down Expand Up @@ -204,7 +204,7 @@ cp "{manifest_file}" "{release_dir}/{release_name}.manifest"
script_content = """#!/bin/bash
set -euo pipefail

{maybe_install_erlang}
{erl_rootdir_setup}

# Set up paths
if [[ "{elixir_home}" == /* ]]; then
Expand All @@ -213,13 +213,9 @@ else
ABS_ELIXIR_HOME=$PWD/{elixir_home}
fi

if [[ "{erlang_home}" == /* ]]; then
ABS_ERLANG_HOME="{erlang_home}"
else
ABS_ERLANG_HOME=$PWD/{erlang_home}
fi

export PATH="$ABS_ELIXIR_HOME/bin:$ABS_ERLANG_HOME/bin:$PATH"
# erlang_home is either an absolute host path (external erlang) or the literal
# "$ERL_ROOTDIR" (relocatable OTP), already exported above by erl_rootdir_setup.
export PATH="$ABS_ELIXIR_HOME/bin:{erlang_home}/bin:$PATH"

# Create output directory
mkdir -p "{release_dir}"
Expand Down Expand Up @@ -250,7 +246,7 @@ ARGS+=("--version" "{release_version}")
chmod -w "{release_dir}/{release_name}.script"
chmod -w "{release_dir}/{release_name}.boot"
""".format(
maybe_install_erlang = maybe_install_erlang(ctx),
erl_rootdir_setup = erl_rootdir_setup(ctx),
elixir_home = elixir_home,
erlang_home = erlang_home,
processor = processor.path,
Expand Down
6 changes: 3 additions & 3 deletions private/elixir_release_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ load(
"//private:elixir_toolchain.bzl",
"elixir_dirs",
"erlang_dirs",
"maybe_install_erlang",
"erl_rootdir_setup",
)

def _impl(ctx):
Expand Down Expand Up @@ -299,7 +299,7 @@ chmod +x "$BUNDLE_DIR/bin/$RELEASE_NAME"
# Build the complete bundle creation script
script = """set -euo pipefail

{maybe_install_erlang}
{erl_rootdir_setup}

BUNDLE_DIR="{bundle_dir}"
MANIFEST_FILE="{manifest_file}"
Expand Down Expand Up @@ -353,7 +353,7 @@ echo " $BUNDLE_DIR/bin/$RELEASE_NAME start # Start as daemon"
echo " $BUNDLE_DIR/bin/$RELEASE_NAME console # Start with console"
echo " $BUNDLE_DIR/bin/$RELEASE_NAME foreground # Start in foreground"
""".format(
maybe_install_erlang = maybe_install_erlang(ctx),
erl_rootdir_setup = erl_rootdir_setup(ctx),
bundle_dir = bundle_dir.path,
manifest_file = erlang_release_info.manifest_file.path,
release_name = release_name,
Expand Down
20 changes: 12 additions & 8 deletions private/elixir_source_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ load(
"@rules_erlang//private:erlang_build.bzl",
"OtpInfo",
)
load(
"@rules_erlang//tools:erlang_toolchain.bzl",
"erlang_home",
"otp_rootdir_setup",
"otp_runfiles",
)
load(
":elixir_build.bzl",
"ElixirInfo",
"otp_install_snippet",
"otp_runfiles",
)

def _elixir_source_build_impl(ctx):
Expand All @@ -24,7 +28,7 @@ def _elixir_source_build_impl(ctx):
outputs = [release_dir],
command = """set -euo pipefail

{maybe_install_erlang}
{erl_rootdir_setup}

export PATH="{erlang_home}"/bin:${{PATH}}

Expand Down Expand Up @@ -52,8 +56,8 @@ make
cp -r bin $ABS_RELEASE_DIR/
cp -r lib $ABS_RELEASE_DIR/
""".format(
maybe_install_erlang = otp_install_snippet(otp_info),
erlang_home = otp_info.erlang_home,
erl_rootdir_setup = otp_rootdir_setup(otp_info),
erlang_home = erlang_home(otp_info),
release_path = release_dir.path,
source_files = " ".join([f.path for f in ctx.files.srcs]),
first_source_file = ctx.files.srcs[0].path if ctx.files.srcs else "",
Expand All @@ -71,14 +75,14 @@ cp -r lib $ABS_RELEASE_DIR/
outputs = [version_file],
command = """set -euo pipefail

{maybe_install_erlang}
{erl_rootdir_setup}

export PATH="{erlang_home}"/bin:${{PATH}}

"{elixir_home}"/bin/iex --version > {version_file}
""".format(
maybe_install_erlang = otp_install_snippet(otp_info),
erlang_home = otp_info.erlang_home,
erl_rootdir_setup = otp_rootdir_setup(otp_info),
erlang_home = erlang_home(otp_info),
elixir_home = release_dir.path,
version_file = version_file.path,
),
Expand Down
56 changes: 15 additions & 41 deletions private/elixir_sys_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ extend this to also inject specs for these into `sys.config`.

load("@rules_erlang//:erlang_app_info.bzl", "ErlangAppInfo")
load("//:elixir_app_info.bzl", "ElixirAppInfo")
load(":release_info.bzl", "ReleaseInfo", "get_release_info")
load(
":config_inference.bzl",
"infer_app_name",
Expand All @@ -34,6 +33,8 @@ load(
"parse_config_provider_options",
"should_use_runtime_config",
)
load(":elixir_toolchain.bzl", "erlang_escript_wrapper")
load(":release_info.bzl", "ReleaseInfo", "get_release_info")

def _elixir_sys_config_impl(ctx):
"""Generate a sys.config file with intelligent defaults and inference."""
Expand Down Expand Up @@ -99,45 +100,21 @@ def _elixir_sys_config_impl(ctx):
# Get the sys_config_builder tool
builder = ctx.executable._sys_config_builder

# Get the toolchain for Erlang paths
toolchain = ctx.toolchains["//:toolchain_type"]
erlang_toolchain = toolchain.otpinfo

# Create a wrapper script that sets up the PATH for escript
wrapper = ctx.actions.declare_file("{}_sys_config_wrapper.sh".format(ctx.attr.name))
# Runtime-config builds read the runtime path from a file and forward it.
exec_line = None
if has_runtime and runtime_config_path:
wrapper_content = """#!/bin/bash
set -euo pipefail

# Set up Erlang/OTP paths
export PATH="{erlang_home}/bin:$PATH"

# Read runtime config path from file and pass to tool
RUNTIME_PATH=$(cat "{runtime_config_path_file}")
exec "{tool_path}" "$@" --runtime-path "$RUNTIME_PATH"
""".format(
erlang_home = erlang_toolchain.erlang_home,
runtime_config_path_file = runtime_config_path.path,
tool_path = builder.path,
)
else:
wrapper_content = """#!/bin/bash
set -euo pipefail

# Set up Erlang/OTP paths
export PATH="{erlang_home}/bin:$PATH"

# Run the escript with all arguments
exec "{tool_path}" "$@"
""".format(
erlang_home = erlang_toolchain.erlang_home,
tool_path = builder.path,
exec_line = 'RUNTIME_PATH=$(cat "{}")\nexec "{}" "$@" --runtime-path "$RUNTIME_PATH"'.format(
runtime_config_path.path,
builder.path,
)

ctx.actions.write(
output = wrapper,
content = wrapper_content,
is_executable = True,
# Wrapper puts the toolchain's Erlang/OTP on PATH; erlang_runfiles must be
# staged as action inputs (below) so $ERL_ROOTDIR/bin/erl exists.
(wrapper, erlang_runfiles) = erlang_escript_wrapper(
ctx,
"{}_sys_config_wrapper.sh".format(ctx.attr.name),
builder,
exec_line = exec_line,
)

# Prepare inputs
Expand All @@ -154,7 +131,7 @@ exec "{tool_path}" "$@"
ctx.actions.run(
executable = wrapper,
arguments = [args],
inputs = depset(inputs),
inputs = depset(inputs, transitive = [erlang_runfiles.files]),
outputs = outputs,
mnemonic = "SysConfig",
progress_message = "Generating sys.config for {}".format(ctx.label),
Expand Down Expand Up @@ -194,7 +171,6 @@ elixir_sys_config = rule(
app_configs = [":config_prod"] # From eval_config
""",
),

"runtime_configs": attr.label_list(
allow_files = [".exs"],
default = [],
Expand Down Expand Up @@ -228,7 +204,6 @@ elixir_sys_config = rule(
proper config files with eval_config instead.
""",
),

"config_provider_options": attr.string_dict(
default = {},
doc = """Advanced Config.Provider options.
Expand Down Expand Up @@ -279,7 +254,6 @@ elixir_sys_config = rule(
env = "staging" # Custom environment
""",
),

"version": attr.string(
doc = """Explicitly set the release version.

Expand Down
Loading