Chrome and V8 support building with Clang on windows. Node.js should include support for this too.
Benefits:
Blockers:
configure.ninja. However, gyp backend for ninja only supports MSVC.openssl.gyp requires llvm_version to be set.python .\deps\v8\tools\clang\scripts\update.py.clang-cl.exe./cc @targos
/cc @nodejs/platform-windows
Official way to build V8 on Windows is to use ninja. However, gyp backend for ninja only supports MSVC.
How can we solve this?
Good question. One of the reasons for moving off gyp is scenarios like this where new toolchains are not supported. I guess implementing support for ninja with clang on Windows would be a way. It might be not all that complicated, if clang on Windows works the same way as clang on linux.
You mean implementing it in gyp?
I suspect this already works when you use clang-cl, the cl.exe compatible driver.
Untested, but configure would need to write out a make_global_settings stanza to config.gypi that looks like this and will be picked up by the ninja generator:
# ...
'make_global_settings': {
'CC': '/path/to/clang-cl', # or just 'clang-cl' if it's on the path
'CXX': '/path/to/clang-cl', # ditto
}
# ...
bnoordhuis's comment is correct, except you don't need to set CXX since the ninja generator doesn't look at that in msvc compat mode (cf https://chromium.googlesource.com/external/gyp/+/e540e6ab6291d4e63f262c8169ef0d0aae11dbec / https://codereview.chromium.org/147083011 for how we hooked up clang-cl in the Chromium gyp win build back in the day).
The amazing @agrieve from the Chrome team has come up with this PR (against the fork of Node.js that V8 is maintaining) to make building with Clang on Windows work!
We could use some feedback on whether this way to build works well for everyone, similar to when I initially implemented the --build-v8-with-gn configure flag. @seishun @targos
Although as @bnoordhuis mentions there are workarounds for cmake/ninja/GYP/Windows, I'll look into getting that to work out of the box.
This still does not remove the VisualStudio dependency per se, since clang-win / clang-cl and the Windows SDK are still interdependent, so IMHO the more "interesting" issue IMHO is smooth integration with the Windows SDK.
[1]
There might be conflicting interests with the @nodejs/chakracore team. But IMHO that should be upstreamed to V8 & Chromium.
There might be conflicting interests with the @nodejs/chakracore team. But IMHO that should be upstreamed to V8 & Chromium.
My interpretation is that having alternatives is always great :)
So if this doesn't remove the dependency on Visual Studio, the only benefit would be that we'd have to do this at some point if V8 drop support for MSVC?
FWIW we have a fork of Node that can fully build with clang-cl on Windows.
It still requires Visual Studio and some dependencies to be installed.
The benefit of that build is that the gyp-gn bridge works on that build. I.e. gyp files do not need to be maintained when updated to a new V8 version. It is also guaranteed to build if V8 decides to deprecate MSVC support in the future. This is relevant when MSVC and Clang differ in how they interpret C++ syntax.
Dependencies:
deps/v8/build/toolchain/win/setup_toolchain.py.Build steps:
python configure --build-v8-with-gn --use-clang-cl --ninja
deps\v8\_depot_tools\ninja -C out\Release node
There is obviously still a bit of a way to go here.
What also doesn't work is that add-on tests do not have a ninja target, and npm install probably doesn't work with clang-cl.
Edit: missing dependency.
The list of dependencies seems incomplete. I'm getting the following error:
FAILED: obj/deps/v8/gypfiles/v8_monolith.gen/gn/build.ninja
C:\Python27\python.exe gyp-win-tool action-wrapper environment.x64 v8_monolith_target_build_with_gn_generate_build_files_f3270641800ff32515e443515bf752cc..rsp ..\..\deps\v8\gypfiles
Traceback (most recent call last):
File "C:/Users/Nikolai/node/deps/v8/build/vs_toolchain.py", line 482, in <module>
sys.exit(main())
File "C:/Users/Nikolai/node/deps/v8/build/vs_toolchain.py", line 478, in main
return commands[sys.argv[1]](*sys.argv[2:])
File "C:/Users/Nikolai/node/deps/v8/build/vs_toolchain.py", line 306, in CopyDlls
_CopyDebugger(target_dir, target_cpu)
File "C:/Users/Nikolai/node/deps/v8/build/vs_toolchain.py", line 337, in _CopyDebugger
' 10 SDK.' % (debug_file, full_path))
Exception: dbghelp.dll not found in "C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\dbghelp.dll"
You must install the "Debugging Tools for Windows" feature from the Windows 10 SDK.
ERROR at //build/toolchain/win/BUILD.gn:43:3: Script returned non-zero exit code.
exec_script("../../vs_toolchain.py",
^----------
Current dir: C:/Users/Nikolai/node/out/Release/obj/deps/v8/gypfiles/v8_monolith.gen/gn/
Command: C:/Python27/python.exe -- C:/Users/Nikolai/node/deps/v8/build/vs_toolchain.py copy_dlls C:/Users/Nikolai/node/out/Release/obj/deps/v8/gypfiles/v8_monolith.gen/gn Release x64
Returned 1.
See //BUILD.gn:618:1: which caused the file to be included.
action("js2c") {
^---------------
Traceback (most recent call last):
File "..\tools\node\build_gn.py", line 141, in <module>
GenerateBuildFiles(options)
File "..\tools\node\build_gn.py", line 75, in GenerateBuildFiles
subprocess.check_call(args)
File "C:\Python27\lib\subprocess.py", line 540, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['C:\\Users\\Nikolai\\node\\deps\\v8\\buildtools\\win\\gn', 'gen', 'C:\\Users\\Nikolai\\node\\out\\Release\\obj\\deps\\v8\\gypfiles\\v8_monolith.gen\\gn', '-q', '--args=v8_monolithic=true is_component_build=false v8_use_external_startup_data=false use_custom_libcxx=false v8_promise_internal_field_count=true target_cpu="x64" target_os="win" v8_target_cpu="x64" v8_embedder_string="-node.4" v8_use_snapshot=true v8_optimized_debug=false v8_enable_disassembler=true v8_postmortem_support=false is_debug=false clang_base_path="C:\\Users\\Nikolai\\node\\deps\\v8\\third_party\\llvm-build\\Release+Asserts"']' returned non-zero exit status 1
I'm also getting a warning on every file, is that a known issue?
[20/664] CC obj\deps\openssl\openssl\crypto\ecdsa\openssl.ecs_err.obj
clang-cl.exe: warning: argument unused during compilation: '/GL' [-Wunused-command-line-argument]
[21/664] CC obj\deps\openssl\openssl\crypto\engine\openssl.tb_rand.obj
clang-cl.exe: warning: argument unused during compilation: '/GL' [-Wunused-command-line-argument]
[22/664] CC obj\deps\openssl\openssl\crypto\ecdh\openssl.ech_kdf.obj
clang-cl.exe: warning: argument unused during compilation: '/GL' [-Wunused-command-line-argument]
[23/664] CC obj\deps\openssl\openssl\crypto\ec\openssl.ecp_oct.obj
clang-cl.exe: warning: argument unused during compilation: '/GL' [-Wunused-command-line-argument]
That might indeed by true. I've seen that error too and solved it by following the hint in the error log, and installed Debugging Tools for Windows.
I've seen the same warning too. The issue here is that Clang does not support the whole program optimization flag. V8 files are not affected by this.
Getting the following error now:
C:\Users\Nikolai\node>deps\v8\_depot_tools\ninja -C out\Release node
ninja: Entering directory `out\Release'
[0/13] ACTION v8_monolith: build_with_gn_f3270641800ff32515e443515bf752cc
Using depot tools in ..\_depot_tools
ninja: Entering directory `C:\Users\Nikolai\node\out\Release\obj\deps\v8\gypfiles\v8_monolith.gen\gn'
ninja: no work to do.
[8/12] CXX obj\src\node_lib.node_win32_perfctr_provider.obj
FAILED: obj/src/node_lib.node_win32_perfctr_provider.obj
ninja -t msvc -e environment.x64 -- "C:\Users\Nikolai\node\deps\v8\third_party\llvm-build\Release+Asserts\bin\clang-cl" -m64 /nologo /showIncludes /FC @obj\src\node_lib.node_win32_perfctr_provider.obj.rsp /c ..\..\src\node_win32_perfctr_provider.cc /Foobj\src\node_lib.node_win32_perfctr_provider.obj /Fdobj\node_lib.cc.pdb
clang-cl.exe: warning: argument unused during compilation: '/GL' [-Wunused-command-line-argument]
In file included from ..\..\src\node_win32_perfctr_provider.cc:28:
..\..\tools\msvs\genfiles\node_perfctr_provider.h(19,90): warning: suggest braces around initialization of subobject [-Wmissing-braces]
EXTERN_C DECLSPEC_SELECTANY GUID NodeCounterProviderGuid = { 0x793c9b44, 0x3d6b, 0x4f57, 0xb5, 0xd7, 0x4f, 0xf8, 0xa, 0xdc, 0xf9, 0xa2 };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ }
..\..\tools\msvs\genfiles\node_perfctr_provider.h(21,85): warning: suggest braces around initialization of subobject [-Wmissing-braces]
EXTERN_C DECLSPEC_SELECTANY GUID NodeCounterSetGuid = { 0x3a22a8ec, 0x297c, 0x48ac, 0xab, 0x15, 0x33, 0xec, 0x93, 0x3, 0x3f, 0xd8 };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ }
..\..\tools\msvs\genfiles\node_perfctr_provider.h(39,37): warning: suggest braces around initialization of subobject [-Wmissing-braces]
{ { 0x3a22a8ec, 0x297c, 0x48ac, 0xab, 0x15, 0x33, 0xec, 0x93, 0x3, 0x3f, 0xd8 }, { 0x793c9b44, 0x3d6b, 0x4f57, 0xb5, 0xd7, 0x4f, 0xf8, 0xa, 0xdc, 0xf9, 0xa2 }, 10, PERF_COUNTERSET_MULTI_AGGREGATE },
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ }
..\..\tools\msvs\genfiles\node_perfctr_provider.h(39,116): warning: suggest braces around initialization of subobject [-Wmissing-braces]
{ { 0x3a22a8ec, 0x297c, 0x48ac, 0xab, 0x15, 0x33, 0xec, 0x93, 0x3, 0x3f, 0xd8 }, { 0x793c9b44, 0x3d6b, 0x4f57, 0xb5, 0xd7, 0x4f, 0xf8, 0xa, 0xdc, 0xf9, 0xa2 }, 10, PERF_COUNTERSET_MULTI_AGGREGATE },
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ }
..\..\tools\msvs\genfiles\node_perfctr_provider.h(45,77): warning: implicit conversion from 'long long' to 'LONG' (aka 'long') changes value from 4294967293 to -3 [-Wconstant-conversion]
{ 6, PERF_COUNTER_BULK_COUNT, 0, sizeof(ULONGLONG), PERF_DETAIL_NOVICE, 4294967293, 0 },
~ ^~~~~~~~~~
..\..\tools\msvs\genfiles\node_perfctr_provider.h(46,77): warning: implicit conversion from 'long long' to 'LONG' (aka 'long') changes value from 4294967293 to -3 [-Wconstant-conversion]
{ 7, PERF_COUNTER_BULK_COUNT, 0, sizeof(ULONGLONG), PERF_DETAIL_NOVICE, 4294967293, 0 },
~ ^~~~~~~~~~
..\..\tools\msvs\genfiles\node_perfctr_provider.h(48,77): warning: implicit conversion from 'long long' to 'LONG' (aka 'long') changes value from 4294967293 to -3 [-Wconstant-conversion]
{ 9, PERF_COUNTER_BULK_COUNT, 0, sizeof(ULONGLONG), PERF_DETAIL_NOVICE, 4294967293, 0 },
~ ^~~~~~~~~~
..\..\tools\msvs\genfiles\node_perfctr_provider.h(49,78): warning: implicit conversion from 'long long' to 'LONG' (aka 'long') changes value from 4294967293 to -3 [-Wconstant-conversion]
{ 10, PERF_COUNTER_BULK_COUNT, 0, sizeof(ULONGLONG), PERF_DETAIL_NOVICE, 4294967293, 0 },
~ ^~~~~~~~~~
..\..\src\node_win32_perfctr_provider.cc(122,36): error: declaration of 'NodeCounterProvider' has a different language linkage
EXTERN_C DECLSPEC_SELECTANY HANDLE NodeCounterProvider = nullptr;
^
..\..\src/node_win32_perfctr_provider.h(35,15): note: previous declaration is here
extern HANDLE NodeCounterProvider;
^
8 warnings and 1 error generated.
[9/12] CXX obj\gen\node_lib.node_javascript.obj
clang-cl.exe: warning: argument unused during compilation: '/GL' [-Wunused-command-line-argument]
ninja: build stopped: subcommand failed.
Thanks for making it this far! That's the last remaining issue. The fix is here. I wasn't sure whether it's the right fix, which is why I haven't landed it yet.
It builds now. A few questions:
Yup. It's not perfect yet. I'm working on making all of this smoother.
You should be able to run tests by using the test script in tools directly.
So what's the plan now? Are you going to create a PR here?
I want to figure out how far we can go with reducing the dependencies before upstreaming anything. Also, this is not particularly urgent.
This is an interesting issue, I'll try to pick it...
Actually fetching Clang. This is done in V8 through python .\deps\v8\tools\clang\scripts\update.py.
IIUC non googlers can't do that. The docs say "install MSVS and set DEPOT_TOOLS_WIN_TOOLCHAIN=0"
But I'm making progress using the "official" prebuilt llvm binaries, and the ninja-msvs GYP generator.
found a bug in V8's src/base/debug/stack_trace_win.cc (mix and max MACROS breaking compilation with "official" prebuilt llvm binaries).
CL: https://chromium-review.googlesource.com/c/v8/v8/+/1297479
There's been no further activity on this and it's not clear if it's going to move forward. Closing but we can reopen if necessary
This is a very necessary feature. MSVC is not an optimized compiler. Can we open this again?