Skip to content

Commit 28f6118

Browse files
feature: added CPack Support (#35)
* 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 <lars.melchior@gmail.com>
1 parent 48db482 commit 28f6118

9 files changed

Lines changed: 120 additions & 13 deletions

File tree

.cmake-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ parse:
5757
VERSION_HEADER: 1
5858
EXPORT_HEADER: 1
5959
DISABLE_VERSION_SUFFIX: 1
60+
CPACK: 1
6061
DEPENDENCIES: +

.github/workflows/style.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
name: Style
32

43
on:
@@ -12,15 +11,22 @@ on:
1211
jobs:
1312
style:
1413

15-
runs-on: macos-latest
14+
runs-on: ubuntu-latest
1615

1716
steps:
18-
- uses: actions/checkout@v1
17+
- uses: actions/checkout@v3
1918

19+
- name: Setup cmake
20+
uses: jwlawson/actions-setup-cmake@v1.13
21+
with:
22+
cmake-version: 3.18.4
23+
24+
- uses: actions/setup-python@v4
25+
with:
26+
python-version: '3.10'
27+
2028
- name: Install format dependencies
21-
run: |
22-
brew install clang-format
23-
pip3 install cmake_format==0.6.13 pyyaml
29+
run: pip3 install clang-format==14.0.6 cmake_format==0.6.11 pyyaml
2430

2531
- name: configure
2632
run: cmake -S test/style -B build/style

.github/workflows/test.yml

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
name: Test
32

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

1615
jobs:
17-
build:
16+
install:
1817
runs-on: ubuntu-latest
1918

2019
steps:
21-
- uses: actions/checkout@v2
20+
- uses: actions/checkout@v3
2221

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

27+
- name: Setup cmake
28+
uses: jwlawson/actions-setup-cmake@v1.13
29+
with:
30+
cmake-version: 3.18.4
31+
2832
- name: test and install local build
2933
run: |
3034
cmake -S test -B build/local
3135
cmake --build build/local
3236
cmake --build build/local --target test
33-
sudo cmake --build build/local --target install
37+
sudo -E cmake --build build/local --target install
38+
39+
- name: test installed build
40+
run: |
41+
cmake -S test -B build/installed -D TEST_INSTALLED_VERSION=1
42+
cmake --build build/installed
43+
cmake --build build/installed --target test
44+
45+
package:
46+
runs-on: ubuntu-latest
47+
48+
steps:
49+
- uses: actions/checkout@v3
50+
51+
- uses: actions/cache@v3
52+
with:
53+
path: ${{ github.workspace }}/cpm_modules
54+
key: ${{ github.workflow }}-cpm-modules-${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}
55+
56+
- name: Setup cmake
57+
uses: jwlawson/actions-setup-cmake@v1.13
58+
with:
59+
cmake-version: 3.18.4
60+
61+
- name: build for packaging
62+
run: |
63+
cmake -S test -B build/local -DTEST_CPACK=YES
64+
cmake --build build/local
65+
cmake --build build/local --target test
66+
cd build/local
67+
cpack -G DEB .
68+
dpkg -I *deb | grep "Maintainer: Foo Bar <foo@bar.local>"
69+
sudo -E dpkg -i *.deb
3470
3571
- name: test installed build
3672
run: |

CMakeLists.txt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function(packageProject)
1212
cmake_parse_arguments(
1313
PROJECT
1414
""
15-
"NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;EXPORT_HEADER;VERSION_HEADER;NAMESPACE;DISABLE_VERSION_SUFFIX;ARCH_INDEPENDENT;INCLUDE_HEADER_PATTERN"
15+
"NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;EXPORT_HEADER;VERSION_HEADER;NAMESPACE;DISABLE_VERSION_SUFFIX;ARCH_INDEPENDENT;INCLUDE_HEADER_PATTERN;CPACK;"
1616
"DEPENDENCIES"
1717
${ARGN}
1818
)
@@ -34,6 +34,9 @@ function(packageProject)
3434
# first place we also provide an alias to ensure that local and installed versions have the same
3535
# name
3636
if(DEFINED PROJECT_NAMESPACE)
37+
if(PROJECT_CPACK)
38+
set(CPACK_PACKAGE_NAMESPACE ${PROJECT_NAMESPACE})
39+
endif()
3740
set(PROJECT_NAMESPACE ${PROJECT_NAMESPACE}::)
3841
add_library(${PROJECT_NAMESPACE}${PROJECT_NAME} ALIAS ${PROJECT_NAME})
3942
endif()
@@ -179,4 +182,41 @@ function(packageProject)
179182
${PROJECT_VERSION}
180183
CACHE INTERNAL ""
181184
)
185+
186+
if(PROJECT_CPACK)
187+
if(CPACK_PACKAGE_NAMESPACE)
188+
set(CPACK_PACKAGE_NAME ${CPACK_PACKAGE_NAMESPACE}-${PROJECT_NAME})
189+
else()
190+
set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
191+
endif()
192+
if(NOT CPACK_PACKAGE_DESCRIPTION_SUMMARY)
193+
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PROJECT_DESCRIPTION}")
194+
endif()
195+
if(NOT CPACK_PACKAGE_HOMEPAGE_URL)
196+
set(CPACK_PACKAGE_HOMEPAGE_URL "${PROJECT_HOMEPAGE_URL}")
197+
endif()
198+
set(CPACK_VERBATIM_VARIABLES YES)
199+
set(CPACK_THREADS 0)
200+
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
201+
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
202+
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
203+
204+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
205+
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
206+
endif()
207+
208+
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
209+
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
210+
set(CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
211+
OWNER_READ
212+
OWNER_WRITE
213+
OWNER_EXECUTE
214+
GROUP_READ
215+
GROUP_EXECUTE
216+
WORLD_READ
217+
WORLD_EXECUTE
218+
)
219+
220+
include(CPack)
221+
endif()
182222
endfunction()

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Creating installable CMake scripts always requires a large amount of boilerplate code to get things working.
66
This small script should simplify the CMake packaging process into a single, easy-to-use command.
77

8+
PackageProject.cmake has been tested with CMake v3.18.4.
9+
810
## Usage
911

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

55+
## CPack
56+
57+
We provide only the most basic variables for CPack generation, however this might not be enough
58+
for each and every generator.
59+
60+
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`:
61+
62+
```cmake
63+
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Foo Bar <foo@bar.local>")
64+
```
65+
5166
If you need extra functionality feel free to open an issue or a PR.

test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ if(TEST_INSTALLED_VERSION)
1313
find_package(namespaced_dependency 4.5.6 REQUIRED)
1414
find_package(transitive_dependency 7.8.9 REQUIRED)
1515
else()
16+
if(TEST_CPACK)
17+
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Foo Bar <foo@bar.local>")
18+
endif()
1619
add_subdirectory(dependency)
1720
add_subdirectory(namespaced_dependency)
1821
add_subdirectory(transitive_dependency)

test/dependency/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ project(
44
dependency
55
VERSION 1.2
66
LANGUAGES CXX
7+
DESCRIPTION "A dependency for testing PackageProject.cmake"
78
)
89

910
add_library(dependency STATIC source/dependency.cpp)
@@ -23,4 +24,5 @@ packageProject(
2324
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
2425
VERSION_HEADER "dependency/version.h"
2526
DEPENDENCIES ""
27+
CPACK "${TEST_CPACK}"
2628
)

test/namespaced_dependency/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ project(
44
namespaced_dependency
55
VERSION 4.5.6
66
LANGUAGES CXX
7+
DESCRIPTION "A namespaced dependency for testing PackageProject.cmake"
78
)
89

910
set(PROJECT_NAMESPACE "ns")
@@ -28,4 +29,5 @@ packageProject(
2829
VERSION_HEADER "namespaced_dependency/version.h"
2930
INCLUDE_HEADER_PATTERN "*.h"
3031
DEPENDENCIES ""
32+
CPACK "${TEST_CPACK}"
3133
)

test/transitive_dependency/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ project(
44
transitive_dependency
55
VERSION 7.8.9.21948124
66
LANGUAGES CXX
7+
DESCRIPTION "A transitive dependency for testing PackageProject.cmake"
78
)
89

910
include(cmake/CPM.cmake)
@@ -47,4 +48,5 @@ packageProject(
4748
VERSION_HEADER "transitive_dependency/version.h"
4849
EXPORT_HEADER "transitive_dependency/export.h"
4950
DEPENDENCIES "fmt 7.1.3;cxxopts 2.2.0"
51+
CPACK "${TEST_CPACK}"
5052
)

0 commit comments

Comments
 (0)