Skip to content

cmake file changes for macOS universal2 support #8953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 84 additions & 24 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ set(ONNX_CUSTOM_PROTOC_EXECUTABLE "" CACHE STRING "Specify custom protoc executa
# pre-build python path
option(onnxruntime_PREBUILT_PYTORCH_PATH "Path to pytorch installation dir")

if(APPLE)
if(NOT CMAKE_OSX_ARCHITECTURES)
message("Building ONNX Runtime for ${CMAKE_HOST_SYSTEM_PROCESSOR}")
endif()
elseif(NOT WIN32 AND NOT APPLE)
message("Building ONNX Runtime for ${CMAKE_SYSTEM_PROCESSOR}")
endif()

# Single output director for all binaries
set (RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin CACHE PATH "Single output directory for all binaries.")

Expand Down Expand Up @@ -507,6 +515,30 @@ endmacro()
#Set global compile flags for all the source code(including third_party code like protobuf)
#This section must be before any add_subdirectory, otherwise build may fail because /MD,/MT mismatch
if (MSVC)
enable_language(ASM_MASM)
if(CMAKE_GENERATOR_PLATFORM)
# Multi-platform generator
set(onnxruntime_target_platform ${CMAKE_GENERATOR_PLATFORM})
else()
set(onnxruntime_target_platform ${CMAKE_SYSTEM_PROCESSOR})
endif()
if(onnxruntime_target_platform STREQUAL "ARM64")
set(onnxruntime_target_platform "ARM64")
elseif(onnxruntime_target_platform STREQUAL "ARM64EC")
set(onnxruntime_target_platform "ARM64EC")
elseif(onnxruntime_target_platform STREQUAL "ARM" OR CMAKE_GENERATOR MATCHES "ARM")
set(onnxruntime_target_platform "ARM")
elseif(onnxruntime_target_platform STREQUAL "x64" OR onnxruntime_target_platform STREQUAL "x86_64" OR onnxruntime_target_platform STREQUAL "AMD64" OR CMAKE_GENERATOR MATCHES "Win64")
set(onnxruntime_target_platform "x64")
elseif(onnxruntime_target_platform STREQUAL "Win32" OR onnxruntime_target_platform STREQUAL "x86" OR onnxruntime_target_platform STREQUAL "i386" OR onnxruntime_target_platform STREQUAL "i686")
set(onnxruntime_target_platform "x86")
if(NOT onnxruntime_BUILD_WEBASSEMBLY)
message("Enabling SAFESEH for x86 build")
set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /safeseh")
endif()
endif()


#Always enable exception handling, even for Windows ARM
if(NOT onnxruntime_DISABLE_EXCEPTIONS)
string(APPEND CMAKE_CXX_FLAGS " /EHsc")
Expand Down Expand Up @@ -555,6 +587,9 @@ if (MSVC)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /guard:cf")
endif()
else()
if(NOT APPLE)
set(onnxruntime_target_platform ${CMAKE_SYSTEM_PROCESSOR})
endif()
if(onnxruntime_BUILD_FOR_NATIVE_MACHINE)
string(APPEND CMAKE_CXX_FLAGS " -march=native -mtune=native")
string(APPEND CMAKE_C_FLAGS " -march=native -mtune=native")
Expand Down Expand Up @@ -736,6 +771,17 @@ else()
endif()
if(TARGET protoc)
set_target_properties(protoc PROPERTIES FOLDER "External/Protobuf")
get_target_property(PROTOC_OSX_ARCH protoc OSX_ARCHITECTURES)
if(PROTOC_OSX_ARCH)
if (${CMAKE_HOST_SYSTEM_PROCESSOR} IN_LIST PROTOC_OSX_ARCH)
message("protoc can run")
else()
list(APPEND PROTOC_OSX_ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR})
set_target_properties(protoc PROPERTIES OSX_ARCHITECTURES "${CMAKE_HOST_SYSTEM_PROCESSOR}")
set_target_properties(libprotoc PROPERTIES OSX_ARCHITECTURES "${PROTOC_OSX_ARCH}")
set_target_properties(libprotobuf PROPERTIES OSX_ARCHITECTURES "${PROTOC_OSX_ARCH}")
endif()
endif()
endif()
if (onnxruntime_USE_FULL_PROTOBUF)
set(PROTOBUF_LIB libprotobuf)
Expand Down Expand Up @@ -810,31 +856,39 @@ if(NOT TARGET re2::re2)
set(RE2_INCLUDE_DIR ${REPO_ROOT}/cmake/external/re2)
endif()


# Adding pytorch CPU info library
# TODO!! need a better way to find out the supported architectures
set(TARGET_ARCH "${CMAKE_SYSTEM_PROCESSOR}")
if (NOT DEFINED TARGET_ARCH OR "${TARGET_ARCH}" STREQUAL "")
set(TARGET_ARCH "${CMAKE_OSX_ARCHITECTURES}")
endif()

set(CPUINFO_SUPPORTED TRUE)
if (NOT DEFINED TARGET_ARCH OR "${TARGET_ARCH}" STREQUAL "")
message(WARNING
"Target processor architecture is not specified. cpuinfo not included")
set(CPUINFO_SUPPORTED FALSE)
elseif(NOT TARGET_ARCH MATCHES "^(i[3-6]86|AMD64|x86(_64)?|armv[5-8].*|aarch64|arm64)$")
message(WARNING
"Target processor architecture \"${CPUINFO_TARGET_PROCESSOR}\" is not supported in cpuinfo. "
"cpuinfo not included.")
set(CPUINFO_SUPPORTED FALSE)
elseif(MSVC AND (( CMAKE_SYSTEM_PROCESSOR MATCHES "^(ARM.*|arm.*)$" ) OR (CMAKE_GENERATOR_PLATFORM MATCHES "^(ARM.*|arm.*)$" ) ))
message(WARNING
"Cpuinfo not included for compilation problems with Windows ARM.")
set(CPUINFO_SUPPORTED FALSE)
elseif(WINDOWS_STORE)
message(WARNING
"Cpuinfo not included in Windows Store builds")
set(CPUINFO_SUPPORTED FALSE)
list(LENGTH CMAKE_OSX_ARCHITECTURES CMAKE_OSX_ARCHITECTURES_LEN)
if(APPLE)
if(CMAKE_OSX_ARCHITECTURES_LEN LESS_EQUAL 1)
set(CPUINFO_SUPPORTED TRUE)
elseif(onnxruntime_BUILD_APPLE_FRAMEWORK)
# We stitch multiple static libraries together when onnxruntime_BUILD_APPLE_FRAMEWORK is true,
# but that would not work for universal static libraries
message(FATAL_ERROR "universal binary is not supported for apple framework")
endif()
else()
if(onnxruntime_BUILD_WEBASSEMBLY)
set(CPUINFO_SUPPORTED FALSE)
else()
set(CPUINFO_SUPPORTED TRUE)
endif()
if(WIN32)
# Exclude Windows ARM build and Windows Store
if(${onnxruntime_target_platform} MATCHES "^(ARM.*|arm.*)$" )
message(WARNING "Cpuinfo not included for compilation problems with Windows ARM.")
set(CPUINFO_SUPPORTED FALSE)
elseif(WINDOWS_STORE)
message(WARNING "Cpuinfo not included in Windows Store builds")
set(CPUINFO_SUPPORTED FALSE)
endif()
elseif(NOT ${onnxruntime_target_platform} MATCHES "^(i[3-6]86|AMD64|x86(_64)?|armv[5-8].*|aarch64|arm64)$")
message(WARNING
"Target processor architecture \"${onnxruntime_target_platform}\" is not supported in cpuinfo. "
"cpuinfo not included.")
set(CPUINFO_SUPPORTED FALSE)
endif()
endif()

# TODO do we have to add target_include_directories to each project that uses this?
Expand Down Expand Up @@ -1162,6 +1216,9 @@ function(onnxruntime_add_shared_library_module target_name)
endif()

onnxruntime_configure_target(${target_name})
if (onnxruntime_target_platform STREQUAL "x86" AND NOT onnxruntime_BUILD_WEBASSEMBLY)
target_link_options(${target_name} PRIVATE /SAFESEH)
endif()
endfunction()

function(onnxruntime_add_executable target_name)
Expand All @@ -1170,6 +1227,9 @@ function(onnxruntime_add_executable target_name)
endif()
add_executable(${target_name} ${ARGN})
onnxruntime_configure_target(${target_name})
if (onnxruntime_target_platform STREQUAL "x86" AND NOT onnxruntime_BUILD_WEBASSEMBLY)
target_link_options(${target_name} PRIVATE /SAFESEH)
endif()
endfunction()

function(onnxruntime_add_include_to_target dst_target)
Expand Down Expand Up @@ -1813,7 +1873,7 @@ if (WINDOWS_STORE)
target_link_options(onnxruntime PRIVATE /DYNAMICBASE /NXCOMPAT /APPCONTAINER)
target_link_options(winml_dll PRIVATE /DYNAMICBASE /NXCOMPAT /APPCONTAINER)

if (onnxruntime_target_platform STREQUAL "x86")
if (onnxruntime_target_platform STREQUAL "x86" AND NOT onnxruntime_BUILD_WEBASSEMBLY)
target_link_options(onnxruntime PRIVATE /SAFESEH)
target_link_options(winml_dll PRIVATE /SAFESEH)
endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ set(onnxruntime_INTERNAL_LIBRARIES
${onnxruntime_tvm_libs}
onnxruntime_framework
onnxruntime_graph
${ONNXRUNTIME_MLAS_LIBS}
onnxruntime_common
onnxruntime_mlas
onnxruntime_flatbuffers
)

Expand Down
33 changes: 4 additions & 29 deletions cmake/onnxruntime_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,6 @@ else()
endif()
endif()

if(CMAKE_GENERATOR_PLATFORM)
# Multi-platform generator
set(onnxruntime_target_platform ${CMAKE_GENERATOR_PLATFORM})
else()
set(onnxruntime_target_platform ${CMAKE_SYSTEM_PROCESSOR})
endif()
if(onnxruntime_target_platform STREQUAL "ARM64")
set(onnxruntime_target_platform "ARM64")
elseif(onnxruntime_target_platform STREQUAL "ARM64EC")
set(onnxruntime_target_platform "ARM64EC")
elseif(onnxruntime_target_platform STREQUAL "ARM" OR CMAKE_GENERATOR MATCHES "ARM")
set(onnxruntime_target_platform "ARM")
elseif(onnxruntime_target_platform STREQUAL "x64" OR onnxruntime_target_platform STREQUAL "x86_64" OR onnxruntime_target_platform STREQUAL "AMD64" OR CMAKE_GENERATOR MATCHES "Win64")
set(onnxruntime_target_platform "x64")
elseif(onnxruntime_target_platform STREQUAL "Win32" OR onnxruntime_target_platform STREQUAL "x86" OR onnxruntime_target_platform STREQUAL "i386" OR onnxruntime_target_platform STREQUAL "i686")
set(onnxruntime_target_platform "x86")
endif()

if(onnxruntime_target_platform STREQUAL "ARM64EC")
if (MSVC)
link_directories("$ENV{VCINSTALLDIR}/Tools/MSVC/$ENV{VCToolsVersion}/lib/ARM64EC")
Expand Down Expand Up @@ -185,18 +167,11 @@ if(MSVC)
elseif(onnxruntime_target_platform STREQUAL "x86")
set(X86 TRUE)
endif()
elseif(NOT onnxruntime_BUILD_WEBASSEMBLY)
if (CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
set(ARM64 TRUE)
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "arm64e")
set(ARM64 TRUE)
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "arm")
set(ARM TRUE)
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
set(X86_64 TRUE)
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "i386")
set(X86 TRUE)
elseif(APPLE)
if(CMAKE_OSX_ARCHITECTURES_LEN LESS_EQUAL 1)
set(X64 TRUE)
endif()
elseif(NOT onnxruntime_BUILD_WEBASSEMBLY)
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
if (CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
set(ARM TRUE)
Expand Down
19 changes: 18 additions & 1 deletion cmake/onnxruntime_java.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,24 @@ endif()

# Set platform and arch for packaging
# Checks the names set by MLAS on non-Windows platforms first
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
if(APPLE)
get_target_property(ONNXRUNTIME4J_OSX_ARCH onnxruntime4j_jni OSX_ARCHITECTURES)
list(LENGTH ONNXRUNTIME4J_OSX_ARCH ONNXRUNTIME4J_OSX_ARCH_LEN)
if(ONNXRUNTIME4J_OSX_ARCH)
if(ONNXRUNTIME4J_OSX_ARCH_LEN LESS_EQUAL 1)
list(GET ONNXRUNTIME4J_OSX_ARCH 0 JNI_ARCH)
message("Set Java ARCH TO macOS/iOS ${JNI_ARCH}")
else()
message(FATAL_ERROR "Java is currently not supported for macOS universal")
endif()
else()
set(JNI_ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR})
message("Set Java ARCH TO macOS/iOS ${JNI_ARCH}")
endif()
if(JNI_ARCH STREQUAL "x86_64")
set(JNI_ARCH x64)
endif()
elseif (CMAKE_SYSTEM_NAME STREQUAL "Android")
set(JNI_ARCH ${ANDROID_ABI})
elseif (ARM64)
set(JNI_ARCH aarch64)
Expand Down
Loading