Skip to content

Commit b00b210

Browse files
committed
Update cargo and fix multiple artifacts per package
1 parent d83fdc1 commit b00b210

File tree

4 files changed

+57
-33
lines changed

4 files changed

+57
-33
lines changed

rustler_mix/lib/rustler.ex

+19-17
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,27 @@ defmodule Rustler do
8888
end
8989

9090
defmacro __before_compile_nif__(_env) do
91-
quote do
92-
@on_load :rustler_init
93-
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)
91+
quoted =
92+
quote do
93+
def rustler_path do
94+
# TODO: Parametrise, and keep all crates in the list
95+
{otp_app, path} = @load_from
96+
Path.join(:code.priv_dir(otp_app), path)
97+
end
98+
99+
@on_load :rustler_init
100+
@doc false
101+
def rustler_init do
102+
# Remove any old modules that may be loaded so we don't get
103+
# :error, {:upgrade, 'Upgrade not supported by this NIF library.'}}
104+
:code.purge(__MODULE__)
105+
load_path = String.to_charlist(rustler_path())
106+
:ok = :erlang.load_nif(load_path, @load_data)
107+
end
98108
end
99109

100-
@doc false
101-
def rustler_init do
102-
# Remove any old modules that may be loaded so we don't get
103-
# :error, {:upgrade, 'Upgrade not supported by this NIF library.'}}
104-
:code.purge(__MODULE__)
105-
load_path = String.to_charlist(rustler_path())
106-
data = @load_data
107-
:erlang.load_nif(load_path, data)
108-
end
109-
end
110+
# quoted |> Macro.to_string |> IO.puts
111+
quoted
110112
end
111113

112114
@doc false

rustler_mix/lib/rustler/compiler.ex

+33-12
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,39 @@ defmodule Rustler.Compiler do
1717
is_release = Mix.env() in [:prod, :bench]
1818
entry = artifacts[crate]
1919

20-
# Only a single file result per crate is supported right now
21-
[filename] = entry[:filenames]
22-
rel_filename = Path.basename(filename)
20+
is_lib = :cargo_artifact.kind(entry) == :cdylib
21+
is_bin = :cargo_artifact.kind(entry) == :bin
2322

24-
out_path = Path.join(priv_dir(entry, is_release), Path.basename(filename))
25-
dest = Path.join(:code.priv_dir(config.otp_app), out_path)
26-
rel_dest = Path.relative_to_cwd(dest)
23+
if !is_lib and !is_bin do
24+
Mix.raise("Crate #{crate} is neither a 'bin' nor a 'cdylib' but #{entry[:kind]}")
25+
end
2726

28-
shell.info(" Copying #{rel_filename} to #{rel_dest}")
29-
File.mkdir_p!(Path.dirname(dest))
30-
File.copy!(filename, dest)
27+
priv_dir = priv_dir(entry, is_release)
28+
dest_root = :code.priv_dir(config.otp_app)
3129

32-
is_lib = entry[:kind] == ["cdylib"]
30+
out_paths =
31+
for filename <- :cargo_artifact.filenames(entry) do
32+
out_path = Path.join(priv_dir, Path.basename(filename))
33+
dest = Path.join(dest_root, out_path)
34+
rel_filename = Path.basename(filename)
35+
rel_dest = Path.relative_to_cwd(dest)
3336

34-
%Config{config | lib: is_lib, load_path: Path.rootname(out_path)}
37+
shell.info(" Copying #{rel_filename} to #{rel_dest}")
38+
File.mkdir_p!(Path.dirname(dest))
39+
File.copy!(filename, dest)
40+
41+
out_path
42+
end
43+
44+
[load_path] =
45+
if is_lib do
46+
out_paths |> Enum.filter(&:cargo_util.is_dylib/1)
47+
else
48+
exec = :cargo_artifact.executable(entry)
49+
[Path.join(priv_dir, Path.basename(exec))]
50+
end
51+
52+
%Config{config | lib: is_lib, load_path: Path.rootname(load_path)}
3553
end
3654

3755
defp priv_dir(entry, is_release) do
@@ -42,7 +60,10 @@ defmodule Rustler.Compiler do
4260
"debug"
4361
end
4462

45-
"crates/#{entry[:name]}/#{entry[:version]}/#{type}"
63+
name = :cargo_artifact.name(entry)
64+
version = :cargo_artifact.version(entry)
65+
66+
"crates/#{name}/#{version}/#{type}"
4667
end
4768

4869
defp ensure_string(atom) when is_atom(atom) do

rustler_mix/lib/rustler/compiler/server.ex

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ defmodule Rustler.Compiler.Server do
2626
release: is_release
2727
}
2828

29+
Mix.shell().info("Starting build in #{File.cwd!()}")
30+
2931
cargo = :cargo.init(File.cwd!(), cargo_opts)
30-
artifacts = :cargo.build_and_capture(cargo)
32+
artifacts = :cargo.build(cargo)
3133

3234
# This drops the unique key in favour of the crate name
3335
artifacts =
3436
artifacts
35-
|> Map.values()
36-
|> Map.new(&{&1[:name], &1})
37+
|> Map.new(&{:cargo_artifact.name(&1), &1})
3738

3839
{:reply, artifacts, artifacts}
3940
end

rustler_mix/mix.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%{
2-
"cargo": {:git, "https://github.com/filmor/erlang-cargo.git", "1456f74ae8b064ad2c9e853953a30b6392a249ab", []},
2+
"cargo": {:git, "https://github.com/filmor/erlang-cargo.git", "b4159576565554e689eb1ac25ae2e634252d514b", []},
33
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"},
44
"ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "0db1ee8d1547ab4877c5b5dffc6604ef9454e189928d5ba8967d4a58a801f161"},
55
"jsx": {:hex, :jsx, "2.10.0", "77760560d6ac2b8c51fd4c980e9e19b784016aa70be354ce746472c33beb0b1c", [:rebar3], [], "hexpm", "9a83e3704807298016968db506f9fad0f027de37546eb838b3ae1064c3a0ad62"},

0 commit comments

Comments
 (0)