microsoft / vscode-cmake-tools

CMake integration in Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=vector-of-bool.cmake-tools
MIT License
1.48k stars 454 forks source link

Cache is not deleted when switching to kit with different target arch, but same host arch #2699

Closed TishSerg closed 4 months ago

TishSerg commented 2 years ago

Brief Issue Summary

The cache is not deleted when switching to a kit with a different target arch, but the same host arch. It results in error during building:

[cmake] CMake Error: Error: generator platform: win32
[cmake] Does not match the platform used previously: x64
[cmake] Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.

I detailed this in the Additional Info section. If logs are still needed, let me know. I'll add them.

CMake Tools Diagnostics

No response

Debug Log

No response

Additional Information

I want to compile an MSVC project for x86 and x64. I do switch target arch by switching the kit in CMake Tools Statusbar: image As you can see, there are 4 compilers/kits available:

  1. C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe
  2. C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x86/cl.exe
  3. C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.33.31629/bin/Hostx86/x86/cl.exe
  4. C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.33.31629/bin/Hostx86/x64/cl.exe

The host arch is tagged with a green line in the picture, and the target arch is tagged with a cyan line.

When I switch between the kits with the different host arch, CMake Cache gets removed. Example of switching from kit 1 to kit 3 (after successful build on kit 1):

[driver] Switching to kit: Visual Studio Enterprise 2022 Release - x86
[visual-studio] Patch Windows SDK path from C:\Program Files (x86)\Windows Kits\10\bin\x86 to C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x86 for C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat
[driver] Removing d:/Lab/_repos/MemoryPEInjector/build/CMakeCache.txt
[driver] Removing d:\Lab\_repos\MemoryPEInjector\build\CMakeFiles

Then on the build, CMake configures the folder and successfully builds an exe.

But when switching kits with the same host arch, CMake doesn't remove the Cache. Example of switching from kit 1 to kit 2 (after successful build on kit 1):

[driver] Switching to kit: Visual Studio Enterprise 2022 Release - amd64_x86
[visual-studio] Patch Windows SDK path from C:\Program Files (x86)\Windows Kits\10\bin\x64 to C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64 for C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat

As you can see it lacks Removing Cache lines. Then on the build, CMake tries to configure the folder, but fails:

[cmake] CMake Error: Error: generator platform: win32
[cmake] Does not match the platform used previously: x64
[cmake] Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.

To fix that I have to execute in command palette >CMake: Delete Cache and Reconfigure. Only after this, I can successfully build with the new kit selected.

Expected behavior: Remove the Cache when the target arch is switched.

Albeit, I don't know if the Cache removal is necessary when only the host arch is switched (current behavior), i.e [kit 1] ↔ [kit 4] or [kit 2] ↔ [kit 3]. Maybe it's ok to not remove the Cache in this situation. Maybe it was intended behavior, but someone messed that up and set Cache removal on host arch switching instead of target arch switching?

elahehrashedi commented 2 years ago

Thank you for reporting this issue. We may not be able to get to this issue immediately, however, we accept PRs from the community. This is the place in code that needs to include the target architecture. https://github.com/microsoft/vscode-cmake-tools/blob/86a48bf30dc449178fd63c7d5458f2807fdbbc16/src/kit.ts#L1220-L1226

snehara99 commented 4 months ago

I was able to repro this issue. Investigating a fix

snehara99 commented 4 months ago

hey @TishSerg This issue should now be fixed in pre-release. If possible try it out and let me know if it works for you :)

Ashley-Li commented 4 months ago

Verified on CMake Tools v1.18.43(release), this issue was fixed: switching from kit 1 to kit 2 (after successful build on kit 1), CMake Cache gets removed. image

snehara99 commented 4 months ago

@Ashley-Li Thanks for confirming!