From 52f257463ee49e67d7c86d98525712518b8fd2a6 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Sat, 25 Apr 2020 22:51:34 +0200 Subject: [PATCH] Improve messages, make the tests pass --- rustler_mix/lib/rustler.ex | 30 ++++++++++++++------ rustler_mix/lib/rustler/compiler.ex | 14 ++++----- rustler_mix/lib/rustler/compiler/server.ex | 9 ++++-- rustler_tests/lib/binary_example.ex | 3 +- rustler_tests/native/rustler_test/Cargo.toml | 4 --- rustler_tests/test/binary_example_test.exs | 19 ++----------- 6 files changed, 39 insertions(+), 40 deletions(-) diff --git a/rustler_mix/lib/rustler.ex b/rustler_mix/lib/rustler.ex index 1a2c1abba..9f79f6d63 100644 --- a/rustler_mix/lib/rustler.ex +++ b/rustler_mix/lib/rustler.ex @@ -67,30 +67,44 @@ defmodule Rustler do quote bind_quoted: [opts: opts] do config = Rustler.Compiler.compile_crate(__MODULE__, opts) + @load_from {config.otp_app, config.load_path} + if config.lib do - @load_from {config.otp_app, config.load_path} @load_data config.load_data - + @before_compile {Rustler, :__before_compile_nif__} + else @before_compile Rustler end end end defmacro __before_compile__(_env) do + quote do + def rustler_path do + {otp_app, path} = @load_from + Path.join(:code.priv_dir(otp_app), path) + end + end + end + + defmacro __before_compile_nif__(_env) do quote do @on_load :rustler_init + def rustler_path do + # TODO: Parametrise, and keep all crates in the list + {otp_app, path} = @load_from + Path.join(:code.priv_dir(otp_app), path) + end + @doc false def rustler_init do # Remove any old modules that may be loaded so we don't get # :error, {:upgrade, 'Upgrade not supported by this NIF library.'}} :code.purge(__MODULE__) - - {otp_app, path} = @load_from - - load_path = Path.join(:code.priv_dir(otp_app), path) |> to_charlist() - - :erlang.load_nif(load_path, @load_data) + load_path = String.to_charlist(rustler_path()) + data = @load_data + :erlang.load_nif(load_path, data) end end end diff --git a/rustler_mix/lib/rustler/compiler.ex b/rustler_mix/lib/rustler/compiler.ex index 435deb957..006927630 100644 --- a/rustler_mix/lib/rustler/compiler.ex +++ b/rustler_mix/lib/rustler/compiler.ex @@ -14,17 +14,17 @@ defmodule Rustler.Compiler do artifacts = Server.build() is_release = Mix.env() in [:prod, :bench] + entry = artifacts[crate] - entry = - artifacts - |> Map.values() - |> Enum.find(&(crate == &1[:name])) - + # Only a single file result per crate is supported right now [filename] = entry[:filenames] - out_path = Path.join(priv_dir(entry, is_release), Path.basename(filename)) - shell.info(" Copying file #{filename} to #{out_path}") + rel_filename = Path.basename(filename) + out_path = Path.join(priv_dir(entry, is_release), Path.basename(filename)) dest = Path.join(:code.priv_dir(config.otp_app), out_path) + rel_dest = Path.relative_to_cwd(dest) + + shell.info(" Copying #{rel_filename} to #{rel_dest}") File.mkdir_p!(Path.dirname(dest)) File.copy!(filename, dest) diff --git a/rustler_mix/lib/rustler/compiler/server.ex b/rustler_mix/lib/rustler/compiler/server.ex index bbcd06fc6..827d62031 100644 --- a/rustler_mix/lib/rustler/compiler/server.ex +++ b/rustler_mix/lib/rustler/compiler/server.ex @@ -20,8 +20,6 @@ defmodule Rustler.Compiler.Server do @impl true def handle_call(:compile, _from, nil) do - shell = Mix.shell() - _otp_app = Mix.Project.config() |> Keyword.get(:app) is_release = Mix.env() in [:prod, :bench] cargo_opts = %{ @@ -30,6 +28,13 @@ defmodule Rustler.Compiler.Server do cargo = :cargo.init(File.cwd!(), cargo_opts) artifacts = :cargo.build_and_capture(cargo) + + # This drops the unique key in favour of the crate name + artifacts = + artifacts + |> Map.values() + |> Map.new(&{&1[:name], &1}) + {:reply, artifacts, artifacts} end diff --git a/rustler_tests/lib/binary_example.ex b/rustler_tests/lib/binary_example.ex index b21f13737..5946f27cf 100644 --- a/rustler_tests/lib/binary_example.ex +++ b/rustler_tests/lib/binary_example.ex @@ -1,6 +1,5 @@ defmodule BinaryExample do use Rustler, otp_app: :rustler_test, - crate: :binary_example, - lib: false + crate: :binary_example end diff --git a/rustler_tests/native/rustler_test/Cargo.toml b/rustler_tests/native/rustler_test/Cargo.toml index 9f0b4d04c..0bd0d3da6 100644 --- a/rustler_tests/native/rustler_test/Cargo.toml +++ b/rustler_tests/native/rustler_test/Cargo.toml @@ -13,10 +13,6 @@ crate-type = ["cdylib"] name = "hello_rust" path = "src/main.rs" -[[bin]] -name = "hello_rust2" -path = "src/main.rs" - [dependencies] lazy_static = "1.4" rustler = { path = "../../../rustler" } diff --git a/rustler_tests/test/binary_example_test.exs b/rustler_tests/test/binary_example_test.exs index 7c1476c52..81154516c 100644 --- a/rustler_tests/test/binary_example_test.exs +++ b/rustler_tests/test/binary_example_test.exs @@ -2,22 +2,7 @@ defmodule BinaryExampleTest do use ExUnit.Case test "binary is compiled" do - bins = ~w(binary_example hello_rust hello_rust2) - - for bin <- bins do - assert_exists(bin) - end - end - - defp assert_exists(name) do - assert_exists(name, :os.type()) - end - - defp assert_exists(name, {:win32, _} = type) do - assert File.exists?("priv/crates/#{name}.exe") - end - - defp assert_exists(name, type) do - assert File.exists?("priv/native/#{name}") + path = BinaryExample.rustler_path() + File.exists?(path) end end