PCH broken when using Ninja Multi-Config + CMAKE_CROSS_CONFIGS and MSVC as the compiler
It seems there are issues when using precompiled headers together with MSVC and Ninja Multi Config on Windows.
Here's some snippets of the failure from my actual project
ninja: warning: multiple rules generate src/corelib/CMakeFiles/Core.dir/RelWithDebInfo/cmake_pch.cxx.pch. builds involving this target will not be correct; continuing anyway [-w dupbuild=warn]
...
FAILED: C:\PROGRA~2\MIB055~1\2019\PROFES~1\VC\Tools\MSVC\1423~1.281\bin\Hostx64\x64\cl.exe /nologo /TP -DHAVE_CONFIG_H -DPCRE2_CODE_UNIT_WIDTH=16 -DPCRE2_DISABLE_JIT -DPCRE2_STATIC -DPROEVALUATOR_FULL -DQT_BOOTSTRAPPED -DQT_BUILD_QMAKE -DQT_NO_DEBUG -DQT_NO_EXCEPTIONS -DQT_NO_FOREACH -DQT_USE_QSTRINGBUILDER -DQT_VERSION_MAJOR=6 -DQT_VERSION_MINOR=0 -DQT_VERSION_PATCH=0 -DQT_VERSION_STR=\"6.0.0\" -DUNICODE -D_CRT_SECURE_NO_WARNINGS -D_ENABLE_EXTENDED_ALIGNED_STORAGE -D_SCL_SECURE_NO_WARNINGS -DCMAKE_INTDIR=\"RelWithDebInfo\" -I\Users\qt\work\qt\qtbase\qmake -Iqmake -I\Users\qt\work\qt\qtbase\qmake\generators -I\Users\qt\work\qt\qtbase\qmake\generators\mac -I\Users\qt\work\qt\qtbase\qmake\generators\unix -I\Users\qt\work\qt\qtbase\qmake\generators\win32 -I\Users\qt\work\qt\qtbase\qmake\library -Isrc\corelib\Core_autogen\include_RelWithDebInfo -Iinclude -Iinclude\QtCore -I\Users\qt\work\qt\qtbase\src\corelib -Isrc\corelib -Isrc\corelib\global -I\Users\qt\work\qt\qtbase\src\corelib\..\3rdparty\tinycbor\src -Iinclude\QtCore\6.0.0 -Iinclude\QtCore\6.0.0\QtCore -I\Users\qt\work\qt\qtbase\src\corelib\..\3rdparty\zlib\src -I\Users\qt\work\qt\qtbase\src\corelib\..\3rdparty\double-conversion\.. -I\Users\qt\work\qt\qtbase\src\corelib\..\3rdparty\double-conversion\include -Isrc\corelib\.rcc -I\Users\qt\work\qt\qtbase\mkspecs\win32-msvc -I\Users\qt\work\qt\qtbase\src\3rdparty\pcre2\src /DWIN32 /D_WINDOWS /GR /EHsc /Zi /O2 /Ob1 /DNDEBUG -MD /W3 /wd4530 /wd4577 -Zc:__cplusplus -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -Zc:referenceBinding -Zc:wchar_t -std:c++17 /YcC:/Users/qt/work/qt/qtbase/qmake/CMakeFiles/qmake.dir/RelWithDebInfo/cmake_pch.hxx /FpC:/Users/qt/work/qt/qtbase/qmake/CMakeFiles/qmake.dir/RelWithDebInfo/cmake_pch.cxx.pch /FIC:/Users/qt/work/qt/qtbase/qmake/CMakeFiles/qmake.dir/RelWithDebInfo/cmake_pch.hxx /showIncludes /Foqmake\CMakeFiles\qmake.dir\RelWithDebInfo\cmake_pch.cxx.obj /Fdqmake\CMakeFiles\qmake.dir\RelWithDebInfo\ /FS -c qmake\CMakeFiles\qmake.dir\cmake_pch.cxx
agent:2020/05/14 15:39:50 build.go:253: C:\Users\qt\work\qt\qtbase\qmake\CMakeFiles\qmake.dir\cmake_pch.cxx : fatal error C1083: Cannot open compiler intermediate file: 'c:\users\qt\work\qt\qtbase\qmake\cmakefiles\qmake.dir\relwithdebinfo\cmake_pch.cxx.pch': Permission denied
agent:2020/05/14 15:40:00 build.go:253: ninja: build stopped: subcommand failed.
agent:2020/05/14 15:40:00 build.go:253: be correct; continuing anyway [-w dupbuild=warn]
agent:2020/05/14 15:40:00 build.go:253: ninja: warning: multiple rules generate qmake/CMakeFiles/qmake.dir/RelWithDebInfo/cmake_pch.cxx.pch. builds involving this target will not be correct; continuing anyway [-w dupbuild=warn]
Here is a small repro project that displays the warnings, although building didn't fail. But I assume if the warnings go away, then my actual project will also work.
# CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(test_pch CXX)
set(source_file "${CMAKE_CURRENT_BINARY_DIR}/lib.cpp")
set(header_file "${CMAKE_CURRENT_BINARY_DIR}/header.h")
file(WRITE "${source_file}" "#include \"header.h\"\n\n\
int foo() {return 0;}")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/header.h" "int foo();")
add_library(lib "${source_file}")
target_precompile_headers(lib PRIVATE "${header_file}")
Configure and build with
$ cmake .. -G"Ninja Multi-Config" -DCMAKE_CONFIGURATION_TYPES="Release;Debug" -DCMAKE_CROSS_CONFIGS="all" -DCMAKE_CXX_COMPILER=cl.exe
$ ninja -v
Edited by alcroito