Skip to content

Commit 2573116

Browse files
committed
Added build-android script
1 parent 6d862d6 commit 2573116

File tree

8 files changed

+173
-6
lines changed

8 files changed

+173
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ project.xcworkspace
1010

1111
# build files generated by the configure script
1212
*.ninja
13+
*.ninja~
1314
.ninja_deps
1415
.ninja_log
1516

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,10 +1325,12 @@ CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name) {
13251325
}
13261326

13271327
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length) {
1328+
#if !TARGET_OS_ANDROID
13281329
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
13291330
return pthread_getname_np(pthread_self(), buf, length);
13301331
#elif DEPLOYMENT_TARGET_LINUX
13311332
return pthread_getname_np(pthread_self(), buf, length);
1333+
#endif
13321334
#endif
13331335
return -1;
13341336
}

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
#include <CoreFoundation/ForFoundationOnly.h>
2828
#include <fts.h>
2929
#include <pthread.h>
30+
31+
#if !TARGET_OS_ANDROID
3032
#include <execinfo.h>
33+
#endif
3134

3235
#if __has_include(<malloc/malloc.h>)
3336
#include <malloc/malloc.h>

CoreFoundation/URL.subproj/CFURL.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
#if __has_include(<sys/syslog.h>)
2929
#include <sys/syslog.h>
3030
#else
31+
#if !TARGET_OS_ANDROID
3132
#include <syslog.h>
3233
#endif
34+
#endif
3335
#include <CoreFoundation/CFURLPriv.h>
3436
#endif
3537

Foundation/Host.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ open class Host: NSObject {
2828
internal var _names = [String]()
2929
internal var _addresses = [String]()
3030

31+
#if os(Android)
32+
static internal let NI_MAXHOST = 1025
33+
#endif
34+
3135
static internal let _current = Host(currentHostName(), .current)
3236

3337
internal init(_ info: String?, _ type: ResolveType) {
@@ -36,7 +40,7 @@ open class Host: NSObject {
3640
}
3741

3842
static internal func currentHostName() -> String {
39-
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
43+
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
4044
defer {
4145
hname.deinitialize()
4246
hname.deallocate(capacity: Int(NI_MAXHOST))
@@ -65,6 +69,9 @@ open class Host: NSObject {
6569
}
6670

6771
internal func _resolveCurrent() {
72+
#if os(Android)
73+
return
74+
#else
6875
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
6976
if getifaddrs(&ifaddr) != 0 {
7077
return
@@ -88,9 +95,13 @@ open class Host: NSObject {
8895
}
8996
ifa = ifaValue.ifa_next
9097
}
98+
#endif
9199
}
92100

93101
internal func _resolve() {
102+
#if os(Android)
103+
return
104+
#else
94105
if _resolved {
95106
return
96107
}
@@ -148,7 +159,7 @@ open class Host: NSObject {
148159
res = info.ai_next
149160
}
150161
}
151-
162+
#endif
152163
}
153164

154165
open var name: String? {

Foundation/Thread.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,11 @@ open class Thread : NSObject {
256256
let maxSupportedStackDepth = 128;
257257
let addrs = UnsafeMutablePointer<UnsafeMutableRawPointer?>.allocate(capacity: maxSupportedStackDepth)
258258
defer { addrs.deallocate(capacity: maxSupportedStackDepth) }
259+
#if os(Android)
260+
let count = maxSupportedStackDepth
261+
#else
259262
let count = backtrace(addrs, Int32(maxSupportedStackDepth))
263+
#endif
260264
let addressCount = max(0, min(Int(count), maxSupportedStackDepth))
261265
return body(addrs, addressCount)
262266
}
@@ -270,6 +274,9 @@ open class Thread : NSObject {
270274
}
271275

272276
open class var callStackSymbols: [String] {
277+
#if os(Android)
278+
return []
279+
#else
273280
return backtraceAddresses({ (addrs, count) in
274281
var symbols: [String] = []
275282
if let bs = backtrace_symbols(addrs, Int32(count)) {
@@ -283,6 +290,7 @@ open class Thread : NSObject {
283290
}
284291
return symbols
285292
})
293+
#endif
286294
}
287295
}
288296

TestFoundation/TestNSLock.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,11 @@ class TestNSLock: XCTestCase {
4141
}
4242

4343
let thread = Thread() {
44-
lock.lock()
45-
46-
// Now wake up the main thread so it can try to obtain the lock that
47-
// this thread just obtained.
4844
condition.lock()
4945
condition.signal()
5046
condition.unlock()
5147

48+
lock.lock()
5249
Thread.sleep(forTimeInterval: 8)
5350
lock.unlock()
5451
}

build-android

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env bash
2+
#
3+
# build-android
4+
#
5+
# This source file is part of the Swift.org open source project
6+
#
7+
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
8+
# Licensed under Apache License v2.0 with Runtime Library Exception
9+
#
10+
# See https://swift.org/LICENSE.txt for license information
11+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
12+
13+
set -e
14+
15+
cd "$(dirname $0)/.." || exit 1
16+
SWIFT_PATH=$PWD
17+
18+
ANDROID_NDK_PATH="${ANDROID_NDK_PATH:?Please set the Android NDK path in the ANDROID_NDK_PATH environment variable}"
19+
ANDROID_ICU_PATH=${SWIFT_PATH}/libiconv-libicu-android
20+
21+
SWIFT_ANDROID_TOOLCHAIN_PATH="${SWIFT_PATH}/swift-android-toolchain"
22+
SWIFT_ANDROID_BUILD_PATH="${SWIFT_PATH}/build/Ninja-ReleaseAssert"
23+
24+
cd ${SWIFT_PATH}/swift-corelibs-foundation
25+
26+
mkdir -p .build
27+
cd .build
28+
29+
ANDROID_STANDALONE_TOOLCHAIN=`realpath ./android-standalone-toolchain`
30+
ANDROID_STANDALONE_SYSROOT=$ANDROID_STANDALONE_TOOLCHAIN/sysroot
31+
32+
if [ ! -d android-standalone-toolchain ]; then
33+
echo Creating Android standalone toolchain ...
34+
$ANDROID_NDK_PATH/build/tools/make_standalone_toolchain.py --api 21 --arch arm --stl libc++ --install-dir $ANDROID_STANDALONE_TOOLCHAIN --force -v
35+
fi
36+
37+
export PATH=$ANDROID_STANDALONE_TOOLCHAIN/bin:$PATH
38+
export CC=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-clang
39+
export CXX=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-clang++
40+
export AR=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-ar
41+
export AS=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-as
42+
export LD=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-ld
43+
export RANLIB=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-ranlib
44+
export NM=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-nm
45+
export STRIP=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-strip
46+
export CHOST=arm-linux-androideabi
47+
export ARCH_FLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
48+
export ARCH_LINK="-march=armv7-a -Wl,--fix-cortex-a8"
49+
export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing "
50+
export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -frtti -fexceptions -std=c++11 -Wno-error=unused-command-line-argument "
51+
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing "
52+
export LDFLAGS=" ${ARCH_LINK} "
53+
54+
if [ ! -d curl ]; then
55+
git clone https://github.com/curl/curl.git
56+
fi
57+
if [ ! -f $ANDROID_STANDALONE_SYSROOT/usr/lib/libcurl.so ]; then
58+
pushd curl
59+
autoreconf -i
60+
./configure --host=arm-linux-androideabi --enable-shared --disable-static --disable-dependency-tracking --with-zlib=$ANDROID_STANDALONE_SYSROOT/usr --with-ssl=$ANDROID_STANDALONE_SYSROOT/usr --without-ca-bundle --without-ca-path --enable-ipv6 --enable-http --enable-ftp --disable-file --disable-ldap --disable-ldaps --disable-rtsp --disable-proxy --disable-dict --disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smtp --disable-gopher --disable-sspi --disable-manual --target=arm-linux-androideabi --build=x86_64-unknown-linux-gnu --prefix=$ANDROID_STANDALONE_SYSROOT/usr
61+
make
62+
make install
63+
popd
64+
fi
65+
if [ ! -d libxml2 ]; then
66+
git clone git://git.gnome.org/libxml2
67+
fi
68+
if [ ! -f $ANDROID_STANDALONE_SYSROOT/usr/lib/libxml2.so ]; then
69+
pushd libxml2
70+
autoreconf -i
71+
./configure --with-sysroot=$ANDROID_STANDALONE_SYSROOT --with-zlib=$ANDROID_STANDALONE_SYSROOT/usr --prefix=$ANDROID_STANDALONE_SYSROOT/usr --host=$CHOST --without-lzma --disable-static --enable-shared --without-http --without-html --without-ftp
72+
make libxml2.la || exit 1
73+
make install-libLTLIBRARIES || exit 1
74+
pushd include
75+
make install
76+
popd
77+
popd
78+
fi
79+
if [ ! -f libFoundation.so ]; then
80+
pushd $ANDROID_STANDALONE_SYSROOT
81+
82+
# Move dispatch public and private headers to the directory foundation is expecting to get it
83+
mkdir -p $ANDROID_STANDALONE_SYSROOT/usr/include/dispatch
84+
cp $SWIFT_PATH/swift-corelibs-libdispatch/dispatch/*.h $ANDROID_STANDALONE_SYSROOT/usr/include/dispatch
85+
cp $SWIFT_PATH/swift-corelibs-libdispatch/private/*.h $ANDROID_STANDALONE_SYSROOT/usr/include/dispatch
86+
87+
pushd $SWIFT_PATH
88+
pushd swift-corelibs-foundation
89+
# Libfoundation script is not completely prepared to handle cross compilation yet.
90+
ln -sf $SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/lib/swift $ANDROID_STANDALONE_SYSROOT/usr/lib/
91+
cp $SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/lib/swift/android/armv7/* $SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/lib/swift/android/
92+
93+
# Search path for curl seems to be wrong in foundation
94+
#cp -r .build/openssl-1.0.2l/ssl $ANDROID_STANDALONE_SYSROOT/usr/include
95+
cp -r .build/curl/include/curl $ANDROID_STANDALONE_SYSROOT/usr/include
96+
if [ ! -e $ANDROID_STANDALONE_SYSROOT/usr/include/curl/curl ]; then
97+
ln -s $ANDROID_STANDALONE_SYSROOT/usr/include/curl $ANDROID_STANDALONE_SYSROOT/usr/include/curl/curl
98+
fi
99+
if [ ! -e $ANDROID_STANDALONE_SYSROOT/usr/include/libxml ]; then
100+
ln -s $ANDROID_STANDALONE_SYSROOT/usr/include/libxml2/libxml $ANDROID_STANDALONE_SYSROOT/usr/include/libxml
101+
fi
102+
103+
env \
104+
SWIFTC="$SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/bin/swiftc" \
105+
CLANG="$SWIFT_ANDROID_BUILD_PATH/llvm-linux-x86_64/bin/clang" \
106+
SWIFT="$SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/bin/swift" \
107+
SDKROOT="$SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64" \
108+
BUILD_DIR="$SWIFT_ANDROID_BUILD_PATH/foundation-linux-x86_64" \
109+
DSTROOT="/" \
110+
PREFIX="/usr" \
111+
CFLAGS="-DDEPLOYMENT_TARGET_ANDROID -DDEPLOYMENT_ENABLE_LIBDISPATCH --sysroot=$ANDROID_NDK_PATH/platforms/android-21/arch-arm -I$ANDROID_ICU_PATH/armeabi-v7a/include -I${SDKROOT}/lib/swift -I$ANDROID_NDK_PATH/sources/android/support/include -I$ANDROID_STANDALONE_SYSROOT/usr/include -I$SWIFT_PATH/swift-corelibs-foundation/closure" \
112+
SWIFTCFLAGS="-DDEPLOYMENT_TARGET_ANDROID -DDEPLOYMENT_ENABLE_LIBDISPATCH -I$ANDROID_NDK_PATH/platforms/android-21/arch-arm/usr/include" \
113+
LDFLAGS="-fuse-ld=gold --sysroot=$ANDROID_NDK_PATH/platforms/android-21/arch-arm -L$ANDROID_NDK_PATH/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x -L$ANDROID_ICU_PATH/armeabi-v7a -L$ANDROID_STANDALONE_SYSROOT/usr/lib -ldispatch " \
114+
./configure \
115+
Release \
116+
--target=armv7-none-linux-androideabi \
117+
--sysroot=$ANDROID_STANDALONE_SYSROOT \
118+
-DXCTEST_BUILD_DIR=$SWIFT_ANDROID_BUILD_PATH/xctest-linux-x86_64 \
119+
-DLIBDISPATCH_SOURCE_DIR=$SWIFT_PATH/swift-corelibs-libdispatch \
120+
-DLIBDISPATCH_BUILD_DIR=$SWIFT_PATH/swift-corelibs-libdispatch &&
121+
122+
cp -r /usr/include/uuid $ANDROID_STANDALONE_SYSROOT/usr/include
123+
sed -i~ "s/-I.\/ -I\/usr\/include\/x86_64-linux-gnu -I\/usr\/include\/x86_64-linux-gnu -I\/usr\/include\/libxml2//" build.ninja
124+
sed -i~ "s/-licui18n/-licui18nswift/g" build.ninja
125+
sed -i~ "s/-licuuc/-licuucswift/g" build.ninja
126+
sed -i~ "s/-licudata/-licudataswift/g" build.ninja
127+
128+
ninja || exit 1
129+
130+
# There's no installation script for foundation yet, so the installation needs to be done manually.
131+
# Apparently the installation for the main script is in swift repo.
132+
rsync -av $SWIFT_ANDROID_BUILD_PATH/foundation-linux-x86_64/Foundation/Foundation.swift* $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift/android/armv7/
133+
rsync -av $ANDROID_STANDALONE_SYSROOT/usr/lib/libxml2.* $ANDROID_STANDALONE_SYSROOT/usr/lib/libcurl.* $ANDROID_ICU_PATH/armeabi-v7a/libicu{uc,i18n,data}swift.so $ANDROID_NDK_PATH/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so $SWIFT_ANDROID_BUILD_PATH/foundation-linux-x86_64/Foundation/libFoundation.so $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift/android/armv7
134+
135+
# prep install so it can be used to build foundation
136+
cp -r $SWIFT_PATH/swift-corelibs-foundation/.build/libxml2/include/libxml $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift
137+
cp -r $SWIFT_PATH/swift-corelibs-foundation/.build/curl/include/curl $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift
138+
popd
139+
popd
140+
141+
popd
142+
fi
143+

0 commit comments

Comments
 (0)