diff --git a/.github/workflow/test-swift-build-with-musl-and-glibc.yaml b/.github/workflow/test-swift-build-with-musl-and-glibc.yaml new file mode 100644 index 0000000..ec81fcf --- /dev/null +++ b/.github/workflow/test-swift-build-with-musl-and-glibc.yaml @@ -0,0 +1,48 @@ +name: Test swift build with musl and glibc + +on: + pull_request: + branches: [master] + +jobs: + build-image-glibc: + name: Normal swift build i.e. glibc + container: + image: swift:6.1 + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Cache build dependencies + uses: actions/cache@v4 + with: + path: .build + key: ${{ runner.os }}-build-${{ hashFiles('Package.resolved') }} + + - name: Build + run: swift build + + build-image-musl: + name: Build image using swift container plugin i.e. musl + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Cache build dependencies + uses: actions/cache@v4 + with: + path: .build + key: ${{ runner.os }}-build-${{ hashFiles('Package.resolved') }} + + - name: Install the static SDK + run: | + swift sdk install \ + https://download.swift.org/swift-6.1-release/static-sdk/swift-6.1-RELEASE/swift-6.1-RELEASE_static-linux-0.0.1.artifactbundle.tar.gz \ + --checksum 111c6f7d280a651208b8c74c0521dd99365d785c1976a6e23162f55f65379ac6 + + - name: Build + run: | + swift build \ + --swift-sdk x86_64-swift-linux-musl \ No newline at end of file diff --git a/Package.swift b/Package.swift index 6407c42..1b00de9 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.1 +// swift-tools-version:5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. /** diff --git a/Sources/Signals/Signals.swift b/Sources/Signals/Signals.swift index dba84e2..86cff2d 100644 --- a/Sources/Signals/Signals.swift +++ b/Sources/Signals/Signals.swift @@ -21,7 +21,11 @@ #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) import Darwin #elseif os(Linux) - import Glibc + #if canImport(Musl) + import Musl + #else + import Glibc + #endif #endif import Foundation @@ -90,8 +94,16 @@ public class Signals { return Int32(SIGPROF) case .winch: return Int32(SIGWINCH) - case .info: - return Int32(SIGINFO) + +#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || canImport(Glibc) + case .info: return Int32(SIGINFO) +#elseif os(Linux) + #if canImport(Musl) + case .info: return Int32(SIGUSR1) // No SIGINFO signal in Musl. SIGUSR1 may be the best alternative for SIGINFO in Musl + #else + case .info: return Int32(SIGINFO) + #endif +#endif case .user(let sig): return Int32(sig) @@ -117,8 +129,16 @@ public class Signals { case Int(SIGIO): self = .io case Int(SIGPROF): self = .prof case Int(SIGWINCH): self = .winch +#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || canImport(Glibc) case Int(SIGINFO): self = .info - +#elseif os(Linux) + #if canImport(Musl) + // no SIGINFO in Musl + #else + case Int(SIGINFO): self = .info + #endif +#endif + default: self = .user(rawValue) } @@ -154,14 +174,17 @@ public class Signals { sigaction(signal.rawValue, actionPointer, nil) } - #elseif os(Linux) - - var sigAction = sigaction() - - sigAction.__sigaction_handler = unsafeBitCast(action, to: sigaction.__Unnamed_union___sigaction_handler.self) - - _ = sigaction(signal.rawValue, &sigAction, nil) - + #elseif os(Linux) + #if canImport(Musl) + // Musl: sigAction is not supported or need more research for alternative + #else + var sigAction = sigaction() + + sigAction.__sigaction_handler = unsafeBitCast(action, to: sigaction.__Unnamed_union___sigaction_handler.self) + + _ = sigaction(signal.rawValue, &sigAction, nil) + #endif + #endif } @@ -201,13 +224,13 @@ public class Signals { public class func raise(signal: Signal) { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) - _ = Darwin.raise(signal.rawValue) - - #elseif os(Linux) - - _ = Glibc.raise(signal.rawValue) - + #elseif os(Linux) + #if canImport(Musl) + _ = Musl.raise(signal.rawValue) + #else + _ = Glibc.raise(signal.rawValue) + #endif #endif } @@ -221,12 +244,13 @@ public class Signals { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) _ = Darwin.signal(signal.rawValue, SIG_IGN) - - #elseif os(Linux) - - _ = Glibc.signal(signal.rawValue, SIG_IGN) - - #endif + #elseif os(Linux) + #if canImport(Musl) + _ = Musl.signal(signal.rawValue, SIG_IGN) + #else + _ = Glibc.signal(signal.rawValue, SIG_IGN) + #endif + #endif } /// @@ -237,14 +261,14 @@ public class Signals { public class func restore(signal: Signal) { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) - _ = Darwin.signal(signal.rawValue, SIG_DFL) - - #elseif os(Linux) - - _ = Glibc.signal(signal.rawValue, SIG_DFL) - - #endif + #elseif os(Linux) + #if canImport(Musl) + _ = Musl.signal(signal.rawValue, SIG_DFL) + #else + _ = Glibc.signal(signal.rawValue, SIG_DFL) + #endif + #endif } }