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