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

Add support for importing CUB targets with add_subdirectory. #200

Merged
merged 1 commit into from
Oct 5, 2020
Merged
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
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
4 changes: 4 additions & 0 deletions cmake/CubAddSubdir.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
find_package(CUB REQUIRED CONFIG
NO_DEFAULT_PATH # Only check the explicit path in HINTS:
HINTS "${CMAKE_CURRENT_LIST_DIR}/.."
)
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 11 additions & 0 deletions examples/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}"
)
32 changes: 32 additions & 0 deletions examples/cmake/add_subdir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions examples/cmake/add_subdir/dummy.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <cub/config.cuh>

#include <iostream>

int main()
{
std::cout << "Hello from CUB version " << CUB_VERSION << ":\n";
}