Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / CMakeExtraGeneratorDetermineCompilerMacrosAndIncludeDirs.cmake
1
2 #=============================================================================
3 # Copyright 2009 Kitware, Inc.
4 #
5 # Distributed under the OSI-approved BSD License (the "License");
6 # see accompanying file Copyright.txt for details.
7 #
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.)
14
15 # This file is included by CMakeFindEclipseCDT4.cmake and CMakeFindCodeBlocks.cmake
16
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})
23   set(_gccOutput)
24   file(WRITE "${CMAKE_BINARY_DIR}/CMakeFiles/dummy" "\n" )
25
26   if (${_lang} STREQUAL "c++")
27     set(_compilerExecutable "${CMAKE_CXX_COMPILER}")
28     set(_arg1 "${CMAKE_CXX_COMPILER_ARG1}")
29   else ()
30     set(_compilerExecutable "${CMAKE_C_COMPILER}")
31     set(_arg1 "${CMAKE_C_COMPILER_ARG1}")
32   endif ()
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")
38
39   # First find the system include dirs:
40   if( "${_gccOutput}" MATCHES "> search starts here[^\n]+\n *(.+ *\n) *End of (search) list" )
41
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}")
50     endforeach()
51
52   endif()
53
54
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__))
60 #  #define   FOO (23)
61 #  #define __UINTMAX_TYPE__ long long unsigned int
62 #  #define __UINTMAX_TYPE__ long long unsigned int
63 #  #define __i386__  1
64
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}-")
70
71     list(APPEND ${_resultDefines} "${_name}")
72     if(_value)
73       list(APPEND ${_resultDefines} "${_value}")
74     else()
75       list(APPEND ${_resultDefines} " ")
76     endif()
77   endforeach()
78
79 endmacro()
80
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})
86
87 set(ENV{LC_ALL}      C)
88 set(ENV{LC_MESSAGES} C)
89 set(ENV{LANG}        C)
90
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")
97   endif ()
98 endif ()
99
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")
106   endif ()
107 endif ()
108
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})