android NDK交叉编译mosquitto服务器端及其使用(二)

mosquitto 编译支持TLS

把openssl编译好的文件目录放入到mosquitto-1.5.9目录下

编译openssl过程,见我其他文章
android NDK交叉编译静态openssl

修改编译文件mosquitto_android_cmake.sh

把WITH_TLS=ON,并设置OPENSSL目录

#!/bin/bash
CURRENT_DIR=$(dirname $(readlink -f $0))

rm -rf ./build
mkdir -p build

ANDROID_ABI="armeabi-v7a"
ANDROID_NDK="/home/kaober/disk4T1/raojun/android-ndk-r16b"
cmake \
   -DANDROID_NDK=${ANDROID_NDK} \
   -DANDROID_ABI=${ANDROID_ABI} \
   -DANDROID_NDK_HOST_X64="YES"  \
   -DANDROID_NATIVE_API_LEVEL=22 \
   -DANDROID_TOOLCHAIN_NAME="arm-linux-androideabi-4.9" \
   -DCMAKE_TOOLCHAIN_FILE="/home/kaober/disk4T1/raojun/android-ndk-r16b/build/cmake/android.toolchain.cmake" \
   -DOPENSSL_LIBRARIES="${CURRENT_DIR}/openssl/arch-${ANDROID_ABI}/lib" \
   -DOPENSSL_INCLUDE_DIR="${CURRENT_DIR}/openssl/sources/include" \
   -DWITH_TLS=ON \
   -DWITH_THREADING=OFF \
   -H. -B./build

cd build
cmake --build .
cd ..

修改mosquitto-1.5.9\CMakeLists.txt

if (WITH_TLS)
    if(NOT ANDROID) #add by kaober
        find_package(OpenSSL REQUIRED)
    endif(NOT ANDROID) #add by kaober
    add_definitions("-DWITH_TLS")

    if (WITH_TLS_PSK)
        add_definitions("-DWITH_TLS_PSK")
    endif (WITH_TLS_PSK)

    if (WITH_EC)
        add_definitions("-DWITH_EC")
    endif (WITH_EC)
else (WITH_TLS)
    set (OPENSSL_INCLUDE_DIR "")
endif (WITH_TLS)

修改mosquitto-1.5.9\src\CMakeLists.txt

add_executable(mosquitto ${MOSQ_SRCS})
#modify by kaober
if(ANDROID)
if(WITH_TLS)
    add_library(libcrypto STATIC IMPORTED)
    set_target_properties(libcrypto PROPERTIES IMPORTED_LOCATION ${OPENSSL_LIBRARIES}/libcrypto.a)
    add_library(libssl STATIC IMPORTED)
    set_target_properties(libssl PROPERTIES IMPORTED_LOCATION ${OPENSSL_LIBRARIES}/libssl.a)
endif(WITH_TLS)
endif(ANDROID)
    
if (ANDROID)
if(WITH_TLS)
    target_link_libraries(mosquitto ${MOSQ_LIBS} libssl libcrypto)
else (WITH_TLS)
    target_link_libraries(mosquitto ${MOSQ_LIBS})
endif (WITH_TLS)
else (ANDROID)
    target_link_libraries(mosquitto ${MOSQ_LIBS})
endif (ANDROID)
#modify by kaober

if (WITH_TLS)
    add_executable(mosquitto_passwd mosquitto_passwd.c)
    #modify by kaober
    if(ANDROID)
        target_link_libraries(mosquitto_passwd libssl libcrypto)
    else (ANDROID)
        target_link_libraries(mosquitto_passwd ${OPENSSL_LIBRARIES})
    endif(ANDROID)
    #modify by kaober
    install(TARGETS mosquitto_passwd RUNTIME DESTINATION "${BINDIR}" LIBRARY DESTINATION "${LIBDIR}")
endif (WITH_TLS)

mosquitto-1.5.9\lib\CMakeLists.txt

#add by kaober
if(ANDROID)
if(WITH_TLS)
    add_library(libcrypto STATIC IMPORTED)
    set_target_properties(libcrypto PROPERTIES IMPORTED_LOCATION ${OPENSSL_LIBRARIES}/libcrypto.a)
    add_library(libssl STATIC IMPORTED)
    set_target_properties(libssl PROPERTIES IMPORTED_LOCATION ${OPENSSL_LIBRARIES}/libssl.a)
    target_link_libraries(libmosquitto ${LIBRARIES} libssl libcrypto)
else (WITH_TLS)
    target_link_libraries(libmosquitto ${LIBRARIES})
endif(WITH_TLS)    
else (ANDROID)
    target_link_libraries(libmosquitto ${LIBRARIES})
endif(ANDROID)
#add by kaober

运行mosquitto_android_cmake.sh

完成!

你可能感兴趣的:(android NDK交叉编译mosquitto服务器端及其使用(二))