Skip to content

Commit 09b7eb7

Browse files
authored
Add support for Musl. (#726)
Motivation: We would like to make this work for Musl so that we can build fully statically linked binaries that use AsyncHTTPClient. Modifications: Define `_GNU_SOURCE` as a compiler argument; doing it in a source file doesn't work properly with modular headers. Add imports of `Musl` in appropriate places. `Musl` doesn't have `strptime_l`, so avoid using that. Result: async-http-client will build for Musl.
1 parent 2914386 commit 09b7eb7

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

Package.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ let package = Package(
3232
.package(url: "https://github.com/apple/swift-algorithms", from: "1.0.0"),
3333
],
3434
targets: [
35-
.target(name: "CAsyncHTTPClient"),
35+
.target(
36+
name: "CAsyncHTTPClient",
37+
cSettings: [
38+
.define("_GNU_SOURCE"),
39+
]
40+
),
3641
.target(
3742
name: "AsyncHTTPClient",
3843
dependencies: [

Sources/AsyncHTTPClient/ConnectionPool.swift

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import NIOSSL
1616

1717
#if canImport(Darwin)
1818
import Darwin.C
19+
#elseif canImport(Musl)
20+
import Musl
1921
#elseif os(Linux) || os(FreeBSD) || os(Android)
2022
import Glibc
2123
#else

Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+Backoff.swift

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import NIOCore
1616
#if canImport(Darwin)
1717
import func Darwin.pow
18+
#elseif canImport(Musl)
19+
import func Musl.pow
1820
#else
1921
import func Glibc.pow
2022
#endif

Sources/AsyncHTTPClient/HTTPClient+HTTPCookie.swift

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import NIOHTTP1
1616
#if canImport(Darwin)
1717
import Darwin
18+
#elseif canImport(Musl)
19+
import Musl
1820
#elseif canImport(Glibc)
1921
import Glibc
2022
#endif

Sources/CAsyncHTTPClient/CAsyncHTTPClient.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#if __APPLE__
1616
#include <xlocale.h>
1717
#elif __linux__
18-
#define _GNU_SOURCE
1918
#include <locale.h>
2019
#endif
2120

@@ -32,7 +31,11 @@ bool swiftahc_cshims_strptime(const char * string, const char * format, struct t
3231

3332
bool swiftahc_cshims_strptime_l(const char * string, const char * format, struct tm * result, void * locale) {
3433
// The pointer cast is fine as long we make sure it really points to a locale_t.
34+
#ifdef __musl__
35+
const char * firstNonProcessed = strptime(string, format, result);
36+
#else
3537
const char * firstNonProcessed = strptime_l(string, format, result, (locale_t)locale);
38+
#endif
3639
if (firstNonProcessed) {
3740
return *firstNonProcessed == 0;
3841
}

Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import NIOTransportServices
3030
import XCTest
3131
#if canImport(Darwin)
3232
import Darwin
33+
#elseif canImport(Musl)
34+
import Musl
3335
#elseif canImport(Glibc)
3436
import Glibc
3537
#endif

0 commit comments

Comments
 (0)