CMakeParseImplicitIncludeInfo.cmake 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # This is used internally by CMake and should not be included by user code.
  4. # helper function that parses implicit include dirs from a single line
  5. # for compilers that report them that way. on success we return the
  6. # list of dirs in id_var and set state_var to the 'done' state.
  7. function(cmake_parse_implicit_include_line line lang id_var log_var state_var)
  8. # clear variables we append to (avoids possible polution from parent scopes)
  9. unset(rv)
  10. set(log "")
  11. # Cray compiler (from cray wrapper, via PrgEnv-cray)
  12. if("${CMAKE_${lang}_COMPILER_ID}" STREQUAL "Cray" AND
  13. "${line}" MATCHES "^/" AND "${line}" MATCHES "/ccfe |/ftnfe " AND
  14. "${line}" MATCHES " -isystem| -I")
  15. string(REGEX MATCHALL " (-I ?|-isystem )([^ ]*)" incs "${line}")
  16. foreach(inc IN LISTS incs)
  17. string(REGEX REPLACE " (-I ?|-isystem )([^ ]*)" "\\2" idir "${inc}")
  18. list(APPEND rv "${idir}")
  19. endforeach()
  20. if(rv)
  21. string(APPEND log " got implicit includes via cray ccfe parser!\n")
  22. else()
  23. string(APPEND log " warning: cray ccfe parse failed!\n")
  24. endif()
  25. endif()
  26. # PGI compiler
  27. if("${CMAKE_${lang}_COMPILER_ID}" STREQUAL "PGI")
  28. # pgc++ verbose output differs
  29. if(("${lang}" STREQUAL "C" OR "${lang}" STREQUAL "Fortran") AND
  30. "${line}" MATCHES "^/" AND
  31. "${line}" MATCHES "/pgc |/pgf901 |/pgftnc " AND
  32. "${line}" MATCHES " -cmdline ")
  33. # cmdline has unparsed cmdline, remove it
  34. string(REGEX REPLACE "-cmdline .*" "" line "${line}")
  35. if("${line}" MATCHES " -nostdinc ")
  36. set(rv "") # defined, but empty
  37. else()
  38. string(REGEX MATCHALL " -stdinc ([^ ]*)" incs "${line}")
  39. foreach(inc IN LISTS incs)
  40. string(REGEX REPLACE " -stdinc ([^ ]*)" "\\1" idir "${inc}")
  41. string(REPLACE ":" ";" idir "${idir}")
  42. list(APPEND rv ${idir})
  43. endforeach()
  44. endif()
  45. if(DEFINED rv)
  46. string(APPEND log " got implicit includes via PGI C/F parser!\n")
  47. else()
  48. string(APPEND log " warning: PGI C/F parse failed!\n")
  49. endif()
  50. elseif("${lang}" STREQUAL "CXX" AND "${line}" MATCHES "^/" AND
  51. "${line}" MATCHES "/pggpp1 " AND "${line}" MATCHES " -I")
  52. # oddly, -Mnostdinc does not get rid of system -I's, at least in
  53. # PGI 18.10.1 ...
  54. string(REGEX MATCHALL " (-I ?)([^ ]*)" incs "${line}")
  55. foreach(inc IN LISTS incs)
  56. string(REGEX REPLACE " (-I ?)([^ ]*)" "\\2" idir "${inc}")
  57. if(NOT "${idir}" STREQUAL "-") # filter out "-I-"
  58. list(APPEND rv "${idir}")
  59. endif()
  60. endforeach()
  61. if(DEFINED rv)
  62. string(APPEND log " got implicit includes via PGI CXX parser!\n")
  63. else()
  64. string(APPEND log " warning: PGI CXX parse failed!\n")
  65. endif()
  66. endif()
  67. endif()
  68. # SunPro compiler
  69. if("${CMAKE_${lang}_COMPILER_ID}" STREQUAL "SunPro" AND
  70. ("${line}" MATCHES "-D__SUNPRO_C" OR "${line}" MATCHES "-D__SUNPRO_F") )
  71. string(REGEX MATCHALL " (-I ?)([^ ]*)" incs "${line}")
  72. foreach(inc IN LISTS incs)
  73. string(REGEX REPLACE " (-I ?)([^ ]*)" "\\2" idir "${inc}")
  74. if(NOT "${idir}" STREQUAL "-xbuiltin")
  75. list(APPEND rv "${idir}")
  76. endif()
  77. endforeach()
  78. if(rv)
  79. if ("${lang}" STREQUAL "C" OR "${lang}" STREQUAL "CXX")
  80. # /usr/include appears to be hardwired in
  81. list(APPEND rv "/usr/include")
  82. endif()
  83. string(APPEND log " got implicit includes via sunpro parser!\n")
  84. else()
  85. string(APPEND log " warning: sunpro parse failed!\n")
  86. endif()
  87. endif()
  88. # XL compiler
  89. if("${CMAKE_${lang}_COMPILER_ID}" STREQUAL "XL" AND "${line}" MATCHES "^/"
  90. AND ( ("${lang}" STREQUAL "Fortran" AND
  91. "${line}" MATCHES "/xl[fF]entry " AND
  92. "${line}" MATCHES "OSVAR\\([^ ]+\\)")
  93. OR
  94. ( ("${lang}" STREQUAL "C" OR "${lang}" STREQUAL "CXX") AND
  95. "${line}" MATCHES "/xl[cC]entry " AND
  96. "${line}" MATCHES " -qosvar=")
  97. ) )
  98. # -qnostdinc cancels other stdinc flags, even if present
  99. string(FIND "${line}" " -qnostdinc" nostd)
  100. if(NOT ${nostd} EQUAL -1)
  101. set(rv "") # defined but empty
  102. string(APPEND log " got implicit includes via XL parser (nostdinc)\n")
  103. else()
  104. if("${lang}" STREQUAL "CXX")
  105. string(REGEX MATCHALL " -qcpp_stdinc=([^ ]*)" std "${line}")
  106. string(REGEX MATCHALL " -qgcc_cpp_stdinc=([^ ]*)" gcc_std "${line}")
  107. else()
  108. string(REGEX MATCHALL " -qc_stdinc=([^ ]*)" std "${line}")
  109. string(REGEX MATCHALL " -qgcc_c_stdinc=([^ ]*)" gcc_std "${line}")
  110. endif()
  111. set(xlstd ${std} ${gcc_std})
  112. foreach(inc IN LISTS xlstd)
  113. string(REGEX REPLACE " -q(cpp|gcc_cpp|c|gcc_c)_stdinc=([^ ]*)" "\\2"
  114. ipath "${inc}")
  115. string(REPLACE ":" ";" ipath "${ipath}")
  116. list(APPEND rv ${ipath})
  117. endforeach()
  118. endif()
  119. # user can add -I flags via CMAKE_{C,CXX}_FLAGS, look for that too
  120. string(REGEX MATCHALL " (-I ?)([^ ]*)" incs "${line}")
  121. unset(urv)
  122. foreach(inc IN LISTS incs)
  123. string(REGEX REPLACE " (-I ?)([^ ]*)" "\\2" idir "${inc}")
  124. list(APPEND urv "${idir}")
  125. endforeach()
  126. if(urv)
  127. if ("${rv}" STREQUAL "")
  128. set(rv ${urv})
  129. else()
  130. list(APPEND rv ${urv})
  131. endif()
  132. endif()
  133. if(DEFINED rv)
  134. string(APPEND log " got implicit includes via XL parser!\n")
  135. else()
  136. string(APPEND log " warning: XL parse failed!\n")
  137. endif()
  138. endif()
  139. if(log)
  140. set(${log_var} "${log}" PARENT_SCOPE)
  141. else()
  142. unset(${log_var} PARENT_SCOPE)
  143. endif()
  144. if(DEFINED rv)
  145. set(${id_var} "${rv}" PARENT_SCOPE)
  146. set(${state_var} "done" PARENT_SCOPE)
  147. endif()
  148. endfunction()
  149. # top-level function to parse implicit include directory information
  150. # from verbose compiler output. sets state_var in parent to 'done' on success.
  151. function(cmake_parse_implicit_include_info text lang dir_var log_var state_var)
  152. set(state start) # values: start, loading, done
  153. # clear variables we append to (avoids possible polution from parent scopes)
  154. set(implicit_dirs_tmp)
  155. set(log "")
  156. # go through each line of output...
  157. string(REGEX REPLACE "\r?\n" ";" output_lines "${text}")
  158. foreach(line IN LISTS output_lines)
  159. if(state STREQUAL start)
  160. string(FIND "${line}" "#include \"...\" search starts here:" rv)
  161. if(rv GREATER -1)
  162. set(state loading)
  163. set(preload 1) # looking for include <...> now
  164. string(APPEND log " found start of include info\n")
  165. else()
  166. cmake_parse_implicit_include_line("${line}" "${lang}" implicit_dirs_tmp
  167. linelog state)
  168. if(linelog)
  169. string(APPEND log ${linelog})
  170. endif()
  171. if(state STREQUAL done)
  172. break()
  173. endif()
  174. endif()
  175. elseif(state STREQUAL loading)
  176. string(FIND "${line}" "End of search list." rv)
  177. if(rv GREATER -1)
  178. set(state done)
  179. string(APPEND log " end of search list found\n")
  180. break()
  181. endif()
  182. if(preload)
  183. string(FIND "${line}" "#include <...> search starts here:" rv)
  184. if(rv GREATER -1)
  185. set(preload 0)
  186. string(APPEND log " found start of implicit include info\n")
  187. endif()
  188. continue()
  189. endif()
  190. if("${line}" MATCHES "^ ")
  191. string(SUBSTRING "${line}" 1 -1 line) # remove leading space
  192. endif()
  193. if ("${line}" MATCHES " \\(framework directory\\)$")
  194. continue() # frameworks are handled elsewhere, ignore them here
  195. endif()
  196. string(REPLACE "\\" "/" path "${line}")
  197. list(APPEND implicit_dirs_tmp "${path}")
  198. string(APPEND log " add: [${path}]\n")
  199. endif()
  200. endforeach()
  201. set(implicit_dirs "")
  202. foreach(d IN LISTS implicit_dirs_tmp)
  203. if(IS_ABSOLUTE "${d}")
  204. get_filename_component(dir "${d}" ABSOLUTE)
  205. list(APPEND implicit_dirs "${dir}")
  206. string(APPEND log " collapse include dir [${d}] ==> [${dir}]\n")
  207. else()
  208. string(APPEND log " skipping relative include dir [${d}]\n")
  209. endif()
  210. endforeach()
  211. list(REMOVE_DUPLICATES implicit_dirs)
  212. # Log results.
  213. if(state STREQUAL done)
  214. string(APPEND log " implicit include dirs: [${implicit_dirs}]\n")
  215. else()
  216. string(APPEND log " warn: unable to parse implicit include dirs!\n")
  217. endif()
  218. # Return results.
  219. set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
  220. set(${log_var} "${log}" PARENT_SCOPE)
  221. set(${state_var} "${state}" PARENT_SCOPE)
  222. endfunction()