Please provide a possibility to set default include paths (%INCLUDE%) for every compilation
--- Problem ---
I need to get the INCLUDE environment variable appended at the moment the module https://cmake.org/cmake/help/latest/module/CheckIncludeFiles.html runs cl.exe. Unfortunately CheckIncludeFile does not run cl.exe directly. It runs msbuild.exe which discards INCLUDE, LIB, PATH environment variables unless the /p:UseEnv=true msbuild flag is set, but CMake provides no option to operate with msbuild flags as I know.
--- Why only INCLUDE is an option in my case (why not command line) ---
I would like to use headers from Cygwin directory on the top of the standard headers from Visual Studio. The Cygwin directory has different headers which conflict with the Visual Studio headers with same names. So I want that the Cygwin directory gets used really after all, so that the compiler looks into it only if all Visual Studio include paths failed. The only way to do it is to append the INCLUDE with the Cygwin path.
I cannot use CMake options CMAKE_REQUIRED_FLAGS, CMAKE_REQUIRED_INCLUDES, CMAKE_C_FLAGS to achieve what I want, because in this case the Cygwin include path is added into the compilation command and it has higher priority than INCLUDE env variable and msbuild's IncludePath which makes cl.exe to take standard headers like stdio.h also from Cygwin path and it breaks the build.
--- What I tried already, but it did not work ---
I tried CMAKE_INCLUDE_PATH, CMAKE_SYSTEM_INCLUDE_PATH, CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES, CMAKE_C_STANDARD_INCLUDE_DIRECTORIES, CMAKE_VS_SDK_INCLUDE_DIRECTORIES - no effect on CheckIncludeFile. I tried to replace the full command using CMAKE_VS_MSBUILD_COMMAND and CMAKE_VS_DEVENV_COMMAND - it does not work that way I thought, at least for CheckIncludeFile.
--- Workaround ---
Currently I have to use this workaround in CMakeLists.txt (explicitly add all paths from INCLUDE to the command line):
foreach(inc $ENV{INCLUDE})
set (CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} "${inc}")
endforeach()
--- Proposed solutions ---
-
Fix CMAKE_C_STANDARD_INCLUDE_DIRECTORIES, CMAKE_VS_SDK_INCLUDE_DIRECTORIES, CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES, because they don't work as described (they have no effect on CheckXXX modules). https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_IMPLICIT_INCLUDE_DIRECTORIES.html says that it "to be used for every source file compiled with the compiler", however it does not work for the source compilation with CheckIncludeFile, that is not indeed "for every".
-
Alternative way. Add a possibility to set build tool (msbuild) parameters, then it will be possible to use /p:UseEnv=true or /p:IncludePath=%INCLUDE% to force msbuild to respect the user's env and propagate it to the inner cl.exe.