/ build-android
build-android
  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_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_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
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