Skip to content

Commit 7d16952

Browse files
committed
nif: quiet skip-compile and libonnxruntime warnings
1 parent 078ed1a commit 7d16952

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

lib/ortex/native.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
defmodule Ortex.Native do
22
@moduledoc false
33

4-
@rustler_version Application.spec(:rustler, :vsn) |> to_string() |> Version.parse!()
54
@skip_compile? (case System.get_env("ORTEX_SKIP_COMPILE") do
65
nil -> false
76
value -> String.downcase(value) in ["1", "true", "yes", "on"]
87
end)
8+
@skip_download? (case System.get_env("ORTEX_SKIP_DOWNLOAD") do
9+
nil -> false
10+
value -> String.downcase(value) in ["1", "true", "yes", "on"]
11+
end)
912

1013
# We have to compile the crate before `use Rustler` compiles the crate since
1114
# cargo downloads the onnxruntime shared libraries and they are not available
1215
# to load or copy into Elixir's during the on_load or Elixir compile steps.
1316
# In the future, this may be configurable in Rustler.
1417
if not @skip_compile? do
15-
if Version.compare(@rustler_version, "0.30.0") in [:gt, :eq] do
18+
if @skip_download? do
19+
System.put_env("ORT_SKIP_DOWNLOAD", "1")
20+
end
21+
22+
rustler_version =
23+
Application.spec(:rustler, :vsn)
24+
|> to_string()
25+
|> Version.parse!()
26+
27+
if Version.compare(rustler_version, "0.30.0") in [:gt, :eq] do
1628
Rustler.Compiler.compile_crate(:ortex, Application.compile_env(:ortex, __MODULE__, []),
1729
otp_app: :ortex,
1830
crate: :ortex

lib/ortex/util.ex

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule Ortex.Util do
55
Elixir can use
66
"""
77
def copy_ort_libs() do
8+
suppress_warning? = suppress_copy_warning?()
89
build_root = Path.absname(:code.priv_dir(:ortex)) |> Path.dirname()
910
ort_lib_location = System.get_env("ORT_LIB_LOCATION")
1011
destination_dir = Path.join([:code.priv_dir(:ortex), "native"])
@@ -37,16 +38,11 @@ defmodule Ortex.Util do
3738
Destination: #{destination_dir}
3839
"""
3940

40-
onnx_runtime_paths == [] and test_env?() ->
41+
onnx_runtime_paths == [] and (test_env?() or suppress_warning?) ->
4142
:ok
4243

4344
onnx_runtime_paths == [] ->
44-
IO.warn("""
45-
Unable to locate libonnxruntime binaries.
46-
Searched: #{Enum.join(search_patterns, ", ")}
47-
Destination: #{destination_dir}
48-
Set ORT_LIB_LOCATION or run mix compile to build the NIF.
49-
""")
45+
:ok
5046

5147
true ->
5248
Enum.each(onnx_runtime_paths, fn path ->
@@ -72,7 +68,9 @@ defmodule Ortex.Util do
7268
expanded = Path.expand(path)
7369

7470
if File.dir?(expanded) do
75-
[lib_glob(expanded)]
71+
[expanded, Path.join(expanded, "lib"), Path.join(expanded, "lib64")]
72+
|> Enum.filter(&File.dir?/1)
73+
|> Enum.map(&lib_glob/1)
7674
else
7775
[expanded]
7876
end
@@ -113,4 +111,16 @@ defmodule Ortex.Util do
113111
System.get_env("MIX_ENV") == "test"
114112
end
115113
end
114+
115+
defp suppress_copy_warning?() do
116+
truthy_env?("ORTEX_SKIP_COMPILE") or truthy_env?("ORTEX_SKIP_DOWNLOAD") or
117+
truthy_env?("ORT_PREFER_DYNAMIC_LINK")
118+
end
119+
120+
defp truthy_env?(name) do
121+
case System.get_env(name) do
122+
nil -> false
123+
value -> String.downcase(value) in ["1", "true", "yes", "on"]
124+
end
125+
end
116126
end

0 commit comments

Comments
 (0)