Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
snnn committed Aug 26, 2021
1 parent ced2d8e commit a0af414
Show file tree
Hide file tree
Showing 102 changed files with 2,168 additions and 1,613 deletions.
145 changes: 83 additions & 62 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ option(onnxruntime_USE_NUPHAR "Build with Nuphar" OFF)
option(onnxruntime_USE_VITISAI "Build with Vitis-AI" OFF)
option(onnxruntime_USE_TENSORRT "Build with TensorRT support" OFF)
option(onnxruntime_ENABLE_LTO "Enable link time optimization" OFF)
option(onnxruntime_CROSS_COMPILING "Cross compiling onnx runtime" OFF)
option(onnxruntime_GCOV_COVERAGE "Compile with options necessary to run code coverage" OFF)

#It's preferred to turn it OFF when onnxruntime is dynamically linked to PROTOBUF. But Tensort always required the full version of protobuf.
Expand Down Expand Up @@ -160,6 +159,14 @@ option(onnxruntime_ENABLE_EXTENSION_CUSTOM_OPS "Enable custom operators in onnxr
# 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 @@ -461,15 +468,6 @@ if(MSVC)
-DEIGEN_STRONG_INLINE=inline)
endif()

if(onnxruntime_CROSS_COMPILING)
set(CMAKE_CROSSCOMPILING ON)
check_cxx_compiler_flag(-Wno-error HAS_NOERROR)
if(HAS_NOERROR)
string(APPEND CMAKE_CXX_FLAGS " -Wno-error=attributes")
string(APPEND CMAKE_C_FLAGS " -Wno-error=attributes")
endif()
endif()

# Mark symbols to be invisible, for macOS/iOS target only
# Due to many dependencies have different symbol visibility settings, set global compile flags here.
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS")
Expand All @@ -495,6 +493,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 @@ -543,6 +565,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 @@ -595,16 +620,6 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-strong")
endif()

if (${CMAKE_SYSTEM_NAME} MATCHES "iOSCross")
#For ios compliance
message("Adding flags for ios builds")
if (CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target arm64-apple-darwin-macho")
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "arm")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target armv7a-apple-darwin-macho")
endif()
endif()

#Dependencies begin
if (onnxruntime_BUILD_UNIT_TESTS)
if(onnxruntime_PREFER_SYSTEM_LIB)
Expand Down Expand Up @@ -639,10 +654,6 @@ set(ENABLE_DATE_TESTING OFF CACHE BOOL "" FORCE)
set(USE_SYSTEM_TZ_DB ON CACHE BOOL "" FORCE)
set(RE2_BUILD_TESTING OFF CACHE BOOL "" FORCE)

if(CMAKE_CROSSCOMPILING)
message("Doing crosscompiling")
endif()

#Need python to generate def file
if(onnxruntime_BUILD_SHARED_LIB OR onnxruntime_ENABLE_PYTHON)
if(onnxruntime_ENABLE_PYTHON)
Expand Down Expand Up @@ -724,6 +735,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 @@ -794,31 +816,35 @@ 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)
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 All @@ -828,12 +854,7 @@ if(CPUINFO_SUPPORTED)
set(CPUINFO_BUILD_TOOLS OFF CACHE INTERNAL "")
set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE INTERNAL "")
set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE INTERNAL "")
set(CPUINFO_BUILD_BENCHMARKS OFF CACHE INTERNAL "")

if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
set(IOS ON CACHE INTERNAL "")
set(IOS_ARCH "${CMAKE_OSX_ARCHITECTURES}" CACHE INTERNAL "")
endif()
set(CPUINFO_BUILD_BENCHMARKS OFF CACHE INTERNAL "")

string(APPEND CMAKE_CXX_FLAGS " -DCPUINFO_SUPPORTED")
add_subdirectory(external/pytorch_cpuinfo EXCLUDE_FROM_ALL)
Expand Down Expand Up @@ -1084,10 +1105,7 @@ function(onnxruntime_set_compile_flags target_name)
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options -Werror>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:-Werror>")
endif()

target_compile_definitions(${target_name} PUBLIC -DNSYNC_ATOMIC_CPP11)
if(APPLE AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "iOSCross" AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "iOS")
target_compile_definitions(${target_name} PUBLIC -Doptional_CONFIG_SELECT_OPTIONAL=optional_OPTIONAL_NONSTD)
endif()
target_compile_definitions(${target_name} PUBLIC -DNSYNC_ATOMIC_CPP11)
target_include_directories(${target_name} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/external/nsync/public")
endif()
foreach(ORT_FLAG ${ORT_PROVIDER_FLAGS})
Expand Down Expand Up @@ -1140,22 +1158,25 @@ endfunction()

#For plugins that are not linked into other targets but may be loaded dynamically at runtime using dlopen-like functionality.
function(onnxruntime_add_shared_library_module target_name)
if ((${CMAKE_SYSTEM_NAME} MATCHES "Darwin") OR (${CMAKE_SYSTEM_NAME} MATCHES "iOSCross") OR (${CMAKE_SYSTEM_NAME} MATCHES "iOS"))
if ((${CMAKE_SYSTEM_NAME} MATCHES "Darwin") OR (${CMAKE_SYSTEM_NAME} MATCHES "iOS"))
add_library(${target_name} SHARED ${ARGN})
else()
#On Windows, this target shouldn't generate an import lib, but I don't know how to disable it.
add_library(${target_name} MODULE ${ARGN})
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)
if(${CMAKE_SYSTEM_NAME} MATCHES "iOSCross")
message(FATAL_ERROR "iOS doesn't support commmand line tool")
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 @@ -1814,7 +1835,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 @@ -175,8 +175,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

0 comments on commit a0af414

Please sign in to comment.