FindCUDA.cmake 84 KB

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