@@ -1292,10 +1292,7 @@ public final class LdLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @unchec
12921292 switch specifier. kind {
12931293 case . static, . dynamic, . textBased, . framework:
12941294 if specifier. useSearchPaths {
1295- let args = specifier. searchPathFlagsForLd ( )
1296- if !args. isEmpty { // no search args, fallback to absolute path to library
1297- return ( args, [ ] )
1298- }
1295+ return ( specifier. searchPathFlagsForLd ( ) , [ ] )
12991296 }
13001297 return ( specifier. absolutePathFlagsForLd ( ) , [ specifier. path] )
13011298 case . object:
@@ -1533,15 +1530,18 @@ public final class LdLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @unchec
15331530
15341531/// Extensions to `LinkerSpec.LibrarySpecifier` specific to the dynamic linker.
15351532fileprivate extension LinkerSpec . LibrarySpecifier {
1533+
15361534 func searchPathFlagsForLd( ) -> [ String ] {
1535+ precondition ( useSearchPaths)
1536+ // Extract basename once to avoid redundant operations
1537+ let basename = path. basename
1538+ let basenameWithoutSuffix = Path ( basename) . withoutSuffix
1539+ // Strip the prefix if one exists and is present in the basename
15371540 let strippedName : String
1538- if let prefix = libPrefix, path . basename. hasPrefix ( prefix) {
1539- strippedName = Path ( path . basename ) . withoutSuffix . withoutPrefix ( prefix)
1541+ if let prefix = libPrefix, basename. hasPrefix ( prefix) {
1542+ strippedName = basenameWithoutSuffix . withoutPrefix ( prefix)
15401543 } else {
1541- if libPrefix != nil { // we need a prefix for linking with search paths
1542- return [ ] // this will fallback to using absolute paths
1543- }
1544- strippedName = Path ( path. basename) . withoutSuffix
1544+ strippedName = basenameWithoutSuffix
15451545 }
15461546 switch ( kind, mode) {
15471547 case ( . dynamic, . normal) :
0 commit comments