From e64eff1f131eb1ddaf8bb138752df6b2e7f205fe Mon Sep 17 00:00:00 2001 From: Guoyu Wang <62914304+gwang-msft@users.noreply.github.com> Date: Mon, 8 Mar 2021 22:56:13 -0800 Subject: [PATCH] Enable build with bitcode for iOS (#6905) * Enable build with bitcode for iOS * minor format update * Minor format update * Addressed CR comments --- cmake/CMakeLists.txt | 27 ++++++++++++++++++++++----- cmake/onnxruntime_unittests.cmake | 12 +++++++----- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 5307f4f851..b9d63a163d 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -148,6 +148,8 @@ option(onnxruntime_ENABLE_CPU_FP16_OPS "Build with advanced instruction sets" ON option(onnxruntime_USE_NCCL "Build with NCCL support" OFF) option(onnxruntime_USE_MPI "Build with MPI support" OFF) +# Enable bitcode for iOS +option(onnxruntime_ENABLE_BITCODE "Enable bitcode for iOS only" OFF) # Fuzz test has only been tested with BUILD_SHARED_LIB option, # using the MSVC compiler and on windows OS. @@ -169,6 +171,14 @@ if (onnxruntime_ENABLE_NVTX_PROFILE) add_definitions(-DENABLE_NVTX_PROFILE=1) endif() +if (onnxruntime_ENABLE_BITCODE) + if (NOT (CMAKE_SYSTEM_NAME STREQUAL "iOS")) + message(FATAL_ERROR "iOS platform required for onnxruntime_ENABLE_BITCODE") + endif() + set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE YES) + set(CMAKE_XCODE_ATTRIBUTE_BITCODE_GENERATION_MODE "bitcode") +endif() + if (onnxruntime_ENABLE_MEMORY_PROFILE) add_definitions(-DORT_MEMORY_PROFILE=1) endif() @@ -227,6 +237,13 @@ if (onnxruntime_EXTENDED_MINIMAL_BUILD AND NOT onnxruntime_MINIMAL_BUILD) set(onnxruntime_MINIMAL_BUILD ON) endif() +# Enable space optimization for gcc/clang +# Cannot use "-ffunction-sections -fdata-sections" if we enable bitcode (iOS) +if (NOT MSVC AND NOT onnxruntime_ENABLE_BITCODE) + string(APPEND CMAKE_CXX_FLAGS " -ffunction-sections -fdata-sections") + string(APPEND CMAKE_C_FLAGS " -ffunction-sections -fdata-sections") +endif() + # ORT build with as much excluded as possible. Supports ORT flatbuffers models only. if (onnxruntime_MINIMAL_BUILD) add_compile_definitions(ORT_MINIMAL_BUILD) @@ -261,7 +278,6 @@ if (onnxruntime_MINIMAL_BUILD) string(APPEND CMAKE_SHARED_LINKER_FLAGS " /debug") endif() else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections") if (CMAKE_HOST_SYSTEM MATCHES "Darwin") add_link_options(-Wl, -dead_strip) else() @@ -331,7 +347,8 @@ endif() # don't have built-in support for 8 byte atomics # Derived from https://github.com/protocolbuffers/protobuf/blob/master/cmake/CMakeLists.txt set(onnxruntime_LINK_LIBATOMIC false) -if (NOT MSVC) +# We don't need to link libatomic on iOS +if (NOT MSVC AND NOT (CMAKE_SYSTEM_NAME STREQUAL "iOS")) include(CheckCXXSourceCompiles) set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -std=c++11) @@ -1028,9 +1045,9 @@ if (WIN32) endforeach() else() add_definitions(-DPLATFORM_POSIX) - # Enable warning and enable space optimization in Linux - string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -ffunction-sections -fdata-sections") - string(APPEND CMAKE_C_FLAGS " -Wall -Wextra -ffunction-sections -fdata-sections") + # Enable warning + string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra") + string(APPEND CMAKE_C_FLAGS " -Wall -Wextra") if(onnxruntime_DEV_MODE) string(APPEND CMAKE_CXX_FLAGS " -Werror") diff --git a/cmake/onnxruntime_unittests.cmake b/cmake/onnxruntime_unittests.cmake index e73397b2dc..981f1dacbe 100644 --- a/cmake/onnxruntime_unittests.cmake +++ b/cmake/onnxruntime_unittests.cmake @@ -96,7 +96,8 @@ function(AddTest) MACOSX_BUNDLE_LONG_VERSION_STRING ${ORT_VERSION} MACOSX_BUNDLE_BUNDLE_VERSION ${ORT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${ORT_VERSION} - XCODE_ATTRIBUTE_CLANG_ENABLE_MODULES "YES") + XCODE_ATTRIBUTE_CLANG_ENABLE_MODULES "YES" + XCODE_ATTRIBUTE_ENABLE_BITCODE "NO") xctest_add_bundle(${_UT_TARGET}_xc ${_UT_TARGET} ${TEST_SRC_DIR}/xctest/ortxctest.m @@ -120,7 +121,8 @@ function(AddTest) MACOSX_BUNDLE_GUI_IDENTIFIER com.onnxruntime.utest.${_UT_TARGET} MACOSX_BUNDLE_LONG_VERSION_STRING ${ORT_VERSION} MACOSX_BUNDLE_BUNDLE_VERSION ${ORT_VERSION} - MACOSX_BUNDLE_SHORT_VERSION_STRING ${ORT_VERSION}) + MACOSX_BUNDLE_SHORT_VERSION_STRING ${ORT_VERSION} + XCODE_ATTRIBUTE_ENABLE_BITCODE "NO") xctest_add_test(xctest.${_UT_TARGET} ${_UT_TARGET}_xc) else() @@ -814,9 +816,9 @@ if (WIN32) endif() if (onnxruntime_BUILD_SHARED_LIB) - set(onnxruntime_perf_test_libs - onnx_test_runner_common onnxruntime_test_utils onnxruntime_common - onnxruntime onnxruntime_flatbuffers onnx_test_data_proto + set(onnxruntime_perf_test_libs + onnx_test_runner_common onnxruntime_test_utils onnxruntime_common + onnxruntime onnxruntime_flatbuffers onnx_test_data_proto ${onnxruntime_EXTERNAL_LIBRARIES} ${GETOPT_LIB_WIDE} ${SYS_PATH_LIB} ${CMAKE_DL_LIBS}) if(NOT WIN32)