0

I am working on a CMake build script that requires the ARM embedded toolchain. Depending on the user's OS and CPU architecture, a different version of the toolkit needs to be downloaded. This is fine when only differentiating OSs, as I can do something like the following:

if(WIN32)
    # link to Windows version
elseif(APPLE)
    # link to Apple version
elseif(LINUX)
    # link to Linux version

However, the toolkit for Apple Silicon is different from Intel MacBooks, and CMAKE_SYSTEM_PROCESSOR and other similar variables all seem empty, so they are of no use, so I cannot differentiate Apple Silicon from Intel chips.
Is there a way around this?

Farzan
  • 11
  • 5
  • Do you read the info after the first `project` command cmake encounters? That's the point where cmake determines several properties. based on the compiler provided. (If you're unsure: printing the `PROJECT_NAME` variable should result in empty output too...) – fabian Aug 07 '23 at 16:00

2 Answers2

1

Your statement about CMAKE_SYSTEM_PROCESSOR being empty and, therefore, useless is surprising. Something like this should work:

if(APPLE)
  if(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
   # Link to MacOSARM version
  else()
   # Link to MacOSX version
  endif()
endif(APPLE)

Perhaps updating your CMAKE version can help.

RHertel
  • 23,412
  • 5
  • 38
  • 64
1

Fabian's answer pointed out that I was trying to read the value of CMAKE_SYSTEM_PROCESSOR before the call to project(), at which point CMake will determine several properties.
So, I wrote the following function:

function(host_uname_machine var)
    execute_process(COMMAND uname -m
        OUTPUT_STRIP_TRAILING_WHITESPACE
        OUTPUT_VARIABLE ${var})
    set(${var} ${${var}} PARENT_SCOPE)
endfunction()

The function leverages the uname command and can be used as follows:

if(APPLE)
     host_uname_machine(machine)
     if(machine STREQUAL "x86_64")
          # download x86_64 toolchain
     elseif(machine STREQUAL "arm64")
          # download arm64 toolchain (Apple Silicon)
Farzan
  • 11
  • 5