1
0

CMakeExtraGeneratorDetermineCompilerMacrosAndIncludeDirs.cmake 4.9 KB

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