FindCUDA.cmake 77 KB

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