@@ -16,8 +16,8 @@ mod x86_64_unknown_linux_musl;
1616
1717use crate :: {
1818 cargo_env, effective_target, emit_warning, env_var_to_bool, execute_command, get_crate_cflags,
19- is_no_asm, optional_env_optional_crate_target, out_dir , requested_c_std , set_env_for_target ,
20- target, target_arch, target_env, target_os, CStdRequested , OutputLibType ,
19+ is_no_asm, optional_env_optional_crate_target, optional_env_target , out_dir , requested_c_std ,
20+ set_env_for_target , target, target_arch, target_env, target_os, CStdRequested , OutputLibType ,
2121} ;
2222use std:: cell:: Cell ;
2323use std:: collections:: HashMap ;
@@ -373,7 +373,19 @@ impl CcBuilder {
373373 option. apply_cc ( & mut je_builder) ;
374374 }
375375
376- let compiler = je_builder. get_compiler ( ) ;
376+ let compiler = if let Some ( original_cflags) = optional_env_target ( "CFLAGS" ) {
377+ let mut new_cflags = original_cflags. clone ( ) ;
378+ new_cflags. push_str ( " -O0" ) ;
379+ set_env_for_target ( "CFLAGS" , & new_cflags) ;
380+ // cc-rs currently prioritizes flags provided by CFLAGS over the flags provided by the build script.
381+ // The environment variables used by the compiler are set when `get_compiler` is called.
382+ let compiler = je_builder. get_compiler ( ) ;
383+ set_env_for_target ( "CFLAGS" , & original_cflags) ;
384+ compiler
385+ } else {
386+ je_builder. get_compiler ( )
387+ } ;
388+
377389 je_builder. define ( "AWSLC" , "1" ) ;
378390 je_builder. pic ( true ) ;
379391 if target_os ( ) == "windows" && compiler. is_like_msvc ( ) {
@@ -391,6 +403,7 @@ impl CcBuilder {
391403 . flag ( "-Wextra" )
392404 . flag ( "-Wall" )
393405 . flag ( "-pedantic" )
406+ // Compilation will fail if optimizations are enabled.
394407 . flag ( "-O0" )
395408 . flag ( "-fwrapv" )
396409 . flag ( "-Wconversion" ) ;
0 commit comments