CMakeParseImplicitIncludeInfo.cmake 8.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

# This is used internally by CMake and should not be included by user code.

# helper function that parses implicit include dirs from a single line
# for compilers that report them that way.  on success we return the
# list of dirs in id_var and set state_var to the 'done' state.
function(cmake_parse_implicit_include_line line lang id_var log_var state_var)
  # clear variables we append to (avoids possible polution from parent scopes)
  unset(rv)
  set(log "")

14
  # Cray compiler (from cray wrapper, via PrgEnv-cray)
15
  if("${CMAKE_${lang}_COMPILER_ID}" STREQUAL "Cray" AND
16 17
     "${line}" MATCHES "^/" AND "${line}" MATCHES "/ccfe |/ftnfe " AND
     "${line}" MATCHES " -isystem| -I")
18 19 20 21 22 23 24 25 26 27 28 29
    string(REGEX MATCHALL " (-I ?|-isystem )([^ ]*)" incs "${line}")
    foreach(inc IN LISTS incs)
      string(REGEX REPLACE " (-I ?|-isystem )([^ ]*)" "\\2" idir "${inc}")
      list(APPEND rv "${idir}")
    endforeach()
    if(rv)
      string(APPEND log "  got implicit includes via cray ccfe parser!\n")
    else()
      string(APPEND log "  warning: cray ccfe parse failed!\n")
    endif()
  endif()

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  # PGI compiler
  if("${CMAKE_${lang}_COMPILER_ID}" STREQUAL "PGI")
    # pgc++ verbose output differs
    if(("${lang}" STREQUAL "C" OR "${lang}" STREQUAL "Fortran") AND
        "${line}" MATCHES "^/" AND
        "${line}" MATCHES "/pgc |/pgf901 |/pgftnc " AND
        "${line}" MATCHES " -cmdline ")
      # cmdline has unparsed cmdline, remove it
      string(REGEX REPLACE "-cmdline .*" "" line "${line}")
      if("${line}" MATCHES " -nostdinc ")
        set(rv "")    # defined, but empty
      else()
        string(REGEX MATCHALL " -stdinc ([^ ]*)" incs "${line}")
        foreach(inc IN LISTS incs)
          string(REGEX REPLACE " -stdinc ([^ ]*)" "\\1" idir "${inc}")
          string(REPLACE ":" ";" idir "${idir}")
          list(APPEND rv ${idir})
        endforeach()
      endif()
      if(DEFINED rv)
        string(APPEND log "  got implicit includes via PGI C/F parser!\n")
      else()
        string(APPEND log "  warning: PGI C/F parse failed!\n")
      endif()
    elseif("${lang}" STREQUAL "CXX" AND "${line}" MATCHES "^/" AND
           "${line}" MATCHES "/pggpp1 " AND "${line}" MATCHES " -I")
      # oddly, -Mnostdinc does not get rid of system -I's, at least in
      # PGI 18.10.1 ...
      string(REGEX MATCHALL " (-I ?)([^ ]*)" incs "${line}")
      foreach(inc IN LISTS incs)
        string(REGEX REPLACE " (-I ?)([^ ]*)" "\\2" idir "${inc}")
        if(NOT "${idir}" STREQUAL "-")   # filter out "-I-"
          list(APPEND rv "${idir}")
        endif()
      endforeach()
      if(DEFINED rv)
        string(APPEND log "  got implicit includes via PGI CXX parser!\n")
      else()
        string(APPEND log "  warning: PGI CXX parse failed!\n")
      endif()
    endif()
  endif()

73 74
  # SunPro compiler
  if("${CMAKE_${lang}_COMPILER_ID}" STREQUAL "SunPro" AND
75
     ("${line}" MATCHES "-D__SUNPRO_C" OR "${line}" MATCHES "-D__SUNPRO_F") )
76 77 78 79 80 81 82 83
    string(REGEX MATCHALL " (-I ?)([^ ]*)" incs "${line}")
    foreach(inc IN LISTS incs)
      string(REGEX REPLACE " (-I ?)([^ ]*)" "\\2" idir "${inc}")
      if(NOT "${idir}" STREQUAL "-xbuiltin")
        list(APPEND rv "${idir}")
      endif()
    endforeach()
    if(rv)
84 85 86 87
      if ("${lang}" STREQUAL "C" OR "${lang}" STREQUAL "CXX")
        # /usr/include appears to be hardwired in
        list(APPEND rv "/usr/include")
      endif()
88 89 90 91 92 93
      string(APPEND log "  got implicit includes via sunpro parser!\n")
    else()
      string(APPEND log "  warning: sunpro parse failed!\n")
    endif()
  endif()

94 95 96 97 98 99 100 101 102 103
  # XL compiler
  if("${CMAKE_${lang}_COMPILER_ID}" STREQUAL "XL" AND "${line}" MATCHES "^/"
     AND ( ("${lang}" STREQUAL "Fortran" AND
            "${line}" MATCHES "/xl[fF]entry " AND
            "${line}" MATCHES "OSVAR\\([^ ]+\\)")
           OR
            (  ("${lang}" STREQUAL "C" OR "${lang}" STREQUAL "CXX") AND
            "${line}" MATCHES "/xl[cC]entry " AND
            "${line}" MATCHES " -qosvar=")
         )  )
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    # -qnostdinc cancels other stdinc flags, even if present
    string(FIND "${line}" " -qnostdinc" nostd)
    if(NOT ${nostd} EQUAL -1)
      set(rv "")    # defined but empty
      string(APPEND log "  got implicit includes via XL parser (nostdinc)\n")
    else()
      if("${lang}" STREQUAL "CXX")
        string(REGEX MATCHALL " -qcpp_stdinc=([^ ]*)" std "${line}")
        string(REGEX MATCHALL " -qgcc_cpp_stdinc=([^ ]*)" gcc_std "${line}")
      else()
        string(REGEX MATCHALL " -qc_stdinc=([^ ]*)" std "${line}")
        string(REGEX MATCHALL " -qgcc_c_stdinc=([^ ]*)" gcc_std "${line}")
      endif()
      set(xlstd ${std} ${gcc_std})
      foreach(inc IN LISTS xlstd)
        string(REGEX REPLACE " -q(cpp|gcc_cpp|c|gcc_c)_stdinc=([^ ]*)" "\\2"
               ipath "${inc}")
        string(REPLACE ":" ";" ipath "${ipath}")
        list(APPEND rv ${ipath})
      endforeach()
    endif()
    # user can add -I flags via CMAKE_{C,CXX}_FLAGS, look for that too
    string(REGEX MATCHALL " (-I ?)([^ ]*)" incs "${line}")
    unset(urv)
    foreach(inc IN LISTS incs)
      string(REGEX REPLACE " (-I ?)([^ ]*)" "\\2" idir "${inc}")
      list(APPEND urv "${idir}")
    endforeach()
    if(urv)
      if ("${rv}" STREQUAL "")
        set(rv ${urv})
      else()
        list(APPEND rv ${urv})
      endif()
    endif()

    if(DEFINED rv)
      string(APPEND log "  got implicit includes via XL parser!\n")
    else()
      string(APPEND log "  warning: XL parse failed!\n")
    endif()
  endif()

147 148
  if(log)
    set(${log_var} "${log}" PARENT_SCOPE)
149 150
  else()
    unset(${log_var} PARENT_SCOPE)
151
  endif()
152
  if(DEFINED rv)
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    set(${id_var} "${rv}" PARENT_SCOPE)
    set(${state_var} "done" PARENT_SCOPE)
  endif()
endfunction()

# top-level function to parse implicit include directory information
# from verbose compiler output. sets state_var in parent to 'done' on success.
function(cmake_parse_implicit_include_info text lang dir_var log_var state_var)
  set(state start)    # values: start, loading, done

  # clear variables we append to (avoids possible polution from parent scopes)
  set(implicit_dirs_tmp)
  set(log "")

  # go through each line of output...
  string(REGEX REPLACE "\r?\n" ";" output_lines "${text}")
  foreach(line IN LISTS output_lines)
    if(state STREQUAL start)
171
      string(FIND "${line}" "#include \"...\" search starts here:" rv)
172 173
      if(rv GREATER -1)
        set(state loading)
174 175
        set(preload 1)      # looking for include <...> now
        string(APPEND log "  found start of include info\n")
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
      else()
        cmake_parse_implicit_include_line("${line}" "${lang}" implicit_dirs_tmp
                                          linelog state)
        if(linelog)
          string(APPEND log ${linelog})
        endif()
        if(state STREQUAL done)
          break()
        endif()
      endif()
    elseif(state STREQUAL loading)
      string(FIND "${line}" "End of search list." rv)
      if(rv GREATER -1)
        set(state done)
        string(APPEND log "  end of search list found\n")
        break()
192 193 194 195 196 197
      endif()
      if(preload)
        string(FIND "${line}" "#include <...> search starts here:" rv)
        if(rv GREATER -1)
          set(preload 0)
          string(APPEND log "  found start of implicit include info\n")
198
        endif()
199 200 201 202 203 204 205
        continue()
      endif()
      if("${line}" MATCHES "^ ")
        string(SUBSTRING "${line}" 1 -1 line)  # remove leading space
      endif()
      if ("${line}" MATCHES " \\(framework directory\\)$")
        continue() # frameworks are handled elsewhere, ignore them here
206
      endif()
207 208 209
      string(REPLACE "\\" "/" path "${line}")
      list(APPEND implicit_dirs_tmp "${path}")
      string(APPEND log "    add: [${path}]\n")
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    endif()
  endforeach()

  # Log results.
  if(state STREQUAL done)
    string(APPEND log "  implicit include dirs: [${implicit_dirs_tmp}]\n")
  else()
    string(APPEND log "  warn: unable to parse implicit include dirs!\n")
  endif()

  # Return results.
  set(${dir_var} "${implicit_dirs_tmp}" PARENT_SCOPE)
  set(${log_var} "${log}" PARENT_SCOPE)
  set(${state_var} "${state}" PARENT_SCOPE)

endfunction()