@@ -19,74 +19,59 @@ pub fn main(rustc_path: &OsStr) {
1919 // Binaries and C dynamic libraries are not built as non-primary packages,
2020 // so this should not cause issues with Cargo caches.
2121 if env:: var_os ( "CARGO_PRIMARY_PACKAGE" ) . is_some ( ) {
22- let arg_parsing_result = rustc_arguments:: parse_args ( ) ;
23- if let Ok ( args) = rustc_arguments:: parse_args ( ) {
24- if should_embed_audit_data ( & args) {
25- // Get the audit data to embed
26- let target_triple = args
27- . target
28- . clone ( )
29- . unwrap_or_else ( || rustc_host_target_triple ( rustc_path) ) ;
30- let contents: Vec < u8 > =
31- collect_audit_data:: compressed_dependency_list ( & args, & target_triple) ;
32- // write the audit info to an object file
33- let target_info = target_info:: rustc_target_info ( rustc_path, & target_triple) ;
34- let binfile = binary_file:: create_binary_file (
35- & target_info,
36- & target_triple,
37- & contents,
38- "AUDITABLE_VERSION_INFO" ,
22+ let args = rustc_arguments:: parse_args ( ) . unwrap ( ) ; // descriptive enough message
23+ if should_embed_audit_data ( & args) {
24+ // Get the audit data to embed
25+ let target_triple = args
26+ . target
27+ . clone ( )
28+ . unwrap_or_else ( || rustc_host_target_triple ( rustc_path) ) ;
29+ let contents: Vec < u8 > =
30+ collect_audit_data:: compressed_dependency_list ( & args, & target_triple) ;
31+ // write the audit info to an object file
32+ let target_info = target_info:: rustc_target_info ( rustc_path, & target_triple) ;
33+ let binfile = binary_file:: create_binary_file (
34+ & target_info,
35+ & target_triple,
36+ & contents,
37+ "AUDITABLE_VERSION_INFO" ,
38+ ) ;
39+ if let Some ( file) = binfile {
40+ // Place the audit data in the output dir.
41+ // We can place it anywhere really, the only concern is clutter and name collisions,
42+ // and the target dir is locked so we're probably good
43+ let filename = format ! (
44+ "{}_audit_data.o" ,
45+ args. crate_name
46+ . expect( "rustc command is missing --crate-name" )
3947 ) ;
40- if let Some ( file) = binfile {
41- // Place the audit data in the output dir.
42- // We can place it anywhere really, the only concern is clutter and name collisions,
43- // and the target dir is locked so we're probably good
44- let filename = format ! ( "{}_audit_data.o" , args. crate_name) ;
45- let path = args. out_dir . join ( filename) ;
46- std:: fs:: write ( & path, file) . expect ( "Unable to write output file" ) ;
48+ let path = args
49+ . out_dir
50+ . expect ( "rustc command is missing --out-dir" )
51+ . join ( filename) ;
52+ std:: fs:: write ( & path, file) . expect ( "Unable to write output file" ) ;
4753
48- // Modify the rustc command to link the object file with audit data
49- let mut linker_command = OsString :: from ( "-Clink-arg=" ) ;
50- linker_command. push ( & path) ;
51- command. arg ( linker_command) ;
52- // Prevent the symbol from being removed as unused by the linker
53- if is_apple ( & target_info) {
54- command. arg ( "-Clink-arg=-Wl,-u,_AUDITABLE_VERSION_INFO" ) ;
55- } else if is_msvc ( & target_info) {
56- command. arg ( "-Clink-arg=/INCLUDE:AUDITABLE_VERSION_INFO" ) ;
57- } else if is_wasm ( & target_info) {
58- // We don't emit the symbol name in WASM, so nothing to do
59- } else {
60- command. arg ( "-Clink-arg=-Wl,--undefined=AUDITABLE_VERSION_INFO" ) ;
61- }
54+ // Modify the rustc command to link the object file with audit data
55+ let mut linker_command = OsString :: from ( "-Clink-arg=" ) ;
56+ linker_command. push ( & path) ;
57+ command. arg ( linker_command) ;
58+ // Prevent the symbol from being removed as unused by the linker
59+ if is_apple ( & target_info) {
60+ command. arg ( "-Clink-arg=-Wl,-u,_AUDITABLE_VERSION_INFO" ) ;
61+ } else if is_msvc ( & target_info) {
62+ command. arg ( "-Clink-arg=/INCLUDE:AUDITABLE_VERSION_INFO" ) ;
63+ } else if is_wasm ( & target_info) {
64+ // We don't emit the symbol name in WASM, so nothing to do
6265 } else {
63- // create_binary_file() returned None, indicating an unsupported architecture
64- eprintln ! ( "WARNING: target '{target_triple}' is not supported by 'cargo auditable'!\n \
65- The build will continue, but no audit data will be injected into the binary.") ;
66+ command. arg ( "-Clink-arg=-Wl,--undefined=AUDITABLE_VERSION_INFO" ) ;
6667 }
68+ } else {
69+ // create_binary_file() returned None, indicating an unsupported architecture
70+ eprintln ! (
71+ "WARNING: target '{target_triple}' is not supported by 'cargo auditable'!\n \
72+ The build will continue, but no audit data will be injected into the binary."
73+ ) ;
6774 }
68- } else {
69- // Failed to parse rustc arguments.
70-
71- // This may be due to a `rustc -vV` call, or similar non-compilation command.
72- // This never happens with Cargo - it does call `rustc -vV`,
73- // but either bypasses the wrapper or doesn't set CARGO_PRIMARY_PACKAGE=true.
74- // However it does happen with `sccache`:
75- // https://github.com/rust-secure-code/cargo-auditable/issues/87
76- // This is probably a bug in `sccache`, but it's easier to fix here.
77-
78- // There are many non-compilation flags (and they can be compound),
79- // so parsing them properly adds a lot of complexity.
80- // So we just check if `--crate-name` is passed and if not,
81- // assume that it's a non-compilation command.
82- if env:: args_os ( )
83- . skip ( 2 )
84- . any ( |arg| arg == OsStr :: new ( "--crate-name" ) )
85- {
86- // this was a compilation command, bail
87- arg_parsing_result. unwrap ( ) ;
88- }
89- // for commands like `rustc --version` we just pass on the arguments without changes
9075 }
9176 }
9277
0 commit comments