Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cmake-format
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ parse:
CPACK: 1
RUNTIME_DESTINATION: 1
DEPENDENCIES: +
HEADER_SETS: +
2 changes: 1 addition & 1 deletion .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup cmake
uses: jwlawson/actions-setup-cmake@v1.13
with:
cmake-version: 3.18.4
cmake-version: 3.24.4

- uses: actions/setup-python@v4
with:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Setup cmake
uses: jwlawson/actions-setup-cmake@v1.13
with:
cmake-version: 3.18.4
cmake-version: 3.24.4

- name: test and install local build
run: |
Expand All @@ -38,7 +38,7 @@ jobs:

- name: test installed build
run: |
test -e /usr/local/share/cmake/header_only-1.0/header_onlyConfig.cmake # test if header only library's CMake configs are installed to a ABI-independent dir.
test -e /usr/local/share/cmake/header_only-1.0.0.0/header_onlyConfig.cmake # test if header only library's CMake configs are installed to a ABI-independent dir.
cmake -S test -B build/installed -D TEST_INSTALLED_VERSION=1
cmake --build build/installed
cmake --build build/installed --target test
Expand All @@ -57,7 +57,7 @@ jobs:
- name: Setup cmake
uses: jwlawson/actions-setup-cmake@v1.13
with:
cmake-version: 3.18.4
cmake-version: 3.24.4

- name: build for packaging
run: |
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/build
.*.swp
build/
/out
/.vs
/.cache
Expand Down
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
cmake_minimum_required(VERSION 3.14)
cmake_minimum_required(VERSION 3.14...3.31)

set(PACKAGE_PROJECT_ROOT_PATH
"${CMAKE_CURRENT_LIST_DIR}"
CACHE INTERNAL "The path to the PackageProject directory"
)

# if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.31.0") cmake_policy(SET CMP0177 NEW) endif()

function(packageProject)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
Expand All @@ -13,7 +15,7 @@ function(packageProject)
PROJECT
""
"NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;EXPORT_HEADER;VERSION_HEADER;NAMESPACE;DISABLE_VERSION_SUFFIX;ARCH_INDEPENDENT;INCLUDE_HEADER_PATTERN;CPACK;RUNTIME_DESTINATION"
"DEPENDENCIES"
"DEPENDENCIES;HEADER_SETS"
${ARGN}
)

Expand Down Expand Up @@ -133,6 +135,11 @@ function(packageProject)
set(PROJECT_RUNTIME_DESTINATION ${PROJECT_NAME}${PROJECT_VERSION_SUFFIX})
endif()

if(PROJECT_HEADER_SETS)
# required to install if use in project target since CMake 3.23
set(FILE_SET_ARGS "FILE_SET" "${PROJECT_HEADER_SETS}")
endif()

install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
Expand All @@ -145,7 +152,10 @@ function(packageProject)
COMPONENT "${PROJECT_NAME}_Runtime"
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}/${PROJECT_RUNTIME_DESTINATION}
COMPONENT "${PROJECT_NAME}_Runtime"
PUBLIC_HEADER DESTINATION ${PROJECT_INCLUDE_DESTINATION} COMPONENT "${PROJECT_NAME}_Development"
PUBLIC_HEADER
DESTINATION ${PROJECT_INCLUDE_DESTINATION}
COMPONENT "${PROJECT_NAME}_Development"
${FILE_SET_ARGS}
INCLUDES
DESTINATION "${PROJECT_INCLUDE_DESTINATION}"
)
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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.
PackageProject.cmake has been tested with CMake v3.24.4.

## Usage

Expand All @@ -15,7 +15,7 @@ Also the [CPM.cmake](https://github.com/TheLartians/CPM.cmake) script should be
See [here](https://github.com/TheLartians/ModernCppStarter/blob/master/CMakeLists.txt) for an example usage.

```cmake
CPMAddPackage("gh:TheLartians/PackageProject.cmake@1.6.0")
CPMAddPackage("gh:TheLartians/PackageProject.cmake@1.14.0")

packageProject(
# the name of the target to export
Expand Down Expand Up @@ -53,6 +53,9 @@ packageProject(
# by default libs will be installed to <...>/lib/<packagename-version>/
# / - means relative to <...>/lib, i.e. install libs to <...>/lib/, bins to <...>/bin/, etc
RUNTIME_DESTINATION /
# (optional) FILE_SET "public_headers;..."
# Install the target related header file sets too
HEADER_SETS HEADERS
)
```

Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14...3.22)
cmake_minimum_required(VERSION 3.24...3.31)

option(TEST_INSTALLED_VERSION "Test the version found by find_package" OFF)

Expand Down
2 changes: 1 addition & 1 deletion test/dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14...3.22)
cmake_minimum_required(VERSION 3.24...3.31)

project(
dependency
Expand Down
43 changes: 32 additions & 11 deletions test/header_only/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
cmake_minimum_required(VERSION 3.14...3.22)
cmake_minimum_required(VERSION 3.24...3.31)

project(
header_only
VERSION 1.0
VERSION 1.0.0.0
LANGUAGES CXX
DESCRIPTION "A header only dependency for testing PackageProject.cmake"
)

add_library(${PROJECT_NAME} INTERFACE)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. PackageProject)

target_include_directories(
dependency PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)
# supported in CMake 3.23 and newer
set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ON)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. PackageProject)
add_library(header_only INTERFACE)

string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION)
string(TOUPPER ${PROJECT_NAME} UPPERCASE_PROJECT_NAME)
set(${UPPERCASE_PROJECT_NAME}_VERSION \"${PROJECT_VERSION}\")
configure_file(${PACKAGE_PROJECT_ROOT_PATH}/version.h.in ${VERSION_HEADER_LOCATION} @ONLY)

if(CMAKE_VERIFY_INTERFACE_HEADER_SETS)
target_sources(
header_only
PUBLIC FILE_SET
HEADERS
BASE_DIRS
include
${PROJECT_BINARY_DIR}
FILES
include/header_only/adder.h
${PROJECT_BINARY_DIR}/${VERSION_HEADER_LOCATION}
)
else()
target_include_directories(
header_only INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
endif()

packageProject(
NAME ${PROJECT_NAME}
VERSION ${PROJECT_VERSION}
BINARY_DIR ${PROJECT_BINARY_DIR}
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
VERSION_HEADER "${PROJECT_NAME}/version.h"
INCLUDE_DESTINATION include
DEPENDENCIES ""
CPACK "${TEST_CPACK}"
HEADER_SETS HEADERS
)
2 changes: 1 addition & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main() {
result &= TRANSITIVE_DEPENDENCY_VERSION_PATCH == 9;
result &= TRANSITIVE_DEPENDENCY_VERSION_TWEAK == 21948124;
result &= (5 == add(2, 3));
result &= HEADER_ONLY_VERSION == std::string("1.0");
result &= HEADER_ONLY_VERSION == std::string("1.0.0.0");
result &= HEADER_ONLY_VERSION_MAJOR == 1;
result &= HEADER_ONLY_VERSION_MINOR == 0;
result &= HEADER_ONLY_VERSION_PATCH == 0;
Expand Down
33 changes: 24 additions & 9 deletions test/namespaced_dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14...3.22)
cmake_minimum_required(VERSION 3.24...3.31)

project(
namespaced_dependency
Expand All @@ -7,27 +7,42 @@ project(
DESCRIPTION "A namespaced dependency for testing PackageProject.cmake"
)

# supported in CMake 3.23 and newer
set(CMAKE_VERIFY_INTERFACE_HEADER_SETS NO)

set(PROJECT_NAMESPACE "ns")
add_library(${PROJECT_NAME} STATIC source/namespaced_dependency.cpp)
# the alias ${PROJECT_NAMESPACE}::${PROJECT_NAME} is automatically provided by PackageProject.cmake
# if we use the `NAMESPACE` parameter
add_library(namespaced_dependency STATIC source/namespaced_dependency.cpp)

target_include_directories(
namespaced_dependency PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)
if(CMAKE_VERIFY_INTERFACE_HEADER_SETS)
target_sources(
namespaced_dependency
PUBLIC FILE_SET HEADERS BASE_DIRS include FILES
include/namespaced_dependency/namespaced_dependency.h
# TODO: add missing generated headers too!
)
else()
target_include_directories(
namespaced_dependency PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
endif()

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. PackageProject)

# the alias ${PROJECT_NAMESPACE}::${PROJECT_NAME} is automatically provided by PackageProject.cmake
# if we use the `NAMESPACE` parameter

packageProject(
NAME ${PROJECT_NAME}
VERSION ${PROJECT_VERSION}
NAMESPACE ${PROJECT_NAMESPACE}
BINARY_DIR ${PROJECT_BINARY_DIR}
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
INCLUDE_DESTINATION include
VERSION_HEADER "namespaced_dependency/version.h"
INCLUDE_HEADER_PATTERN "*.h"
DEPENDENCIES ""
CPACK "${TEST_CPACK}"
RUNTIME_DESTINATION /
HEADER_SETS HEADERS
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <namespaced_dependency/namespaced_dependency.h>
#include <namespaced_dependency/version.h>

#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion test/runtime_destination_dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14...3.22)
cmake_minimum_required(VERSION 3.24...3.31)

project(
runtime_destination_dependency
Expand Down
2 changes: 1 addition & 1 deletion test/style/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14)
cmake_minimum_required(VERSION 3.24...3.31)

project(StyleCheck)

Expand Down
36 changes: 25 additions & 11 deletions test/transitive_dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14...3.22)
cmake_minimum_required(VERSION 3.24...3.31)

project(
transitive_dependency
Expand All @@ -11,30 +11,42 @@ include(cmake/CPM.cmake)

CPMAddPackage(
NAME fmt
GIT_TAG 7.1.3
GIT_TAG 11.1.4
GITHUB_REPOSITORY fmtlib/fmt
OPTIONS "FMT_INSTALL YES"
)

CPMAddPackage(
NAME cxxopts
GITHUB_REPOSITORY jarro2783/cxxopts
VERSION 2.2.0
OPTIONS "CXXOPTS_BUILD_EXAMPLES Off" "CXXOPTS_BUILD_TESTS Off"
VERSION 3.2.1
OPTIONS "CXXOPTS_ENABLE_INSTALL YES"
)

# Set default visibility to hidden for all targets
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)

# supported in CMake 3.23 and newer
set(CMAKE_VERIFY_INTERFACE_HEADER_SETS NO)

add_library(${PROJECT_NAME} SHARED source/transitive_dependency.cpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
target_link_libraries(${PROJECT_NAME} PUBLIC fmt::fmt-header-only cxxopts)
target_link_libraries(${PROJECT_NAME} PUBLIC fmt::fmt-header-only cxxopts::cxxopts)

target_include_directories(
transitive_dependency PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)
if(CMAKE_VERIFY_INTERFACE_HEADER_SETS)
target_sources(
transitive_dependency
PUBLIC FILE_SET HEADERS BASE_DIRS include FILES
include/transitive_dependency/transitive_dependency.h
# TODO: add missing generated headers too!
)
else()
target_include_directories(
transitive_dependency PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
endif()

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. PackageProject)

Expand All @@ -44,9 +56,11 @@ packageProject(
NAMESPACE ${PROJECT_NAME}
BINARY_DIR ${PROJECT_BINARY_DIR}
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
INCLUDE_DESTINATION include
VERSION_HEADER "transitive_dependency/version.h"
EXPORT_HEADER "transitive_dependency/export.h"
DEPENDENCIES "fmt 7.1.3;cxxopts 2.2.0"
DEPENDENCIES "fmt 11.1.4;cxxopts 3.2.1"
CPACK "${TEST_CPACK}"
RUNTIME_DESTINATION /
HEADER_SETS HEADERS
)
27 changes: 15 additions & 12 deletions test/transitive_dependency/cmake/CPM.cmake
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
set(CPM_DOWNLOAD_VERSION 0.35.0)
# SPDX - License - Identifier : MIT
#
# SPDX - FileCopyrightText : Copyright(c) 2019 - 2023 Lars Melchior and contributors

set(CPM_DOWNLOAD_VERSION 0.40.6)
set(CPM_HASH_SUM "3440292907e35353bf0761049d3680e9ed0031443c8ebeb9cbb6a421c4550ec1")

if(CPM_SOURCE_CACHE)
# Expand relative path. This is important if the provided path contains a tilde (~)
get_filename_component(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE} ABSOLUTE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED $ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/" "CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
file(DOWNLOAD
https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endif()
# Expand relative path.This is important if the provided path contains a tilde(~)
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)

file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
)

include(${CPM_DOWNLOAD_LOCATION})