FindCUDA.cmake 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. #[=======================================================================[.rst:
  2. FindCUDA
  3. --------
  4. .. deprecated:: 3.10
  5. Superseded by first-class support for the CUDA language in CMake.
  6. Superseded by the :module:`FindCUDAToolkit` for CUDA toolkit libraries.
  7. Replacement
  8. ^^^^^^^^^^^
  9. It is no longer necessary to use this module or call ``find_package(CUDA)``
  10. for compiling CUDA code. Instead, list ``CUDA`` among the languages named
  11. in the top-level call to the :command:`project` command, or call the
  12. :command:`enable_language` command with ``CUDA``.
  13. Then one can add CUDA (``.cu``) sources to programs directly
  14. in calls to :command:`add_library` and :command:`add_executable`.
  15. To find and use the CUDA toolkit libraries the :module:`FindCUDAToolkit`
  16. module has superseded this module. It works whether or not the ``CUDA``
  17. language is enabled.
  18. Documentation of Deprecated Usage
  19. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  20. Tools for building CUDA C files: libraries and build dependencies.
  21. This script locates the NVIDIA CUDA C tools. It should work on Linux,
  22. Windows, and macOS and should be reasonably up to date with CUDA C
  23. releases.
  24. This script makes use of the standard :command:`find_package` arguments of
  25. ``<VERSION>``, ``REQUIRED`` and ``QUIET``. ``CUDA_FOUND`` will report if an
  26. acceptable version of CUDA was found.
  27. The script will prompt the user to specify ``CUDA_TOOLKIT_ROOT_DIR`` if
  28. the prefix cannot be determined by the location of nvcc in the system
  29. path and ``REQUIRED`` is specified to :command:`find_package`. To use
  30. a different installed version of the toolkit set the environment variable
  31. ``CUDA_BIN_PATH`` before running cmake (e.g.
  32. ``CUDA_BIN_PATH=/usr/local/cuda1.0`` instead of the default
  33. ``/usr/local/cuda``) or set ``CUDA_TOOLKIT_ROOT_DIR`` after configuring. If
  34. you change the value of ``CUDA_TOOLKIT_ROOT_DIR``, various components that
  35. depend on the path will be relocated.
  36. It might be necessary to set ``CUDA_TOOLKIT_ROOT_DIR`` manually on certain
  37. platforms, or to use a CUDA runtime not installed in the default
  38. location. In newer versions of the toolkit the CUDA library is
  39. included with the graphics driver -- be sure that the driver version
  40. matches what is needed by the CUDA runtime version.
  41. The following variables affect the behavior of the macros in the
  42. script (in alphabetical order). Note that any of these flags can be
  43. changed multiple times in the same directory before calling
  44. ``CUDA_ADD_EXECUTABLE``, ``CUDA_ADD_LIBRARY``, ``CUDA_COMPILE``,
  45. ``CUDA_COMPILE_PTX``, ``CUDA_COMPILE_FATBIN``, ``CUDA_COMPILE_CUBIN``
  46. or ``CUDA_WRAP_SRCS``::
  47. CUDA_64_BIT_DEVICE_CODE (Default matches host bit size)
  48. -- Set to ON to compile for 64 bit device code, OFF for 32 bit device code.
  49. Note that making this different from the host code when generating object
  50. or C files from CUDA code just won't work, because size_t gets defined by
  51. nvcc in the generated source. If you compile to PTX and then load the
  52. file yourself, you can mix bit sizes between device and host.
  53. CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE (Default ON)
  54. -- Set to ON if you want the custom build rule to be attached to the source
  55. file in Visual Studio. Turn OFF if you add the same cuda file to multiple
  56. targets.
  57. This allows the user to build the target from the CUDA file; however, bad
  58. things can happen if the CUDA source file is added to multiple targets.
  59. When performing parallel builds it is possible for the custom build
  60. command to be run more than once and in parallel causing cryptic build
  61. errors. VS runs the rules for every source file in the target, and a
  62. source can have only one rule no matter how many projects it is added to.
  63. When the rule is run from multiple targets race conditions can occur on
  64. the generated file. Eventually everything will get built, but if the user
  65. is unaware of this behavior, there may be confusion. It would be nice if
  66. this script could detect the reuse of source files across multiple targets
  67. and turn the option off for the user, but no good solution could be found.
  68. CUDA_BUILD_CUBIN (Default OFF)
  69. -- Set to ON to enable and extra compilation pass with the -cubin option in
  70. Device mode. The output is parsed and register, shared memory usage is
  71. printed during build.
  72. CUDA_BUILD_EMULATION (Default OFF for device mode)
  73. -- Set to ON for Emulation mode. -D_DEVICEEMU is defined for CUDA C files
  74. when CUDA_BUILD_EMULATION is TRUE.
  75. CUDA_LINK_LIBRARIES_KEYWORD (Default "")
  76. -- The <PRIVATE|PUBLIC|INTERFACE> keyword to use for internal
  77. target_link_libraries calls. The default is to use no keyword which
  78. uses the old "plain" form of target_link_libraries. Note that is matters
  79. because whatever is used inside the FindCUDA module must also be used
  80. outside - the two forms of target_link_libraries cannot be mixed.
  81. CUDA_GENERATED_OUTPUT_DIR (Default CMAKE_CURRENT_BINARY_DIR)
  82. -- Set to the path you wish to have the generated files placed. If it is
  83. blank output files will be placed in CMAKE_CURRENT_BINARY_DIR.
  84. Intermediate files will always be placed in
  85. CMAKE_CURRENT_BINARY_DIR/CMakeFiles.
  86. CUDA_HOST_COMPILATION_CPP (Default ON)
  87. -- Set to OFF for C compilation of host code.
  88. CUDA_HOST_COMPILER (Default CMAKE_C_COMPILER)
  89. -- Set the host compiler to be used by nvcc. Ignored if -ccbin or
  90. --compiler-bindir is already present in the CUDA_NVCC_FLAGS or
  91. CUDA_NVCC_FLAGS_<CONFIG> variables. For Visual Studio targets,
  92. the host compiler is constructed with one or more visual studio macros
  93. such as $(VCInstallDir), that expands out to the path when
  94. the command is run from within VS.
  95. If the CUDAHOSTCXX environment variable is set it will
  96. be used as the default.
  97. CUDA_NVCC_FLAGS
  98. CUDA_NVCC_FLAGS_<CONFIG>
  99. -- Additional NVCC command line arguments. NOTE: multiple arguments must be
  100. semi-colon delimited (e.g. --compiler-options;-Wall)
  101. CUDA_PROPAGATE_HOST_FLAGS (Default ON)
  102. -- Set to ON to propagate CMAKE_{C,CXX}_FLAGS and their configuration
  103. dependent counterparts (e.g. CMAKE_C_FLAGS_DEBUG) automatically to the
  104. host compiler through nvcc's -Xcompiler flag. This helps make the
  105. generated host code match the rest of the system better. Sometimes
  106. certain flags give nvcc problems, and this will help you turn the flag
  107. propagation off. This does not affect the flags supplied directly to nvcc
  108. via CUDA_NVCC_FLAGS or through the OPTION flags specified through
  109. CUDA_ADD_LIBRARY, CUDA_ADD_EXECUTABLE, or CUDA_WRAP_SRCS. Flags used for
  110. shared library compilation are not affected by this flag.
  111. CUDA_SEPARABLE_COMPILATION (Default OFF)
  112. -- If set this will enable separable compilation for all CUDA runtime object
  113. files. If used outside of CUDA_ADD_EXECUTABLE and CUDA_ADD_LIBRARY
  114. (e.g. calling CUDA_WRAP_SRCS directly),
  115. CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME and
  116. CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS should be called.
  117. CUDA_SOURCE_PROPERTY_FORMAT
  118. -- If this source file property is set, it can override the format specified
  119. to CUDA_WRAP_SRCS (OBJ, PTX, CUBIN, or FATBIN). If an input source file
  120. is not a .cu file, setting this file will cause it to be treated as a .cu
  121. file. See documentation for set_source_files_properties on how to set
  122. this property.
  123. CUDA_USE_STATIC_CUDA_RUNTIME (Default ON)
  124. -- When enabled the static version of the CUDA runtime library will be used
  125. in CUDA_LIBRARIES. If the version of CUDA configured doesn't support
  126. this option, then it will be silently disabled.
  127. CUDA_VERBOSE_BUILD (Default OFF)
  128. -- Set to ON to see all the commands used when building the CUDA file. When
  129. using a Makefile generator the value defaults to VERBOSE (run make
  130. VERBOSE=1 to see output), although setting CUDA_VERBOSE_BUILD to ON will
  131. always print the output.
  132. The script creates the following macros (in alphabetical order)::
  133. CUDA_ADD_CUFFT_TO_TARGET( cuda_target )
  134. -- Adds the cufft library to the target (can be any target). Handles whether
  135. you are in emulation mode or not.
  136. CUDA_ADD_CUBLAS_TO_TARGET( cuda_target )
  137. -- Adds the cublas library to the target (can be any target). Handles
  138. whether you are in emulation mode or not.
  139. CUDA_ADD_EXECUTABLE( cuda_target file0 file1 ...
  140. [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
  141. -- Creates an executable "cuda_target" which is made up of the files
  142. specified. All of the non CUDA C files are compiled using the standard
  143. build rules specified by CMAKE and the cuda files are compiled to object
  144. files using nvcc and the host compiler. In addition CUDA_INCLUDE_DIRS is
  145. added automatically to include_directories(). Some standard CMake target
  146. calls can be used on the target after calling this macro
  147. (e.g. set_target_properties and target_link_libraries), but setting
  148. properties that adjust compilation flags will not affect code compiled by
  149. nvcc. Such flags should be modified before calling CUDA_ADD_EXECUTABLE,
  150. CUDA_ADD_LIBRARY or CUDA_WRAP_SRCS.
  151. CUDA_ADD_LIBRARY( cuda_target file0 file1 ...
  152. [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
  153. -- Same as CUDA_ADD_EXECUTABLE except that a library is created.
  154. CUDA_BUILD_CLEAN_TARGET()
  155. -- Creates a convenience target that deletes all the dependency files
  156. generated. You should make clean after running this target to ensure the
  157. dependency files get regenerated.
  158. CUDA_COMPILE( generated_files file0 file1 ... [STATIC | SHARED | MODULE]
  159. [OPTIONS ...] )
  160. -- Returns a list of generated files from the input source files to be used
  161. with ADD_LIBRARY or ADD_EXECUTABLE.
  162. CUDA_COMPILE_PTX( generated_files file0 file1 ... [OPTIONS ...] )
  163. -- Returns a list of PTX files generated from the input source files.
  164. CUDA_COMPILE_FATBIN( generated_files file0 file1 ... [OPTIONS ...] )
  165. -- Returns a list of FATBIN files generated from the input source files.
  166. CUDA_COMPILE_CUBIN( generated_files file0 file1 ... [OPTIONS ...] )
  167. -- Returns a list of CUBIN files generated from the input source files.
  168. CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME( output_file_var
  169. cuda_target
  170. object_files )
  171. -- Compute the name of the intermediate link file used for separable
  172. compilation. This file name is typically passed into
  173. CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS. output_file_var is produced
  174. based on cuda_target the list of objects files that need separable
  175. compilation as specified by object_files. If the object_files list is
  176. empty, then output_file_var will be empty. This function is called
  177. automatically for CUDA_ADD_LIBRARY and CUDA_ADD_EXECUTABLE. Note that
  178. this is a function and not a macro.
  179. CUDA_INCLUDE_DIRECTORIES( path0 path1 ... )
  180. -- Sets the directories that should be passed to nvcc
  181. (e.g. nvcc -Ipath0 -Ipath1 ... ). These paths usually contain other .cu
  182. files.
  183. CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS( output_file_var cuda_target
  184. nvcc_flags object_files)
  185. -- Generates the link object required by separable compilation from the given
  186. object files. This is called automatically for CUDA_ADD_EXECUTABLE and
  187. CUDA_ADD_LIBRARY, but can be called manually when using CUDA_WRAP_SRCS
  188. directly. When called from CUDA_ADD_LIBRARY or CUDA_ADD_EXECUTABLE the
  189. nvcc_flags passed in are the same as the flags passed in via the OPTIONS
  190. argument. The only nvcc flag added automatically is the bitness flag as
  191. specified by CUDA_64_BIT_DEVICE_CODE. Note that this is a function
  192. instead of a macro.
  193. CUDA_SELECT_NVCC_ARCH_FLAGS(out_variable [target_CUDA_architectures])
  194. -- Selects GPU arch flags for nvcc based on target_CUDA_architectures
  195. target_CUDA_architectures : Auto | Common | All | LIST(ARCH_AND_PTX ...)
  196. - "Auto" detects local machine GPU compute arch at runtime.
  197. - "Common" and "All" cover common and entire subsets of architectures
  198. ARCH_AND_PTX : NAME | NUM.NUM | NUM.NUM(NUM.NUM) | NUM.NUM+PTX
  199. NAME: Fermi Kepler Maxwell Kepler+Tegra Kepler+Tesla Maxwell+Tegra Pascal
  200. NUM: Any number. Only those pairs are currently accepted by NVCC though:
  201. 2.0 2.1 3.0 3.2 3.5 3.7 5.0 5.2 5.3 6.0 6.2
  202. Returns LIST of flags to be added to CUDA_NVCC_FLAGS in ${out_variable}
  203. Additionally, sets ${out_variable}_readable to the resulting numeric list
  204. Example:
  205. CUDA_SELECT_NVCC_ARCH_FLAGS(ARCH_FLAGS 3.0 3.5+PTX 5.2(5.0) Maxwell)
  206. LIST(APPEND CUDA_NVCC_FLAGS ${ARCH_FLAGS})
  207. More info on CUDA architectures: https://en.wikipedia.org/wiki/CUDA
  208. Note that this is a function instead of a macro.
  209. CUDA_WRAP_SRCS ( cuda_target format generated_files file0 file1 ...
  210. [STATIC | SHARED | MODULE] [OPTIONS ...] )
  211. -- This is where all the magic happens. CUDA_ADD_EXECUTABLE,
  212. CUDA_ADD_LIBRARY, CUDA_COMPILE, and CUDA_COMPILE_PTX all call this
  213. function under the hood.
  214. Given the list of files (file0 file1 ... fileN) this macro generates
  215. custom commands that generate either PTX or linkable objects (use "PTX" or
  216. "OBJ" for the format argument to switch). Files that don't end with .cu
  217. or have the HEADER_FILE_ONLY property are ignored.
  218. The arguments passed in after OPTIONS are extra command line options to
  219. give to nvcc. You can also specify per configuration options by
  220. specifying the name of the configuration followed by the options. General
  221. options must precede configuration specific options. Not all
  222. configurations need to be specified, only the ones provided will be used.
  223. OPTIONS -DFLAG=2 "-DFLAG_OTHER=space in flag"
  224. DEBUG -g
  225. RELEASE --use_fast_math
  226. RELWITHDEBINFO --use_fast_math;-g
  227. MINSIZEREL --use_fast_math
  228. For certain configurations (namely VS generating object files with
  229. CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE set to ON), no generated file will
  230. be produced for the given cuda file. This is because when you add the
  231. cuda file to Visual Studio it knows that this file produces an object file
  232. and will link in the resulting object file automatically.
  233. This script will also generate a separate cmake script that is used at
  234. build time to invoke nvcc. This is for several reasons.
  235. 1. nvcc can return negative numbers as return values which confuses
  236. Visual Studio into thinking that the command succeeded. The script now
  237. checks the error codes and produces errors when there was a problem.
  238. 2. nvcc has been known to not delete incomplete results when it
  239. encounters problems. This confuses build systems into thinking the
  240. target was generated when in fact an unusable file exists. The script
  241. now deletes the output files if there was an error.
  242. 3. By putting all the options that affect the build into a file and then
  243. make the build rule dependent on the file, the output files will be
  244. regenerated when the options change.
  245. This script also looks at optional arguments STATIC, SHARED, or MODULE to
  246. determine when to target the object compilation for a shared library.
  247. BUILD_SHARED_LIBS is ignored in CUDA_WRAP_SRCS, but it is respected in
  248. CUDA_ADD_LIBRARY. On some systems special flags are added for building
  249. objects intended for shared libraries. A preprocessor macro,
  250. <target_name>_EXPORTS is defined when a shared library compilation is
  251. detected.
  252. Flags passed into add_definitions with -D or /D are passed along to nvcc.
  253. The script defines the following variables::
  254. CUDA_VERSION_MAJOR -- The major version of cuda as reported by nvcc.
  255. CUDA_VERSION_MINOR -- The minor version.
  256. CUDA_VERSION
  257. CUDA_VERSION_STRING -- CUDA_VERSION_MAJOR.CUDA_VERSION_MINOR
  258. CUDA_HAS_FP16 -- Whether a short float (float16,fp16) is supported.
  259. CUDA_TOOLKIT_ROOT_DIR -- Path to the CUDA Toolkit (defined if not set).
  260. CUDA_SDK_ROOT_DIR -- Path to the CUDA SDK. Use this to find files in the
  261. SDK. This script will not directly support finding
  262. specific libraries or headers, as that isn't
  263. supported by NVIDIA. If you want to change
  264. libraries when the path changes see the
  265. FindCUDA.cmake script for an example of how to clear
  266. these variables. There are also examples of how to
  267. use the CUDA_SDK_ROOT_DIR to locate headers or
  268. libraries, if you so choose (at your own risk).
  269. CUDA_INCLUDE_DIRS -- Include directory for cuda headers. Added automatically
  270. for CUDA_ADD_EXECUTABLE and CUDA_ADD_LIBRARY.
  271. CUDA_LIBRARIES -- Cuda RT library.
  272. CUDA_CUFFT_LIBRARIES -- Device or emulation library for the Cuda FFT
  273. implementation (alternative to:
  274. CUDA_ADD_CUFFT_TO_TARGET macro)
  275. CUDA_CUBLAS_LIBRARIES -- Device or emulation library for the Cuda BLAS
  276. implementation (alternative to:
  277. CUDA_ADD_CUBLAS_TO_TARGET macro).
  278. CUDA_cudart_static_LIBRARY -- Statically linkable cuda runtime library.
  279. Only available for CUDA version 5.5+
  280. CUDA_cudadevrt_LIBRARY -- Device runtime library.
  281. Required for separable compilation.
  282. CUDA_cupti_LIBRARY -- CUDA Profiling Tools Interface library.
  283. Only available for CUDA version 4.0+.
  284. CUDA_curand_LIBRARY -- CUDA Random Number Generation library.
  285. Only available for CUDA version 3.2+.
  286. CUDA_cusolver_LIBRARY -- CUDA Direct Solver library.
  287. Only available for CUDA version 7.0+.
  288. CUDA_cusparse_LIBRARY -- CUDA Sparse Matrix library.
  289. Only available for CUDA version 3.2+.
  290. CUDA_npp_LIBRARY -- NVIDIA Performance Primitives lib.
  291. Only available for CUDA version 4.0+.
  292. CUDA_nppc_LIBRARY -- NVIDIA Performance Primitives lib (core).
  293. Only available for CUDA version 5.5+.
  294. CUDA_nppi_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  295. Only available for CUDA version 5.5 - 8.0.
  296. CUDA_nppial_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  297. Only available for CUDA version 9.0.
  298. CUDA_nppicc_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  299. Only available for CUDA version 9.0.
  300. CUDA_nppicom_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  301. Only available for CUDA version 9.0.
  302. CUDA_nppidei_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  303. Only available for CUDA version 9.0.
  304. CUDA_nppif_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  305. Only available for CUDA version 9.0.
  306. CUDA_nppig_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  307. Only available for CUDA version 9.0.
  308. CUDA_nppim_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  309. Only available for CUDA version 9.0.
  310. CUDA_nppist_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  311. Only available for CUDA version 9.0.
  312. CUDA_nppisu_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  313. Only available for CUDA version 9.0.
  314. CUDA_nppitc_LIBRARY -- NVIDIA Performance Primitives lib (image processing).
  315. Only available for CUDA version 9.0.
  316. CUDA_npps_LIBRARY -- NVIDIA Performance Primitives lib (signal processing).
  317. Only available for CUDA version 5.5+.
  318. CUDA_nvcuvenc_LIBRARY -- CUDA Video Encoder library.
  319. Only available for CUDA version 3.2+.
  320. Windows only.
  321. CUDA_nvcuvid_LIBRARY -- CUDA Video Decoder library.
  322. Only available for CUDA version 3.2+.
  323. Windows only.
  324. CUDA_nvToolsExt_LIBRARY
  325. -- NVIDA CUDA Tools Extension library.
  326. Available for CUDA version 5+.
  327. CUDA_OpenCL_LIBRARY -- NVIDA CUDA OpenCL library.
  328. Available for CUDA version 5+.
  329. #]=======================================================================]
  330. # James Bigler, NVIDIA Corp (nvidia.com - jbigler)
  331. # Abe Stephens, SCI Institute -- http://www.sci.utah.edu/~abe/FindCuda.html
  332. #
  333. # Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
  334. #
  335. # Copyright (c) 2007-2009
  336. # Scientific Computing and Imaging Institute, University of Utah
  337. #
  338. # This code is licensed under the MIT License. See the FindCUDA.cmake script
  339. # for the text of the license.
  340. # The MIT License
  341. #
  342. # License for the specific language governing rights and limitations under
  343. # Permission is hereby granted, free of charge, to any person obtaining a
  344. # copy of this software and associated documentation files (the "Software"),
  345. # to deal in the Software without restriction, including without limitation
  346. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  347. # and/or sell copies of the Software, and to permit persons to whom the
  348. # Software is furnished to do so, subject to the following conditions:
  349. #
  350. # The above copyright notice and this permission notice shall be included
  351. # in all copies or substantial portions of the Software.
  352. #
  353. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  354. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  355. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  356. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  357. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  358. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  359. # DEALINGS IN THE SOFTWARE.
  360. #
  361. ###############################################################################
  362. # FindCUDA.cmake
  363. # This macro helps us find the location of helper files we will need the full path to
  364. macro(CUDA_FIND_HELPER_FILE _name _extension)
  365. set(_full_name "${_name}.${_extension}")
  366. # CMAKE_CURRENT_LIST_FILE contains the full path to the file currently being
  367. # processed. Using this variable, we can pull out the current path, and
  368. # provide a way to get access to the other files we need local to here.
  369. get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
  370. set(CUDA_${_name} "${CMAKE_CURRENT_LIST_DIR}/FindCUDA/${_full_name}")
  371. if(NOT EXISTS "${CUDA_${_name}}")
  372. set(error_message "${_full_name} not found in ${CMAKE_CURRENT_LIST_DIR}/FindCUDA")
  373. if(CUDA_FIND_REQUIRED)
  374. message(FATAL_ERROR "${error_message}")
  375. else()
  376. if(NOT CUDA_FIND_QUIETLY)
  377. message(STATUS "${error_message}")
  378. endif()
  379. endif()
  380. endif()
  381. # Set this variable as internal, so the user isn't bugged with it.
  382. set(CUDA_${_name} ${CUDA_${_name}} CACHE INTERNAL "Location of ${_full_name}" FORCE)
  383. endmacro()
  384. #####################################################################
  385. ## CUDA_INCLUDE_NVCC_DEPENDENCIES
  386. ##
  387. # So we want to try and include the dependency file if it exists. If
  388. # it doesn't exist then we need to create an empty one, so we can
  389. # include it.
  390. # If it does exist, then we need to check to see if all the files it
  391. # depends on exist. If they don't then we should clear the dependency
  392. # file and regenerate it later. This covers the case where a header
  393. # file has disappeared or moved.
  394. macro(CUDA_INCLUDE_NVCC_DEPENDENCIES dependency_file)
  395. set(CUDA_NVCC_DEPEND)
  396. set(CUDA_NVCC_DEPEND_REGENERATE FALSE)
  397. # Include the dependency file. Create it first if it doesn't exist . The
  398. # INCLUDE puts a dependency that will force CMake to rerun and bring in the
  399. # new info when it changes. DO NOT REMOVE THIS (as I did and spent a few
  400. # hours figuring out why it didn't work.
  401. if(NOT EXISTS ${dependency_file})
  402. file(WRITE ${dependency_file} "#FindCUDA.cmake generated file. Do not edit.\n")
  403. endif()
  404. # Always include this file to force CMake to run again next
  405. # invocation and rebuild the dependencies.
  406. #message("including dependency_file = ${dependency_file}")
  407. include(${dependency_file})
  408. # Now we need to verify the existence of all the included files
  409. # here. If they aren't there we need to just blank this variable and
  410. # make the file regenerate again.
  411. # if(DEFINED CUDA_NVCC_DEPEND)
  412. # message("CUDA_NVCC_DEPEND set")
  413. # else()
  414. # message("CUDA_NVCC_DEPEND NOT set")
  415. # endif()
  416. if(CUDA_NVCC_DEPEND)
  417. #message("CUDA_NVCC_DEPEND found")
  418. foreach(f ${CUDA_NVCC_DEPEND})
  419. # message("searching for ${f}")
  420. if(NOT EXISTS ${f})
  421. #message("file ${f} not found")
  422. set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
  423. endif()
  424. endforeach()
  425. else()
  426. #message("CUDA_NVCC_DEPEND false")
  427. # No dependencies, so regenerate the file.
  428. set(CUDA_NVCC_DEPEND_REGENERATE TRUE)
  429. endif()
  430. #message("CUDA_NVCC_DEPEND_REGENERATE = ${CUDA_NVCC_DEPEND_REGENERATE}")
  431. # No incoming dependencies, so we need to generate them. Make the
  432. # output depend on the dependency file itself, which should cause the
  433. # rule to re-run.
  434. if(CUDA_NVCC_DEPEND_REGENERATE)
  435. set(CUDA_NVCC_DEPEND ${dependency_file})
  436. #message("Generating an empty dependency_file: ${dependency_file}")
  437. file(WRITE ${dependency_file} "#FindCUDA.cmake generated file. Do not edit.\n")
  438. endif()
  439. endmacro()
  440. ###############################################################################
  441. ###############################################################################
  442. # Setup variables' defaults
  443. ###############################################################################
  444. ###############################################################################
  445. # Allow the user to specify if the device code is supposed to be 32 or 64 bit.
  446. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  447. set(CUDA_64_BIT_DEVICE_CODE_DEFAULT ON)
  448. else()
  449. set(CUDA_64_BIT_DEVICE_CODE_DEFAULT OFF)
  450. endif()
  451. option(CUDA_64_BIT_DEVICE_CODE "Compile device code in 64 bit mode" ${CUDA_64_BIT_DEVICE_CODE_DEFAULT})
  452. # Attach the build rule to the source file in VS. This option
  453. 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)
  454. # Prints out extra information about the cuda file during compilation
  455. option(CUDA_BUILD_CUBIN "Generate and parse .cubin files in Device mode." OFF)
  456. # Set whether we are using emulation or device mode.
  457. option(CUDA_BUILD_EMULATION "Build in Emulation mode" OFF)
  458. # Where to put the generated output.
  459. 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")
  460. # Parse HOST_COMPILATION mode.
  461. option(CUDA_HOST_COMPILATION_CPP "Generated file extension" ON)
  462. # Extra user settable flags
  463. cmake_initialize_per_config_variable(CUDA_NVCC_FLAGS "Semi-colon delimit multiple arguments.")
  464. if(DEFINED ENV{CUDAHOSTCXX})
  465. set(CUDA_HOST_COMPILER "$ENV{CUDAHOSTCXX}" CACHE FILEPATH "Host side compiler used by NVCC")
  466. elseif(CMAKE_GENERATOR MATCHES "Visual Studio")
  467. set(_CUDA_MSVC_HOST_COMPILER "$(VCInstallDir)Tools/MSVC/$(VCToolsVersion)/bin/Host$(Platform)/$(PlatformTarget)")
  468. if(MSVC_VERSION LESS 1910)
  469. set(_CUDA_MSVC_HOST_COMPILER "$(VCInstallDir)bin")
  470. endif()
  471. set(CUDA_HOST_COMPILER "${_CUDA_MSVC_HOST_COMPILER}" CACHE FILEPATH "Host side compiler used by NVCC")
  472. else()
  473. if(APPLE
  474. AND "${CMAKE_C_COMPILER_ID}" MATCHES "Clang"
  475. AND "${CMAKE_C_COMPILER}" MATCHES "/cc$")
  476. # Using cc which is symlink to clang may let NVCC think it is GCC and issue
  477. # unhandled -dumpspecs option to clang. Also in case neither
  478. # CMAKE_C_COMPILER is defined (project does not use C language) nor
  479. # CUDA_HOST_COMPILER is specified manually we should skip -ccbin and let
  480. # nvcc use its own default C compiler.
  481. # Only care about this on APPLE with clang to avoid
  482. # following symlinks to things like ccache
  483. if(DEFINED CMAKE_C_COMPILER AND NOT DEFINED CUDA_HOST_COMPILER)
  484. get_filename_component(c_compiler_realpath "${CMAKE_C_COMPILER}" REALPATH)
  485. # if the real path does not end up being clang then
  486. # go back to using CMAKE_C_COMPILER
  487. if(NOT "${c_compiler_realpath}" MATCHES "/clang$")
  488. set(c_compiler_realpath "${CMAKE_C_COMPILER}")
  489. endif()
  490. else()
  491. set(c_compiler_realpath "")
  492. endif()
  493. set(CUDA_HOST_COMPILER "${c_compiler_realpath}" CACHE FILEPATH "Host side compiler used by NVCC")
  494. elseif(MSVC AND "${CMAKE_C_COMPILER}" MATCHES "clcache|sccache")
  495. # NVCC does not think it will work if it is passed clcache.exe or sccache.exe
  496. # as the host compiler, which means that builds with CC=cl.exe won't work.
  497. # Best to just feed it whatever the actual cl.exe is as the host compiler.
  498. set(CUDA_HOST_COMPILER "cl.exe" CACHE FILEPATH "Host side compiler used by NVCC")
  499. else()
  500. set(CUDA_HOST_COMPILER "${CMAKE_C_COMPILER}"
  501. CACHE FILEPATH "Host side compiler used by NVCC")
  502. endif()
  503. endif()
  504. # Propagate the host flags to the host compiler via -Xcompiler
  505. option(CUDA_PROPAGATE_HOST_FLAGS "Propagate C/CXX_FLAGS and friends to the host compiler via -Xcompile" ON)
  506. # Enable CUDA_SEPARABLE_COMPILATION
  507. option(CUDA_SEPARABLE_COMPILATION "Compile CUDA objects with separable compilation enabled. Requires CUDA 5.0+" OFF)
  508. # Specifies whether the commands used when compiling the .cu file will be printed out.
  509. 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)
  510. mark_as_advanced(
  511. CUDA_64_BIT_DEVICE_CODE
  512. CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE
  513. CUDA_GENERATED_OUTPUT_DIR
  514. CUDA_HOST_COMPILATION_CPP
  515. CUDA_NVCC_FLAGS
  516. CUDA_PROPAGATE_HOST_FLAGS
  517. CUDA_BUILD_CUBIN
  518. CUDA_BUILD_EMULATION
  519. CUDA_VERBOSE_BUILD
  520. CUDA_SEPARABLE_COMPILATION
  521. )
  522. # Single config generators like Makefiles or Ninja don't usually have
  523. # CMAKE_CONFIGURATION_TYPES defined (but note that it can be defined if set by
  524. # projects or developers). Even CMAKE_BUILD_TYPE might not be defined for
  525. # single config generators (and should not be defined for multi-config
  526. # generators). To ensure we get a complete superset of all possible
  527. # configurations, we combine CMAKE_CONFIGURATION_TYPES, CMAKE_BUILD_TYPE and
  528. # all of the standard configurations, then weed out duplicates with
  529. # list(REMOVE_DUPLICATES). Looping over the unique set then ensures we have
  530. # each configuration-specific set of nvcc flags defined and marked as advanced.
  531. set(CUDA_configuration_types ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} Debug MinSizeRel Release RelWithDebInfo)
  532. list(REMOVE_DUPLICATES CUDA_configuration_types)
  533. ###############################################################################
  534. ###############################################################################
  535. # Locate CUDA, Set Build Type, etc.
  536. ###############################################################################
  537. ###############################################################################
  538. macro(cuda_unset_include_and_libraries)
  539. unset(CUDA_TOOLKIT_INCLUDE CACHE)
  540. unset(CUDA_CUDART_LIBRARY CACHE)
  541. unset(CUDA_CUDA_LIBRARY CACHE)
  542. # Make sure you run this before you unset CUDA_VERSION.
  543. if(CUDA_VERSION VERSION_EQUAL "3.0")
  544. # This only existed in the 3.0 version of the CUDA toolkit
  545. unset(CUDA_CUDARTEMU_LIBRARY CACHE)
  546. endif()
  547. unset(CUDA_cudart_static_LIBRARY CACHE)
  548. unset(CUDA_cudadevrt_LIBRARY CACHE)
  549. unset(CUDA_cublas_LIBRARY CACHE)
  550. unset(CUDA_cublas_device_LIBRARY CACHE)
  551. unset(CUDA_cublasemu_LIBRARY CACHE)
  552. unset(CUDA_cufft_LIBRARY CACHE)
  553. unset(CUDA_cufftemu_LIBRARY CACHE)
  554. unset(CUDA_cupti_LIBRARY CACHE)
  555. unset(CUDA_curand_LIBRARY CACHE)
  556. unset(CUDA_cusolver_LIBRARY CACHE)
  557. unset(CUDA_cusparse_LIBRARY CACHE)
  558. unset(CUDA_npp_LIBRARY CACHE)
  559. unset(CUDA_nppc_LIBRARY CACHE)
  560. unset(CUDA_nppi_LIBRARY CACHE)
  561. unset(CUDA_npps_LIBRARY CACHE)
  562. unset(CUDA_nvcuvenc_LIBRARY CACHE)
  563. unset(CUDA_nvcuvid_LIBRARY CACHE)
  564. unset(CUDA_nvToolsExt_LIBRARY CACHE)
  565. unset(CUDA_OpenCL_LIBRARY CACHE)
  566. unset(CUDA_GPU_DETECT_OUTPUT CACHE)
  567. endmacro()
  568. # Check to see if the CUDA_TOOLKIT_ROOT_DIR and CUDA_SDK_ROOT_DIR have changed,
  569. # if they have then clear the cache variables, so that will be detected again.
  570. if(NOT "${CUDA_TOOLKIT_ROOT_DIR}" STREQUAL "${CUDA_TOOLKIT_ROOT_DIR_INTERNAL}")
  571. unset(CUDA_TOOLKIT_TARGET_DIR CACHE)
  572. unset(CUDA_NVCC_EXECUTABLE CACHE)
  573. cuda_unset_include_and_libraries()
  574. unset(CUDA_VERSION CACHE)
  575. endif()
  576. if(NOT "${CUDA_TOOLKIT_TARGET_DIR}" STREQUAL "${CUDA_TOOLKIT_TARGET_DIR_INTERNAL}")
  577. cuda_unset_include_and_libraries()
  578. endif()
  579. #
  580. # End of unset()
  581. #
  582. #
  583. # Start looking for things
  584. #
  585. # Search for the cuda distribution.
  586. if(NOT CUDA_TOOLKIT_ROOT_DIR AND NOT CMAKE_CROSSCOMPILING)
  587. # Search in the CUDA_BIN_PATH first.
  588. find_program(CUDA_TOOLKIT_ROOT_DIR_NVCC
  589. NAMES nvcc nvcc.exe
  590. PATHS
  591. ENV CUDA_TOOLKIT_ROOT
  592. ENV CUDA_PATH
  593. ENV CUDA_BIN_PATH
  594. PATH_SUFFIXES bin bin64
  595. DOC "Toolkit location."
  596. NO_DEFAULT_PATH
  597. )
  598. # Now search default paths
  599. find_program(CUDA_TOOLKIT_ROOT_DIR_NVCC
  600. NAMES nvcc nvcc.exe
  601. PATHS /opt/cuda/bin
  602. PATH_SUFFIXES cuda/bin
  603. DOC "Toolkit location."
  604. )
  605. if (CUDA_TOOLKIT_ROOT_DIR_NVCC)
  606. get_filename_component(CUDA_TOOLKIT_ROOT_DIR_NVCC_PAR "${CUDA_TOOLKIT_ROOT_DIR_NVCC}" DIRECTORY)
  607. get_filename_component(CUDA_TOOLKIT_ROOT_DIR "${CUDA_TOOLKIT_ROOT_DIR_NVCC_PAR}" DIRECTORY CACHE)
  608. string(REGEX REPLACE "[/\\\\]?bin[64]*[/\\\\]?$" "" CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR})
  609. # We need to force this back into the cache.
  610. set(CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR} CACHE PATH "Toolkit location." FORCE)
  611. set(CUDA_TOOLKIT_TARGET_DIR ${CUDA_TOOLKIT_ROOT_DIR})
  612. endif()
  613. unset(CUDA_TOOLKIT_ROOT_DIR_NVCC CACHE)
  614. if (NOT EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
  615. if(CUDA_FIND_REQUIRED)
  616. message(FATAL_ERROR "Specify CUDA_TOOLKIT_ROOT_DIR")
  617. elseif(NOT CUDA_FIND_QUIETLY)
  618. message("CUDA_TOOLKIT_ROOT_DIR not found or specified")
  619. endif()
  620. endif ()
  621. endif ()
  622. if(CMAKE_CROSSCOMPILING)
  623. SET (CUDA_TOOLKIT_ROOT $ENV{CUDA_TOOLKIT_ROOT})
  624. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
  625. # Support for NVPACK
  626. set (CUDA_TOOLKIT_TARGET_NAME "armv7-linux-androideabi")
  627. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
  628. # Support for arm cross compilation
  629. set(CUDA_TOOLKIT_TARGET_NAME "armv7-linux-gnueabihf")
  630. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  631. # Support for aarch64 cross compilation
  632. if (ANDROID_ARCH_NAME STREQUAL "arm64")
  633. set(CUDA_TOOLKIT_TARGET_NAME "aarch64-linux-androideabi")
  634. else()
  635. set(CUDA_TOOLKIT_TARGET_NAME "aarch64-linux")
  636. endif (ANDROID_ARCH_NAME STREQUAL "arm64")
  637. endif()
  638. if (EXISTS "${CUDA_TOOLKIT_ROOT}/targets/${CUDA_TOOLKIT_TARGET_NAME}")
  639. set(CUDA_TOOLKIT_TARGET_DIR "${CUDA_TOOLKIT_ROOT}/targets/${CUDA_TOOLKIT_TARGET_NAME}" CACHE PATH "CUDA Toolkit target location.")
  640. SET (CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT})
  641. mark_as_advanced(CUDA_TOOLKIT_TARGET_DIR)
  642. endif()
  643. # add known CUDA targetr root path to the set of directories we search for programs, libraries and headers
  644. set( CMAKE_FIND_ROOT_PATH "${CUDA_TOOLKIT_TARGET_DIR};${CMAKE_FIND_ROOT_PATH}")
  645. macro( cuda_find_host_program )
  646. if (COMMAND find_host_program)
  647. find_host_program( ${ARGN} )
  648. else()
  649. find_program( ${ARGN} )
  650. endif()
  651. endmacro()
  652. else()
  653. # for non-cross-compile, find_host_program == find_program and CUDA_TOOLKIT_TARGET_DIR == CUDA_TOOLKIT_ROOT_DIR
  654. macro( cuda_find_host_program )
  655. find_program( ${ARGN} )
  656. endmacro()
  657. SET (CUDA_TOOLKIT_TARGET_DIR ${CUDA_TOOLKIT_ROOT_DIR})
  658. endif()
  659. # CUDA_NVCC_EXECUTABLE
  660. if(DEFINED ENV{CUDA_NVCC_EXECUTABLE})
  661. set(CUDA_NVCC_EXECUTABLE "$ENV{CUDA_NVCC_EXECUTABLE}" CACHE FILEPATH "The CUDA compiler")
  662. else()
  663. cuda_find_host_program(CUDA_NVCC_EXECUTABLE
  664. NAMES nvcc
  665. PATHS "${CUDA_TOOLKIT_ROOT_DIR}"
  666. ENV CUDA_PATH
  667. ENV CUDA_BIN_PATH
  668. PATH_SUFFIXES bin bin64
  669. NO_DEFAULT_PATH
  670. )
  671. # Search default search paths, after we search our own set of paths.
  672. cuda_find_host_program(CUDA_NVCC_EXECUTABLE nvcc)
  673. endif()
  674. mark_as_advanced(CUDA_NVCC_EXECUTABLE)
  675. if(CUDA_NVCC_EXECUTABLE AND NOT CUDA_VERSION)
  676. # Compute the version.
  677. execute_process (COMMAND ${CUDA_NVCC_EXECUTABLE} "--version" OUTPUT_VARIABLE NVCC_OUT)
  678. string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR ${NVCC_OUT})
  679. string(REGEX REPLACE ".*release ([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR ${NVCC_OUT})
  680. set(CUDA_VERSION "${CUDA_VERSION_MAJOR}.${CUDA_VERSION_MINOR}" CACHE STRING "Version of CUDA as computed from nvcc.")
  681. mark_as_advanced(CUDA_VERSION)
  682. else()
  683. # Need to set these based off of the cached value
  684. string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\1" CUDA_VERSION_MAJOR "${CUDA_VERSION}")
  685. string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\2" CUDA_VERSION_MINOR "${CUDA_VERSION}")
  686. endif()
  687. # Always set this convenience variable
  688. set(CUDA_VERSION_STRING "${CUDA_VERSION}")
  689. # CUDA_TOOLKIT_INCLUDE
  690. find_path(CUDA_TOOLKIT_INCLUDE
  691. device_functions.h # Header included in toolkit
  692. PATHS ${CUDA_TOOLKIT_TARGET_DIR}
  693. ENV CUDA_PATH
  694. ENV CUDA_INC_PATH
  695. PATH_SUFFIXES include
  696. NO_DEFAULT_PATH
  697. )
  698. # Search default search paths, after we search our own set of paths.
  699. find_path(CUDA_TOOLKIT_INCLUDE device_functions.h)
  700. mark_as_advanced(CUDA_TOOLKIT_INCLUDE)
  701. if (CUDA_VERSION VERSION_GREATER "7.0" OR EXISTS "${CUDA_TOOLKIT_INCLUDE}/cuda_fp16.h")
  702. set(CUDA_HAS_FP16 TRUE)
  703. else()
  704. set(CUDA_HAS_FP16 FALSE)
  705. endif()
  706. # Set the user list of include dir to nothing to initialize it.
  707. set (CUDA_NVCC_INCLUDE_DIRS_USER "")
  708. set (CUDA_INCLUDE_DIRS ${CUDA_TOOLKIT_INCLUDE})
  709. macro(cuda_find_library_local_first_with_path_ext _var _names _doc _path_ext )
  710. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  711. # CUDA 3.2+ on Windows moved the library directories, so we need the new
  712. # and old paths.
  713. set(_cuda_64bit_lib_dir "${_path_ext}lib/x64" "${_path_ext}lib64" "${_path_ext}libx64" )
  714. endif()
  715. # CUDA 3.2+ on Windows moved the library directories, so we need to new
  716. # (lib/Win32) and the old path (lib).
  717. find_library(${_var}
  718. NAMES ${_names}
  719. PATHS "${CUDA_TOOLKIT_TARGET_DIR}"
  720. ENV CUDA_PATH
  721. ENV CUDA_LIB_PATH
  722. PATH_SUFFIXES ${_cuda_64bit_lib_dir} "${_path_ext}lib/Win32" "${_path_ext}lib" "${_path_ext}libWin32"
  723. DOC ${_doc}
  724. NO_DEFAULT_PATH
  725. )
  726. if (NOT CMAKE_CROSSCOMPILING)
  727. # Search default search paths, after we search our own set of paths.
  728. find_library(${_var}
  729. NAMES ${_names}
  730. PATHS "/usr/lib/nvidia-current"
  731. DOC ${_doc}
  732. )
  733. endif()
  734. endmacro()
  735. macro(cuda_find_library_local_first _var _names _doc)
  736. cuda_find_library_local_first_with_path_ext( "${_var}" "${_names}" "${_doc}" "" )
  737. endmacro()
  738. macro(find_library_local_first _var _names _doc )
  739. cuda_find_library_local_first( "${_var}" "${_names}" "${_doc}" "" )
  740. endmacro()
  741. # CUDA_LIBRARIES
  742. cuda_find_library_local_first(CUDA_CUDART_LIBRARY cudart "\"cudart\" library")
  743. if(CUDA_VERSION VERSION_EQUAL "3.0")
  744. # The cudartemu library only existed for the 3.0 version of CUDA.
  745. cuda_find_library_local_first(CUDA_CUDARTEMU_LIBRARY cudartemu "\"cudartemu\" library")
  746. mark_as_advanced(
  747. CUDA_CUDARTEMU_LIBRARY
  748. )
  749. endif()
  750. if(NOT CUDA_VERSION VERSION_LESS "5.5")
  751. cuda_find_library_local_first(CUDA_cudart_static_LIBRARY cudart_static "static CUDA runtime library")
  752. mark_as_advanced(CUDA_cudart_static_LIBRARY)
  753. endif()
  754. if(CUDA_cudart_static_LIBRARY)
  755. # If static cudart available, use it by default, but provide a user-visible option to disable it.
  756. option(CUDA_USE_STATIC_CUDA_RUNTIME "Use the static version of the CUDA runtime library if available" ON)
  757. else()
  758. # If not available, silently disable the option.
  759. set(CUDA_USE_STATIC_CUDA_RUNTIME OFF CACHE INTERNAL "")
  760. endif()
  761. if(CUDA_USE_STATIC_CUDA_RUNTIME)
  762. set(CUDA_CUDART_LIBRARY_VAR CUDA_cudart_static_LIBRARY)
  763. else()
  764. set(CUDA_CUDART_LIBRARY_VAR CUDA_CUDART_LIBRARY)
  765. endif()
  766. if(NOT CUDA_VERSION VERSION_LESS "5.0")
  767. cuda_find_library_local_first(CUDA_cudadevrt_LIBRARY cudadevrt "\"cudadevrt\" library")
  768. mark_as_advanced(CUDA_cudadevrt_LIBRARY)
  769. endif()
  770. if(CUDA_USE_STATIC_CUDA_RUNTIME)
  771. if(UNIX)
  772. # Check for the dependent libraries.
  773. # Many of the FindXYZ CMake comes with makes use of try_compile with int main(){return 0;}
  774. # as the source file. Unfortunately this causes a warning with -Wstrict-prototypes and
  775. # -Werror causes the try_compile to fail. We will just temporarily disable other flags
  776. # when doing the find_package command here.
  777. set(_cuda_cmake_c_flags ${CMAKE_C_FLAGS})
  778. set(CMAKE_C_FLAGS "-fPIC")
  779. find_package(Threads REQUIRED)
  780. set(CMAKE_C_FLAGS ${_cuda_cmake_c_flags})
  781. if(NOT APPLE)
  782. #On Linux, you must link against librt when using the static cuda runtime.
  783. find_library(CUDA_rt_LIBRARY rt)
  784. if (NOT CUDA_rt_LIBRARY)
  785. message(WARNING "Expecting to find librt for libcudart_static, but didn't find it.")
  786. endif()
  787. endif()
  788. endif()
  789. endif()
  790. # CUPTI library showed up in cuda toolkit 4.0
  791. if(NOT CUDA_VERSION VERSION_LESS "4.0")
  792. cuda_find_library_local_first_with_path_ext(CUDA_cupti_LIBRARY cupti "\"cupti\" library" "extras/CUPTI/")
  793. mark_as_advanced(CUDA_cupti_LIBRARY)
  794. endif()
  795. # Set the CUDA_LIBRARIES variable. This is the set of stuff to link against if you are
  796. # using the CUDA runtime. For the dynamic version of the runtime, most of the
  797. # dependencies are brough in, but for the static version there are additional libraries
  798. # and linker commands needed.
  799. # Initialize to empty
  800. set(CUDA_LIBRARIES)
  801. # If we are using emulation mode and we found the cudartemu library then use
  802. # that one instead of cudart.
  803. if(CUDA_BUILD_EMULATION AND CUDA_CUDARTEMU_LIBRARY)
  804. list(APPEND CUDA_LIBRARIES ${CUDA_CUDARTEMU_LIBRARY})
  805. elseif(CUDA_USE_STATIC_CUDA_RUNTIME AND CUDA_cudart_static_LIBRARY)
  806. list(APPEND CUDA_LIBRARIES ${CUDA_cudart_static_LIBRARY})
  807. if (TARGET Threads::Threads)
  808. list(APPEND CUDA_LIBRARIES Threads::Threads)
  809. endif()
  810. list(APPEND CUDA_LIBRARIES ${CMAKE_DL_LIBS})
  811. if (CUDA_rt_LIBRARY)
  812. list(APPEND CUDA_LIBRARIES ${CUDA_rt_LIBRARY})
  813. endif()
  814. if(APPLE)
  815. # We need to add the default path to the driver (libcuda.dylib) as an rpath, so that
  816. # the static cuda runtime can find it at runtime.
  817. list(APPEND CUDA_LIBRARIES -Wl,-rpath,/usr/local/cuda/lib)
  818. endif()
  819. else()
  820. list(APPEND CUDA_LIBRARIES ${CUDA_CUDART_LIBRARY})
  821. endif()
  822. # 1.1 toolkit on linux doesn't appear to have a separate library on
  823. # some platforms.
  824. cuda_find_library_local_first(CUDA_CUDA_LIBRARY cuda "\"cuda\" library (older versions only).")
  825. mark_as_advanced(
  826. CUDA_CUDA_LIBRARY
  827. CUDA_CUDART_LIBRARY
  828. )
  829. #######################
  830. # Look for some of the toolkit helper libraries
  831. macro(FIND_CUDA_HELPER_LIBS _name)
  832. cuda_find_library_local_first(CUDA_${_name}_LIBRARY ${_name} "\"${_name}\" library")
  833. mark_as_advanced(CUDA_${_name}_LIBRARY)
  834. endmacro()
  835. #######################
  836. # Disable emulation for v3.1 onward
  837. if(CUDA_VERSION VERSION_GREATER "3.0")
  838. if(CUDA_BUILD_EMULATION)
  839. message(FATAL_ERROR "CUDA_BUILD_EMULATION is not supported in version 3.1 and onwards. You must disable it to proceed. You have version ${CUDA_VERSION}.")
  840. endif()
  841. endif()
  842. # Search for additional CUDA toolkit libraries.
  843. if(CUDA_VERSION VERSION_LESS "3.1")
  844. # Emulation libraries aren't available in version 3.1 onward.
  845. find_cuda_helper_libs(cufftemu)
  846. find_cuda_helper_libs(cublasemu)
  847. endif()
  848. find_cuda_helper_libs(cufft)
  849. find_cuda_helper_libs(cublas)
  850. if(NOT CUDA_VERSION VERSION_LESS "3.2")
  851. # cusparse showed up in version 3.2
  852. find_cuda_helper_libs(cusparse)
  853. find_cuda_helper_libs(curand)
  854. if (WIN32)
  855. find_cuda_helper_libs(nvcuvenc)
  856. find_cuda_helper_libs(nvcuvid)
  857. endif()
  858. endif()
  859. if(CUDA_VERSION VERSION_GREATER "5.0" AND CUDA_VERSION VERSION_LESS "9.2")
  860. # In CUDA 9.2 cublas_device was deprecated
  861. find_cuda_helper_libs(cublas_device)
  862. endif()
  863. if(NOT CUDA_VERSION VERSION_LESS "5.0")
  864. find_cuda_helper_libs(nvToolsExt)
  865. find_cuda_helper_libs(OpenCL)
  866. endif()
  867. if(NOT CUDA_VERSION VERSION_LESS "9.0")
  868. # In CUDA 9.0 NPP was nppi was removed
  869. find_cuda_helper_libs(nppc)
  870. find_cuda_helper_libs(nppial)
  871. find_cuda_helper_libs(nppicc)
  872. find_cuda_helper_libs(nppicom)
  873. find_cuda_helper_libs(nppidei)
  874. find_cuda_helper_libs(nppif)
  875. find_cuda_helper_libs(nppig)
  876. find_cuda_helper_libs(nppim)
  877. find_cuda_helper_libs(nppist)
  878. find_cuda_helper_libs(nppisu)
  879. find_cuda_helper_libs(nppitc)
  880. find_cuda_helper_libs(npps)
  881. set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")
  882. elseif(CUDA_VERSION VERSION_GREATER "5.0")
  883. # In CUDA 5.5 NPP was split into 3 separate libraries.
  884. find_cuda_helper_libs(nppc)
  885. find_cuda_helper_libs(nppi)
  886. find_cuda_helper_libs(npps)
  887. set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")
  888. elseif(NOT CUDA_VERSION VERSION_LESS "4.0")
  889. find_cuda_helper_libs(npp)
  890. endif()
  891. if(NOT CUDA_VERSION VERSION_LESS "7.0")
  892. # cusolver showed up in version 7.0
  893. find_cuda_helper_libs(cusolver)
  894. endif()
  895. if (CUDA_BUILD_EMULATION)
  896. set(CUDA_CUFFT_LIBRARIES ${CUDA_cufftemu_LIBRARY})
  897. set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublasemu_LIBRARY})
  898. else()
  899. set(CUDA_CUFFT_LIBRARIES ${CUDA_cufft_LIBRARY})
  900. set(CUDA_CUBLAS_LIBRARIES ${CUDA_cublas_LIBRARY} ${CUDA_cublas_device_LIBRARY})
  901. endif()
  902. ########################
  903. # Look for the SDK stuff. As of CUDA 3.0 NVSDKCUDA_ROOT has been replaced with
  904. # NVSDKCOMPUTE_ROOT with the old CUDA C contents moved into the C subdirectory
  905. find_path(CUDA_SDK_ROOT_DIR common/inc/cutil.h
  906. HINTS
  907. "$ENV{NVSDKCOMPUTE_ROOT}/C"
  908. ENV NVSDKCUDA_ROOT
  909. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Installed Products\\NVIDIA SDK 10\\Compute;InstallDir]"
  910. PATHS
  911. "/Developer/GPU\ Computing/C"
  912. )
  913. # Keep the CUDA_SDK_ROOT_DIR first in order to be able to override the
  914. # environment variables.
  915. set(CUDA_SDK_SEARCH_PATH
  916. "${CUDA_SDK_ROOT_DIR}"
  917. "${CUDA_TOOLKIT_ROOT_DIR}/local/NVSDK0.2"
  918. "${CUDA_TOOLKIT_ROOT_DIR}/NVSDK0.2"
  919. "${CUDA_TOOLKIT_ROOT_DIR}/NV_CUDA_SDK"
  920. "$ENV{HOME}/NVIDIA_CUDA_SDK"
  921. "$ENV{HOME}/NVIDIA_CUDA_SDK_MACOSX"
  922. "/Developer/CUDA"
  923. )
  924. # Example of how to find an include file from the CUDA_SDK_ROOT_DIR
  925. # find_path(CUDA_CUT_INCLUDE_DIR
  926. # cutil.h
  927. # PATHS ${CUDA_SDK_SEARCH_PATH}
  928. # PATH_SUFFIXES "common/inc"
  929. # DOC "Location of cutil.h"
  930. # NO_DEFAULT_PATH
  931. # )
  932. # # Now search system paths
  933. # find_path(CUDA_CUT_INCLUDE_DIR cutil.h DOC "Location of cutil.h")
  934. # mark_as_advanced(CUDA_CUT_INCLUDE_DIR)
  935. # Example of how to find a library in the CUDA_SDK_ROOT_DIR
  936. # # cutil library is called cutil64 for 64 bit builds on windows. We don't want
  937. # # to get these confused, so we are setting the name based on the word size of
  938. # # the build.
  939. # if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  940. # set(cuda_cutil_name cutil64)
  941. # else()
  942. # set(cuda_cutil_name cutil32)
  943. # endif()
  944. # find_library(CUDA_CUT_LIBRARY
  945. # NAMES cutil ${cuda_cutil_name}
  946. # PATHS ${CUDA_SDK_SEARCH_PATH}
  947. # # The new version of the sdk shows up in common/lib, but the old one is in lib
  948. # PATH_SUFFIXES "common/lib" "lib"
  949. # DOC "Location of cutil library"
  950. # NO_DEFAULT_PATH
  951. # )
  952. # # Now search system paths
  953. # find_library(CUDA_CUT_LIBRARY NAMES cutil ${cuda_cutil_name} DOC "Location of cutil library")
  954. # mark_as_advanced(CUDA_CUT_LIBRARY)
  955. # set(CUDA_CUT_LIBRARIES ${CUDA_CUT_LIBRARY})
  956. #############################
  957. # Check for required components
  958. set(CUDA_FOUND TRUE)
  959. set(CUDA_TOOLKIT_ROOT_DIR_INTERNAL "${CUDA_TOOLKIT_ROOT_DIR}" CACHE INTERNAL
  960. "This is the value of the last time CUDA_TOOLKIT_ROOT_DIR was set successfully." FORCE)
  961. set(CUDA_TOOLKIT_TARGET_DIR_INTERNAL "${CUDA_TOOLKIT_TARGET_DIR}" CACHE INTERNAL
  962. "This is the value of the last time CUDA_TOOLKIT_TARGET_DIR was set successfully." FORCE)
  963. set(CUDA_SDK_ROOT_DIR_INTERNAL "${CUDA_SDK_ROOT_DIR}" CACHE INTERNAL
  964. "This is the value of the last time CUDA_SDK_ROOT_DIR was set successfully." FORCE)
  965. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  966. find_package_handle_standard_args(CUDA
  967. REQUIRED_VARS
  968. CUDA_TOOLKIT_ROOT_DIR
  969. CUDA_NVCC_EXECUTABLE
  970. CUDA_INCLUDE_DIRS
  971. ${CUDA_CUDART_LIBRARY_VAR}
  972. VERSION_VAR
  973. CUDA_VERSION
  974. )
  975. ###############################################################################
  976. ###############################################################################
  977. # Macros
  978. ###############################################################################
  979. ###############################################################################
  980. ###############################################################################
  981. # Add include directories to pass to the nvcc command.
  982. macro(CUDA_INCLUDE_DIRECTORIES)
  983. foreach(dir ${ARGN})
  984. list(APPEND CUDA_NVCC_INCLUDE_DIRS_USER ${dir})
  985. endforeach()
  986. endmacro()
  987. ##############################################################################
  988. cuda_find_helper_file(parse_cubin cmake)
  989. cuda_find_helper_file(make2cmake cmake)
  990. cuda_find_helper_file(run_nvcc cmake)
  991. include("${CMAKE_CURRENT_LIST_DIR}/FindCUDA/select_compute_arch.cmake")
  992. ##############################################################################
  993. # Separate the OPTIONS out from the sources
  994. #
  995. macro(CUDA_GET_SOURCES_AND_OPTIONS _sources _cmake_options _options)
  996. set( ${_sources} )
  997. set( ${_cmake_options} )
  998. set( ${_options} )
  999. set( _found_options FALSE )
  1000. foreach(arg ${ARGN})
  1001. if("x${arg}" STREQUAL "xOPTIONS")
  1002. set( _found_options TRUE )
  1003. elseif(
  1004. "x${arg}" STREQUAL "xWIN32" OR
  1005. "x${arg}" STREQUAL "xMACOSX_BUNDLE" OR
  1006. "x${arg}" STREQUAL "xEXCLUDE_FROM_ALL" OR
  1007. "x${arg}" STREQUAL "xSTATIC" OR
  1008. "x${arg}" STREQUAL "xSHARED" OR
  1009. "x${arg}" STREQUAL "xMODULE"
  1010. )
  1011. list(APPEND ${_cmake_options} ${arg})
  1012. else()
  1013. if ( _found_options )
  1014. list(APPEND ${_options} ${arg})
  1015. else()
  1016. # Assume this is a file
  1017. list(APPEND ${_sources} ${arg})
  1018. endif()
  1019. endif()
  1020. endforeach()
  1021. endmacro()
  1022. ##############################################################################
  1023. # Parse the OPTIONS from ARGN and set the variables prefixed by _option_prefix
  1024. #
  1025. macro(CUDA_PARSE_NVCC_OPTIONS _option_prefix)
  1026. set( _found_config )
  1027. foreach(arg ${ARGN})
  1028. # Determine if we are dealing with a perconfiguration flag
  1029. foreach(config ${CUDA_configuration_types})
  1030. string(TOUPPER ${config} config_upper)
  1031. if (arg STREQUAL "${config_upper}")
  1032. set( _found_config _${arg})
  1033. # Set arg to nothing to keep it from being processed further
  1034. set( arg )
  1035. endif()
  1036. endforeach()
  1037. if ( arg )
  1038. list(APPEND ${_option_prefix}${_found_config} "${arg}")
  1039. endif()
  1040. endforeach()
  1041. endmacro()
  1042. ##############################################################################
  1043. # Helper to add the include directory for CUDA only once
  1044. function(CUDA_ADD_CUDA_INCLUDE_ONCE)
  1045. get_directory_property(_include_directories INCLUDE_DIRECTORIES)
  1046. set(_add TRUE)
  1047. if(_include_directories)
  1048. foreach(dir ${_include_directories})
  1049. if("${dir}" STREQUAL "${CUDA_INCLUDE_DIRS}")
  1050. set(_add FALSE)
  1051. endif()
  1052. endforeach()
  1053. endif()
  1054. if(_add)
  1055. include_directories(${CUDA_INCLUDE_DIRS})
  1056. endif()
  1057. endfunction()
  1058. function(CUDA_BUILD_SHARED_LIBRARY shared_flag)
  1059. set(cmake_args ${ARGN})
  1060. # If SHARED, MODULE, or STATIC aren't already in the list of arguments, then
  1061. # add SHARED or STATIC based on the value of BUILD_SHARED_LIBS.
  1062. list(FIND cmake_args SHARED _cuda_found_SHARED)
  1063. list(FIND cmake_args MODULE _cuda_found_MODULE)
  1064. list(FIND cmake_args STATIC _cuda_found_STATIC)
  1065. if( _cuda_found_SHARED GREATER -1 OR
  1066. _cuda_found_MODULE GREATER -1 OR
  1067. _cuda_found_STATIC GREATER -1)
  1068. set(_cuda_build_shared_libs)
  1069. else()
  1070. if (BUILD_SHARED_LIBS)
  1071. set(_cuda_build_shared_libs SHARED)
  1072. else()
  1073. set(_cuda_build_shared_libs STATIC)
  1074. endif()
  1075. endif()
  1076. set(${shared_flag} ${_cuda_build_shared_libs} PARENT_SCOPE)
  1077. endfunction()
  1078. ##############################################################################
  1079. # Helper to avoid clashes of files with the same basename but different paths.
  1080. # This doesn't attempt to do exactly what CMake internals do, which is to only
  1081. # add this path when there is a conflict, since by the time a second collision
  1082. # in names is detected it's already too late to fix the first one. For
  1083. # consistency sake the relative path will be added to all files.
  1084. function(CUDA_COMPUTE_BUILD_PATH path build_path)
  1085. #message("CUDA_COMPUTE_BUILD_PATH([${path}] ${build_path})")
  1086. # Only deal with CMake style paths from here on out
  1087. file(TO_CMAKE_PATH "${path}" bpath)
  1088. if (IS_ABSOLUTE "${bpath}")
  1089. # Absolute paths are generally unnessary, especially if something like
  1090. # file(GLOB_RECURSE) is used to pick up the files.
  1091. string(FIND "${bpath}" "${CMAKE_CURRENT_BINARY_DIR}" _binary_dir_pos)
  1092. if (_binary_dir_pos EQUAL 0)
  1093. file(RELATIVE_PATH bpath "${CMAKE_CURRENT_BINARY_DIR}" "${bpath}")
  1094. else()
  1095. file(RELATIVE_PATH bpath "${CMAKE_CURRENT_SOURCE_DIR}" "${bpath}")
  1096. endif()
  1097. endif()
  1098. # This recipe is from cmLocalGenerator::CreateSafeUniqueObjectFileName in the
  1099. # CMake source.
  1100. # Remove leading /
  1101. string(REGEX REPLACE "^[/]+" "" bpath "${bpath}")
  1102. # Avoid absolute paths by removing ':'
  1103. string(REPLACE ":" "_" bpath "${bpath}")
  1104. # Avoid relative paths that go up the tree
  1105. string(REPLACE "../" "__/" bpath "${bpath}")
  1106. # Avoid spaces
  1107. string(REPLACE " " "_" bpath "${bpath}")
  1108. # Strip off the filename. I wait until here to do it, since removin the
  1109. # basename can make a path that looked like path/../basename turn into
  1110. # path/.. (notice the trailing slash).
  1111. get_filename_component(bpath "${bpath}" PATH)
  1112. set(${build_path} "${bpath}" PARENT_SCOPE)
  1113. #message("${build_path} = ${bpath}")
  1114. endfunction()
  1115. ##############################################################################
  1116. # This helper macro populates the following variables and setups up custom
  1117. # commands and targets to invoke the nvcc compiler to generate C or PTX source
  1118. # dependent upon the format parameter. The compiler is invoked once with -M
  1119. # to generate a dependency file and a second time with -cuda or -ptx to generate
  1120. # a .cpp or .ptx file.
  1121. # INPUT:
  1122. # cuda_target - Target name
  1123. # format - PTX, CUBIN, FATBIN or OBJ
  1124. # FILE1 .. FILEN - The remaining arguments are the sources to be wrapped.
  1125. # OPTIONS - Extra options to NVCC
  1126. # OUTPUT:
  1127. # generated_files - List of generated files
  1128. ##############################################################################
  1129. ##############################################################################
  1130. macro(CUDA_WRAP_SRCS cuda_target format generated_files)
  1131. # Put optional arguments in list.
  1132. set(_argn_list "${ARGN}")
  1133. # If one of the given optional arguments is "PHONY", make a note of it, then
  1134. # remove it from the list.
  1135. list(FIND _argn_list "PHONY" _phony_idx)
  1136. if("${_phony_idx}" GREATER "-1")
  1137. set(_target_is_phony true)
  1138. list(REMOVE_AT _argn_list ${_phony_idx})
  1139. else()
  1140. set(_target_is_phony false)
  1141. endif()
  1142. # If CMake doesn't support separable compilation, complain
  1143. if(CUDA_SEPARABLE_COMPILATION AND CMAKE_VERSION VERSION_LESS "2.8.10.1")
  1144. message(SEND_ERROR "CUDA_SEPARABLE_COMPILATION isn't supported for CMake versions less than 2.8.10.1")
  1145. endif()
  1146. # Set up all the command line flags here, so that they can be overridden on a per target basis.
  1147. set(nvcc_flags "")
  1148. # Emulation if the card isn't present.
  1149. if (CUDA_BUILD_EMULATION)
  1150. # Emulation.
  1151. set(nvcc_flags ${nvcc_flags} --device-emulation -D_DEVICEEMU -g)
  1152. else()
  1153. # Device mode. No flags necessary.
  1154. endif()
  1155. if(CUDA_HOST_COMPILATION_CPP)
  1156. set(CUDA_C_OR_CXX CXX)
  1157. else()
  1158. if(CUDA_VERSION VERSION_LESS "3.0")
  1159. set(nvcc_flags ${nvcc_flags} --host-compilation C)
  1160. else()
  1161. message(WARNING "--host-compilation flag is deprecated in CUDA version >= 3.0. Removing --host-compilation C flag" )
  1162. endif()
  1163. set(CUDA_C_OR_CXX C)
  1164. endif()
  1165. set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
  1166. if(CUDA_64_BIT_DEVICE_CODE)
  1167. set(nvcc_flags ${nvcc_flags} -m64)
  1168. else()
  1169. set(nvcc_flags ${nvcc_flags} -m32)
  1170. endif()
  1171. if(CUDA_TARGET_CPU_ARCH)
  1172. set(nvcc_flags ${nvcc_flags} "--target-cpu-architecture=${CUDA_TARGET_CPU_ARCH}")
  1173. endif()
  1174. # This needs to be passed in at this stage, because VS needs to fill out the
  1175. # various macros from within VS. Note that CCBIN is only used if
  1176. # -ccbin or --compiler-bindir isn't used and CUDA_HOST_COMPILER matches
  1177. # _CUDA_MSVC_HOST_COMPILER
  1178. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  1179. set(ccbin_flags -D "\"CCBIN:PATH=${_CUDA_MSVC_HOST_COMPILER}\"" )
  1180. else()
  1181. set(ccbin_flags)
  1182. endif()
  1183. # Figure out which configure we will use and pass that in as an argument to
  1184. # the script. We need to defer the decision until compilation time, because
  1185. # for VS projects we won't know if we are making a debug or release build
  1186. # until build time.
  1187. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  1188. set( CUDA_build_configuration "$(ConfigurationName)" )
  1189. else()
  1190. set( CUDA_build_configuration "${CMAKE_BUILD_TYPE}")
  1191. endif()
  1192. # Initialize our list of includes with the user ones followed by the CUDA system ones.
  1193. set(CUDA_NVCC_INCLUDE_DIRS ${CUDA_NVCC_INCLUDE_DIRS_USER} "${CUDA_INCLUDE_DIRS}")
  1194. if(_target_is_phony)
  1195. # If the passed in target name isn't a real target (i.e., this is from a call to one of the
  1196. # cuda_compile_* functions), need to query directory properties to get include directories
  1197. # and compile definitions.
  1198. get_directory_property(_dir_include_dirs INCLUDE_DIRECTORIES)
  1199. get_directory_property(_dir_compile_defs COMPILE_DEFINITIONS)
  1200. list(APPEND CUDA_NVCC_INCLUDE_DIRS "${_dir_include_dirs}")
  1201. set(CUDA_NVCC_COMPILE_DEFINITIONS "${_dir_compile_defs}")
  1202. else()
  1203. # Append the include directories for this target via generator expression, which is
  1204. # expanded by the FILE(GENERATE) call below. This generator expression captures all
  1205. # include dirs set by the user, whether via directory properties or target properties
  1206. list(APPEND CUDA_NVCC_INCLUDE_DIRS "$<TARGET_PROPERTY:${cuda_target},INCLUDE_DIRECTORIES>")
  1207. # Do the same thing with compile definitions
  1208. set(CUDA_NVCC_COMPILE_DEFINITIONS "$<TARGET_PROPERTY:${cuda_target},COMPILE_DEFINITIONS>")
  1209. endif()
  1210. # Reset these variables
  1211. set(CUDA_WRAP_OPTION_NVCC_FLAGS)
  1212. foreach(config ${CUDA_configuration_types})
  1213. string(TOUPPER ${config} config_upper)
  1214. set(CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper})
  1215. endforeach()
  1216. CUDA_GET_SOURCES_AND_OPTIONS(_cuda_wrap_sources _cuda_wrap_cmake_options _cuda_wrap_options ${_argn_list})
  1217. CUDA_PARSE_NVCC_OPTIONS(CUDA_WRAP_OPTION_NVCC_FLAGS ${_cuda_wrap_options})
  1218. # Figure out if we are building a shared library. BUILD_SHARED_LIBS is
  1219. # respected in CUDA_ADD_LIBRARY.
  1220. set(_cuda_build_shared_libs FALSE)
  1221. # SHARED, MODULE
  1222. list(FIND _cuda_wrap_cmake_options SHARED _cuda_found_SHARED)
  1223. list(FIND _cuda_wrap_cmake_options MODULE _cuda_found_MODULE)
  1224. if(_cuda_found_SHARED GREATER -1 OR _cuda_found_MODULE GREATER -1)
  1225. set(_cuda_build_shared_libs TRUE)
  1226. endif()
  1227. # STATIC
  1228. list(FIND _cuda_wrap_cmake_options STATIC _cuda_found_STATIC)
  1229. if(_cuda_found_STATIC GREATER -1)
  1230. set(_cuda_build_shared_libs FALSE)
  1231. endif()
  1232. # CUDA_HOST_FLAGS
  1233. if(_cuda_build_shared_libs)
  1234. # If we are setting up code for a shared library, then we need to add extra flags for
  1235. # compiling objects for shared libraries.
  1236. set(CUDA_HOST_SHARED_FLAGS ${CMAKE_SHARED_LIBRARY_${CUDA_C_OR_CXX}_FLAGS})
  1237. else()
  1238. set(CUDA_HOST_SHARED_FLAGS)
  1239. endif()
  1240. # Only add the CMAKE_{C,CXX}_FLAGS if we are propagating host flags. We
  1241. # always need to set the SHARED_FLAGS, though.
  1242. if(CUDA_PROPAGATE_HOST_FLAGS)
  1243. set(_cuda_host_flags "set(CMAKE_HOST_FLAGS ${CMAKE_${CUDA_C_OR_CXX}_FLAGS} ${CUDA_HOST_SHARED_FLAGS})")
  1244. else()
  1245. set(_cuda_host_flags "set(CMAKE_HOST_FLAGS ${CUDA_HOST_SHARED_FLAGS})")
  1246. endif()
  1247. set(_cuda_nvcc_flags_config "# Build specific configuration flags")
  1248. # Loop over all the configuration types to generate appropriate flags for run_nvcc.cmake
  1249. foreach(config ${CUDA_configuration_types})
  1250. string(TOUPPER ${config} config_upper)
  1251. # CMAKE_FLAGS are strings and not lists. By not putting quotes around CMAKE_FLAGS
  1252. # we convert the strings to lists (like we want).
  1253. if(CUDA_PROPAGATE_HOST_FLAGS)
  1254. # nvcc chokes on -g3 in versions previous to 3.0, so replace it with -g
  1255. set(_cuda_fix_g3 FALSE)
  1256. if(CMAKE_COMPILER_IS_GNUCC)
  1257. if (CUDA_VERSION VERSION_LESS "3.0" OR
  1258. CUDA_VERSION VERSION_EQUAL "4.1" OR
  1259. CUDA_VERSION VERSION_EQUAL "4.2"
  1260. )
  1261. set(_cuda_fix_g3 TRUE)
  1262. endif()
  1263. endif()
  1264. if(_cuda_fix_g3)
  1265. string(REPLACE "-g3" "-g" _cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
  1266. else()
  1267. set(_cuda_C_FLAGS "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
  1268. endif()
  1269. string(APPEND _cuda_host_flags "\nset(CMAKE_HOST_FLAGS_${config_upper} ${_cuda_C_FLAGS})")
  1270. endif()
  1271. # Note that if we ever want CUDA_NVCC_FLAGS_<CONFIG> to be string (instead of a list
  1272. # like it is currently), we can remove the quotes around the
  1273. # ${CUDA_NVCC_FLAGS_${config_upper}} variable like the CMAKE_HOST_FLAGS_<CONFIG> variable.
  1274. string(APPEND _cuda_nvcc_flags_config "\nset(CUDA_NVCC_FLAGS_${config_upper} ${CUDA_NVCC_FLAGS_${config_upper}} ;; ${CUDA_WRAP_OPTION_NVCC_FLAGS_${config_upper}})")
  1275. endforeach()
  1276. # Process the C++11 flag. If the host sets the flag, we need to add it to nvcc and
  1277. # remove it from the host. This is because -Xcompile -std=c++ will choke nvcc (it uses
  1278. # the C preprocessor). In order to get this to work correctly, we need to use nvcc's
  1279. # specific c++11 flag.
  1280. if( "${_cuda_host_flags}" MATCHES "-std=c\\+\\+11")
  1281. # Add the c++11 flag to nvcc if it isn't already present. Note that we only look at
  1282. # the main flag instead of the configuration specific flags.
  1283. if( NOT "${CUDA_NVCC_FLAGS}" MATCHES "-std=c\\+\\+11" )
  1284. list(APPEND nvcc_flags --std c++11)
  1285. endif()
  1286. string(REGEX REPLACE "[-]+std=c\\+\\+11" "" _cuda_host_flags "${_cuda_host_flags}")
  1287. endif()
  1288. if(_cuda_build_shared_libs)
  1289. list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS")
  1290. endif()
  1291. # Reset the output variable
  1292. set(_cuda_wrap_generated_files "")
  1293. # Iterate over the macro arguments and create custom
  1294. # commands for all the .cu files.
  1295. foreach(file ${_argn_list})
  1296. # Ignore any file marked as a HEADER_FILE_ONLY
  1297. get_source_file_property(_is_header ${file} HEADER_FILE_ONLY)
  1298. # Allow per source file overrides of the format. Also allows compiling non-.cu files.
  1299. get_source_file_property(_cuda_source_format ${file} CUDA_SOURCE_PROPERTY_FORMAT)
  1300. if((${file} MATCHES "\\.cu$" OR _cuda_source_format) AND NOT _is_header)
  1301. if(NOT _cuda_source_format)
  1302. set(_cuda_source_format ${format})
  1303. endif()
  1304. # If file isn't a .cu file, we need to tell nvcc to treat it as such.
  1305. if(NOT ${file} MATCHES "\\.cu$")
  1306. set(cuda_language_flag -x=cu)
  1307. else()
  1308. set(cuda_language_flag)
  1309. endif()
  1310. if( ${_cuda_source_format} MATCHES "OBJ")
  1311. set( cuda_compile_to_external_module OFF )
  1312. else()
  1313. set( cuda_compile_to_external_module ON )
  1314. if( ${_cuda_source_format} MATCHES "PTX" )
  1315. set( cuda_compile_to_external_module_type "ptx" )
  1316. elseif( ${_cuda_source_format} MATCHES "CUBIN")
  1317. set( cuda_compile_to_external_module_type "cubin" )
  1318. elseif( ${_cuda_source_format} MATCHES "FATBIN")
  1319. set( cuda_compile_to_external_module_type "fatbin" )
  1320. else()
  1321. message( FATAL_ERROR "Invalid format flag passed to CUDA_WRAP_SRCS or set with CUDA_SOURCE_PROPERTY_FORMAT file property for file '${file}': '${_cuda_source_format}'. Use OBJ, PTX, CUBIN or FATBIN.")
  1322. endif()
  1323. endif()
  1324. if(cuda_compile_to_external_module)
  1325. # Don't use any of the host compilation flags for PTX targets.
  1326. set(CUDA_HOST_FLAGS)
  1327. set(CUDA_NVCC_FLAGS_CONFIG)
  1328. else()
  1329. set(CUDA_HOST_FLAGS ${_cuda_host_flags})
  1330. set(CUDA_NVCC_FLAGS_CONFIG ${_cuda_nvcc_flags_config})
  1331. endif()
  1332. # Determine output directory
  1333. cuda_compute_build_path("${file}" cuda_build_path)
  1334. set(cuda_compile_intermediate_directory "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${cuda_target}.dir/${cuda_build_path}")
  1335. if(CUDA_GENERATED_OUTPUT_DIR)
  1336. set(cuda_compile_output_dir "${CUDA_GENERATED_OUTPUT_DIR}")
  1337. else()
  1338. if ( cuda_compile_to_external_module )
  1339. set(cuda_compile_output_dir "${CMAKE_CURRENT_BINARY_DIR}")
  1340. else()
  1341. set(cuda_compile_output_dir "${cuda_compile_intermediate_directory}")
  1342. endif()
  1343. endif()
  1344. # Add a custom target to generate a c or ptx file. ######################
  1345. get_filename_component( basename ${file} NAME )
  1346. if( cuda_compile_to_external_module )
  1347. set(generated_file_path "${cuda_compile_output_dir}")
  1348. set(generated_file_basename "${cuda_target}_generated_${basename}.${cuda_compile_to_external_module_type}")
  1349. set(format_flag "-${cuda_compile_to_external_module_type}")
  1350. file(MAKE_DIRECTORY "${cuda_compile_output_dir}")
  1351. else()
  1352. set(generated_file_path "${cuda_compile_output_dir}/${CMAKE_CFG_INTDIR}")
  1353. set(generated_file_basename "${cuda_target}_generated_${basename}${generated_extension}")
  1354. if(CUDA_SEPARABLE_COMPILATION)
  1355. set(format_flag "-dc")
  1356. else()
  1357. set(format_flag "-c")
  1358. endif()
  1359. endif()
  1360. # Set all of our file names. Make sure that whatever filenames that have
  1361. # generated_file_path in them get passed in through as a command line
  1362. # argument, so that the ${CMAKE_CFG_INTDIR} gets expanded at run time
  1363. # instead of configure time.
  1364. set(generated_file "${generated_file_path}/${generated_file_basename}")
  1365. set(cmake_dependency_file "${cuda_compile_intermediate_directory}/${generated_file_basename}.depend")
  1366. set(NVCC_generated_dependency_file "${cuda_compile_intermediate_directory}/${generated_file_basename}.NVCC-depend")
  1367. set(generated_cubin_file "${generated_file_path}/${generated_file_basename}.cubin.txt")
  1368. set(custom_target_script_pregen "${cuda_compile_intermediate_directory}/${generated_file_basename}.cmake.pre-gen")
  1369. set(custom_target_script "${cuda_compile_intermediate_directory}/${generated_file_basename}$<$<BOOL:$<CONFIG>>:.$<CONFIG>>.cmake")
  1370. # Setup properties for obj files:
  1371. if( NOT cuda_compile_to_external_module )
  1372. set_source_files_properties("${generated_file}"
  1373. PROPERTIES
  1374. EXTERNAL_OBJECT true # This is an object file not to be compiled, but only be linked.
  1375. )
  1376. endif()
  1377. # Don't add CMAKE_CURRENT_SOURCE_DIR if the path is already an absolute path.
  1378. get_filename_component(file_path "${file}" PATH)
  1379. if(IS_ABSOLUTE "${file_path}")
  1380. set(source_file "${file}")
  1381. else()
  1382. set(source_file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
  1383. endif()
  1384. if( NOT cuda_compile_to_external_module AND CUDA_SEPARABLE_COMPILATION)
  1385. list(APPEND ${cuda_target}_SEPARABLE_COMPILATION_OBJECTS "${generated_file}")
  1386. endif()
  1387. # Bring in the dependencies. Creates a variable CUDA_NVCC_DEPEND #######
  1388. cuda_include_nvcc_dependencies(${cmake_dependency_file})
  1389. # Convenience string for output #########################################
  1390. if(CUDA_BUILD_EMULATION)
  1391. set(cuda_build_type "Emulation")
  1392. else()
  1393. set(cuda_build_type "Device")
  1394. endif()
  1395. # Build the NVCC made dependency file ###################################
  1396. set(build_cubin OFF)
  1397. if ( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
  1398. if ( NOT cuda_compile_to_external_module )
  1399. set ( build_cubin ON )
  1400. endif()
  1401. endif()
  1402. # Configure the build script
  1403. configure_file("${CUDA_run_nvcc}" "${custom_target_script_pregen}" @ONLY)
  1404. file(GENERATE
  1405. OUTPUT "${custom_target_script}"
  1406. INPUT "${custom_target_script_pregen}"
  1407. )
  1408. # So if a user specifies the same cuda file as input more than once, you
  1409. # can have bad things happen with dependencies. Here we check an option
  1410. # to see if this is the behavior they want.
  1411. if(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE)
  1412. set(main_dep MAIN_DEPENDENCY ${source_file})
  1413. else()
  1414. set(main_dep DEPENDS ${source_file})
  1415. endif()
  1416. if(CUDA_VERBOSE_BUILD)
  1417. set(verbose_output ON)
  1418. elseif(CMAKE_GENERATOR MATCHES "Makefiles")
  1419. set(verbose_output "$(VERBOSE)")
  1420. else()
  1421. set(verbose_output OFF)
  1422. endif()
  1423. # Create up the comment string
  1424. file(RELATIVE_PATH generated_file_relative_path "${CMAKE_BINARY_DIR}" "${generated_file}")
  1425. if(cuda_compile_to_external_module)
  1426. set(cuda_build_comment_string "Building NVCC ${cuda_compile_to_external_module_type} file ${generated_file_relative_path}")
  1427. else()
  1428. set(cuda_build_comment_string "Building NVCC (${cuda_build_type}) object ${generated_file_relative_path}")
  1429. endif()
  1430. set(_verbatim VERBATIM)
  1431. if(ccbin_flags MATCHES "\\$\\(VCInstallDir\\)")
  1432. set(_verbatim "")
  1433. endif()
  1434. # Build the generated file and dependency file ##########################
  1435. add_custom_command(
  1436. OUTPUT ${generated_file}
  1437. # These output files depend on the source_file and the contents of cmake_dependency_file
  1438. ${main_dep}
  1439. DEPENDS ${CUDA_NVCC_DEPEND}
  1440. DEPENDS ${custom_target_script}
  1441. # Make sure the output directory exists before trying to write to it.
  1442. COMMAND ${CMAKE_COMMAND} -E make_directory "${generated_file_path}"
  1443. COMMAND ${CMAKE_COMMAND} ARGS
  1444. -D verbose:BOOL=${verbose_output}
  1445. ${ccbin_flags}
  1446. -D build_configuration:STRING=${CUDA_build_configuration}
  1447. -D "generated_file:STRING=${generated_file}"
  1448. -D "generated_cubin_file:STRING=${generated_cubin_file}"
  1449. -P "${custom_target_script}"
  1450. WORKING_DIRECTORY "${cuda_compile_intermediate_directory}"
  1451. COMMENT "${cuda_build_comment_string}"
  1452. ${_verbatim}
  1453. )
  1454. # Make sure the build system knows the file is generated.
  1455. set_source_files_properties(${generated_file} PROPERTIES GENERATED TRUE)
  1456. list(APPEND _cuda_wrap_generated_files ${generated_file})
  1457. # Add the other files that we want cmake to clean on a cleanup ##########
  1458. list(APPEND CUDA_ADDITIONAL_CLEAN_FILES "${cmake_dependency_file}")
  1459. list(REMOVE_DUPLICATES CUDA_ADDITIONAL_CLEAN_FILES)
  1460. set(CUDA_ADDITIONAL_CLEAN_FILES ${CUDA_ADDITIONAL_CLEAN_FILES} CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
  1461. endif()
  1462. endforeach()
  1463. # Set the return parameter
  1464. set(${generated_files} ${_cuda_wrap_generated_files})
  1465. endmacro()
  1466. function(_cuda_get_important_host_flags important_flags flag_string)
  1467. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  1468. string(REGEX MATCHALL "/M[DT][d]?" flags "${flag_string}")
  1469. list(APPEND ${important_flags} ${flags})
  1470. else()
  1471. string(REGEX MATCHALL "-fPIC" flags "${flag_string}")
  1472. list(APPEND ${important_flags} ${flags})
  1473. endif()
  1474. set(${important_flags} ${${important_flags}} PARENT_SCOPE)
  1475. endfunction()
  1476. ###############################################################################
  1477. ###############################################################################
  1478. # Separable Compilation Link
  1479. ###############################################################################
  1480. ###############################################################################
  1481. # Compute the filename to be used by CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS
  1482. function(CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME output_file_var cuda_target object_files)
  1483. if (object_files)
  1484. set(generated_extension ${CMAKE_${CUDA_C_OR_CXX}_OUTPUT_EXTENSION})
  1485. set(output_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${cuda_target}.dir/${CMAKE_CFG_INTDIR}/${cuda_target}_intermediate_link${generated_extension}")
  1486. else()
  1487. set(output_file)
  1488. endif()
  1489. set(${output_file_var} "${output_file}" PARENT_SCOPE)
  1490. endfunction()
  1491. # Setup the build rule for the separable compilation intermediate link file.
  1492. function(CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS output_file cuda_target options object_files)
  1493. if (object_files)
  1494. set_source_files_properties("${output_file}"
  1495. PROPERTIES
  1496. EXTERNAL_OBJECT TRUE # This is an object file not to be compiled, but only
  1497. # be linked.
  1498. GENERATED TRUE # This file is generated during the build
  1499. )
  1500. # For now we are ignoring all the configuration specific flags.
  1501. set(nvcc_flags)
  1502. CUDA_PARSE_NVCC_OPTIONS(nvcc_flags ${options})
  1503. if(CUDA_64_BIT_DEVICE_CODE)
  1504. list(APPEND nvcc_flags -m64)
  1505. else()
  1506. list(APPEND nvcc_flags -m32)
  1507. endif()
  1508. # If -ccbin, --compiler-bindir has been specified, don't do anything. Otherwise add it here.
  1509. list( FIND nvcc_flags "-ccbin" ccbin_found0 )
  1510. list( FIND nvcc_flags "--compiler-bindir" ccbin_found1 )
  1511. if( ccbin_found0 LESS 0 AND ccbin_found1 LESS 0 AND CUDA_HOST_COMPILER )
  1512. # Match VERBATIM check below.
  1513. if(CUDA_HOST_COMPILER MATCHES "\\$\\(VCInstallDir\\)")
  1514. list(APPEND nvcc_flags -ccbin "\"${CUDA_HOST_COMPILER}\"")
  1515. else()
  1516. list(APPEND nvcc_flags -ccbin "${CUDA_HOST_COMPILER}")
  1517. endif()
  1518. endif()
  1519. # Create a list of flags specified by CUDA_NVCC_FLAGS_${CONFIG} and CMAKE_${CUDA_C_OR_CXX}_FLAGS*
  1520. set(config_specific_flags)
  1521. set(flags)
  1522. foreach(config ${CUDA_configuration_types})
  1523. string(TOUPPER ${config} config_upper)
  1524. # Add config specific flags
  1525. foreach(f ${CUDA_NVCC_FLAGS_${config_upper}})
  1526. list(APPEND config_specific_flags $<$<CONFIG:${config}>:${f}>)
  1527. endforeach()
  1528. set(important_host_flags)
  1529. _cuda_get_important_host_flags(important_host_flags "${CMAKE_${CUDA_C_OR_CXX}_FLAGS_${config_upper}}")
  1530. foreach(f ${important_host_flags})
  1531. list(APPEND flags $<$<CONFIG:${config}>:-Xcompiler> $<$<CONFIG:${config}>:${f}>)
  1532. endforeach()
  1533. endforeach()
  1534. # Add CMAKE_${CUDA_C_OR_CXX}_FLAGS
  1535. set(important_host_flags)
  1536. _cuda_get_important_host_flags(important_host_flags "${CMAKE_${CUDA_C_OR_CXX}_FLAGS}")
  1537. foreach(f ${important_host_flags})
  1538. list(APPEND flags -Xcompiler ${f})
  1539. endforeach()
  1540. # Add our general CUDA_NVCC_FLAGS with the configuration specifig flags
  1541. set(nvcc_flags ${CUDA_NVCC_FLAGS} ${config_specific_flags} ${nvcc_flags})
  1542. file(RELATIVE_PATH output_file_relative_path "${CMAKE_BINARY_DIR}" "${output_file}")
  1543. # Some generators don't handle the multiple levels of custom command
  1544. # dependencies correctly (obj1 depends on file1, obj2 depends on obj1), so
  1545. # we work around that issue by compiling the intermediate link object as a
  1546. # pre-link custom command in that situation.
  1547. set(do_obj_build_rule TRUE)
  1548. if (MSVC_VERSION GREATER 1599 AND MSVC_VERSION LESS 1800)
  1549. # VS 2010 and 2012 have this problem.
  1550. set(do_obj_build_rule FALSE)
  1551. endif()
  1552. set(_verbatim VERBATIM)
  1553. if(nvcc_flags MATCHES "\\$\\(VCInstallDir\\)")
  1554. set(_verbatim "")
  1555. endif()
  1556. if (do_obj_build_rule)
  1557. add_custom_command(
  1558. OUTPUT ${output_file}
  1559. DEPENDS ${object_files}
  1560. COMMAND ${CUDA_NVCC_EXECUTABLE} ${nvcc_flags} -dlink ${object_files} -o ${output_file}
  1561. ${flags}
  1562. COMMENT "Building NVCC intermediate link file ${output_file_relative_path}"
  1563. COMMAND_EXPAND_LISTS
  1564. ${_verbatim}
  1565. )
  1566. else()
  1567. get_filename_component(output_file_dir "${output_file}" DIRECTORY)
  1568. add_custom_command(
  1569. TARGET ${cuda_target}
  1570. PRE_LINK
  1571. COMMAND ${CMAKE_COMMAND} -E echo "Building NVCC intermediate link file ${output_file_relative_path}"
  1572. COMMAND ${CMAKE_COMMAND} -E make_directory "${output_file_dir}"
  1573. COMMAND ${CUDA_NVCC_EXECUTABLE} ${nvcc_flags} ${flags} -dlink ${object_files} -o "${output_file}"
  1574. COMMAND_EXPAND_LISTS
  1575. ${_verbatim}
  1576. )
  1577. endif()
  1578. endif()
  1579. endfunction()
  1580. ###############################################################################
  1581. ###############################################################################
  1582. # ADD LIBRARY
  1583. ###############################################################################
  1584. ###############################################################################
  1585. macro(CUDA_ADD_LIBRARY cuda_target)
  1586. CUDA_ADD_CUDA_INCLUDE_ONCE()
  1587. # Separate the sources from the options
  1588. CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
  1589. CUDA_BUILD_SHARED_LIBRARY(_cuda_shared_flag ${ARGN})
  1590. # Create custom commands and targets for each file.
  1591. CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources}
  1592. ${_cmake_options} ${_cuda_shared_flag}
  1593. OPTIONS ${_options} )
  1594. # Compute the file name of the intermedate link file used for separable
  1595. # compilation.
  1596. CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME(link_file ${cuda_target} "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
  1597. # Add the library.
  1598. add_library(${cuda_target} ${_cmake_options}
  1599. ${_generated_files}
  1600. ${_sources}
  1601. ${link_file}
  1602. )
  1603. # Add a link phase for the separable compilation if it has been enabled. If
  1604. # it has been enabled then the ${cuda_target}_SEPARABLE_COMPILATION_OBJECTS
  1605. # variable will have been defined.
  1606. CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS("${link_file}" ${cuda_target} "${_options}" "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
  1607. target_link_libraries(${cuda_target} ${CUDA_LINK_LIBRARIES_KEYWORD}
  1608. ${CUDA_LIBRARIES}
  1609. )
  1610. if(CUDA_SEPARABLE_COMPILATION)
  1611. target_link_libraries(${cuda_target} ${CUDA_LINK_LIBRARIES_KEYWORD}
  1612. ${CUDA_cudadevrt_LIBRARY}
  1613. )
  1614. endif()
  1615. # We need to set the linker language based on what the expected generated file
  1616. # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
  1617. set_target_properties(${cuda_target}
  1618. PROPERTIES
  1619. LINKER_LANGUAGE ${CUDA_C_OR_CXX}
  1620. )
  1621. endmacro()
  1622. ###############################################################################
  1623. ###############################################################################
  1624. # ADD EXECUTABLE
  1625. ###############################################################################
  1626. ###############################################################################
  1627. macro(CUDA_ADD_EXECUTABLE cuda_target)
  1628. CUDA_ADD_CUDA_INCLUDE_ONCE()
  1629. # Separate the sources from the options
  1630. CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
  1631. # Create custom commands and targets for each file.
  1632. CUDA_WRAP_SRCS( ${cuda_target} OBJ _generated_files ${_sources} OPTIONS ${_options} )
  1633. # Compute the file name of the intermedate link file used for separable
  1634. # compilation.
  1635. CUDA_COMPUTE_SEPARABLE_COMPILATION_OBJECT_FILE_NAME(link_file ${cuda_target} "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
  1636. # Add the library.
  1637. add_executable(${cuda_target} ${_cmake_options}
  1638. ${_generated_files}
  1639. ${_sources}
  1640. ${link_file}
  1641. )
  1642. # Add a link phase for the separable compilation if it has been enabled. If
  1643. # it has been enabled then the ${cuda_target}_SEPARABLE_COMPILATION_OBJECTS
  1644. # variable will have been defined.
  1645. CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS("${link_file}" ${cuda_target} "${_options}" "${${cuda_target}_SEPARABLE_COMPILATION_OBJECTS}")
  1646. target_link_libraries(${cuda_target} ${CUDA_LINK_LIBRARIES_KEYWORD}
  1647. ${CUDA_LIBRARIES}
  1648. )
  1649. # We need to set the linker language based on what the expected generated file
  1650. # would be. CUDA_C_OR_CXX is computed based on CUDA_HOST_COMPILATION_CPP.
  1651. set_target_properties(${cuda_target}
  1652. PROPERTIES
  1653. LINKER_LANGUAGE ${CUDA_C_OR_CXX}
  1654. )
  1655. endmacro()
  1656. ###############################################################################
  1657. ###############################################################################
  1658. # (Internal) helper for manually added cuda source files with specific targets
  1659. ###############################################################################
  1660. ###############################################################################
  1661. macro(cuda_compile_base cuda_target format generated_files)
  1662. # Update a counter in this directory, to keep phony target names unique.
  1663. set(_cuda_target "${cuda_target}")
  1664. get_property(_counter DIRECTORY PROPERTY _cuda_internal_phony_counter)
  1665. if(_counter)
  1666. math(EXPR _counter "${_counter} + 1")
  1667. else()
  1668. set(_counter 1)
  1669. endif()
  1670. string(APPEND _cuda_target "_${_counter}")
  1671. set_property(DIRECTORY PROPERTY _cuda_internal_phony_counter ${_counter})
  1672. # Separate the sources from the options
  1673. CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
  1674. # Create custom commands and targets for each file.
  1675. CUDA_WRAP_SRCS( ${_cuda_target} ${format} _generated_files ${_sources}
  1676. ${_cmake_options} OPTIONS ${_options} PHONY)
  1677. set( ${generated_files} ${_generated_files})
  1678. endmacro()
  1679. ###############################################################################
  1680. ###############################################################################
  1681. # CUDA COMPILE
  1682. ###############################################################################
  1683. ###############################################################################
  1684. macro(CUDA_COMPILE generated_files)
  1685. cuda_compile_base(cuda_compile OBJ ${generated_files} ${ARGN})
  1686. endmacro()
  1687. ###############################################################################
  1688. ###############################################################################
  1689. # CUDA COMPILE PTX
  1690. ###############################################################################
  1691. ###############################################################################
  1692. macro(CUDA_COMPILE_PTX generated_files)
  1693. cuda_compile_base(cuda_compile_ptx PTX ${generated_files} ${ARGN})
  1694. endmacro()
  1695. ###############################################################################
  1696. ###############################################################################
  1697. # CUDA COMPILE FATBIN
  1698. ###############################################################################
  1699. ###############################################################################
  1700. macro(CUDA_COMPILE_FATBIN generated_files)
  1701. cuda_compile_base(cuda_compile_fatbin FATBIN ${generated_files} ${ARGN})
  1702. endmacro()
  1703. ###############################################################################
  1704. ###############################################################################
  1705. # CUDA COMPILE CUBIN
  1706. ###############################################################################
  1707. ###############################################################################
  1708. macro(CUDA_COMPILE_CUBIN generated_files)
  1709. cuda_compile_base(cuda_compile_cubin CUBIN ${generated_files} ${ARGN})
  1710. endmacro()
  1711. ###############################################################################
  1712. ###############################################################################
  1713. # CUDA ADD CUFFT TO TARGET
  1714. ###############################################################################
  1715. ###############################################################################
  1716. macro(CUDA_ADD_CUFFT_TO_TARGET target)
  1717. if (CUDA_BUILD_EMULATION)
  1718. target_link_libraries(${target} ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_cufftemu_LIBRARY})
  1719. else()
  1720. target_link_libraries(${target} ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_cufft_LIBRARY})
  1721. endif()
  1722. endmacro()
  1723. ###############################################################################
  1724. ###############################################################################
  1725. # CUDA ADD CUBLAS TO TARGET
  1726. ###############################################################################
  1727. ###############################################################################
  1728. macro(CUDA_ADD_CUBLAS_TO_TARGET target)
  1729. if (CUDA_BUILD_EMULATION)
  1730. target_link_libraries(${target} ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_cublasemu_LIBRARY})
  1731. else()
  1732. target_link_libraries(${target} ${CUDA_LINK_LIBRARIES_KEYWORD} ${CUDA_cublas_LIBRARY} ${CUDA_cublas_device_LIBRARY})
  1733. endif()
  1734. endmacro()
  1735. ###############################################################################
  1736. ###############################################################################
  1737. # CUDA BUILD CLEAN TARGET
  1738. ###############################################################################
  1739. ###############################################################################
  1740. macro(CUDA_BUILD_CLEAN_TARGET)
  1741. # Call this after you add all your CUDA targets, and you will get a
  1742. # convenience target. You should also make clean after running this target
  1743. # to get the build system to generate all the code again.
  1744. set(cuda_clean_target_name clean_cuda_depends)
  1745. if (CMAKE_GENERATOR MATCHES "Visual Studio")
  1746. string(TOUPPER ${cuda_clean_target_name} cuda_clean_target_name)
  1747. endif()
  1748. add_custom_target(${cuda_clean_target_name}
  1749. COMMAND ${CMAKE_COMMAND} -E rm -f ${CUDA_ADDITIONAL_CLEAN_FILES})
  1750. # Clear out the variable, so the next time we configure it will be empty.
  1751. # This is useful so that the files won't persist in the list after targets
  1752. # have been removed.
  1753. set(CUDA_ADDITIONAL_CLEAN_FILES "" CACHE INTERNAL "List of intermediate files that are part of the cuda dependency scanning.")
  1754. endmacro()