From 426026a22af98451bbbf60a500b97e92bd977100 Mon Sep 17 00:00:00 2001 From: hz Date: Tue, 19 Nov 2024 07:55:07 +0800 Subject: [PATCH] improved load-cmdr-cxx.cmake --- README.md | 111 +++--------------- ...{deps-cmdr11.cmake => load-cmdr-cxx.cmake} | 17 ++- 2 files changed, 34 insertions(+), 94 deletions(-) rename cmake/{deps-cmdr11.cmake => load-cmdr-cxx.cmake} (90%) diff --git a/README.md b/README.md index 5154288..0ad246f 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,10 @@ cmake --install build/ # cmake --install build/ --prefix ./dist/install --strip # sudo cp -R ./dist/install/include/* /usr/local/include/ # sudo cp -R ./dist/install/lib/cmake/cmdr11 /usr/local/lib/cmake/ +# +# macOS users could install to Homebrew directly without super privilidges: +# cmake --install build/ --prefix $(brew --prefix) --strip +# rm -rf ./build cd .. ``` @@ -202,107 +206,30 @@ For more information, please refer to the chapter [Others](#others). #### CMake ExternalProject -Adding cmdr11 with `ExternalProject` is possible. A `deps-cmdr11.cmake` could be: - -```cmake -# -# FOR CMDR11 -# - -# message(STATUS "cmdr-cxx: defined macro add_cmdr_cxx_to") -macro(add_cmdr_cxx_to target) - find_package(cmdr11) - - set(CMDR_CXX_STAGE_DIR "${CMAKE_CURRENT_BINARY_DIR}/cmdr-cxx-stage") - - option(CMAKE_VERBOSE_DEBUG "for cmdr-cxx, enable CMAKE_VERBOSE_MAKEFILE to print command line before execute them" OFF) - option(CMDR_NO_3RDPARTY "for cmdr-cxx, disable source codes and targets alternate to yaml-cpp and others" ON) - - if(${CMDR11_FOUND}) - message(STATUS "cmdr-cxx: package FOUND at ${CMDR11_INCLUDE_DIR}, ${CMDR11_VERSION}") - message(STATUS "cmdr-cxx: add cmdr-cxx v${CMDR11_VERSION_STRING} module to '${target}' from CMake Modules registry.") - target_link_libraries(${target} - PRIVATE - ${CMDR11_LIBRARIES} - ) - else() - message(STATUS "cmdr-cxx: not found, try pulling a local one...") - - set(CMDR_CXX_TGT_NAME "third-cmdr-cxx") - include(ExternalProject) - ExternalProject_Add(${CMDR_CXX_TGT_NAME} - GIT_REPOSITORY https://github.com/hedzr/cmdr-cxx - GIT_TAG origin/master # v0.3.13 - GIT_SHALLOW 1 - GIT_PROGRESS ON - - # STEP_TARGETS build - # SOURCE_DIR "${PROJECT_SOURCE_DIR}/third-party/cmdr-cxx-src" - # BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/cmdr-cxx-build" - CMAKE_ARGS - -DCMAKE_VERBOSE_DEBUG:BOOL=${CMAKE_VERBOSE_DEBUG} - -DCMDR_NO_3RDPARTY:BOOL=${CMDR_NO_3RDPARTY} - -DENABLE_TESTS:BOOL=OFF - -DENABLE_CLI_APP:BOOL=OFF - -DENABLE_AUTOMATE_TESTS:BOOL=OFF - -DBUILD_DOCUMENTATION:BOOL=OFF - -DBUILD_EXAMPLES:BOOL=OFF - --no-warn-unused-cli - -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} - -DCMAKE_INSTALL_PREFIX:PATH=${CMDR_CXX_STAGE_DIR} - - # -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} - # -DCMAKE_INSTALL_PREFIX:PATH=${EXECUTABLE_OUTPUT_PATH} - BUILD_COMMAND ${CMAKE_COMMAND} -E echo "[cmdr-cxx] Starting $ build, install_prefix: ${CMAKE_INSTALL_PREFIX} or ${CMDR_CXX_STAGE_DIR}, BINARY_DIR: " - - # COMMAND ${CMAKE_COMMAND} -E sudo "chmod a+w /usr/local/lib" - COMMAND ${CMAKE_COMMAND} --build --config $ - COMMAND ${CMAKE_COMMAND} -E echo "[cmdr-cxx] $ build complete" - ) - - message(STATUS "cmdr-cxx: add '${CMDR_CXX_TGT_NAME}' module to '${target}' from building dir.") - message(STATUS "cmdr-cxx: CI_RUNNING = $ENV{CI_RUNNING}") - message(STATUS "cmdr-cxx: add_dependencies") - add_dependencies(${target} ${CMDR_CXX_TGT_NAME}) - - set(CMDR11_INCLUDE_DIR ${CMDR_CXX_STAGE_DIR}/include) - set(CMDR11_LIB_DIR ${CMDR_CXX_STAGE_DIR}/lib) - endif() - - target_include_directories(${target} PRIVATE - $ - $ - /usr/local/include - /opt/homebrew/include - - # ${CMDR_CXX_STAGE_DIR}/include - ${CMDR11_INCLUDE_DIR} - ) - target_link_directories(${target} PRIVATE - /usr/local/lib - - # ${CMDR_CXX_STAGE_DIR}/lib - ${CMDR11_LIBRARY_DIR} +Adding cmdr11 with `ExternalProject` is possible. A `load-cmdr-cxx.cmake` was provided to make integrating `cmdr-cxx` easier. - # ${CMAKE_CURRENT_BINARY_DIR}/${CMDR_CXX_TGT_NAME}-prefix/src/${CMDR_CXX_TGT_NAME}-build - ) - message(STATUS "cmdr-cxx: include-dir = ${CMDR11_INCLUDE_DIR}") -endmacro() -``` - -And the only things you need to do in your CMakeLists.txt are: +Extract `cmake/load-cmdr-cxx.cmake` into your project, load it and use `add-cmdr-cxx-to` macro. For example, your cli-app could be: ```cmake +# define_cxx_executable_project(myapp +# PREFIX myapp +# LIBRARIES ${myapp_libs} +# SOURCES ${myapp_source_files} +# INCLUDE_DIRECTORIES ${myapp_INCDIR} +# ) +# enable_version_increaser(myapp-cli myapp my MY_) + project(cmdr-cli-demo) add_executable(${PROJECT_NAME} cmdr_main.cc) target_include_directories(${PROJECT_NAME} PRIVATE $ ) -include(deps-cmdr11) # include our deps-cmdr11.cmake -add_cmdr_cxx_to(${PROJECT_NAME}) + +include(load-cmdr-cxx) # load ME here. +add_cmdr_cxx_to(myapp) # attach cmdr11::cmdr11 ``` -That's all. +It works. ### Integrate to your cmake script @@ -321,7 +248,7 @@ set_target_properties(${PROJECT_NAME} PROPERTIES ) ``` -Or you can download [deps-cmdr11.cmake](https://github.com/hedzr/cmdr-cxx/blob/master/cmake/deps-cmdr11.cmake) and +Or you can download [load-cmdr-cxx.cmake](https://github.com/hedzr/cmdr-cxx/blob/master/cmake/load-cmdr-cxx.cmake) and include it: ```cmake diff --git a/cmake/deps-cmdr11.cmake b/cmake/load-cmdr-cxx.cmake similarity index 90% rename from cmake/deps-cmdr11.cmake rename to cmake/load-cmdr-cxx.cmake index db71d86..5ea25f7 100755 --- a/cmake/deps-cmdr11.cmake +++ b/cmake/load-cmdr-cxx.cmake @@ -1,5 +1,18 @@ # -# FOR CMDR11 +# TO LOADING CMDR11 / CMDR-CXX EASILY: +# +# define_cxx_executable_project(myapp +# PREFIX myapp +# LIBRARIES ${myapp_libs} +# SOURCES ${myapp_source_files} +# INCLUDE_DIRECTORIES ${myapp_INCDIR} +# ) +# enable_version_increaser(myapp-cli myapp my MY_) +# +# include(load-cmdr-cxx) # load ME here. +# add_cmdr_cxx_to(myapp) # attach cmdr11::cmdr11 +# + # # message(STATUS "cmdr-cxx: defined macro add_cmdr_cxx_to") @@ -15,7 +28,7 @@ macro(add_cmdr_cxx_to target) message(STATUS "cmdr-cxx: package FOUND at ${CMDR11_INCLUDE_DIR}, ${CMDR11_VERSION}") message(STATUS "cmdr-cxx: add cmdr-cxx v${CMDR11_VERSION_STRING} module to '${target}' from CMake Modules registry.") target_link_libraries(${target} - PRIVATE + # PRIVATE ${CMDR11_LIBRARIES} ) else()