пятница, 28 февраля 2020 г.

Clcache to speed up rebuilds of large Visual C++ projects

Caching tool for Microsoft Visual C++ compiler to speed up rebuilds of large Visual C++ projects

On Windows with Microsoft Visual Studio and C++ compiler its not easy to use very effective and popular ccache. Still when you change a line of code and have to wait a half an hour for the solution is being rebuild then it forces to find a solution for caching of obj files. Incremental builds with /Zi option help but in case of switching git branches or changing some base class you will be forced to rebuild entire project or solution.
You can try to use a patch for ccache to cache C++ builds in Microsoft Visual Studio. Another way is clcache project. Here is the recepie:

1. Get clcache.exedistribution or build clcache.exe from sources

  • To setup clcache.7z extract the archive to some folder (e.g. C:\TEMP\clcache), the archive contains clcache.exe which is compiled Python source code and support libraries. The clcache.exe is built using Python 3.7 and PyInstaller Python module.
  • Another way is to make clcache.exe from source: https://github.com/frerich/clcache

2. Setup cl compiler

See «Integration for Visual Studio» or follow the next steps:
The next step will tell clcache that the original compiler to forward calls to is no longer available using the default name. See Visual Studio 2017 Community path to cl.exe:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\bin\Hostx86\x64
Some users have reported (see e.g. #18) that in order to make Visual Studio pick up clcache, the original cl.exe compiler binary you need to rename:
Rename cl.exe to e.g. cl_original.exe
Rename cl.exe.config to e.g. cl_original.exe.config
Copy the generated (or use distributed) clcache.exe file to the folder with cl_original.exe (original cl compiler) and rename clcache.exe toi cl.exe.
Copy all other files from the distributive archive to the same folder with cl_original.exe file.
So the idea is to cheat Visual Studio and make it to use clcache.

3. Set environment variables to use clcache

Set CLCACHE_CL environment variable to point to cl_original.exe:
CLCACHE_CL=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\bin\Hostx86\x64\cl_original.exe

In order to solve an issue with timeouts set environment variable to 100 seconds timeout:
CLCACHE_OBJECT_CACHE_TIMEOUT_MS=100000

4. Use /Z7 compiler option in all C++ projects in order to generate appropriate format of binary files

The /Z7 option produces object files that also contain full symbolic debugging information for use with the debugger. These object files and the built executable can be substantially larger than files that have no debugging information. The symbolic debugging information includes the names and types of variables, as well as functions and line numbers. No PDB file is produced.
Another option is to use some environment variable like CL_Z_OPTION to use its value in Visual Studio projects options:
CL_Z_OPTION=/Z7
Then in All options settings of your project in Visual Studio you will add to the end:
%CL_Z_OPTION%

5. Configure clcache storage capacity to keep all your object files

If you have over 1 GB of objects files then default settings of clcache are not enough so please enlarge the cache size to the required one. Go to the folder with clcache.exe and run this command to setup 4 GB cache in bytes:
clcache.exe -M 4294967296

6. Check clcache statistics

Run "clcache.exe -s" to check the effectiveness of the settings. Pay attention to cache misses. Then more cache hits and less cache misses then more effective your clcache is configured.