From 26e892c8541c7dd8fef04b4a20e9055fe5f09245 Mon Sep 17 00:00:00 2001 From: Colin Graf Date: Sat, 14 Mar 2020 18:32:51 +0100 Subject: [PATCH] Extended example_package example --- README.md | 109 +++++++++++++++++++++-- examples/example_package/CMakeLists.txt | 89 +++++++++++------- examples/example_project3/CDeploy | 75 ---------------- examples/example_project3/CMakeLists.txt | 29 ------ 4 files changed, 163 insertions(+), 139 deletions(-) delete mode 100644 examples/example_project3/CDeploy delete mode 100644 examples/example_project3/CMakeLists.txt diff --git a/README.md b/README.md index d290723..46da702 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,11 @@ target_link_libraries(example_binary Example: -libxml2-2.7.8-ubuntu16.04-x64-gcc7.2.0.zip +libxml2-2.7.8-ubuntu16.04-x64-gcc7.2.zip ### Package Contents -Everything should be packaged in one directory with a unique name. This directory will be the argument `PATHS` for `find_package`, so it should provide a CMake package configuration. +Everything should be packaged in one directory with a unique name. This directory will be the argument for `PATHS` of `find_package`, so it should provide a CMake package configuration. ## Creating a CMake Deploy Package @@ -55,7 +55,7 @@ If you are building your project with CMake, you can create a CDeploy package us cmake_minimum_required(VERSION 3.1) cmake_policy(SET CMP0048 NEW) -project(example_project2 VERSION 0.2.0) +project(example_package VERSION 0.2.0) include(CDeploy) include(CPack) @@ -88,7 +88,7 @@ install(DIRECTORY include ) install(EXPORT ${PROJECT_NAME}Config DESTINATION lib/${PROJECT_NAME} - NAMESPACE example_project2:: + NAMESPACE example_package:: ) ``` @@ -97,7 +97,106 @@ The package is then generated using the `package` target in CMake. If you want to create a package with multiple configurations (e.g. Release and Debug) you will need a CMake project dedicated to build the package: ```cmake -todo +cmake_minimum_required(VERSION 3.1) +cmake_policy(SET CMP0048 NEW) + +project(example_package VERSION 0.2.0) + +include(ExternalProject) + +ExternalProject_Add(release + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../example_package" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/extern-release" + CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install-release" + BUILD_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-release" --config Release + INSTALL_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-release" --config Release --target install +) + +ExternalProject_Add(debug + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../example_package" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/extern-debug" + CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install-debug" + BUILD_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-debug" --config Debug + INSTALL_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-debug" --config Debug --target install +) + +include(CDeploy) +include(CPack) + +install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/install-debug/" DESTINATION .) +install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/install-release/" DESTINATION .) +``` + +Using a CMake option, this can be combined into a single CMake project: + +```cmake +cmake_minimum_required(VERSION 3.1) +cmake_policy(SET CMP0048 NEW) + +option(BUILD_PACKAGE "Build a binary package that provide a Release and Debug build" OFF) + +project(example_package VERSION 0.2.0) + +include(CDeploy) +include(CPack) + +if(BUILD_PACKAGE) + +include(ExternalProject) + + ExternalProject_Add(release + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/extern-release" + CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install-release" + BUILD_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-release" --config Release + INSTALL_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-release" --config Release --target install + ) + + ExternalProject_Add(debug + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/extern-debug" + CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install-debug" + BUILD_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-debug" --config Debug + INSTALL_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-debug" --config Debug --target install + ) + + install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/install-debug/" DESTINATION . USE_SOURCE_PERMISSIONS) + install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/install-release/" DESTINATION . USE_SOURCE_PERMISSIONS) + +else() + + set(CMAKE_DEBUG_POSTFIX d) + + add_library(mylib STATIC + src/MyLib.cpp + include/MyLib.hpp + ) + target_include_directories(mylib + PUBLIC $ + PUBLIC $ + ) + + add_executable(mytool + src/Main.cpp + ) + + install(TARGETS mytool + DESTINATION bin + EXPORT ${PROJECT_NAME}Config + ) + install(TARGETS mylib + DESTINATION lib + EXPORT ${PROJECT_NAME}Config + ) + install(DIRECTORY include + DESTINATION . + ) + install(EXPORT ${PROJECT_NAME}Config + DESTINATION lib/cmake/${PROJECT_NAME} + NAMESPACE example_package:: + ) + +endif() ``` ### From an External Project diff --git a/examples/example_package/CMakeLists.txt b/examples/example_package/CMakeLists.txt index 08a7a90..5ad6ed0 100644 --- a/examples/example_package/CMakeLists.txt +++ b/examples/example_package/CMakeLists.txt @@ -1,38 +1,67 @@ cmake_minimum_required(VERSION 3.1) cmake_policy(SET CMP0048 NEW) +option(BUILD_PACKAGE "Build a binary package that provide a Release and Debug build" OFF) + project(example_package VERSION 0.2.0) include(CDeploy) include(CPack) -set(CMAKE_DEBUG_POSTFIX d) - -add_library(mylib STATIC - src/MyLib.cpp - include/MyLib.hpp -) -target_include_directories(mylib - PUBLIC $ - PUBLIC $ -) - -add_executable(mytool - src/Main.cpp -) - -install(TARGETS mytool - DESTINATION bin - EXPORT ${PROJECT_NAME}Config -) -install(TARGETS mylib - DESTINATION lib - EXPORT ${PROJECT_NAME}Config -) -install(DIRECTORY include - DESTINATION . -) -install(EXPORT ${PROJECT_NAME}Config - DESTINATION lib/cmake/${PROJECT_NAME} - NAMESPACE example_package:: -) +if(BUILD_PACKAGE) + +include(ExternalProject) + + ExternalProject_Add(release + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/build-release" + CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install-release" + BUILD_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/build-release" --config Release + INSTALL_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/build-release" --config Release --target install + ) + + ExternalProject_Add(debug + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/build-debug" + CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install-debug" + BUILD_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/build-debug" --config Debug + INSTALL_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/build-debug" --config Debug --target install + ) + + install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/install-debug/" DESTINATION . USE_SOURCE_PERMISSIONS) + install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/install-release/" DESTINATION . USE_SOURCE_PERMISSIONS) + +else() + + set(CMAKE_DEBUG_POSTFIX d) + + add_library(mylib STATIC + src/MyLib.cpp + include/MyLib.hpp + ) + target_include_directories(mylib + PUBLIC $ + PUBLIC $ + ) + + add_executable(mytool + src/Main.cpp + ) + + install(TARGETS mytool + DESTINATION bin + EXPORT ${PROJECT_NAME}Config + ) + install(TARGETS mylib + DESTINATION lib + EXPORT ${PROJECT_NAME}Config + ) + install(DIRECTORY include + DESTINATION . + ) + install(EXPORT ${PROJECT_NAME}Config + DESTINATION lib/cmake/${PROJECT_NAME} + NAMESPACE example_package:: + ) + +endif() \ No newline at end of file diff --git a/examples/example_project3/CDeploy b/examples/example_project3/CDeploy deleted file mode 100644 index 815fdbd..0000000 --- a/examples/example_project3/CDeploy +++ /dev/null @@ -1,75 +0,0 @@ - -function(get_target_arch var) - set(${var} ${CMAKE_SYSTEM_PROCESSOR}) - if(WIN32) - if(MSVC) - if(CMAKE_CL_64) - set(${var} x64) - else() - set(${var} x86) - endif() - elseif(CMAKE_COMPILER_IS_GNUCC) # CMAKE_CXX_COMPILER_ARCHITECTURE_ID does not provide anything with MinGW compilers - execute_process(COMMAND "${CMAKE_CXX_COMPILER}" -v OUTPUT_VARIABLE GCC_VERSION_OUTPUT ERROR_VARIABLE GCC_VERSION_OUTPUT) - if(GCC_VERSION_OUTPUT MATCHES ".*x86_64.*") - set(${var} x64) - else() - set(${var} x86) - endif() - endif() - endif() - set(${var} ${${var}} PARENT_SCOPE) -endfunction() - -function(get_target_compiler var) - if(CMAKE_COMPILER_IS_GNUCC) - string(REGEX REPLACE "([0-9]+\\.[0-9]+).*" "gcc\\1" ${var} "${CMAKE_CXX_COMPILER_VERSION}") - elseif(MSVC) - set(MSVC_YEARS 2008 2010 2012 2013 2015 2017) - set(MSVC_MSC_VERS 1500 1600 1700 1800 1900 1910) - list(FIND MSVC_MSC_VERS ${MSVC_VERSION} MSVC_INDEX) - list(GET MSVC_YEARS ${MSVC_INDEX} MSVS_YEAR) - set(${var} vs${MSVS_YEAR}) - else() - set(${var} ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}) - endif() - set(${var} ${${var}} PARENT_SCOPE) -endfunction() - -function(get_target_os var) - if(WIN32) - set(${var} windows) - else() - file(GLOB ETC_RELEASE_FILES /etc/*-release) - list(REMOVE_ITEM ETC_RELEASE_FILES /etc/lsb-release) - list(GET ETC_RELEASE_FILES 0 ETC_RELEASE_FILE) - file(READ "${ETC_RELEASE_FILE}" ETC_RELEASE_FILE_CONTENT) - string(REGEX REPLACE "=|\n" ";" ETC_RELEASE_FILE_CONTENT "${ETC_RELEASE_FILE_CONTENT}") - string(REGEX REPLACE "; +| +;" ";" ETC_RELEASE_FILE_CONTENT "${ETC_RELEASE_FILE_CONTENT}") - list(FIND ETC_RELEASE_FILE_CONTENT "NAME" NAME_INDEX) - if(NAME_INDEX GREATER -1) - math(EXPR NAME_INDEX "${NAME_INDEX} + 1") - list(GET ETC_RELEASE_FILE_CONTENT "${NAME_INDEX}" ETC_RELEASE_NAME) - else() - list(GET ETC_RELEASE_FILE_CONTENT 0 ETC_RELEASE_NAME) - endif() - string(REGEX MATCH "[^ \"]+" ETC_RELEASE_NAME "${ETC_RELEASE_NAME}") - list(FIND ETC_RELEASE_FILE_CONTENT "VERSION_ID" VERSION_INDEX) - if(NOT VERSION_INDEX GREATER -1) - list(FIND ETC_RELEASE_FILE_CONTENT "VERSION" VERSION_INDEX) - endif() - if(VERSION_INDEX GREATER -1) - math(EXPR VERSION_INDEX "${VERSION_INDEX} + 1") - list(GET ETC_RELEASE_FILE_CONTENT "${VERSION_INDEX}" ETC_RELEASE_VERSION) - string(REGEX MATCH "[^ \"]+" ETC_RELEASE_VERSION "${ETC_RELEASE_VERSION}") - endif() - string(TOLOWER "${ETC_RELEASE_NAME}${ETC_RELEASE_VERSION}" ${var}) - endif() - set(${var} ${${var}} PARENT_SCOPE) -endfunction() - -get_target_arch(CDEPLOY_ARCH) -get_target_compiler(CDEPLOY_COMPILER) -get_target_os(CDEPLOY_OS) - -set(CPACK_GENERATOR "ZIP") -set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION}-${CDEPLOY_OS}-${CDEPLOY_ARCH}-${CDEPLOY_COMPILER}") diff --git a/examples/example_project3/CMakeLists.txt b/examples/example_project3/CMakeLists.txt deleted file mode 100644 index 946337c..0000000 --- a/examples/example_project3/CMakeLists.txt +++ /dev/null @@ -1,29 +0,0 @@ - -cmake_minimum_required(VERSION 3.1) -cmake_policy(SET CMP0048 NEW) - -project(example_project2 VERSION 0.2.0) - -include(ExternalProject) - -ExternalProject_Add(release - SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../example_project2" - BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/extern-release" - CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install-release" - BUILD_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-release" --config Release - INSTALL_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-release" --config Release --target install -) - -ExternalProject_Add(debug - SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../example_project2" - BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/extern-debug" - CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install-debug" - BUILD_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-debug" --config Debug - INSTALL_COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/extern-debug" --config Debug --target install -) - -include(CDeploy) -include(CPack) - -install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/install-debug/" DESTINATION .) -install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/install-release/" DESTINATION .)