Retrieve CPU information in terminal / script / CMake running on Apple M1 (and later models)
I am looking for a possibility to identify the CPU of current and future(!) ARM-based MacBooks. We are developing research codes that can be compiled under macOS/Linux/Windows and use CMake for the build process. The build system tries to identify the CPU type as accurate as possible to activate optimisation flags of the compiler. We support cross-compilation so -mtune=native is not an option.
Under Linux we read out /proc/cpuinfo and parse "vendor_id", "cpu_family", etc. for x86-based CPU and "CPU implementer", "CPU architecture", etc. for ARM-based systems.
Under macOS (x86_64) we use the following command
/usr/sbin/sysctl -n machdep.cpu.vendor machdep.cpu.family machdep.cpu.model machdep.cpu.stepping machdep.cpu.features
to retrieve the relevant information.
I was able to retrieve some information on my ARM-based MacBook Pro using
/usr/sbin/sysctl -a machdep.cpu
machdep.cpu.cores_per_package: 8
machdep.cpu.core_count: 8
machdep.cpu.logical_per_package: 8
machdep.cpu.thread_count: 8
machdep.cpu.brand_string: Apple M1
I wonder if the brand_string follows/will follow a systematic naming convention. Looking at the CPU types supported by the clang++ compiler I do see such systematic naming convention:
clang++ --print-supported-cpus
Apple clang version 12.0.0 (clang-1200.0.32.28)
Target: arm64-apple-darwin20.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Available CPUs for this target:
apple-a10
apple-a11
apple-a12
apple-a13
apple-a7
apple-a8
apple-a9
apple-latest
apple-s4
apple-s5
cortex-a35
cortex-a53
cortex-a55
...
I also wonder which target cpu should be selected to compile code optimised for Apple M1. Any help is appreciated.
MacBook Pro