用了那麼多年 c++,今天才搞明白 cmake 該怎麼用……html
cmake 是一個跨平臺的 c++ 構建工具,與 makefile 相似,可是 makefile 更關注依賴,cmake 更關注構建自己,因此語法上要比makefile 要簡潔清晰一些,而最近發現 cmake 原來還自帶了依賴管理的功能,瞬間以爲以前的用法都過低級了……c++
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
庫文件git
這裏核心的命令是 ExternalProject_Add,功能很強大,支持不一樣的地址去獲取依賴,能夠是打包文件的 URL
,好比 github 上的某個項目的 tag,或者像 boost 這種,在官網提供的下載連接,也能夠直接是 GIT_REPOSITORY
,通常建議直接使用打包的 tag,由於比較快,並且有固定的 tag,比較好作版本管理,可是有些項目引用了外部項目須要執行 git submodule update --init
,這種就比較適合用 git 地址,會自動下載依賴模塊github
另外就是編譯這個過程,若是是標準的使用 cmake 構建的項目,基本不須要額外的配置,會自動編譯,我通常習慣設置一個編譯後的 install 目錄,能夠經過 CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${DMP_CLIENT_SOURCE_DIR}/third/gtest/build
設置 cmake 的參數來實現,還有一些直接使用 makefile 構建的項目,須要本身去配置這個構建的過程,還有就是像 boost 這種,本身搞了工具,反正基本上每一個庫都會有些不同,都會有些小問題須要解決,仍是挺麻煩的,可是多了以後也都是同樣的套路redis
總結一下就是這個功能有,可是使用起來仍是挺麻煩的,也有人爲了簡化這種配置,基於這個功能整了一個 cpm,惋惜如今已經再也不維護了,並且裏面不少庫也都找不到shell
在發現這個依賴管理以前,咱們是經過 shell 腳原本下載依賴的,雖然醜陋一點,但也基本能解決依賴的問題,相比之下,這種方式統一在了 CMakeLists.txt
裏面,可讀性上會更好一些,使用上面編譯安裝的命令都統一了,不須要執行額外的腳本,也會更方便一些json
可是依舊很醜陋……可能歷史的包袱過重吧,各類各樣的庫,五花八門的構建方式,cmake 能作到這樣已經很不錯了bootstrap
include_directories( "${DMP_CLIENT_SOURCE_DIR}/third/gtest/build/include" ) link_directories( "${DMP_CLIENT_SOURCE_DIR}/third/gtest/build/lib" )
這樣咱們就能使用剛剛下載的 gtest 依賴了工具
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
執行單測了
install(TARGETS dmpclient DESTINATION lib) install(DIRECTORY ${DMP_CLIENT_SOURCE_DIR}/include/datasource DESTINATION include/dmpclient)
install 更簡單,指定源和目標便可
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)