@@ -192,7 +192,8 @@ impl NginxSource {
192
192
/// Generates Rust bindings for NGINX
193
193
fn generate_binding ( nginx : & NginxSource ) {
194
194
let autoconf_makefile_path = nginx. build_dir . join ( "Makefile" ) ;
195
- let includes: Vec < _ > = parse_includes_from_makefile ( & autoconf_makefile_path)
195
+ let ( includes, defines) = parse_makefile ( & autoconf_makefile_path) ;
196
+ let includes: Vec < _ > = includes
196
197
. into_iter ( )
197
198
. map ( |path| {
198
199
if path. is_absolute ( ) {
@@ -207,7 +208,7 @@ fn generate_binding(nginx: &NginxSource) {
207
208
. map ( |path| format ! ( "-I{}" , path. to_string_lossy( ) ) )
208
209
. collect ( ) ;
209
210
210
- print_cargo_metadata ( nginx, & includes) . expect ( "cargo dependency metadata" ) ;
211
+ print_cargo_metadata ( nginx, & includes, & defines ) . expect ( "cargo dependency metadata" ) ;
211
212
212
213
// bindgen targets the latest known stable by default
213
214
let rust_target: bindgen:: RustTarget = env:: var ( "CARGO_PKG_RUST_VERSION" )
@@ -243,20 +244,47 @@ fn generate_binding(nginx: &NginxSource) {
243
244
/// Reads through the makefile generated by autoconf and finds all of the includes
244
245
/// used to compile nginx. This is used to generate the correct bindings for the
245
246
/// nginx source code.
246
- fn parse_includes_from_makefile ( nginx_autoconf_makefile_path : & PathBuf ) -> Vec < PathBuf > {
247
- fn extract_include_part ( line : & str ) -> & str {
248
- line. strip_suffix ( '\\' ) . map_or ( line, |s| s. trim ( ) )
249
- }
250
- /// Extracts the include path from a line of the autoconf generated makefile.
251
- fn extract_after_i_flag ( line : & str ) -> Option < & str > {
252
- let mut parts = line. split ( "-I " ) ;
253
- match parts. next ( ) {
254
- Some ( _) => parts. next ( ) . map ( extract_include_part) ,
255
- None => None ,
247
+ pub fn parse_makefile (
248
+ nginx_autoconf_makefile_path : & PathBuf ,
249
+ ) -> ( Vec < PathBuf > , Vec < ( String , Option < String > ) > ) {
250
+ fn parse_line (
251
+ includes : & mut Vec < String > ,
252
+ mut defines : Option < & mut Vec < ( String , Option < String > ) > > ,
253
+ line : & str ,
254
+ ) {
255
+ let mut words = shlex:: Shlex :: new ( line) ;
256
+
257
+ while let Some ( word) = words. next ( ) {
258
+ if let Some ( inc) = word. strip_prefix ( "-I" ) {
259
+ let value = if inc. is_empty ( ) {
260
+ words. next ( ) . expect ( "-I argument" )
261
+ } else {
262
+ inc. to_string ( )
263
+ } ;
264
+ includes. push ( value) ;
265
+ } else if let Some ( defs) = defines. as_mut ( ) {
266
+ if let Some ( def) = word. strip_prefix ( "-D" ) {
267
+ let def = if def. is_empty ( ) {
268
+ words. next ( ) . expect ( "-D argument" )
269
+ } else {
270
+ def. to_string ( )
271
+ } ;
272
+
273
+ if let Some ( ( name, value) ) = def. split_once ( "=" ) {
274
+ defs. push ( ( name. to_string ( ) , Some ( value. to_string ( ) ) ) ) ;
275
+ } else {
276
+ defs. push ( ( def. to_string ( ) , None ) ) ;
277
+ }
278
+ }
279
+ }
256
280
}
257
281
}
258
282
259
283
let mut includes = vec ! [ ] ;
284
+ let mut cflags_includes = vec ! [ ] ;
285
+
286
+ let mut defines = vec ! [ ] ;
287
+
260
288
let makefile_contents = match read_to_string ( nginx_autoconf_makefile_path) {
261
289
Ok ( path) => path,
262
290
Err ( e) => {
@@ -268,35 +296,36 @@ fn parse_includes_from_makefile(nginx_autoconf_makefile_path: &PathBuf) -> Vec<P
268
296
}
269
297
} ;
270
298
271
- let mut includes_lines = false ;
272
- for line in makefile_contents. lines ( ) {
273
- if !includes_lines {
274
- if let Some ( stripped) = line. strip_prefix ( "ALL_INCS" ) {
275
- includes_lines = true ;
276
- if let Some ( part) = extract_after_i_flag ( stripped) {
277
- includes. push ( part) ;
278
- }
279
- continue ;
280
- }
299
+ let lines = makefile_contents. lines ( ) ;
300
+ let mut line: String = "" . to_string ( ) ;
301
+ for l in lines {
302
+ if let Some ( part) = l. strip_suffix ( "\\ " ) {
303
+ line += part;
304
+ continue ;
281
305
}
282
306
283
- if includes_lines {
284
- if let Some ( part ) = extract_after_i_flag ( line ) {
285
- includes . push ( part ) ;
286
- } else {
287
- break ;
288
- }
307
+ line += l ;
308
+
309
+ if let Some ( tail ) = line . strip_prefix ( "ALL_INCS" ) {
310
+ parse_line ( & mut includes , None , tail ) ;
311
+ } else if let Some ( tail ) = line . strip_prefix ( "CFLAGS" ) {
312
+ parse_line ( & mut cflags_includes , Some ( & mut defines ) , tail ) ;
289
313
}
314
+
315
+ line. clear ( ) ;
290
316
}
291
317
292
- includes. into_iter ( ) . map ( PathBuf :: from) . collect ( )
318
+ includes. append ( & mut cflags_includes) ;
319
+
320
+ ( includes. into_iter ( ) . map ( PathBuf :: from) . collect ( ) , defines)
293
321
}
294
322
295
323
/// Collect info about the nginx configuration and expose it to the dependents via
296
324
/// `DEP_NGINX_...` variables.
297
325
pub fn print_cargo_metadata < T : AsRef < Path > > (
298
326
nginx : & NginxSource ,
299
327
includes : & [ T ] ,
328
+ defines : & [ ( String , Option < String > ) ] ,
300
329
) -> Result < ( ) , Box < dyn StdError > > {
301
330
// Unquote and merge C string constants
302
331
let unquote_re = regex:: Regex :: new ( r#""(.*?[^\\])"\s*"# ) . unwrap ( ) ;
@@ -311,7 +340,7 @@ pub fn print_cargo_metadata<T: AsRef<Path>>(
311
340
let mut ngx_features: Vec < String > = vec ! [ ] ;
312
341
let mut ngx_os = String :: new ( ) ;
313
342
314
- let expanded = expand_definitions ( includes) ?;
343
+ let expanded = expand_definitions ( includes, defines ) ?;
315
344
for line in String :: from_utf8 ( expanded) ?. lines ( ) {
316
345
let Some ( ( name, value) ) = line
317
346
. trim ( )
@@ -350,6 +379,19 @@ pub fn print_cargo_metadata<T: AsRef<Path>>(
350
379
. expect( "Unicode include paths" )
351
380
) ;
352
381
382
+ println ! (
383
+ "cargo:metadata=cflags={}" ,
384
+ defines
385
+ . iter( )
386
+ . map( |( n, ov) | if let Some ( v) = ov {
387
+ format!( "-D{n}={v}" )
388
+ } else {
389
+ format!( "-D{n}" )
390
+ } )
391
+ . collect:: <Vec <_>>( )
392
+ . join( " " )
393
+ ) ;
394
+
353
395
// A quoted list of all recognized features to be passed to rustc-check-cfg.
354
396
let values = NGX_CONF_FEATURES . join ( "\" ,\" " ) ;
355
397
println ! ( "cargo::metadata=features_check=\" {values}\" " ) ;
@@ -372,7 +414,10 @@ pub fn print_cargo_metadata<T: AsRef<Path>>(
372
414
Ok ( ( ) )
373
415
}
374
416
375
- fn expand_definitions < T : AsRef < Path > > ( includes : & [ T ] ) -> Result < Vec < u8 > , Box < dyn StdError > > {
417
+ fn expand_definitions < T : AsRef < Path > > (
418
+ includes : & [ T ] ,
419
+ defines : & [ ( String , Option < String > ) ] ,
420
+ ) -> Result < Vec < u8 > , Box < dyn StdError > > {
376
421
let path = PathBuf :: from ( env:: var ( "OUT_DIR" ) ?) . join ( "expand.c" ) ;
377
422
let mut writer = std:: io:: BufWriter :: new ( File :: create ( & path) ?) ;
378
423
@@ -418,8 +463,13 @@ RUST_CONF_{flag}=NGX_{flag}
418
463
writer. flush ( ) ?;
419
464
drop ( writer) ;
420
465
421
- Ok ( cc:: Build :: new ( )
422
- . includes ( includes)
423
- . file ( path)
424
- . try_expand ( ) ?)
466
+ let mut builder = cc:: Build :: new ( ) ;
467
+
468
+ builder. includes ( includes) . file ( path) ;
469
+
470
+ for def in defines {
471
+ builder. define ( & def. 0 , def. 1 . as_deref ( ) ) ;
472
+ }
473
+
474
+ Ok ( builder. try_expand ( ) ?)
425
475
}
0 commit comments