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
54 changes: 49 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,37 @@ set(PACKAGE_PROJECT_ROOT_PATH
CACHE INTERNAL "The path to the PackageProject directory"
)

# Add custom TO_INSTALL property to the project targets
define_property(
TARGET
PROPERTY TO_INSTALL
BRIEF_DOCS "Marks traget for installation"
FULL_DOCS "Marks traget for installation"
)

# Function to get all project targets recursively
function (get_all_cmake_targets out_var current_dir)
get_property(targets DIRECTORY ${current_dir} PROPERTY BUILDSYSTEM_TARGETS)
get_property(subdirs DIRECTORY ${current_dir} PROPERTY SUBDIRECTORIES)

foreach(subdir ${subdirs})
get_all_cmake_targets(subdir_targets ${subdir})
list(APPEND targets ${subdir_targets})
endforeach()

set(${out_var} ${targets} PARENT_SCOPE)
endfunction()

# Main function
function(packageProject)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

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;"
"DEPENDENCIES"
"NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;EXPORT_HEADER;VERSION_HEADER;NAMESPACE;DISABLE_VERSION_SUFFIX;ARCH_INDEPENDENT;INCLUDE_HEADER_PATTERN;CPACK;AUTO_FIND_TARGETS_TO_INSTALL;"
"DEPENDENCIES;TARGETS_TO_INSTALL;"
${ARGN}
)

Expand Down Expand Up @@ -121,8 +143,30 @@ function(packageProject)
COMPATIBILITY ${PROJECT_COMPATIBILITY} ${wbpvf_extra_args}
)

# Populating the list of targets to install
if(PROJECT_AUTO_FIND_TARGETS_TO_INSTALL)
message(STATUS "Searching for targets to install...")
set(PROJECT_TARGETS_TO_INSTALL "")
get_all_cmake_targets(all_targets ${PROJECT_SOURCE_DIR})
foreach(target_name ${all_targets})
get_target_property(inst ${target_name} TO_INSTALL)
if(${inst})
list(APPEND PROJECT_TARGETS_TO_INSTALL ${target_name})
endif()
endforeach()
else()
if(NOT PROJECT_TARGETS_TO_INSTALL)
set(PROJECT_TARGETS_TO_INSTALL ${PROJECT_NAME})
endif()
endif()

message(STATUS "Targets to install:")
foreach(target_name ${PROJECT_TARGETS_TO_INSTALL})
message(STATUS \t${target_name})
endforeach()

install(
TARGETS ${PROJECT_NAME}
TARGETS ${PROJECT_TARGETS_TO_INSTALL}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
COMPONENT "${PROJECT_NAME}_Runtime"
Expand All @@ -139,8 +183,8 @@ function(packageProject)
)

set("${PROJECT_NAME}_INSTALL_CMAKEDIR"
"${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}"
CACHE PATH "CMake package config location relative to the install prefix"
"${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}"
CACHE PATH "CMake package config location relative to the install prefix"
)

mark_as_advanced("${PROJECT_NAME}_INSTALL_CMAKEDIR")
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ packageProject(
NAME ${PROJECT_NAME}
# the version of the target to export
VERSION ${PROJECT_VERSION}
# The next to options are mutually exclusive:
# Explicit list of targets to install. If empty single target ${PROJECT_NAME} is installed.
TARGETS_TO_INSTALL ${PROJECT_TARGETS_TO_INSTALL}
# If YES all the targets in the project with TO_INSTALL property set to true will be installed.
AUTO_FIND_TARGETS_TO_INSTALL NO
# a temporary directory to create the config files
BINARY_DIR ${PROJECT_BINARY_DIR}
# location of the target's public headers
Expand Down