@@ -15,29 +15,30 @@ defmodule Rustler.Compiler do
15
15
16
16
Mix . shell ( ) . info ( "Compiling crate #{ config . crate } in #{ config . mode } mode (#{ config . path } )" )
17
17
18
- [ cmd | args ] =
18
+ cmd_line =
19
19
make_base_command ( config . cargo )
20
20
|> make_no_default_features_flag ( config . default_features )
21
21
|> make_features_flag ( config . features )
22
22
|> make_target_flag ( config . target )
23
23
|> 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
41
42
end
42
43
43
44
handle_artifacts ( crate_full_path , config )
@@ -69,12 +70,12 @@ defmodule Rustler.Compiler do
69
70
[ "rustup" , "run" , version , "cargo" , "rustc" ]
70
71
end
71
72
72
- defp make_platform_hacks ( args , crate_path , { :unix , :darwin } ) do
73
+ defp make_platform_hacks ( cmd_line , crate_path , { :unix , :darwin } ) do
73
74
root = Path . join ( [ ".cargo" , "config" ] )
74
75
path = Path . join ( [ crate_path , ".cargo" , "config" ] )
75
76
76
77
if File . exists? ( root ) || File . exists? ( path ) do
77
- args
78
+ [ cmd_line ]
78
79
else
79
80
IO . write ( [
80
81
"\n " ,
@@ -104,11 +105,21 @@ defmodule Rustler.Compiler do
104
105
"\n "
105
106
] )
106
107
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
108
119
end
109
120
end
110
121
111
- defp make_platform_hacks ( args , _ , _ ) , do: args
122
+ defp make_platform_hacks ( cmd_line , _ , _ ) , do: [ cmd_line ]
112
123
113
124
defp make_no_default_features_flag ( args , true ) , do: args
114
125
defp make_no_default_features_flag ( args , false ) , do: args ++ [ "--no-default-features" ]
@@ -150,9 +161,7 @@ defmodule Rustler.Compiler do
150
161
end
151
162
152
163
defp handle_artifacts ( path , config ) do
153
- toml = toml_data ( path )
154
164
target = config . target
155
- names = get_name ( toml , :lib ) ++ get_name ( toml , :bin )
156
165
157
166
output_dir =
158
167
if is_binary ( target ) do
@@ -161,7 +170,7 @@ defmodule Rustler.Compiler do
161
170
Atom . to_string ( config . mode )
162
171
end
163
172
164
- Enum . each ( names , fn { name , type } ->
173
+ Enum . each ( names ( path ) , fn { name , type } ->
165
174
{ src_file , dst_file } = make_file_names ( name , type , target )
166
175
compiled_lib = Path . join ( [ config . target_dir , output_dir , src_file ] )
167
176
destination_lib = Path . join ( priv_dir ( ) , dst_file )
@@ -175,6 +184,11 @@ defmodule Rustler.Compiler do
175
184
end )
176
185
end
177
186
187
+ defp names ( path ) do
188
+ toml = toml_data ( path )
189
+ get_name ( toml , :lib ) ++ get_name ( toml , :bin )
190
+ end
191
+
178
192
defp get_name ( toml , section ) do
179
193
case toml [ to_string ( section ) ] do
180
194
nil -> [ ]
0 commit comments