Skip to content

Commit

Permalink
Extended example_package example
Browse files Browse the repository at this point in the history
  • Loading branch information
craflin committed Mar 14, 2020
1 parent a7fe57c commit 26e892c
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 139 deletions.
109 changes: 104 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -88,7 +88,7 @@ install(DIRECTORY include
)
install(EXPORT ${PROJECT_NAME}Config
DESTINATION lib/${PROJECT_NAME}
NAMESPACE example_project2::
NAMESPACE example_package::
)
```

Expand All @@ -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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PUBLIC $<INSTALL_INTERFACE:include>
)
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
Expand Down
89 changes: 59 additions & 30 deletions examples/example_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PUBLIC $<INSTALL_INTERFACE:include>
)

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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PUBLIC $<INSTALL_INTERFACE:include>
)

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()
75 changes: 0 additions & 75 deletions examples/example_project3/CDeploy

This file was deleted.

29 changes: 0 additions & 29 deletions examples/example_project3/CMakeLists.txt

This file was deleted.

0 comments on commit 26e892c

Please sign in to comment.