Skip to content

Commit

Permalink
Add RUNTIME_DESTINATION variable (#45)
Browse files Browse the repository at this point in the history
* Add RUNTIME_DESTINATION

It's possible to put libraries and binaries directly to bin/ and lib/ (using RUNTIME_DESTINATION /) instead of bin/<name-version> and lib/<name-version>

* Add tests for RUNTIME_DESTINATION feature

* Update README.md

* Update .cmake-format

* Update .github/workflows/test.yml

Refactor: move test to it's own step

Co-authored-by: Lars Melchior <[email protected]>

* Fix: syntax error in test.yml

---------

Co-authored-by: Lars Melchior <[email protected]>
  • Loading branch information
dilshodm and TheLartians authored Dec 22, 2024
1 parent 5f51898 commit 738d2ed
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 8 deletions.
1 change: 1 addition & 0 deletions .cmake-format
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ parse:
EXPORT_HEADER: 1
DISABLE_VERSION_SUFFIX: 1
CPACK: 1
RUNTIME_DESTINATION: 1
DEPENDENCIES: +
7 changes: 5 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
uses: jwlawson/[email protected]
with:
cmake-version: 3.18.4

- name: test and install local build
run: |
cmake -S test -B build/local
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
uses: jwlawson/[email protected]
with:
cmake-version: 3.18.4

- name: build for packaging
run: |
cmake -S test -B build/local -DTEST_CPACK=YES
Expand All @@ -68,6 +68,9 @@ jobs:
cpack -G DEB .
dpkg -I *deb | grep "Maintainer: Foo Bar <[email protected]>"
sudo -E dpkg -i *.deb
- name: check if we can explicitly set the runtime destination
run: test -e /usr/lib/libruntime_destination_dependency.a

- name: test installed build
run: |
Expand Down
15 changes: 10 additions & 5 deletions 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;CPACK;"
"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"
${ARGN}
)
Expand Down Expand Up @@ -128,17 +128,22 @@ function(packageProject)
COMPATIBILITY ${PROJECT_COMPATIBILITY} ${wbpvf_extra_args}
)

# set default runtime install subdirectory (RUNTIME_DESTINATION)
if(NOT DEFINED PROJECT_RUNTIME_DESTINATION)
set(PROJECT_RUNTIME_DESTINATION ${PROJECT_NAME}${PROJECT_VERSION_SUFFIX})
endif()

install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_RUNTIME_DESTINATION}
COMPONENT "${PROJECT_NAME}_Runtime"
NAMELINK_COMPONENT "${PROJECT_NAME}_Development"
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_RUNTIME_DESTINATION}
COMPONENT "${PROJECT_NAME}_Development"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}/${PROJECT_RUNTIME_DESTINATION}
COMPONENT "${PROJECT_NAME}_Runtime"
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}/${PROJECT_RUNTIME_DESTINATION}
COMPONENT "${PROJECT_NAME}_Runtime"
PUBLIC_HEADER DESTINATION ${PROJECT_INCLUDE_DESTINATION} COMPONENT "${PROJECT_NAME}_Development"
INCLUDES
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ packageProject(
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
# (optional) option to install only header files with matching pattern
INCLUDE_HEADER_PATTERN "*.h"
# relative install directory for runtimes: bins, libs, archives
# if omitted, 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 /
# semicolon separated list of the project's dependencies
DEPENDENCIES "fmt 7.1.3;cxxopts 2.2.0"
# (optional) create a header containing the version info
Expand Down
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ if(TEST_INSTALLED_VERSION)
find_package(header_only 1.0 REQUIRED)
find_package(namespaced_dependency 4.5.6 REQUIRED)
find_package(transitive_dependency 7.8.9 REQUIRED)
find_package(runtime_destination_dependency 1.5 REQUIRED)
else()
if(TEST_CPACK)
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Foo Bar <[email protected]>")
Expand All @@ -21,13 +22,14 @@ else()
add_subdirectory(header_only)
add_subdirectory(namespaced_dependency)
add_subdirectory(transitive_dependency)
add_subdirectory(runtime_destination_dependency)
endif()

add_executable(main main.cpp)

target_link_libraries(
main dependency header_only ns::namespaced_dependency
transitive_dependency::transitive_dependency
transitive_dependency::transitive_dependency runtime_destination_dependency
)

enable_testing()
Expand Down
8 changes: 8 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <header_only/version.h>
#include <namespaced_dependency/namespaced_dependency.h>
#include <namespaced_dependency/version.h>
#include <runtime_destination_dependency/dependency.h>
#include <runtime_destination_dependency/version.h>
#include <transitive_dependency/transitive_dependency.h>
#include <transitive_dependency/version.h>

Expand All @@ -13,6 +15,7 @@ int main() {
dependencyFunction();
ns::namespacedDependencyFunction();
transitiveDependencyFunction();
runtimeDestinationDependency();
auto result = true;
result &= DEPENDENCY_VERSION == std::string("1.2");
result &= DEPENDENCY_VERSION_MAJOR == 1;
Expand All @@ -35,5 +38,10 @@ int main() {
result &= HEADER_ONLY_VERSION_MINOR == 0;
result &= HEADER_ONLY_VERSION_PATCH == 0;
result &= HEADER_ONLY_VERSION_TWEAK == 0;
result &= RUNTIME_DESTINATION_DEPENDENCY_VERSION == std::string("1.5");
result &= RUNTIME_DESTINATION_DEPENDENCY_VERSION_MAJOR == 1;
result &= RUNTIME_DESTINATION_DEPENDENCY_VERSION_MINOR == 5;
result &= RUNTIME_DESTINATION_DEPENDENCY_VERSION_PATCH == 0;
result &= RUNTIME_DESTINATION_DEPENDENCY_VERSION_TWEAK == 0;
return result ? 0 : 1;
}
30 changes: 30 additions & 0 deletions test/runtime_destination_dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.14...3.22)

project(
runtime_destination_dependency
VERSION 1.5
LANGUAGES CXX
DESCRIPTION "A dependency for testing RUNTIME_DESTINATION in PackageProject.cmake"
)

add_library(runtime_destination_dependency STATIC source/dependency.cpp)

target_include_directories(
runtime_destination_dependency
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)

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

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 "runtime_destination_dependency/version.h"
RUNTIME_DESTINATION / # this is relative path to <...>/lib
DEPENDENCIES ""
CPACK YES
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void runtimeDestinationDependency();
15 changes: 15 additions & 0 deletions test/runtime_destination_dependency/source/dependency.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <runtime_destination_dependency/version.h>

#include <iostream>

void runtimeDestinationDependency() {
std::cout << "Using dependency version " << RUNTIME_DESTINATION_DEPENDENCY_VERSION << std::endl;
std::cout << "Dependency version major: " << RUNTIME_DESTINATION_DEPENDENCY_VERSION_MAJOR
<< std::endl;
std::cout << "Dependency version minor: " << RUNTIME_DESTINATION_DEPENDENCY_VERSION_MINOR
<< std::endl;
std::cout << "Dependency version patch: " << RUNTIME_DESTINATION_DEPENDENCY_VERSION_PATCH
<< std::endl;
std::cout << "Dependency version build: " << RUNTIME_DESTINATION_DEPENDENCY_VERSION_TWEAK
<< std::endl;
}

0 comments on commit 738d2ed

Please sign in to comment.