1. 程式人生 > >cmake 的正確打開方式

cmake 的正確打開方式

gen libs 添加 總結 cot 目錄 版本管理 mkdir 但是

用了那麽多年 c++,今天才搞明白 cmake 該怎麽用……

cmake 是一個跨平臺的 c++ 構建工具,與 makefile 類似,但是 makefile 更關註依賴,cmake 更關註構建本身,所以語法上要比makefile 要簡潔清晰一些,而最近發現 cmake 原來還自帶了依賴管理的功能,瞬間覺得之前的用法都太低級了……

依賴管理

include(ExternalProject)

add_custom_target(third)

ExternalProject_Add(
    google_gtest
    URL https://github.com/google/googletest/archive/release-1.8.0.zip
    PREFIX ${DMP_CLIENT_SOURCE_DIR}/third/gtest
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${DMP_CLIENT_SOURCE_DIR}/third/gtest/build -DBUILD_SHARED_LIBS=OFF
)
add_dependencies(third google_gtest)

上面這段代碼就可以自動下載 gtest 依賴到本地的 third/gtest/ 目錄,並安裝在 third/gtest/build 下,這個目錄下面將有兩個目錄, include 頭文件以及 lib 庫文件

這裏核心的命令是 ExternalProject_Add,功能很強大,支持不同的地址去獲取依賴,可以是打包文件的 URL,比如 github 上的某個項目的 tag,或者像 boost 這種,在官網提供的下載鏈接,也可以直接是 GIT_REPOSITORY,一般建議直接使用打包的 tag,因為比較快,而且有固定的 tag,比較好做版本管理,但是有些項目引用了外部項目需要執行 git submodule update --init

,這種就比較適合用 git 地址,會自動下載依賴模塊

另外就是編譯這個過程,如果是標準的使用 cmake 構建的項目,基本不需要額外的配置,會自動編譯,我一般習慣設置一個編譯後的 install 目錄,可以通過 CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${DMP_CLIENT_SOURCE_DIR}/third/gtest/build 設置 cmake 的參數來實現,還有一些直接使用 makefile 構建的項目,需要自己去配置這個構建的過程,還有就是像 boost 這種,自己搞了工具,反正基本上每個庫都會有些不一樣,都會有些小問題需要解決,還是挺麻煩的,但是多了之後也都是一樣的套路

總結一下就是這個功能有,但是使用起來還是挺麻煩的,也有人為了簡化這種配置,基於這個功能整了一個 cpm,可惜現在已經不再維護了,而且裏面很多庫也都找不到

在發現這個依賴管理之前,我們是通過 shell 腳本來下載依賴的,雖然醜陋一點,但也基本能解決依賴的問題,相比之下,這種方式統一在了 CMakeLists.txt 裏面,可讀性上會更好一些,使用上面編譯安裝的命令都統一了,不需要執行額外的腳本,也會更方便一些

但是依舊很醜陋……可能歷史的包袱太重吧,各種各樣的庫,五花八門的構建方式,cmake 能做到這樣已經很不錯了

添加頭文件目錄和庫搜索目錄

include_directories(
    "${DMP_CLIENT_SOURCE_DIR}/third/gtest/build/include"    
)

link_directories(
    "${DMP_CLIENT_SOURCE_DIR}/third/gtest/build/lib"
)

這樣我們就能使用剛剛下載的 gtest 依賴了

生成 proto 代碼

add_custom_command(
    OUTPUT ${DMP_CLIENT_SOURCE_DIR}/proto/dmpval_pb_message.pb.cc ${DMP_CLIENT_SOURCE_DIR}/proto/dmpval_pb_message.pb.h
    DEPENDS ${DMP_CLIENT_SOURCE_DIR}/proto/dmpval_pb_message.proto
    COMMAND ${DMP_CLIENT_SOURCE_DIR}/third/protobuf/build/bin/protoc -I. --cpp_out=. dmpval_pb_message.proto
    WORKING_DIRECTORY ${DMP_CLIENT_SOURCE_DIR}/proto
)
add_custom_target(
    pbout
    DEPENDS ${DMP_CLIENT_SOURCE_DIR}/proto/dmpval_pb_message.pb.cc
)
add_dependencies(pbout third)

add_custom_command 可以執行自定義的命令,然後再使用 add_custom_target 生成一個 pbout 的目標供下面的可執行程序依賴

添加可執行文件

add_executable(test_dmpkey test/dmpkey_test.cpp ${dmpkey_source})
add_dependencies(test_dmpkey third pbout)
target_link_libraries(
    test_dmpkey
    gtest
    murmur3
    pthread
)

使用代碼文件生成一個測試的可執行程序 test_dmpkey,並讓這個可執行程序依賴第三方依賴 third 和我們的 proto 編譯結果 pbout

單元測試

enable_testing()
add_test(NAME dmpkey_test COMMAND test_dmpkey)
add_test(NAME dmpval_test COMMAND test_dmpval)

增加單元測試比較容易,使用 add_test 命令,在 COMMAND 後面添加需要執行的測試命令即可,添加後就可以使用 make test 執行單測了

make install

install(TARGETS dmpclient DESTINATION lib)
install(DIRECTORY ${DMP_CLIENT_SOURCE_DIR}/include/datasource DESTINATION include/dmpclient)

install 更簡單,指定源和目標即可

CMakeLists.txt 參考代碼

cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)
project(DMP_CLIENT)

find_package(Threads REQUIRED)
include(ExternalProject)

add_custom_target(third)

ExternalProject_Add(
    google_gtest
    URL https://github.com/google/googletest/archive/release-1.8.0.zip
    PREFIX ${DMP_CLIENT_SOURCE_DIR}/third/gtest
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${DMP_CLIENT_SOURCE_DIR}/third/gtest/build -DBUILD_SHARED_LIBS=OFF
)
add_dependencies(third google_gtest)

ExternalProject_Add(
    google_protobuf
    URL https://github.com/google/protobuf/archive/v3.5.2.zip
    PREFIX ${DMP_CLIENT_SOURCE_DIR}/third/protobuf
    BUILD_IN_SOURCE true
    CONFIGURE_COMMAND ""
    BUILD_COMMAND sh autogen.sh && ./configure --prefix=${DMP_CLIENT_SOURCE_DIR}/third/protobuf/build --disable-shared && make -j8
    INSTALL_COMMAND make install
)
add_dependencies(third google_protobuf)

ExternalProject_Add(
    boostorg_boost
    URL https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.tar.gz
    PREFIX ${DMP_CLIENT_SOURCE_DIR}/third/boost
    BUILD_IN_SOURCE true
    CONFIGURE_COMMAND ""
    BUILD_COMMAND sh bootstrap.sh && ./b2 link=static -j8
    INSTALL_COMMAND ./b2 install --prefix=${DMP_CLIENT_SOURCE_DIR}/third/boost/build
)
add_dependencies(third boostorg_boost)

ExternalProject_Add(
    peterscott_murmur3
    URL https://github.com/PeterScott/murmur3/archive/master.zip
    PREFIX ${DMP_CLIENT_SOURCE_DIR}/third/murmur3
    BUILD_IN_SOURCE true
    CONFIGURE_COMMAND ""
    BUILD_COMMAND gcc -c murmur3.c && ar rcs libmurmur3.a murmur3.o
    INSTALL_COMMAND mkdir -p ../../build/{include,lib} && cp murmur3.h ../../build/include && cp libmurmur3.a ../../build/lib
)
add_dependencies(third peterscott_murmur3)

ExternalProject_Add(
    vipshop_hiredis_vip
    URL https://github.com/vipshop/hiredis-vip/archive/0.3.0.zip
    PREFIX ${DMP_CLIENT_SOURCE_DIR}/third/hiredis-vip
    BUILD_IN_SOURCE true
    CONFIGURE_COMMAND ""
    BUILD_COMMAND make
    INSTALL_COMMAND PREFIX=${DMP_CLIENT_SOURCE_DIR}/third/hiredis-vip/build make install
    COMMAND rm -rf ${DMP_CLIENT_SOURCE_DIR}/third/hiredis-vip/build/lib/*.dylib
)
add_dependencies(third vipshop_hiredis_vip)

ExternalProject_Add(
    aerospike_aerospike_client_c
    GIT_REPOSITORY [email protected]:aerospike/aerospike-client-c.git
    PREFIX ${DMP_CLIENT_SOURCE_DIR}/third/aerospike-client-c
    BUILD_IN_SOURCE true
    CONFIGURE_COMMAND ""
    BUILD_COMMAND make
    INSTALL_COMMAND ls target | xargs -I {} cp -r target/{}/ ${DMP_CLIENT_SOURCE_DIR}/third/aerospike-client-c/build
    COMMAND rm -rf ${DMP_CLIENT_SOURCE_DIR}/third/aerospike-client-c/build/lib/libaerospike.dylib
)
add_dependencies(third aerospike_aerospike_client_c)

ExternalProject_Add(
    nlohmann_json
    URL https://github.com/nlohmann/json/archive/v3.1.2.zip
    PREFIX ${DMP_CLIENT_SOURCE_DIR}/third/json
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${DMP_CLIENT_SOURCE_DIR}/third/json/build -DBUILD_SHARED_LIBS=OFF
)
add_dependencies(third nlohmann_json)

set(CMAKE_CXX_FLAGS "-std=c++11 -O2 -g")

# generate proto struct
add_custom_command(
    OUTPUT ${DMP_CLIENT_SOURCE_DIR}/proto/dmpval_pb_message.pb.cc ${DMP_CLIENT_SOURCE_DIR}/proto/dmpval_pb_message.pb.h
    DEPENDS ${DMP_CLIENT_SOURCE_DIR}/proto/dmpval_pb_message.proto
    COMMAND ${DMP_CLIENT_SOURCE_DIR}/third/protobuf/build/bin/protoc -I. --cpp_out=. dmpval_pb_message.proto
    WORKING_DIRECTORY ${DMP_CLIENT_SOURCE_DIR}/proto
)
add_custom_target(
    pbout
    DEPENDS ${DMP_CLIENT_SOURCE_DIR}/proto/dmpval_pb_message.pb.cc
)
add_dependencies(pbout third)

include_directories(
    "${DMP_CLIENT_SOURCE_DIR}/include"
    "${DMP_CLIENT_SOURCE_DIR}/proto"
    "${DMP_CLIENT_SOURCE_DIR}/third/boost/build/include"
    "${DMP_CLIENT_SOURCE_DIR}/third/murmur3/build/include"
    "${DMP_CLIENT_SOURCE_DIR}/third/gtest/build/include"
    "${DMP_CLIENT_SOURCE_DIR}/third/protobuf/build/include"
    "${DMP_CLIENT_SOURCE_DIR}/third/hiredis-vip/build/include"
    "${DMP_CLIENT_SOURCE_DIR}/third/aerospike-client-c/build/include"
    "${DMP_CLIENT_SOURCE_DIR}/third/json/build/include"
)

link_directories(
    "${DMP_CLIENT_SOURCE_DIR}/third/gtest/build/lib"
    "${DMP_CLIENT_SOURCE_DIR}/third/protobuf/build/lib"
    "${DMP_CLIENT_SOURCE_DIR}/third/murmur3/build/lib"
    "${DMP_CLIENT_SOURCE_DIR}/third/hiredis-vip/build/lib"
    "${DMP_CLIENT_SOURCE_DIR}/third/aerospike-client-c/build/lib"
    "${DMP_CLIENT_SOURCE_DIR}/third/json/build/lib"
)

aux_source_directory(src/dmpkey dmpkey_source)
aux_source_directory(src/dmpval dmpval_source)
aux_source_directory(src/datasource datasource_source)
aux_source_directory(src/dmpclient dmpclient_source)
set(proto_source proto/dmpval_pb_message.pb.cc)
set(all_source ${dmpkey_source} ${dmpval_source} ${datasource_source} ${dmpclient_source} ${proto_source})

add_executable(test_dmpkey test/dmpkey_test.cpp ${dmpkey_source})
add_dependencies(test_dmpkey third pbout)
target_link_libraries(
    test_dmpkey
    gtest
    murmur3
    pthread
)

add_executable(test_dmpval test/dmpval_test.cpp ${dmpval_source} ${proto_source})
add_dependencies(test_dmpval third pbout)
target_link_libraries(
    test_dmpval
    gtest
    protobuf
    pthread
)

add_executable(test_redis_string test/redis_string_test.cpp src/datasource/redis_string.cpp)
add_dependencies(test_redis_string third pbout)
target_link_libraries(
    test_redis_string
    gtest
    hiredis_vip
    pthread
)

add_executable(test_aerospike test/aerospike_test.cpp src/datasource/aerospike.cpp)
add_dependencies(test_aerospike third pbout)
target_link_libraries(
    test_aerospike
    gtest
    pthread
    aerospike
    ssl
    crypto
    z
)

add_executable(test_dmpclient test/dmpclient_test.cpp ${all_source})
add_dependencies(test_dmpclient third pbout)
target_link_libraries(
    test_dmpclient
    gtest
    hiredis_vip
    aerospike
    ssl
    crypto
    z
    protobuf
    murmur3
    pthread
)

enable_testing()
add_test(NAME dmpkey_test COMMAND test_dmpkey)
add_test(NAME dmpval_test COMMAND test_dmpval)
add_test(NAME redis_string_test COMMAND test_redis_string)
add_test(NAME aerospike_test COMMAND test_aerospike)
add_test(NAME dmpclient_test COMMAND test_dmpclient)

add_library(dmpclient STATIC ${all_source})
add_dependencies(dmpclient third pbout)

install(TARGETS dmpclient DESTINATION lib)
install(DIRECTORY ${DMP_CLIENT_SOURCE_DIR}/include/datasource DESTINATION include/dmpclient)
install(DIRECTORY ${DMP_CLIENT_SOURCE_DIR}/include/localcache DESTINATION include/dmpclient)
install(DIRECTORY ${DMP_CLIENT_SOURCE_DIR}/include/dmpclient DESTINATION include/dmpclient)
install(DIRECTORY ${DMP_CLIENT_SOURCE_DIR}/include/dmpkey DESTINATION include/dmpclient)
install(DIRECTORY ${DMP_CLIENT_SOURCE_DIR}/include/dmpval DESTINATION include/dmpclient)

參考鏈接

  • cmake 官方文檔:https://cmake.org/cmake/help/v3.11/manual/cmake-commands.7.html
  • ExternalProject:https://cmake.org/cmake/help/v3.11/module/ExternalProject.html

cmake 的正確打開方式