Skip to content

Commit

Permalink
feature: added CPack Support (#35)
Browse files Browse the repository at this point in the history
* feature: added CPack Support

* Added test case for cpack

* refactor testing for cpack

* bump CI CMake version to 3.18.4

* remove redundant apt install code

---------

Co-authored-by: Lars Melchior <[email protected]>
  • Loading branch information
schtobia and TheLartians authored May 29, 2023
1 parent 48db482 commit 28f6118
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 13 deletions.
1 change: 1 addition & 0 deletions .cmake-format
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ parse:
VERSION_HEADER: 1
EXPORT_HEADER: 1
DISABLE_VERSION_SUFFIX: 1
CPACK: 1
DEPENDENCIES: +
18 changes: 12 additions & 6 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
name: Style

on:
Expand All @@ -12,15 +11,22 @@ on:
jobs:
style:

runs-on: macos-latest
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3

- name: Setup cmake
uses: jwlawson/[email protected]
with:
cmake-version: 3.18.4

- uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install format dependencies
run: |
brew install clang-format
pip3 install cmake_format==0.6.13 pyyaml
run: pip3 install clang-format==14.0.6 cmake_format==0.6.11 pyyaml

- name: configure
run: cmake -S test/style -B build/style
Expand Down
48 changes: 42 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
name: Test

on:
Expand All @@ -14,23 +13,60 @@ env:
CPM_SOURCE_CACHE: ${{ github.workspace }}/cpm_modules

jobs:
build:
install:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: "**/cpm_modules"
path: ${{ github.workspace }}/cpm_modules
key: ${{ github.workflow }}-cpm-modules-${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}

- name: Setup cmake
uses: jwlawson/[email protected]
with:
cmake-version: 3.18.4

- name: test and install local build
run: |
cmake -S test -B build/local
cmake --build build/local
cmake --build build/local --target test
sudo cmake --build build/local --target install
sudo -E cmake --build build/local --target install
- name: test installed build
run: |
cmake -S test -B build/installed -D TEST_INSTALLED_VERSION=1
cmake --build build/installed
cmake --build build/installed --target test
package:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- uses: actions/cache@v3
with:
path: ${{ github.workspace }}/cpm_modules
key: ${{ github.workflow }}-cpm-modules-${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}

- name: Setup cmake
uses: jwlawson/[email protected]
with:
cmake-version: 3.18.4

- name: build for packaging
run: |
cmake -S test -B build/local -DTEST_CPACK=YES
cmake --build build/local
cmake --build build/local --target test
cd build/local
cpack -G DEB .
dpkg -I *deb | grep "Maintainer: Foo Bar <[email protected]>"
sudo -E dpkg -i *.deb
- name: test installed build
run: |
Expand Down
42 changes: 41 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function(packageProject)
cmake_parse_arguments(
PROJECT
""
"NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;EXPORT_HEADER;VERSION_HEADER;NAMESPACE;DISABLE_VERSION_SUFFIX;ARCH_INDEPENDENT;INCLUDE_HEADER_PATTERN"
"NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;EXPORT_HEADER;VERSION_HEADER;NAMESPACE;DISABLE_VERSION_SUFFIX;ARCH_INDEPENDENT;INCLUDE_HEADER_PATTERN;CPACK;"
"DEPENDENCIES"
${ARGN}
)
Expand All @@ -34,6 +34,9 @@ function(packageProject)
# first place we also provide an alias to ensure that local and installed versions have the same
# name
if(DEFINED PROJECT_NAMESPACE)
if(PROJECT_CPACK)
set(CPACK_PACKAGE_NAMESPACE ${PROJECT_NAMESPACE})
endif()
set(PROJECT_NAMESPACE ${PROJECT_NAMESPACE}::)
add_library(${PROJECT_NAMESPACE}${PROJECT_NAME} ALIAS ${PROJECT_NAME})
endif()
Expand Down Expand Up @@ -179,4 +182,41 @@ function(packageProject)
${PROJECT_VERSION}
CACHE INTERNAL ""
)

if(PROJECT_CPACK)
if(CPACK_PACKAGE_NAMESPACE)
set(CPACK_PACKAGE_NAME ${CPACK_PACKAGE_NAMESPACE}-${PROJECT_NAME})
else()
set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
endif()
if(NOT CPACK_PACKAGE_DESCRIPTION_SUMMARY)
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PROJECT_DESCRIPTION}")
endif()
if(NOT CPACK_PACKAGE_HOMEPAGE_URL)
set(CPACK_PACKAGE_HOMEPAGE_URL "${PROJECT_HOMEPAGE_URL}")
endif()
set(CPACK_VERBATIM_VARIABLES YES)
set(CPACK_THREADS 0)
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
endif()

set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
set(CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
OWNER_READ
OWNER_WRITE
OWNER_EXECUTE
GROUP_READ
GROUP_EXECUTE
WORLD_READ
WORLD_EXECUTE
)

include(CPack)
endif()
endfunction()
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Creating installable CMake scripts always requires a large amount of boilerplate code to get things working.
This small script should simplify the CMake packaging process into a single, easy-to-use command.

PackageProject.cmake has been tested with CMake v3.18.4.

## Usage

To create an installable target for your current project, add the following to your CMakeLists.txt.
Expand Down Expand Up @@ -45,7 +47,20 @@ packageProject(
# (optional) option to ignore target architecture for package resolution
# defaults to YES for header only (i.e. INTERFACE) libraries
ARCH_INDEPENDENT YES
# (optional) option to generate CPack variables
CPACK YES
)
```

## CPack

We provide only the most basic variables for CPack generation, however this might not be enough
for each and every generator.

For example, Debians' DEB generator needs additionally [`CPACK_DEBIAN_PACKAGE_MAINTAINER`](https://cmake.org/cmake/help/latest/cpack_gen/deb.html#variable:CPACK_DEBIAN_PACKAGE_MAINTAINER) or a `CPACK_PACKAGE_CONTACT`:

```cmake
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Foo Bar <[email protected]>")
```

If you need extra functionality feel free to open an issue or a PR.
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ if(TEST_INSTALLED_VERSION)
find_package(namespaced_dependency 4.5.6 REQUIRED)
find_package(transitive_dependency 7.8.9 REQUIRED)
else()
if(TEST_CPACK)
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Foo Bar <[email protected]>")
endif()
add_subdirectory(dependency)
add_subdirectory(namespaced_dependency)
add_subdirectory(transitive_dependency)
Expand Down
2 changes: 2 additions & 0 deletions test/dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ project(
dependency
VERSION 1.2
LANGUAGES CXX
DESCRIPTION "A dependency for testing PackageProject.cmake"
)

add_library(dependency STATIC source/dependency.cpp)
Expand All @@ -23,4 +24,5 @@ packageProject(
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
VERSION_HEADER "dependency/version.h"
DEPENDENCIES ""
CPACK "${TEST_CPACK}"
)
2 changes: 2 additions & 0 deletions test/namespaced_dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ project(
namespaced_dependency
VERSION 4.5.6
LANGUAGES CXX
DESCRIPTION "A namespaced dependency for testing PackageProject.cmake"
)

set(PROJECT_NAMESPACE "ns")
Expand All @@ -28,4 +29,5 @@ packageProject(
VERSION_HEADER "namespaced_dependency/version.h"
INCLUDE_HEADER_PATTERN "*.h"
DEPENDENCIES ""
CPACK "${TEST_CPACK}"
)
2 changes: 2 additions & 0 deletions test/transitive_dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ project(
transitive_dependency
VERSION 7.8.9.21948124
LANGUAGES CXX
DESCRIPTION "A transitive dependency for testing PackageProject.cmake"
)

include(cmake/CPM.cmake)
Expand Down Expand Up @@ -47,4 +48,5 @@ packageProject(
VERSION_HEADER "transitive_dependency/version.h"
EXPORT_HEADER "transitive_dependency/export.h"
DEPENDENCIES "fmt 7.1.3;cxxopts 2.2.0"
CPACK "${TEST_CPACK}"
)

0 comments on commit 28f6118

Please sign in to comment.