@@ -15,25 +15,26 @@ 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: [ { "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
37
38
end
38
39
39
40
handle_artifacts ( crate_full_path , config )
@@ -61,12 +62,12 @@ defmodule Rustler.Compiler do
61
62
[ "rustup" , "run" , version , "cargo" , "rustc" ]
62
63
end
63
64
64
- defp make_platform_hacks ( args , crate_path , { :unix , :darwin } ) do
65
+ defp make_platform_hacks ( cmd_line , crate_path , { :unix , :darwin } ) do
65
66
root = Path . join ( [ ".cargo" , "config" ] )
66
67
path = Path . join ( [ crate_path , ".cargo" , "config" ] )
67
68
68
69
if File . exists? ( root ) || File . exists? ( path ) do
69
- args
70
+ [ cmd_line ]
70
71
else
71
72
IO . write ( [
72
73
"\n " ,
@@ -96,11 +97,21 @@ defmodule Rustler.Compiler do
96
97
"\n "
97
98
] )
98
99
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
100
111
end
101
112
end
102
113
103
- defp make_platform_hacks ( args , _ , _ ) , do: args
114
+ defp make_platform_hacks ( cmd_line , _ , _ ) , do: [ cmd_line ]
104
115
105
116
defp make_no_default_features_flag ( args , true ) , do: args
106
117
defp make_no_default_features_flag ( args , false ) , do: args ++ [ "--no-default-features" ]
@@ -115,10 +126,7 @@ defmodule Rustler.Compiler do
115
126
defp make_build_mode_flag ( args , :debug ) , do: args
116
127
117
128
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 } ->
122
130
{ src_file , dst_file } = make_file_names ( name , type )
123
131
compiled_lib = Path . join ( [ config . target_dir , Atom . to_string ( config . mode ) , src_file ] )
124
132
destination_lib = Path . join ( priv_dir ( ) , dst_file )
@@ -132,6 +140,11 @@ defmodule Rustler.Compiler do
132
140
end )
133
141
end
134
142
143
+ defp names ( path ) do
144
+ toml = toml_data ( path )
145
+ get_name ( toml , :lib ) ++ get_name ( toml , :bin )
146
+ end
147
+
135
148
defp get_name ( toml , section ) do
136
149
case toml [ to_string ( section ) ] do
137
150
nil -> [ ]
0 commit comments