Skip to content

Commit 65d4171

Browse files
committed
Run rustc separately for each target
1 parent f93e3c3 commit 65d4171

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

rustler_mix/lib/rustler/compiler.ex

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@ 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: [{"CARGO_TARGET_DIR", config.target_dir} | config.env],
31-
into: IO.stream(:stdio, :line)
32-
)
33-
34-
case compile_result do
35-
{_, 0} -> :ok
36-
{_, 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: [{"CARGO_TARGET_DIR", config.target_dir} | config.env],
31+
into: IO.stream(:stdio, :line)
32+
)
33+
34+
case compile_result do
35+
{_, 0} -> :ok
36+
{_, code} -> raise "Rust NIF compile error (rustc exit code #{code})"
37+
end
3738
end
3839

3940
handle_artifacts(crate_full_path, config)
@@ -61,12 +62,12 @@ defmodule Rustler.Compiler do
6162
["rustup", "run", version, "cargo", "rustc"]
6263
end
6364

64-
defp make_platform_hacks(args, crate_path, {:unix, :darwin}) do
65+
defp make_platform_hacks(cmd_line, crate_path, {:unix, :darwin}) do
6566
root = Path.join([".cargo", "config"])
6667
path = Path.join([crate_path, ".cargo", "config"])
6768

6869
if File.exists?(root) || File.exists?(path) do
69-
args
70+
[cmd_line]
7071
else
7172
IO.write([
7273
"\n",
@@ -96,11 +97,21 @@ defmodule Rustler.Compiler do
9697
"\n"
9798
])
9899

99-
args ++ ["--", "-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
100+
for {name, type} <- names(crate_path) do
101+
type_switch =
102+
if type == :lib do
103+
["--lib"]
104+
else
105+
["--bin", name]
106+
end
107+
108+
cmd_line ++
109+
type_switch ++ ["--", "-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
110+
end
100111
end
101112
end
102113

103-
defp make_platform_hacks(args, _, _), do: args
114+
defp make_platform_hacks(cmd_line, _, _), do: [cmd_line]
104115

105116
defp make_no_default_features_flag(args, true), do: args
106117
defp make_no_default_features_flag(args, false), do: args ++ ["--no-default-features"]
@@ -115,10 +126,7 @@ defmodule Rustler.Compiler do
115126
defp make_build_mode_flag(args, :debug), do: args
116127

117128
defp handle_artifacts(path, config) do
118-
toml = toml_data(path)
119-
names = get_name(toml, :lib) ++ get_name(toml, :bin)
120-
121-
Enum.each(names, fn {name, type} ->
129+
Enum.each(names(path), fn {name, type} ->
122130
{src_file, dst_file} = make_file_names(name, type)
123131
compiled_lib = Path.join([config.target_dir, Atom.to_string(config.mode), src_file])
124132
destination_lib = Path.join(priv_dir(), dst_file)
@@ -132,6 +140,11 @@ defmodule Rustler.Compiler do
132140
end)
133141
end
134142

143+
defp names(path) do
144+
toml = toml_data(path)
145+
get_name(toml, :lib) ++ get_name(toml, :bin)
146+
end
147+
135148
defp get_name(toml, section) do
136149
case toml[to_string(section)] do
137150
nil -> []

0 commit comments

Comments
 (0)