FindCUDA.cmake 84 KB

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