Host target triple on Windows

I’m trying to figure out how to get, from CMake, the architecture of the platform that I’m compiling LLVM for. If I’m building LLVM on x86, I want x86. On x64 I want x86_64. On arm arm. etc.

The best I can figure out so far is that I should use LLVM_HOST_TRIPLE, and extract the first component of this. At least if my understanding is correct, I should have the following behavior:

Compiling using 32-bit toolchain on Windows: LLVM_HOST_TRIPLE = i686-pc-win32
Compiling using 64-bit toolchain on Windows: LLVM_HOST_TRIPLE = x86_64-pc-win32

This doesn’t currently seem to work. See, for example, the following output from when I run CMake twice from the command line, once with an x86 toolchain and once with an x64 toolchain:

D:\src\llvm\build\ninja>“c:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat” x86

D:\src\llvm\build\ninja>where cl
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64\cl.exe

D:\src\llvm\build\ninja>cmake …..
– Target triple: i686-pc-win32
– Native target architecture is X86

D:\src\llvm\build\ninja>“c:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat” x86_amd64

D:\src\llvm\build\ninja>where cl
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64\cl.exe
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe

D:\src\llvm\build\ninja>cmake …..
– Target triple: i686-pc-win32
– Native target architecture is X86

In both cases my triple is the same. This seems to boil down to some code in llvm\cmake\modules\GetHostTriple.cmake. It uses the variable CMAKE_CL_64. From what I can tell, the value of this variable depends on your CMake generator, and not on your toolchain. Some googling suggests that CMAKE_CL_64 will be set to 1 if you run cmake -G “Visual Studio 12 Win64”. I can’t even get this to work, however, as it just says that generator doesn’t exist. Furthermore, it won’t work with other generators, such as the ninja generator.

Ultimately, it would be nice if there were a way to just ask the compiler what architecture it’s going to generate code for. Luckily, it seems like there might be a solution. If you run cl with no arguments it is always prefaced with a header such as the following:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_arm>cl.exe
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for ARM
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86>cl.exe
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

Note at the end it says “for ARM”, or “for x86”, etc. So, I think a better solution for the GetHostTriple() function might be to run cl.exe and use STRING(REGEX MATCH …) to figure out what cl reports.

Disagreements? Suggestions?

If there’s no objections I’d like to make this change.

Could this be due to CMake caching (and not updating that cache) the
compiler it uses? If I run cmake in an empty directory, it seems to
get the Target triple right:

D:\src>mkdir build.x86 && cd build.x86
D:\src\build.x86>"c:\Program Files (x86)\Microsoft Visual Studio
12.0\VC\vcvarsall.bat" x86
D:\src\build.x86>cmake -GNinja ..\llvm
[...]
-- Target triple: i686-pc-win32
-- Native target architecture is X86

D:\src\build.x86>cd .. && mkdir build.amd64 && cd build.amd64
D:\src\build.amd64>"c:\Program Files (x86)\Microsoft Visual Studio
12.0\VC\vcvarsall.bat" x86_amd64
D:\src\build.amd64>cmake -GNinja ..\llvm
[...]
-- Target triple: x86_64-pc-win32
-- Native target architecture is X86

What version of CMake are you using? If it’s 3.0, then it’s possible that they’ve gotten smarter. I’m using 2.8, and I confirmed that even with an empty directory, CMAKE_CL_64 in GetHostTriple() is never set regardless of which toolchain is in use.

I'm using 2.8.12.2, but I never looked at CMAKE_CL_64 or
GetHostTriple() internally, just at the printouts and noticed that "--
Target triple" seemed to work.