Skip to content

Commit ac39ae8

Browse files
committed
Run rustc separately for each target
1 parent f1d90f6 commit ac39ae8

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

rustler_mix/lib/rustler/compiler.ex

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,30 @@ defmodule Rustler.Compiler do
1515

1616
Mix.shell().info("Compiling crate #{config.crate} in #{config.mode} mode (#{config.path})")
1717

18-
[cmd | args] =
18+
cmd_line =
1919
make_base_command(config.cargo)
2020
|> make_no_default_features_flag(config.default_features)
2121
|> make_features_flag(config.features)
2222
|> make_target_flag(config.target)
2323
|> make_build_mode_flag(config.mode)
24-
|> make_platform_hacks(crate_full_path, :os.type())
25-
26-
compile_result =
27-
System.cmd(cmd, args,
28-
cd: crate_full_path,
29-
stderr_to_stdout: true,
30-
env: [
31-
{"CARGO_TARGET_DIR", config.target_dir},
32-
{"RUSTLER_NIF_VERSION", nif_version()}
33-
| config.env
34-
],
35-
into: IO.stream(:stdio, :line)
36-
)
37-
38-
case compile_result do
39-
{_, 0} -> :ok
40-
{_, code} -> raise "Rust NIF compile error (rustc exit code #{code})"
24+
25+
for [cmd | args] <- make_platform_hacks(cmd_line, crate_full_path, :os.type()) do
26+
compile_result =
27+
System.cmd(cmd, args,
28+
cd: crate_full_path,
29+
stderr_to_stdout: true,
30+
env: [
31+
{"CARGO_TARGET_DIR", config.target_dir},
32+
{"RUSTLER_NIF_VERSION", nif_version()}
33+
| config.env
34+
],
35+
into: IO.stream(:stdio, :line)
36+
)
37+
38+
case compile_result do
39+
{_, 0} -> :ok
40+
{_, code} -> raise "Rust NIF compile error (rustc exit code #{code})"
41+
end
4142
end
4243

4344
handle_artifacts(crate_full_path, config)
@@ -69,12 +70,12 @@ defmodule Rustler.Compiler do
6970
["rustup", "run", version, "cargo", "rustc"]
7071
end
7172

72-
defp make_platform_hacks(args, crate_path, {:unix, :darwin}) do
73+
defp make_platform_hacks(cmd_line, crate_path, {:unix, :darwin}) do
7374
root = Path.join([".cargo", "config"])
7475
path = Path.join([crate_path, ".cargo", "config"])
7576

7677
if File.exists?(root) || File.exists?(path) do
77-
args
78+
[cmd_line]
7879
else
7980
IO.write([
8081
"\n",
@@ -104,11 +105,21 @@ defmodule Rustler.Compiler do
104105
"\n"
105106
])
106107

107-
args ++ ["--", "-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
108+
for {name, type} <- names(crate_path) do
109+
type_switch =
110+
if type == :lib do
111+
["--lib"]
112+
else
113+
["--bin", name]
114+
end
115+
116+
cmd_line ++
117+
type_switch ++ ["--", "-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
118+
end
108119
end
109120
end
110121

111-
defp make_platform_hacks(args, _, _), do: args
122+
defp make_platform_hacks(cmd_line, _, _), do: [cmd_line]
112123

113124
defp make_no_default_features_flag(args, true), do: args
114125
defp make_no_default_features_flag(args, false), do: args ++ ["--no-default-features"]
@@ -150,9 +161,7 @@ defmodule Rustler.Compiler do
150161
end
151162

152163
defp handle_artifacts(path, config) do
153-
toml = toml_data(path)
154164
target = config.target
155-
names = get_name(toml, :lib) ++ get_name(toml, :bin)
156165

157166
output_dir =
158167
if is_binary(target) do
@@ -161,7 +170,7 @@ defmodule Rustler.Compiler do
161170
Atom.to_string(config.mode)
162171
end
163172

164-
Enum.each(names, fn {name, type} ->
173+
Enum.each(names(path), fn {name, type} ->
165174
{src_file, dst_file} = make_file_names(name, type, target)
166175
compiled_lib = Path.join([config.target_dir, output_dir, src_file])
167176
destination_lib = Path.join(priv_dir(), dst_file)
@@ -175,6 +184,11 @@ defmodule Rustler.Compiler do
175184
end)
176185
end
177186

187+
defp names(path) do
188+
toml = toml_data(path)
189+
get_name(toml, :lib) ++ get_name(toml, :bin)
190+
end
191+
178192
defp get_name(toml, section) do
179193
case toml[to_string(section)] do
180194
nil -> []

0 commit comments

Comments
 (0)