用了那么多年 c++,今天才搞明白 cmake 該怎么用……
cmake 是一個跨平臺的 c++ 構(gòu)建工具,與 makefile 類似聊浅,但是 makefile 更關(guān)注依賴,cmake 更關(guān)注構(gòu)建本身现使,所以語法上要比makefile 要簡潔清晰一些低匙,而最近發(fā)現(xiàn) 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 這種报强,在官網(wǎng)提供的下載鏈接灸姊,也可以直接是 GIT_REPOSITORY
,一般建議直接使用打包的 tag秉溉,因為比較快力惯,而且有固定的 tag,比較好做版本管理召嘶,但是有些項目引用了外部項目需要執(zhí)行 git submodule update --init
父晶,這種就比較適合用 git 地址,會自動下載依賴模塊
另外就是編譯這個過程弄跌,如果是標(biāo)準(zhǔn)的使用 cmake 構(gòu)建的項目甲喝,基本不需要額外的配置,會自動編譯铛只,我一般習(xí)慣設(shè)置一個編譯后的 install 目錄埠胖,可以通過 CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${DMP_CLIENT_SOURCE_DIR}/third/gtest/build
設(shè)置 cmake 的參數(shù)來實現(xiàn),還有一些直接使用 makefile 構(gòu)建的項目淳玩,需要自己去配置這個構(gòu)建的過程直撤,還有就是像 boost 這種,自己搞了工具蜕着,反正基本上每個庫都會有些不一樣谋竖,都會有些小問題需要解決,還是挺麻煩的,但是多了之后也都是一樣的套路
總結(jié)一下就是這個功能有蓖乘,但是使用起來還是挺麻煩的锤悄,也有人為了簡化這種配置,基于這個功能整了一個 cpm驱敲,可惜現(xiàn)在已經(jīng)不再維護(hù)了铁蹈,而且里面很多庫也都找不到
在發(fā)現(xiàn)這個依賴管理之前,我們是通過 shell 腳本來下載依賴的众眨,雖然丑陋一點握牧,但也基本能解決依賴的問題,相比之下娩梨,這種方式統(tǒng)一在了 CMakeLists.txt
里面沿腰,可讀性上會更好一些,使用上面編譯安裝的命令都統(tǒng)一了狈定,不需要執(zhí)行額外的腳本颂龙,也會更方便一些
但是依舊很丑陋……可能歷史的包袱太重吧,各種各樣的庫纽什,五花八門的構(gòu)建方式措嵌,cmake 能做到這樣已經(jīng)很不錯了
添加頭文件目錄和庫搜索目錄
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 可以執(zhí)行自定義的命令,然后再使用 add_custom_target 生成一個 pbout 的目標(biāo)供下面的可執(zhí)行程序依賴
添加可執(zhí)行文件
add_executable(test_dmpkey test/dmpkey_test.cpp ${dmpkey_source})
add_dependencies(test_dmpkey third pbout)
target_link_libraries(
test_dmpkey
gtest
murmur3
pthread
)
使用代碼文件生成一個測試的可執(zhí)行程序 test_dmpkey
芦缰,并讓這個可執(zhí)行程序依賴第三方依賴 third
和我們的 proto 編譯結(jié)果 pbout
單元測試
enable_testing()
add_test(NAME dmpkey_test COMMAND test_dmpkey)
add_test(NAME dmpval_test COMMAND test_dmpval)
增加單元測試比較容易企巢,使用 add_test 命令,在 COMMAND 后面添加需要執(zhí)行的測試命令即可让蕾,添加后就可以使用 make test
執(zhí)行單測了
make install
install(TARGETS dmpclient DESTINATION lib)
install(DIRECTORY ${DMP_CLIENT_SOURCE_DIR}/include/datasource DESTINATION include/dmpclient)
install 更簡單浪规,指定源和目標(biāo)即可
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 git@github.com: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)