Skip to content

Commit da3eeac

Browse files
committed
no message
1 parent cdcfe04 commit da3eeac

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Sources/SWBCore/SpecImplementations/LinkerSpec.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ open class LinkerSpec : CommandLineToolSpec, @unchecked Sendable {
9595
self.kind = kind
9696
self.path = path
9797
self.mode = mode
98-
self.useSearchPaths = useSearchPaths
9998
self.swiftModulePaths = swiftModulePaths
10099
self.swiftModuleAdditionalLinkerArgResponseFilePaths = swiftModuleAdditionalLinkerArgResponseFilePaths
101100
self.explicitDependencies = explicitDependencies
@@ -104,6 +103,9 @@ open class LinkerSpec : CommandLineToolSpec, @unchecked Sendable {
104103
self.xcframeworkSourcePath = xcframeworkSourcePath
105104
self.privacyFile = privacyFile
106105
self.libPrefix = prefixes.first
106+
// Only use search paths when no prefix is required or when the prefix matches
107+
let hasValidPrefix = libPrefix.map { path.basename.hasPrefix($0) } ?? true
108+
self.useSearchPaths = hasValidPrefix && useSearchPaths
107109
}
108110
}
109111

Sources/SWBCore/SpecImplementations/Tools/LinkerTools.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
15351532
fileprivate 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

Comments
 (0)