Skip to content

Commit 1c5a46c

Browse files
committed
Fix compilation on Linux
1 parent 883081c commit 1c5a46c

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

Sources/CGNUSourceExports/exports.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010

1111
int spi_execvpe(const char *file, char *const argv[], char *const envp[]);
1212

13-
14-
/* In the same principle of importing the less stuff possible, we declare ptsname ourselves,
15-
* though in this case we most definitely could import the whole of stdlib.h w/ _GNU_SOURCE set to 500 w/o conflict (we do it in the exports for the tests). */
16-
13+
/* From stdlib.h w/ _GNU_SOURCE set to 500. */
1714
char *spi_ptsname(int fd);
1815

19-
2016
#endif /* exports_h */
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# define _XOPEN_SOURCE 600
2+
# include <fcntl.h>
3+
# include <stdlib.h>
4+
5+
6+
7+
int spift_posix_openpt(int fd) {
8+
return posix_openpt(fd);
9+
}
10+
11+
int spift_grantpt(int fd) {
12+
return grantpt(fd);
13+
}
14+
15+
int spift_unlockpt(int fd) {
16+
return unlockpt(fd);
17+
}
18+
19+
char *spift_ptsname(int fd) {
20+
return ptsname(fd);
21+
}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
#ifndef exports_for_tests_h
22
# define exports_for_tests_h
33

4-
/* We need posix_openpt, grantpt, unlockpt and ptsname. */
5-
# define _XOPEN_SOURCE 600
6-
# include <fcntl.h>
7-
# include <stdlib.h>
4+
/* We need posix_openpt, grantpt, unlockpt and ptsname.
5+
* Using the following used to work but does not anymore.
6+
* AFAIU this is due to the `stat` struct being exported by this module and
7+
* being different than the one in CDispatch due to the `_XOPEN_SOURCE` define.
8+
* So instead we write shims. */
9+
//# define _XOPEN_SOURCE 600
10+
//# include <fcntl.h>
11+
//# include <stdlib.h>
12+
13+
int spift_posix_openpt(int);
14+
int spift_grantpt(int);
15+
int spift_unlockpt(int);
16+
char *spift_ptsname(int);
817

918
#endif /* exports_for_tests_h */

Sources/ProcessInvocation/ProcessInvocation+Pipe.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import StreamReader
99

1010

1111

12+
/* Some utilities to avoid name clash with ProcessInvocation functions and avoid using `Darwin.` which does not exist on Linux.
13+
* We’ll probably use System methods instead. */
14+
private func globalClose(_ fd: Int32) -> Int32 {close(fd)}
15+
private func globalWrite(_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int) -> Int {write(fd, buf, nbyte)}
16+
1217
public extension ProcessInvocation {
1318

1419
/**
@@ -51,7 +56,7 @@ public extension ProcessInvocation {
5156
fhWrite.writeabilityHandler = { fh in
5257
let closeFH = {
5358
fhWrite.writeabilityHandler = nil
54-
if Darwin.close(fh.fileDescriptor) == -1 {
59+
if globalClose(fh.fileDescriptor) == -1 {
5560
Conf.logger?.error("Failed closing write end of fd for pipe to swift; pipe might stay open forever.", metadata: ["errno": "\(errno)", "errno-str": "\(Errno(rawValue: errno).localizedDescription)"])
5661
}
5762
}
@@ -67,7 +72,7 @@ public extension ProcessInvocation {
6772
var ret: Int
6873
repeat {
6974
Conf.logger?.trace("Trying to write on write end of pipe.", metadata: ["bytes_count": "\(bytes.count)"])
70-
ret = Darwin.write(fh.fileDescriptor, bytes.baseAddress!, bytes.count)
75+
ret = globalWrite(fh.fileDescriptor, bytes.baseAddress!, bytes.count)
7176
} while ret == -1 && errno == EINTR
7277
return (ret, errno)
7378
}()

Tests/ProcessInvocationTests/ProcessInvocationTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ import CGNUSourceExportsForTests
1818

1919

2020

21+
#if !canImport(Darwin)
22+
private let posix_openpt = spift_posix_openpt
23+
private let grantpt = spift_grantpt
24+
private let unlockpt = spift_unlockpt
25+
private let ptsname = spift_ptsname
26+
#endif
27+
2128
final class ProcessInvocationTests : XCTestCase {
2229

2330
override class func setUp() {

0 commit comments

Comments
 (0)