Skip to content
Open
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
12 changes: 11 additions & 1 deletion Sources/SWBCore/DependencyResolution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,17 @@ struct SpecializationParameters: Hashable, CustomStringConvertible {
func imposed(on parameters: BuildParameters, workspaceContext: WorkspaceContext) -> BuildParameters {
var overrides = parameters.overrides
if let sdkRoot {
overrides["SDKROOT"] = sdkRoot
// The sdkCanonicalName from a Platform is not guaranteed to be available
let defaultSDKAvailable: Bool
do {
let foundSDK = try workspaceContext.core.sdkRegistry.lookup(sdkRoot, activeRunDestination: parameters.activeRunDestination)
defaultSDKAvailable = foundSDK != nil
} catch {
defaultSDKAvailable = false
}
if defaultSDKAvailable {
overrides["SDKROOT"] = sdkRoot
}
Comment on lines +209 to +219
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure if this is the right approach. Do we really need to override SDKROOT here in the first place? The overridden SDKROOT was always platform.sdkCanonicalName, which is always the same as platform.name ("webassembly"), but "webassembly" SDK is unavailable.

I'm a bit lost here because I can't access the git history before it got open-sourced.

}
if let sdkVariant {
overrides["SDK_VARIANT"] = sdkVariant.name
Expand Down
28 changes: 19 additions & 9 deletions Sources/SWBCore/SwiftSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,10 @@ public struct SwiftSDK: Sendable {
/// Target-specific properties for this SDK.
public let targetTriples: [String: TripleProperties]

init?(identifier: String, version: String, path: Path, fs: any FSProxy) throws {
init?(identifier: String, version: String, metadataPath: Path, fs: any FSProxy) throws {
self.identifier = identifier
self.version = version
self.path = path

let metadataPath = path.join("swift-sdk.json")
guard fs.exists(metadataPath) else { return nil }
self.path = metadataPath.dirname

let metadataData = try Data(fs.read(metadataPath))
let schema = try JSONDecoder().decode(SchemaVersionInfo.self, from: metadataData)
Expand All @@ -70,6 +67,8 @@ public struct SwiftSDK: Sendable {

/// The default location storing Swift SDKs installed by SwiftPM.
static func defaultSwiftSDKsDirectory(hostOperatingSystem: OperatingSystem) throws -> Path {
// NOTE: Keep in sync with SwiftPM's defaultSwiftSDKsDirectory implementation.
// https://github.com/swiftlang/swift-package-manager/blob/deac56dc94d85d28f2f2b5c37ec347ae3523a3fe/Sources/Basics/FileSystem/FileSystem%2BExtensions.swift#L492
let spmURL: URL
if hostOperatingSystem == .macOS {
spmURL = try FileManager.default.url(
Expand All @@ -79,7 +78,11 @@ public struct SwiftSDK: Sendable {
create: false
).appendingPathComponent("org.swift.swiftpm")
} else {
spmURL = URL.homeDirectory.appendingPathComponent(".swiftpm")
if let configurationDirectory = Environment.current["XDG_CONFIG_HOME"] {
spmURL = URL(fileURLWithPath: configurationDirectory, isDirectory: true).appendingPathComponent("swiftpm")
} else {
spmURL = URL.homeDirectory.appendingPathComponent(".swiftpm")
}
}
return try spmURL.appendingPathComponent("swift-sdks").filePath
}
Expand Down Expand Up @@ -137,13 +140,20 @@ public struct SwiftSDK: Sendable {

for (identifier, artifact) in info.artifacts {
for variant in artifact.variants {
let sdkPath = artifactBundle.join(variant.path)
guard fs.isDirectory(sdkPath) else { continue }
// A variant's path may be a directory or a file. If it's a directory, we assume it
// contains a swift-sdk.json file. If it's a file, we use it directly as a Swift SDK
// metadata file.
var sdkPath = artifactBundle.join(variant.path)
if sdkPath.fileExtension != "json" && fs.isDirectory(sdkPath) {
sdkPath = sdkPath.join("swift-sdk.json")
}

guard fs.exists(sdkPath) else { continue }

// FIXME: For now, we only support SDKs that are compatible with any host triple.
guard variant.supportedTriples?.isEmpty ?? true else { continue }

guard let sdk = try SwiftSDK(identifier: identifier, version: artifact.version, path: sdkPath, fs: fs) else { continue }
guard let sdk = try SwiftSDK(identifier: identifier, version: artifact.version, metadataPath: sdkPath, fs: fs) else { continue }
// Filter out SDKs that don't support any of the target triples.
if let targetTriples {
guard targetTriples.contains(where: { sdk.targetTriples[$0] != nil }) else { continue }
Expand Down
4 changes: 0 additions & 4 deletions Sources/SWBWebAssemblyPlatform/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ struct WebAssemblySDKRegistryExtension: SDKRegistryExtension {
"LLVM_TARGET_TRIPLE_OS_VERSION": .plString(os),
"SWIFT_LIBRARY_PATH": .plString(swiftResourceDir.join("wasi").str),
"SWIFT_RESOURCE_DIR": .plString(swiftResourceDir.str),
// HACK: Ld step does not use swiftc as linker driver but instead uses clang, so we need to add some Swift specific flags
// assuming static linking.
// Tracked in https://github.com/swiftlang/swift-build/issues/3
"OTHER_LDFLAGS": .plArray(["-lc++", "-lc++abi", "-resource-dir", "$(SWIFT_RESOURCE_DIR)/clang", "@$(SWIFT_LIBRARY_PATH)/static-executable-args.lnk"]),
]),
"SupportedTargets": .plDict([
"webassembly": .plDict([
Expand Down
24 changes: 24 additions & 0 deletions Sources/SWBWebAssemblyPlatform/Specs/WasmLd.xcspec
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@
Type = Path;
Condition = "NO"; // wasm-ld does not support -filelist
},
{
Name = "SWIFTC_LINKER_RESOURCE_DIR";
Type = Path;
DefaultValue = "$(SWIFT_RESOURCE_DIR)";
Condition = "$(LINKER_DRIVER) == swiftc";
CommandLineArgs = (
"-resource-dir",
"$(value)",
"-Xclang-linker",
"-resource-dir",
"-Xclang-linker",
"$(value)/clang",
);
IsInputDependency = Yes;
},
// TODO(yuta): Remove this override entry after https://github.com/swiftlang/swift-driver/pull/2053
{
Name = SWIFTC_SDKROOT_LINKER_INPUT;
Type = Path;
DefaultValue = "$(SYSROOT:default=$(SDKROOT))";
Condition = "$(LINKER_DRIVER) == swiftc";
CommandLineFlag = "-sdk";
IsInputDependency = Yes;
},
);
},
)
Loading