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