Skip to content

Commit

Permalink
cmake: Add install target
Browse files Browse the repository at this point in the history
We can now install the headers, which allows KDAlgorithms to be
packaged by Brew and other package managers.

Added a test, called by CI, which tests building a standalone
example against a system-wide KDAlgorithms.
  • Loading branch information
iamsergio committed Nov 21, 2024
1 parent 5d1732e commit 7b0c690
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ jobs:
-DCMAKE_BUILD_TYPE=Release
-DBUILD_TESTING=ON
-DKDALGORITHMS_BUILD_TEST=ON
-DCMAKE_INSTALL_PREFIX=installed
- name: Build Project (Release)
run: cmake --build ./build-release
Expand All @@ -85,6 +86,15 @@ jobs:
id: tests2
run: ctest --test-dir ./build-release -C Release --output-on-failure

- name: Install Project
run: cmake --install ./build-release

- name: Build standalone example against systemwide KDAlgorithms
run: |
cd tests/test_install
cmake -DCMAKE_PREFIX_PATH="../../installed" .
cmake --build .
- name: Read tests log when it fails
uses: andstor/file-reader-action@v1
if: ${{ steps.tests1.outcome == 'failure' || steps.tests2.outcome == 'failure' }}
Expand Down
53 changes: 52 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ endif()

add_library(kdalgorithms INTERFACE)
add_library(KDAB::KDAlgorithms ALIAS kdalgorithms)
target_include_directories(kdalgorithms INTERFACE src/)
target_include_directories(kdalgorithms INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>
)

if(MSVC)
target_compile_options(kdalgorithms INTERFACE /Zc:__cplusplus)
Expand Down Expand Up @@ -95,3 +98,51 @@ if(BUILD_TESTING AND ${KDALGORITHMS_BUILD_TEST})

add_subdirectory(Inspiration)
endif()

install(TARGETS kdalgorithms EXPORT KDAlgorithmsTargets)

install(EXPORT KDAlgorithmsTargets
FILE KDAlgorithmsTargets.cmake
NAMESPACE KDAB::
DESTINATION lib/cmake/KDAlgorithms
)

install(FILES
src/kdalgorithms.h
DESTINATION include/kdalgorithms
)

install(FILES
src/kdalgorithms_bits/read_iterator_wrapper.h
src/kdalgorithms_bits/find_if.h
src/kdalgorithms_bits/filter.h
src/kdalgorithms_bits/generate.h
src/kdalgorithms_bits/insert_wrapper.h
src/kdalgorithms_bits/is_const_method.h
src/kdalgorithms_bits/is_detected.h
src/kdalgorithms_bits/method_tests.h
src/kdalgorithms_bits/operators.h
src/kdalgorithms_bits/reserve_helper.h
src/kdalgorithms_bits/return_type_trait.h
src/kdalgorithms_bits/shared.h
src/kdalgorithms_bits/to_function_object.h
src/kdalgorithms_bits/transform.h
src/kdalgorithms_bits/zip.h
src/kdalgorithms_bits/tuple_utils.h
src/kdalgorithms_bits/invoke.h
src/kdalgorithms_bits/cartesian_product.h
DESTINATION include/kdalgorithms/kdalgorithms_bits
)

include(CMakePackageConfigHelpers)

configure_package_config_file(
KDAlgorithmsConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/KDAlgorithmsConfig.cmake
INSTALL_DESTINATION lib/cmake/KDAlgorithms
)

install(FILES
${CMAKE_CURRENT_BINARY_DIR}/KDAlgorithmsConfig.cmake
DESTINATION lib/cmake/KDAlgorithms
)
4 changes: 4 additions & 0 deletions KDAlgorithmsConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/KDAlgorithmsTargets.cmake")
2 changes: 1 addition & 1 deletion REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SPDX-PackageSupplier = "<info@kdab.com>"
SPDX-PackageDownloadLocation = "https://www.github.com/KDAB/KDAlgorithms"

[[annotations]]
path = ["run", ".gitignore", "CMakeLists.txt", "CMakePresets.json", "README.md", "_clang-format", ".pre-commit-config.yaml", "appveyor.yml", "Documentation/**", "Example/CMakeLists.txt", "Inspiration/CMakeLists.txt", "REUSE.toml"]
path = ["run", ".gitignore", "CMakeLists.txt", "KDAlgorithmsConfig.cmake.in", "CMakePresets.json", "README.md", "_clang-format", ".pre-commit-config.yaml", "appveyor.yml", "Documentation/**", "Example/CMakeLists.txt", "Inspiration/CMakeLists.txt", "tests/test_install/CMakeLists.txt", "REUSE.toml"]
precedence = "aggregate"
SPDX-FileCopyrightText = "2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>"
SPDX-License-Identifier = "MIT"
15 changes: 15 additions & 0 deletions tests/test_install/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Tests that users can use find_package and "link" against KDAlgorithms target
# This test is built by the GitHub CI workflow

cmake_minimum_required(VERSION 3.10)

project(test_find_package LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(KDAlgorithms REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} PRIVATE KDAB::kdalgorithms)
28 changes: 28 additions & 0 deletions tests/test_install/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/****************************************************************************
**
** This file is part of KDAlgorithms
**
** SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
**
** SPDX-License-Identifier: MIT
**
****************************************************************************/

#include <kdalgorithms/kdalgorithms.h>

#include <iostream>
#include <string>
#include <vector>

int main()
{
auto vec = kdalgorithms::iota(1, 100);
auto odds = kdalgorithms::filtered(vec, [](int i) { return i % 2 == 1; });
auto result = kdalgorithms::accumulate(odds, [](const std::string &partial, int value) {
return partial + "," + std::to_string(value);
});

std::cout << result << "\n";

return 0;
}

0 comments on commit 7b0c690

Please sign in to comment.