CMakeExtraGeneratorDetermineCompilerMacrosAndIncludeDirs.cmake 5.1 KB

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