6
6
//!
7
7
//! Reference: <https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script>
8
8
9
- use crate :: {
10
- allow_use ,
11
- ident :: { is_ascii_ident , is_ident } ,
12
- } ;
13
- use std :: { ffi :: OsStr , fmt :: Display , fmt :: Write , path :: Path , str } ;
9
+ use std :: ffi :: OsStr ;
10
+ use std :: path :: Path ;
11
+ use std :: { fmt :: Display , fmt :: Write as _ } ;
12
+
13
+ use crate :: ident :: { is_ascii_ident , is_ident } ;
14
14
15
15
fn emit ( directive : & str , value : impl Display ) {
16
- if allow_use:: double_colon_directives ( ) {
17
- println ! ( "cargo::{}={}" , directive, value) ;
18
- } else {
19
- println ! ( "cargo:{}={}" , directive, value) ;
20
- }
16
+ println ! ( "cargo::{}={}" , directive, value) ;
21
17
}
22
18
23
19
/// The `rerun-if-changed` instruction tells Cargo to re-run the build script if the
@@ -171,7 +167,7 @@ pub fn rustc_link_arg_benches(flag: &str) {
171
167
/// to the symbols from the given lib, and the binary should access them through
172
168
/// the library target’s public API.
173
169
///
174
- /// The optional `KIND` may be one of dylib, static, or framework. See the
170
+ /// The optional `KIND` may be one of ` dylib`, ` static` , or ` framework` . See the
175
171
/// [rustc book][-l] for more detail.
176
172
///
177
173
/// [-l]: https://doc.rust-lang.org/stable/rustc/command-line-arguments.html#option-l-link-lib
@@ -301,7 +297,7 @@ pub fn rustc_cfg_value(key: &str, value: &str) {
301
297
/// and other mistakes.
302
298
///
303
299
/// [`unexpected_cfgs`]: https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#unexpected-cfgs
304
- #[ doc = msrv ! ( "1.80" ) ]
300
+ #[ doc = respected_msrv ! ( "1.80" ) ]
305
301
#[ track_caller]
306
302
pub fn rustc_check_cfgs ( keys : & [ & str ] ) {
307
303
if keys. is_empty ( ) {
@@ -313,13 +309,11 @@ pub fn rustc_check_cfgs(keys: &[&str]) {
313
309
}
314
310
}
315
311
316
- if allow_use:: check_cfg ( ) {
317
- let mut directive = keys[ 0 ] . to_string ( ) ;
318
- for key in & keys[ 1 ..] {
319
- write ! ( directive, ", {key}" ) . expect ( "writing to string should be infallible" ) ;
320
- }
321
- emit ( "rustc-check-cfg" , format_args ! ( "cfg({directive})" ) ) ;
312
+ let mut directive = keys[ 0 ] . to_string ( ) ;
313
+ for key in & keys[ 1 ..] {
314
+ write ! ( directive, ", {key}" ) . expect ( "writing to string should be infallible" ) ;
322
315
}
316
+ emit ( "rustc-check-cfg" , format_args ! ( "cfg({directive})" ) ) ;
323
317
}
324
318
325
319
/// Add to the list of expected config names that is used when checking the
@@ -332,7 +326,7 @@ pub fn rustc_check_cfgs(keys: &[&str]) {
332
326
/// and other mistakes.
333
327
///
334
328
/// [`unexpected_cfgs`]: https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#unexpected-cfgs
335
- #[ doc = msrv ! ( "1.80" ) ]
329
+ #[ doc = respected_msrv ! ( "1.80" ) ]
336
330
#[ track_caller]
337
331
pub fn rustc_check_cfg_values ( key : & str , values : & [ & str ] ) {
338
332
if !is_ident ( key) {
@@ -343,17 +337,15 @@ pub fn rustc_check_cfg_values(key: &str, values: &[&str]) {
343
337
return ;
344
338
}
345
339
346
- if allow_use:: check_cfg ( ) {
347
- let mut directive = format ! ( "\" {}\" " , values[ 0 ] . escape_default( ) ) ;
348
- for value in & values[ 1 ..] {
349
- write ! ( directive, ", \" {}\" " , value. escape_default( ) )
350
- . expect ( "writing to string should be infallible" ) ;
351
- }
352
- emit (
353
- "rustc-check-cfg" ,
354
- format_args ! ( "cfg({key}, values({directive}))" ) ,
355
- ) ;
356
- }
340
+ let mut directive = format ! ( "\" {}\" " , values[ 0 ] . escape_default( ) ) ;
341
+ for value in & values[ 1 ..] {
342
+ write ! ( directive, ", \" {}\" " , value. escape_default( ) )
343
+ . expect ( "writing to string should be infallible" ) ;
344
+ }
345
+ emit (
346
+ "rustc-check-cfg" ,
347
+ format_args ! ( "cfg({key}, values({directive}))" ) ,
348
+ ) ;
357
349
}
358
350
359
351
/// The `rustc-env` instruction tells Cargo to set the given environment variable
@@ -411,6 +403,26 @@ pub fn warning(message: &str) {
411
403
emit ( "warning" , message) ;
412
404
}
413
405
406
+ /// The `error` instruction tells Cargo to display an error after the build script has finished
407
+ /// running, and then fail the build.
408
+ ///
409
+ /// <div class="warning">
410
+ ///
411
+ /// Build script libraries should carefully consider if they want to use [`error`] versus
412
+ /// returning a `Result`. It may be better to return a `Result`, and allow the caller to decide if the
413
+ /// error is fatal or not. The caller can then decide whether or not to display the `Err` variant
414
+ /// using [`error`].
415
+ ///
416
+ /// </div>
417
+ #[ doc = respected_msrv ! ( "1.84" ) ]
418
+ #[ track_caller]
419
+ pub fn error ( message : & str ) {
420
+ if message. contains ( '\n' ) {
421
+ panic ! ( "cannot emit warning: message contains newline" ) ;
422
+ }
423
+ emit ( "error" , message) ;
424
+ }
425
+
414
426
/// Metadata, used by `links` scripts.
415
427
#[ track_caller]
416
428
pub fn metadata ( key : & str , val : & str ) {
@@ -421,9 +433,5 @@ pub fn metadata(key: &str, val: &str) {
421
433
panic ! ( "cannot emit metadata: invalid value {val:?}" ) ;
422
434
}
423
435
424
- if allow_use:: double_colon_directives ( ) {
425
- emit ( "metadata" , format_args ! ( "{}={}" , key, val) ) ;
426
- } else {
427
- emit ( key, val) ;
428
- }
436
+ emit ( "metadata" , format_args ! ( "{}={}" , key, val) ) ;
429
437
}
0 commit comments