Skip to content

Commit 1d9b92c

Browse files
committed
Make SPM_posix_spawn_file_actions_addchdir_np* available on all non-Windows platforms
This will now be able to work correctly under FreeBSD, Android, musl, and so on.
1 parent 436c042 commit 1d9b92c

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

Sources/TSCBasic/Process/Process.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -680,24 +680,11 @@ public final class Process {
680680
defer { posix_spawn_file_actions_destroy(&fileActions) }
681681

682682
if let workingDirectory = workingDirectory?.pathString {
683-
#if canImport(Darwin)
684-
// The only way to set a workingDirectory is using an availability-gated initializer, so we don't need
685-
// to handle the case where the posix_spawn_file_actions_addchdir_np method is unavailable. This check only
686-
// exists here to make the compiler happy.
687-
if #available(macOS 10.15, *) {
688-
posix_spawn_file_actions_addchdir_np(&fileActions, workingDirectory)
689-
}
690-
#elseif os(FreeBSD)
691-
posix_spawn_file_actions_addchdir_np(&fileActions, workingDirectory)
692-
#elseif os(Linux)
693683
guard SPM_posix_spawn_file_actions_addchdir_np_supported() else {
694684
throw Process.Error.workingDirectoryNotSupported
695685
}
696686

697687
SPM_posix_spawn_file_actions_addchdir_np(&fileActions, workingDirectory)
698-
#else
699-
throw Process.Error.workingDirectoryNotSupported
700-
#endif
701688
}
702689

703690
var stdinPipe: [Int32] = [-1, -1]

Sources/TSCclibc/include/process.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined(__linux__)
1+
#if !defined(_WIN32)
22

33
#include <spawn.h>
44
#include <stdbool.h>

Sources/TSCclibc/process.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,54 @@
1-
#if defined(__linux__)
1+
#if !defined(_WIN32)
22

3+
#if defined(__linux__)
34
#ifndef _GNU_SOURCE
45
#define _GNU_SOURCE /* for posix_spawn_file_actions_addchdir_np */
56
#endif
7+
#endif
8+
9+
#ifndef __GLIBC_PREREQ
10+
#define __GLIBC_PREREQ(maj, min) 0
11+
#endif
612

713
#include <errno.h>
814

915
#include "process.h"
1016

1117
int SPM_posix_spawn_file_actions_addchdir_np(posix_spawn_file_actions_t *restrict file_actions, const char *restrict path) {
12-
#if defined(__GLIBC__)
13-
# if __GLIBC_PREREQ(2, 29)
14-
return posix_spawn_file_actions_addchdir_np(file_actions, path);
15-
# else
18+
#if defined(__GLIBC__) && !__GLIBC_PREREQ(2, 29)
19+
// Glibc versions prior to 2.29 don't support posix_spawn_file_actions_addchdir_np, impacting:
20+
// - Amazon Linux 2 (EoL mid-2025)
1621
return ENOSYS;
17-
# endif
18-
#else
22+
#elif defined(__ANDROID__) && __ANDROID_API__ < 34
23+
// Android versions prior to 14 (API level 34) don't support posix_spawn_file_actions_addchdir_np
1924
return ENOSYS;
25+
#elif defined(__OpenBSD__) || defined(__QNX__)
26+
// Currently missing as of:
27+
// - OpenBSD 7.5 (April 2024)
28+
// - QNX 8 (December 2023)
29+
return ENOSYS;
30+
#elif defined(__GLIBC__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(__musl__)
31+
// Pre-standard posix_spawn_file_actions_addchdir_np version available in:
32+
// - Solaris 11.3 (October 2015)
33+
// - Glibc 2.29 (February 2019)
34+
// - macOS 10.15 (October 2019)
35+
// - musl 1.1.24 (October 2019)
36+
// - FreeBSD 13.1 (May 2022)
37+
// - Android 14 (October 2023)
38+
return posix_spawn_file_actions_addchdir_np((posix_spawn_file_actions_t *)file_actions, path);
39+
#else
40+
// Standardized posix_spawn_file_actions_addchdir version (POSIX.1-2024, June 2024) available in:
41+
// - Solaris 11.4 (August 2018)
42+
// - NetBSD 10.0 (March 2024)
43+
return posix_spawn_file_actions_addchdir((posix_spawn_file_actions_t *)file_actions, path);
2044
#endif
2145
}
2246

2347
bool SPM_posix_spawn_file_actions_addchdir_np_supported() {
24-
#if defined(__GLIBC__)
25-
# if __GLIBC_PREREQ(2, 29)
26-
return true;
27-
# else
48+
#if (defined(__GLIBC__) && !__GLIBC_PREREQ(2, 29)) || (defined(__OpenBSD__)) || (defined(__ANDROID__) && __ANDROID_API__ < 34) || (defined(__QNX__))
2849
return false;
29-
# endif
3050
#else
31-
return false;
51+
return true;
3252
#endif
3353
}
3454

Tests/TSCBasicTests/ProcessTests.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,7 @@ class ProcessTests: XCTestCase {
428428
}
429429

430430
func testWorkingDirectory() throws {
431-
guard #available(macOS 10.15, *) else {
432-
// Skip this test since it's not supported in this OS.
433-
return
434-
}
435-
436-
#if os(Linux) || os(Android)
431+
#if !os(Windows)
437432
guard SPM_posix_spawn_file_actions_addchdir_np_supported() else {
438433
// Skip this test since it's not supported in this OS.
439434
return

0 commit comments

Comments
 (0)