Skip to content

Add a fallback extension none os dylibs #8022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion Sources/Basics/Triple+Basics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ extension Triple {
case .wasi:
return ".wasm"
default:
fatalError("Cannot create dynamic libraries for os \"\(os)\".")
// Fallback to a nonsense extension for operating systems with
// unknown conventions.
return ".dynamiclibrary"
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason this is preferred over .so?

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 don't really like the idea of the extension for "macho" being "so". I think that would be kinda surprising to the user.

I was considering this default branch determining the extension based on the object file format instead, thought?

Choose a reason for hiding this comment

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

What is the behavior if building on a non-macho platform like Linux? Would this still work?

Copy link
Member Author

Choose a reason for hiding this comment

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

This code path is only used to plan the build of a dynamic library product for an os none triple. Actually doing this build is nonsensical because the dynamic library would require a specific dynamic loader provided by the OS and by definition, os none does not have a dynamic loader.

One alternative option is to use the object format e.g.:

switch os {
case _ where isDarwin():
    return ".dylib"
case .linux, .openbsd:
    return ".so"
case .win32:
    return ".dll"
case .wasi:
    return ".wasm"
default:
  break
}

guard let objectFormat = self.objectFormat else {
    fatalError("Cannot create dynamic libraries unknown object format.")
}

switch objectFormat {
case .coff:
    return ".coff"
case .elf:
    return ".elf"
case .macho:
    return ".macho"
case .wasm:
    return ".wasm"
case .xcoff:
    return ".xcoff"
}

Choose a reason for hiding this comment

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

So this basically just spoofs the "dynamic library" on a none OS but doesn't actually generate anything? That's kind of weird. But if that's the case then I guess it's just arbitrary what you pick there.

Copy link
Member Author

@rauhul rauhul Oct 3, 2024

Choose a reason for hiding this comment

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

yeah its not a great thing to do, but fixing the underlying issue with build planning is something the swift pm team is working on

Choose a reason for hiding this comment

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

Well, I'm okay with this if it fixes the none OS issue and if it doesn't break anything!

Copy link
Member

Choose a reason for hiding this comment

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

I think doing this based on object format looks better.

}
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/BasicsTests/TripleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ final class TripleTests: XCTestCase {
_ = wasi.dynamicLibraryExtension
}

func testNoneOSDynamicLibrary() throws {
let noneOS = try Triple("armv7em-apple-none-macho")

// Dynamic libraries aren't actually supported for OS none, but swiftpm
// wants an extension to avoid crashing during build planning.
XCTAssertEqual(noneOS.dynamicLibraryExtension, ".dynamiclibrary")
}

func testIsRuntimeCompatibleWith() throws {
try XCTAssertTrue(Triple("x86_64-apple-macosx").isRuntimeCompatible(with: Triple("x86_64-apple-macosx")))
try XCTAssertTrue(Triple("x86_64-unknown-linux").isRuntimeCompatible(with: Triple("x86_64-unknown-linux")))
Expand Down