Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit fc0846e

Browse files
committed
WIP: Prep for libcudacxx dependency.
1 parent 5159454 commit fc0846e

File tree

3 files changed

+103
-35
lines changed

3 files changed

+103
-35
lines changed

thrust/cmake/thrust-config.cmake

+101-33
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@
3737
# [ADVANCED] # Optionally mark options as advanced
3838
# )
3939
#
40-
# # Use a custom TBB, CUB, and/or OMP
40+
# # Use a custom TBB, CUB, libcudacxx, and/or OMP
4141
# # (Note that once set, these cannot be changed. This includes COMPONENT
4242
# # preloading and lazy lookups in thrust_create_target)
4343
# find_package(Thrust REQUIRED)
4444
# thrust_set_CUB_target(MyCUBTarget) # MyXXXTarget contains an existing
4545
# thrust_set_TBB_target(MyTBBTarget) # interface to XXX for Thrust to use.
46+
# thrust_set_libcudacxx_target(MyLibcudacxxTarget)
4647
# thrust_set_OMP_target(MyOMPTarget)
47-
# thrust_create_target(ThrustWithMyCUB DEVICE CUDA)
48+
# thrust_create_target(ThrustWithMyCUBAndLibcudacxx DEVICE CUDA)
4849
# thrust_create_target(ThrustWithMyTBB DEVICE TBB)
4950
# thrust_create_target(ThrustWithMyOMP DEVICE OMP)
5051
#
@@ -85,19 +86,21 @@ cmake_minimum_required(VERSION 3.15)
8586
set(THRUST_HOST_SYSTEM_OPTIONS
8687
CPP OMP TBB
8788
CACHE INTERNAL "Valid Thrust host systems."
89+
FORCE
8890
)
8991
set(THRUST_DEVICE_SYSTEM_OPTIONS
9092
CUDA CPP OMP TBB
9193
CACHE INTERNAL "Valid Thrust device systems"
94+
FORCE
9295
)
9396

9497
# Workaround cmake issue #20670 https://gitlab.kitware.com/cmake/cmake/-/issues/20670
95-
set(THRUST_VERSION ${${CMAKE_FIND_PACKAGE_NAME}_VERSION} CACHE INTERNAL "")
96-
set(THRUST_VERSION_MAJOR ${${CMAKE_FIND_PACKAGE_NAME}_VERSION_MAJOR} CACHE INTERNAL "")
97-
set(THRUST_VERSION_MINOR ${${CMAKE_FIND_PACKAGE_NAME}_VERSION_MINOR} CACHE INTERNAL "")
98-
set(THRUST_VERSION_PATCH ${${CMAKE_FIND_PACKAGE_NAME}_VERSION_PATCH} CACHE INTERNAL "")
99-
set(THRUST_VERSION_TWEAK ${${CMAKE_FIND_PACKAGE_NAME}_VERSION_TWEAK} CACHE INTERNAL "")
100-
set(THRUST_VERSION_COUNT ${${CMAKE_FIND_PACKAGE_NAME}_VERSION_COUNT} CACHE INTERNAL "")
98+
set(THRUST_VERSION ${${CMAKE_FIND_PACKAGE_NAME}_VERSION} CACHE INTERNAL "" FORCE)
99+
set(THRUST_VERSION_MAJOR ${${CMAKE_FIND_PACKAGE_NAME}_VERSION_MAJOR} CACHE INTERNAL "" FORCE)
100+
set(THRUST_VERSION_MINOR ${${CMAKE_FIND_PACKAGE_NAME}_VERSION_MINOR} CACHE INTERNAL "" FORCE)
101+
set(THRUST_VERSION_PATCH ${${CMAKE_FIND_PACKAGE_NAME}_VERSION_PATCH} CACHE INTERNAL "" FORCE)
102+
set(THRUST_VERSION_TWEAK ${${CMAKE_FIND_PACKAGE_NAME}_VERSION_TWEAK} CACHE INTERNAL "" FORCE)
103+
set(THRUST_VERSION_COUNT ${${CMAKE_FIND_PACKAGE_NAME}_VERSION_COUNT} CACHE INTERNAL "" FORCE)
101104

102105
function(thrust_create_target target_name)
103106
thrust_debug("Assembling target ${target_name}. Options: ${ARGN}" internal)
@@ -109,21 +112,21 @@ function(thrust_create_target target_name)
109112
IGNORE_DEPRECATED_COMPILER
110113
IGNORE_DEPRECATED_CPP_11
111114
IGNORE_DEPRECATED_CPP_DIALECT
112-
)
115+
)
113116
set(keys
114117
DEVICE
115118
DEVICE_OPTION
116119
DEVICE_OPTION_DOC
117120
HOST
118121
HOST_OPTION
119122
HOST_OPTION_DOC
120-
)
123+
)
121124
cmake_parse_arguments(TCT "${options}" "${keys}" "" ${ARGN})
122125
if (TCT_UNPARSED_ARGUMENTS)
123126
message(AUTHOR_WARNING
124127
"Unrecognized arguments passed to thrust_create_target: "
125128
${TCT_UNPARSED_ARGUMENTS}
126-
)
129+
)
127130
endif()
128131

129132
# Check that the main Thrust internal target is available
@@ -133,7 +136,7 @@ function(thrust_create_target target_name)
133136
message(AUTHOR_WARNING
134137
"The `thrust_create_target` function was called outside the scope of the "
135138
"thrust targets. Call find_package again to recreate targets."
136-
)
139+
)
137140
endif()
138141

139142
_thrust_set_if_undefined(TCT_HOST CPP)
@@ -145,12 +148,14 @@ function(thrust_create_target target_name)
145148

146149
if (NOT TCT_HOST IN_LIST THRUST_HOST_SYSTEM_OPTIONS)
147150
message(FATAL_ERROR
148-
"Requested HOST=${TCT_HOST}; must be one of ${THRUST_HOST_SYSTEM_OPTIONS}")
151+
"Requested HOST=${TCT_HOST}; must be one of ${THRUST_HOST_SYSTEM_OPTIONS}"
152+
)
149153
endif()
150154

151155
if (NOT TCT_DEVICE IN_LIST THRUST_DEVICE_SYSTEM_OPTIONS)
152156
message(FATAL_ERROR
153-
"Requested DEVICE=${TCT_DEVICE}; must be one of ${THRUST_DEVICE_SYSTEM_OPTIONS}")
157+
"Requested DEVICE=${TCT_DEVICE}; must be one of ${THRUST_DEVICE_SYSTEM_OPTIONS}"
158+
)
154159
endif()
155160

156161
if (TCT_FROM_OPTIONS)
@@ -172,7 +177,7 @@ function(thrust_create_target target_name)
172177

173178
# We can just create an INTERFACE IMPORTED target here instead of going
174179
# through _thrust_declare_interface_alias as long as we aren't hanging any
175-
# Thrust/CUB include paths on ${target_name}.
180+
# Thrust/CUB include paths directly on ${target_name}.
176181
add_library(${target_name} INTERFACE IMPORTED)
177182
target_link_libraries(${target_name}
178183
INTERFACE
@@ -346,14 +351,17 @@ function(thrust_debug_internal_targets)
346351

347352
_thrust_debug_backend_targets(CPP "Thrust ${THRUST_VERSION}")
348353

349-
_thrust_debug_backend_targets(CUDA "CUB ${THRUST_CUB_VERSION}")
350-
thrust_debug_target(CUB::CUB "${THRUST_CUB_VERSION}")
354+
_thrust_debug_backend_targets(OMP "${THRUST_OMP_VERSION}")
355+
thrust_debug_target(OpenMP::OpenMP_CXX "${THRUST_OMP_VERSION}")
351356

352357
_thrust_debug_backend_targets(TBB "${THRUST_TBB_VERSION}")
353358
thrust_debug_target(TBB:tbb "${THRUST_TBB_VERSION}")
354359

355-
_thrust_debug_backend_targets(OMP "${THRUST_OMP_VERSION}")
356-
thrust_debug_target(OpenMP::OpenMP_CXX "${THRUST_OMP_VERSION}")
360+
_thrust_debug_backend_targets(CUDA
361+
"CUB ${THRUST_CUB_VERSION} libcudacxx ${THRUST_libcudacxx_VERSION}"
362+
)
363+
thrust_debug_target(CUB::CUB "${THRUST_CUB_VERSION}")
364+
thrust_debug_target(libcudacxx::libcudacxx "${THRUST_libcudacxx_VERSION}")
357365
endfunction()
358366

359367
################################################################################
@@ -434,18 +442,37 @@ function(_thrust_setup_system backend)
434442
endif()
435443
endfunction()
436444

437-
# Use the provided cub_target for the CUDA backend. If Thrust::CUDA already
445+
# Use the provided cub_target for the CUDA backend. If Thrust::CUB already
438446
# exists, this call has no effect.
439447
function(thrust_set_CUB_target cub_target)
440-
if (NOT TARGET Thrust::CUDA)
448+
if (NOT TARGET Thrust::CUB)
441449
thrust_debug("Setting CUB target to ${cub_target}" internal)
442450
# Workaround cmake issue #20670 https://gitlab.kitware.com/cmake/cmake/-/issues/20670
443-
set(THRUST_CUB_VERSION ${CUB_VERSION} CACHE INTERNAL "CUB version used by Thrust")
444-
_thrust_declare_interface_alias(Thrust::CUDA _Thrust_CUDA)
445-
target_link_libraries(_Thrust_CUDA INTERFACE Thrust::Thrust ${cub_target})
451+
set(THRUST_CUB_VERSION ${CUB_VERSION} CACHE INTERNAL
452+
"CUB version used by Thrust"
453+
FORCE
454+
)
455+
_thrust_declare_interface_alias(Thrust::CUB _Thrust_CUB)
456+
target_link_libraries(_Thrust_CUB INTERFACE ${cub_target})
446457
thrust_debug_target(${cub_target} "${THRUST_CUB_VERSION}" internal)
447-
thrust_debug_target(Thrust::CUDA "CUB ${THRUST_CUB_VERSION}" internal)
448-
_thrust_setup_system(CUDA)
458+
thrust_debug_target(Thrust::CUB "CUB ${THRUST_CUB_VERSION}" internal)
459+
endif()
460+
endfunction()
461+
462+
# Use the provided libcudacxx_target for the CUDA backend. If Thrust::libcudacxx
463+
# already exists, this call has no effect.
464+
function(thrust_set_libcudacxx_target libcudacxx_target)
465+
if (NOT TARGET Thrust::libcudacxx)
466+
thrust_debug("Setting libcudacxx target to ${libcudacxx_target}" internal)
467+
# Workaround cmake issue #20670 https://gitlab.kitware.com/cmake/cmake/-/issues/20670
468+
set(THRUST_libcudacxx_VERSION ${libcudacxx_VERSION} CACHE INTERNAL
469+
"libcudacxx version used by Thrust"
470+
FORCE
471+
)
472+
_thrust_declare_interface_alias(Thrust::libcudacxx _Thrust_libcudacxx)
473+
target_link_libraries(_Thrust_libcudacxx INTERFACE ${libcudacxx_target})
474+
thrust_debug_target(${libcudacxx_target} "${THRUST_libcudacxx_VERSION}" internal)
475+
thrust_debug_target(Thrust::libcudacxx "libcudacxx ${THRUST_libcudacxx_VERSION}" internal)
449476
endif()
450477
endfunction()
451478

@@ -455,7 +482,10 @@ function(thrust_set_TBB_target tbb_target)
455482
if (NOT TARGET Thrust::TBB)
456483
thrust_debug("Setting TBB target to ${tbb_target}" internal)
457484
# Workaround cmake issue #20670 https://gitlab.kitware.com/cmake/cmake/-/issues/20670
458-
set(THRUST_TBB_VERSION ${TBB_VERSION} CACHE INTERNAL "TBB version used by Thrust")
485+
set(THRUST_TBB_VERSION ${TBB_VERSION} CACHE INTERNAL
486+
"TBB version used by Thrust"
487+
FORCE
488+
)
459489
_thrust_declare_interface_alias(Thrust::TBB _Thrust_TBB)
460490
target_link_libraries(_Thrust_TBB INTERFACE Thrust::Thrust ${tbb_target})
461491
thrust_debug_target(${tbb_target} "${THRUST_TBB_VERSION}" internal)
@@ -470,7 +500,10 @@ function(thrust_set_OMP_target omp_target)
470500
if (NOT TARGET Thrust::OMP)
471501
thrust_debug("Setting OMP target to ${omp_target}" internal)
472502
# Workaround cmake issue #20670 https://gitlab.kitware.com/cmake/cmake/-/issues/20670
473-
set(THRUST_OMP_VERSION ${OpenMP_CXX_VERSION} CACHE INTERNAL "OpenMP version used by Thrust")
503+
set(THRUST_OMP_VERSION ${OpenMP_CXX_VERSION} CACHE INTERNAL
504+
"OpenMP version used by Thrust"
505+
FORCE
506+
)
474507
_thrust_declare_interface_alias(Thrust::OMP _Thrust_OMP)
475508
target_link_libraries(_Thrust_OMP INTERFACE Thrust::Thrust ${omp_target})
476509
thrust_debug_target(${omp_target} "${THRUST_OMP_VERSION}" internal)
@@ -495,9 +528,29 @@ endfunction()
495528
# #20670 -- otherwise variables like CUB_VERSION, etc won't be in the caller's
496529
# scope.
497530
macro(_thrust_find_CUDA required)
498-
if (NOT TARGET Thrust::CUDA)
531+
# Find libcudacxx first to ensure that CUB finds the same package.
532+
if (NOT TARGET Thrust::libcudacxx)
533+
thrust_debug("Searching for libcudacxx ${required}" internal)
534+
find_package(libcudacxx 1.7.999 CONFIG
535+
${_THRUST_QUIET_FLAG}
536+
${required}
537+
NO_DEFAULT_PATH # Only check the explicit HINTS below:
538+
HINTS
539+
"${_THRUST_INCLUDE_DIR}/dependencies/libcudacxx" # Source layout (GitHub)
540+
"${_THRUST_INCLUDE_DIR}/../libcudacxx" # Source layout (Perforce)
541+
"${_THRUST_CMAKE_DIR}/.." # Install layout
542+
)
543+
544+
if (TARGET libcudacxx::libcudacxx)
545+
thrust_set_libcudacxx_target(libcudacxx::libcudacxx)
546+
else()
547+
thrust_debug("libcudacxx not found!" internal)
548+
endif()
549+
endif()
550+
551+
if (NOT TARGET Thrust::CUB)
499552
thrust_debug("Searching for CUB ${required}" internal)
500-
find_package(CUB CONFIG
553+
find_package(CUB ${THRUST_VERSION} EXACT CONFIG
501554
${_THRUST_QUIET_FLAG}
502555
${required}
503556
NO_DEFAULT_PATH # Only check the explicit HINTS below:
@@ -513,6 +566,17 @@ macro(_thrust_find_CUDA required)
513566
thrust_debug("CUB not found!" internal)
514567
endif()
515568
endif()
569+
570+
if (NOT TARGET Thrust::CUDA)
571+
_thrust_declare_interface_alias(Thrust::CUDA _Thrust_CUDA)
572+
_thrust_setup_system(CUDA)
573+
target_link_libraries(_Thrust_CUDA INTERFACE
574+
Thrust::Thrust
575+
Thrust::CUB
576+
Thrust::libcudacxx
577+
)
578+
thrust_debug_target(Thrust::CUDA "" internal)
579+
endif()
516580
endmacro()
517581

518582
# This must be a macro instead of a function to ensure that backends passed to
@@ -619,21 +683,25 @@ endmacro()
619683
#
620684

621685
if (${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
622-
set(_THRUST_QUIET ON CACHE INTERNAL "Quiet mode enabled for Thrust find_package calls.")
623-
set(_THRUST_QUIET_FLAG "QUIET" CACHE INTERNAL "")
686+
set(_THRUST_QUIET ON CACHE INTERNAL "Quiet mode enabled for Thrust find_package calls." FORCE)
687+
set(_THRUST_QUIET_FLAG "QUIET" CACHE INTERNAL "" FORCE)
624688
else()
625689
unset(_THRUST_QUIET CACHE)
626690
unset(_THRUST_QUIET_FLAG CACHE)
627691
endif()
628692

629-
set(_THRUST_CMAKE_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "Location of thrust-config.cmake")
693+
set(_THRUST_CMAKE_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL
694+
"Location of thrust-config.cmake"
695+
FORCE
696+
)
630697

631698
# Internal target that actually holds the Thrust interface. Used by all other Thrust targets.
632699
if (NOT TARGET Thrust::Thrust)
633700
_thrust_declare_interface_alias(Thrust::Thrust _Thrust_Thrust)
634701
# Pull in the include dir detected by thrust-config-version.cmake
635702
set(_THRUST_INCLUDE_DIR "${_THRUST_VERSION_INCLUDE_DIR}"
636703
CACHE INTERNAL "Location of Thrust headers."
704+
FORCE
637705
)
638706
unset(_THRUST_VERSION_INCLUDE_DIR CACHE) # Clear tmp variable from cache
639707
target_include_directories(_Thrust_Thrust INTERFACE "${_THRUST_INCLUDE_DIR}")

0 commit comments

Comments
 (0)