Skip to content

Commit

Permalink
Enable build with bitcode for iOS (microsoft#6905)
Browse files Browse the repository at this point in the history
* Enable build with bitcode for iOS

* minor format update

* Minor format update

* Addressed CR comments
guoyu-wang authored Mar 9, 2021
1 parent 73fe1f2 commit e64eff1
Showing 2 changed files with 29 additions and 10 deletions.
27 changes: 22 additions & 5 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
12 changes: 7 additions & 5 deletions cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e64eff1

Please sign in to comment.