From bb693f2936206a595a823a123406f7fcde0928a1 Mon Sep 17 00:00:00 2001 From: Magnus Ottenklinger Date: Thu, 17 Aug 2023 22:48:28 +0200 Subject: [PATCH] Extend load_from to allow loading from an absolute path `load_from` already has a format to specify a path relative to an OTP application. This commit extends the type for `load_from` to also allow setting an absolute path. Note that no special check for the validity of the path is being done, as we do not know beforehand what the target operating system looks like. Setting this configuration correctly is thus the user's responsibility. --- rustler_mix/lib/rustler.ex | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/rustler_mix/lib/rustler.ex b/rustler_mix/lib/rustler.ex index 1297fe60..1e083b82 100644 --- a/rustler_mix/lib/rustler.ex +++ b/rustler_mix/lib/rustler.ex @@ -65,9 +65,18 @@ defmodule Rustler do loaded from at runtime. By default the compiled artifact is loaded from the owning `:otp_app`'s `priv/native` directory. This option comes in handy in combination with the `:skip_compilation?` option in order to load pre-compiled - artifacts. To override the default behaviour specify a tuple: - `{:my_app, "priv/native/"}`. Due to the way `:erlang.load_nif/2` - works, the artifact should not include the file extension (i.e. `.so`, `.dll`). + artifacts. The configuration format: + + ``` + {otp_app :: atom(), path :: String.t()} | abspath :: String.t()} + ``` + + The tuple version defines that the artifact shall be found relative to `otp_app`, + for example `{:my_app, "priv/native/"}`. The non-tuple alternative requires + an absolute path. + + Due to the way `:erlang.load_nif/2` works, the artifact should not + include the file extension (i.e. `.so`, `.dll`). * `:mode` - Specify which mode to compile the crate with (default: `:release`) @@ -123,12 +132,16 @@ defmodule Rustler do # {:error, {:upgrade, 'Upgrade not supported by this NIF library.'}} :code.purge(__MODULE__) - {otp_app, path} = @load_from - load_path = - otp_app - |> Application.app_dir(path) - |> to_charlist() + case @load_from do + {otp_app, path} -> + otp_app + |> Application.app_dir(path) + |> to_charlist() + + path -> + to_charlist(path) + end load_data = Rustler.construct_load_data(@load_data, @load_data_fun)