Skip to content

Commit deaa565

Browse files
Adds options to configure in a similar fashion and with similar
constraints to the options added in `swift` to add Android Support. This will allow initially those features to be just passed from `utils/build-script` without any change until a full strategy for cross-compilation in all the involved projects is defined. This is an example call to build for Android with Swift support: ``` env \ CC="${swift_android_path}/build/Ninja-ReleaseAssert/llvm-linux-x86_64/bi n/clang" \ CXX="${swift_android_path}/build/Ninja-ReleaseAssert/llvm-linux-x86_64/b in/clang++" \ SWIFTC="${swift_android_path}/build/Ninja-ReleaseAssert/swift-linux-x86_ 64/bin/swiftc" \ ${swift_android_path}/swift-corelibs-libdispatch/configure \ --with-swift-toolchain=“${swift_android_path}/build/Ninja-ReleaseAssert/ swift-linux-x86_64/" \ --with-build-variant=release \ --enable-android \ --host=arm-linux-androideabi \ --with-android-ndk=${ndk_path} \ --with-android-api-level=21 \ --disable-build-tests ```
1 parent 548a1b9 commit deaa565

20 files changed

+167
-18
lines changed

Makefile.am

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ if BUILD_OWN_KQUEUES
1212
MAYBE_KQUEUES = libkqueue
1313
endif
1414

15+
if BUILD_TESTS
16+
MAYBE_TESTS = tests
17+
endif
18+
1519
SUBDIRS= \
1620
dispatch \
1721
$(MAYBE_PTHREAD_WORKQUEUES) \
@@ -20,7 +24,7 @@ SUBDIRS= \
2024
os \
2125
private \
2226
src \
23-
tests
27+
$(MAYBE_TESTS)
2428

2529
EXTRA_DIST= \
2630
README.md \

configure.ac

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ AC_CONFIG_MACRO_DIR([m4])
1111
ac_clean_files=a.out.dSYM
1212
AM_MAINTAINER_MODE
1313

14+
AC_CANONICAL_BUILD
15+
AC_CANONICAL_HOST
16+
AC_CANONICAL_TARGET
17+
1418
#
1519
# Command line argument to specify build variant (default to release).
1620
# Impacts default value of CFLAGS et al. so must come before AC_PROG_CC
@@ -56,6 +60,53 @@ AC_PROG_CXX([clang++ g++ c++])
5660
AC_PROG_OBJC([clang gcc cc])
5761
AC_PROG_OBJCXX([clang++ g++ c++])
5862

63+
#
64+
# Android cross-compilation support
65+
#
66+
AC_ARG_WITH([android-ndk],
67+
[AS_HELP_STRING([--with-android-ndk],
68+
[Android NDK location])], [
69+
android_ndk=${withval}
70+
])
71+
AC_ARG_WITH([android-ndk-gcc-version],
72+
[AS_HELP_STRING([--with-android-ndk-gcc-version],
73+
[Android NDK GCC version [defaults=4.9]])],
74+
[android_ndk_gcc_version=${withval}], [android_ndk_gcc_version=4.9])
75+
AC_ARG_WITH([android-api-level],
76+
[AS_HELP_STRING([--with-android-api-level],
77+
[Android API level to link with])], [
78+
android_api_level=${withval}
79+
])
80+
AC_ARG_ENABLE([android],
81+
[AS_HELP_STRING([--enable-android],
82+
[Compile for Android])], [
83+
android=true
84+
85+
# Override values until there's real support for multiple Android platforms
86+
host=armv7-none-linux-androideabi
87+
host_alias=arm-linux-androideabi
88+
host_cpu=armv7
89+
host_os=linux-androideabi
90+
host_vendor=unknown
91+
arch=arm
92+
93+
sysroot=${android_ndk}/platforms/android-${android_api_level}/arch-${arch}
94+
toolchain=${android_ndk}/toolchains/${host_alias}-${android_ndk_gcc_version}/prebuilt/linux-${build_cpu}
95+
96+
CFLAGS="$CFLAGS -target ${host_alias} --sysroot=${sysroot} -B${toolchain}/${host_alias}/bin"
97+
CXXFLAGS="$CXXFLAGS -target ${host_alias} --sysroot=${sysroot} -B${toolchain}/${host_alias}/bin"
98+
SWIFTC_FLAGS="-target ${host} -sdk ${sysroot} -L${toolchain}/lib/gcc/${host_alias}/${android_ndk_gcc_version}.x"
99+
LIBS="$LIBS -L${toolchain}/lib/gcc/${host_alias}/${android_ndk_gcc_version}.x"
100+
LDFLAGS="$LDFLAGS -Wc,'-target','${host_alias}','-B${toolchain}/${host_alias}/bin'"
101+
102+
# FIXME: empty CFLAGS and CXXFLAGS are assumed for this to work.
103+
# FIXME: there should be a more elegant way to do this
104+
ac_configure_args=`echo $ac_configure_args | sed -e "s/ 'CFLAGS='//" -e "s/ 'CXXFLAGS='//"`
105+
# CFLAGS, CXXFLAGS and LIBS needs to be passed to libkqueue and libpwq
106+
ac_configure_args="$ac_configure_args --enable-bionic-libc 'CFLAGS=$CFLAGS' 'CXXFLAGS=$CXXFLAGS' 'LIBS=$LIBS'"
107+
], [android=false])
108+
AM_CONDITIONAL(ANDROID, $android)
109+
59110
#
60111
# On Mac OS X, some required header files come from other source packages;
61112
# allow specifying where those are.
@@ -134,8 +185,6 @@ AS_IF([test "x$enable_apple_tsd_optimizations" = "xyes"],
134185
[Define to use non-portable pthread TSD optimizations for Mac OS X)])]
135186
)
136187

137-
AC_CANONICAL_TARGET
138-
139188
#
140189
# Enable building Swift overlay support into libdispatch
141190
#
@@ -164,6 +213,7 @@ AC_ARG_WITH([swift-toolchain],
164213
)
165214
AM_CONDITIONAL(HAVE_SWIFT, $have_swift)
166215
AC_SUBST([SWIFTC])
216+
AC_SUBST([SWIFTC_FLAGS])
167217
AC_SUBST([SWIFT_LIBDIR])
168218

169219
#
@@ -473,6 +523,13 @@ AC_COMPILE_IFELSE(
473523
[AC_DEFINE(HAVE_NORETURN_BUILTIN_TRAP, 1, [Define if __builtin_trap marked noreturn])]
474524
)
475525

526+
#
527+
# Add option to avoid building tests
528+
#
529+
AC_ARG_ENABLE([build-tests],
530+
[AS_HELP_STRING([--disable-build-tests], [Disable tests compilation])])
531+
AM_CONDITIONAL(BUILD_TESTS, [test "x$enable_build_tests" != "xno"])
532+
476533
#
477534
# Generate Makefiles.
478535
#

src/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ libdispatch_la_SOURCES= \
4141
trace.h \
4242
voucher_internal.h \
4343
firehose/firehose_internal.h \
44+
shims/android_stubs.h \
4445
shims/atomic.h \
4546
shims/atomic_sfb.h \
4647
shims/getprogname.h \
@@ -148,7 +149,7 @@ SWIFT_SRC_FILES=\
148149
SWIFT_ABS_SRC_FILES = $(SWIFT_SRC_FILES:%=$(abs_srcdir)/%)
149150
SWIFT_OBJ_FILES = $(abs_builddir)/swift/swift_overlay.o
150151

151-
SWIFTC_FLAGS = -Xcc -fmodule-map-file=$(abs_top_srcdir)/dispatch/module.modulemap -I$(abs_top_srcdir) -Xcc -fblocks
152+
SWIFTC_FLAGS+= -Xcc -fmodule-map-file=$(abs_top_srcdir)/dispatch/module.modulemap -I$(abs_top_srcdir) -Xcc -fblocks
152153
if DISPATCH_ENABLE_OPTIMIZATION
153154
SWIFTC_FLAGS+=-O
154155
endif
@@ -180,4 +181,3 @@ BUILT_SOURCES=$(MIG_SOURCES) $(DTRACE_SOURCES)
180181
nodist_libdispatch_la_SOURCES=$(BUILT_SOURCES)
181182
CLEANFILES=$(BUILT_SOURCES) $(SWIFT_GEN_FILES)
182183
DISTCLEANFILES=pthread_machdep.h pthread System mach objc
183-

src/internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
#endif
156156

157157
/* private.h must be included last to avoid picking up installed headers. */
158+
#include <pthread.h>
158159
#include "os/object_private.h"
159160
#include "queue_private.h"
160161
#include "source_private.h"
@@ -245,7 +246,11 @@ DISPATCH_EXPORT DISPATCH_NOTHROW void dispatch_atfork_child(void);
245246
#include <sys/event.h>
246247
#include <sys/mount.h>
247248
#include <sys/queue.h>
249+
#ifdef __ANDROID__
250+
#include <linux/sysctl.h>
251+
#else
248252
#include <sys/sysctl.h>
253+
#endif /* __ANDROID__ */
249254
#include <sys/socket.h>
250255
#include <sys/time.h>
251256
#include <sys/mman.h>

src/queue.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ _dispatch_get_mach_host_port(void)
976976
#include <unistd.h>
977977
#include <sys/syscall.h>
978978

979+
#ifndef __ANDROID__
979980
#ifdef SYS_gettid
980981
DISPATCH_ALWAYS_INLINE
981982
static inline pid_t
@@ -985,7 +986,8 @@ gettid(void)
985986
}
986987
#else
987988
#error "SYS_gettid unavailable on this system"
988-
#endif
989+
#endif /* SYS_gettid */
990+
#endif /* ! __ANDROID__ */
989991

990992
#define _tsd_call_cleanup(k, f) do { \
991993
if ((f) && tsd->k) ((void(*)(void*))(f))(tsd->k); \

src/shims.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ typedef unsigned long pthread_priority_t;
9797
#include "shims/linux_stubs.h"
9898
#endif
9999

100+
#ifdef __ANDROID__
101+
#include "shims/android_stubs.h"
102+
#endif
103+
100104
typedef uint32_t dispatch_priority_t;
101105
#define DISPATCH_SATURATED_OVERRIDE ((dispatch_priority_t)UINT32_MAX)
102106

src/shims/android_stubs.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This source file is part of the Swift.org open source project
3+
*
4+
* Copyright (c) 2015 Apple Inc. and the Swift project authors
5+
*
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+
// forward declarations for functions we are stubbing out
14+
// in the intial android port.
15+
16+
#ifndef __DISPATCH__ANDROID__STUBS__INTERNAL
17+
#define __DISPATCH__ANDROID__STUBS__INTERNAL
18+
19+
/*
20+
* Missing sys/queue.h macro stubs
21+
*/
22+
23+
#ifndef TAILQ_FOREACH_SAFE
24+
# define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
25+
for ((var) = TAILQ_FIRST((head)); \
26+
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
27+
(var) = (tvar))
28+
#endif /* TAILQ_FOREACH_SAFE */
29+
30+
#ifndef TRASHIT
31+
# define TRASHIT(x) do {(x) = (void *)-1;} while (0)
32+
#endif /* TRASHIT */
33+
34+
#endif /* __DISPATCH__ANDROID__STUBS__INTERNAL */

src/shims/getprogname.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@
2323
#define __DISPATCH_SHIMS_GETPROGNAME__
2424

2525
#if !HAVE_GETPROGNAME
26+
27+
#ifdef __ANDROID__
28+
extern const char *__progname;
29+
#endif /* __ANDROID */)
30+
2631
static inline char *
2732
getprogname(void)
2833
{
2934
# if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
3035
return program_invocation_short_name;
36+
# elif defined(__ANDROID__)
37+
return __progname;
3138
# else
3239
# error getprogname(3) is not available on this platform
3340
# endif

src/shims/linux_stubs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
*/
1818

1919
#include <stdint.h>
20+
#ifdef __ANDROID__
21+
#include <sys/syscall.h>
22+
#else
2023
#include <syscall.h>
24+
#endif /* __ANDROID__ */
2125

2226
#if __has_include(<config/config_ac.h>)
2327
#include <config/config_ac.h>

src/shims/linux_stubs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ typedef void (*dispatch_mach_msg_destructor_t)(void*);
7171
#endif
7272

7373
// SIZE_T_MAX should not be hardcoded like this here.
74-
#define SIZE_T_MAX (0x7fffffff)
74+
#ifndef SIZE_T_MAX
75+
#define SIZE_T_MAX (~(size_t)0)
76+
#endif
7577

7678
// Define to 0 the NOTE_ values that are not present on Linux.
7779
// Revisit this...would it be better to ifdef out the uses instead??

src/shims/lock.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ _dispatch_unfair_lock_wake(uint32_t *uaddr, uint32_t flags)
117117
#pragma mark - futex wrappers
118118
#if HAVE_FUTEX
119119
#include <sys/time.h>
120+
#ifdef __ANDROID__
121+
#include <sys/syscall.h>
122+
#else
120123
#include <syscall.h>
124+
#endif /* __ANDROID__ */
121125

122126
DISPATCH_ALWAYS_INLINE
123127
static inline int

src/source.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2034,8 +2034,10 @@ _dispatch_kevent_qos_s _dispatch_kevent_timeout[] = {
20342034
};
20352035
#define DISPATCH_KEVENT_TIMEOUT_COUNT \
20362036
((sizeof(_dispatch_kevent_timeout) / sizeof(_dispatch_kevent_timeout[0])))
2037-
static_assert(DISPATCH_KEVENT_TIMEOUT_COUNT == DISPATCH_TIMER_INDEX_COUNT - 1,
2037+
#if __has_feature(c_static_assert)
2038+
_Static_assert(DISPATCH_KEVENT_TIMEOUT_COUNT == DISPATCH_TIMER_INDEX_COUNT - 1,
20382039
"should have a kevent for everything but disarm (ddt assumes this)");
2040+
#endif
20392041

20402042
#define DISPATCH_KEVENT_COALESCING_WINDOW_INIT(qos, ms) \
20412043
[DISPATCH_TIMER_QOS_##qos] = 2ull * (ms) * NSEC_PER_MSEC

src/swift/Source.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public extension DispatchSource {
112112
}
113113
#endif
114114

115-
#if !os(Linux)
115+
#if !os(Linux) && !os(Android)
116116
public struct ProcessEvent : OptionSet, RawRepresentable {
117117
public let rawValue: UInt
118118
public init(rawValue: UInt) { self.rawValue = rawValue }
@@ -170,7 +170,7 @@ public extension DispatchSource {
170170
}
171171
#endif
172172

173-
#if !os(Linux)
173+
#if !os(Linux) && !os(Android)
174174
public class func makeProcessSource(identifier: pid_t, eventMask: ProcessEvent, queue: DispatchQueue? = nil) -> DispatchSourceProcess {
175175
let source = dispatch_source_create(_swift_dispatch_source_type_proc(), UInt(identifier), eventMask.rawValue, queue?.__wrapped)
176176
return DispatchSource(source: source) as DispatchSourceProcess
@@ -202,7 +202,7 @@ public extension DispatchSource {
202202
return DispatchSource(source: source) as DispatchSourceUserDataOr
203203
}
204204

205-
#if !os(Linux)
205+
#if !os(Linux) && !os(Android)
206206
public class func makeFileSystemObjectSource(fileDescriptor: Int32, eventMask: FileSystemEvent, queue: DispatchQueue? = nil) -> DispatchSourceFileSystemObject {
207207
let source = dispatch_source_create(_swift_dispatch_source_type_vnode(), UInt(fileDescriptor), eventMask.rawValue, queue?.__wrapped)
208208
return DispatchSource(source: source) as DispatchSourceFileSystemObject
@@ -255,7 +255,7 @@ public extension DispatchSourceMemoryPressure {
255255
}
256256
#endif
257257

258-
#if !os(Linux)
258+
#if !os(Linux) && !os(Android)
259259
public extension DispatchSourceProcess {
260260
public var handle: pid_t {
261261
return pid_t(dispatch_source_get_handle(self as! DispatchSource))
@@ -299,7 +299,7 @@ public extension DispatchSourceTimer {
299299
}
300300
}
301301

302-
#if !os(Linux)
302+
#if !os(Linux) && !os(Android)
303303
public extension DispatchSourceFileSystemObject {
304304
public var handle: Int32 {
305305
return Int32(dispatch_source_get_handle((self as! DispatchSource).__wrapped))
@@ -368,7 +368,7 @@ internal func _swift_dispatch_source_type_mach_recv() -> dispatch_source_type_t
368368
internal func _swift_dispatch_source_type_memorypressure() -> dispatch_source_type_t
369369
#endif
370370

371-
#if !os(Linux)
371+
#if !os(Linux) && !os(Android)
372372
@_silgen_name("_swift_dispatch_source_type_PROC")
373373
internal func _swift_dispatch_source_type_proc() -> dispatch_source_type_t
374374
#endif
@@ -382,7 +382,7 @@ internal func _swift_dispatch_source_type_signal() -> dispatch_source_type_t
382382
@_silgen_name("_swift_dispatch_source_type_TIMER")
383383
internal func _swift_dispatch_source_type_timer() -> dispatch_source_type_t
384384

385-
#if !os(Linux)
385+
#if !os(Linux) && !os(Android)
386386
@_silgen_name("_swift_dispatch_source_type_VNODE")
387387
internal func _swift_dispatch_source_type_vnode() -> dispatch_source_type_t
388388
#endif

src/swift/Wrapper.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ extension DispatchSource : DispatchSourceMachSend,
180180
}
181181
#endif
182182

183-
#if !os(Linux)
183+
#if !os(Linux) && !os(Android)
184184
extension DispatchSource : DispatchSourceProcess,
185185
DispatchSourceFileSystemObject {
186186
}
@@ -268,7 +268,7 @@ public protocol DispatchSourceMemoryPressure : DispatchSourceProtocol {
268268
}
269269
#endif
270270

271-
#if !os(Linux)
271+
#if !os(Linux) && !os(Android)
272272
public protocol DispatchSourceProcess : DispatchSourceProtocol {
273273
var handle: pid_t { get }
274274

@@ -298,7 +298,7 @@ public protocol DispatchSourceTimer : DispatchSourceProtocol {
298298
func scheduleRepeating(wallDeadline: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval)
299299
}
300300

301-
#if !os(Linux)
301+
#if !os(Linux) && !os(Android)
302302
public protocol DispatchSourceFileSystemObject : DispatchSourceProtocol {
303303
var handle: Int32 { get }
304304

tests/Foundation/bench.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020

2121
#include <Foundation/Foundation.h>
2222
#include <libkern/OSAtomic.h>
23+
#ifdef __ANDROID__
24+
#include <linux/sysctl.h>
25+
#else
2326
#include <sys/sysctl.h>
27+
#endif /* __ANDROID__ */
2428
#include <mach/mach.h>
2529
#include <mach/mach_time.h>
2630
#include <stdio.h>

0 commit comments

Comments
 (0)