1

Is there a simple, reliable way to detect in a CMake file that the system CMake is building for is based on the x86 instruction set (regardless of bitness)?

François Beaune
  • 4,270
  • 7
  • 41
  • 65
  • I don't believe this is a duplicate. I have done extensive checking of similar questions on StackOverflow and haven't found one that actually covers this case: detecting the x86 instruction set using only CMake. I believe a solution based on searching for "x86" in `CMAKE_SYSTEM_PROCESSOR` would work, but I'm interested in hearing other people's opinion and experience. – François Beaune Oct 30 '14 at 15:40
  • Okay, I agree. I'll remove that comment. – sfrehse Oct 30 '14 at 16:06
  • 1
    target: https://stackoverflow.com/questions/11944060/how-to-detect-target-architecture-using-cmake – Ciro Santilli OurBigBook.com Nov 29 '17 at 17:54

1 Answers1

3

Here is my solution so far:

if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
    set (X86 TRUE)
else ()
    set (X86 FALSE)
endif ()

Please complete the regular expression if you know of or find x86 systems for which CMAKE_SYSTEM_PROCESSOR does not contain "x86", "X86", "amd64" or "AMD64" substrings.

François Beaune
  • 4,270
  • 7
  • 41
  • 65
  • 6
    `CMAKE_SYSTEM_PROCESSOR` returns "unknown" for so many processors its mostly useless as a general solution. I'm glad its working for you. Its amazing how poorly Cmake performs as a build system. – jww May 11 '17 at 21:08
  • It also returns host architecture (for me), not the target arch. – Dan M. Dec 21 '21 at 12:19