@@ -456,10 +456,15 @@ mod c_vendor {
456
456
457
457
#[ cfg( feature = "c-system" ) ]
458
458
mod c_system {
459
+ extern crate ar;
460
+
461
+ use std:: collections:: HashMap ;
459
462
use std:: env;
463
+ use std:: fs:: File ;
460
464
use std:: process:: { Command , Output } ;
461
465
use std:: str;
462
- use std:: path:: Path ;
466
+ use std:: path:: { Path , PathBuf } ;
467
+
463
468
use sources;
464
469
465
470
fn success_output ( err : & str , cmd : & mut Command ) -> Output {
@@ -499,57 +504,99 @@ mod c_system {
499
504
r. to_string ( )
500
505
}
501
506
507
+ fn find_library < I > ( dirs : I , libname : & str ) -> Result < PathBuf , Vec < String > >
508
+ where
509
+ I : Iterator < Item = PathBuf >
510
+ {
511
+ let mut paths = Vec :: new ( ) ;
512
+ for dir in dirs {
513
+ let try_path = dir. join ( format ! ( "lib{}.a" , libname) ) ;
514
+ if try_path. exists ( ) {
515
+ return Ok ( try_path. to_path_buf ( ) ) ;
516
+ } else {
517
+ paths. push ( format ! ( "{:?}" , try_path) )
518
+ }
519
+ }
520
+ Err ( paths)
521
+ }
522
+
502
523
/// Link against system clang runtime libraries
503
524
pub fn compile ( llvm_target : & [ & str ] ) {
504
525
let target = env:: var ( "TARGET" ) . unwrap ( ) ;
505
526
let target_os = env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) ;
506
527
let compiler_rt_arch = get_arch_name_for_compiler_rtlib ( ) ;
528
+ let out_dir = env:: var ( "OUT_DIR" ) . unwrap ( ) ;
507
529
508
530
if ALL_SUPPORTED_ARCHES . split ( ";" ) . find ( |x| * x == compiler_rt_arch) == None {
509
531
return ;
510
532
}
511
533
512
- if let Ok ( clang) = env:: var ( "CLANG" ) {
534
+ println ! ( "cargo:rerun-if-env-changed=CLANG" ) ;
535
+ println ! ( "cargo:rerun-if-env-changed=LLVM_CONFIG" ) ;
536
+
537
+ let fullpath = if let Ok ( clang) = env:: var ( "CLANG" ) {
513
538
let output = success_output (
514
539
"failed to find clang's compiler-rt" ,
515
540
Command :: new ( clang)
516
541
. arg ( format ! ( "--target={}" , target) )
517
542
. arg ( "--rtlib=compiler-rt" )
518
543
. arg ( "--print-libgcc-file-name" ) ,
519
544
) ;
520
- let fullpath = Path :: new ( str:: from_utf8 ( & output. stdout ) . unwrap ( ) ) ;
521
- let libpath = fullpath. parent ( ) . unwrap ( ) . display ( ) ;
522
- let libname = fullpath
523
- . file_stem ( )
524
- . unwrap ( )
525
- . to_str ( )
526
- . unwrap ( )
527
- . trim_start_matches ( "lib" ) ;
528
- println ! ( "cargo:rustc-link-search=native={}" , libpath) ;
529
- println ! ( "cargo:rustc-link-lib=static={}" , libname) ;
545
+ let path = str:: from_utf8 ( & output. stdout ) . unwrap ( ) . trim_end ( ) ;
546
+ Path :: new ( path) . to_path_buf ( )
530
547
} else if let Ok ( llvm_config) = env:: var ( "LLVM_CONFIG" ) {
531
548
// fallback if clang is not installed
532
549
let ( subpath, libname) = match target_os. as_str ( ) {
533
550
"linux" => ( "linux" , format ! ( "clang_rt.builtins-{}" , & compiler_rt_arch) ) ,
534
551
"macos" => ( "darwin" , "clang_rt.builtins_osx_dynamic" . to_string ( ) ) ,
535
552
_ => panic ! ( "unsupported target os: {}" , target_os) ,
536
553
} ;
537
- let cmd = format ! ( "ls -1d $({} --libdir)/clang/*/lib/{}" , llvm_config, subpath) ;
538
554
let output = success_output (
539
- "failed to find clang 's lib dir" ,
540
- Command :: new ( "sh" ) . args ( & [ "-ec" , & cmd ] ) ,
555
+ "failed to find llvm-config 's lib dir" ,
556
+ Command :: new ( llvm_config ) . arg ( "--libdir" ) ,
541
557
) ;
542
- for search_dir in str:: from_utf8 ( & output. stdout ) . unwrap ( ) . lines ( ) {
543
- println ! ( "cargo:rustc-link-search=native={}" , search_dir) ;
558
+ let libdir = str:: from_utf8 ( & output. stdout ) . unwrap ( ) . trim_end ( ) ;
559
+ let paths = std:: fs:: read_dir ( Path :: new ( libdir) . join ( "clang" ) ) . unwrap ( ) . map ( |e| {
560
+ e. unwrap ( ) . path ( ) . join ( "lib" ) . join ( subpath)
561
+ } ) ;
562
+ match find_library ( paths, & libname) {
563
+ Ok ( p) => p,
564
+ Err ( paths) => panic ! ( "failed to find llvm-config's compiler-rt: {}" , paths. join( ":" ) ) ,
544
565
}
545
- println ! ( "cargo:rustc-link-lib=static={}" , libname) ;
546
566
} else {
547
567
panic ! ( "neither CLANG nor LLVM_CONFIG could be read" ) ;
568
+ } ;
569
+
570
+ let mut index = 0 ;
571
+ let mut files = HashMap :: new ( ) ;
572
+ let mut orig = ar:: Archive :: new ( File :: open ( & fullpath) . unwrap ( ) ) ;
573
+ while let Some ( entry_result) = orig. next_entry ( ) {
574
+ let entry = entry_result. unwrap ( ) ;
575
+ let name = str:: from_utf8 ( entry. header ( ) . identifier ( ) ) . unwrap ( ) ;
576
+ files. insert ( name. to_owned ( ) , index) ;
577
+ index += 1 ;
548
578
}
549
579
550
580
let sources = sources:: get_sources ( llvm_target) ;
581
+ let mut new = ar:: Builder :: new ( File :: create ( Path :: new ( & out_dir) . join ( "libcompiler-rt.a" ) ) . unwrap ( ) ) ;
551
582
for ( sym, _src) in sources. map . iter ( ) {
583
+ let & i = {
584
+ let sym_ = if sym. starts_with ( "__" ) { & sym[ 2 ..] } else { & sym } ;
585
+ match files. get ( & format ! ( "{}.c.o" , sym_) ) {
586
+ Some ( i) => i,
587
+ None => match files. get ( & format ! ( "{}.S.o" , sym_) ) {
588
+ Some ( i) => i,
589
+ None => panic ! ( "could not find expected symbol {} in {:?}" , sym, & fullpath) ,
590
+ } ,
591
+ }
592
+ } ;
593
+ let mut entry = orig. jump_to_entry ( i) . unwrap ( ) ;
594
+ // TODO: ar really should have an append_entry to avoid the clone
595
+ new. append ( & entry. header ( ) . clone ( ) , & mut entry) . unwrap ( ) ;
552
596
println ! ( "cargo:rustc-cfg={}=\" optimized-c\" " , sym) ;
553
597
}
598
+
599
+ println ! ( "cargo:rustc-link-search=native={}" , out_dir) ;
600
+ println ! ( "cargo:rustc-link-lib=static={}" , "compiler-rt" ) ;
554
601
}
555
602
}
0 commit comments