[MSVC] How does cmake determine which host architecture to use

Hi All,

We have a question, why does CMake call x64/x86/cl.exe instead of x86/x86/cl.exe when we have clean cmd set to VS x86_x86 command prompt and generator set to MSVC? But setting the generator to Ninja it calls x86/x86/cl.exe? We know that “-Thost” can specify the host architecture, but we want to confirm if this is by design?

Repro(for example):

  1. open a clean cmd
  2. “C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\Tools\VsDevCmd.bat” -host_arch=x86 -arch=x86
  3. git clone GitHub - abseil/abseil-cpp: Abseil Common Libraries (C++) f:\abseil-cpp
  4. mkdir f:\abseil-cpp\build_x86_msvc & cd f:\abseil-cpp\build_x86_msvc
  5. cmake -G “Visual Studio 16 2019” -A Win32 -DCMAKE_SYSTEM_VERSION=10.0.18362.0 -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Release -DABSL_USE_GOOGLETEST_HEAD=ON …
  6. msbuild /m /p:Platform=Win32 /p:Configuration=Release absl.sln /t:Rebuild > build.log
  7. mkdir f:\abseil-cpp\build_x86_Ninja & cd f:\abseil-cpp\build_x86_Ninja
  8. cmake -G Ninja -DCMAKE_SYSTEM_VERSION=10.0.18362.0 -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Release -DABSL_USE_GOOGLETEST_HEAD=ON .
  9. cmake --build . --config Release --verbose > build.log

Log(MSVC):
f:\abseil-cpp\build_x86_msvc>cmake.exe -G “Visual Studio 16 2019” -A Win32 -DCMAKE_SYSTEM_VERSION=10.0.18362.0 -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Release -DABSL_USE_GOOGLETEST_HEAD=ON …
– The CXX compiler identification is MSVC 19.29.30146.0
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x86/cl.exe - skipped
– Detecting CXX compile features

Log(Ninja):
f:\abseil-cpp\build_x86_Ninja>cmake.exe -G Ninja -DCMAKE_SYSTEM_VERSION=10.0.18362.0 -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Release -DABSL_USE_GOOGLETEST_HEAD=ON …
– The CXX compiler identification is MSVC 19.34.31823.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.34.31823/bin/Hostx86/x86/cl.exe - skipped
– Detecting CXX compile features

Cc: @brad.king

It is by design, and as documented.

The Ninja generator uses the established command-line environment in which it is executed.

The Visual Studio generators do not care about the calling environment. They use -T and -A arguments to control the toolset and platform/architecture selection via CMAKE_GENERATOR_TOOLSET and CMAKE_GENERATOR_PLATFORM.