@@ -59,7 +59,7 @@ impl BuildScriptOutput {
59
59
if key_split. len ( ) <= 1 || key_split[ 0 ] != "cargo" {
60
60
// Not a cargo directive.
61
61
print ! ( "{}" , line) ;
62
- return None
62
+ return None ;
63
63
}
64
64
match key_split[ 1 ] {
65
65
"rustc-link-lib" => Some ( BuildScriptOutput :: LinkLib ( param) ) ,
@@ -68,21 +68,30 @@ impl BuildScriptOutput {
68
68
"rustc-flags" => Some ( BuildScriptOutput :: Flags ( param) ) ,
69
69
"rustc-env" => Some ( BuildScriptOutput :: Env ( param) ) ,
70
70
"rerun-if-changed" | "rerun-if-env-changed" =>
71
- // Ignored because Bazel will re-run if those change all the time.
72
- None ,
71
+ // Ignored because Bazel will re-run if those change all the time.
72
+ {
73
+ None
74
+ }
73
75
"warning" => {
74
76
eprintln ! ( "Build Script Warning: {}" , split[ 1 ] ) ;
75
77
None
76
- } ,
78
+ }
77
79
"rustc-cdylib-link-arg" => {
78
80
// cargo:rustc-cdylib-link-arg=FLAG — Passes custom flags to a linker for cdylib crates.
79
- eprintln ! ( "Warning: build script returned unsupported directive `{}`" , split[ 0 ] ) ;
81
+ eprintln ! (
82
+ "Warning: build script returned unsupported directive `{}`" ,
83
+ split[ 0 ]
84
+ ) ;
80
85
None
81
- } ,
86
+ }
82
87
_ => {
83
88
// cargo:KEY=VALUE — Metadata, used by links scripts.
84
- Some ( BuildScriptOutput :: DepEnv ( format ! ( "{}={}" , key_split[ 1 ] . to_uppercase( ) , param) ) )
85
- } ,
89
+ Some ( BuildScriptOutput :: DepEnv ( format ! (
90
+ "{}={}" ,
91
+ key_split[ 1 ] . to_uppercase( ) ,
92
+ param
93
+ ) ) )
94
+ }
86
95
}
87
96
}
88
97
@@ -101,29 +110,26 @@ impl BuildScriptOutput {
101
110
102
111
/// Take a [Command], execute it and converts its input into a vector of [BuildScriptOutput]
103
112
pub fn from_command ( cmd : & mut Command ) -> Result < Vec < BuildScriptOutput > , Option < i32 > > {
104
- let mut child = cmd. stdout ( Stdio :: piped ( ) ) . spawn ( ) . expect ( "Unable to start binary" ) ;
113
+ let mut child = cmd
114
+ . stdout ( Stdio :: piped ( ) )
115
+ . spawn ( )
116
+ . expect ( "Unable to start binary" ) ;
105
117
let ecode = child. wait ( ) . expect ( "failed to wait on child" ) ;
106
- let reader = BufReader :: new (
107
- child
108
- . stdout
109
- . as_mut ( )
110
- . expect ( "Failed to open stdout" ) ,
111
- ) ;
118
+ let reader = BufReader :: new ( child. stdout . as_mut ( ) . expect ( "Failed to open stdout" ) ) ;
112
119
let output = Self :: from_reader ( reader) ;
113
120
if ecode. success ( ) {
114
121
Ok ( output)
115
122
} else {
116
123
Err ( ecode. code ( ) )
117
124
}
118
-
119
125
}
120
126
121
127
/// Convert a vector of [BuildScriptOutput] into a list of environment variables.
122
- pub fn to_env ( v : & Vec < BuildScriptOutput > ) -> String {
128
+ pub fn to_env ( v : & Vec < BuildScriptOutput > , exec_root : & str ) -> String {
123
129
v. iter ( )
124
130
. filter_map ( |x| {
125
131
if let BuildScriptOutput :: Env ( env) = x {
126
- Some ( env . to_owned ( ) )
132
+ Some ( Self :: redact_exec_root ( env , exec_root ) )
127
133
} else {
128
134
None
129
135
}
@@ -137,7 +143,9 @@ impl BuildScriptOutput {
137
143
// TODO: make use of `strip_suffix`.
138
144
const SYS_CRATE_SUFFIX : & str = "-sys" ;
139
145
let name = if crate_name. ends_with ( SYS_CRATE_SUFFIX ) {
140
- crate_name. split_at ( crate_name. rfind ( SYS_CRATE_SUFFIX ) . unwrap ( ) ) . 0
146
+ crate_name
147
+ . split_at ( crate_name. rfind ( SYS_CRATE_SUFFIX ) . unwrap ( ) )
148
+ . 0
141
149
} else {
142
150
crate_name
143
151
} ;
@@ -165,14 +173,18 @@ impl BuildScriptOutput {
165
173
BuildScriptOutput :: Flags ( e) => compile_flags. push ( e. to_owned ( ) ) ,
166
174
BuildScriptOutput :: LinkLib ( e) => link_flags. push ( format ! ( "-l{}" , e) ) ,
167
175
BuildScriptOutput :: LinkSearch ( e) => link_flags. push ( format ! ( "-L{}" , e) ) ,
168
- _ => { } ,
176
+ _ => { }
169
177
}
170
178
}
171
179
CompileAndLinkFlags {
172
180
compile_flags : compile_flags. join ( "\n " ) ,
173
- link_flags : link_flags. join ( "\n " ) . replace ( exec_root , "${pwd}" ) ,
181
+ link_flags : Self :: redact_exec_root ( & link_flags. join ( "\n " ) , exec_root ) ,
174
182
}
175
183
}
184
+
185
+ fn redact_exec_root ( value : & str , exec_root : & str ) -> String {
186
+ value. replace ( exec_root, "${pwd}" )
187
+ }
176
188
}
177
189
178
190
#[ cfg( test) ]
@@ -193,30 +205,44 @@ cargo:rerun-if-changed=ignored
193
205
cargo:rustc-cfg=feature=awesome
194
206
cargo:version=123
195
207
cargo:version_number=1010107f
208
+ cargo:rustc-env=SOME_PATH=/some/absolute/path/beep
196
209
" ,
197
210
) ;
198
211
let reader = BufReader :: new ( buff) ;
199
212
let result = BuildScriptOutput :: from_reader ( reader) ;
200
- assert_eq ! ( result. len( ) , 8 ) ;
213
+ assert_eq ! ( result. len( ) , 9 ) ;
201
214
assert_eq ! ( result[ 0 ] , BuildScriptOutput :: LinkLib ( "sdfsdf" . to_owned( ) ) ) ;
202
215
assert_eq ! ( result[ 1 ] , BuildScriptOutput :: Env ( "FOO=BAR" . to_owned( ) ) ) ;
203
- assert_eq ! ( result[ 2 ] , BuildScriptOutput :: LinkSearch ( "/some/absolute/path/bleh" . to_owned( ) ) ) ;
216
+ assert_eq ! (
217
+ result[ 2 ] ,
218
+ BuildScriptOutput :: LinkSearch ( "/some/absolute/path/bleh" . to_owned( ) )
219
+ ) ;
204
220
assert_eq ! ( result[ 3 ] , BuildScriptOutput :: Env ( "BAR=FOO" . to_owned( ) ) ) ;
205
221
assert_eq ! ( result[ 4 ] , BuildScriptOutput :: Flags ( "-Lblah" . to_owned( ) ) ) ;
206
222
assert_eq ! (
207
223
result[ 5 ] ,
208
224
BuildScriptOutput :: Cfg ( "feature=awesome" . to_owned( ) )
209
225
) ;
210
- assert_eq ! ( result[ 6 ] , BuildScriptOutput :: DepEnv ( "VERSION=123" . to_owned( ) ) ) ;
211
- assert_eq ! ( result[ 7 ] , BuildScriptOutput :: DepEnv ( "VERSION_NUMBER=1010107f" . to_owned( ) ) ) ;
226
+ assert_eq ! (
227
+ result[ 6 ] ,
228
+ BuildScriptOutput :: DepEnv ( "VERSION=123" . to_owned( ) )
229
+ ) ;
230
+ assert_eq ! (
231
+ result[ 7 ] ,
232
+ BuildScriptOutput :: DepEnv ( "VERSION_NUMBER=1010107f" . to_owned( ) )
233
+ ) ;
234
+ assert_eq ! (
235
+ result[ 8 ] ,
236
+ BuildScriptOutput :: Env ( "SOME_PATH=/some/absolute/path/beep" . to_owned( ) )
237
+ ) ;
212
238
213
239
assert_eq ! (
214
240
BuildScriptOutput :: to_dep_env( & result, "my-crate-sys" ) ,
215
241
"DEP_MY_CRATE_VERSION=123\n DEP_MY_CRATE_VERSION_NUMBER=1010107f" . to_owned( )
216
242
) ;
217
243
assert_eq ! (
218
- BuildScriptOutput :: to_env( & result) ,
219
- "FOO=BAR\n BAR=FOO" . to_owned( )
244
+ BuildScriptOutput :: to_env( & result, "/some/absolute/path" ) ,
245
+ "FOO=BAR\n BAR=FOO\n SOME_PATH=${pwd}/beep " . to_owned( )
220
246
) ;
221
247
assert_eq ! (
222
248
BuildScriptOutput :: to_flags( & result, "/some/absolute/path" ) ,
@@ -228,5 +254,4 @@ cargo:version_number=1010107f
228
254
}
229
255
) ;
230
256
}
231
-
232
257
}
0 commit comments