macOS: cmake detect system processor incorrectly on Apple Silicon?

When I use cmake(3.22 or 3.23), the CMAKE_SYSTEM_PROCESSOR equal “x86_64” with Mac 1M silicon, while the expected value is “arm64”.
I don’t know whether there are bugs or my environments errors.

How did you get the CMake you are using? If you are using a CMake compiled only as x86_64, the arm64-ness of the processor is hidden behind Rosetta2 and not accessible.

I think there is a way for a x86_64 process to tell what the underlying processor is even when running under Rosetta. I seem to recall past discussions about this and CMake might even be using it in some places. @brad.king might recall where this is used or was discussed.

I install the cmake from homebrew.
The issue found when I build OpenCV from source code.
And I found the CMAKE_SYSTEM_PROCESSOR=“x86_64” after the function enable_language(CXX C) done.

Yes, but when executing a process, you’ll get the x86_64 version, so it will be “cross compiling” as far as any tooling is concerned. It’s just easier to get a multi-arch CMake instead.

See the CMAKE_APPLE_SILICON_PROCESSOR option, added by CMake MR 5589.

For reference, the implementation is here. It uses sysctl -q hw.optional.arm64 to pierce Rosetta, but only to verify that it’s actually running on an arm64 host, where CMAKE_APPLE_SILICON_PROCESSOR is documented to be meaningful.

As Ben said, the defaults are more reliable when one uses the official CMake binaries for macOS on, which are universal binaries supporting arm64 and x86_64 hosts.