CMakeNVCCParseImplicitInfo.cmake 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. macro(cmake_nvcc_parse_implicit_info lang lang_var_)
  4. set(_nvcc_log "")
  5. string(REPLACE "\r" "" _nvcc_output_orig "${CMAKE_${lang}_COMPILER_PRODUCED_OUTPUT}")
  6. if(_nvcc_output_orig MATCHES "#\\\$ +PATH= *([^\n]*)\n")
  7. set(_nvcc_path "${CMAKE_MATCH_1}")
  8. string(APPEND _nvcc_log " found 'PATH=' string: [${_nvcc_path}]\n")
  9. string(REPLACE ":" ";" _nvcc_path "${_nvcc_path}")
  10. else()
  11. set(_nvcc_path "")
  12. string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
  13. string(APPEND _nvcc_log " no 'PATH=' string found in nvcc output:${_nvcc_output_log}\n")
  14. endif()
  15. if(_nvcc_output_orig MATCHES "#\\\$ +LIBRARIES= *([^\n]*)\n")
  16. set(_nvcc_libraries "${CMAKE_MATCH_1}")
  17. string(APPEND _nvcc_log " found 'LIBRARIES=' string: [${_nvcc_libraries}]\n")
  18. else()
  19. set(_nvcc_libraries "")
  20. string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
  21. string(APPEND _nvcc_log " no 'LIBRARIES=' string found in nvcc output:${_nvcc_output_log}\n")
  22. endif()
  23. if(_nvcc_output_orig MATCHES "#\\\$ +INCLUDES= *([^\n]*)\n")
  24. set(_nvcc_includes "${CMAKE_MATCH_1}")
  25. string(APPEND _nvcc_log " found 'INCLUDES=' string: [${_nvcc_includes}]\n")
  26. else()
  27. set(_nvcc_includes "")
  28. string(REPLACE "\n" "\n " _nvcc_output_log "\n${_nvcc_output_orig}")
  29. string(APPEND _nvcc_log " no 'INCLUDES=' string found in nvcc output:${_nvcc_output_log}\n")
  30. endif()
  31. string(REGEX MATCHALL "-arch compute_([0-9]+)" _nvcc_target_cpus "${_nvcc_output_orig}")
  32. foreach(_nvcc_target_cpu ${_nvcc_target_cpus})
  33. if(_nvcc_target_cpu MATCHES "-arch compute_([0-9]+)")
  34. list(APPEND CMAKE_${lang}_ARCHITECTURES_DEFAULT "${CMAKE_MATCH_1}")
  35. endif()
  36. endforeach()
  37. set(_nvcc_link_line "")
  38. if(_nvcc_libraries)
  39. # Remove variable assignments.
  40. string(REGEX REPLACE "#\\\$ *[^= ]+=[^\n]*\n" "" _nvcc_output "${_nvcc_output_orig}")
  41. # Encode [] characters that break list expansion.
  42. string(REPLACE "[" "{==={" _nvcc_output "${_nvcc_output}")
  43. string(REPLACE "]" "}===}" _nvcc_output "${_nvcc_output}")
  44. # Split lines.
  45. string(REGEX REPLACE "\n+(#\\\$ )?" ";" _nvcc_output "${_nvcc_output}")
  46. foreach(line IN LISTS _nvcc_output)
  47. set(_nvcc_output_line "${line}")
  48. string(REPLACE "{==={" "[" _nvcc_output_line "${_nvcc_output_line}")
  49. string(REPLACE "}===}" "]" _nvcc_output_line "${_nvcc_output_line}")
  50. string(APPEND _nvcc_log " considering line: [${_nvcc_output_line}]\n")
  51. if("${_nvcc_output_line}" MATCHES "^ *nvlink")
  52. string(APPEND _nvcc_log " ignoring nvlink line\n")
  53. elseif("${_nvcc_output_line}" MATCHES "(link\\.exe .*CompilerId${lang}\\.exe.*)$")
  54. set(_nvcc_link_line "${CMAKE_MATCH_1}")
  55. string(APPEND _nvcc_log " extracted link line: [${_nvcc_link_line}]\n")
  56. elseif(_nvcc_libraries)
  57. if("${_nvcc_output_line}" MATCHES "(@\"?((tmp/)?a\\.exe\\.res)\"?)")
  58. set(_nvcc_link_res_arg "${CMAKE_MATCH_1}")
  59. set(_nvcc_link_res_file "${CMAKE_MATCH_2}")
  60. set(_nvcc_link_res "${CMAKE_PLATFORM_INFO_DIR}/CompilerId${lang}/${_nvcc_link_res_file}")
  61. if(EXISTS "${_nvcc_link_res}")
  62. file(READ "${_nvcc_link_res}" _nvcc_link_res_content)
  63. string(REPLACE "${_nvcc_link_res_arg}" "${_nvcc_link_res_content}" _nvcc_output_line "${_nvcc_output_line}")
  64. endif()
  65. endif()
  66. string(FIND "${_nvcc_output_line}" "${_nvcc_libraries}" _nvcc_libraries_pos)
  67. if(NOT _nvcc_libraries_pos EQUAL -1)
  68. set(_nvcc_link_line "${_nvcc_output_line}")
  69. string(APPEND _nvcc_log " extracted link line: [${_nvcc_link_line}]\n")
  70. endif()
  71. endif()
  72. endforeach()
  73. endif()
  74. if(_nvcc_link_line)
  75. if("x${CMAKE_${lang}_SIMULATE_ID}" STREQUAL "xMSVC")
  76. set(CMAKE_${lang}_HOST_LINK_LAUNCHER "${CMAKE_LINKER}")
  77. else()
  78. #extract the compiler that is being used for linking
  79. separate_arguments(_nvcc_link_line_args UNIX_COMMAND "${_nvcc_link_line}")
  80. list(GET _nvcc_link_line_args 0 _nvcc_host_link_launcher)
  81. if(IS_ABSOLUTE "${_nvcc_host_link_launcher}")
  82. string(APPEND _nvcc_log " extracted link launcher absolute path: [${_nvcc_host_link_launcher}]\n")
  83. set(CMAKE_${lang}_HOST_LINK_LAUNCHER "${_nvcc_host_link_launcher}")
  84. else()
  85. string(APPEND _nvcc_log " extracted link launcher name: [${_nvcc_host_link_launcher}]\n")
  86. find_program(_nvcc_find_host_link_launcher
  87. NAMES ${_nvcc_host_link_launcher}
  88. PATHS ${_nvcc_path} NO_DEFAULT_PATH)
  89. find_program(_nvcc_find_host_link_launcher
  90. NAMES ${_nvcc_host_link_launcher})
  91. if(_nvcc_find_host_link_launcher)
  92. string(APPEND _nvcc_log " found link launcher absolute path: [${_nvcc_find_host_link_launcher}]\n")
  93. set(CMAKE_${lang}_HOST_LINK_LAUNCHER "${_nvcc_find_host_link_launcher}")
  94. else()
  95. string(APPEND _nvcc_log " could not find link launcher absolute path\n")
  96. set(CMAKE_${lang}_HOST_LINK_LAUNCHER "${_nvcc_host_link_launcher}")
  97. endif()
  98. unset(_nvcc_find_host_link_launcher CACHE)
  99. endif()
  100. endif()
  101. #prefix the line with cuda-fake-ld so that implicit link info believes it is
  102. #a link line
  103. set(_nvcc_link_line "cuda-fake-ld ${_nvcc_link_line}")
  104. CMAKE_PARSE_IMPLICIT_LINK_INFO("${_nvcc_link_line}"
  105. CMAKE_${lang}_HOST_IMPLICIT_LINK_LIBRARIES
  106. CMAKE_${lang}_HOST_IMPLICIT_LINK_DIRECTORIES
  107. CMAKE_${lang}_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
  108. log
  109. "${CMAKE_${lang}_IMPLICIT_OBJECT_REGEX}"
  110. LANGUAGE ${lang})
  111. # Detect CMAKE_${lang}_RUNTIME_LIBRARY_DEFAULT from the compiler by looking at which
  112. # cudart library exists in the implicit link libraries passed to the host linker.
  113. # This is required when a project sets the cuda runtime library as part of the
  114. # initial flags.
  115. if(";${CMAKE_${lang}_HOST_IMPLICIT_LINK_LIBRARIES};" MATCHES [[;cudart_static(\.lib)?;]])
  116. set(CMAKE_${lang}_RUNTIME_LIBRARY_DEFAULT "STATIC")
  117. elseif(";${CMAKE_${lang}_HOST_IMPLICIT_LINK_LIBRARIES};" MATCHES [[;cudart(\.lib)?;]])
  118. set(CMAKE_${lang}_RUNTIME_LIBRARY_DEFAULT "SHARED")
  119. else()
  120. set(CMAKE_${lang}_RUNTIME_LIBRARY_DEFAULT "NONE")
  121. endif()
  122. message(CONFIGURE_LOG
  123. "Parsed ${lang} nvcc implicit link information:\n${_nvcc_log}\n${log}\n\n")
  124. else()
  125. message(CONFIGURE_LOG
  126. "Failed to parse ${lang} nvcc implicit link information:\n${_nvcc_log}\n\n")
  127. message(FATAL_ERROR "Failed to extract nvcc implicit link line.")
  128. endif()
  129. set(${lang_var_}TOOLKIT_INCLUDE_DIRECTORIES)
  130. if(_nvcc_includes)
  131. # across all operating system each include directory is prefixed with -I
  132. separate_arguments(_nvcc_output NATIVE_COMMAND "${_nvcc_includes}")
  133. foreach(line IN LISTS _nvcc_output)
  134. string(REGEX REPLACE "^-I" "" line "${line}")
  135. get_filename_component(line "${line}" ABSOLUTE)
  136. list(APPEND ${lang_var_}TOOLKIT_INCLUDE_DIRECTORIES "${line}")
  137. endforeach()
  138. message(CONFIGURE_LOG
  139. "Parsed CUDA nvcc include information:\n${_nvcc_log}\n${log}\n\n")
  140. else()
  141. message(CONFIGURE_LOG
  142. "Failed to detect CUDA nvcc include information:\n${_nvcc_log}\n\n")
  143. endif()
  144. endmacro()