美文网首页
NDK下编译libwebsockets

NDK下编译libwebsockets

作者: 三万分之一 | 来源:发表于2018-10-31 09:30 被阅读0次

    libwebsockets依赖libuv,libz,mbedtls,所以构建脚本需要提前构建以上几个库,下面介绍使用NDK的独立工具链(standalone toolchain)来编译步骤。独立工具链的参考链接为:https://developer.android.com/ndk/guides/standalone_toolchain

    #!/bin/bash

    # Build libwebsockets  library for Android

    # path to NDK

    export NDK=<Your_tool_path>/android-ndk-r17-beta2-linux-x86_64/android-ndk-r17-beta2

    export ANDROID_NDK=${NDK}

    export TOOLCHAIN=${NDK}/toolchain

    export CORSS_SYSROOT=${NDK}/sysroot

    export SYSROOT=${NDK}/platforms/android-22/arch-arm

    set -e

    # for zlib-1.2.8, libuv, mbedtls-2.11.0, libwebsockets-3.0

    # create a local android toolchain

    API=${3:-24}

    $NDK/build/tools/make-standalone-toolchain.sh \

    --toolchain=arm-linux-androideabi-4.9 \

    --arch=arm \

    --install-dir=`pwd`/android-toolchain-arm \

    --platform=android-$API \

    --stl=libc++ \

    --force \

    --verbose

    # setup environment to use the gcc/ld from the android toolchain

    export INSTALL_PATH=/media/luckfish/share_ext/libwebsockets_android/android-toolchain-arm

    export TOOLCHAIN_PATH=`pwd`/android-toolchain-arm

    export TOOL=arm-linux-androideabi

    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/bin/${TOOL}

    export PATH=`pwd`/android-toolchain-arm/bin:$PATH

    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 PLATFORM=android

    export CFLAGS="D__ANDROID_API__=$API"

    # configure and build libuv

    [ ! -f ./android-toolchain-arm/lib/libuv.so ] && {

    cd libuv

    echo "=============================================>> build libuv"

    PATH=$TOOLCHAIN_PATH:$PATH make clean

    PATH=$TOOLCHAIN_PATH:$PATH make

    PATH=$TOOLCHAIN_PATH:$PATH make install

    echo "<<============================================= build libuv"

    cd ..

    }

    # configure and build zlib

    [ ! -f ./android-toolchain-arm/lib/libz.a ] && {

    cd zlib-1.2.8

    echo "=============================================>> build libz"

    PATH=$TOOLCHAIN_PATH:$PATH make clean

    PATH=$TOOLCHAIN_PATH:$PATH make

    PATH=$TOOLCHAIN_PATH:$PATH make install

    echo "<<============================================= build libz"

    cd ..

    }

    # configure and build mbedtls

    [ ! -f ./android-toolchain-arm/lib/libmbedtls.so ] && {

    echo "=============================================>> build mbedtls"

    PREFIX=$TOOLCHAIN_PATH

    cd mbedtls-2.11.0

    [ ! -d build ] && mkdir build

    cd build

    export CFLAGS="$CFLAGS -fomit-frame-pointer"

    PATH=$TOOLCHAIN_PATH:$PATH cmake .. -DCMAKE_TOOLCHAIN_FILE=`pwd`/../cross-arm-android-gnueabi.cmake \

      -DCMAKE_INSTALL_PREFIX:PATH=${INSTALL_PATH} \

      -DCMAKE_BUILD_TYPE=RELEASE -DUSE_SHARED_MBEDTLS_LIBRARY=On

    PATH=$TOOLCHAIN_PATH:$PATH make clean

    PATH=$TOOLCHAIN_PATH:$PATH make SHARED=1

    PATH=$TOOLCHAIN_PATH:$PATH make install

    echo "<<============================================= build mbedtls"

    cd ../..

    }

    # configure and build libwebsockets

    [ ! -f ./android-toolchain-arm/lib/libwebsockets.a ] && {

    cd libwebsockets

    [ ! -d build ] && mkdir build

    cd build

    echo "=============================================>> build libwebsockets"

    PATH=$TOOLCHAIN_PATH:$PATH cmake .. -DCMAKE_TOOLCHAIN_FILE=`pwd`/../cross-arm-android-gnueabi.cmake \

      -DCMAKE_INSTALL_PREFIX:PATH=${INSTALL_PATH} \

      -DLWS_WITH_LWSWS=1 \

      -DLWS_WITH_MBEDTLS=1 \

      -DLWS_WITHOUT_TESTAPPS=1 \

      -DLWS_MBEDTLS_LIBRARIES="${INSTALL_PATH}/lib/libmbedcrypto.a;${INSTALL_PATH}/lib/libmbedtls.a;${INSTALL_PATH}/lib/libmbedx509.a" \

      -DLWS_MBEDTLS_INCLUDE_DIRS=${INSTALL_PATH}/include \

      -DLWS_LIBUV_LIBRARIES=${INSTALL_PATH}/lib/libuv.so \

      -DLWS_LIBUV_INCLUDE_DIRS=${INSTALL_PATH}/include \

      -DLWS_ZLIB_LIBRARIES=${INSTALL_PATH}/lib/libz.so \

      -DLWS_ZLIB_INCLUDE_DIRS=${INSTALL_PATH}/include

    PATH=$TOOLCHAIN_PATH:$PATH make

    PATH=$TOOLCHAIN_PATH:$PATH make install

    echo "<<============================================= build libwebsockets"

    cd ../..

    }

    后记:

    如果单独编译mbedtls库,可以使用命令:

    1)cmake .. -DCMAKE_TOOLCHAIN_FILE=`pwd`/../cross-arm-android-gnueabi.cmake -DCMAKE_BUILD_TYPE=RELEASE -DUSE_SHARED_MBEDTLS_LIBRARY=On

    2)删除build目录下的CMakeCache.txt

    3)cmake -DUSE_SHARED_MBEDTLS_LIBRARY=On ..

    4)make

    得到动态库,其中cross-arm-android-gnueabi.cmake 指定了编译工具,

    cross-arm-android-gnueabi.cmake文件如下:

    #

    # CMake Toolchain file for crosscompiling on ARM.

    #

    # This can be used when running cmake in the following way:

    #  cd build/

    #  cmake .. -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake

    #

    set(CROSS_PATH /media/xxx/share_ext/libwebsockets_android/android-toolchain-arm)

    # Target operating system name.

    set(CMAKE_SYSTEM_NAME Android)

    # Target build dynamic libs.

    set(BUILD_SHARED_LIBS ON)

    # Name of C compiler.

    set(CMAKE_C_COMPILER "${CROSS_PATH}/bin/arm-linux-androideabi-gcc")

    set(CMAKE_CXX_COMPILER "${CROSS_PATH}/bin/arm-linux-androideabi-g++")

    # Where to look for the target environment. (More paths can be added here)

    set(CMAKE_FIND_ROOT_PATH "${CROSS_PATH}")

    # Adjust the default behavior of the FIND_XXX() commands:

    # search programs in the host environment only.

    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

    # Search headers and libraries in the target environment only.

    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)

    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

    相关文章

      网友评论

          本文标题:NDK下编译libwebsockets

          本文链接:https://www.haomeiwen.com/subject/druotqtx.html