Skip to content
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

Use compile_env for compile-time configuration #550

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions rustler_mix/lib/rustler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ defmodule Rustler do

Example

defmodule NIF do
use Rustler, load_data_fun: {Deployment, :nif_data}
end
defmodule NIF do
use Rustler, load_data_fun: {Deployment, :nif_data}
end

defmodule Deployment do
def nif_data do
:code.priv_dir(:otp_app) |> IO.iodata_to_binary()
defmodule Deployment do
def nif_data do
:code.priv_dir(:otp_app) |> IO.iodata_to_binary()
end
end
end

* `:load_from` - This option allows control over where the final artifact should be
loaded from at runtime. By default the compiled artifact is loaded from the
Expand All @@ -79,7 +79,7 @@ defmodule Rustler do

* `:target` - Specify a compile [target] triple.

* `:target_dir`: Override the compiler output directory.
* `:target_dir` - Override the compiler output directory.

Any of the above options can be passed directly into the `use` macro like so:

Expand All @@ -95,7 +95,9 @@ defmodule Rustler do

defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
config = Rustler.Compiler.compile_crate(__MODULE__, opts)
otp_app = Keyword.fetch!(opts, :otp_app)
env = Application.compile_env(otp_app, __MODULE__, [])
config = Rustler.Compiler.compile_crate(otp_app, __MODULE__, env, opts)

for resource <- config.external_resources do
@external_resource resource
Expand Down Expand Up @@ -170,8 +172,8 @@ defmodule Rustler do
"""
def nif_versions,
do: [
'2.14',
'2.15',
'2.16'
~c"2.14",
~c"2.15",
~c"2.16"
]
end
5 changes: 2 additions & 3 deletions rustler_mix/lib/rustler/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ defmodule Rustler.Compiler do
alias Rustler.Compiler.{Config, Messages, Rustup}

@doc false
def compile_crate(module, opts) do
otp_app = Keyword.fetch!(opts, :otp_app)
config = Config.from(otp_app, module, opts)
def compile_crate(otp_app, module, config, opts) do
config = Config.from(otp_app, module, config, opts)

unless config.skip_compilation? do
crate_full_path = Path.expand(config.path, File.cwd!())
Expand Down
13 changes: 9 additions & 4 deletions rustler_mix/lib/rustler/compiler/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ defmodule Rustler.Compiler.Config do

alias Rustler.Compiler.Config

def from(otp_app, module, opts) do
config = Application.get_env(otp_app, module, [])

def from(otp_app, module, config, opts) do
crate = config[:crate] || opts[:crate] || otp_app

# TODO: Remove in 1.0
rustler_crates = Mix.Project.config()[:rustler_crates] || []
rustler_crates =
if mix_config = Mix.Project.config()[:rustler_crates] do
mix_config
else
IO.warn(":rustler_crates in mix.exs is deprecated, please use config instead")
[]
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josevalim Am I misunderstanding something or is this logic the wrong way round? We should raise the warning if :rustler_crates is found, right?


legacy_config = rustler_crates[to_atom(crate)] || []

defaults = %Config{
Expand Down
22 changes: 0 additions & 22 deletions rustler_mix/lib/rustler/compiler/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ defmodule Rustler.Compiler.Messages do
"""
end

def message(:no_crates_property) do
"""
No NIF crates listed in project definition.
Add some crate directories under :rustler_crates in the mix.exs project definition to compile them.

Example:
rustler_crates: ["/native/my_nif_crate"]
"""
end

def message(:no_config) do
"""
Add your crate to the 'rustler_crates' attribute in the project function.
"""
end

def message({:differing_versions, crate, rustler_version, codegen_version}) do
"""
The '#{crate}' crate should have the same rustler and rustler_codegen version in its Cargo.toml:
Expand Down Expand Up @@ -78,12 +62,6 @@ defmodule Rustler.Compiler.Messages do
"""
end

def message({:cargo_no_name, crate}) do
"""
No library or binary with name listed in Cargo.toml of crate '#{crate}'.
"""
end
filmor marked this conversation as resolved.
Show resolved Hide resolved

def message({:unsupported_nif_version, version}) do
"""
Your current version of Erlang is on NIF version '#{version}'.
Expand Down