From a39e385cc6be20754f859dd266021ab1d88459d3 Mon Sep 17 00:00:00 2001 From: Allison Vacanti Date: Tue, 22 Sep 2020 16:53:51 -0400 Subject: [PATCH] Add support for importing CUB targets with add_subdirectory. --- CMakeLists.txt | 8 ++++++ cmake/CubAddSubdir.cmake | 4 +++ examples/CMakeLists.txt | 1 + examples/cmake/CMakeLists.txt | 11 ++++++++ examples/cmake/add_subdir/CMakeLists.txt | 32 ++++++++++++++++++++++++ examples/cmake/add_subdir/dummy.cu | 8 ++++++ 6 files changed, 64 insertions(+) create mode 100644 cmake/CubAddSubdir.cmake create mode 100644 examples/cmake/CMakeLists.txt create mode 100644 examples/cmake/add_subdir/CMakeLists.txt create mode 100644 examples/cmake/add_subdir/dummy.cu diff --git a/CMakeLists.txt b/CMakeLists.txt index bb6f345c8e..410efba90f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,11 @@ +# Support adding CUB to a parent project via add_subdirectory. +# See examples/cmake/add_subdir/CMakeLists.txt for details. +if (NOT "${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_LIST_DIR}" AND + NOT CUB_IN_THRUST) + include(cmake/CubAddSubdir.cmake) + return() +endif() + # Will be increased to 3.18 when C++17 is enabled: cmake_minimum_required(VERSION 3.15) diff --git a/cmake/CubAddSubdir.cmake b/cmake/CubAddSubdir.cmake new file mode 100644 index 0000000000..99829b196c --- /dev/null +++ b/cmake/CubAddSubdir.cmake @@ -0,0 +1,4 @@ +find_package(CUB REQUIRED CONFIG + NO_DEFAULT_PATH # Only check the explicit path in HINTS: + HINTS "${CMAKE_CURRENT_LIST_DIR}/.." +) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index eabb7d5009..9d93253e84 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -54,5 +54,6 @@ function(cub_add_example target_name_var example_name example_src cub_target) ) endfunction() +add_subdirectory(cmake) add_subdirectory(block) add_subdirectory(device) diff --git a/examples/cmake/CMakeLists.txt b/examples/cmake/CMakeLists.txt new file mode 100644 index 0000000000..0fc9296f5d --- /dev/null +++ b/examples/cmake/CMakeLists.txt @@ -0,0 +1,11 @@ +add_test( + NAME cub.example.cmake.add_subdir + COMMAND "${CMAKE_COMMAND}" + --log-level=VERBOSE + -G "${CMAKE_GENERATOR}" + -S "${CMAKE_CURRENT_SOURCE_DIR}/add_subdir" + -B "${CMAKE_CURRENT_BINARY_DIR}/add_subdir" + -D "CUB_ROOT=${CUB_SOURCE_DIR}" + -D "CMAKE_CUDA_COMPILER=${CMAKE_CUDA_COMPILER}" + -D "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" +) diff --git a/examples/cmake/add_subdir/CMakeLists.txt b/examples/cmake/add_subdir/CMakeLists.txt new file mode 100644 index 0000000000..41a460593d --- /dev/null +++ b/examples/cmake/add_subdir/CMakeLists.txt @@ -0,0 +1,32 @@ +# This example demonstrates / tests adding CUB via a CMake add_subdirectory +# call from a parent project. + +cmake_minimum_required(VERSION 3.15) + +# Silence warnings about empty CUDA_ARCHITECTURES properties on example targets: +if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.18) + cmake_policy(SET CMP0104 OLD) +endif() + +project(CubAddSubDirExample CUDA) + +# Use your project's checkout of CUB here, for most cases +# `add_subdirectory(cub)` will be sufficient. +add_subdirectory("${CUB_ROOT}" cub) + +# Link the CUB::CUB target to your project's targets +add_executable(HelloCUB dummy.cu) +target_link_libraries(HelloCUB CUB::CUB) + +# +# Validation +# + +function(assert_target target_name) + if (NOT TARGET "${target_name}") + message(FATAL_ERROR "Target '${target_name}' not defined.") + endif() +endfunction() + +assert_target(CUB::CUB) +assert_target(HelloCUB) diff --git a/examples/cmake/add_subdir/dummy.cu b/examples/cmake/add_subdir/dummy.cu new file mode 100644 index 0000000000..d3d1870833 --- /dev/null +++ b/examples/cmake/add_subdir/dummy.cu @@ -0,0 +1,8 @@ +#include + +#include + +int main() +{ + std::cout << "Hello from CUB version " << CUB_VERSION << ":\n"; +}