-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Overhaul how we search for clang-format #4888
Conversation
tools/format/CMakeLists.txt
Outdated
find_program(CLANG_FORMAT | ||
NAMES clang-format | ||
PATHS "C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/Llvm/bin" | ||
HINTS "$ENV{VCINSTALLDIR}/Tools/Llvm/$ENV{VSCMD_ARG_HOST_ARCH}/bin" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VSCMD_ARG_HOST_ARCH
is probably wrong.
I have only C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\Llvm\x64\bin
and C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\Llvm\ARM64\bin
but %VSCMD_ARG_HOST_ARCH%
is x86
if I open x86 Native tools
and I want to build x86
library.
And because ARM machines can run x64 binary I think we can change $ENV{VSCMD_ARG_HOST_ARCH}
to x64
. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, yes, I forgot that x86 LLVM isn't in an x86
directory as is the case for x64
and arm64
. I agree we should simply default to x64
for now. We can devise something clever if anyone ever complains.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! 😻 The x86 clang-format is lurking in C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\Llvm\bin\clang-format.exe
but I also agree, there's no point in attempting to ever use it. Validated the change:
Click to expand successful formatting, x64-native:
C:\Temp>"C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.12.0-pre.1.0
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
C:\Temp>where clang-format
C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\Llvm\x64\bin\clang-format.exe
C:\Temp>pushd D:\GitHub\STL && cmake --preset x64 && cmake --build --preset x64 --target format
-- The CXX compiler identification is MSVC 19.42.34226.3
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.42.34226/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test WINDOWS_SDK_VERSION_CHECK
-- Performing Test WINDOWS_SDK_VERSION_CHECK - Success
-- The ASM_MASM compiler identification is MSVC
-- Found assembler: C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.42.34226/bin/Hostx64/x64/ml64.exe
-- Found clang-format 17.0.3.
-- Found Python: C:/Users/stl/AppData/Local/Programs/Python/Python312/python.exe (found suitable version "3.12.5", minimum required is "3.12") found components: Interpreter
-- Boost.Math: standalone mode ON
-- Configuring done (1.4s)
-- Generating done (0.2s)
-- Build files have been written to: D:/GitHub/STL/out/x64
[1328/1328] C:\WINDOWS\system32\cmd.exe /C "cd /D D:\GitHu...ub/STL/tools/format/../../tests/tr1/tests/regex2/test.cpp"
Click to expand successful formatting, x86-native (and observe different where clang-format
location):
C:\Temp>"C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Auxiliary\Build\vcvarsall.bat" x86
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.12.0-pre.1.0
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'
C:\Temp>where clang-format
C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\Llvm\bin\clang-format.exe
C:\Temp>pushd D:\GitHub\STL && cmake --preset x86 && cmake --build --preset x86 --target format
-- The CXX compiler identification is MSVC 19.42.34226.3
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.42.34226/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test WINDOWS_SDK_VERSION_CHECK
-- Performing Test WINDOWS_SDK_VERSION_CHECK - Success
-- The ASM_MASM compiler identification is MSVC
-- Found assembler: C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.42.34226/bin/Hostx86/x86/ml.exe
-- Found clang-format 17.0.3.
-- Found Python: C:/Users/stl/AppData/Local/Programs/Python/Python312/python.exe (found suitable version "3.12.5", minimum required is "3.12") found components: Interpreter
-- Boost.Math: standalone mode ON
-- Configuring done (1.5s)
-- Generating done (0.2s)
-- Build files have been written to: D:/GitHub/STL/out/x86
[1328/1328] C:\WINDOWS\system32\cmd.exe /C "cd /D D:\GitHu...ub/STL/tools/format/../../tests/tr1/tests/regex2/test.cpp"
Despite x86's where clang-format
difference, both CMake builds used the x64 clang-format.exe:
D:\GitHub\STL>rg CLANG_FORMAT out\x64\CMakeCache.txt out\x86\CMakeCache.txt
out\x64\CMakeCache.txt
24:CLANG_FORMAT:FILEPATH=C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/Llvm/x64/bin/clang-format.exe
out\x86\CMakeCache.txt
24:CLANG_FORMAT:FILEPATH=C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/Llvm/x64/bin/clang-format.exe
This is a nice verification that even on my machine (with only VS Preview's Clang/LLVM installed, no other copies), the HINTS
take effect.
I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed. |
Our CMake machinery for clang-format is awesome, but it's remained largely unchanged since being added by #2671 on 2022-05-01. Thanks to @TheStormN for bringing my attention to issues here.
The first problem is that we're looking for the clang-format executable in a hardcoded path. That's easily broken if a contributor has installed VS Preview to a non-default location.
Second, during configuration of the STL build, we're simply warning about clang-format being missing. This isn't really helpful - to be successful, contributors really need to have both Clang installed (for testing) and clang-format (to make all but the most trivial source changes without over-relying on PR checks).
Third, we weren't validating what version of clang-format we're picking up. clang-format routinely changes behavior between different major versions, so it's important for all contributors to use the same version that the PR checks are validating.
I'm still a CMake novice, but I've managed to overhaul how we handle clang-format here.
format
target is invoked later, and this isn't going to affect whether we emit warnings or errors.find_program
to search hardcodedPATHS
with no defaults (aside: justNO_DEFAULT_PATH
would have been sufficient, everything below was redundant), actually use CMake's default behavior, which will search the environment'sPATH
(among other things). This is how we expect to pick upcl.exe
,clang-cl.exe
, etc.REQUIRED
. This will emit a hard error ifclang-format
isn't found. (Tested by temporarily removing LLVM from myPATH
.)execute_process
to runclang-format --version
and inspect the output.OUTPUT_STRIP_TRAILING_WHITESPACE
is necessary to strip newlines.