@@ -95,6 +95,7 @@ pub struct Config {
9595pub struct Library {
9696 pub libs : Vec < String > ,
9797 pub link_paths : Vec < PathBuf > ,
98+ pub link_files : Vec < PathBuf > ,
9899 pub frameworks : Vec < String > ,
99100 pub framework_paths : Vec < PathBuf > ,
100101 pub include_paths : Vec < PathBuf > ,
@@ -558,6 +559,7 @@ impl Library {
558559 Library {
559560 libs : Vec :: new ( ) ,
560561 link_paths : Vec :: new ( ) ,
562+ link_files : Vec :: new ( ) ,
561563 include_paths : Vec :: new ( ) ,
562564 ld_args : Vec :: new ( ) ,
563565 frameworks : Vec :: new ( ) ,
@@ -568,9 +570,67 @@ impl Library {
568570 }
569571 }
570572
573+ /// Extract the &str to pass to cargo:rustc-link-lib from a filename (just the file name, not including directories)
574+ /// using target-specific logic.
575+ fn extract_lib_from_filename < ' a > ( target : & str , filename : & ' a str ) -> Option < & ' a str > {
576+ fn test_suffixes < ' b > ( filename : & ' b str , suffixes : & [ & str ] ) -> Option < & ' b str > {
577+ for suffix in suffixes {
578+ if filename. ends_with ( suffix) {
579+ return Some ( & filename[ ..filename. len ( ) - suffix. len ( ) ] ) ;
580+ }
581+ }
582+ None
583+ }
584+
585+ let prefix = "lib" ;
586+ if target. contains ( "msvc" ) {
587+ // According to link.exe documentation:
588+ // https://learn.microsoft.com/en-us/cpp/build/reference/link-input-files?view=msvc-170
589+ //
590+ // LINK doesn't use file extensions to make assumptions about the contents of a file.
591+ // Instead, LINK examines each input file to determine what kind of file it is.
592+ //
593+ // However, rustc appends `.lib` to the string it receives from the -l command line argument,
594+ // which it receives from Cargo via cargo:rustc-link-lib:
595+ // https://github.com/rust-lang/rust/blob/657f246812ab2684e3c3954b1c77f98fd59e0b21/compiler/rustc_codegen_ssa/src/back/linker.rs#L828
596+ // https://github.com/rust-lang/rust/blob/657f246812ab2684e3c3954b1c77f98fd59e0b21/compiler/rustc_codegen_ssa/src/back/linker.rs#L843
597+ // So the only file extension that works for MSVC targets is `.lib`
598+ return test_suffixes ( filename, & [ ".lib" ] ) ;
599+ } else if target. contains ( "windows" ) && target. contains ( "gnu" ) {
600+ // GNU targets for Windows, including gnullvm, use `LinkerFlavor::Gcc` internally in rustc,
601+ // which tells rustc to use the GNU linker. rustc does not prepend/append to the string it
602+ // receives via the -l command line argument before passing it to the linker:
603+ // https://github.com/rust-lang/rust/blob/657f246812ab2684e3c3954b1c77f98fd59e0b21/compiler/rustc_codegen_ssa/src/back/linker.rs#L446
604+ // https://github.com/rust-lang/rust/blob/657f246812ab2684e3c3954b1c77f98fd59e0b21/compiler/rustc_codegen_ssa/src/back/linker.rs#L457
605+ // GNU ld can work with more types of files than just the .lib files that MSVC's link.exe needs.
606+ // GNU ld will prepend the `lib` prefix to the filename if necessary, so it is okay to remove
607+ // the `lib` prefix from the filename. The `.a` suffix *requires* the `lib` prefix.
608+ // https://sourceware.org/binutils/docs-2.39/ld.html#index-direct-linking-to-a-dll
609+ if filename. starts_with ( prefix) {
610+ let filename = & filename[ prefix. len ( ) ..] ;
611+ return test_suffixes ( filename, & [ ".dll.a" , ".dll" , ".lib" , ".a" ] ) ;
612+ } else {
613+ return test_suffixes ( filename, & [ ".dll.a" , ".dll" , ".lib" ] ) ;
614+ }
615+ } else if target. contains ( "apple" ) {
616+ if filename. starts_with ( prefix) {
617+ let filename = & filename[ prefix. len ( ) ..] ;
618+ return test_suffixes ( filename, & [ ".a" , ".dylib" ] ) ;
619+ }
620+ return None ;
621+ } else {
622+ if filename. starts_with ( prefix) {
623+ let filename = & filename[ prefix. len ( ) ..] ;
624+ return test_suffixes ( filename, & [ ".a" , ".so" ] ) ;
625+ }
626+ return None ;
627+ }
628+ }
629+
571630 fn parse_libs_cflags ( & mut self , name : & str , output : & [ u8 ] , config : & Config ) {
572631 let mut is_msvc = false ;
573- if let Ok ( target) = env:: var ( "TARGET" ) {
632+ let target = env:: var ( "TARGET" ) ;
633+ if let Ok ( target) = & target {
574634 if target. contains ( "msvc" ) {
575635 is_msvc = true ;
576636 }
@@ -670,7 +730,29 @@ impl Library {
670730 self . include_paths . push ( PathBuf :: from ( inc) ) ;
671731 }
672732 }
673- _ => ( ) ,
733+ _ => {
734+ let path = std:: path:: Path :: new ( part) ;
735+ if path. is_file ( ) {
736+ // Cargo doesn't have a means to directly specify a file path to link,
737+ // so split up the path into the parent directory and library name.
738+ // TODO: pass file path directly when link-arg library type is stabilized
739+ // https://github.com/rust-lang/rust/issues/99427
740+ if let Some ( dir) = path. parent ( ) {
741+ let link_search = format ! ( "rustc-link-search={}" , dir. display( ) ) ;
742+ config. print_metadata ( & link_search) ;
743+ }
744+ if let ( Some ( file_name) , Ok ( target) ) = ( path. file_name ( ) , & target) {
745+ if let Some ( lib_basename) = Self :: extract_lib_from_filename (
746+ target,
747+ & file_name. to_string_lossy ( ) ,
748+ ) {
749+ let link_lib = format ! ( "rustc-link-lib={}" , lib_basename) ;
750+ config. print_metadata ( & link_lib) ;
751+ self . link_files . push ( PathBuf :: from ( file_name) ) ;
752+ }
753+ }
754+ }
755+ }
674756 }
675757 }
676758
@@ -776,60 +858,102 @@ fn split_flags(output: &[u8]) -> Vec<String> {
776858 words
777859}
778860
779- #[ test]
780- #[ cfg( target_os = "macos" ) ]
781- fn system_library_mac_test ( ) {
782- use std:: path:: Path ;
783-
784- let system_roots = vec ! [ PathBuf :: from( "/Library" ) , PathBuf :: from( "/System" ) ] ;
785-
786- assert ! ( !is_static_available(
787- "PluginManager" ,
788- & system_roots,
789- & [ PathBuf :: from( "/Library/Frameworks" ) ]
790- ) ) ;
791- assert ! ( !is_static_available(
792- "python2.7" ,
793- & system_roots,
794- & [ PathBuf :: from(
795- "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config"
796- ) ]
797- ) ) ;
798- assert ! ( !is_static_available(
799- "ffi_convenience" ,
800- & system_roots,
801- & [ PathBuf :: from(
802- "/Library/Ruby/Gems/2.0.0/gems/ffi-1.9.10/ext/ffi_c/libffi-x86_64/.libs"
803- ) ]
804- ) ) ;
805-
806- // Homebrew is in /usr/local, and it's not a part of the OS
807- if Path :: new ( "/usr/local/lib/libpng16.a" ) . exists ( ) {
808- assert ! ( is_static_available(
809- "png16" ,
861+ #[ cfg( test) ]
862+ mod tests {
863+ use super :: * ;
864+
865+ #[ test]
866+ #[ cfg( target_os = "macos" ) ]
867+ fn system_library_mac_test ( ) {
868+ use std:: path:: Path ;
869+
870+ let system_roots = vec ! [ PathBuf :: from( "/Library" ) , PathBuf :: from( "/System" ) ] ;
871+
872+ assert ! ( !is_static_available(
873+ "PluginManager" ,
874+ & system_roots,
875+ & [ PathBuf :: from( "/Library/Frameworks" ) ]
876+ ) ) ;
877+ assert ! ( !is_static_available(
878+ "python2.7" ,
879+ & system_roots,
880+ & [ PathBuf :: from(
881+ "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config"
882+ ) ]
883+ ) ) ;
884+ assert ! ( !is_static_available(
885+ "ffi_convenience" ,
810886 & system_roots,
811- & [ PathBuf :: from( "/usr/local/lib" ) ]
887+ & [ PathBuf :: from(
888+ "/Library/Ruby/Gems/2.0.0/gems/ffi-1.9.10/ext/ffi_c/libffi-x86_64/.libs"
889+ ) ]
812890 ) ) ;
813891
814- let libpng = Config :: new ( )
815- . range_version ( "1" .."99" )
816- . probe ( "libpng16" )
817- . unwrap ( ) ;
818- assert ! ( libpng. version. find( '\n' ) . is_none( ) ) ;
892+ // Homebrew is in /usr/local, and it's not a part of the OS
893+ if Path :: new ( "/usr/local/lib/libpng16.a" ) . exists ( ) {
894+ assert ! ( is_static_available(
895+ "png16" ,
896+ & system_roots,
897+ & [ PathBuf :: from( "/usr/local/lib" ) ]
898+ ) ) ;
899+
900+ let libpng = Config :: new ( )
901+ . range_version ( "1" .."99" )
902+ . probe ( "libpng16" )
903+ . unwrap ( ) ;
904+ assert ! ( libpng. version. find( '\n' ) . is_none( ) ) ;
905+ }
906+ }
907+
908+ #[ test]
909+ #[ cfg( target_os = "linux" ) ]
910+ fn system_library_linux_test ( ) {
911+ assert ! ( !is_static_available(
912+ "util" ,
913+ & [ PathBuf :: from( "/usr" ) ] ,
914+ & [ PathBuf :: from( "/usr/lib/x86_64-linux-gnu" ) ]
915+ ) ) ;
916+ assert ! ( !is_static_available(
917+ "dialog" ,
918+ & [ PathBuf :: from( "/usr" ) ] ,
919+ & [ PathBuf :: from( "/usr/lib" ) ]
920+ ) ) ;
819921 }
820- }
821922
822- #[ test]
823- #[ cfg( target_os = "linux" ) ]
824- fn system_library_linux_test ( ) {
825- assert ! ( !is_static_available(
826- "util" ,
827- & [ PathBuf :: from( "/usr" ) ] ,
828- & [ PathBuf :: from( "/usr/lib/x86_64-linux-gnu" ) ]
829- ) ) ;
830- assert ! ( !is_static_available(
831- "dialog" ,
832- & [ PathBuf :: from( "/usr" ) ] ,
833- & [ PathBuf :: from( "/usr/lib" ) ]
834- ) ) ;
923+ fn test_library_filename ( target : & str , filename : & str ) {
924+ assert_eq ! (
925+ Library :: extract_lib_from_filename( target, filename) ,
926+ Some ( "foo" )
927+ ) ;
928+ }
929+
930+ #[ test]
931+ fn link_filename_linux ( ) {
932+ let target = "x86_64-unknown-linux-gnu" ;
933+ test_library_filename ( target, "libfoo.a" ) ;
934+ test_library_filename ( target, "libfoo.so" ) ;
935+ }
936+
937+ #[ test]
938+ fn link_filename_apple ( ) {
939+ let target = "x86_64-apple-darwin" ;
940+ test_library_filename ( target, "libfoo.a" ) ;
941+ test_library_filename ( target, "libfoo.dylib" ) ;
942+ }
943+
944+ #[ test]
945+ fn link_filename_msvc ( ) {
946+ let target = "x86_64-pc-windows-msvc" ;
947+ // static and dynamic libraries have the same .lib suffix
948+ test_library_filename ( target, "foo.lib" ) ;
949+ }
950+
951+ #[ test]
952+ fn link_filename_mingw ( ) {
953+ let target = "x86_64-pc-windows-gnu" ;
954+ test_library_filename ( target, "foo.lib" ) ;
955+ test_library_filename ( target, "libfoo.a" ) ;
956+ test_library_filename ( target, "foo.dll" ) ;
957+ test_library_filename ( target, "foo.dll.a" ) ;
958+ }
835959}
0 commit comments