0

I'm trying to configure CMake 3.24.2 to generate a Visual Studio 2019 project that uses ccache to cache the output of previous builds. For that, I'm following alternative #1 from ccache's docs, where I'm trying to configure CMake to point the compiler to a copy of ccache which I've dumped in C:/tools/ccache/cl.exe. For that purpose I'm setting CMAKE_CXX_COMPILER to point to my ccache.exe, and I'm generating the project with the following command.

cmake.exe -DCMAKE_CXX_COMPILER=C:/tools/ccache/cl.exe -G "Visual Studio 16 2019" -A Win32 -DCMAKE_INSTALL_LIBDIR=lib/win/Release/x86 -S D:\proj\testproject -B D:\proj\testproject\cmake-build-release

However, this does not work and the vs2019 project keeps on pointing to the original cl.exe command. Here's an except I've taken from D:\proj\testproject\cmake-build-release\CMakeFiles\CMakeOutput.log:

The system is: Windows - 10.0.19041.0 - AMD64
Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" succeeded.
Compiler: C:/tools/ccache/cl.exe 
Build flags: 
Id flags:  

The output was:
0
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 2/11/2023 4:51:26 PM.
Project "D:\proj\testproject\cmake-build-release\CMakeFiles\3.24.2\CompilerIdCXX\CompilerIdCXX.vcxproj" on node 1 (default targets).
PrepareForBuild:
  Creating directory "Debug\".
  Creating directory "Debug\CompilerIdCXX.tlog\".
InitializeBuildStatus:
  Creating "Debug\CompilerIdCXX.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\bin\HostX64\x86\CL.exe /c /nologo /W0 /WX- /diagnostics:column /Od /Oy- /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"Debug\\" /Fd"Debug\vc142.pdb" /external:W0 /Gd /TP /analyze- /FC /errorReport:queue CMakeCXXCompilerId.cpp
  CMakeCXXCompilerId.cpp

(...)

Apparently the Visual Studio 2019 project does get the hint that it should use C:/tools/ccache/cl.exe, but for some unknown reason its ClCompile entry points to the original cl.exe.

Does anyone know what's going on for the Visual Studio 2019 insisting on not using the C++ compiler passed with CMAKE_CXX_COMPILER?

RAM
  • 2,257
  • 2
  • 19
  • 41
  • 2
    Yes, with Visual Studio being a generator, CMake ignores CMAKE_CXX_COMPILER variable: https://stackoverflow.com/a/49533782/3440745. The document you reference describes the exact way for use ccache with CMake and Visual Studio: https://github.com/ccache/ccache/wiki/MS-Visual-Studio#usage-with-cmake. Why do not follow it? – Tsyvarev Feb 11 '23 at 21:01
  • @Tsyvarev thanks for that suggestion, but unfortunately it does not work. When I copy/paste that definition into my project and re-run cmake after deleting the output dir, cmake still returns `Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x86/cl.exe - skipped`. – RAM Feb 12 '23 at 13:50
  • As far as I understand from the [documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_VS_GLOBALS.html), variable `CMAKE_VS_GLOBALS` is treated as a default value for every target created afterwards. So, it may to not affect on the compiler which is **checked** by CMake itself, but it will affect on the compiler which will be **used** for build the specific targets. – Tsyvarev Feb 12 '23 at 16:23

1 Answers1

1

This happens because the original cl.exe is inferred from the chosen generator "Visual Studio 16 2019".

In order to switch to another compiler you should use other generator e.g. Ninja or Unix Makefiles.

273K
  • 29,503
  • 10
  • 41
  • 64