Apple Silicon: Forcing CMAKE_HOST_SYSTEM_PROCESSOR to be x86_64
I'm trying to build the x86 half of a Universal 2 Pytorch on an Apple Silicon (M1) Mac (to be combined an arm64 build on the same machine via lipo
). The Pytorch build process doesn't directly expose the CMake command to me, so I'm relying on environment variables to set things up.
I have a Universal 2 build of CMake (cmake version 3.19.1). Starting from a fresh x86 build of Python 3.9.1rc1, I'm doing the Pytorch build like this:
python3.9 -m pip install pyyaml
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
CMAKE_SYSTEM_PROCESSOR=x86_64 CMAKE_OSX_ARCHITECTURES=x86_64 BUILD_CAFFE2=OFF BUILD_CAFFE2_OPS=OFF BUILD_TEST=OFF python3.9 setup.py build
This is an x86 build of Python, so the Universal 2 build of CMake will execute as x86 as well.
This ends up building an arm64 Pytorch because the call to project()
sees CMAKE_HOST_SYSTEM_PROCESSOR=x86_64 does a sysctl hw.optional.arm64 check, and overrides my manually specified CPU architecture.
From /usr/local/share/cmake-3.19/Modules/CMakeDetermineSystem.cmake
:
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
# Check whether we are running under Rosetta on arm64 hardware.
execute_process(COMMAND sysctl -q hw.optional.arm64
OUTPUT_VARIABLE _sysctl_stdout
ERROR_VARIABLE _sysctl_stderr
RESULT_VARIABLE _sysctl_result
)
if(_sysctl_result EQUAL 0 AND _sysctl_stdout MATCHES "hw.optional.arm64: 1")
set(CMAKE_HOST_SYSTEM_PROCESSOR "arm64")
endif()
So, just because I'm building on an ARM Mac, my x86 Python can't build Pytorch. I think this is an overeager behavior by CMake but I'm not sure the best path forward.
If I comment out set(CMAKE_HOST_SYSTEM_PROCESSOR "arm64")
in CMakeDetermineSystem.cmake
I am able to build an x86 Pytorch fine on an M1 Mac. I don't know how to control this from outside the build process besides putting a fake sysctl
in my PATH that inverts hw.optional.arm64
.