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
48 changes: 48 additions & 0 deletions .github/workflow/test-swift-build-with-musl-and-glibc.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -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.

/**
Expand Down
86 changes: 55 additions & 31 deletions Sources/Signals/Signals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

///
Expand All @@ -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
}

}