CMakeExtraGeneratorDetermineCompilerMacrosAndIncludeDirs.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. STRING(STRIP "${nextLine}" _includePath)
  40. LIST(APPEND ${_resultIncludeDirs} "${_includePath}")
  41. ENDFOREACH(nextLine)
  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)
  59. LIST(APPEND ${_resultDefines} "${_value}")
  60. ELSE()
  61. LIST(APPEND ${_resultDefines} " ")
  62. ENDIF()
  63. ENDFOREACH(nextLine)
  64. ENDMACRO(_DETERMINE_GCC_SYSTEM_INCLUDE_DIRS _lang)
  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. IF(_orig_lc_all)
  71. SET(ENV{LC_ALL} C)
  72. ENDIF()
  73. IF(_orig_lc_messages)
  74. SET(ENV{LC_MESSAGES} C)
  75. ENDIF()
  76. IF(_orig_lang)
  77. SET(ENV{LANG} C)
  78. ENDIF()
  79. # Now check for C, works for gcc and Intel compiler at least
  80. IF (NOT CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS)
  81. IF ("${CMAKE_C_COMPILER_ID}" MATCHES GNU OR "${CMAKE_C_COMPILER_ID}" MATCHES Intel)
  82. _DETERMINE_GCC_SYSTEM_INCLUDE_DIRS(c _dirs _defines)
  83. SET(CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS "${_dirs}" CACHE INTERNAL "C compiler system include directories")
  84. SET(CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS "${_defines}" CACHE INTERNAL "C compiler system defined macros")
  85. ENDIF ()
  86. ENDIF ()
  87. # And now the same for C++
  88. IF (NOT CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS)
  89. IF ("${CMAKE_CXX_COMPILER_ID}" MATCHES GNU OR "${CMAKE_CXX_COMPILER_ID}" MATCHES Intel)
  90. _DETERMINE_GCC_SYSTEM_INCLUDE_DIRS(c++ _dirs _defines)
  91. SET(CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS "${_dirs}" CACHE INTERNAL "CXX compiler system include directories")
  92. SET(CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS "${_defines}" CACHE INTERNAL "CXX compiler system defined macros")
  93. ENDIF ()
  94. ENDIF ()
  95. # Restore original LC_ALL, LC_MESSAGES, and LANG
  96. IF(_orig_lc_all)
  97. SET(ENV{LC_ALL} ${_orig_lc_all})
  98. ENDIF()
  99. IF(_orig_lc_messages)
  100. SET(ENV{LC_MESSAGES} ${_orig_lc_messages})
  101. ENDIF()
  102. IF(_orig_lang)
  103. SET(ENV{LANG} ${_orig_lang})
  104. ENDIF()