cmakeapple-silicon

CMAKE return CMAKE_HOST_SYSTEM_PROCESSOR with x86_64 on apple M2 CPU


I get CMAKE_HOST_SYSTEM_PROCESSOR return value x86_64(why not arm64) when I run on apple M2 CPU(with cmake version 3.23.0)

The system information and cmake version are as follow:

╰─➤  uname -a
Darwin Kernel Version 22.6.0: Wed Jul  5 22:17:35 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T8112 arm64
╭─ ‹master*›
╰─➤  cmake --version                                                                                                      
cmake version 3.23.0
CMake suite maintained and supported by Kitware (kitware.com/cmake).

In addition, when compiling and linking, it will automatically add flags: "-arch x86_64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk"

make VERBOSE=1 print log as below:

/Library/Developer/CommandLineTools/usr/bin/c++  -arch x86_64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names -L/usr/local/opt/protobuf@3.6/lib "CMakeFiles/arch-dependent.dir/arch-dependent.cpp.o" -o arch-dependent

This is a test program:

╰─➤  cat ../CMakeLists.txt
project(test)

message("CMAKE_HOST_SYSTEM_PROCESSOR: ${CMAKE_HOST_SYSTEM_PROCESSOR}")

add_executable(test test.cpp)


╰─➤  cmake ..
CMAKE_HOST_SYSTEM_PROCESSOR: x86_64
-- Configuring done
-- Generating done
...


╰─➤  make VERBOSE=1
...
/Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
[ 50%] Building CXX object CMakeFiles/test.dir/test.cpp.o
/Library/Developer/CommandLineTools/usr/bin/c++   -arch x86_64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -MD -MT CMakeFiles/test.dir/test.cpp.o -MF CMakeFiles/test.dir/test.cpp.o.d -o CMakeFiles/test.dir/test.cpp.o -c /home/arvin/test/test.cpp
[100%] Linking CXX executable test
/usr/local/Cellar/cmake/3.23.0/bin/cmake -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/c++  -arch x86_64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names -L/usr/local/opt/protobuf@3.6/lib CMakeFiles/test.dir/test.cpp.o -o test
...
  1. I can set(CMAKE_OSX_ARCHITECTURES arm64) to change x86_64 to arm64, but i want to know what‘s the problem.
  2. Although I can add "-Wno-unused-command-line-argument" to mask these auto auto-added flags, I do want to remove from the root.

Thanks a lot


Solution

  • The CMAKE_HOST_SYSTEM_PROCESSOR=x86_64 you are seeing is because the CMake version you are using is an Intel version and not a arm64 or universal binary.

    For example, my CMake from https://cmake.org/download/ shows

    $ file /Applications/CMake.app/Contents/bin/cmake 
    /Applications/CMake.app/Contents/bin/cmake: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64]
    /Applications/CMake.app/Contents/bin/cmake (for architecture x86_64):   Mach-O 64-bit executable x86_64
    /Applications/CMake.app/Contents/bin/cmake (for architecture arm64):    Mach-O 64-bit executable arm64
    

    So it will default to arm64 when ran on an AppleSilicon Mac:

    $ /Applications/CMake.app/Contents/bin/cmake --fresh .
    ...
    -- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    CMAKE_HOST_SYSTEM_PROCESSOR: arm64
    -- Configuring done (0.4s)
    -- Generating done (0.0s)
    

    But if you force it to run as an Intel binary:

    $ arch -arch x86_64 /Applications/CMake.app/Contents/bin/cmake --fresh .
    ...
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    CMAKE_HOST_SYSTEM_PROCESSOR: x86_64
    -- Configuring done (1.0s)
    -- Generating done (0.0s)
    

    So by default CMake will use CMAKE_HOST_SYSTEM_PROCESSOR for CMAKE_OSX_ARCHITECTURES. If you want to build universal binary, the recommended setting is:

    set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")