FindCUDA.cmake 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. # - Tools for building CUDA C files: libraries and build dependencies.
  2. # This script locates the NVIDIA CUDA C tools. It should work on linux, windows,
  3. # and mac and should be reasonably up to date with CUDA C releases.
  4. #
  5. # This script makes use of the standard find_package arguments of <VERSION>,
  6. # REQUIRED and QUIET. CUDA_FOUND will report if an acceptable version of CUDA
  7. # was found.
  8. #
  9. # The script will prompt the user to specify CUDA_TOOLKIT_ROOT_DIR if the prefix
  10. # cannot be determined by the location of nvcc in the system path and REQUIRED
  11. # is specified to find_package(). To use a different installed version of the
  12. # toolkit set the environment variable CUDA_BIN_PATH before running cmake
  13. # (e.g. CUDA_BIN_PATH=/usr/local/cuda1.0 instead of the default /usr/local/cuda)
  14. # or set CUDA_TOOLKIT_ROOT_DIR after configuring. If you change the value of
  15. # CUDA_TOOLKIT_ROOT_DIR, various components that depend on the path will be
  16. # relocated.
  17. #
  18. # It might be necessary to set CUDA_TOOLKIT_ROOT_DIR manually on certain
  19. # platforms, or to use a cuda runtime not installed in the default location. In
  20. # newer versions of the toolkit the cuda library is included with the graphics
  21. # driver- be sure that the driver version matches what is needed by the cuda
  22. # runtime version.
  23. #
  24. # The following variables affect the behavior of the macros in the script (in
  25. # alphebetical order). Note that any of these flags can be changed multiple
  26. # times in the same directory before calling CUDA_ADD_EXECUTABLE,
  27. # CUDA_ADD_LIBRARY, CUDA_COMPILE, CUDA_COMPILE_PTX or CUDA_WRAP_SRCS.
  28. #
  29. # CUDA_64_BIT_DEVICE_CODE (Default matches host bit size)
  30. # -- Set to ON to compile for 64 bit device code, OFF for 32 bit device code.
  31. # Note that making this different from the host code when generating object
  32. # or C files from CUDA code just won't work, because size_t gets defined by
  33. # nvcc in the generated source. If you compile to PTX and then load the
  34. # file yourself, you can mix bit sizes between device and host.
  35. #
  36. # CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE (Default ON)
  37. # -- Set to ON if you want the custom build rule to be attached to the source
  38. # file in Visual Studio. Turn OFF if you add the same cuda file to multiple
  39. # targets.
  40. #
  41. # This allows the user to build the target from the CUDA file; however, bad
  42. # things can happen if the CUDA source file is added to multiple targets.
  43. # When performing parallel builds it is possible for the custom build
  44. # command to be run more than once and in parallel causing cryptic build
  45. # errors. VS runs the rules for every source file in the target, and a
  46. # source can have only one rule no matter how many projects it is added to.
  47. # When the rule is run from multiple targets race conditions can occur on
  48. # the generated file. Eventually everything will get built, but if the user
  49. # is unaware of this behavior, there may be confusion. It would be nice if
  50. # this script could detect the reuse of source files across multiple targets
  51. # and turn the option off for the user, but no good solution could be found.
  52. #
  53. # CUDA_BUILD_CUBIN (Default OFF)
  54. # -- Set to ON to enable and extra compilation pass with the -cubin option in
  55. # Device mode. The output is parsed and register, shared memory usage is
  56. # printed during build.
  57. #
  58. # CUDA_BUILD_EMULATION (Default OFF for device mode)
  59. # -- Set to ON for Emulation mode. -D_DEVICEEMU is defined for CUDA C files
  60. # when CUDA_BUILD_EMULATION is TRUE.
  61. #
  62. # CUDA_GENERATED_OUTPUT_DIR (Default CMAKE_CURRENT_BINARY_DIR)
  63. # -- Set to the path you wish to have the generated files placed. If it is
  64. # blank output files will be placed in CMAKE_CURRENT_BINARY_DIR.
  65. # Intermediate files will always be placed in
  66. # CMAKE_CURRENT_BINARY_DIR/CMakeFiles.
  67. #
  68. # CUDA_HOST_COMPILATION_CPP (Default ON)
  69. # -- Set to OFF for C compilation of host code.
  70. #
  71. # CUDA_NVCC_FLAGS
  72. # CUDA_NVCC_FLAGS_<CONFIG>
  73. # -- Additional NVCC command line arguments. NOTE: multiple arguments must be
  74. # semi-colon delimited (e.g. --compiler-options;-Wall)
  75. #
  76. # CUDA_PROPAGATE_HOST_FLAGS (Default ON)
  77. # -- Set to ON to propagate CMAKE_{C,CXX}_FLAGS and their configuration
  78. # dependent counterparts (e.g. CMAKE_C_FLAGS_DEBUG) automatically to the
  79. # host compiler through nvcc's -Xcompiler flag. This helps make the
  80. # generated host code match the rest of the system better. Sometimes
  81. # certain flags give nvcc problems, and this will help you turn the flag
  82. # propagation off. This does not affect the flags supplied directly to nvcc
  83. # via CUDA_NVCC_FLAGS or through the OPTION flags specified through
  84. # CUDA_ADD_LIBRARY, CUDA_ADD_EXECUTABLE, or CUDA_WRAP_SRCS. Flags used for
  85. # shared library compilation are not affected by this flag.
  86. #
  87. # CUDA_VERBOSE_BUILD (Default OFF)
  88. # -- Set to ON to see all the commands used when building the CUDA file. When
  89. # using a Makefile generator the value defaults to VERBOSE (run make
  90. # VERBOSE=1 to see output), although setting CUDA_VERBOSE_BUILD to ON will
  91. # always print the output.
  92. #
  93. # The script creates the following macros (in alphebetical order):
  94. #
  95. # CUDA_ADD_CUFFT_TO_TARGET( cuda_target )
  96. # -- Adds the cufft library to the target (can be any target). Handles whether
  97. # you are in emulation mode or not.
  98. #
  99. # CUDA_ADD_CUBLAS_TO_TARGET( cuda_target )
  100. # -- Adds the cublas library to the target (can be any target). Handles
  101. # whether you are in emulation mode or not.
  102. #
  103. # CUDA_ADD_EXECUTABLE( cuda_target file0 file1 ...
  104. # [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
  105. # -- Creates an executable "cuda_target" which is made up of the files
  106. # specified. All of the non CUDA C files are compiled using the standard
  107. # build rules specified by CMAKE and the cuda files are compiled to object
  108. # files using nvcc and the host compiler. In addition CUDA_INCLUDE_DIRS is
  109. # added automatically to include_directories(). Standard CMake target calls
  110. # can be used on the target after calling this macro
  111. # (e.g. set_target_properties and target_link_libraries).
  112. #
  113. # CUDA_ADD_LIBRARY( cuda_target file0 file1 ...
  114. # [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
  115. # -- Same as CUDA_ADD_EXECUTABLE except that a library is created.
  116. #
  117. # CUDA_BUILD_CLEAN_TARGET()
  118. # -- Creates a convience target that deletes all the dependency files
  119. # generated. You should make clean after running this target to ensure the
  120. # dependency files get regenerated.
  121. #
  122. # CUDA_COMPILE( generated_files file0 file1 ... [STATIC | SHARED | MODULE]
  123. # [OPTIONS ...] )
  124. # -- Returns a list of generated files from the input source files to be used
  125. # with ADD_LIBRARY or ADD_EXECUTABLE.
  126. #
  127. # CUDA_COMPILE_PTX( generated_files file0 file1 ... [OPTIONS ...] )
  128. # -- Returns a list of PTX files generated from the input source files.
  129. #
  130. # CUDA_INCLUDE_DIRECTORIES( path0 path1 ... )
  131. # -- Sets the directories that should be passed to nvcc
  132. # (e.g. nvcc -Ipath0 -Ipath1 ... ). These paths usually contain other .cu
  133. # files.
  134. #
  135. # CUDA_WRAP_SRCS ( cuda_target format generated_files file0 file1 ...
  136. # [STATIC | SHARED | MODULE] [OPTIONS ...] )
  137. # -- This is where all the magic happens. CUDA_ADD_EXECUTABLE,
  138. # CUDA_ADD_LIBRARY, CUDA_COMPILE, and CUDA_COMPILE_PTX all call this
  139. # function under the hood.
  140. #
  141. # Given the list of files (file0 file1 ... fileN) this macro generates
  142. # custom commands that generate either PTX or linkable objects (use "PTX" or
  143. # "OBJ" for the format argument to switch). Files that don't end with .cu
  144. # or have the HEADER_FILE_ONLY property are ignored.
  145. #
  146. # The arguments passed in after OPTIONS are extra command line options to
  147. # give to nvcc. You can also specify per configuration options by
  148. # specifying the name of the configuration followed by the options. General
  149. # options must preceed configuration specific options. Not all
  150. # configurations need to be specified, only the ones provided will be used.
  151. #
  152. # OPTIONS -DFLAG=2 "-DFLAG_OTHER=space in flag"
  153. # DEBUG -g
  154. # RELEASE --use_fast_math
  155. # RELWITHDEBINFO --use_fast_math;-g
  156. # MINSIZEREL --use_fast_math
  157. #
  158. # For certain configurations (namely VS generating object files with
  159. # CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE set to ON), no generated file will
  160. # be produced for the given cuda file. This is because when you add the
  161. # cuda file to Visual Studio it knows that this file produces an object file
  162. # and will link in the resulting object file automatically.
  163. #
  164. # This script also looks at optional arguments STATIC, SHARED, or MODULE to
  165. # override the behavior specified by the value of the CMake variable
  166. # BUILD_SHARED_LIBS. See BUILD_SHARED_LIBS below for more details.
  167. #
  168. # This script will also generate a separate cmake script that is used at
  169. # build time to invoke nvcc. This is for serveral reasons.
  170. #
  171. # 1. nvcc can return negative numbers as return values which confuses
  172. # Visual Studio into thinking that the command succeeded. The script now
  173. # checks the error codes and produces errors when there was a problem.
  174. #
  175. # 2. nvcc has been known to not delete incomplete results when it
  176. # encounters problems. This confuses build systems into thinking the
  177. # target was generated when in fact an unusable file exists. The script
  178. # now deletes the output files if there was an error.
  179. #
  180. # 3. By putting all the options that affect the build into a file and then
  181. # make the build rule dependent on the file, the output files will be
  182. # regenerated when the options change.
  183. #
  184. # In addition, on some systems special flags are added for building objects
  185. # intended for shared libraries. FindCUDA make use of the CMake variable
  186. # BUILD_SHARED_LIBS and the usual STATIC, SHARED, and MODULE arguments to
  187. # determine if these flags should be used. Please set BUILD_SHARED_LIBS or
  188. # pass in STATIC, SHARED, or MODULE according to how the objects are to be
  189. # used before calling CUDA_ADD_LIBRARY. A preprocessor macro,
  190. # <target_name>_EXPORTS is defined when BUILD_SHARED_LIBS is defined.
  191. #
  192. # Flags passed into add_definitions with -D or /D are passed along to nvcc.
  193. #
  194. # The script defines the following variables:
  195. #
  196. # CUDA_VERSION_MAJOR -- The major version of cuda as reported by nvcc.
  197. # CUDA_VERSION_MINOR -- The minor version.
  198. # CUDA_VERSION
  199. # CUDA_VERSION_STRING -- CUDA_VERSION_MAJOR.CUDA_VERSION_MINOR
  200. #
  201. # CUDA_INCLUDE_DIRS -- Include directory for cuda headers. Added automatically
  202. # for CUDA_ADD_EXECUTABLE and CUDA_ADD_LIBRARY.
  203. # CUDA_LIBRARIES -- Cuda RT library.
  204. # CUDA_CUT_INCLUDE_DIR -- Include directory for cuda SDK headers (cutil.h).
  205. # CUDA_CUT_LIBRARIES -- SDK libraries.
  206. # CUDA_CUFFT_LIBRARIES -- Device or emulation library for the Cuda FFT
  207. # implementation (alternative to:
  208. # CUDA_ADD_CUFFT_TO_TARGET macro)
  209. # CUDA_CUBLAS_LIBRARIES -- Device or emulation library for the Cuda BLAS
  210. # implementation (alterative to:
  211. # CUDA_ADD_CUBLAS_TO_TARGET macro).
  212. #
  213. #
  214. # James Bigler, NVIDIA Corp
  215. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  216. #
  217. # Copyright (c) 2008-2009
  218. # NVIDIA Corp.
  219. #
  220. # Copyright (c) 2007-2009
  221. # Scientific Computing and Imaging Institute, University of Utah
  222. #
  223. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  224. # for the text of the license.
  225. # The MIT License
  226. #
  227. # License for the specific language governing rights and limitations under
  228. # Permission is hereby granted, free of charge, to any person obtaining a
  229. # copy of this software and associated documentation files (the "Software"),
  230. # to deal in the Software without restriction, including without limitation
  231. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  232. # and/or sell copies of the Software, and to permit persons to whom the
  233. # Software is furnished to do so, subject to the following conditions:
  234. #
  235. # The above copyright notice and this permission notice shall be included
  236. # in all copies or substantial portions of the Software.
  237. #
  238. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  239. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  240. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  241. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  242. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  243. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  244. # DEALINGS IN THE SOFTWARE.
  245. #
  246. ###############################################################################
  247. # FindCUDA.cmake
  248. # We need to have at least this version to support the VERSION_LESS argument to 'if' (2.6.2) and unset (2.6.3)
  249. cmake_policy(PUSH)
  250. cmake_minimum_required(VERSION 2.6.3)
  251. cmake_policy(POP)
  252. # This macro helps us find the location of helper files we will need the full path to
  253. macro(CUDA_FIND_HELPER_FILE _name _extension)
  254. set(_full_name "${_name}.${_extension}")
  255. # CMAKE_CURRENT_LIST_FILE contains the full path to the file currently being
  256. # processed. Using this variable, we can pull out the current path, and
  257. # provide a way to get access to the other files we need local to here.
  258. get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
  259. find_file(CUDA_${_name} ${_full_name} PATHS ${CMAKE_CURRENT_LIST_DIR}/FindCUDA NO_DEFAULT_PATH)
  260. if(NOT CUDA_${_name})
  261. set(error_message "${_full_name} not found in CMAKE_MODULE_PATH")
  262. if(CUDA_FIND_REQUIRED)
  263. message(FATAL_ERROR "${error_message}")
  264. else(CUDA_FIND_REQUIRED)
  265. if(NOT CUDA_FIND_QUIETLY)
  266. message(STATUS "${error_message}")
  267. endif(NOT CUDA_FIND_QUIETLY)
  268. endif(CUDA_FIND_REQUIRED)
  269. endif(NOT CUDA_${_name})
  270. # Set this variable as internal, so the user isn't bugged with it.
  271. set(CUDA_${_name} ${CUDA_${_name}} CACHE INTERNAL "Location of ${_full_name}" FORCE)
  272. endmacro(CUDA_FIND_HELPER_FILE)
  273. #####################################################################
  274. ## CUDA_INCLUDE_NVCC_DEPENDENCIES
  275. ##
  276. # So we want to try and include the dependency file if it exists. If
  277. # it doesn't exist then we need to create an empty one, so we can
  278. # include it.
  279. # If it does exist, then we need to check to see if all the files it
  280. # depends on exist. If they don't then we should clear the dependency
  281. # file and regenerate it later. This covers the case where a header
  282. # file has disappeared or moved.
  283. macro(CUDA_INCLUDE_NVCC_DEPENDENCIES dependency_file)
  284. set(CUDA_NVCC_DEPEND)
  285. set(CUDA_NVCC_DEPEND_REGENERATE FALSE)
  286. # Include the dependency file. Create it first if it doesn't exist . The
  287. # INCLUDE puts a dependency that will force CMake to rerun and bring in the
  288. # new info when it changes. DO NOT REMOVE THIS (as I did and spent a few
  289. # hours figuring out why it didn't work.
  290. if(NOT EXISTS ${dependency_file})
  291. file(WRITE ${dependency_file} "#FindCUDA.cmake generated file. Do not edit.\n")
  292. endif()
  293. # Always include this file to force CMake to run again next
  294. # invocation and rebuild the dependencies.
  295. #message("including dependency_file = ${dependency_file}")
  296. include(${dependency_file})
  297. # Now we need to verify the existence of all the included files
  298. # here. If they aren't there we need to just blank this variable and
  299. # make the file regenerate again.
  300. # if(DEFINED CUDA_NVCC_DEPEND)
  301. # message("CUDA_NVCC_DEPEND set")
  302. # else()
  303. # message("CUDA_NVCC_DEPEND NOT set")
  304. # endif()
  305. if(CUDA_NVCC_DEPEND)
  306. #message("CUDA_NVCC_DEPEND true")
  307. foreach(f ${CUDA_NVCC_DEPEND})
  308. #message("searching for ${f}")
  309. if(NOT EXISTS ${f})
  310. #message("file ${f} not found")
  311. set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
  312. endif()
  313. endforeach(f)
  314. else(CUDA_NVCC_DEPEND)
  315. #message("CUDA_NVCC_DEPEND false")
  316. # No dependencies, so regenerate the file.
  317. set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
  318. endif(CUDA_NVCC_DEPEND)
  319. #message("CUDA_NVCC_DEPEND_REGENERATE = ${CUDA_NVCC_DEPEND_REGENERATE}")
  320. # No incoming dependencies, so we need to generate them. Make the
  321. # output depend on the dependency file itself, which should cause the
  322. # rule to re-run.
  323. if(CUDA_NVCC_DEPEND_REGENERATE)
  324. file(WRITE ${dependency_file} "#FindCUDA.cmake generated file. Do not edit.\n")
  325. endif(CUDA_NVCC_DEPEND_REGENERATE)
  326. endmacro(CUDA_INCLUDE_NVCC_DEPENDENCIES)
  327. ###############################################################################
  328. ###############################################################################
  329. # Setup variables' defaults
  330. ###############################################################################
  331. ###############################################################################
  332. # Allow the user to specify if the device code is supposed to be 32 or 64 bit.
  333. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  334. set(CUDA_64_BIT_DEVICE_CODE_DEFAULT ON)
  335. else()
  336. set(CUDA_64_BIT_DEVICE_CODE_DEFAULT OFF)
  337. endif()
  338. option(CUDA_64_BIT_DEVICE_CODE "Compile device code in 64 bit mode" ${CUDA_64_BIT_DEVICE_CODE_DEFAULT})
  339. # Attach the build rule to the source file in VS. This option
  340. option(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE "Attach the build rule to the CUDA source file. Enable only when the CUDA source file is added to at most one target." ON)
  341. # Prints out extra information about the cuda file during compilation
  342. option(CUDA_BUILD_CUBIN "Generate and parse .cubin files in Device mode." OFF)
  343. # Set whether we are using emulation or device mode.
  344. option(CUDA_BUILD_EMULATION "Build in Emulation mode" OFF)
  345. # Where to put the generated output.
  346. set(CUDA_GENERATED_OUTPUT_DIR "" CACHE PATH "Directory to put all the output files. If blank it will default to the CMAKE_CURRENT_BINARY_DIR")
  347. # Parse HOST_COMPILATION mode.
  348. option(CUDA_HOST_COMPILATION_CPP "Generated file extension" ON)
  349. # Extra user settable flags
  350. set(CUDA_NVCC_FLAGS "" CACHE STRING "Semi-colon delimit multiple arguments.")
  351. # Propagate the host flags to the host compiler via -Xcompiler
  352. option(CUDA_PROPAGATE_HOST_FLAGS "Propage C/CXX_FLAGS and friends to the host compiler via -Xcompile" ON)
  353. # Specifies whether the commands used when compiling the .cu file will be printed out.
  354. option(CUDA_VERBOSE_BUILD "Print out the commands run while compiling the CUDA source file. With the Makefile generator this defaults to VERBOSE variable specified on the command line, but can be forced on with this option." OFF)
  355. mark_as_advanced(
  356. CUDA_64_BIT_DEVICE_CODE
  357. CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE
  358. CUDA_GENERATED_OUTPUT_DIR
  359. CUDA_HOST_COMPILATION_CPP
  360. CUDA_NVCC_FLAGS
  361. CUDA_PROPAGATE_HOST_FLAGS
  362. )
  363. # Makefile and similar generators don't define CMAKE_CONFIGURATION_TYPES, so we
  364. # need to add another entry for the CMAKE_BUILD_TYPE. We also need to add the
  365. # standerd set of 4 build types (Debug, MinSizeRel, Release, and RelWithDebInfo)
  366. # for completeness. We need run this loop in order to accomodate the addition
  367. # of extra configuration types. Duplicate entries will be removed by
  368. # REMOVE_DUPLICATES.
  369. set(CUDA_configuration_types ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} Debug MinSizeRel Release RelWithDebInfo)
  370. list(REMOVE_DUPLICATES CUDA_configuration_types)
  371. foreach(config ${CUDA_configuration_types})
  372. string(TOUPPER ${config} config_upper)
  373. set(CUDA_NVCC_FLAGS_${config_upper} "" CACHE STRING "Semi-colon delimit multiple arguments.")
  374. mark_as_advanced(CUDA_NVCC_FLAGS_${config_upper})
  375. endforeach()
  376. ###############################################################################
  377. ###############################################################################
  378. # Locate CUDA, Set Build Type, etc.
  379. ###############################################################################
  380. ###############################################################################
  381. # Check to see if the CUDA_TOOLKIT_ROOT_DIR and CUDA_SDK_ROOT_DIR have changed,
  382. # if they have then clear the cache variables, so that will be detected again.
  383. if(NOT "${CUDA_TOOLKIT_ROOT_DIR}" STREQUAL "${CUDA_TOOLKIT_ROOT_DIR_INTERNAL}")
  384. unset(CUDA_NVCC_EXECUTABLE CACHE)
  385. unset(CUDA_VERSION CACHE)
  386. unset(CUDA_TOOLKIT_INCLUDE CACHE)
  387. unset(CUDA_CUDART_LIBRARY CACHE)
  388. unset(CUDA_CUDA_LIBRARY CACHE)
  389. unset(CUDA_cublas_LIBRARY CACHE)
  390. unset(CUDA_cublasemu_LIBRARY CACHE)
  391. unset(CUDA_cufft_LIBRARY CACHE)
  392. unset(CUDA_cufftemu_LIBRARY CACHE)
  393. endif()
  394. if(NOT "${CUDA_SDK_ROOT_DIR}" STREQUAL "${CUDA_SDK_ROOT_DIR_INTERNAL}")
  395. unset(CUDA_CUT_INCLUDE_DIR CACHE)
  396. unset(CUDA_CUT_LIBRARY CACHE)
  397. endif()
  398. # Search for the cuda distribution.
  399. if(NOT CUDA_TOOLKIT_ROOT_DIR)
  400. # Search in the CUDA_BIN_PATH first.
  401. find_path(CUDA_TOOLKIT_ROOT_DIR
  402. NAMES nvcc nvcc.exe
  403. PATHS ENV CUDA_BIN_PATH
  404. DOC "Toolkit location."
  405. NO_DEFAULT_PATH
  406. )
  407. # Now search default paths
  408. find_path(CUDA_TOOLKIT_ROOT_DIR
  409. NAMES nvcc nvcc.exe
  410. PATHS /usr/local/bin
  411. /usr/local/cuda/bin
  412. DOC "Toolkit location."
  413. )
  414. if (CUDA_TOOLKIT_ROOT_DIR)
  415. string(REGEX REPLACE "[/\\\\]?bin[64]*[/\\\\]?$" "" CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR})
  416. # We need to force this back into the cache.
  417. set(CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR} CACHE PATH "Toolkit location." FORCE)
  418. endif(CUDA_TOOLKIT_ROOT_DIR)
  419. if (NOT EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
  420. if(CUDA_FIND_REQUIRED)
  421. message(FATAL_ERROR "Specify CUDA_TOOLKIT_ROOT_DIR")
  422. elseif(NOT CUDA_FIND_QUIETLY)
  423. message("CUDA_TOOLKIT_ROOT_DIR not found or specified")
  424. endif()
  425. endif (NOT EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
  426. endif (NOT CUDA_TOOLKIT_ROOT_DIR)
  427. # CUDA_NVCC_EXECUTABLE
  428. find_program(CUDA_NVCC_EXECUTABLE
  429. NAMES nvcc
  430. PATHS "${CUDA_TOOLKIT_ROOT_DIR}/bin64"
  431. "${CUDA_TOOLKIT_ROOT_DIR}/bin"
  432. ENV CUDA_BIN_PATH
  433. NO_DEFAULT_PATH
  434. )
  435. # Search default search paths, after we search our own set of paths.
  436. find_program(CUDA_NVCC_EXECUTABLE nvcc)
  437. mark_as_advanced(CUDA_NVCC_EXECUTABLE)
  438. if(CUDA_NVCC_EXECUTABLE AND NOT CUDA_VERSION)
  439. # Compute the version.
  440. exec_program(${CUDA_NVCC_EXECUTABLE} ARGS "--version" OUTPUT_VARIABLE NVCC_OUT)
  441. string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR ${NVCC_OUT})
  442. string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR ${NVCC_OUT})
  443. set(CUDA_VERSION "${CUDA_VERSION_MAJOR}.${CUDA_VERSION_MINOR}" CACHE STRING "Version of CUDA as computed from nvcc.")
  444. mark_as_advanced(CUDA_VERSION)
  445. endif()
  446. # Always set this convenience variable
  447. set(CUDA_VERSION_STRING "${CUDA_VERSION}")
  448. # Here we need to determine if the version we found is acceptable. We will
  449. # assume that is unless CUDA_FIND_VERSION_EXACT or CUDA_FIND_VERSION is
  450. # specified. The presence of either of these options checks the version
  451. # string and signals if the version is acceptable or not.
  452. set(_cuda_version_acceptable TRUE)
  453. #
  454. if(CUDA_FIND_VERSION_EXACT AND NOT CUDA_VERSION VERSION_EQUAL CUDA_FIND_VERSION)
  455. set(_cuda_version_acceptable FALSE)
  456. endif()
  457. #
  458. if(CUDA_FIND_VERSION AND CUDA_VERSION VERSION_LESS CUDA_FIND_VERSION)
  459. set(_cuda_version_acceptable FALSE)
  460. endif()
  461. #
  462. if(NOT _cuda_version_acceptable)
  463. set(_cuda_error_message "Requested CUDA version ${CUDA_FIND_VERSION}, but found unacceptable version ${CUDA_VERSION}")
  464. if(CUDA_FIND_REQUIRED)
  465. message("${_cuda_error_message}")
  466. elseif(NOT CUDA_FIND_QUIETLY)
  467. message("${_cuda_error_message}")
  468. endif()
  469. endif()
  470. # CUDA_TOOLKIT_INCLUDE
  471. find_path(CUDA_TOOLKIT_INCLUDE
  472. device_functions.h # Header included in toolkit
  473. PATHS "${CUDA_TOOLKIT_ROOT_DIR}/include"
  474. ENV CUDA_INC_PATH
  475. NO_DEFAULT_PATH
  476. )
  477. # Search default search paths, after we search our own set of paths.
  478. find_path(CUDA_TOOLKIT_INCLUDE device_functions.h)
  479. mark_as_advanced(CUDA_TOOLKIT_INCLUDE)
  480. # Set the user list of include dir to nothing to initialize it.
  481. set (CUDA_NVCC_INCLUDE_ARGS_USER "")
  482. set (CUDA_INCLUDE_DIRS ${CUDA_TOOLKIT_INCLUDE})
  483. macro(FIND_LIBRARY_LOCAL_FIRST _var _names _doc)
  484. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  485. set(_cuda_64bit_lib_dir "${CUDA_TOOLKIT_ROOT_DIR}/lib64")
  486. endif()
  487. find_library(${_var}
  488. NAMES ${_names}
  489. PATHS ${_cuda_64bit_lib_dir}
  490. "${CUDA_TOOLKIT_ROOT_DIR}/lib"
  491. ENV CUDA_LIB_PATH
  492. DOC ${_doc}
  493. NO_DEFAULT_PATH
  494. )
  495. # Search default search paths, after we search our own set of paths.
  496. find_library(${_var} NAMES ${_names} DOC ${_doc})
  497. endmacro()
  498. # CUDA_LIBRARIES
  499. find_library_local_first(CUDA_CUDART_LIBRARY cudart "\"cudart\" library")
  500. set(CUDA_LIBRARIES ${CUDA_CUDART_LIBRARY})
  501. # 1.1 toolkit on linux doesn't appear to have a separate library on
  502. # some platforms.
  503. find_library_local_first(CUDA_CUDA_LIBRARY cuda "\"cuda\" library (older versions only).")
  504. # Add cuda library to the link line only if it is found.
  505. if (CUDA_CUDA_LIBRARY)
  506. set(CUDA_LIBRARIES ${CUDA_LIBRARIES} ${CUDA_CUDA_LIBRARY})
  507. endif(CUDA_CUDA_LIBRARY)
  508. mark_as_advanced(
  509. CUDA_CUDA_LIBRARY
  510. CUDA_CUDART_LIBRARY
  511. )
  512. #######################
  513. # Look for some of the toolkit helper libraries
  514. macro(FIND_CUDA_HELPER_LIBS _name)
  515. find_library_local_first(CUDA_${_name}_LIBRARY ${_name} "\"${_name}\" library")
  516. mark_as_advanced(CUDA_${_name}_LIBRARY)
  517. endmacro(FIND_CUDA_HELPER_LIBS)
  518. # Search for cufft and cublas libraries.
  519. find_cuda_helper_libs(cufftemu)
  520. find_cuda_helper_libs(cublasemu)
  521. find_cuda_helper_libs(cufft)
  522. find_cuda_helper_libs(cublas)
  523. if (CUDA_BUILD_EMULATION)
  524. set(CUDA_CUFFT_LIBRARIES ${CUDA_cufftemu_LIBRARY})
  525. set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublasemu_LIBRARY})
  526. else()
  527. set(CUDA_CUFFT_LIBRARIES ${CUDA_cufft_LIBRARY})
  528. set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublas_LIBRARY})
  529. endif()
  530. ########################
  531. # Look for the SDK stuff
  532. find_path(CUDA_SDK_ROOT_DIR common/inc/cutil.h
  533. "$ENV{NVSDKCUDA_ROOT}"
  534. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Installed Products\\NVIDIA SDK 10\\Compute;InstallDir]"
  535. )
  536. # Keep the CUDA_SDK_ROOT_DIR first in order to be able to override the
  537. # environment variables.
  538. set(CUDA_SDK_SEARCH_PATH
  539. "${CUDA_SDK_ROOT_DIR}"
  540. "${CUDA_TOOLKIT_ROOT_DIR}/local/NVSDK0.2"
  541. "${CUDA_TOOLKIT_ROOT_DIR}/NVSDK0.2"
  542. "${CUDA_TOOLKIT_ROOT_DIR}/NV_CUDA_SDK"
  543. "$ENV{HOME}/NVIDIA_CUDA_SDK"
  544. "$ENV{HOME}/NVIDIA_CUDA_SDK_MACOSX"
  545. "/Developer/CUDA"
  546. )
  547. # CUDA_CUT_INCLUDE_DIR
  548. find_path(CUDA_CUT_INCLUDE_DIR
  549. cutil.h
  550. PATHS ${CUDA_SDK_SEARCH_PATH}
  551. PATH_SUFFIXES "common/inc"
  552. DOC "Location of cutil.h"
  553. NO_DEFAULT_PATH
  554. )
  555. # Now search system paths
  556. find_path(CUDA_CUT_INCLUDE_DIR cutil.h DOC "Location of cutil.h")
  557. mark_as_advanced(CUDA_CUT_INCLUDE_DIR)
  558. # CUDA_CUT_LIBRARIES
  559. # cutil library is called cutil64 for 64 bit builds on windows. We don't want
  560. # to get these confused, so we are setting the name based on the word size of
  561. # the build.
  562. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  563. set(cuda_cutil_name cutil64)
  564. else(CMAKE_SIZEOF_VOID_P EQUAL 8)
  565. set(cuda_cutil_name cutil32)
  566. endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
  567. find_library(CUDA_CUT_LIBRARY
  568. NAMES cutil ${cuda_cutil_name}
  569. PATHS ${CUDA_SDK_SEARCH_PATH}
  570. # The new version of the sdk shows up in common/lib, but the old one is in lib
  571. PATH_SUFFIXES "common/lib" "lib"
  572. DOC "Location of cutil library"
  573. NO_DEFAULT_PATH
  574. )
  575. # Now search system paths
  576. find_library(CUDA_CUT_LIBRARY NAMES cutil ${cuda_cutil_name} DOC "Location of cutil library")
  577. mark_as_advanced(CUDA_CUT_LIBRARY)
  578. set(CUDA_CUT_LIBRARIES ${CUDA_CUT_LIBRARY})
  579. #############################
  580. # Check for required components
  581. set(CUDA_FOUND TRUE)
  582. set(CUDA_TOOLKIT_ROOT_DIR_INTERNAL "${CUDA_TOOLKIT_ROOT_DIR}" CACHE INTERNAL
  583. "This is the value of the last time CUDA_TOOLKIT_ROOT_DIR was set successfully." FORCE)
  584. set(CUDA_SDK_ROOT_DIR_INTERNAL "${CUDA_SDK_ROOT_DIR}" CACHE INTERNAL
  585. "This is the value of the last time CUDA_SDK_ROOT_DIR was set successfully." FORCE)
  586. include(FindPackageHandleStandardArgs)
  587. find_package_handle_standard_args(CUDA DEFAULT_MSG
  588. CUDA_TOOLKIT_ROOT_DIR
  589. CUDA_NVCC_EXECUTABLE
  590. CUDA_INCLUDE_DIRS
  591. CUDA_CUDART_LIBRARY
  592. _cuda_version_acceptable
  593. )
  594. ###############################################################################
  595. ###############################################################################
  596. # Macros
  597. ###############################################################################
  598. ###############################################################################
  599. ###############################################################################
  600. # Add include directories to pass to the nvcc command.
  601. macro(CUDA_INCLUDE_DIRECTORIES)
  602. foreach(dir ${ARGN})
  603. list(APPEND CUDA_NVCC_INCLUDE_ARGS_USER "-I${dir}")
  604. endforeach(dir ${ARGN})
  605. endmacro(CUDA_INCLUDE_DIRECTORIES)
  606. ##############################################################################
  607. cuda_find_helper_file(parse_cubin cmake)
  608. cuda_find_helper_file(make2cmake cmake)
  609. cuda_find_helper_file(run_nvcc cmake)
  610. ##############################################################################
  611. # Separate the OPTIONS out from the sources
  612. #
  613. macro(CUDA_GET_SOURCES_AND_OPTIONS _sources _cmake_options _options)
  614. set( ${_sources} )
  615. set( ${_cmake_options} )
  616. set( ${_options} )
  617. set( _found_options FALSE )
  618. foreach(arg ${ARGN})
  619. if(arg STREQUAL "OPTIONS")
  620. set( _found_options TRUE )
  621. elseif(
  622. arg STREQUAL "WIN32" OR
  623. arg STREQUAL "MACOSX_BUNDLE" OR
  624. arg STREQUAL "EXCLUDE_FROM_ALL" OR
  625. arg STREQUAL "STATIC" OR
  626. arg STREQUAL "SHARED" OR
  627. arg STREQUAL "MODULE"
  628. )
  629. list(APPEND ${_cmake_options} "${arg}")
  630. else()
  631. if ( _found_options )
  632. list(APPEND ${_options} "${arg}")
  633. else()
  634. # Assume this is a file
  635. list(APPEND ${_sources} "${arg}")
  636. endif()
  637. endif()
  638. endforeach()
  639. endmacro()
  640. ##############################################################################
  641. # Parse the OPTIONS from ARGN and set the variables prefixed by _option_prefix
  642. #
  643. macro(CUDA_PARSE_NVCC_OPTIONS _option_prefix)
  644. set( _found_config )
  645. foreach(arg ${ARGN})
  646. # Determine if we are dealing with a perconfiguration flag
  647. foreach(config ${CUDA_configuration_types})
  648. string(TOUPPER ${config} config_upper)
  649. if (arg STREQUAL "${config_upper}")
  650. set( _found_config _${arg})
  651. # Set arg to nothing to keep it from being processed further
  652. set( arg )
  653. endif()
  654. endforeach()
  655. if ( arg )
  656. list(APPEND ${_option_prefix}${_found_config} "${arg}")
  657. endif()
  658. endforeach()
  659. endmacro()
  660. ##############################################################################
  661. # Helper to add the include directory for CUDA only once
  662. function(CUDA_ADD_CUDA_INCLUDE_ONCE)
  663. get_directory_property(_include_directories INCLUDE_DIRECTORIES)
  664. set(_add TRUE)
  665. if(_include_directories)
  666. foreach(dir ${_include_directories})
  667. if("${dir}" STREQUAL "${CUDA_INCLUDE_DIRS}")
  668. set(_add FALSE)
  669. endif()
  670. endforeach()
  671. endif()
  672. if(_add)
  673. include_directories(${CUDA_INCLUDE_DIRS})
  674. endif()
  675. endfunction()
  676. ##############################################################################
  677. # This helper macro populates the following variables and setups up custom
  678. # commands and targets to invoke the nvcc compiler to generate C or PTX source
  679. # dependant upon the format parameter. The compiler is invoked once with -M
  680. # to generate a dependency file and a second time with -cuda or -ptx to generate
  681. # a .cpp or .ptx file.
  682. # INPUT:
  683. # cuda_target - Target name
  684. # format - PTX or OBJ
  685. # FILE1 .. FILEN - The remaining arguments are the sources to be wrapped.
  686. # OPTIONS - Extra options to NVCC
  687. # OUTPUT:
  688. # generated_files - List of generated files
  689. ##############################################################################
  690. ##############################################################################
  691. macro(CUDA_WRAP_SRCS cuda_target format generated_files)
  692. if( ${format} MATCHES "PTX" )
  693. set( compile_to_ptx ON )
  694. elseif( ${format} MATCHES "OBJ")
  695. set( compile_to_ptx OFF )
  696. else()
  697. message( FATAL_ERROR "Invalid format flag passed to CUDA_WRAP_SRCS: '${format}'. Use OBJ or PTX.")
  698. endif()
  699. # Set up all the command line flags here, so that they can be overriden on a per target basis.
  700. set(nvcc_flags "")
  701. # Emulation if the card isn't present.
  702. if (CUDA_BUILD_EMULATION)
  703. # Emulation.
  704. set(nvcc_flags ${nvcc_flags} --device-emulation -D_DEVICEEMU -g)
  705. else(CUDA_BUILD_EMULATION)
  706. # Device mode. No flags necessary.
  707. endif(CUDA_BUILD_EMULATION)
  708. if(CUDA_HOST_COMPILATION_CPP)
  709. set(CUDA_C_OR_CXX CXX)
  710. else(CUDA_HOST_COMPILATION_CPP)
  711. set(nvcc_flags ${nvcc_flags} --host-compilation C)
  712. set(CUDA_C_OR_CXX C)
  713. endif(CUDA_HOST_COMPILATION_CPP)
  714. set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
  715. if(CUDA_64_BIT_DEVICE_CODE)
  716. set(nvcc_flags ${nvcc_flags} -m64)
  717. else()
  718. set(nvcc_flags ${nvcc_flags} -m32)
  719. endif()
  720. # This needs to be passed in at this stage, because VS needs to fill out the
  721. # value of VCInstallDir from within VS.
  722. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  723. if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
  724. # Add nvcc flag for 64b Windows
  725. set(ccbin_flags -D "\"CCBIN:PATH=$(VCInstallDir)bin\"" )
  726. endif()
  727. endif()
  728. # Figure out which configure we will use and pass that in as an argument to
  729. # the script. We need to defer the decision until compilation time, because
  730. # for VS projects we won't know if we are making a debug or release build
  731. # until build time.
  732. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  733. set( CUDA_build_configuration "$(ConfigurationName)" )
  734. else()
  735. set( CUDA_build_configuration "${CMAKE_BUILD_TYPE}")
  736. endif()
  737. # Initialize our list of includes with the user ones followed by the CUDA system ones.
  738. set(CUDA_NVCC_INCLUDE_ARGS ${CUDA_NVCC_INCLUDE_ARGS_USER} "-I${CUDA_INCLUDE_DIRS}")
  739. # Get the include directories for this directory and use them for our nvcc command.
  740. get_directory_property(CUDA_NVCC_INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES)
  741. if(CUDA_NVCC_INCLUDE_DIRECTORIES)
  742. foreach(dir ${CUDA_NVCC_INCLUDE_DIRECTORIES})
  743. list(APPEND CUDA_NVCC_INCLUDE_ARGS "-I${dir}")
  744. endforeach()
  745. endif()
  746. # Reset these variables
  747. set(CUDA_WRAP_OPTION_NVCC_FLAGS)
  748. foreach(config ${CUDA_configuration_types})
  749. string(TOUPPER ${config} config_upper)
  750. set(CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper})
  751. endforeach()
  752. CUDA_GET_SOURCES_AND_OPTIONS(_cuda_wrap_sources _cuda_wrap_cmake_options _cuda_wrap_options ${ARGN})
  753. CUDA_PARSE_NVCC_OPTIONS(CUDA_WRAP_OPTION_NVCC_FLAGS ${_cuda_wrap_options})
  754. # Figure out if we are building a shared library. Default the value of BUILD_SHARED_LIBS.
  755. set(_cuda_build_shared_libs ${BUILD_SHARED_LIBS})
  756. # SHARED, MODULE
  757. list(FIND _cuda_wrap_cmake_options SHARED _cuda_found_SHARED)
  758. list(FIND _cuda_wrap_cmake_options MODULE _cuda_found_MODULE)
  759. if(_cuda_found_SHARED GREATER -1 OR _cuda_found_MODULE GREATER -1)
  760. set(_cuda_build_shared_libs TRUE)
  761. endif()
  762. # STATIC
  763. list(FIND _cuda_wrap_cmake_options STATIC _cuda_found_STATIC)
  764. if(_cuda_found_STATIC GREATER -1)
  765. set(_cuda_build_shared_libs FALSE)
  766. endif()
  767. # CUDA_HOST_FLAGS
  768. if(_cuda_build_shared_libs)
  769. # If we are setting up code for a shared library, then we need to add extra flags for
  770. # compiling objects for shared libraries.
  771. set(CUDA_HOST_SHARED_FLAGS ${CMAKE_SHARED_LIBRARY_${CUDA_C_OR_CXX}_FLAGS})
  772. endif()
  773. # Only add the CMAKE_{C,CXX}_FLAGS if we are propagating host flags. We
  774. # always need to set the SHARED_FLAGS, though.
  775. if(NOT CUDA_PROPAGATE_HOST_FLAGS)
  776. set(CUDA_HOST_FLAGS "set(CMAKE_HOST_FLAGS ${CUDA_HOST_SHARED_FLAGS})")
  777. else()
  778. set(CUDA_HOST_FLAGS "set(CMAKE_HOST_FLAGS ${CMAKE_${CUDA_C_OR_CXX}_FLAGS} ${CUDA_HOST_SHARED_FLAGS})")
  779. set(CUDA_NVCC_FLAGS_CONFIG "# Build specific configuration flags")
  780. # Loop over all the configuration types to generate appropriate flags for run_nvcc.cmake
  781. foreach(config ${CUDA_configuration_types})
  782. string(TOUPPER ${config} config_upper)
  783. # CMAKE_FLAGS are strings and not lists. By not putting quotes around CMAKE_FLAGS
  784. # we convert the strings to lists (like we want).
  785. # nvcc chokes on -g3, so replace it with -g
  786. if(CMAKE_COMPILER_IS_GNUCC)
  787. string(REPLACE "-g3" "-g" _cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
  788. else()
  789. set(_cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
  790. endif()
  791. set(CUDA_HOST_FLAGS "${CUDA_HOST_FLAGS}\nset(CMAKE_HOST_FLAGS_${config_upper} ${_cuda_C_FLAGS})")
  792. # Note that if we ever want CUDA_NVCC_FLAGS_<CONFIG> to be string (instead of a list
  793. # like it is currently), we can remove the quotes around the
  794. # ${CUDA_NVCC_FLAGS_${config_upper}} variable like the CMAKE_HOST_FLAGS_<CONFIG> variable.
  795. set(CUDA_NVCC_FLAGS_CONFIG "${CUDA_NVCC_FLAGS_CONFIG}\nset(CUDA_NVCC_FLAGS_${config_upper} \"${CUDA_NVCC_FLAGS_${config_upper}};${CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper}}\")")
  796. endforeach()
  797. endif()
  798. if(compile_to_ptx)
  799. # Don't use any of the host compilation flags for PTX targets.
  800. set(CUDA_HOST_FLAGS)
  801. set(CUDA_NVCC_FLAGS_CONFIG)
  802. endif()
  803. # Get the list of definitions from the directory property
  804. get_directory_property(CUDA_NVCC_DEFINITIONS COMPILE_DEFINITIONS)
  805. if(CUDA_NVCC_DEFINITIONS)
  806. foreach(_definition ${CUDA_NVCC_DEFINITIONS})
  807. list(APPEND nvcc_flags "-D${_definition}")
  808. endforeach()
  809. endif()
  810. if(_cuda_build_shared_libs)
  811. list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS")
  812. endif()
  813. # Determine output directory
  814. if(CUDA_GENERATED_OUTPUT_DIR)
  815. set(cuda_compile_output_dir "${CUDA_GENERATED_OUTPUT_DIR}")
  816. else()
  817. set(cuda_compile_output_dir "${CMAKE_CURRENT_BINARY_DIR}")
  818. endif()
  819. # Reset the output variable
  820. set(_cuda_wrap_generated_files "")
  821. # Iterate over the macro arguments and create custom
  822. # commands for all the .cu files.
  823. foreach(file ${ARGN})
  824. # Ignore any file marked as a HEADER_FILE_ONLY
  825. get_source_file_property(_is_header ${file} HEADER_FILE_ONLY)
  826. if(${file} MATCHES ".*\\.cu$" AND NOT _is_header)
  827. # Add a custom target to generate a c or ptx file. ######################
  828. get_filename_component( basename ${file} NAME )
  829. if( compile_to_ptx )
  830. set(generated_file_path "${cuda_compile_output_dir}")
  831. set(generated_file_basename "${cuda_target}_generated_${basename}.ptx")
  832. set(format_flag "-ptx")
  833. file(MAKE_DIRECTORY "${cuda_compile_output_dir}")
  834. else( compile_to_ptx )
  835. set(generated_file_path "${cuda_compile_output_dir}/${CMAKE_CFG_INTDIR}")
  836. set(generated_file_basename "${cuda_target}_generated_${basename}${generated_extension}")
  837. set(format_flag "-c")
  838. endif( compile_to_ptx )
  839. # Set all of our file names. Make sure that whatever filenames that have
  840. # generated_file_path in them get passed in through as a command line
  841. # argument, so that the ${CMAKE_CFG_INTDIR} gets expanded at run time
  842. # instead of configure time.
  843. set(generated_file "${generated_file_path}/${generated_file_basename}")
  844. set(cmake_dependency_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.depend")
  845. set(NVCC_generated_dependency_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.NVCC-depend")
  846. set(generated_cubin_file "${generated_file_path}/${generated_file_basename}.cubin.txt")
  847. set(custom_target_script "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.cmake")
  848. # Setup properties for obj files:
  849. if( NOT compile_to_ptx )
  850. set_source_files_properties("${generated_file}"
  851. PROPERTIES
  852. EXTERNAL_OBJECT true # This is an object file not to be compiled, but only be linked.
  853. )
  854. endif()
  855. # Don't add CMAKE_CURRENT_SOURCE_DIR if the path is already an absolute path.
  856. get_filename_component(file_path "${file}" PATH)
  857. if(IS_ABSOLUTE "${file_path}")
  858. set(source_file "${file}")
  859. else()
  860. set(source_file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
  861. endif()
  862. # Bring in the dependencies. Creates a variable CUDA_NVCC_DEPEND #######
  863. cuda_include_nvcc_dependencies(${cmake_dependency_file})
  864. # Convience string for output ###########################################
  865. if(CUDA_BUILD_EMULATION)
  866. set(cuda_build_type "Emulation")
  867. else(CUDA_BUILD_EMULATION)
  868. set(cuda_build_type "Device")
  869. endif(CUDA_BUILD_EMULATION)
  870. # Build the NVCC made dependency file ###################################
  871. set(build_cubin OFF)
  872. if ( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
  873. if ( NOT compile_to_ptx )
  874. set ( build_cubin ON )
  875. endif( NOT compile_to_ptx )
  876. endif( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
  877. # Configure the build script
  878. configure_file("${CUDA_run_nvcc}" "${custom_target_script}" @ONLY)
  879. # So if a user specifies the same cuda file as input more than once, you
  880. # can have bad things happen with dependencies. Here we check an option
  881. # to see if this is the behavior they want.
  882. if(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
  883. set(main_dep MAIN_DEPENDENCY ${source_file})
  884. else()
  885. set(main_dep DEPENDS ${source_file})
  886. endif()
  887. if(CUDA_VERBOSE_BUILD)
  888. set(verbose_output ON)
  889. elseif(CMAKE_GENERATOR MATCHES "Makefiles")
  890. set(verbose_output "$(VERBOSE)")
  891. else()
  892. set(verbose_output OFF)
  893. endif()
  894. # Create up the comment string
  895. file(RELATIVE_PATH generated_file_relative_path "${CMAKE_BINARY_DIR}" "${generated_file}")
  896. if(compile_to_ptx)
  897. set(cuda_build_comment_string "Building NVCC ptx file ${generated_file_relative_path}")
  898. else()
  899. set(cuda_build_comment_string "Building NVCC (${cuda_build_type}) object ${generated_file_relative_path}")
  900. endif()
  901. # Build the generated file and dependency file ##########################
  902. add_custom_command(
  903. OUTPUT ${generated_file}
  904. # These output files depend on the source_file and the contents of cmake_dependency_file
  905. ${main_dep}
  906. DEPENDS ${CUDA_NVCC_DEPEND}
  907. DEPENDS ${custom_target_script}
  908. COMMAND ${CMAKE_COMMAND} ARGS
  909. -D verbose:BOOL=${verbose_output}
  910. ${ccbin_flags}
  911. -D build_configuration:STRING=${CUDA_build_configuration}
  912. -D "generated_file:STRING=${generated_file}"
  913. -D "generated_cubin_file:STRING=${generated_cubin_file}"
  914. -P "${custom_target_script}"
  915. COMMENT "${cuda_build_comment_string}"
  916. )
  917. # Make sure the build system knows the file is generated.
  918. set_source_files_properties(${generated_file} PROPERTIES GENERATED TRUE)
  919. # Don't add the object file to the list of generated files if we are using
  920. # visual studio and we are attaching the build rule to the cuda file. VS
  921. # will add our object file to the linker automatically for us.
  922. set(cuda_add_generated_file TRUE)
  923. if(NOT compile_to_ptx AND CMAKE_GENERATOR MATCHES "Visual Studio" AND CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
  924. # Visual Studio 8 crashes when you close the solution when you don't add the object file.
  925. if(NOT CMAKE_GENERATOR MATCHES "Visual Studio 8")
  926. #message("Not adding ${generated_file}")
  927. set(cuda_add_generated_file FALSE)
  928. endif()
  929. endif()
  930. if(cuda_add_generated_file)
  931. list(APPEND _cuda_wrap_generated_files ${generated_file})
  932. endif()
  933. # Add the other files that we want cmake to clean on a cleanup ##########
  934. list(APPEND CUDA_ADDITIONAL_CLEAN_FILES "${cmake_dependency_file}")
  935. list(REMOVE_DUPLICATES CUDA_ADDITIONAL_CLEAN_FILES)
  936. set(CUDA_ADDITIONAL_CLEAN_FILES ${CUDA_ADDITIONAL_CLEAN_FILES} CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
  937. endif(${file} MATCHES ".*\\.cu$" AND NOT _is_header)
  938. endforeach(file)
  939. # Set the return parameter
  940. set(${generated_files} ${_cuda_wrap_generated_files})
  941. endmacro(CUDA_WRAP_SRCS)
  942. ###############################################################################
  943. ###############################################################################
  944. # ADD LIBRARY
  945. ###############################################################################
  946. ###############################################################################
  947. macro(CUDA_ADD_LIBRARY cuda_target)
  948. CUDA_ADD_CUDA_INCLUDE_ONCE()
  949. # Separate the sources from the options
  950. CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
  951. # Create custom commands and targets for each file.
  952. CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources} ${_cmake_options}
  953. OPTIONS ${_options} )
  954. # Add the library.
  955. add_library(${cuda_target} ${_cmake_options}
  956. ${_generated_files}
  957. ${_sources}
  958. )
  959. target_link_libraries(${cuda_target}
  960. ${CUDA_LIBRARIES}
  961. )
  962. # We need to set the linker language based on what the expected generated file
  963. # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
  964. set_target_properties(${cuda_target}
  965. PROPERTIES
  966. LINKER_LANGUAGE ${CUDA_C_OR_CXX}
  967. )
  968. endmacro(CUDA_ADD_LIBRARY cuda_target)
  969. ###############################################################################
  970. ###############################################################################
  971. # ADD EXECUTABLE
  972. ###############################################################################
  973. ###############################################################################
  974. macro(CUDA_ADD_EXECUTABLE cuda_target)
  975. CUDA_ADD_CUDA_INCLUDE_ONCE()
  976. # Separate the sources from the options
  977. CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
  978. # Create custom commands and targets for each file.
  979. CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources} OPTIONS ${_options} )
  980. # Add the library.
  981. add_executable(${cuda_target} ${_cmake_options}
  982. ${_generated_files}
  983. ${_sources}
  984. )
  985. target_link_libraries(${cuda_target}
  986. ${CUDA_LIBRARIES}
  987. )
  988. # We need to set the linker language based on what the expected generated file
  989. # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
  990. set_target_properties(${cuda_target}
  991. PROPERTIES
  992. LINKER_LANGUAGE ${CUDA_C_OR_CXX}
  993. )
  994. endmacro(CUDA_ADD_EXECUTABLE cuda_target)
  995. ###############################################################################
  996. ###############################################################################
  997. # CUDA COMPILE
  998. ###############################################################################
  999. ###############################################################################
  1000. macro(CUDA_COMPILE generated_files)
  1001. # Separate the sources from the options
  1002. CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
  1003. # Create custom commands and targets for each file.
  1004. CUDA_WRAP_SRCS( cuda_compile OBJ _generated_files ${_sources} ${_cmake_options}
  1005. OPTIONS ${_options} )
  1006. set( ${generated_files} ${_generated_files})
  1007. endmacro(CUDA_COMPILE)
  1008. ###############################################################################
  1009. ###############################################################################
  1010. # CUDA COMPILE PTX
  1011. ###############################################################################
  1012. ###############################################################################
  1013. macro(CUDA_COMPILE_PTX generated_files)
  1014. # Separate the sources from the options
  1015. CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
  1016. # Create custom commands and targets for each file.
  1017. CUDA_WRAP_SRCS( cuda_compile_ptx PTX _generated_files ${_sources} ${_cmake_options}
  1018. OPTIONS ${_options} )
  1019. set( ${generated_files} ${_generated_files})
  1020. endmacro(CUDA_COMPILE_PTX)
  1021. ###############################################################################
  1022. ###############################################################################
  1023. # CUDA ADD CUFFT TO TARGET
  1024. ###############################################################################
  1025. ###############################################################################
  1026. macro(CUDA_ADD_CUFFT_TO_TARGET target)
  1027. if (CUDA_BUILD_EMULATION)
  1028. target_link_libraries(${target} ${CUDA_cufftemu_LIBRARY})
  1029. else()
  1030. target_link_libraries(${target} ${CUDA_cufft_LIBRARY})
  1031. endif()
  1032. endmacro()
  1033. ###############################################################################
  1034. ###############################################################################
  1035. # CUDA ADD CUBLAS TO TARGET
  1036. ###############################################################################
  1037. ###############################################################################
  1038. macro(CUDA_ADD_CUBLAS_TO_TARGET target)
  1039. if (CUDA_BUILD_EMULATION)
  1040. target_link_libraries(${target} ${CUDA_cublasemu_LIBRARY})
  1041. else()
  1042. target_link_libraries(${target} ${CUDA_cublas_LIBRARY})
  1043. endif()
  1044. endmacro()
  1045. ###############################################################################
  1046. ###############################################################################
  1047. # CUDA BUILD CLEAN TARGET
  1048. ###############################################################################
  1049. ###############################################################################
  1050. macro(CUDA_BUILD_CLEAN_TARGET)
  1051. # Call this after you add all your CUDA targets, and you will get a convience
  1052. # target. You should also make clean after running this target to get the
  1053. # build system to generate all the code again.
  1054. set(cuda_clean_target_name clean_cuda_depends)
  1055. if (CMAKE_GENERATOR MATCHES "Visual Studio")
  1056. string(TOUPPER ${cuda_clean_target_name} cuda_clean_target_name)
  1057. endif()
  1058. add_custom_target(${cuda_clean_target_name}
  1059. COMMAND ${CMAKE_COMMAND} -E remove ${CUDA_ADDITIONAL_CLEAN_FILES})
  1060. # Clear out the variable, so the next time we configure it will be empty.
  1061. # This is useful so that the files won't persist in the list after targets
  1062. # have been removed.
  1063. set(CUDA_ADDITIONAL_CLEAN_FILES "" CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
  1064. endmacro(CUDA_BUILD_CLEAN_TARGET)