FindCUDA.cmake 84 KB

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