Poco库使用 CMake 编译,支持 NetSSL

在 CMake 项目中引入Poco库编译,测试工程文件结构如下

test
	|--cmake
		|-- ios.toolchain.cmake
	|-- test.cc
	|-- test.h
	|--CMakeLists.txt
	|--third_party
		|-- poco
		|-- openssl

openssl 各端(Android,IOS,Win,MacOS)可从github下载:

https://github.com/vintonliu/openssl-libs.git

CMakeLists.txt 内容:

# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)

project ("test")

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# openssl need for poco net ssl
set(OPENSSL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/openssl")
set(SSL_NAME "libssl.a")
set(CRYPTO_NAME "libcrypto.a")
if(WIN32)
	set(SSL_NAME "libssl.lib")
	set(CRYPTO_NAME "libcrypto.lib")

	if(CMAKE_CL_64)
		set(OPENSSL_INCLUDE_DIR "${OPENSSL_DIR}/pc/x64/include")
		set(OPENSSL_LIB_DIR "${OPENSSL_DIR}/pc/x64/lib")
	else()
		set(OPENSSL_INCLUDE_DIR "${OPENSSL_DIR}/pc/x86/include")
		set(OPENSSL_LIB_DIR "${OPENSSL_DIR}/pc/x86/lib")
	endif(CMAKE_CL_64)
elseif(ANDROID)
	set(OPENSSL_INCLUDE_DIR "${OPENSSL_DIR}/android/${ANDROID_ABI}/include")
	set(OPENSSL_LIB_DIR "${OPENSSL_DIR}/android/${ANDROID_ABI}/lib")
elseif(APPLE)
	if(IOS)
		set(OPENSSL_INCLUDE_DIR "${OPENSSL_DIR}/ios/include")
		set(OPENSSL_LIB_DIR "${OPENSSL_DIR}/ios/lib")
	else()
		# set(OPENSSL_ROOT_DIR "/usr/local/opt/[email protected]"
		set(OPENSSL_INCLUDE_DIR "${OPENSSL_DIR}/mac/include")
		set(OPENSSL_LIB_DIR "${OPENSSL_DIR}/mac/lib")
	endif(IOS)
endif(WIN32)
set(OPENSSL_INCLUDE_DIR "${OPENSSL_INCLUDE_DIR}" CACHE STRING "openssl include" FORCE)
set(OPENSSL_SSL_LIBRARY "${OPENSSL_LIB_DIR}/${SSL_NAME}" CACHE STRING "ssl library" FORCE)
set(OPENSSL_CRYPTO_LIBRARY "${OPENSSL_LIB_DIR}/${CRYPTO_NAME}" CACHE STRING "crypto library" FORCE)

# 设置poco库编译模块选项
set(POCO_STATIC ON CACHE BOOL "Set to OFF|ON (default is OFF) to control build of POCO as STATIC library" FORCE)
set(ENABLE_NETSSL ON CACHE BOOL "Enable NetSSL" FORCE)
set(ENABLE_CRYPTO ON CACHE BOOL "Enable Crypto" FORCE)
set(ENABLE_ENCODINGS OFF CACHE BOOL "Enable Encodings" FORCE)
set(ENABLE_MONGODB OFF CACHE BOOL "Enable MongoDB" FORCE)
set(ENABLE_REDIS OFF CACHE BOOL "Enable Redis" FORCE)
set(ENABLE_PDF OFF CACHE BOOL "Enable PDF" FORCE)
set(ENABLE_DATA OFF CACHE BOOL "Enable Data" FORCE)
set(ENABLE_DATA_SQLITE OFF CACHE BOOL "Enable Data SQlite" FORCE)
set(ENABLE_DATA_MYSQL OFF CACHE BOOL "Enable Data MySQL" FORCE)
set(ENABLE_DATA_ODBC OFF CACHE BOOL "Enable Data ODBC" FORCE)
set(ENABLE_ZIP OFF CACHE BOOL "Enable Zip" FORCE)
set(ENABLE_PAGECOMPILER OFF CACHE BOOL "Enable PageCompiler" FORCE)
set(ENABLE_PAGECOMPILER_FILE2PAGE OFF CACHE BOOL "Enable File2Page" FORCE)
set(ENABLE_XML OFF CACHE BOOL "Enable XML" FORCE)

# 添加 线程名字宏定义,方便查看线程名字
if(WIN32)
	add_definitions(-DPOCO_WIN32_DEBUGGER_THREAD_NAMES)
else()
	add_definitions(-DPOCO_POSIX_DEBUGGER_THREAD_NAMES)
endif()

# 添加 poco 库编译
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/poco)


set(Poco_COMPONENTS "")
list(APPEND Poco_COMPONENTS "PocoFoundation")
list(APPEND Poco_COMPONENTS "PocoJSON")
list(APPEND Poco_COMPONENTS "PocoNet")
list(APPEND Poco_COMPONENTS "PocoUtil")
# list(APPEND Poco_COMPONENTS "PocoXML")
list(APPEND Poco_COMPONENTS "PocoNetSSL")
list(APPEND Poco_COMPONENTS "PocoCrypto")

set(SRCS 
	test.cc
	test.h
)

add_executable(test
            SHARED
            ${SRCS} )
            
target_link_libraries(test
	${Poco_COMPONENTS}
)

Android 端编译

#!/bin/bash

CWD=`pwd`

BUILD_MODE=$1
if [ -n "$BUILD_MODE" ]
then
    BUILD_MODE=Debug
else
    BUILD_MODE=MinSizeRel
fi

echo "BUILD_MODE: $BUILD_MODE"


#配置交叉编译链
# linux, config ANDROID_NDK_R20_ROOT="path to ndkr20" in profile
ANDROID_NDK_TOOLCHAIN_HOME=$ANDROID_NDK_R20_ROOT
# windows平台,VSCode, git bash
# ANDROID_NDK_TOOLCHAIN_HOME=/d/Android/sdk/ndk-bundle
CMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_TOOLCHAIN_HOME/build/cmake/android.toolchain.cmake

# CMakeList.txt 所在文件夹
SOURCE_PATH=$CWD
# 编译中间文件夹
OBJECT_DIR="$CWD/out/build/android_$BUILD_MODE"
#安装文件夹
INSTALL_DIR="$CWD/out/install/android_$BUILD_MODE"

if [ -n "$1" -a "$1" == "clean" ]; then
	echo "rm -rf $CWD/out/build/android"
	rm -rf "$CWD/out/build/android*"
exit 0
fi

if [ -d $INSTALL_DIR ]
then
    rm -rf $INSTALL_DIR
fi

# 五种类型cpu编译链
android_toolchains=(
    # armeabi is no longer support build
#   "armeabi"
   "armeabi-v7a"
   "arm64-v8a"
   "x86"
   "x86_64"
)

API=23


PLATFORM_CONFIG="-DANDROID=1 -DCMAKE_SYSTEM_NAME=Android \
                -DANDROID_NDK=$ANDROID_NDK_TOOLCHAIN_HOME \
                -DANDROID_TOOLCHAIN=clang \
                -DCMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE \
                -DANDROID_NATIVE_API_LEVEL=$API \
                -DCMAKE_BUILD_TYPE=$BUILD_MODE"

BUILD_CONFIG="-DCMAKE_VERBOSE_MAKEFILE=ON"

num=${#android_toolchains[@]}
for((i=0; i<num; i++))
do
    echo "************* building ${android_toolchains[i]} ***********"
    
    # create build temp dir
    mkdir -p $OBJECT_DIR/${android_toolchains[i]}	
	mkdir -p $INSTALL_DIR/${android_toolchains[i]}

    cd $OBJECT_DIR/${android_toolchains[i]}

    PLATFORM_CONFIG="$PLATFORM_CONFIG \
                -DANDROID_ABI=${android_toolchains[i]} \
                -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR/${android_toolchains[i]}"

    cmake -G "Ninja" $PLATFORM_CONFIG $BUILD_CONFIG $SOURCE_PATH
    echo "******************** cmake generator done ****************"
    #cmake --build .
    ninja -j8
    echo "******************** cmake build done ********************"
    
done

IOS 端编译:

#!/bin/bash

CWD=`pwd`

# BUILD_MODE=Release
BUILD_MODE=MinSizeRel
LINK_MODE=shared

BUILD_MODE=$1
if [ -n "$BUILD_MODE" ]
then
    BUILD_MODE=Debug
else
    BUILD_MODE=MinSizeRel
fi

# must add -DANDROID=1, else could not generate Android CMakeFiles
PLATFORM_CONFIG="-DCMAKE_TOOLCHAIN_FILE=$CWD/cmake/ios.toolchain.cmake \
               -DCMAKE_BUILD_TYPE=$BUILD_MODE \
               -DENABLE_BITCODE=0 \
               -DENABLE_ARC=0 \
               -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=E8TD92447B \
               -DDEPLOYMENT_TARGET=9.0"

PLATFORMS=(
    "OS"
    "SIMULATOR"
    "SIMULATOR64"
)

SOURCE_PATH=$CWD

# 编译中间文件夹
OBJECT_DIR="$CWD/out/build/ios_$BUILD_MODE"

#安装文件夹
INSTALL_DIR="$CWD/out/install/ios_$BUILD_MODE"
rm -rf $INSTALL_DIR

num=${#PLATFORMS[@]}
for((i=0; i<num; i++))
do
    PLATFORM_CONFIG="$PLATFORM_CONFIG \
                -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR/${PLATFORMS[i]}"

    mkdir -p "$OBJECT_DIR/${PLATFORMS[i]}"
    cd "$OBJECT_DIR/${PLATFORMS[i]}"

    cmake -G"Xcode" $PLATFORM_CONFIG -DPLATFORM=${PLATFORMS[i]} $SOURCE_PATH
    cmake --build . --config $BUILD_MODE --target install
done

其中,ios.toolchain.cmake 可从GitHub下载:

git clone https://github.com/vintonliu/ios-cmake.git

你可能感兴趣的:(Poco,cmake,android,mac)