Cross Build nghttp2 for Andriod

6708 ワード

Cross Build nghttp2 for Andriod

  • Build Environment Prepare
  • Get Nghttp2-v1.41 Source Code
  • Get Andriod NDK and Configure Environment Variable.
  • Writing the Build Scripts
  • The Target Device for Arm64-v8a
  • Build Output

  • Build Environment Prepare


    If you wan to build nghttp2 for andriod you shoud do two things. Frist you should get nghttp2 source code, second you need to configure your build environment.

    Get Nghttp2-v1.41 Source Code

  • The nghttp2 source web site at: https://github.com/nghttp2/nghttp2
  • Get libevent sorce code git clone https://github.com/nghttp2/nghttp2.git after you clone you need to switch to a stable tag like v1.4.1.

  • Get Andriod NDK and Configure Environment Variable.

  • NDK cross build tools web site at: https://developer.android.google.cn/ndk/downloads/index.html
  • Set NDK Environment Variable export NDK_HOME=/home/heyongxin/android-ndk-r16b
  • Openssl and libcrpto Note if you wan to use libevent_openssl.a, you need to set openssl include path in build scripts. My computer have make and install libevent in defaut path so i don’t need set it.

  • Writing the Build Scripts


    Arm64-v8a and Armeabi-v7a chip arch hvave covered the most device in the market. so we only provide Arm64-v8a arch’s build script,the armeabi-v7a’s build scripts reference the v8a.

    The Target Device for Arm64-v8a

    #!/bin/sh
    export ANDROID_NDK_ROOT=/home/heyongxin/android-ndk-r16b
    NDK=/home/heyongxin/android-ndk-r16b
    NGHTTP2_TMP_FOLDER=$PWD
    NGHTTP2_TARGET_API=android-23
    NGHTTP2_GCC_VERSION=4.9
    NGHTTP2_OUTPUT_PATH=$PWD/nghttp2_build
    rm -rf $NGHTTP2_OUTPUT_PATH
    mkdir -p $NGHTTP2_OUTPUT_PATH
    mkdir -p $NGHTTP2_TMP_FOLDER/android-toolchain-aarch64
    NDK_MAKE_TOOLCHAIN=$NDK/build/tools/make-standalone-toolchain.sh
    ${NDK_MAKE_TOOLCHAIN} \
        --platform=${NGHTTP2_TARGET_API} \
        --arch=arm64 \
        --toolchain=aarch64-linux-android-${NGHTTP2_GCC_VERSION} \
        --install-dir="${NGHTTP2_TMP_FOLDER}/android-toolchain-aarch64" \
        --force
    export TOOLCHAIN_PATH="${NGHTTP2_TMP_FOLDER}/android-toolchain-aarch64/bin"
    export TOOL=aarch64-linux-android
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=${NDK_TOOLCHAIN_BASENAME}-gcc
    export CXX=${NDK_TOOLCHAIN_BASENAME}-g++
    export LINK=${CXX}
    export LD=${NDK_TOOLCHAIN_BASENAME}-ld
    export AR=${NDK_TOOLCHAIN_BASENAME}-ar
    export RANLIB=${NDK_TOOLCHAIN_BASENAME}-ranlib
    export STRIP=${NDK_TOOLCHAIN_BASENAME}-strip
    export ARCH_FLAGS=
    export ARCH_LINK=
    export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
    export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
    export LDFLAGS=" ${ARCH_LINK} "
    cd ${NGHTTP2_TMP_FOLDER}
    echo $CC
    ./configure --host=aarch64-linux-android  --disable-shared --enable-static 
    make 
    

    Build Output


    If you run build scripts no error, it mean you have build success. Right now you can check the build output, in the source code root path. you will find the .libs folder. the folder context are as follows:
    .libs
    ├── libnghttp2.a
    ├── libnghttp2.la -> ../libnghttp2.la
    └── libnghttp2.lai