Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/SWBCore/Settings/BuiltinMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ public final class BuiltinMacros {
public static let EXECUTABLE_DEBUG_DYLIB_MAPPED_PLATFORM = BuiltinMacros.declareStringMacro("EXECUTABLE_DEBUG_DYLIB_MAPPED_PLATFORM")
public static let EXECUTABLE_BLANK_INJECTION_DYLIB_PATH = BuiltinMacros.declareStringMacro("EXECUTABLE_BLANK_INJECTION_DYLIB_PATH")

public static let EXECUTABLE_PREFIX = BuiltinMacros.declareStringMacro("EXECUTABLE_PREFIX")
public static let EXECUTABLE_SUFFIX = BuiltinMacros.declareStringMacro("EXECUTABLE_SUFFIX")
public static let EXECUTABLE_VARIANT_SUFFIX = BuiltinMacros.declareStringMacro("EXECUTABLE_VARIANT_SUFFIX")
public static let EXPORTED_SYMBOLS_FILE = BuiltinMacros.declarePathMacro("EXPORTED_SYMBOLS_FILE")
Expand Down Expand Up @@ -1764,6 +1765,7 @@ public final class BuiltinMacros {
EXECUTABLE_DEBUG_DYLIB_MAPPED_INSTALL_NAME,
EXECUTABLE_DEBUG_DYLIB_MAPPED_PLATFORM,
EXECUTABLE_BLANK_INJECTION_DYLIB_PATH,
EXECUTABLE_PREFIX,
EXECUTABLE_SUFFIX,
EXECUTABLE_VARIANT_SUFFIX,
EXCLUDED_ARCHS,
Expand Down
11 changes: 10 additions & 1 deletion Sources/SWBCore/SpecImplementations/LinkerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ open class LinkerSpec : CommandLineToolSpec, @unchecked Sendable {
/// The path to the privacy file, if one exists.
public let privacyFile: Path?

public init(kind: Kind, path: Path, mode: Mode, useSearchPaths: Bool, swiftModulePaths: [String: Path], swiftModuleAdditionalLinkerArgResponseFilePaths: [String: Path], explicitDependencies: [Path] = [], topLevelItemPath: Path? = nil, dsymPath: Path? = nil, xcframeworkSourcePath: Path? = nil, privacyFile: Path? = nil) {
public let libPrefix: String

public init(kind: Kind, path: Path, mode: Mode, useSearchPaths: Bool, swiftModulePaths: [String: Path], swiftModuleAdditionalLinkerArgResponseFilePaths: [String: Path], refSettings: Settings? = nil, explicitDependencies: [Path] = [], topLevelItemPath: Path? = nil, dsymPath: Path? = nil, xcframeworkSourcePath: Path? = nil, privacyFile: Path? = nil) {
self.kind = kind
self.path = path
self.mode = mode
Expand All @@ -101,6 +103,13 @@ open class LinkerSpec : CommandLineToolSpec, @unchecked Sendable {
self.dsymPath = dsymPath
self.xcframeworkSourcePath = xcframeworkSourcePath
self.privacyFile = privacyFile
let prefix = refSettings?.globalScope.evaluate(BuiltinMacros.EXECUTABLE_PREFIX) ?? ""
#if os(Windows)
let default_prefix = ""
#else
let default_prefix = "lib"
#endif
self.libPrefix = prefix == "" ? default_prefix : prefix
}
}

Expand Down
71 changes: 24 additions & 47 deletions Sources/SWBCore/SpecImplementations/Tools/LinkerTools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1289,41 +1289,12 @@ public final class LdLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @unchec
private static func computeLibraryArgs(_ libraries: [LibrarySpecifier], scope: MacroEvaluationScope) -> (args: [String], inputs: [Path]) {
// Construct the library arguments.
return libraries.compactMap { specifier -> (args: [String], inputs: [Path]) in
let basename = specifier.path.basename

// FIXME: This isn't a good system, we need to redesign how we talk to the linker w.r.t. search paths and our notion of paths.
switch specifier.kind {
case .static:
if specifier.useSearchPaths, basename.hasPrefix("lib"), basename.hasSuffix(".a") {
return (specifier.searchPathFlagsForLd(basename.withoutPrefix("lib").withoutSuffix(".a")), [])
}
return (specifier.absolutePathFlagsForLd(), [specifier.path])
case .dynamic:
let suffix = ".\(scope.evaluate(BuiltinMacros.DYNAMIC_LIBRARY_EXTENSION))"
if specifier.useSearchPaths, basename.hasPrefix("lib"), basename.hasSuffix(suffix) {
return (specifier.searchPathFlagsForLd(basename.withoutPrefix("lib").withoutSuffix(suffix)), [])
}
return (specifier.absolutePathFlagsForLd(), [specifier.path])
case .textBased:
if specifier.useSearchPaths, basename.hasPrefix("lib"), basename.hasSuffix(".tbd") {
// .merge and .reexport are not supported for text-based libraries.
return (specifier.searchPathFlagsForLd(basename.withoutPrefix("lib").withoutSuffix(".tbd")), [])
}
return (specifier.absolutePathFlagsForLd(), [specifier.path])
case .framework:
let frameworkName = Path(basename).withoutSuffix
case .static, .dynamic, .textBased, .framework:
if specifier.useSearchPaths {
return (specifier.searchPathFlagsForLd(frameworkName), [])
}
let absPathArgs = specifier.absolutePathFlagsForLd()
let returnPath: Path
if let pathArg = absPathArgs.last, Path(pathArg).basename == frameworkName {
returnPath = Path(pathArg)
}
else {
returnPath = specifier.path
return (specifier.searchPathFlagsForLd(), [])
}
return (absPathArgs, [returnPath])
return (specifier.absolutePathFlagsForLd(), [specifier.path])
case .object:
// Object files are added to linker inputs in the sources task producer.
return ([], [])
Expand Down Expand Up @@ -1559,35 +1530,41 @@ public final class LdLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @unchec

/// Extensions to `LinkerSpec.LibrarySpecifier` specific to the dynamic linker.
fileprivate extension LinkerSpec.LibrarySpecifier {
func searchPathFlagsForLd(_ name: String) -> [String] {
func searchPathFlagsForLd() -> [String] {
let strippedName: String
if path.basename.hasPrefix(libPrefix) {
strippedName = Path(path.basename).withoutSuffix.withoutPrefix(libPrefix)
} else {
strippedName = Path(path.basename).withoutSuffix
}
switch (kind, mode) {
case (.dynamic, .normal):
return ["-l" + name]
return ["-l" + strippedName]
case (.dynamic, .reexport):
return ["-Xlinker", "-reexport-l" + name]
return ["-Xlinker", "-reexport-l" + strippedName]
case (.dynamic, .merge):
return ["-Xlinker", "-merge-l" + name]
return ["-Xlinker", "-merge-l" + strippedName]
case (.dynamic, .reexport_merge):
return ["-Xlinker", "-no_merge-l" + name]
return ["-Xlinker", "-no_merge-l" + strippedName]
case (.dynamic, .weak):
return ["-weak-l" + name]
return ["-weak-l" + strippedName]
case (.static, .weak),
(.textBased, .weak):
return ["-weak-l" + name]
return ["-weak-l" + strippedName]
case (.static, _),
(.textBased, _):
// Other modes are not supported for these kinds.
return ["-l" + name]
return ["-l" + strippedName]
case (.framework, .normal):
return ["-framework", name]
return ["-framework", strippedName]
case (.framework, .reexport):
return ["-Xlinker", "-reexport_framework", "-Xlinker", name]
return ["-Xlinker", "-reexport_framework", "-Xlinker", strippedName]
case (.framework, .merge):
return ["-Xlinker", "-merge_framework", "-Xlinker", name]
return ["-Xlinker", "-merge_framework", "-Xlinker", strippedName]
case (.framework, .reexport_merge):
return ["-Xlinker", "-no_merge_framework", "-Xlinker", name]
return ["-Xlinker", "-no_merge_framework", "-Xlinker", strippedName]
case (.framework, .weak):
return ["-weak_framework", name]
return ["-weak_framework", strippedName]
case (.object, _):
// Object files are added to linker inputs in the sources task producer.
return []
Expand Down Expand Up @@ -1724,9 +1701,9 @@ public final class LibtoolLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @u
delegate.warning("Product \(cbc.output.basename) cannot weak-link \(specifier.kind) \(basename)")
}

if specifier.useSearchPaths, basename.hasPrefix("lib"), basename.hasSuffix(".a") {
if specifier.useSearchPaths {
// Locate using search paths: Add a -l option and *don't* add the path to the library as an input to the task.
return ["-l" + basename.withoutPrefix("lib").withoutSuffix(".a")]
return ["-l" + Path(specifier.path.basename).withoutSuffix.withoutPrefix(specifier.libPrefix)]
}
else {
// Locate using an absolute path: Add the path as an option and as an input to the task.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ package final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, F
useSearchPaths: useSearchPaths,
swiftModulePaths: swiftModulePaths,
swiftModuleAdditionalLinkerArgResponseFilePaths: swiftModuleAdditionalLinkerArgResponseFilePaths,
refSettings: settingsForRef,
privacyFile: privacyFile
)
} else if fileType.conformsTo(context.lookupFileType(identifier: "compiled.mach-o.dylib")!) {
Expand All @@ -516,6 +517,7 @@ package final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, F
useSearchPaths: useSearchPaths,
swiftModulePaths: [:],
swiftModuleAdditionalLinkerArgResponseFilePaths: [:],
refSettings: settingsForRef,
privacyFile: privacyFile
)
} else if fileType.conformsTo(context.lookupFileType(identifier: "sourcecode.text-based-dylib-definition")!) {
Expand All @@ -526,6 +528,7 @@ package final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, F
useSearchPaths: useSearchPaths,
swiftModulePaths: [:],
swiftModuleAdditionalLinkerArgResponseFilePaths: [:],
refSettings: settingsForRef,
privacyFile: privacyFile
)
} else if fileType.conformsTo(context.lookupFileType(identifier: "wrapper.framework")!) {
Expand Down Expand Up @@ -571,6 +574,7 @@ package final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, F
useSearchPaths: useSearchPaths,
swiftModulePaths: [:],
swiftModuleAdditionalLinkerArgResponseFilePaths: [:],
refSettings: settingsForRef,
topLevelItemPath: topLevelItemPath,
dsymPath: dsymPath,
privacyFile: privacyFile
Expand All @@ -580,7 +584,7 @@ package final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, F
kind: .object,
path: absolutePath,
mode: buildFile.shouldLinkWeakly ? .weak : .normal,
useSearchPaths: useSearchPaths,
useSearchPaths: false,
swiftModulePaths: swiftModulePaths,
swiftModuleAdditionalLinkerArgResponseFilePaths: swiftModuleAdditionalLinkerArgResponseFilePaths,
privacyFile: privacyFile
Expand Down
1 change: 1 addition & 0 deletions Sources/SWBUniversalPlatform/Specs/ProductTypes.xcspec
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
FULL_PRODUCT_NAME = "$(EXECUTABLE_NAME)";
MACH_O_TYPE = "mh_dylib";
REZ_EXECUTABLE = YES;
EXECUTABLE_PREFIX = "lib";
EXECUTABLE_SUFFIX = ".$(EXECUTABLE_EXTENSION)";
EXECUTABLE_EXTENSION = "$(DYNAMIC_LIBRARY_EXTENSION:default=dylib)";
PUBLIC_HEADERS_FOLDER_PATH = "/usr/local/include";
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBWindowsPlatform/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ struct WindowsSDKRegistryExtension: SDKRegistryExtension {
"GENERATE_INTERMEDIATE_TEXT_BASED_STUBS": "NO",

"LIBRARY_SEARCH_PATHS": "$(inherited) $(SDKROOT)/usr/lib/swift/windows/$(CURRENT_ARCH)",
"TEST_LIBRARY_SEARCH_PATHS": .plString("\(testingLibraryPath.strWithPosixSlashes)/Testing-$(SWIFT_TESTING_VERSION)/usr/lib/swift/windows/$(CURRENT_ARCH) \(testingLibraryPath.strWithPosixSlashes)/XCTest-$(XCTEST_VERSION)/usr/lib/swift/windows/$(CURRENT_ARCH)"),
"TEST_LIBRARY_SEARCH_PATHS": .plString("\(testingLibraryPath.strWithPosixSlashes)/Testing-$(SWIFT_TESTING_VERSION)/usr/lib/swift/windows/ \(testingLibraryPath.strWithPosixSlashes)/Testing-$(SWIFT_TESTING_VERSION)/usr/lib/swift/windows/$(CURRENT_ARCH) \(testingLibraryPath.strWithPosixSlashes)/XCTest-$(XCTEST_VERSION)/usr/lib/swift/windows/$(CURRENT_ARCH) \(testingLibraryPath.strWithPosixSlashes)/XCTest-$(XCTEST_VERSION)/usr/lib/swift/windows"),
"OTHER_SWIFT_FLAGS": "$(inherited) -libc $(DEFAULT_USE_RUNTIME)",

"DEFAULT_USE_RUNTIME": "MD",
Expand Down
Loading
Loading