Skip to content

Commit

Permalink
Improve messages, make the tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Apr 25, 2020
1 parent 2b8f61e commit 0b435b3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 40 deletions.
30 changes: 22 additions & 8 deletions rustler_mix/lib/rustler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions rustler_mix/lib/rustler/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 7 additions & 2 deletions rustler_mix/lib/rustler/compiler/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 = %{
Expand All @@ -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

Expand Down
3 changes: 1 addition & 2 deletions rustler_tests/lib/binary_example.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule BinaryExample do
use Rustler,
otp_app: :rustler_test,
crate: :binary_example,
lib: false
crate: :binary_example
end
4 changes: 0 additions & 4 deletions rustler_tests/native/rustler_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
19 changes: 2 additions & 17 deletions rustler_tests/test/binary_example_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0b435b3

Please sign in to comment.