CMakeCUDAInformation.cmake 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. if(UNIX)
  4. set(CMAKE_CUDA_OUTPUT_EXTENSION .o)
  5. else()
  6. set(CMAKE_CUDA_OUTPUT_EXTENSION .obj)
  7. endif()
  8. set(CMAKE_INCLUDE_FLAG_CUDA "-I")
  9. # Load compiler-specific information.
  10. if(CMAKE_CUDA_COMPILER_ID)
  11. include(Compiler/${CMAKE_CUDA_COMPILER_ID}-CUDA OPTIONAL)
  12. endif()
  13. # load the system- and compiler specific files
  14. if(CMAKE_CUDA_COMPILER_ID)
  15. # load a hardware specific file, mostly useful for embedded compilers
  16. if(CMAKE_SYSTEM_PROCESSOR)
  17. include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_CUDA_COMPILER_ID}-CUDA-${CMAKE_SYSTEM_PROCESSOR} OPTIONAL)
  18. endif()
  19. include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_CUDA_COMPILER_ID}-CUDA OPTIONAL)
  20. endif()
  21. if(NOT CMAKE_SHARED_LIBRARY_RUNTIME_CUDA_FLAG)
  22. set(CMAKE_SHARED_LIBRARY_RUNTIME_CUDA_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG})
  23. endif()
  24. if(NOT CMAKE_SHARED_LIBRARY_RUNTIME_CUDA_FLAG_SEP)
  25. set(CMAKE_SHARED_LIBRARY_RUNTIME_CUDA_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP})
  26. endif()
  27. if(NOT CMAKE_SHARED_LIBRARY_RPATH_LINK_CUDA_FLAG)
  28. set(CMAKE_SHARED_LIBRARY_RPATH_LINK_CUDA_FLAG ${CMAKE_SHARED_LIBRARY_RPATH_LINK_C_FLAG})
  29. endif()
  30. if(NOT DEFINED CMAKE_EXE_EXPORTS_CUDA_FLAG)
  31. set(CMAKE_EXE_EXPORTS_CUDA_FLAG ${CMAKE_EXE_EXPORTS_C_FLAG})
  32. endif()
  33. if(NOT DEFINED CMAKE_SHARED_LIBRARY_SONAME_CUDA_FLAG)
  34. set(CMAKE_SHARED_LIBRARY_SONAME_CUDA_FLAG ${CMAKE_SHARED_LIBRARY_SONAME_C_FLAG})
  35. endif()
  36. if(NOT CMAKE_EXECUTABLE_RUNTIME_CUDA_FLAG)
  37. set(CMAKE_EXECUTABLE_RUNTIME_CUDA_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_CUDA_FLAG})
  38. endif()
  39. if(NOT CMAKE_EXECUTABLE_RUNTIME_CUDA_FLAG_SEP)
  40. set(CMAKE_EXECUTABLE_RUNTIME_CUDA_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_CUDA_FLAG_SEP})
  41. endif()
  42. if(NOT CMAKE_EXECUTABLE_RPATH_LINK_CUDA_FLAG)
  43. set(CMAKE_EXECUTABLE_RPATH_LINK_CUDA_FLAG ${CMAKE_SHARED_LIBRARY_RPATH_LINK_CUDA_FLAG})
  44. endif()
  45. if(NOT DEFINED CMAKE_SHARED_LIBRARY_LINK_CUDA_WITH_RUNTIME_PATH)
  46. set(CMAKE_SHARED_LIBRARY_LINK_CUDA_WITH_RUNTIME_PATH ${CMAKE_SHARED_LIBRARY_LINK_C_WITH_RUNTIME_PATH})
  47. endif()
  48. # for most systems a module is the same as a shared library
  49. # so unless the variable CMAKE_MODULE_EXISTS is set just
  50. # copy the values from the LIBRARY variables
  51. if(NOT CMAKE_MODULE_EXISTS)
  52. set(CMAKE_SHARED_MODULE_CUDA_FLAGS ${CMAKE_SHARED_LIBRARY_CUDA_FLAGS})
  53. set(CMAKE_SHARED_MODULE_CREATE_CUDA_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_CUDA_FLAGS})
  54. endif()
  55. # add the flags to the cache based
  56. # on the initial values computed in the platform/*.cmake files
  57. # use _INIT variables so that this only happens the first time
  58. # and you can set these flags in the cmake cache
  59. set(CMAKE_CUDA_FLAGS_INIT "$ENV{CUDAFLAGS} ${CMAKE_CUDA_FLAGS_INIT}")
  60. cmake_initialize_per_config_variable(CMAKE_CUDA_FLAGS "Flags used by the CUDA compiler")
  61. if(CMAKE_CUDA_STANDARD_LIBRARIES_INIT)
  62. set(CMAKE_CUDA_STANDARD_LIBRARIES "${CMAKE_CUDA_STANDARD_LIBRARIES_INIT}"
  63. CACHE STRING "Libraries linked by default with all CUDA applications.")
  64. mark_as_advanced(CMAKE_CUDA_STANDARD_LIBRARIES)
  65. endif()
  66. include(CMakeCommonLanguageInclude)
  67. # now define the following rules:
  68. # CMAKE_CUDA_CREATE_SHARED_LIBRARY
  69. # CMAKE_CUDA_CREATE_SHARED_MODULE
  70. # CMAKE_CUDA_COMPILE_WHOLE_COMPILATION
  71. # CMAKE_CUDA_COMPILE_PTX_COMPILATION
  72. # CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION
  73. # CMAKE_CUDA_LINK_EXECUTABLE
  74. if(CMAKE_CUDA_HOST_COMPILER)
  75. set(CMAKE_CUDA_HOST_FLAGS "-ccbin=<CMAKE_CUDA_HOST_COMPILER>")
  76. else()
  77. set(CMAKE_CUDA_HOST_FLAGS "")
  78. endif()
  79. set(__IMPLICT_LINKS )
  80. foreach(dir ${CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES})
  81. string(APPEND __IMPLICT_LINKS " -L\"${dir}\"")
  82. endforeach()
  83. foreach(lib ${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES})
  84. if(${lib} MATCHES "/")
  85. string(APPEND __IMPLICT_LINKS " \"${lib}\"")
  86. else()
  87. string(APPEND __IMPLICT_LINKS " -l${lib}")
  88. endif()
  89. endforeach()
  90. # create a shared library
  91. if(NOT CMAKE_CUDA_CREATE_SHARED_LIBRARY)
  92. set(CMAKE_CUDA_CREATE_SHARED_LIBRARY
  93. "<CMAKE_CUDA_HOST_LINK_LAUNCHER> <CMAKE_SHARED_LIBRARY_CUDA_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CUDA_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>${__IMPLICT_LINKS}")
  94. endif()
  95. # create a shared module copy the shared library rule by default
  96. if(NOT CMAKE_CUDA_CREATE_SHARED_MODULE)
  97. set(CMAKE_CUDA_CREATE_SHARED_MODULE ${CMAKE_CUDA_CREATE_SHARED_LIBRARY})
  98. endif()
  99. # Create a static archive incrementally for large object file counts.
  100. if(NOT DEFINED CMAKE_CUDA_ARCHIVE_CREATE)
  101. set(CMAKE_CUDA_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")
  102. endif()
  103. if(NOT DEFINED CMAKE_CUDA_ARCHIVE_APPEND)
  104. set(CMAKE_CUDA_ARCHIVE_APPEND "<CMAKE_AR> q <TARGET> <LINK_FLAGS> <OBJECTS>")
  105. endif()
  106. if(NOT DEFINED CMAKE_CUDA_ARCHIVE_FINISH)
  107. set(CMAKE_CUDA_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>")
  108. endif()
  109. #Specify how to compile when ptx has been requested
  110. if(NOT CMAKE_CUDA_COMPILE_PTX_COMPILATION)
  111. set(CMAKE_CUDA_COMPILE_PTX_COMPILATION
  112. "<CMAKE_CUDA_COMPILER> ${CMAKE_CUDA_HOST_FLAGS} <DEFINES> <INCLUDES> <FLAGS> -x cu -ptx <SOURCE> -o <OBJECT>")
  113. endif()
  114. #Specify how to compile when separable compilation has been requested
  115. if(NOT CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION)
  116. set(CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION
  117. "<CMAKE_CUDA_COMPILER> ${CMAKE_CUDA_HOST_FLAGS} <DEFINES> <INCLUDES> <FLAGS> -x cu -dc <SOURCE> -o <OBJECT>")
  118. endif()
  119. #Specify how to compile when whole compilation has been requested
  120. if(NOT CMAKE_CUDA_COMPILE_WHOLE_COMPILATION)
  121. set(CMAKE_CUDA_COMPILE_WHOLE_COMPILATION
  122. "<CMAKE_CUDA_COMPILER> ${CMAKE_CUDA_HOST_FLAGS} <DEFINES> <INCLUDES> <FLAGS> -x cu -c <SOURCE> -o <OBJECT>")
  123. endif()
  124. if(CMAKE_GENERATOR STREQUAL "Ninja")
  125. set(CMAKE_CUDA_COMPILE_DEPENDENCY_DETECTION
  126. "<CMAKE_CUDA_COMPILER> ${CMAKE_CUDA_HOST_FLAGS} <DEFINES> <INCLUDES> <FLAGS> -x cu -M <SOURCE> -MT <OBJECT> -o $DEP_FILE")
  127. #The Ninja generator uses the make file dependency files to determine what
  128. #files need to be recompiled. Unfortunately, nvcc doesn't support building
  129. #a source file and generating the dependencies of said file in a single
  130. #invocation. Instead we have to state that you need to chain two commands.
  131. #
  132. #The makefile generators uses the custom CMake dependency scanner, and thus
  133. #it is exempt from this logic.
  134. list(APPEND CMAKE_CUDA_COMPILE_PTX_COMPILATION "${CMAKE_CUDA_COMPILE_DEPENDENCY_DETECTION}")
  135. list(APPEND CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION "${CMAKE_CUDA_COMPILE_DEPENDENCY_DETECTION}")
  136. list(APPEND CMAKE_CUDA_COMPILE_WHOLE_COMPILATION "${CMAKE_CUDA_COMPILE_DEPENDENCY_DETECTION}")
  137. endif()
  138. # compile a cu file into an executable
  139. if(NOT CMAKE_CUDA_LINK_EXECUTABLE)
  140. set(CMAKE_CUDA_LINK_EXECUTABLE
  141. "<CMAKE_CUDA_HOST_LINK_LAUNCHER> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>${__IMPLICT_LINKS}")
  142. endif()
  143. if( CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA" AND
  144. CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL "8.0.0")
  145. set(_CMAKE_CUDA_EXTRA_DEVICE_LINK_FLAGS "-Wno-deprecated-gpu-targets")
  146. else()
  147. set(_CMAKE_CUDA_EXTRA_DEVICE_LINK_FLAGS "")
  148. endif()
  149. # Add implicit host link directories that contain device libraries
  150. # to the device link line.
  151. set(__IMPLICT_DLINK_DIRS ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})
  152. if(__IMPLICT_DLINK_DIRS)
  153. list(REMOVE_ITEM __IMPLICT_DLINK_DIRS ${CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES})
  154. endif()
  155. set(__IMPLICT_DLINK_FLAGS )
  156. foreach(dir ${__IMPLICT_DLINK_DIRS})
  157. if(EXISTS "${dir}/libcurand_static.a")
  158. string(APPEND __IMPLICT_DLINK_FLAGS " -L\"${dir}\"")
  159. endif()
  160. endforeach()
  161. unset(__IMPLICT_DLINK_DIRS)
  162. #These are used when linking relocatable (dc) cuda code
  163. if(NOT CMAKE_CUDA_DEVICE_LINK_LIBRARY)
  164. set(CMAKE_CUDA_DEVICE_LINK_LIBRARY
  165. "<CMAKE_CUDA_COMPILER> ${CMAKE_CUDA_HOST_FLAGS} <LANGUAGE_COMPILE_FLAGS> ${CMAKE_CUDA_COMPILE_OPTIONS_PIC} ${_CMAKE_CUDA_EXTRA_DEVICE_LINK_FLAGS} -shared -dlink <OBJECTS> -o <TARGET> <LINK_LIBRARIES>${__IMPLICT_DLINK_FLAGS}")
  166. endif()
  167. if(NOT CMAKE_CUDA_DEVICE_LINK_EXECUTABLE)
  168. set(CMAKE_CUDA_DEVICE_LINK_EXECUTABLE
  169. "<CMAKE_CUDA_COMPILER> ${CMAKE_CUDA_HOST_FLAGS} <FLAGS> ${CMAKE_CUDA_COMPILE_OPTIONS_PIC} ${_CMAKE_CUDA_EXTRA_DEVICE_LINK_FLAGS} -shared -dlink <OBJECTS> -o <TARGET> <LINK_LIBRARIES>${__IMPLICT_DLINK_FLAGS}")
  170. endif()
  171. unset(_CMAKE_CUDA_EXTRA_DEVICE_LINK_FLAGS)
  172. unset(__IMPLICT_DLINK_FLAGS)
  173. set(CMAKE_CUDA_INFORMATION_LOADED 1)