Skip to content

Commit 171a4e5

Browse files
committed
[android][test] Fix missing fcntl functions in Bionic
`stdlib/public/Glibc` defines shims to functions like `fcntl`, whereas `stdlib/public/Bionic` does not. These shims are needed because, for example, the sysroot `fcntl` takes variadic arguments (which are unsupported). Copy the shims over from `stdlib/public/Glibc`. This fixes `test/1_stdlib/POSIX.swift` when run on Android.
1 parent 85c1031 commit 171a4e5

File tree

4 files changed

+188
-2
lines changed

4 files changed

+188
-2
lines changed

stdlib/public/Bionic/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
set(sources
22
module.map
33
spawn.h
4-
)
4+
)
5+
56
set(output_dir "${SWIFTLIB_DIR}/bionic")
67

78
set(commands
@@ -37,6 +38,7 @@ swift_install_in_component(stdlib
3738
add_swift_library(swiftGlibc IS_SDK_OVERLAY
3839
Glibc.swift
3940
posix_spawn.c
41+
Misc.c
4042
FILE_DEPENDS copy_bionic_module "${SWIFTLIB_DIR}/bionic"
4143
TARGET_SDKS ANDROID
4244
INSTALL_IN_COMPONENT stdlib-experimental)

stdlib/public/Bionic/Glibc.swift

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,100 @@ public var errno: Int32 {
2929
// fcntl.h
3030
//===----------------------------------------------------------------------===//
3131

32+
@warn_unused_result
33+
@_silgen_name("_swift_Bionic_open")
34+
func _swift_Bionic_open(path: UnsafePointer<CChar>,
35+
_ oflag: CInt,
36+
_ mode: mode_t
37+
) -> CInt
38+
39+
@warn_unused_result
40+
@_silgen_name("_swift_Bionic_openat")
41+
func _swift_Bionic_openat(
42+
fd: CInt,
43+
_ path: UnsafePointer<CChar>,
44+
_ oflag: CInt,
45+
_ mode: mode_t
46+
) -> CInt
47+
48+
@warn_unused_result
49+
public func open(
50+
path: UnsafePointer<CChar>,
51+
_ oflag: CInt
52+
) -> CInt {
53+
return _swift_Bionic_open(path, oflag, 0)
54+
}
55+
56+
@warn_unused_result
57+
public func open(
58+
path: UnsafePointer<CChar>,
59+
_ oflag: CInt,
60+
_ mode: mode_t
61+
) -> CInt {
62+
return _swift_Bionic_open(path, oflag, mode)
63+
}
64+
65+
@warn_unused_result
66+
public func openat(
67+
fd: CInt,
68+
_ path: UnsafePointer<CChar>,
69+
_ oflag: CInt
70+
) -> CInt {
71+
return _swift_Bionic_openat(fd, path, oflag, 0)
72+
}
73+
74+
@warn_unused_result
75+
public func openat(
76+
fd: CInt,
77+
_ path: UnsafePointer<CChar>,
78+
_ oflag: CInt,
79+
_ mode: mode_t
80+
) -> CInt {
81+
return _swift_Bionic_openat(fd, path, oflag, mode)
82+
}
83+
84+
@warn_unused_result
85+
@_silgen_name("_swift_Bionic_fcntl")
86+
internal func _swift_Bionic_fcntl(
87+
fd: CInt,
88+
_ cmd: CInt,
89+
_ value: CInt
90+
) -> CInt
91+
92+
@warn_unused_result
93+
@_silgen_name("_swift_Bionic_fcntlPtr")
94+
internal func _swift_Bionic_fcntlPtr(
95+
fd: CInt,
96+
_ cmd: CInt,
97+
_ ptr: UnsafeMutablePointer<Void>
98+
) -> CInt
99+
100+
@warn_unused_result
101+
public func fcntl(
102+
fd: CInt,
103+
_ cmd: CInt
104+
) -> CInt {
105+
return _swift_Bionic_fcntl(fd, cmd, 0)
106+
}
107+
108+
@warn_unused_result
109+
public func fcntl(
110+
fd: CInt,
111+
_ cmd: CInt,
112+
_ value: CInt
113+
) -> CInt {
114+
return _swift_Bionic_fcntl(fd, cmd, value)
115+
}
116+
117+
@warn_unused_result
118+
public func fcntl(
119+
fd: CInt,
120+
_ cmd: CInt,
121+
_ ptr: UnsafeMutablePointer<Void>
122+
) -> CInt {
123+
return _swift_Bionic_fcntlPtr(fd, cmd, ptr)
124+
}
125+
32126
public var S_IFMT: mode_t { return mode_t(0o170000) }
33127
public var S_IFIFO: mode_t { return mode_t(0o010000) }
34128
public var S_IFCHR: mode_t { return mode_t(0o020000) }
@@ -74,3 +168,48 @@ public var SIG_HOLD: __sighandler_t {
74168
}
75169
#endif
76170

171+
//===----------------------------------------------------------------------===//
172+
// semaphore.h
173+
//===----------------------------------------------------------------------===//
174+
175+
/// The value returned by `sem_open()` in the case of failure.
176+
public var SEM_FAILED: UnsafeMutablePointer<sem_t> {
177+
// FIXME: swift/stdlib/public/Glibc/Glibc.swift claims that
178+
// this value is ABI. The value here has not been verified
179+
// on Bionic.
180+
return UnsafeMutablePointer<sem_t>(bitPattern: 0)
181+
}
182+
183+
@warn_unused_result
184+
@_silgen_name("_swift_Bionic_sem_open2")
185+
internal func _swift_Bionic_sem_open2(
186+
name: UnsafePointer<CChar>,
187+
_ oflag: CInt
188+
) -> UnsafeMutablePointer<sem_t>
189+
190+
@warn_unused_result
191+
@_silgen_name("_swift_Bionic_sem_open4")
192+
internal func _swift_Bionic_sem_open4(
193+
name: UnsafePointer<CChar>,
194+
_ oflag: CInt,
195+
_ mode: mode_t,
196+
_ value: CUnsignedInt
197+
) -> UnsafeMutablePointer<sem_t>
198+
199+
@warn_unused_result
200+
public func sem_open(
201+
name: UnsafePointer<CChar>,
202+
_ oflag: CInt
203+
) -> UnsafeMutablePointer<sem_t> {
204+
return _swift_Bionic_sem_open2(name, oflag)
205+
}
206+
207+
@warn_unused_result
208+
public func sem_open(
209+
name: UnsafePointer<CChar>,
210+
_ oflag: CInt,
211+
_ mode: mode_t,
212+
_ value: CUnsignedInt
213+
) -> UnsafeMutablePointer<sem_t> {
214+
return _swift_Bionic_sem_open4(name, oflag, mode, value)
215+
}

stdlib/public/Bionic/Misc.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===--- Misc.c - Bionic overlay helpers -----------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <fcntl.h>
14+
#include <sys/types.h>
15+
#include <sys/stat.h>
16+
#include <semaphore.h>
17+
18+
extern int
19+
_swift_Bionic_open(const char *path, int oflag, mode_t mode) {
20+
return open(path, oflag, mode);
21+
}
22+
23+
extern int
24+
_swift_Bionic_openat(int fd, const char *path, int oflag, mode_t mode) {
25+
return openat(fd, path, oflag, mode);
26+
}
27+
28+
extern sem_t *_swift_Bionic_sem_open2(const char *name, int oflag) {
29+
return sem_open(name, oflag);
30+
}
31+
32+
extern sem_t *_swift_Bionic_sem_open4(const char *name, int oflag,
33+
mode_t mode, unsigned int value) {
34+
return sem_open(name, oflag, mode, value);
35+
}
36+
37+
extern int
38+
_swift_Bionic_fcntl(int fd, int cmd, int value) {
39+
return fcntl(fd, cmd, value);
40+
}
41+
42+
extern int
43+
_swift_Bionic_fcntlPtr(int fd, int cmd, void* ptr) {
44+
return fcntl(fd, cmd, ptr);
45+
}

test/1_stdlib/POSIX.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// REQUIRES: executable_test
33

44
import StdlibUnittest
5-
#if os(Linux)
5+
#if os(Linux) || os(Android)
66
import Glibc
77
#else
88
import Darwin

0 commit comments

Comments
 (0)