Skip to content

Commit

Permalink
Migrate install rules to top-level, use cccl_generate_install_rules
Browse files Browse the repository at this point in the history
… helper to reduce redundancy.
  • Loading branch information
alliepiper committed Sep 16, 2024
1 parent 5edeaeb commit 93833e9
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 184 deletions.
6 changes: 1 addition & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ endif()
# Enable CXX so CMake can configure install paths
project(CCCL LANGUAGES CXX)

# Optionally include installation rules for non-top-level builds:
option(CCCL_ENABLE_INSTALL_RULES "Enable installation of CCCL." ${CCCL_TOPLEVEL_PROJECT})
if (CCCL_ENABLE_INSTALL_RULES)
include(cmake/CCCLInstallRules.cmake)
endif()
include(cmake/CCCLInstallRules.cmake)

# Support adding CCCL to a parent project via add_subdirectory.
include(cmake/CCCLAddSubdirHelper.cmake) # Always include, this is used by subprojects as well.
Expand Down
121 changes: 116 additions & 5 deletions cmake/CCCLInstallRules.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,121 @@
# Bring in CMAKE_INSTALL_LIBDIR
# Bring in CMAKE_INSTALL_* vars
include(GNUInstallDirs)

# CCCL has no installable binaries, no need to build before installing:
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY TRUE)

# CMake package
install(DIRECTORY "${CCCL_SOURCE_DIR}/lib/cmake/cccl"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/"
)
# Usage:
# cccl_generate_install_rules(PROJECT_NAME DEFAULT_ENABLE
# [NO_HEADERS]
# [HEADER_SUBDIR <subdir1> [subdir2 ...]]
# [HEADERS_INCLUDE <pattern1> [pattern2 ...]]
# [HEADERS_EXCLUDE <pattern1> [pattern2 ...]]
# [PACKAGE]
# )
#
# Options:
# PROJECT_NAME: The case-sensitive name of the project. Used to generate the option flag.
# DEFAULT_ENABLE: Whether the install rules should be enabled by default.
# NO_HEADERS: If set, no install rules will be generated for headers.
# HEADERS_SUBDIRS: If set, a separate install rule will be generated for each subdirectory relative to the project dir.
# If not set, <CCCL_SOURCE_DIR>/<PROJECT_NAME_LOWER>/<PROJECT_NAME_LOWER> will be used.
# HEADERS_INCLUDE: A list of globbing patterns that match installable header files.
# HEADERS_EXCLUDE: A list of globbing patterns that match header files to exclude from installation.
# PACKAGE: If set, install the project's CMake package.
#
# Notes:
# - The generated cache option will be named <PROJECT_NAME>_ENABLE_INSTALL_RULES.
# - The header globs are applied relative to <CCCL_SOURCE_DIR>/<PROJECT_NAME_LOWER>/<SUBDIR>.
# - The cmake package is assumed to be located at <CCCL_SOURCE_DIR>/lib/cmake/<PROJECT_NAME_LOWER>.
# - If a <PROJECT_NAME_LOWER>-header-search.cmake.in file exists in the CMake package directory,
# it will be configured and installed.
#
function(cccl_generate_install_rules project_name enable_rules_by_default)
set(options PACKAGE NO_HEADERS)
set(oneValueArgs)
set(multiValueArgs HEADERS_SUBDIRS HEADERS_INCLUDE HEADERS_EXCLUDE)
cmake_parse_arguments(CGIR "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

string(TOLOWER ${project_name} project_name_lower)
set(project_source_dir "${CCCL_SOURCE_DIR}/${project_name_lower}")
set(header_dest_dir "${CMAKE_INSTALL_INCLUDEDIR}")
set(package_source_dir "${CCCL_SOURCE_DIR}/lib/cmake/${project_name_lower}")
set(package_dest_dir "${CMAKE_INSTALL_LIBDIR}/cmake/")
set(header_search_template "${package_source_dir}/${project_name_lower}-header-search.cmake.in")
set(header_search_temporary "${CCCL_BINARY_DIR}/${project_name_lower}-header-search.cmake")

if (NOT DEFINED CGIR_HEADERS_SUBDIRS)
set(CGIR_HEADERS_SUBDIRS "${project_name_lower}")
endif()

set(flag_name ${project_name}_ENABLE_INSTALL_RULES)
option(${flag_name} "Enable installation of ${project_name} files." ${enable_rules_by_default})
if (${flag_name})
# Headers:
if (NOT CGIR_NO_HEADERS)
foreach(subdir IN LISTS CGIR_HEADERS_SUBDIRS)
set(header_globs)
if(DEFINED CGIR_HEADERS_INCLUDE OR DEFINED CGIR_HEADERS_EXCLUDE)
set(header_globs "FILES_MATCHING")

foreach (header_glob IN LISTS CGIR_HEADERS_INCLUDE)
list(APPEND header_globs "PATTERN" "${header_glob}")
endforeach()

foreach (header_glob IN LISTS CGIR_HEADERS_EXCLUDE)
list(APPEND header_globs "PATTERN" "${header_glob}" "EXCLUDE")
endforeach()
endif()

install(
DIRECTORY "${project_source_dir}/${subdir}"
DESTINATION "${header_dest_dir}"
${header_globs}
)
endforeach()
endif()

# CMake package:
install(
DIRECTORY "${package_source_dir}"
DESTINATION "${package_dest_dir}"
REGEX .*header-search.cmake.* EXCLUDE
)

# Header search infra:
if (EXISTS "${header_search_template}")
# Need to configure a file to store the infix specified in
# CMAKE_INSTALL_INCLUDEDIR since it can be defined by the user
set(_CCCL_RELATIVE_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
if(_CCCL_RELATIVE_LIBDIR MATCHES "^${CMAKE_INSTALL_PREFIX}")
# libdir is an abs string that starts with prefix
string(LENGTH "${CMAKE_INSTALL_PREFIX}" to_remove)
string(SUBSTRING "${_CCCL_RELATIVE_LIBDIR}" ${to_remove} -1 relative)
# remove any leading "/""
string(REGEX REPLACE "^/(.)" "\\1" _CCCL_RELATIVE_LIBDIR "${relative}")
elseif(_CCCL_RELATIVE_LIBDIR MATCHES "^/")
message(FATAL_ERROR "CMAKE_INSTALL_LIBDIR ('${CMAKE_INSTALL_LIBDIR}') must be a relative path or an absolute path under CMAKE_INSTALL_PREFIX ('${CMAKE_INSTALL_PREFIX}')")
endif()
set(install_location "${_CCCL_RELATIVE_LIBDIR}/cmake/${project_name_lower}")

# Transform to a list of directories, replace each directory with "../"
# and convert back to a string
string(REGEX REPLACE "/" ";" from_install_prefix "${install_location}")
list(TRANSFORM from_install_prefix REPLACE ".+" "../")
list(JOIN from_install_prefix "" from_install_prefix)

configure_file("${header_search_template}" "${header_search_temporary}" @ONLY)
install(
FILES "${header_search_temporary}"
DESTINATION "${install_location}"
)
endif()

endif()
endfunction()

include("${CMAKE_CURRENT_LIST_DIR}/install/cccl.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/install/cub.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/install/cudax.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/install/libcudacxx.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/install/thrust.cmake")
4 changes: 4 additions & 0 deletions cmake/install/cccl.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cccl_generate_install_rules(CCCL ${CCCL_TOPLEVEL_PROJECT}
NO_HEADERS
PACKAGE
)
4 changes: 4 additions & 0 deletions cmake/install/cub.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cccl_generate_install_rules(CUB ${CCCL_TOPLEVEL_PROJECT}
HEADERS_INCLUDE "*.cuh" "*.hpp"
PACKAGE
)
5 changes: 5 additions & 0 deletions cmake/install/cudax.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cccl_generate_install_rules(cudax ${CCCL_TOPLEVEL_PROJECT}
HEADERS_SUBDIRS "include/cuda"
HEADERS_INCLUDE "*.cuh"
PACKAGE
)
6 changes: 6 additions & 0 deletions cmake/install/libcudacxx.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cccl_generate_install_rules(libcudacxx ${CCCL_TOPLEVEL_PROJECT}
HEADERS_SUBDIRS "include/cuda" "include/nv"
HEADERS_INCLUDE "*"
HEADERS_EXCLUDE "CMakeLists.txt"
PACKAGE
)
4 changes: 4 additions & 0 deletions cmake/install/thrust.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cccl_generate_install_rules(Thrust ${CCCL_TOPLEVEL_PROJECT}
HEADERS_INCLUDE "*.h" "*.inl"
PACKAGE
)
5 changes: 0 additions & 5 deletions cub/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ endif()
# GNUInstallDirs CMake module.
enable_language(CXX)

option(CUB_ENABLE_INSTALL_RULES "Enable installation of CUB" ${CUB_TOPLEVEL_PROJECT})
if (CUB_ENABLE_INSTALL_RULES)
include(cmake/CubInstallRules.cmake)
endif()

# Support adding CUB to a parent project via add_subdirectory.
# See examples/cmake/add_subdir/CMakeLists.txt for details.
if (NOT CUB_TOPLEVEL_PROJECT)
Expand Down
42 changes: 0 additions & 42 deletions cub/cmake/CubInstallRules.cmake

This file was deleted.

5 changes: 0 additions & 5 deletions cudax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ endif()

project(cudax LANGUAGES CUDA CXX)

option(cudax_ENABLE_INSTALL_RULES "Enable installation of CUDA Experimental." ${cudax_TOPLEVEL_PROJECT})
if (cudax_ENABLE_INSTALL_RULES)
include(cmake/cudaxInstallRules.cmake)
endif()

if (NOT cudax_TOPLEVEL_PROJECT)
include(cmake/cudaxAddSubdir.cmake)
return()
Expand Down
13 changes: 0 additions & 13 deletions cudax/cmake/cudaxInstallRules.cmake

This file was deleted.

1 change: 1 addition & 0 deletions lib/cmake/cub/cub-header-search.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ unset(_CUB_VERSION_INCLUDE_DIR CACHE) # Clear old result to force search
set(from_install_prefix "@from_install_prefix@")

find_path(_CUB_VERSION_INCLUDE_DIR cub/version.cuh
REQUIRED
NO_CMAKE_FIND_ROOT_PATH # Don't allow CMake to re-root the search
NO_DEFAULT_PATH # Only search explicit paths below:
PATHS
Expand Down
1 change: 1 addition & 0 deletions lib/cmake/cudax/cudax-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ endif()
set(cn_cmake_dir "${CMAKE_CURRENT_LIST_DIR}")
set(cn_prefix_dir "${cn_cmake_dir}/../../..")
find_path(cn_include_dir "cuda/experimental/version.cuh"
REQUIRED
NO_DEFAULT_PATH NO_CACHE REQUIRED
HINTS
"${cn_prefix_dir}/cudax/include" # Source
Expand Down
1 change: 1 addition & 0 deletions lib/cmake/libcudacxx/libcudacxx-header-search.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ unset(_libcudacxx_VERSION_INCLUDE_DIR CACHE) # Clear old result to force search
set(from_install_prefix "@from_install_prefix@")

find_path(_libcudacxx_VERSION_INCLUDE_DIR cuda/std/detail/__config
REQUIRED
NO_CMAKE_FIND_ROOT_PATH # Don't allow CMake to re-root the search
NO_DEFAULT_PATH # Only search explicit paths below:
PATHS
Expand Down
1 change: 1 addition & 0 deletions lib/cmake/thrust/thrust-header-search.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ unset(_THRUST_VERSION_INCLUDE_DIR CACHE) # Clear old result to force search
set(from_install_prefix "@from_install_prefix@")

find_path(_THRUST_VERSION_INCLUDE_DIR thrust/version.h
REQUIRED
NO_CMAKE_FIND_ROOT_PATH # Don't allow CMake to re-root the search
NO_DEFAULT_PATH # Only search explicit paths below:
PATHS
Expand Down
5 changes: 0 additions & 5 deletions libcudacxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ set(PACKAGE_VERSION 11.0)
set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
project(libcudacxx LANGUAGES CXX)

option(libcudacxx_ENABLE_INSTALL_RULES "Enable installation of libcu++" ${LIBCUDACXX_TOPLEVEL_PROJECT})
if (libcudacxx_ENABLE_INSTALL_RULES)
include(cmake/libcudacxxInstallRules.cmake)
endif()

if (NOT LIBCUDACXX_TOPLEVEL_PROJECT)
include(cmake/libcudacxxAddSubdir.cmake)
return()
Expand Down
55 changes: 0 additions & 55 deletions libcudacxx/cmake/libcudacxxInstallRules.cmake

This file was deleted.

6 changes: 0 additions & 6 deletions thrust/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ endif()
# GNUInstallDirs CMake module.
enable_language(CXX)

# Optionally include installation rules for non-top-level builds:
option(THRUST_ENABLE_INSTALL_RULES "Enable installation of Thrust" ${THRUST_TOPLEVEL_PROJECT})
if (THRUST_ENABLE_INSTALL_RULES)
include(cmake/ThrustInstallRules.cmake)
endif()

# Support adding Thrust to a parent project via add_subdirectory.
# See examples/cmake/add_subdir/CMakeLists.txt for details.
if (NOT THRUST_TOPLEVEL_PROJECT)
Expand Down
Loading

0 comments on commit 93833e9

Please sign in to comment.