-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Common cargo #302
base: master
Are you sure you want to change the base?
Common cargo #302
Changes from all commits
339586d
29d585b
f7179a7
cdbc0d0
ed245a1
0360019
72f4ecc
109b73b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,13 +33,8 @@ defmodule Rustler do | |
value. If you have more than one crate in your project, you will need to | ||
be explicit about which crate you intend to use. | ||
|
||
* `:default_features` - a boolean to specify whether the crate's default features | ||
should be used. | ||
|
||
* `:env` - Specify a list of environment variables when envoking the compiler. | ||
|
||
* `:features` - a list of features to enable when compiling the crate. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can probably readd this if required. |
||
* `:load_data` - Any valid term. This value is passed into the NIF when it is | ||
loaded (default: `0`) | ||
|
||
|
@@ -56,12 +51,6 @@ defmodule Rustler do | |
- When `Mix.env()` is `:dev` or `:test`, the crate will be compiled in `:debug` mode. | ||
- When `Mix.env()` is `:prod` or `:bench`, the crate will be compiled in `:release` mode. | ||
|
||
* `:path` - By default, rustler expects the crate to be found in `native/<crate>` in the | ||
root of the project. Use this option to override this. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not necessary anymore, as we now expect a |
||
* `:skip_compilation?` - This option skips envoking the rust compiler. Specify this option | ||
in combination with `:load_from` to load a pre-compiled artifact. | ||
|
||
* `:target` - Specify a compile [target] triple. | ||
|
||
* `:target_dir`: Override the compiler output directory. | ||
|
@@ -82,42 +71,52 @@ defmodule Rustler do | |
quote bind_quoted: [opts: opts] do | ||
config = Rustler.Compiler.compile_crate(__MODULE__, opts) | ||
|
||
for resource <- config.external_resources do | ||
@external_resource resource | ||
end | ||
@load_from {config.otp_app, config.load_path} | ||
|
||
if config.lib do | ||
@load_from config.load_from | ||
@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 | ||
@on_load :rustler_init | ||
|
||
@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__) | ||
|
||
def rustler_path do | ||
{otp_app, path} = @load_from | ||
|
||
load_path = | ||
otp_app | ||
|> Application.app_dir(path) | ||
|> to_charlist() | ||
|
||
:erlang.load_nif(load_path, @load_data) | ||
Path.join(:code.priv_dir(otp_app), path) | ||
end | ||
end | ||
end | ||
|
||
defmacro __before_compile_nif__(_env) do | ||
quoted = | ||
quote do | ||
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 | ||
|
||
@on_load :rustler_init | ||
@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__) | ||
load_path = String.to_charlist(rustler_path()) | ||
:ok = :erlang.load_nif(load_path, @load_data) | ||
end | ||
end | ||
|
||
# quoted |> Macro.to_string |> IO.puts | ||
quoted | ||
end | ||
|
||
@doc false | ||
@spec rustler_version() :: binary() | ||
def rustler_version, do: "0.22.0" | ||
|
||
@doc """ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and the other occurrences of
@external_resource
will be readded.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, actually I don't like this, it would mean (if I understand things correctly) that automatic recompilation is only triggered if
lib.rs
is changed, not if any of its dependencies (like other.rs
files or a crate dependencies) are changed, is that right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll do this via
__mix_recompile__?
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__mix_recompile__?
seems very new, I think we might not be able to support older versions of Elixir with that anymore?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just found it in the sources. Worst case: It won't be better than what we have now :)
The issue is that you can't really tell
mix
up-front what exact files will influence the final object files of a Rust project. I'll think about it.