Skip to content

Commit

Permalink
Restrict update of vendor to "unknown" of for Linux targets
Browse files Browse the repository at this point in the history
This rule is kind of strange, but it's needed for most of the targets
running on Linux.

Closes #70
  • Loading branch information
philss committed Jun 21, 2024
1 parent 271e9a1 commit 4bb2416
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 31 deletions.
3 changes: 2 additions & 1 deletion lib/rustler_precompiled.ex
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,8 @@ defmodule RustlerPrecompiled do

# Only replace vendor if remains the same but some other env changed the config.
if original_sys_arch != updated_system_arch and
original_sys_arch.vendor == updated_system_arch.vendor do
original_sys_arch.vendor == updated_system_arch.vendor and
updated_system_arch.os == "linux" do
Map.put(updated_system_arch, :vendor, "unknown")
else
updated_system_arch
Expand Down
122 changes: 92 additions & 30 deletions test/rustler_precompiled_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -253,43 +253,105 @@ defmodule RustlerPrecompiledTest do
assert RustlerPrecompiled.find_compatible_nif_version("2.13", ["2.14"]) == :error
end

test "maybe_override_with_env_vars/2" do
target_system = %{
arch: "x86_64",
vendor: "apple",
os: "darwin20.3.0"
}
describe "maybe_override_with_env_vars/2" do
test "apple host without target env vars" do
host_system = %{
arch: "x86_64",
vendor: "apple",
os: "darwin20.3.0"
}

assert RustlerPrecompiled.maybe_override_with_env_vars(host_system, fn _ -> nil end) ==
host_system
end

assert RustlerPrecompiled.maybe_override_with_env_vars(target_system, fn _ -> nil end) ==
target_system
test "apple host and linux target" do
host_system = %{
arch: "x86_64",
vendor: "apple",
os: "darwin20.3.0"
}

env_with_targets = fn
"TARGET_OS" -> "linux"
"TARGET_ARCH" -> "aarch64"
"TARGET_ABI" -> "gnu"
_ -> nil
env_with_targets = fn
"TARGET_OS" -> "linux"
"TARGET_ARCH" -> "aarch64"
"TARGET_ABI" -> "gnu"
_ -> nil
end

assert RustlerPrecompiled.maybe_override_with_env_vars(host_system, env_with_targets) == %{
arch: "aarch64",
vendor: "unknown",
os: "linux",
abi: "gnu"
}
end

assert RustlerPrecompiled.maybe_override_with_env_vars(target_system, env_with_targets) == %{
arch: "aarch64",
vendor: "unknown",
os: "linux",
abi: "gnu"
}
test "apple host and freebsd target with vendor" do
host_system = %{
arch: "x86_64",
vendor: "apple",
os: "darwin20.3.0"
}

env_with_targets = fn
"TARGET_OS" -> "freebsd"
"TARGET_ARCH" -> "arm"
"TARGET_ABI" -> "musl"
"TARGET_VENDOR" -> "ecorp"
env_with_targets = fn
"TARGET_OS" -> "freebsd"
"TARGET_ARCH" -> "arm"
"TARGET_ABI" -> "musl"
"TARGET_VENDOR" -> "ecorp"
end

assert RustlerPrecompiled.maybe_override_with_env_vars(host_system, env_with_targets) == %{
arch: "arm",
vendor: "ecorp",
os: "freebsd",
abi: "musl"
}
end

assert RustlerPrecompiled.maybe_override_with_env_vars(target_system, env_with_targets) == %{
arch: "arm",
vendor: "ecorp",
os: "freebsd",
abi: "musl"
}
test "apple host and linux target without vendor" do
host_system = %{
arch: "x86_64",
vendor: "apple",
os: "darwin20.3.0"
}

env_with_targets = fn
"TARGET_OS" -> "linux"
"TARGET_ARCH" -> "arm"
"TARGET_ABI" -> "musl"
_ -> nil
end

assert RustlerPrecompiled.maybe_override_with_env_vars(host_system, env_with_targets) == %{
arch: "arm",
vendor: "unknown",
os: "linux",
abi: "musl"
}
end

test "windows host and windows target with the same vendor" do
host_system = %{
arch: "x86_64",
vendor: "pc",
os: "windows"
}

env_with_targets = fn
"TARGET_OS" -> "windows"
"TARGET_ARCH" -> "x86_64"
"TARGET_ABI" -> "msvc"
"TARGET_VENDOR" -> "pc"
end

assert RustlerPrecompiled.maybe_override_with_env_vars(host_system, env_with_targets) == %{
arch: "x86_64",
vendor: "pc",
os: "windows",
abi: "msvc"
}
end
end

@tag :tmp_dir
Expand Down

0 comments on commit 4bb2416

Please sign in to comment.