Skip to content

Commit 52f2574

Browse files
committed
Improve messages, make the tests pass
1 parent ca64bc0 commit 52f2574

File tree

6 files changed

+39
-40
lines changed

6 files changed

+39
-40
lines changed

rustler_mix/lib/rustler.ex

+22-8
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,44 @@ defmodule Rustler do
6767
quote bind_quoted: [opts: opts] do
6868
config = Rustler.Compiler.compile_crate(__MODULE__, opts)
6969

70+
@load_from {config.otp_app, config.load_path}
71+
7072
if config.lib do
71-
@load_from {config.otp_app, config.load_path}
7273
@load_data config.load_data
73-
74+
@before_compile {Rustler, :__before_compile_nif__}
75+
else
7476
@before_compile Rustler
7577
end
7678
end
7779
end
7880

7981
defmacro __before_compile__(_env) do
82+
quote do
83+
def rustler_path do
84+
{otp_app, path} = @load_from
85+
Path.join(:code.priv_dir(otp_app), path)
86+
end
87+
end
88+
end
89+
90+
defmacro __before_compile_nif__(_env) do
8091
quote do
8192
@on_load :rustler_init
8293

94+
def rustler_path do
95+
# TODO: Parametrise, and keep all crates in the list
96+
{otp_app, path} = @load_from
97+
Path.join(:code.priv_dir(otp_app), path)
98+
end
99+
83100
@doc false
84101
def rustler_init do
85102
# Remove any old modules that may be loaded so we don't get
86103
# :error, {:upgrade, 'Upgrade not supported by this NIF library.'}}
87104
:code.purge(__MODULE__)
88-
89-
{otp_app, path} = @load_from
90-
91-
load_path = Path.join(:code.priv_dir(otp_app), path) |> to_charlist()
92-
93-
:erlang.load_nif(load_path, @load_data)
105+
load_path = String.to_charlist(rustler_path())
106+
data = @load_data
107+
:erlang.load_nif(load_path, data)
94108
end
95109
end
96110
end

rustler_mix/lib/rustler/compiler.ex

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ defmodule Rustler.Compiler do
1414
artifacts = Server.build()
1515

1616
is_release = Mix.env() in [:prod, :bench]
17+
entry = artifacts[crate]
1718

18-
entry =
19-
artifacts
20-
|> Map.values()
21-
|> Enum.find(&(crate == &1[:name]))
22-
19+
# Only a single file result per crate is supported right now
2320
[filename] = entry[:filenames]
24-
out_path = Path.join(priv_dir(entry, is_release), Path.basename(filename))
25-
shell.info(" Copying file #{filename} to #{out_path}")
21+
rel_filename = Path.basename(filename)
2622

23+
out_path = Path.join(priv_dir(entry, is_release), Path.basename(filename))
2724
dest = Path.join(:code.priv_dir(config.otp_app), out_path)
25+
rel_dest = Path.relative_to_cwd(dest)
26+
27+
shell.info(" Copying #{rel_filename} to #{rel_dest}")
2828
File.mkdir_p!(Path.dirname(dest))
2929
File.copy!(filename, dest)
3030

rustler_mix/lib/rustler/compiler/server.ex

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ defmodule Rustler.Compiler.Server do
2020

2121
@impl true
2222
def handle_call(:compile, _from, nil) do
23-
shell = Mix.shell()
24-
_otp_app = Mix.Project.config() |> Keyword.get(:app)
2523
is_release = Mix.env() in [:prod, :bench]
2624

2725
cargo_opts = %{
@@ -30,6 +28,13 @@ defmodule Rustler.Compiler.Server do
3028

3129
cargo = :cargo.init(File.cwd!(), cargo_opts)
3230
artifacts = :cargo.build_and_capture(cargo)
31+
32+
# This drops the unique key in favour of the crate name
33+
artifacts =
34+
artifacts
35+
|> Map.values()
36+
|> Map.new(&{&1[:name], &1})
37+
3338
{:reply, artifacts, artifacts}
3439
end
3540

rustler_tests/lib/binary_example.ex

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
defmodule BinaryExample do
22
use Rustler,
33
otp_app: :rustler_test,
4-
crate: :binary_example,
5-
lib: false
4+
crate: :binary_example
65
end

rustler_tests/native/rustler_test/Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ crate-type = ["cdylib"]
1313
name = "hello_rust"
1414
path = "src/main.rs"
1515

16-
[[bin]]
17-
name = "hello_rust2"
18-
path = "src/main.rs"
19-
2016
[dependencies]
2117
lazy_static = "1.4"
2218
rustler = { path = "../../../rustler" }

rustler_tests/test/binary_example_test.exs

+2-17
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,7 @@ defmodule BinaryExampleTest do
22
use ExUnit.Case
33

44
test "binary is compiled" do
5-
bins = ~w(binary_example hello_rust hello_rust2)
6-
7-
for bin <- bins do
8-
assert_exists(bin)
9-
end
10-
end
11-
12-
defp assert_exists(name) do
13-
assert_exists(name, :os.type())
14-
end
15-
16-
defp assert_exists(name, {:win32, _} = type) do
17-
assert File.exists?("priv/crates/#{name}.exe")
18-
end
19-
20-
defp assert_exists(name, type) do
21-
assert File.exists?("priv/native/#{name}")
5+
path = BinaryExample.rustler_path()
6+
File.exists?(path)
227
end
238
end

0 commit comments

Comments
 (0)