From ac2bdf553ee0fb25c19926ba901b856b45405d77 Mon Sep 17 00:00:00 2001 From: Jane Xu Date: Thu, 11 Feb 2021 14:34:19 -0800 Subject: [PATCH] update build_host_protoc command for macos cross compilation (#50922) Summary: Currently, adding a cross compile build is failing on CI due to a cmake builtin compiler check that does not pass due to cross compiling the host protoc library. Setting the CMAKE_TRY_COMPILE_TARGET_TYPE flag should fix it. (Based on this [SOF answer](https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program).) To test that this works, please run: `CMAKE_OSX_ARCHITECTURES=arm64 USE_MKLDNN=OFF USE_NNPACK=OFF USE_QNNPACK=OFF USE_PYTORCH_QNNPACK=OFF BUILD_TEST=OFF python setup.py install` from a Mac x86_64 machine with Xcode12.3 (anything with MacOS 11 SDK). Then, you can check that things were compiled for arm by running `lipo -info ` for any file in the `build/lib` directory. Pull Request resolved: https://github.com/pytorch/pytorch/pull/50922 Reviewed By: malfet Differential Revision: D26355054 Pulled By: janeyx99 fbshipit-source-id: 919f3f9bd95d7c7bba6ab3a95428d3ca309f8ead --- CMakeLists.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f32cb685634..ad23d8df92f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -539,9 +539,15 @@ endif() if(NOT IOS AND CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_ARCHITECTURES MATCHES "^(x86_64|arm64)$") set(CROSS_COMPILING_MACOSX TRUE) # We need to compile a universal protoc to not fail protobuf build - execute_process(COMMAND ./scripts/build_host_protoc.sh --other-flags "-DCMAKE_OSX_ARCHITECTURES=x86_64;arm64" + # We set CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY (vs executable) to succeed the cmake compiler check for cross-compiling + set(protoc_build_command "./scripts/build_host_protoc.sh --other-flags -DCMAKE_OSX_ARCHITECTURES=\"x86_64;arm64\" -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY -DCMAKE_C_COMPILER_WORKS=1 -DCMAKE_CXX_COMPILER_WORKS=1") + # We write to a temp scriptfile because CMake COMMAND dislikes double quotes in commands + file(WRITE ${PROJECT_SOURCE_DIR}/tmp_protoc_script.sh "#!/bin/bash\n${protoc_build_command}") + file(COPY ${PROJECT_SOURCE_DIR}/tmp_protoc_script.sh DESTINATION ${PROJECT_SOURCE_DIR}/scripts/ FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) + execute_process(COMMAND ./scripts/tmp_protoc_script.sh WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} RESULT_VARIABLE BUILD_HOST_PROTOC_RESULT) + file(REMOVE ${PROJECT_SOURCE_DIR}/tmp_protoc_script.sh ${PROJECT_SOURCE_DIR}/scripts/tmp_protoc_script.sh) if(NOT BUILD_HOST_PROTOC_RESULT EQUAL "0") message(FATAL_ERROR "Could not compile universal protoc.") endif()