Skip to content

Commit b42c144

Browse files
committed
Added build-android script
1 parent 82b286a commit b42c144

File tree

8 files changed

+172
-4
lines changed

8 files changed

+172
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
*~
2+
13
# Mac OS X filesystem metadata
24
.DS_Store
35

CoreFoundation/Base.subproj/CFPlatform.c

+1
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,7 @@ CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name) {
13271327
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length) {
13281328
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
13291329
return pthread_getname_np(pthread_self(), buf, length);
1330+
#elif DEPLOYMENT_TARGET_ANDROID
13301331
#elif DEPLOYMENT_TARGET_LINUX
13311332
return pthread_getname_np(pthread_self(), buf, length);
13321333
#endif

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
#include <CoreFoundation/ForFoundationOnly.h>
2828
#include <fts.h>
2929
#include <pthread.h>
30-
#include <execinfo.h>
3130

32-
#if __has_include(<malloc/malloc.h>)
33-
#include <malloc/malloc.h>
31+
#if __has_include(<execinfo.h>)
32+
#include <execinfo.h>
3433
#endif
3534

3635
#if __has_include(<malloc/malloc.h>)

CoreFoundation/URL.subproj/CFURL.c

+2
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/FileManager.swift

+4
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ open class FileManager : NSObject {
343343
This method replaces fileSystemAttributesAtPath:.
344344
*/
345345
open func attributesOfFileSystem(forPath path: String) throws -> [FileAttributeKey : Any] {
346+
#if os(Android)
347+
NSUnimplemented()
348+
#else
346349
// statvfs(2) doesn't support 64bit inode on Darwin (apfs), fallback to statfs(2)
347350
#if os(OSX) || os(iOS)
348351
var s = statfs()
@@ -371,6 +374,7 @@ open class FileManager : NSObject {
371374
result[.systemFreeNodes] = NSNumber(value: UInt64(s.f_ffree))
372375

373376
return result
377+
#endif
374378
}
375379

376380
/* createSymbolicLinkAtPath:withDestination:error: returns YES if the symbolic link that point at 'destPath' was able to be created at the location specified by 'path'. If this method returns NO, the link was unable to be created and an NSError will be returned by reference in the 'error' parameter. This method does not traverse a terminal symlink.

Foundation/Host.swift

+12-1
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) {
@@ -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

+8
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 = 0
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

build-android

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
SWIFT_PATH="$( cd "$(dirname $0)/.." && pwd )"
16+
17+
ANDROID_NDK_PATH="${ANDROID_NDK_PATH:?Please set the Android NDK path in the ANDROID_NDK_PATH environment variable}"
18+
ANDROID_ICU_PATH=${SWIFT_PATH}/libiconv-libicu-android
19+
20+
SWIFT_ANDROID_TOOLCHAIN_PATH="${SWIFT_PATH}/swift-android-toolchain"
21+
SWIFT_ANDROID_BUILD_PATH="${SWIFT_PATH}/build/Ninja-ReleaseAssert"
22+
23+
cd ${SWIFT_PATH}/swift-corelibs-foundation
24+
25+
mkdir -p .build
26+
cd .build
27+
28+
ANDROID_STANDALONE_TOOLCHAIN=`realpath ./android-standalone-toolchain`
29+
ANDROID_STANDALONE_SYSROOT=$ANDROID_STANDALONE_TOOLCHAIN/sysroot
30+
31+
if [ ! -d android-standalone-toolchain ]; then
32+
echo Creating Android standalone toolchain ...
33+
$ANDROID_NDK_PATH/build/tools/make_standalone_toolchain.py --api 21 --arch arm --stl libc++ --install-dir $ANDROID_STANDALONE_TOOLCHAIN --force -v
34+
fi
35+
36+
export PATH=$ANDROID_STANDALONE_TOOLCHAIN/bin:$PATH
37+
export CC=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-clang
38+
export CXX=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-clang++
39+
export AR=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-ar
40+
export AS=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-as
41+
export LD=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-ld
42+
export RANLIB=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-ranlib
43+
export NM=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-nm
44+
export STRIP=$ANDROID_STANDALONE_TOOLCHAIN/bin/arm-linux-androideabi-strip
45+
export CHOST=arm-linux-androideabi
46+
export ARCH_FLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
47+
export ARCH_LINK="-march=armv7-a -Wl,--fix-cortex-a8"
48+
export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing "
49+
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 "
50+
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing "
51+
export LDFLAGS=" ${ARCH_LINK} "
52+
53+
if [ ! -d curl ]; then
54+
git clone https://github.com/curl/curl.git
55+
fi
56+
if [ ! -f $ANDROID_STANDALONE_SYSROOT/usr/lib/libcurl.so ]; then
57+
pushd curl
58+
autoreconf -i
59+
./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
60+
make
61+
make install
62+
popd
63+
fi
64+
if [ ! -d libxml2 ]; then
65+
git clone git://git.gnome.org/libxml2
66+
fi
67+
if [ ! -f $ANDROID_STANDALONE_SYSROOT/usr/lib/libxml2.so ]; then
68+
pushd libxml2
69+
autoreconf -i
70+
./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
71+
make libxml2.la
72+
make install-libLTLIBRARIES
73+
pushd include
74+
make install
75+
popd
76+
popd
77+
fi
78+
if [ ! -f libFoundation.so ]; then
79+
pushd $ANDROID_STANDALONE_SYSROOT
80+
81+
# Move dispatch public and private headers to the directory foundation is expecting to get it
82+
mkdir -p $ANDROID_STANDALONE_SYSROOT/usr/include/dispatch
83+
cp $SWIFT_PATH/swift-corelibs-libdispatch/dispatch/*.h $ANDROID_STANDALONE_SYSROOT/usr/include/dispatch
84+
cp $SWIFT_PATH/swift-corelibs-libdispatch/private/*.h $ANDROID_STANDALONE_SYSROOT/usr/include/dispatch
85+
86+
pushd $SWIFT_PATH
87+
pushd swift-corelibs-foundation
88+
# Libfoundation script is not completely prepared to handle cross compilation yet.
89+
ln -sf $SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/lib/swift $ANDROID_STANDALONE_SYSROOT/usr/lib/
90+
cp $SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/lib/swift/android/armv7/* $SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/lib/swift/android/
91+
92+
# Search path for curl seems to be wrong in foundation
93+
#cp -r .build/openssl-1.0.2l/ssl $ANDROID_STANDALONE_SYSROOT/usr/include
94+
cp -r .build/curl/include/curl $ANDROID_STANDALONE_SYSROOT/usr/include
95+
if [ ! -e $ANDROID_STANDALONE_SYSROOT/usr/include/curl/curl ]; then
96+
ln -s $ANDROID_STANDALONE_SYSROOT/usr/include/curl $ANDROID_STANDALONE_SYSROOT/usr/include/curl/curl
97+
fi
98+
if [ ! -e $ANDROID_STANDALONE_SYSROOT/usr/include/libxml ]; then
99+
ln -s $ANDROID_STANDALONE_SYSROOT/usr/include/libxml2/libxml $ANDROID_STANDALONE_SYSROOT/usr/include/libxml
100+
fi
101+
102+
env \
103+
SWIFTC="$SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/bin/swiftc" \
104+
CLANG="$SWIFT_ANDROID_BUILD_PATH/llvm-linux-x86_64/bin/clang" \
105+
SWIFT="$SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/bin/swift" \
106+
SDKROOT="$SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64" \
107+
BUILD_DIR="$SWIFT_ANDROID_BUILD_PATH/foundation-linux-x86_64" \
108+
DSTROOT="/" \
109+
PREFIX="/usr" \
110+
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" \
111+
SWIFTCFLAGS="-DDEPLOYMENT_TARGET_ANDROID -DDEPLOYMENT_ENABLE_LIBDISPATCH -I$ANDROID_NDK_PATH/platforms/android-21/arch-arm/usr/include" \
112+
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 " \
113+
./configure \
114+
Release \
115+
--target=armv7-none-linux-androideabi \
116+
--sysroot=$ANDROID_STANDALONE_SYSROOT \
117+
-DXCTEST_BUILD_DIR=$SWIFT_ANDROID_BUILD_PATH/xctest-linux-x86_64 \
118+
-DLIBDISPATCH_SOURCE_DIR=$SWIFT_PATH/swift-corelibs-libdispatch \
119+
-DLIBDISPATCH_BUILD_DIR=$SWIFT_PATH/swift-corelibs-libdispatch &&
120+
121+
cp -r /usr/include/uuid $ANDROID_STANDALONE_SYSROOT/usr/include
122+
sed -i~ "s/-I.\/ -I\/usr\/include\/x86_64-linux-gnu -I\/usr\/include\/x86_64-linux-gnu -I\/usr\/include\/libxml2//" build.ninja
123+
sed -i~ "s/-licui18n/-licui18nswift/g" build.ninja
124+
sed -i~ "s/-licuuc/-licuucswift/g" build.ninja
125+
sed -i~ "s/-licudata/-licudataswift/g" build.ninja
126+
127+
ninja
128+
129+
# There's no installation script for foundation yet, so the installation needs to be done manually.
130+
# Apparently the installation for the main script is in swift repo.
131+
rsync -av $SWIFT_ANDROID_BUILD_PATH/foundation-linux-x86_64/Foundation/Foundation.swift* $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift/android/armv7/
132+
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
133+
134+
# prep install so it can be used to build foundation
135+
cp -r $SWIFT_PATH/swift-corelibs-foundation/.build/libxml2/include/libxml $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift
136+
cp -r $SWIFT_PATH/swift-corelibs-foundation/.build/curl/include/curl $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift
137+
popd
138+
popd
139+
popd
140+
fi
141+

0 commit comments

Comments
 (0)