<div dir="ltr">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.  <div>
<br></div><div>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:</div><div><br>
</div><div>Compiling using 32-bit toolchain on Windows: LLVM_HOST_TRIPLE = i686-pc-win32</div><div>Compiling using 64-bit toolchain on Windows: LLVM_HOST_TRIPLE = x86_64-pc-win32</div><div><br></div><div>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:</div>
<div><br></div><div><div>D:\src\llvm\build\ninja>"c:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86</div><div><br></div><div>D:\src\llvm\build\ninja>where cl</div><div>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe</div>
<div>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64\cl.exe</div><div><br></div><div>D:\src\llvm\build\ninja>cmake ..\..</div><div>-- Target triple: i686-pc-win32</div><div>-- Native target architecture is X86</div>
<div><br></div><div>D:\src\llvm\build\ninja>"c:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86_amd64<br></div><div><br></div><div>D:\src\llvm\build\ninja>where cl</div><div>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64\cl.exe</div>
<div>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe</div><div><br></div><div>D:\src\llvm\build\ninja>cmake ..\..</div><div>-- Target triple: i686-pc-win32</div><div>-- Native target architecture is X86</div>
<div><br></div></div><div><br></div><div>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.</div>
<div><br></div><div>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:</div>
<div><br></div><div><div>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_arm>cl.exe</div><div>Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for ARM</div><div>Copyright (C) Microsoft Corporation.  All rights reserved.</div>
</div><div><br></div><div><div>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86>cl.exe</div><div>Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86</div><div>Copyright (C) Microsoft Corporation.  All rights reserved.</div>
</div><div><br></div><div>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.</div>
<div><br></div><div>Disagreements?  Suggestions?</div><div><br></div><div>If there's no objections I'd like to make this change.</div></div>