Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ccache on Windows #56705

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,20 @@ export CC="ccache cc" # add to ~/.zshrc or other shell config file
export CXX="ccache c++" # add to ~/.zshrc or other shell config file
```

On Windows:

Tips: follow <https://github.com/ccache/ccache/wiki/MS-Visual-Studio>, and you
should notice that obj file will be bigger the normal one.

First, install ccache, assume ccache install to c:\ccache, copy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, thinks means that if your ccache is installed at c:\path\to\ccache, then you can just do

cp c:\path\to\ccache\ccache.exe c:\path\to\ccache\cl.exe
.\vcbuild.bat ccache c:\ccache\

?

(We recommend installing Git and the UNIX tools added to PATH on Windows, so it seems fine to assume cp is available).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, that makes sense. I'll add the command about the cp command to the doc.

c:\ccache\ccache.exe to c:\ccache\cl.exe

When building Node.js provide a path to your ccache via the option

```powershell
.\vcbuild.bat ccache c:\ccache\
```

This will allow for near-instantaneous rebuilds when switching branches back
and forth that were built with cache.

Expand Down
5 changes: 4 additions & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# Reset this number to 0 on major V8 upgrades.
# Increment by one for each non-official patch applied to deps/v8.
'v8_embedder_string': '-node.12',
'v8_embedder_string': '-node.13',

##### V8 defaults for Node.js #####

Expand Down Expand Up @@ -483,6 +483,9 @@
'NOMINMAX',
],
}],
['ccache_used == 1', {
'defines': ['CCACHE_USED',],
}],
[ 'OS in "linux freebsd openbsd solaris aix os400"', {
'cflags': [ '-pthread' ],
'ldflags': [ '-pthread' ],
Expand Down
10 changes: 10 additions & 0 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,11 @@
default=None,
help='Configure for clang-cl on Windows. This flag sets the GYP "clang" ' +
'variable to 1 and "llvm_version" to the specified value.')
parser.add_argument('--ccache-used',
action='store_true',
dest='ccache_used',
default=None,
help='Ccache is used in compulation on Windows.')

(options, args) = parser.parse_known_args()

Expand Down Expand Up @@ -1165,6 +1170,8 @@ def get_gas_version(cc):
# check involves checking the build number against an allowlist. I'm not
# quite prepared to go that far yet.
def check_compiler(o):
o['variables']['ccache_used'] = 0

if sys.platform == 'win32':
if options.clang_cl:
o['variables']['clang'] = 1
Expand All @@ -1173,6 +1180,9 @@ def check_compiler(o):
o['variables']['clang'] = 0
o['variables']['llvm_version'] = '0.0'

if options.ccache_used:
o['variables']['ccache_used'] = 1

if not options.openssl_no_asm and options.dest_cpu in ('x86', 'x64'):
nasm_version = get_nasm_version('nasm')
o['variables']['nasm_version'] = nasm_version
Expand Down
8 changes: 8 additions & 0 deletions deps/v8/src/builtins/generate-bytecodes-builtins-list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ void WriteBytecode(std::ofstream& out, Bytecode bytecode,
void WriteHeader(const char* header_filename) {
std::ofstream out(header_filename);

#ifdef CCACHE_USED
// Write a cache invalidator to ensure that ccache does not cache this file.
Copy link
Member

@joyeecheung joyeecheung Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why is it not a problem for non-Windows. Does it have to do with the fact that we only use PCH on Windows? What happens if you just mix and ccache_used != 1 into the condition here?

['OS=="win"', {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is connected to PCH usage. The idea of disabling PCH when wanting to use ccache did cross my mind, but from what I recall, the time for compilation without PCH was much greater than with it. Not sure about the numbers. Let me reiterate it and then I'll share the exact numbers from my machine here together with the PCH error I get from cache. Thanks for the suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that PCH would help a deal when there's no good caching but wouldn't matter as much when there is caching? Especially when dealing with V8 which uses templates very extensively, and headers change quite often...

out << "#ifndef CACHE_INVALIDATOR\n"
<< "#define CACHE_INVALIDATOR\n"
<< "inline const char* cache_invalidator = __TIME__;\n"
<< "#endif\n\n";
#endif

out << "// Automatically generated from interpreter/bytecodes.h\n"
<< "// The following list macro is used to populate the builtins list\n"
<< "// with the bytecode handlers\n\n"
Expand Down
13 changes: 13 additions & 0 deletions tools/msvs/props_4_ccache.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project>
<PropertyGroup>
<UseMultiToolTask>true</UseMultiToolTask>
<TrackFileAccess>false</TrackFileAccess>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<!-- /Z7 of cl.exe, ref: https://learn.microsoft.com/en-us/cpp/build/reference/z7-zi-zi-debug-information-format -->
<DebugInformationFormat>OldStyle</DebugInformationFormat>
<ObjectFileName>$(IntDir)%(FileName).obj</ObjectFileName>
</ClCompile>
</ItemDefinitionGroup>
</Project>
7 changes: 6 additions & 1 deletion vcbuild.bat
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set target_env=
set noprojgen=
set projgen=
set clang_cl=
set ccache_path=
set nobuild=
set sign=
set nosnapshot=
Expand Down Expand Up @@ -87,6 +88,7 @@ if /i "%1"=="vs2022" set target_env=vs2022&goto arg-ok
if /i "%1"=="noprojgen" set noprojgen=1&goto arg-ok
if /i "%1"=="projgen" set projgen=1&goto arg-ok
if /i "%1"=="clang-cl" set clang_cl=1&goto arg-ok
if /i "%1"=="ccache" set "ccache_path=%2%"&goto arg-ok-2
if /i "%1"=="nobuild" set nobuild=1&goto arg-ok
if /i "%1"=="nosign" set "sign="&echo Note: vcbuild no longer signs by default. "nosign" is redundant.&goto arg-ok
if /i "%1"=="sign" set sign=1&goto arg-ok
Expand Down Expand Up @@ -206,6 +208,7 @@ if defined debug_nghttp2 set configure_flags=%configure_flags% --debug-nghttp
if defined openssl_no_asm set configure_flags=%configure_flags% --openssl-no-asm
if defined no_shared_roheap set configure_flags=%configure_flags% --disable-shared-readonly-heap
if defined DEBUG_HELPER set configure_flags=%configure_flags% --verbose
if defined ccache_path set configure_flags=%configure_flags% --ccache-used
if defined compile_commands set configure_flags=%configure_flags% -C

if "%target_arch%"=="x86" (
Expand Down Expand Up @@ -364,6 +367,7 @@ if "%target%"=="Build" (
if "%target%"=="node" if exist "%config%\cctest.exe" del "%config%\cctest.exe"
if "%target%"=="node" if exist "%config%\embedtest.exe" del "%config%\embedtest.exe"
if defined msbuild_args set "extra_msbuild_args=%extra_msbuild_args% %msbuild_args%"
if defined ccache_path set "extra_msbuild_args=%extra_msbuild_args% /p:TrackFileAccess=false /p:CLToolPath=%ccache_path% /p:ForceImportAfterCppProps=%CD%\tools\msvs\props_4_ccache.props"
@rem Setup env variables to use multiprocessor build
set UseMultiToolTask=True
set EnforceProcessCountAcrossBuilds=True
Expand Down Expand Up @@ -800,7 +804,7 @@ set exit_code=1
goto exit

:help
echo vcbuild.bat [debug/release] [msi] [doc] [test/test-all/test-addons/test-doc/test-js-native-api/test-node-api/test-internet/test-tick-processor/test-known-issues/test-node-inspect/test-check-deopts/test-npm/test-v8/test-v8-intl/test-v8-benchmarks/test-v8-all] [ignore-flaky] [static/dll] [noprojgen] [projgen] [clang-cl] [small-icu/full-icu/without-intl] [nobuild] [nosnapshot] [nonpm] [nocorepack] [ltcg] [licensetf] [sign] [x64/arm64] [vs2022] [download-all] [enable-vtune] [lint/lint-ci/lint-js/lint-md] [lint-md-build] [format-md] [package] [build-release] [upload] [no-NODE-OPTIONS] [link-module path-to-module] [debug-http2] [debug-nghttp2] [clean] [cctest] [no-cctest] [openssl-no-asm]
echo vcbuild.bat [debug/release] [msi] [doc] [test/test-all/test-addons/test-doc/test-js-native-api/test-node-api/test-internet/test-tick-processor/test-known-issues/test-node-inspect/test-check-deopts/test-npm/test-v8/test-v8-intl/test-v8-benchmarks/test-v8-all] [ignore-flaky] [static/dll] [noprojgen] [projgen] [clang-cl] [ccache path-to-ccache] [small-icu/full-icu/without-intl] [nobuild] [nosnapshot] [nonpm] [nocorepack] [ltcg] [licensetf] [sign] [x64/arm64] [vs2022] [download-all] [enable-vtune] [lint/lint-ci/lint-js/lint-md] [lint-md-build] [format-md] [package] [build-release] [upload] [no-NODE-OPTIONS] [link-module path-to-module] [debug-http2] [debug-nghttp2] [clean] [cctest] [no-cctest] [openssl-no-asm]
echo Examples:
echo vcbuild.bat : builds release build
echo vcbuild.bat debug : builds debug build
Expand All @@ -811,6 +815,7 @@ echo vcbuild.bat enable-vtune : builds Node.js with Intel VTune pr
echo vcbuild.bat link-module my_module.js : bundles my_module as built-in module
echo vcbuild.bat lint : runs the C++, documentation and JavaScript linter
echo vcbuild.bat no-cctest : skip building cctest.exe
echo vcbuild.bat ccache c:\ccache\ : use ccache to speed build
goto exit

:exit
Expand Down
Loading