@@ -21,7 +21,7 @@ use build_helper::{output, t};
21
21
use crate :: builder:: { Builder , RunConfig , ShouldRun , Step } ;
22
22
use crate :: config:: TargetSelection ;
23
23
use crate :: util:: { self , exe} ;
24
- use crate :: GitRepo ;
24
+ use crate :: { CLang , GitRepo } ;
25
25
use build_helper:: up_to_date;
26
26
27
27
pub struct Meta {
@@ -262,18 +262,6 @@ impl Step for Llvm {
262
262
cfg. define ( "LLVM_LINK_LLVM_DYLIB" , "ON" ) ;
263
263
}
264
264
265
- // For distribution we want the LLVM tools to be *statically* linked to libstdc++.
266
- // We also do this if the user explicitly requested static libstdc++.
267
- if builder. config . llvm_tools_enabled || builder. config . llvm_static_stdcpp {
268
- if !target. contains ( "msvc" ) && !target. contains ( "netbsd" ) {
269
- if target. contains ( "apple" ) {
270
- ldflags. push_all ( "-static-libstdc++" ) ;
271
- } else {
272
- ldflags. push_all ( "-Wl,-Bsymbolic -static-libstdc++" ) ;
273
- }
274
- }
275
- }
276
-
277
265
if target. starts_with ( "riscv" ) && !target. contains ( "freebsd" ) {
278
266
// RISC-V GCC erroneously requires linking against
279
267
// `libatomic` when using 1-byte and 2-byte C++
@@ -529,7 +517,7 @@ fn configure_cmake(
529
517
}
530
518
531
519
cfg. build_arg ( "-j" ) . build_arg ( builder. jobs ( ) . to_string ( ) ) ;
532
- let mut cflags: OsString = builder. cflags ( target, GitRepo :: Llvm ) . join ( " " ) . into ( ) ;
520
+ let mut cflags: OsString = builder. cflags ( target, GitRepo :: Llvm , CLang :: C ) . join ( " " ) . into ( ) ;
533
521
if let Some ( ref s) = builder. config . llvm_cflags {
534
522
cflags. push ( " " ) ;
535
523
cflags. push ( s) ;
@@ -545,23 +533,15 @@ fn configure_cmake(
545
533
if builder. config . llvm_clang_cl . is_some ( ) {
546
534
cflags. push ( & format ! ( " --target={}" , target) ) ;
547
535
}
548
- if let Some ( flags) = env:: var_os ( "CFLAGS" ) {
549
- cflags. push ( " " ) ;
550
- cflags. push ( flags) ;
551
- }
552
536
cfg. define ( "CMAKE_C_FLAGS" , cflags) ;
553
- let mut cxxflags: OsString = builder. cflags ( target, GitRepo :: Llvm ) . join ( " " ) . into ( ) ;
537
+ let mut cxxflags: OsString = builder. cflags ( target, GitRepo :: Llvm , CLang :: Cxx ) . join ( " " ) . into ( ) ;
554
538
if let Some ( ref s) = builder. config . llvm_cxxflags {
555
539
cxxflags. push ( " " ) ;
556
540
cxxflags. push ( s) ;
557
541
}
558
542
if builder. config . llvm_clang_cl . is_some ( ) {
559
543
cxxflags. push ( & format ! ( " --target={}" , target) ) ;
560
544
}
561
- if let Some ( flags) = env:: var_os ( "CXXFLAGS" ) {
562
- cxxflags. push ( " " ) ;
563
- cxxflags. push ( flags) ;
564
- }
565
545
cfg. define ( "CMAKE_CXX_FLAGS" , cxxflags) ;
566
546
if let Some ( ar) = builder. ar ( target) {
567
547
if ar. is_absolute ( ) {
@@ -583,10 +563,22 @@ fn configure_cmake(
583
563
ldflags. push_all ( flags) ;
584
564
}
585
565
586
- if let Some ( flags) = env :: var_os ( "LDFLAGS" ) {
566
+ if let Some ( flags) = get_var ( "LDFLAGS" , & builder . config . build . triple , & target . triple ) {
587
567
ldflags. push_all ( & flags) ;
588
568
}
589
569
570
+ // For distribution we want the LLVM tools to be *statically* linked to libstdc++.
571
+ // We also do this if the user explicitly requested static libstdc++.
572
+ if builder. config . llvm_tools_enabled || builder. config . llvm_static_stdcpp {
573
+ if !target. contains ( "msvc" ) && !target. contains ( "netbsd" ) {
574
+ if target. contains ( "apple" ) {
575
+ ldflags. push_all ( "-static-libstdc++" ) ;
576
+ } else {
577
+ ldflags. push_all ( "-Wl,-Bsymbolic -static-libstdc++" ) ;
578
+ }
579
+ }
580
+ }
581
+
590
582
cfg. define ( "CMAKE_SHARED_LINKER_FLAGS" , & ldflags. shared ) ;
591
583
cfg. define ( "CMAKE_MODULE_LINKER_FLAGS" , & ldflags. module ) ;
592
584
cfg. define ( "CMAKE_EXE_LINKER_FLAGS" , & ldflags. exe ) ;
@@ -596,6 +588,16 @@ fn configure_cmake(
596
588
}
597
589
}
598
590
591
+ // Adapted from https://github.com/alexcrichton/cc-rs/blob/fba7feded71ee4f63cfe885673ead6d7b4f2f454/src/lib.rs#L2347-L2365
592
+ fn get_var ( var_base : & str , host : & str , target : & str ) -> Option < OsString > {
593
+ let kind = if host == target { "HOST" } else { "TARGET" } ;
594
+ let target_u = target. replace ( "-" , "_" ) ;
595
+ env:: var_os ( & format ! ( "{}_{}" , var_base, target) )
596
+ . or_else ( || env:: var_os ( & format ! ( "{}_{}" , var_base, target_u) ) )
597
+ . or_else ( || env:: var_os ( & format ! ( "{}_{}" , kind, var_base) ) )
598
+ . or_else ( || env:: var_os ( var_base) )
599
+ }
600
+
599
601
#[ derive( Debug , Copy , Clone , Hash , PartialEq , Eq ) ]
600
602
pub struct Lld {
601
603
pub target : TargetSelection ,
0 commit comments