CMakeExtraGeneratorDetermineCompilerMacrosAndIncludeDirs.cmake 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 file is included by CMakeFindEclipseCDT4.cmake and CMakeFindCodeBlocks.cmake
  4. # The Eclipse and the CodeBlocks generators need to know the standard include path
  5. # so that they can find the headers at runtime and parsing etc. works better
  6. # This is done here by actually running gcc with the options so it prints its
  7. # system include directories, which are parsed then and stored in the cache.
  8. macro(_DETERMINE_GCC_SYSTEM_INCLUDE_DIRS _lang _resultIncludeDirs _resultDefines)
  9. set(${_resultIncludeDirs})
  10. set(_gccOutput)
  11. file(WRITE "${CMAKE_BINARY_DIR}/CMakeFiles/dummy" "\n" )
  12. if (${_lang} STREQUAL "c++")
  13. set(_compilerExecutable "${CMAKE_CXX_COMPILER}")
  14. set(_arg1 "${CMAKE_CXX_COMPILER_ARG1}")
  15. if (CMAKE_CXX_FLAGS MATCHES "(-stdlib=[^ ]+)")
  16. set(_stdlib "${CMAKE_MATCH_1}")
  17. endif ()
  18. if (CMAKE_CXX_FLAGS MATCHES "(-std=[^ ]+)")
  19. set(_stdver "${CMAKE_MATCH_1}")
  20. endif ()
  21. else ()
  22. set(_compilerExecutable "${CMAKE_C_COMPILER}")
  23. set(_arg1 "${CMAKE_C_COMPILER_ARG1}")
  24. endif ()
  25. execute_process(COMMAND ${_compilerExecutable} ${_arg1} ${_stdver} ${_stdlib} -v -E -x ${_lang} -dD dummy
  26. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/CMakeFiles
  27. ERROR_VARIABLE _gccOutput
  28. OUTPUT_VARIABLE _gccStdout )
  29. file(REMOVE "${CMAKE_BINARY_DIR}/CMakeFiles/dummy")
  30. # First find the system include dirs:
  31. if( "${_gccOutput}" MATCHES "> search starts here[^\n]+\n *(.+ *\n) *End of (search) list" )
  32. # split the output into lines and then remove leading and trailing spaces from each of them:
  33. string(REGEX MATCHALL "[^\n]+\n" _includeLines "${CMAKE_MATCH_1}")
  34. foreach(nextLine ${_includeLines})
  35. # on OSX, gcc says things like this: "/System/Library/Frameworks (framework directory)", strip the last part
  36. string(REGEX REPLACE "\\(framework directory\\)" "" nextLineNoFramework "${nextLine}")
  37. # strip spaces at the beginning and the end
  38. string(STRIP "${nextLineNoFramework}" _includePath)
  39. list(APPEND ${_resultIncludeDirs} "${_includePath}")
  40. endforeach()
  41. endif()
  42. # now find the builtin macros:
  43. string(REGEX MATCHALL "#define[^\n]+\n" _defineLines "${_gccStdout}")
  44. # A few example lines which the regexp below has to match properly:
  45. # #define MAX(a,b) ((a) > (b) ? (a) : (b))
  46. # #define __fastcall __attribute__((__fastcall__))
  47. # #define FOO (23)
  48. # #define __UINTMAX_TYPE__ long long unsigned int
  49. # #define __UINTMAX_TYPE__ long long unsigned int
  50. # #define __i386__ 1
  51. foreach(nextLine ${_defineLines})
  52. string(REGEX MATCH "^#define +([A-Za-z_][A-Za-z0-9_]*)(\\([^\\)]+\\))? +(.+) *$" _dummy "${nextLine}")
  53. set(_name "${CMAKE_MATCH_1}${CMAKE_MATCH_2}")
  54. string(STRIP "${CMAKE_MATCH_3}" _value)
  55. #message(STATUS "m1: -${CMAKE_MATCH_1}- m2: -${CMAKE_MATCH_2}- m3: -${CMAKE_MATCH_3}-")
  56. list(APPEND ${_resultDefines} "${_name}")
  57. if(_value)
  58. list(APPEND ${_resultDefines} "${_value}")
  59. else()
  60. list(APPEND ${_resultDefines} " ")
  61. endif()
  62. endforeach()
  63. endmacro()
  64. # Save the current LC_ALL, LC_MESSAGES, and LANG environment variables and set them
  65. # to "C" that way GCC's "search starts here" text is in English and we can grok it.
  66. set(_orig_lc_all $ENV{LC_ALL})
  67. set(_orig_lc_messages $ENV{LC_MESSAGES})
  68. set(_orig_lang $ENV{LANG})
  69. set(ENV{LC_ALL} C)
  70. set(ENV{LC_MESSAGES} C)
  71. set(ENV{LANG} C)
  72. # Now check for C, works for gcc and Intel compiler at least
  73. if (NOT CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS)
  74. if (CMAKE_C_COMPILER_ID MATCHES GNU OR CMAKE_C_COMPILER_ID MATCHES Intel OR CMAKE_C_COMPILER_ID MATCHES Clang)
  75. _DETERMINE_GCC_SYSTEM_INCLUDE_DIRS(c _dirs _defines)
  76. set(CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS "${_dirs}" CACHE INTERNAL "C compiler system include directories")
  77. set(CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS "${_defines}" CACHE INTERNAL "C compiler system defined macros")
  78. elseif ("${CMAKE_C_COMPILER_ID}" MATCHES MSVC)
  79. set(CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS "$ENV{INCLUDE}" CACHE INTERNAL "C compiler system include directories")
  80. endif ()
  81. endif ()
  82. # And now the same for C++
  83. if (NOT CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS)
  84. if ("${CMAKE_CXX_COMPILER_ID}" MATCHES GNU OR "${CMAKE_CXX_COMPILER_ID}" MATCHES Intel OR "${CMAKE_CXX_COMPILER_ID}" MATCHES Clang)
  85. _DETERMINE_GCC_SYSTEM_INCLUDE_DIRS(c++ _dirs _defines)
  86. set(CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS "${_dirs}" CACHE INTERNAL "CXX compiler system include directories")
  87. set(CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS "${_defines}" CACHE INTERNAL "CXX compiler system defined macros")
  88. elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES MSVC)
  89. set(CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS "$ENV{INCLUDE}" CACHE INTERNAL "CXX compiler system include directories")
  90. endif ()
  91. endif ()
  92. # Restore original LC_ALL, LC_MESSAGES, and LANG
  93. set(ENV{LC_ALL} ${_orig_lc_all})
  94. set(ENV{LC_MESSAGES} ${_orig_lc_messages})
  95. set(ENV{LANG} ${_orig_lang})