Skip to content

Commit 23fa281

Browse files
authored
Add a fallback extension none os dylibs (#8022)
Adds an extension for none os dynamic libraries to avoid a SwiftPM crash during build planning when cross compiling to none OS with a dylib product in the graph. Fixes #7893
1 parent 15cea27 commit 23fa281

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Sources/Basics/Triple+Basics.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ extension Triple {
133133
/// The file extension for dynamic libraries (eg. `.dll`, `.so`, or `.dylib`)
134134
public var dynamicLibraryExtension: String {
135135
guard let os = self.os else {
136-
fatalError("Cannot create dynamic libraries unknown os.")
136+
fatalError("Cannot create dynamic libraries for unknown os.")
137137
}
138138

139139
switch os {
@@ -146,7 +146,24 @@ extension Triple {
146146
case .wasi:
147147
return ".wasm"
148148
default:
149-
fatalError("Cannot create dynamic libraries for os \"\(os)\".")
149+
break
150+
}
151+
152+
guard let objectFormat = self.objectFormat else {
153+
fatalError("Cannot create dynamic libraries for unknown object format.")
154+
}
155+
156+
switch objectFormat {
157+
case .coff:
158+
return ".coff"
159+
case .elf:
160+
return ".elf"
161+
case .macho:
162+
return ".macho"
163+
case .wasm:
164+
return ".wasm"
165+
case .xcoff:
166+
return ".xcoff"
150167
}
151168
}
152169

Tests/BasicsTests/TripleTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,33 @@ final class TripleTests: XCTestCase {
209209
func testWASI() throws {
210210
let wasi = try Triple("wasm32-unknown-wasi")
211211

212+
213+
212214
// WASI dynamic libraries are only experimental,
213215
// but SwiftPM requires this property not to crash.
214216
_ = wasi.dynamicLibraryExtension
215217
}
216218

219+
func testNoneOSDynamicLibrary() throws {
220+
// Dynamic libraries aren't actually supported for OS none, but swiftpm
221+
// wants an extension to avoid crashing during build planning.
222+
try XCTAssertEqual(
223+
Triple("armv7em-unknown-none-coff").dynamicLibraryExtension,
224+
".coff")
225+
try XCTAssertEqual(
226+
Triple("armv7em-unknown-none-elf").dynamicLibraryExtension,
227+
".elf")
228+
try XCTAssertEqual(
229+
Triple("armv7em-unknown-none-macho").dynamicLibraryExtension,
230+
".macho")
231+
try XCTAssertEqual(
232+
Triple("armv7em-unknown-none-wasm").dynamicLibraryExtension,
233+
".wasm")
234+
try XCTAssertEqual(
235+
Triple("armv7em-unknown-none-xcoff").dynamicLibraryExtension,
236+
".xcoff")
237+
}
238+
217239
func testIsRuntimeCompatibleWith() throws {
218240
try XCTAssertTrue(Triple("x86_64-apple-macosx").isRuntimeCompatible(with: Triple("x86_64-apple-macosx")))
219241
try XCTAssertTrue(Triple("x86_64-unknown-linux").isRuntimeCompatible(with: Triple("x86_64-unknown-linux")))

0 commit comments

Comments
 (0)