FindCUDA.cmake 70 KB

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