diff --git a/.gitignore b/.gitignore index aa26514c9..872120bfb 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ Build # build files generated by autotools Makefile Makefile.in +Makefilee config.log configure aclocal.m4 @@ -31,3 +32,9 @@ libtool .dirstamp /dispatch/module.modulemap /private/module.modulemap + +*.cmake +CMakeCache.txt +CMakeFiles +*.so +*.a diff --git a/INSTALL.md b/INSTALL.md index a426bcf30..23032d568 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -168,3 +168,16 @@ to get the gold linker. ninja install ``` +### Building for Android + +To build the Swift Android toolchain including libdispatch, run: + +``` +swift/utils/android$ ./build-toolchain +``` + +Which will call at the end: + +``` +swift-corelibs-libdispatch$ ./build-android +``` diff --git a/build-android b/build-android new file mode 100755 index 000000000..818cdd137 --- /dev/null +++ b/build-android @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# +# build-android +# +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors + +set -e + +cd "$(dirname $0)/.." || exit 1 +SWIFT_PATH=$PWD + +ANDROID_NDK_PATH="${ANDROID_NDK_PATH:?Please set the Android NDK path in the ANDROID_NDK_PATH environment variable}" +SWIFT_ANDROID_BUILD_PATH="${SWIFT_PATH}/build/Ninja-ReleaseAssert" +SWIFT_ANDROID_TOOLCHAIN_PATH="${SWIFT_PATH}/swift-android-toolchain" + +cd ${SWIFT_PATH}/swift-corelibs-libdispatch +sh autogen.sh +env \ + CC="$SWIFT_ANDROID_BUILD_PATH/llvm-linux-x86_64/bin/clang" \ + CXX="$SWIFT_ANDROID_BUILD_PATH/llvm-linux-x86_64/bin/clang++" \ + SWIFTC="$SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/bin/swiftc" \ + CFLAGS="-I$ANDROID_NDK_PATH/sysroot/usr/include" \ + CPPFLAGS="-I$ANDROID_NDK_PATH/sysroot/usr/include -DTARGET_OS_EMBEDDED" \ + ./configure \ + --with-swift-toolchain="$SWIFT_ANDROID_TOOLCHAIN_PATH/usr" \ + --with-build-variant=release \ + --enable-android \ + --host=arm-linux-androideabi \ + --with-android-ndk=$ANDROID_NDK_PATH \ + --with-android-api-level=21 \ + --disable-build-tests \ + --prefix=$SWIFT_ANDROID_TOOLCHAIN_PATH/usr + +make + +cp ${SWIFT_PATH}/swift-corelibs-libdispatch/src/.libs/libdispatch.so $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift/android/armv7 +cp ${SWIFT_PATH}/swift-corelibs-libdispatch/src/swift/Dispatch.{swiftmodule,swiftdoc} $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift/android/armv7 + diff --git a/build-android-cmake b/build-android-cmake new file mode 100755 index 000000000..db7fafee0 --- /dev/null +++ b/build-android-cmake @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# +# build-android +# +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors + +set -e + +cd "$(dirname $0)/.." || exit 1 +SWIFT_PATH=$PWD + +ANDROID_NDK_PATH="${ANDROID_NDK_PATH:?Please set the Android NDK path in the ANDROID_NDK_PATH environment variable}" +SWIFT_ANDROID_BUILD_PATH="${SWIFT_PATH}/build/Ninja-ReleaseAssert" +SWIFT_ANDROID_TOOLCHAIN_PATH="${SWIFT_PATH}/swift-android-toolchain" + +cd ${SWIFT_PATH}/swift-corelibs-libdispatch + +cmake \ + -DBUILD_SHARED_LIBS=OFF \ + -DENABLE_TESTING=OFF \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_SYSTEM_NAME=Android \ + -DCMAKE_SYSTEM_VERSION=21 \ + -DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a \ + -DCMAKE_ANDROID_NDK=$ANDROID_NDK_PATH \ + -DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=$SWIFT_ANDROID_BUILD_PATH/llvm-linux-x86_64/bin/clang \ + -DCMAKE_CXX_COMPILER=$SWIFT_ANDROID_BUILD_PATH/llvm-linux-x86_64/bin/clang++ \ + -DCMAKE_SWIFT_COMPILER=$SWIFT_ANDROID_BUILD_PATH/swift-linux-x86_64/bin/swiftc \ + -DENABLE_SWIFT=YES + +make + +cp ${SWIFT_PATH}/swift-corelibs-libdispatch/src/.libs/libdispatch.so $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift/android/armv7 +cp ${SWIFT_PATH}/swift-corelibs-libdispatch/src/swift/Dispatch.{swiftmodule,swiftdoc} $SWIFT_ANDROID_TOOLCHAIN_PATH/usr/lib/swift/android/armv7 + diff --git a/os/linux_base.h b/os/linux_base.h index 58b497148..278d37360 100644 --- a/os/linux_base.h +++ b/os/linux_base.h @@ -13,9 +13,11 @@ #ifndef __OS_LINUX_BASE__ #define __OS_LINUX_BASE__ +#ifndef __ANDROID__ #if __has_include() #include #endif +#endif #include #if __has_include() diff --git a/src/shims/hw_config.h b/src/shims/hw_config.h index 485dad663..1d164048e 100644 --- a/src/shims/hw_config.h +++ b/src/shims/hw_config.h @@ -104,7 +104,7 @@ _dispatch_hw_get_config(_dispatch_hw_config_t c) return (uint32_t)sysconf(_SC_NPROCESSORS_CONF); case _dispatch_hw_config_active_cpus: { -#ifdef __USE_GNU +#if defined(__USE_GNU) && !defined(__ANDROID__) // Prefer pthread_getaffinity_np because it considers // scheduler cpu affinity. This matters if the program // is restricted to a subset of the online cpus (eg via numactl).