Skip to content

Commit ed3492d

Browse files
targoslemire
andcommitted
build: add option to enable clang-cl on Windows
Most changes are gated by the `clang==1` condition to avoid breaking MSVC builds. Select C/C++ language standard with ClCompile options. This avoids passing the `-std:c++20` flag while compiling C code. Do it only under clang option to avoid breaking addons until node-gyp supports the new LanguageStandard options. Disable precompiled header configuration for now as it doesn't seem to work with clang-cl. Disable C++20 warnings emitted by the Visual Studio C++ STL. They're very noisy and not our responsibility to fix. Co-authored-by: Daniel Lemire <[email protected]>
1 parent a923fed commit ed3492d

File tree

8 files changed

+114
-49
lines changed

8 files changed

+114
-49
lines changed

common.gypi

+24-2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@
152152
'cflags': [ '-fPIC' ],
153153
'ldflags': [ '-fPIC' ]
154154
}],
155+
['clang==1', {
156+
'msbuild_toolset': 'ClangCL',
157+
}],
155158
],
156159
'msvs_settings': {
157160
'VCCLCompilerTool': {
@@ -240,6 +243,9 @@
240243
'cflags': [ '-fPIC', '-I<(android_ndk_path)/sources/android/cpufeatures' ],
241244
'ldflags': [ '-fPIC' ]
242245
}],
246+
['clang==1', {
247+
'msbuild_toolset': 'ClangCL',
248+
}],
243249
],
244250
'msvs_settings': {
245251
'VCCLCompilerTool': {
@@ -279,13 +285,29 @@
279285
'_target_name!="<(node_core_target_name)")', {
280286
'cflags!': ['-Werror'],
281287
}],
288+
# TODO(targos): Remove condition and always use LanguageStandard options
289+
# once node-gyp supports them.
290+
['clang==1', {
291+
'msvs_settings': {
292+
'VCCLCompilerTool': {
293+
'LanguageStandard': 'stdcpp20',
294+
'LanguageStandard_C': 'stdc11',
295+
},
296+
},
297+
}, {
298+
'msvs_settings': {
299+
'VCCLCompilerTool': {
300+
'AdditionalOptions': [
301+
'-std:c++20',
302+
],
303+
},
304+
},
305+
}],
282306
],
283307
'msvs_settings': {
284308
'VCCLCompilerTool': {
285309
'AdditionalOptions': [
286310
'/Zc:__cplusplus',
287-
# The following option enables c++20 on Windows. This is needed for V8 v12.4+
288-
'-std:c++20',
289311
# The following option reduces the "error C1060: compiler is out of heap space"
290312
'/Zm2000',
291313
],

configure.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,13 @@
866866
default=None,
867867
help=argparse.SUPPRESS)
868868

869+
parser.add_argument('--clang-cl',
870+
action='store',
871+
dest='clang_cl',
872+
default=None,
873+
help='Configure for clang-cl on Windows. This flag sets the GYP "clang" ' +
874+
'variable to 1 and "llvm_version" to the specified value.')
875+
869876
(options, args) = parser.parse_known_args()
870877

871878
# Expand ~ in the install prefix now, it gets written to multiple files.
@@ -1042,8 +1049,9 @@ def get_gas_version(cc):
10421049
# quite prepared to go that far yet.
10431050
def check_compiler(o):
10441051
if sys.platform == 'win32':
1045-
o['variables']['clang'] = 0
1046-
o['variables']['llvm_version'] = '0.0'
1052+
if options.clang_cl:
1053+
o['variables']['clang'] = 1
1054+
o['variables']['llvm_version'] = options.clang_cl
10471055
if not options.openssl_no_asm and options.dest_cpu in ('x86', 'x64'):
10481056
nasm_version = get_nasm_version('nasm')
10491057
o['variables']['nasm_version'] = nasm_version

deps/zlib/zlib.gyp

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
'xcode_settings': {
2929
'OTHER_CFLAGS': [ '-mssse3' ],
3030
},
31+
'msvs_settings': {
32+
'VCCLCompilerTool': {
33+
'AdditionalOptions': [ '-mssse3' ],
34+
},
35+
},
3136
}],
3237
],
3338
}],

node.gyp

+2-2
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,8 @@
610610
'msvs_settings': {
611611
'VCLinkerTool': {
612612
'AdditionalOptions': [
613-
'/WHOLEARCHIVE:<(node_lib_target_name)<(STATIC_LIB_SUFFIX)',
614-
'/WHOLEARCHIVE:<(STATIC_LIB_PREFIX)v8_base_without_compiler<(STATIC_LIB_SUFFIX)',
613+
'/WHOLEARCHIVE:<(PRODUCT_DIR)/lib/<(node_lib_target_name)<(STATIC_LIB_SUFFIX)',
614+
'/WHOLEARCHIVE:<(PRODUCT_DIR)/lib/<(STATIC_LIB_PREFIX)v8_base_without_compiler<(STATIC_LIB_SUFFIX)',
615615
],
616616
},
617617
},

node.gypi

+12-8
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,15 @@
6565
'NODE_PLATFORM="win32"',
6666
'_UNICODE=1',
6767
],
68-
'msvs_precompiled_header': 'tools/msvs/pch/node_pch.h',
69-
'msvs_precompiled_source': 'tools/msvs/pch/node_pch.cc',
70-
'sources': [
71-
'<(_msvs_precompiled_header)',
72-
'<(_msvs_precompiled_source)',
68+
'conditions': [
69+
['clang==0', {
70+
'msvs_precompiled_header': 'tools/msvs/pch/node_pch.h',
71+
'msvs_precompiled_source': 'tools/msvs/pch/node_pch.cc',
72+
'sources': [
73+
'<(_msvs_precompiled_header)',
74+
'<(_msvs_precompiled_source)',
75+
],
76+
}],
7377
],
7478
}, { # POSIX
7579
'defines': [ '__POSIX__' ],
@@ -148,7 +152,7 @@
148152
'msvs_settings': {
149153
'VCLinkerTool': {
150154
'AdditionalOptions': [
151-
'/WHOLEARCHIVE:zlib<(STATIC_LIB_SUFFIX)',
155+
'/WHOLEARCHIVE:<(PRODUCT_DIR)/lib/zlib<(STATIC_LIB_SUFFIX)',
152156
],
153157
},
154158
},
@@ -187,7 +191,7 @@
187191
'msvs_settings': {
188192
'VCLinkerTool': {
189193
'AdditionalOptions': [
190-
'/WHOLEARCHIVE:libuv<(STATIC_LIB_SUFFIX)',
194+
'/WHOLEARCHIVE:<(PRODUCT_DIR)/lib/libuv<(STATIC_LIB_SUFFIX)',
191195
],
192196
},
193197
},
@@ -366,7 +370,7 @@
366370
'msvs_settings': {
367371
'VCLinkerTool': {
368372
'AdditionalOptions': [
369-
'/WHOLEARCHIVE:<(openssl_product)',
373+
'/WHOLEARCHIVE:<(PRODUCT_DIR)/lib/<(openssl_product)',
370374
],
371375
},
372376
},

tools/v8_gypfiles/toolchain.gypi

+6
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@
160160
# -Wno-invalid-offsetof
161161
'GCC_WARN_ABOUT_INVALID_OFFSETOF_MACRO': 'NO',
162162
},
163+
'msvs_settings': {
164+
'VCCLCompilerTool': {
165+
'AdditionalOptions': ['-Wno-invalid-offsetof'],
166+
},
167+
},
163168
}],
164169
['v8_target_arch=="arm"', {
165170
'defines': [
@@ -536,6 +541,7 @@
536541
'WIN32',
537542
'NOMINMAX', # Refs: https://chromium-review.googlesource.com/c/v8/v8/+/1456620
538543
'_WIN32_WINNT=0x0602', # Windows 8
544+
'_SILENCE_ALL_CXX20_DEPRECATION_WARNINGS',
539545
],
540546
# 4351: VS 2005 and later are warning us that they've fixed a bug
541547
# present in VS 2003 and earlier.

tools/v8_gypfiles/v8.gyp

+50-34
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
'type': 'none',
4949
'toolsets': ['host', 'target'],
5050
'conditions': [
51-
['OS=="win"', {
51+
['OS=="win" and clang==0', {
5252
'direct_dependent_settings': {
5353
'msvs_precompiled_header': '<(V8_ROOT)/../../tools/msvs/pch/v8_pch.h',
5454
'msvs_precompiled_source': '<(V8_ROOT)/../../tools/msvs/pch/v8_pch.cc',
@@ -381,38 +381,6 @@
381381
'target_name': 'v8_snapshot',
382382
'type': 'static_library',
383383
'toolsets': ['target'],
384-
'conditions': [
385-
['want_separate_host_toolset', {
386-
'conditions': [
387-
['v8_target_arch=="arm64"', {
388-
'msvs_enable_marmasm': 1,
389-
}]
390-
],
391-
'dependencies': [
392-
'generate_bytecode_builtins_list',
393-
'run_torque',
394-
'mksnapshot#host',
395-
'v8_maybe_icu',
396-
# [GYP] added explicitly, instead of inheriting from the other deps
397-
'v8_base_without_compiler',
398-
'v8_compiler_for_mksnapshot',
399-
'v8_initializers',
400-
'v8_libplatform',
401-
]
402-
}, {
403-
'dependencies': [
404-
'generate_bytecode_builtins_list',
405-
'run_torque',
406-
'mksnapshot',
407-
'v8_maybe_icu',
408-
# [GYP] added explicitly, instead of inheriting from the other deps
409-
'v8_base_without_compiler',
410-
'v8_compiler_for_mksnapshot',
411-
'v8_initializers',
412-
'v8_libplatform',
413-
]
414-
}],
415-
],
416384
'sources': [
417385
'<(V8_ROOT)/src/init/setup-isolate-deserialize.cc',
418386
],
@@ -488,6 +456,54 @@
488456
],
489457
},
490458
],
459+
'conditions': [
460+
['want_separate_host_toolset', {
461+
'dependencies': [
462+
'generate_bytecode_builtins_list',
463+
'run_torque',
464+
'mksnapshot#host',
465+
'v8_maybe_icu',
466+
# [GYP] added explicitly, instead of inheriting from the other deps
467+
'v8_base_without_compiler',
468+
'v8_compiler_for_mksnapshot',
469+
'v8_initializers',
470+
'v8_libplatform',
471+
]
472+
}, {
473+
'dependencies': [
474+
'generate_bytecode_builtins_list',
475+
'run_torque',
476+
'mksnapshot',
477+
'v8_maybe_icu',
478+
# [GYP] added explicitly, instead of inheriting from the other deps
479+
'v8_base_without_compiler',
480+
'v8_compiler_for_mksnapshot',
481+
'v8_initializers',
482+
'v8_libplatform',
483+
]
484+
}],
485+
['OS=="win" and clang==1', {
486+
'actions': [
487+
{
488+
'action_name': 'asm_to_inline_asm',
489+
'message': 'generating: >@(_outputs)',
490+
'inputs': [
491+
'<(INTERMEDIATE_DIR)/embedded.S',
492+
],
493+
'outputs': [
494+
'<(INTERMEDIATE_DIR)/embedded.cc',
495+
],
496+
'process_outputs_as_sources': 1,
497+
'action': [
498+
'<(python)',
499+
'<(V8_ROOT)/tools/snapshot/asm_to_inline_asm.py',
500+
'<@(_inputs)',
501+
'<(INTERMEDIATE_DIR)/embedded.cc',
502+
],
503+
},
504+
],
505+
}],
506+
],
491507
}, # v8_snapshot
492508
{
493509
'target_name': 'v8_version',
@@ -1928,7 +1944,7 @@
19281944
}],
19291945
]
19301946
}],
1931-
['OS=="win"', {
1947+
['OS=="win" and clang==0', {
19321948
'conditions': [
19331949
['_toolset == "host" and host_arch == "x64" or _toolset == "target" and target_arch=="x64"', {
19341950
'sources': [

vcbuild.bat

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set ltcg=
3232
set target_env=
3333
set noprojgen=
3434
set projgen=
35+
set clang_cl=
3536
set nobuild=
3637
set sign=
3738
set nosnapshot=
@@ -87,6 +88,7 @@ if /i "%1"=="arm64" set target_arch=arm64&goto arg-ok
8788
if /i "%1"=="vs2022" set target_env=vs2022&goto arg-ok
8889
if /i "%1"=="noprojgen" set noprojgen=1&goto arg-ok
8990
if /i "%1"=="projgen" set projgen=1&goto arg-ok
91+
if /i "%1"=="clang-cl" set clang_cl=1&goto arg-ok
9092
if /i "%1"=="nobuild" set nobuild=1&goto arg-ok
9193
if /i "%1"=="nosign" set "sign="&echo Note: vcbuild no longer signs by default. "nosign" is redundant.&goto arg-ok
9294
if /i "%1"=="sign" set sign=1&goto arg-ok
@@ -190,6 +192,8 @@ if defined nosnapshot set configure_flags=%configure_flags% --without-snap
190192
if defined nonpm set configure_flags=%configure_flags% --without-npm
191193
if defined nocorepack set configure_flags=%configure_flags% --without-corepack
192194
if defined ltcg set configure_flags=%configure_flags% --with-ltcg
195+
:: If clang-cl build is requested, set it to 17.0, which is the version shipped with VS 2022.
196+
if defined clang_cl set configure_flags=%configure_flags% --clang-cl=17.0
193197
if defined release_urlbase set configure_flags=%configure_flags% --release-urlbase=%release_urlbase%
194198
if defined download_arg set configure_flags=%configure_flags% %download_arg%
195199
if defined enable_vtune_arg set configure_flags=%configure_flags% --enable-vtune-profiling
@@ -750,7 +754,7 @@ set exit_code=1
750754
goto exit
751755

752756
:help
753-
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] [small-icu/full-icu/without-intl] [nobuild] [nosnapshot] [nonpm] [nocorepack] [ltcg] [licensetf] [sign] [ia32/x86/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]
757+
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] [small-icu/full-icu/without-intl] [nobuild] [nosnapshot] [nonpm] [nocorepack] [ltcg] [licensetf] [sign] [ia32/x86/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]
754758
echo Examples:
755759
echo vcbuild.bat : builds release build
756760
echo vcbuild.bat debug : builds debug build

0 commit comments

Comments
 (0)