Skip to content

Commit 7ded655

Browse files
Peter Boettcherbradking
Peter Boettcher
authored andcommittedAug 16, 2016
FindCUDA: Take NVCC include directories from target properties
Fixes issue where include directories specified on the target are not passed on to NVCC. This includes both target_include_directories() as well as include directories added by dependency chaining. Closes: #14201
1 parent e240a7c commit 7ded655

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed
 

‎Modules/FindCUDA.cmake

+11-21
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ else()
730730
endif()
731731

732732
# Set the user list of include dir to nothing to initialize it.
733-
set (CUDA_NVCC_INCLUDE_ARGS_USER "")
733+
set (CUDA_NVCC_INCLUDE_DIRS_USER "")
734734
set (CUDA_INCLUDE_DIRS ${CUDA_TOOLKIT_INCLUDE})
735735

736736
macro(cuda_find_library_local_first_with_path_ext _var _names _doc _path_ext )
@@ -1025,7 +1025,7 @@ find_package_handle_standard_args(CUDA
10251025
# Add include directories to pass to the nvcc command.
10261026
macro(CUDA_INCLUDE_DIRECTORIES)
10271027
foreach(dir ${ARGN})
1028-
list(APPEND CUDA_NVCC_INCLUDE_ARGS_USER -I${dir})
1028+
list(APPEND CUDA_NVCC_INCLUDE_DIRS_USER ${dir})
10291029
endforeach()
10301030
endmacro()
10311031

@@ -1249,17 +1249,15 @@ macro(CUDA_WRAP_SRCS cuda_target format generated_files)
12491249
endif()
12501250

12511251
# Initialize our list of includes with the user ones followed by the CUDA system ones.
1252-
set(CUDA_NVCC_INCLUDE_ARGS ${CUDA_NVCC_INCLUDE_ARGS_USER} "-I${CUDA_INCLUDE_DIRS}")
1253-
# Get the include directories for this directory and use them for our nvcc command.
1254-
# Remove duplicate entries which may be present since include_directories
1255-
# in CMake >= 2.8.8 does not remove them.
1256-
get_directory_property(CUDA_NVCC_INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES)
1257-
list(REMOVE_DUPLICATES CUDA_NVCC_INCLUDE_DIRECTORIES)
1258-
if(CUDA_NVCC_INCLUDE_DIRECTORIES)
1259-
foreach(dir ${CUDA_NVCC_INCLUDE_DIRECTORIES})
1260-
list(APPEND CUDA_NVCC_INCLUDE_ARGS -I${dir})
1261-
endforeach()
1262-
endif()
1252+
set(CUDA_NVCC_INCLUDE_DIRS ${CUDA_NVCC_INCLUDE_DIRS_USER} "${CUDA_INCLUDE_DIRS}")
1253+
# Append the include directories for this target via generator expression, which is
1254+
# expanded by the FILE(GENERATE) call below. This generator expression captures all
1255+
# include dirs set by the user, whether via directory properties or target properties
1256+
list(APPEND CUDA_NVCC_INCLUDE_DIRS "$<TARGET_PROPERTY:${cuda_target},INCLUDE_DIRECTORIES>")
1257+
1258+
# Do the same thing with compile definitions
1259+
set(CUDA_NVCC_COMPILE_DEFINITIONS "$<TARGET_PROPERTY:${cuda_target},COMPILE_DEFINITIONS>")
1260+
12631261

12641262
# Reset these variables
12651263
set(CUDA_WRAP_OPTION_NVCC_FLAGS)
@@ -1349,14 +1347,6 @@ macro(CUDA_WRAP_SRCS cuda_target format generated_files)
13491347
string(REGEX REPLACE "[-]+std=c\\+\\+11" "" _cuda_host_flags "${_cuda_host_flags}")
13501348
endif()
13511349

1352-
# Get the list of definitions from the directory property
1353-
get_directory_property(CUDA_NVCC_DEFINITIONS COMPILE_DEFINITIONS)
1354-
if(CUDA_NVCC_DEFINITIONS)
1355-
foreach(_definition ${CUDA_NVCC_DEFINITIONS})
1356-
list(APPEND nvcc_flags "-D${_definition}")
1357-
endforeach()
1358-
endif()
1359-
13601350
if(_cuda_build_shared_libs)
13611351
list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS")
13621352
endif()

‎Modules/FindCUDA/run_nvcc.cmake

+16-1
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,25 @@ set(CUDA_NVCC_EXECUTABLE "@CUDA_NVCC_EXECUTABLE@") # path
7373
set(CUDA_NVCC_FLAGS @CUDA_NVCC_FLAGS@ ;; @CUDA_WRAP_OPTION_NVCC_FLAGS@) # list
7474
@CUDA_NVCC_FLAGS_CONFIG@
7575
set(nvcc_flags @nvcc_flags@) # list
76-
set(CUDA_NVCC_INCLUDE_ARGS "@CUDA_NVCC_INCLUDE_ARGS@") # list (needs to be in quotes to handle spaces properly).
76+
set(CUDA_NVCC_INCLUDE_DIRS "@CUDA_NVCC_INCLUDE_DIRS@") # list (needs to be in quotes to handle spaces properly).
77+
set(CUDA_NVCC_COMPILE_DEFINITIONS "@CUDA_NVCC_COMPILE_DEFINITIONS@") # list (needs to be in quotes to handle spaces properly).
7778
set(format_flag "@format_flag@") # string
7879
set(cuda_language_flag @cuda_language_flag@) # list
7980

81+
# Clean up list of include directories and add -I flags
82+
list(REMOVE_DUPLICATES CUDA_NVCC_INCLUDE_DIRS)
83+
set(CUDA_NVCC_INCLUDE_ARGS)
84+
foreach(dir ${CUDA_NVCC_INCLUDE_DIRS})
85+
# Extra quotes are added around each flag to help nvcc parse out flags with spaces.
86+
list(APPEND CUDA_NVCC_INCLUDE_ARGS "-I${dir}")
87+
endforeach()
88+
89+
# Clean up list of compile definitions, add -D flags, and append to nvcc_flags
90+
list(REMOVE_DUPLICATES CUDA_NVCC_COMPILE_DEFINITIONS)
91+
foreach(def ${CUDA_NVCC_COMPILE_DEFINITIONS})
92+
list(APPEND nvcc_flags "-D${def}")
93+
endforeach()
94+
8095
if(build_cubin AND NOT generated_cubin_file)
8196
message(FATAL_ERROR "You must specify generated_cubin_file on the command line")
8297
endif()

0 commit comments

Comments
 (0)