Sunday, November 13, 2016

How to build C++ static libraries (boost and QuantLib) for Android Studio

(1) Create standalone toolchain for Android in Mac
shell script    Select all
#create standalone toolchain for x86, x86_64, arm and arm64 #assume ndk is installed in Mac at ~/Library/Android/sdk/ndk-bundle #API level will be set to the minimum supported level for the given architecture (currently 9 for 32-bit architectures and 21 for 64-bit architectures) ~/Library/Android/sdk/ndk-bundle/build/tools/make_standalone_toolchain.py --arch x86 --install-dir ~/Library/Android/sdk/ndk-bundle/sources/android-toolchain-x86 ~/Library/Android/sdk/ndk-bundle/build/tools/make_standalone_toolchain.py --arch x86_64 --install-dir ~/Library/Android/sdk/ndk-bundle/sources/android-toolchain-x86_64 ~/Library/Android/sdk/ndk-bundle/build/tools/make_standalone_toolchain.py --arch arm --install-dir ~/Library/Android/sdk/ndk-bundle/sources/android-toolchain-arm ~/Library/Android/sdk/ndk-bundle/build/tools/make_standalone_toolchain.py --arch arm64 --install-dir ~/Library/Android/sdk/ndk-bundle/sources/android-toolchain-arm64


(2) Download and build boost for Android
shell script    Select all
# download boost source to ndk-bundle cd ~/Library/Android/sdk/ndk-bundle/sources curl -O http://ncu.dl.sourceforge.net/project/boost/boost/1.62.0/boost_1_62_0.tar.bz2 tar xjvf boost_1_62_0.tar.bz2 cd boost_1_62_0 # create the following user-config.jam at ~/Library/Android/sdk/ndk-bundle/sources/boost_1_62_0/tools/build/src/ cat > tools/build/src/user-config.jam << 'EOF' # ------------------ # Android configurations. # ------------------ import os ; local NDK_ROOT = [ os.environ NDK_ROOT ] ; # -------------------------------------------------------------------- local ANDROID_TOOLCHAIN_x86 = /sources/android-toolchain-x86 ; using gcc : x86 : $(NDK_ROOT)$(ANDROID_TOOLCHAIN_x86)/bin/i686-linux-android-g++ : <archiver>$(NDK_ROOT)$(ANDROID_TOOLCHAIN_x86)/bin/i686-linux-android-ar <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-ffunction-sections <compileflags>-funwind-tables <compileflags>-Wno-psabi <compileflags>-Wno-missing-field-initializers <compileflags>-no-canonical-prefixes <linkflags>-no-canonical-prefixes <compileflags>-Os <compileflags>-fomit-frame-pointer <compileflags>-fno-strict-aliasing <compileflags>-funswitch-loops <compileflags>-finline-limit=300 <compileflags>-Wa,--noexecstack <compileflags>-DANDROID <compileflags>-D__ANDROID__ <compileflags>-DNDEBUG <compileflags>-O2 <compileflags>-g <root>$(NDK_ROOT)$(ANDROID_TOOLCHAIN_x86)/sysroot # @Moss - Above are the 'official' android flags <architecture>x86 <compileflags>-fvisibility=hidden <compileflags>-fvisibility-inlines-hidden <compileflags>-fdata-sections <cxxflags>-D_REENTRANT <cxxflags>-D_GLIBCXX__PTHREADS <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable ; # -------------------------------------------------------------------- local ANDROID_TOOLCHAIN_x86_64 = /sources/android-toolchain-x86_64 ; using gcc : x86_64 : $(NDK_ROOT)$(ANDROID_TOOLCHAIN_x86_64)/bin/x86_64-linux-android-g++ : <archiver>$(NDK_ROOT)$(ANDROID_TOOLCHAIN_x86_64)/bin/x86_64-linux-android-ar <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-ffunction-sections <compileflags>-funwind-tables <compileflags>-Wno-psabi <compileflags>-Wno-missing-field-initializers <compileflags>-no-canonical-prefixes <linkflags>-no-canonical-prefixes <compileflags>-Os <compileflags>-fomit-frame-pointer <compileflags>-fno-strict-aliasing <compileflags>-funswitch-loops <compileflags>-finline-limit=300 <compileflags>-Wa,--noexecstack <compileflags>-DANDROID <compileflags>-D__ANDROID__ <compileflags>-DNDEBUG <compileflags>-O2 <compileflags>-g <root>$(NDK_ROOT)$(ANDROID_TOOLCHAIN_x86_64)/sysroot # @Moss - Above are the 'official' android flags <architecture>x86 <compileflags>-fvisibility=hidden <compileflags>-fvisibility-inlines-hidden <compileflags>-fdata-sections <cxxflags>-D_REENTRANT <cxxflags>-D_GLIBCXX__PTHREADS <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable ; # -------------------------------------------------------------------- local ANDROID_TOOLCHAIN_ARM = /sources/android-toolchain-arm ; using gcc : armeabi : $(NDK_ROOT)$(ANDROID_TOOLCHAIN_ARM)/bin/arm-linux-androideabi-g++ : <archiver>$(NDK_ROOT)$(ANDROID_TOOLCHAIN_ARM)/bin/arm-linux-androideabi-ar <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-ffunction-sections <compileflags>-funwind-tables <compileflags>-D__ARM_ARCH_5__ <compileflags>-D__ARM_ARCH_5T__ <compileflags>-D__ARM_ARCH_5E__ <compileflags>-D__ARM_ARCH_5TE__ <compileflags>-Wno-psabi <compileflags>-march=armv5te <compileflags>-mtune=xscale <compileflags>-msoft-float <compileflags>-mthumb <linkflags>-march=armv5te <compileflags>-Os <compileflags>-fomit-frame-pointer <compileflags>-fno-strict-aliasing <compileflags>-finline-limit=64 <compileflags>-Wa,--noexecstack <compileflags>-DANDROID <compileflags>-D__ANDROID__ <compileflags>-DNDEBUG <compileflags>-O2 <compileflags>-g <root>$(NDK_ROOT)$(ANDROID_TOOLCHAIN_ARM)/sysroot # @Moss - Above are the 'official' android flags <architecture>arm <compileflags>-fvisibility=hidden <compileflags>-fvisibility-inlines-hidden <compileflags>-fdata-sections <cxxflags>-D_REENTRANT <cxxflags>-D_GLIBCXX__PTHREADS <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable ; # -------------------------------------------------------------------- using gcc : armeabi_v7a : $(NDK_ROOT)$(ANDROID_TOOLCHAIN_ARM)/bin/arm-linux-androideabi-g++ : <archiver>$(NDK_ROOT)$(ANDROID_TOOLCHAIN_ARM)/bin/arm-linux-androideabi-ar <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-ffunction-sections <compileflags>-funwind-tables <compileflags>-Wno-psabi <compileflags>-march=armv7-a <compileflags>-msoft-float <compileflags>-mfpu=neon <compileflags>-mthumb <linkflags>-march=armv7-a <linkflags>-Wl,--fix-cortex-a8 <compileflags>-Os <compileflags>-fomit-frame-pointer <compileflag>-fno-strict-aliasing <compileflags>-finline-limit=64 <compileflags>-Wa,--noexecstack <compileflags>-DANDROID <compileflags>-D__ANDROID__ <compileflags>-DNDEBUG <compileflags>-O2 <compileflags>-g <root>$(NDK_ROOT)$(ANDROID_TOOLCHAIN_ARM)/sysroot # @Moss - Above are the 'official' android flags <architecture>arm <compileflags>-fvisibility=hidden <compileflags>-fvisibility-inlines-hidden <compileflags>-fdata-sections <cxxflags>-D__arm__ <cxxflags>-D_REENTRANT <cxxflags>-D_GLIBCXX__PTHREADS <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable ; # -------------------------------------------------------------------- local ANDROID_TOOLCHAIN_ARM64 = /sources/android-toolchain-arm64 ; using gcc : arm64_v8a : $(NDK_ROOT)$(ANDROID_TOOLCHAIN_ARM64)/bin/aarch64-linux-android-g++ : <archiver>$(NDK_ROOT)$(ANDROID_TOOLCHAIN_ARM64)/bin/aarch64-linux-android-ar <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-ffunction-sections <compileflags>-funwind-tables <compileflags>-fstack-protector <compileflags>-Wno-psabi <compileflags>-march=armv8-a <compileflags>-mtune=cortex-a53 <linkflags>-march=armv8-a <compileflags>-Os <compileflags>-fno-short-enums <compileflags>-fomit-frame-pointer <compileflags>-fno-strict-aliasing <compileflags>-finline-limit=64 <compileflags>-DANDROID <compileflags>-D__ANDROID__ <compileflags>-DNDEBUG <compileflags>-O2 <compileflags>-g <root>$(NDK_ROOT)$(ANDROID_TOOLCHAIN_ARM64)/sysroot # @Moss - Above are the 'official' android flags <architecture>arm <compileflags>-fvisibility=hidden <compileflags>-fvisibility-inlines-hidden <compileflags>-fdata-sections <cxxflags>-D_REENTRANT <cxxflags>-D_GLIBCXX__PTHREADS <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable ; EOF # bootstrap ./bootstrap.sh --with-libraries=atomic,chrono,date_time,exception,filesystem,graph,iostreams,math,program_options,random,regex,serialization,signals,system,test,thread,wave # clean and build for Android, j option is num of cores x 1.5 rm -fr android-build ./b2 -j6 -sNDK_ROOT="$HOME/Library/Android/sdk/ndk-bundle" --build-dir=android-build --stagedir=android-build/x86 toolset=gcc-x86 threading=multi link=static stage ./b2 -j6 -sNDK_ROOT="$HOME/Library/Android/sdk/ndk-bundle" --build-dir=android-build --stagedir=android-build/x86_64 toolset=gcc-x86_64 threading=multi link=static stage ./b2 -j6 -sNDK_ROOT="$HOME/Library/Android/sdk/ndk-bundle" --build-dir=android-build --stagedir=android-build/armeabi toolset=gcc-armeabi threading=multi link=static stage ./b2 -j6 -sNDK_ROOT="$HOME/Library/Android/sdk/ndk-bundle" --build-dir=android-build --stagedir=android-build/armeabi-v7a toolset=gcc-armeabi_v7a threading=multi link=static stage ./b2 -j6 -sNDK_ROOT="$HOME/Library/Android/sdk/ndk-bundle" --build-dir=android-build --stagedir=android-build/arm64-v8a toolset=gcc-arm64_v8a threading=multi link=static stage # create link for include folder cd android-build mkdir -p include cd include ln -s ../../boost .


(3) Create Android.mk at ~/Library/Android/sdk/ndk-bundle/sources/boost_1_62_0/ for ndkBuild in Android Studio
~/Library/Android/sdk/ndk-bundle/sources/boost_1_62_0/Android.mk    Select all
# create the following Android.mk cat > ~/Library/Android/sdk/ndk-bundle/sources/boost_1_62_0/Android.mk << 'EOF' LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libboost_atomic LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_atomic.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_chrono LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_chrono.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_date_time LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_date_time.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_exception LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_exception.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_filesystem LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_filesystem.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_graph LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_graph.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_iostreams LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_iostreams.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_math_c99 LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_math_c99.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_math_c99f LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_math_c99f.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_math_c99l LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_math_c99l.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_math_tr1 LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_math_tr1.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_math_tr1f LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_math_tr1f.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_math_tr1l LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_math_tr1l.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_prg_exec_monitor LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_prg_exec_monitor.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_program_options LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_program_options.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_random LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_random.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_regex LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_regex.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_serialization LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_serialization.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_signals LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_signals.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_system LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_system.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_test_exec_monitor LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_test_exec_monitor.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_thread LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_thread.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_timer LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_timer.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_unit_test_framework LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_unit_test_framework.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_wave LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_wave.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libboost_wserialization LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libboost_wserialization.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) EOF


(4) Download and build QuantLib for Android
shell script    Select all
# download quantlib source to ndk-bundle cd ~/Library/Android/sdk/ndk-bundle/sources curl -O http://nchc.dl.sourceforge.net/project/quantlib/QuantLib/1.9/QuantLib-1.9.tar.gz # for QuantLib-1.10 # curl -O http://nchc.dl.sourceforge.net/project/quantlib/QuantLib/1.10/QuantLib-1.10.tar.gz tar xzvf QuantLib-1.9.tar.gz cd QuantLib-1.9 #Create the following shell script for x86 cat > ~/Library/Android/sdk/ndk-bundle/sources/QuantLib-1.9/build-x86.sh << 'x86EOF' #!/bin/bash # build-x86.sh BUILD_DIR=android-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" NDK_DIR="${HOME}/Library/Android/sdk/ndk-bundle" YOUR_PATH_TO_NDK="`cd "${NDK_DIR}";pwd`" YOUR_TOOLCHAIN=${YOUR_PATH_TO_NDK}/sources/android-toolchain-x86 export CC=${YOUR_TOOLCHAIN}/bin/i686-linux-android-gcc export CXX=${YOUR_TOOLCHAIN}/bin/i686-linux-android-g++ export CPP=${YOUR_TOOLCHAIN}/bin/i686-linux-android-cpp export AR=${YOUR_TOOLCHAIN}/bin/i686-linux-android-ar export RANLIB=${YOUR_TOOLCHAIN}/bin/i686-linux-android-ranlib export LD=${YOUR_TOOLCHAIN}/bin/i686-linux-android-ld export CPPFLAGS="-fexceptions -frtti -fpic -ffunction-sections -funwind-tables -fvisibility=hidden -fvisibility-inlines-hidden -fdata-sections" export CXXFLAGS="-DANDROID -D__ANDROID__ -DNDEBUG -O2 -g -D_REENTRANT -D_GLIBCXX__PTHREADS" sysroot=${YOUR_TOOLCHAIN} boost_include="`cd "${YOUR_PATH_TO_NDK}/sources/boost_1_62_0/android-build/include";pwd`" boost_lib="`cd "${YOUR_PATH_TO_NDK}/sources/boost_1_62_0/android-build/x86/lib";pwd`" make distclean ./configure --prefix=/x86 --host=i686-linux-android --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-static --enable-shared=no --enable-examples=no --enable-thread-safe-observer-pattern make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} x86EOF #Create the following shell script for x86_64 cat > ~/Library/Android/sdk/ndk-bundle/sources/QuantLib-1.9/build-x86_64.sh << 'x86_64EOF' #!/bin/bash # build-x86_64.sh BUILD_DIR=android-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" NDK_DIR="${HOME}/Library/Android/sdk/ndk-bundle" YOUR_PATH_TO_NDK="`cd "${NDK_DIR}";pwd`" YOUR_TOOLCHAIN=${YOUR_PATH_TO_NDK}/sources/android-toolchain-x86_64 export CC=${YOUR_TOOLCHAIN}/bin/x86_64-linux-android-gcc export CXX=${YOUR_TOOLCHAIN}/bin/x86_64-linux-android-g++ export CPP=${YOUR_TOOLCHAIN}/bin/x86_64-linux-android-cpp export AR=${YOUR_TOOLCHAIN}/bin/x86_64-linux-android-ar export RANLIB=${YOUR_TOOLCHAIN}/bin/x86_64-linux-android-ranlib export LD=${YOUR_TOOLCHAIN}/bin/x86_64-linux-android-ld export CPPFLAGS="-fexceptions -frtti -fpic -ffunction-sections -funwind-tables -fvisibility=hidden -fvisibility-inlines-hidden -fdata-sections" export CXXFLAGS="-D_REENTRANT -D_GLIBCXX__PTHREADS -DANDROID -D__ANDROID__ -DNDEBUG -O2 -g" sysroot=${YOUR_TOOLCHAIN} boost_include="`cd "${YOUR_PATH_TO_NDK}/sources/boost_1_62_0/android-build/include";pwd`" boost_lib="`cd "${YOUR_PATH_TO_NDK}/sources/boost_1_62_0/android-build/x86_64/lib";pwd`" make distclean ./configure --prefix=/x86_64 --host=x86_64-linux-android --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-static --enable-shared=no --enable-examples=no --enable-thread-safe-observer-pattern make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} x86_64EOF #Create the following shell script for armeabi cat > ~/Library/Android/sdk/ndk-bundle/sources/QuantLib-1.9/build-armeabi.sh << 'armeabiEOF' #!/bin/bash # build-armeabi.sh BUILD_DIR=android-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" NDK_DIR="${HOME}/Library/Android/sdk/ndk-bundle" YOUR_PATH_TO_NDK="`cd "${NDK_DIR}";pwd`" YOUR_TOOLCHAIN=${YOUR_PATH_TO_NDK}/sources/android-toolchain-arm export CC=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-gcc export CXX=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-g++ export CPP=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-cpp export AR=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-ar export RANLIB=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-ranlib export LD=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-ld export CPPFLAGS="-fexceptions -frtti -fpic -ffunction-sections -funwind-tables -fvisibility=hidden -fvisibility-inlines-hidden -fdata-sections" export CXXFLAGS="-D_REENTRANT -D_GLIBCXX__PTHREADS -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -march=armv5te -mtune=xscale -msoft-float -mthumb -DANDROID -D__ANDROID__ -DNDEBUG -O2 -g" export LDFLAGS="-march=armv5te" sysroot=${YOUR_TOOLCHAIN} boost_include="`cd "${YOUR_PATH_TO_NDK}/sources/boost_1_62_0/android-build/include";pwd`" boost_lib="`cd "${YOUR_PATH_TO_NDK}/sources/boost_1_62_0/android-build/armeabi/lib";pwd`" make distclean ./configure --prefix=/armeabi --host=arm-linux-androideabi --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-static --enable-shared=no --enable-examples=no --enable-thread-safe-observer-pattern make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} armeabiEOF #Create the following shell script for armeabi-v7a cat > ~/Library/Android/sdk/ndk-bundle/sources/QuantLib-1.9/build-armeabi-v7a.sh << 'armeabi-v7aEOF' #!/bin/bash # build-armeabi-v7a.sh BUILD_DIR=android-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" NDK_DIR="${HOME}/Library/Android/sdk/ndk-bundle" YOUR_PATH_TO_NDK="`cd "${NDK_DIR}";pwd`" YOUR_TOOLCHAIN=${YOUR_PATH_TO_NDK}/sources/android-toolchain-arm export CC=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-gcc export CXX=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-g++ export CPP=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-cpp export AR=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-ar export RANLIB=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-ranlib export LD=${YOUR_TOOLCHAIN}/bin/arm-linux-androideabi-ld export CPPFLAGS="-fexceptions -frtti -fpic -ffunction-sections -funwind-tables -fvisibility=hidden -fvisibility-inlines-hidden -fdata-sections" export CXXFLAGS="-D_REENTRANT -D_GLIBCXX__PTHREADS -march=armv7-a -msoft-float -mfpu=neon -mthumb -DANDROID -D__ANDROID__ -DNDEBUG -O2 -g" export LDFLAGS="-march=armv7-a -Wl,--fix-cortex-a8" sysroot=${YOUR_TOOLCHAIN} boost_include="`cd "${YOUR_PATH_TO_NDK}/sources/boost_1_62_0/android-build/include";pwd`" boost_lib="`cd "${YOUR_PATH_TO_NDK}/sources/boost_1_62_0/android-build/armeabi-v7a/lib";pwd`" make distclean ./configure --prefix=/armeabi-v7a --host=arm-linux-androideabi --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-static --enable-shared=no --enable-examples=no --enable-thread-safe-observer-pattern make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} armeabi-v7aEOF #Create the following shell script for arm64-v8a cat > ~/Library/Android/sdk/ndk-bundle/sources/QuantLib-1.9/build-arm64-v8a.sh << 'arm64-v8aEOF' #!/bin/bash # build-arm64-v8a.sh BUILD_DIR=android-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" NDK_DIR="${HOME}/Library/Android/sdk/ndk-bundle" YOUR_PATH_TO_NDK="`cd "${NDK_DIR}";pwd`" YOUR_TOOLCHAIN=${YOUR_PATH_TO_NDK}/sources/android-toolchain-arm64 export CC=${YOUR_TOOLCHAIN}/bin/aarch64-linux-android-gcc export CXX=${YOUR_TOOLCHAIN}/bin/aarch64-linux-android-g++ export CPP=${YOUR_TOOLCHAIN}/bin/aarch64-linux-android-cpp export AR=${YOUR_TOOLCHAIN}/bin/aarch64-linux-android-ar export RANLIB=${YOUR_TOOLCHAIN}/bin/aarch64-linux-android-ranlib export LD=${YOUR_TOOLCHAIN}/bin/aarch64-linux-android-ld export CPPFLAGS="-fexceptions -frtti -fpic -ffunction-sections -funwind-tables -fstack-protector -fvisibility=hidden -fvisibility-inlines-hidden -fdata-sections" export CXXFLAGS="-D_REENTRANT -D_GLIBCXX__PTHREADS -march=armv8-a -mtune=cortex-a53 -DANDROID -D__ANDROID__ -DNDEBUG -O2 -g" export LDFLAGS="-march=armv8-a" sysroot=${YOUR_TOOLCHAIN} boost_include="`cd "${YOUR_PATH_TO_NDK}/sources/boost_1_62_0/android-build/include";pwd`" boost_lib="`cd "${YOUR_PATH_TO_NDK}/sources/boost_1_62_0/android-build/arm64-v8a/lib";pwd`" make distclean ./configure --prefix=/arm64-v8a --host=aarch64-linux-android --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-static --enable-shared=no --enable-examples=no --enable-thread-safe-observer-pattern make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} arm64-v8aEOF #run shell scripts to clean and build for Android cd ~/Library/Android/sdk/ndk-bundle/sources cd QuantLib-1.9 rm -fr android-build sh build-x86.sh sh build-x86_64.sh sh build-armeabi.sh sh build-armeabi-v7a.sh sh build-arm64-v8a.sh


(5) Create Android.mk at ~/Library/Android/sdk/ndk-bundle/sources/QuantLib-1.9/ for ndkBuild in Android Studio
~/Library/Android/sdk/ndk-bundle/sources/QuantLib-1.9/Android.mk    Select all
# create the following Android.mk cat > ~/Library/Android/sdk/ndk-bundle/sources/QuantLib-1.9/Android.mk << 'EOF' LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libQuantLib LOCAL_SRC_FILES := android-build/$(TARGET_ARCH_ABI)/lib/libQuantLib.a LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/android-build/$(TARGET_ARCH_ABI)/include LOCAL_CPP_FEATURES := rtti exceptions include $(PREBUILT_STATIC_LIBRARY) EOF


(6) Demo of using boost c++ libraries for ndkBuild in Android Studio 2.2.2
Assume prebuilt libraries are installed to ~/Library/Android/sdk/ndk-bundle/sources
app/build.gradle    Select all
apply plugin: 'com.android.application' Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) def sdkFolder = properties.getProperty('sdk.dir') def ndkFolder = properties.getProperty('ndk.dir') android { compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { applicationId "com.example.helloboost" minSdkVersion 9 targetSdkVersion 23 versionCode = 1 versionName = '1.0' archivesBaseName = 'HELLOBOOST_' + versionName ndk { moduleName "hello-boost" abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' } externalNativeBuild { ndkBuild { arguments "NDK_APPLICATION_MK=src/main/jni/Application.mk" // default module search path NDK_MODULE_PATH is at ${ndkFolder}/sources } } } buildTypes { debug { minifyEnabled false zipAlignEnabled true debuggable true useProguard false versionNameSuffix 'debug' } release { minifyEnabled false zipAlignEnabled true useProguard true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } externalNativeBuild { ndkBuild { path "src/main/jni/Android.mk" } } }


app/src/main/jni/Android.mk    Select all
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello-boost LOCAL_SRC_FILES := hello-boost.cpp first.cpp LOCAL_STATIC_LIBRARIES := libboost_system libboost_thread libboost_program_options libQuantLib include $(BUILD_SHARED_LIBRARY) $(call import-module, boost_1_62_0) $(call import-module, QuantLib-1.9)


app/src/main/jni/Application.mk    Select all
APP_STL = gnustl_static APP_CPPFLAGS = -Wall


(7) HelloJNI, sample of using cmake CMakeLists.txt after adding Boost and QuantLib libraries
Assume prebuilt libraries are installed to ~/Library/Android/sdk/ndk-bundle/sources
app/src/main/cpp/CMakeLists.txt    Select all
cmake_minimum_required(VERSION 3.4.1) # build native_app_glue as a static lib add_library(app-glue STATIC ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c) # now build app's shared lib set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -frtti -fexceptions") add_library(hello-jni SHARED hello-jni.c) #BOOST set(Boost_INCLUDE_DIR ${ANDROID_NDK}/sources/boost_1_62_0/android-build/include) set(Boost_LIBRARY_DIR ${ANDROID_NDK}/sources/boost_1_62_0/android-build/${ANDROID_ABI}/lib) set(Boost_USE_STATIC_LIBS ON) # only find static libs set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) set(Boost_COMPILER -gcc) find_package(Boost COMPONENTS system thread program_options REQUIRED) #BOOSTEND #QuantLib set(QuantLib_INCLUDE_DIR ${ANDROID_NDK}/sources/QuantLib-1.9/android-build/${ANDROID_ABI}/include) set(QuantLib_LIBRARY_DIR ${ANDROID_NDK}/sources/QuantLib-1.9/android-build/${ANDROID_ABI}/lib) add_library(QuantLib STATIC IMPORTED) set_property(TARGET QuantLib PROPERTY IMPORTED_LOCATION ${QuantLib_LIBRARY_DIR}/libQuantLib.a) #QuantLibEND # if have QuantLibJNI, uncomment below and add QuantLibJNI to target_link_libraries of hello-jni for c++ classes generated from QuantLib-SWIG-1.9 #add_library(QuantLibJNI SHARED quantlib_wrap.cpp) #target_link_libraries(QuantLibJNI ${Boost_LIBRARIES} QuantLib) if(Boost_FOUND) # Include libraries needed include_directories(${Boost_INCLUDE_DIR}) include_directories(${QuantLib_INCLUDE_DIR}) target_link_libraries(hello-jni android app-glue log ${Boost_LIBRARIES} QuantLib) endif()

app/build.gradle    Select all
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion '23.0.3' defaultConfig { applicationId 'com.example.hellojni' minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" archivesBaseName = 'HELLOJNI_' + versionName externalNativeBuild { cmake { arguments '-DANDROID_TOOLCHAIN=clang' cppFlags "" } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { debuggable true jniDebuggable true versionNameSuffix 'debug' } } externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" } } productFlavors { arm { ndk { abiFilter 'armeabi' } } arm7 { ndk { abiFilter 'armeabi-v7a' } } arm64 { ndk { abiFilter 'arm64-v8a' } } x86 { ndk { abiFilter 'x86' } } x86_64 { ndk { abiFilter 'x86_64' } } universal { ndk { abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' } } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7' }


(8) Download & Compile swig and generate java & c++ classes from QuantLib-SWIG-1.9 for Android project
shell scriptSelect all
#download and compile swig cd ~/Downloads curl -O http://nchc.dl.sourceforge.net/project/swig/swig/swig-3.0.10/swig-3.0.10.tar.gz tar xzvf swig-3.0.10.tar.gz curl -O http://nchc.dl.sourceforge.net/project/pcre/pcre/8.39/pcre-8.39.tar.gz tar xzvf pcre-8.39.tar.gz cd ~/Downloads/pcre-8.39 ./configure --enable-utf8 --enable-unicode-properties make sudo make install cd ~/Downloads/swig-3.0.10 ./configure make sudo make install # download QuantLib-SWIG-1.9 cd ~/Library/Android/sdk/ndk-bundle/sources curl -O http://jaist.dl.sourceforge.net/project/quantlib/QuantLib/1.9/other%20languages/QuantLib-SWIG-1.9.tar.gz tar xzvf QuantLib-SWIG-1.9.tar.gz # generate SWIG java & c++ classes to HelloJNI project cd ~/Projects cd HelloJNI/app/src/main mkdir -p cpp mkdir -p java/org/quantlib swig -java -c++ -module QuantLib -package org.quantlib -outdir java/org/quantlib -o cpp/quantlib_wrap.cpp ~/Library/Android/sdk/ndk-bundle/sources/QuantLib-SWIG-1.9/SWIG/quantlib.i



No comments: