onnxruntime/cmake/CMakeLists.txt
tmccrmck 1978b3c953 Add an HTTP server for hosting of ONNX models (#806)
* Simple integration into CMake build system

* Adds vcpkg as a submodule and updates build.py to install hosting dependencies

* Don't create vcpkg executable if already created

* Fixes how CMake finds toolchain file and quick changes to build.py

* Removes setting the CMAKE_TOOLCHAIN_FILE in build.py

* Adds Boost Beast echo server and Boost program_options

* Fixes spacing problem with program_options

* Adds Microsoft headers to all the beast server headers

* Removes CXX 14 from CMake file

* Adds TODO to create configuration class

* Run clang-format on main

* Better exception handling of program_options

* Remove vckpg submodule via ssh

* Add vcpkg as https

* Adds onnxruntime namespace to call classes

* Fixed places where namespaces were anonymous

* Adds a TODO to use the logger

* Moves all setting namespace shortnames outside of onnxruntime namespace

* Add onnxruntime session options to force app to link with it

* Set CMAKE_TOOLCHAIN_FILE in build.py

* Remove whitespace

* Adds initial ONNX Hosting tests (#5)

* Add initial test which is failing linking with no main

* Adds test_main to get hosting tests working

* Deletes useless add_executable line

* Merge changes from upstream

* Enable CI build in Vienna environment

* make hosting_run*.sh executable

* Add boost path in unittest

* Add boost to TEST_INC_DIR

* Add component detection task in ci yaml

* Get tests and hosting to compile with re2 (#7)

* Add finding boost packages before using it in unit tests

* Add predict.proto and build

* Ignore unused parameters in generated code

* Removes std::regex in favor of re2 (#8)

* Removes std::regex in favor of re2

* Adds back find_package in unit tests and fixes regexes

* Adds more negative test cases

* Adding more protos

* Fix google protobuf file path in the cmake file

* Ignore unused parameters for pb generated code

* Updates onnx submodule (#10)

* Remove duplicated lib in link

* Follow Google style guide (#11)

* Google style names
* Adds more 
* Adds an additional namespace
* Fixes header guards to match filepaths

* Consume protobuf

* Unit Test setup

* Json deserialization simple test cases

* Split hosting app to lib and exe for testability

* Add more cases

* Clean up

* Add more comments

* Update namespace and format the cmake files

* Update cmake/external/onnx to checkout 1ec81bc6d49ccae23cd7801515feaadd13082903

* Separate h and cc in http folder

* Clean up hosting application cmake file

* Enable logging and proper initialize the session

* Update const position for GetSession()

* Take latest onnx and onnx-tensorrt

* Creates configuration header file for program_options (#15)

* Sets up PredictRequest callback (#16)

* Init version, porting from prototype, e2e works

* More executor implementation

* Adds function on application startup (#17)

* Attempts to pass HostingEnvironment as a shared_ptr

* Removes logging and environment from all http classes

* Passes http details to OnStart function

* Using full protobuf for hosting app build

* MLValue2TensorProto

* Revert back changes in inference_session.cc

* Refactor logger access and predict handler

* Create an error handling callback (#19)

* Creates error callback

* Logs error and returns back as JSON

* Catches exceptions in user functions

* Refactor executor and add some test cases

* Fix build warning

* Add onnx as a dependency and in includes to hosting app (#20)

* Converter for specific types and more UTs

* More unit tests

* Update onnx submodule

* Fix string data test

* Clean up code

* Cleanup code

* Refactor logging to use unique id per request and take logging level from user (#21)

* Removes capturing env by reference in main

* Uses uuid for logging ids

* Take logging_level as a program argument

* Pass logging_level to default_logging_manager

* Change name of logger to HostingApp

* Log if request id is null

* Update GetHttpStatusCode signature

* Fix random result issue and camel-case names

* Rollback accidentally changed pybin_state.cc

* Rollback pybind_state.cc

* Generate protobuf status from onnxruntime status

* Fix function name in error message

* Clean up comments

* Support protobuf byte array as input

* Refactor predict handler and add unit tests

* Add one more test

* update cmake/external/onnx

* Accept more protobuf MIME types

* Update onnx-tensorrt

* Add build instruction and usage doc

* Address PR comments

* Install g++-7 in the Ubuntu 16.04 build image for vcpkg

* Fix onnx-tensorrt version

* Check return value during initialization

* Fix infinite loop when http port is in use (#29)

* Simplify Executor.cc by breaking up Run method (#27)

* Move request id to Executor constructor

* Refactor the logger to respect user verbosity level

* Use Arena allocator instead of device

* Creates initial executor tests

* Merge upstream master (#31)

* Remove all possible shared_ptrs (#30)

* Changes GetLogger to unique_ptr

* Reserve BFloat raw data vector size

* Change HostingEnvironment to being passed by lvalue and rvalue references

* Change routes to getting passed by const references

* Enable full protobuf if building hosting (#32)

* Building hosting application no longer needs use_full_protobuf flag

* Improve hosting application docs

* Move server core into separate folder (#34)

* Turn hosting project off by default (#38)

* Remove vcpkg as a submodule and download/install Boost from source (#39)

* Remove vcpkg

* Use CMake script to download and build Boost as part of the project

* Remove std::move for const references

* Remove error_code.proto

* Change wording of executable help description

* Better GenerateProtobufStatus description

* Remove error_code protobuf from CMake files

* Use all outputs if no filter is given

* Pass MLValue by const reference in MLValueToTensorProto

* Rename variables to argc and argv

* Revert "Use all outputs if no filter is given"

This reverts commit 7554190ab8e50ba6947648c2f3e2a3d4d9606ce0.

* Remove all header guards in favor of #pragma once

* Reserve size for output vector and optimize for-loop

* Use static libs by default for Boost

* Improves documentation for GenerateResponseInJson function

* Start Result enum at 0 instead of 1

* Remove g++ from Ubuntu's install.sh

* Update cmake files

* Give explanation for Result enum type

* Remove all program options shortcuts except for -h

* Add comments for predict.proto

* Fix JSON for error codes

* Add notice on hosting application docs that it's in beta

* Change HostingEnvironment back to a shared_ptr

* Handle empty output_filter field

* Fix build break

* Refactor unit tests location and groups

* First end-to-end test

* Add missing log

* Missing req id and client req id in error response

* Add one test case to validate failed resp header

* Add build flag for hosting app end to end tests

* Update pipeline setup to run e2e test for CI build

* Model Zoo data preparation and tests

* Add protobuf tests

* Remove mention of needing g++-7 in BUILD.md

* Make GetAppLogger const

* Make using_raw_data_ match the styling of other fields

* Avoid copy of strings when initializing model

* Escape JSON strings correctly for error messages (#44)

* Escape JSON strings correctly

* Add test examples with lots of carriage returns

* Add result validation

* Remove temporary path

* Optimize model zoo test execution

* Improve reliability of test cases

* Generate _pb2.py during the build time

* README for integration tests

* Pass environment by pointer instead of shared_ptr to executor (#49)

* More Integration tests

* Remove generated files

* Make session private and use a getter instead (#53)

* logging_level to log_level for CLI

* Single model prediction shortcut

* Health endpoint

* Integration tests

* Rename to onnxruntime server

* Build ONNX Server application on Windows (#57)

* Gets Boost compiling on Windows

* Fix integer conversion and comparison problems

* Use size_t in converter_tests instead of int

* Fix hosting integration tests on Windows

* Removes checks for port because it's an unsigned short

* Fixes comparison between signed and unsigned data types

* Pip install protobuf and numpy

* Missing test data from the rename change

* Fix server app path (#58)

* Pass shared_ptr by const reference to avoid ref count increase (#59)

* Download test model during test setup

* Make download into test_util

* Rename ci pipeline for onnx runtime server

*  Support up to 10MiB http request (#61)

* Changes minimum request size to 10MB to support all models in ONNX Model Zoo
2019-04-30 18:21:23 -07:00

641 lines
26 KiB
CMake

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# Minimum CMake required
cmake_minimum_required(VERSION 3.13)
# Project
project(onnxruntime C CXX)
cmake_policy(SET CMP0069 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
include(CheckCXXCompilerFlag)
include(CheckLanguage)
# Set C++14 as standard for the whole project
set(CMAKE_CXX_STANDARD 14 CACHE STRING "")
# General C# prperties
if (onnxruntime_BUILD_CSHARP)
check_language(CSharp)
if (CMAKE_CSharp_COMPILER)
enable_language(CSharp)
set(CMAKE_DOTNET_TARGET_FRAMEWORK_VERSION v4.6.1)
set(CMAKE_CSharp_FLAGS ${CMAKE_CSharp_FLAGS} "/langversion:6")
message(STATUS "CMAKE_Csharp_Compiler = ${CMAKE_CSharp_COMPILER}")
else()
message(WARNING "Language Csharp is not found in the system")
endif()
endif()
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# NOTE: POSITION INDEPENDENT CODE hurts performance, and it only make sense on POSIX systems
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Enable CTest
enable_testing()
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type not set - using RelWithDebInfo")
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose build type: Debug Release RelWithDebInfo." FORCE)
endif()
# Options
option(onnxruntime_RUN_ONNX_TESTS "Enable ONNX Compatibility Testing" OFF)
option(onnxruntime_GENERATE_TEST_REPORTS "Enable test report generation" OFF)
option(onnxruntime_ENABLE_STATIC_ANALYSIS "Enable static analysis" OFF)
option(onnxruntime_ENABLE_PYTHON "Enable python buildings" OFF)
option(onnxruntime_USE_CUDA "Build with CUDA support" OFF)
option(onnxruntime_USE_NSYNC "Build with NSYNC support. This option only takes effect on Linux" OFF)
option(onnxruntime_USE_EIGEN_FOR_BLAS "Use eign for blas" ON)
option(onnxruntime_USE_MLAS "Use optimized blas library for GEMM and 2D Convolution" ON)
option(onnxruntime_USE_MKLDNN "Build with MKL-DNN support" OFF)
option(onnxruntime_USE_MKLML "Build MKL-DNN with MKL-ML binary dependency" OFF)
option(onnxruntime_USE_NGRAPH "Build with nGraph support" OFF)
option(onnxruntime_USE_OPENBLAS "Use openblas" OFF)
option(onnxruntime_DEV_MODE "Enable developer warnings and treat most of them as error." OFF)
option(onnxruntime_USE_JEMALLOC "Use jecmalloc" OFF)
option(onnxruntime_MSVC_STATIC_RUNTIME "Compile for the static CRT" OFF)
option(onnxruntime_BUILD_UNIT_TESTS "Build ONNXRuntime unit tests" ON)
option(onnxruntime_USE_PREINSTALLED_EIGEN "Use pre-installed EIGEN. Need to provide eigen_SOURCE_PATH if turn this on." OFF)
option(onnxruntime_BUILD_BENCHMARKS "Build ONNXRuntime micro-benchmarks" OFF)
option(onnxruntime_USE_TVM "Build tvm for code-gen" OFF)
option(onnxruntime_BUILD_FOR_NATIVE_MACHINE "Enable this option for turning on optimization specific to this machine" OFF)
option(onnxruntime_USE_LLVM "Build tvm with LLVM" OFF)
option(onnxruntime_USE_OPENMP "Build with OpenMP support" OFF)
option(onnxruntime_BUILD_SHARED_LIB "Build a shared library" OFF)
option(onnxruntime_ENABLE_MICROSOFT_INTERNAL "Use this option to enable/disable microsoft internal only code" OFF)
option(onnxruntime_USE_NUPHAR "Build with Nupha" OFF)
option(onnxruntime_USE_BRAINSLICE "Build with BrainSlice" OFF)
option(onnxruntime_USE_TENSORRT "Build with TensorRT support" OFF)
option(onnxruntime_ENABLE_LTO "Enable link time optimization, which is not stable on older GCCs" OFF)
option(onnxruntime_CROSS_COMPILING "Cross compiling onnx runtime" OFF)
option(onnxruntime_BUILD_SERVER "Build ONNX Runtime Server" OFF)
option(onnxruntime_USE_FULL_PROTOBUF "Use full protobuf" OFF)
option(onnxruntime_DISABLE_CONTRIB_OPS "Disable contrib ops" OFF)
option(onnxruntime_USE_EIGEN_THREADPOOL "Use eigen threadpool. Otherwise OpenMP or a homemade one will be used" OFF)
option(tensorflow_C_PACKAGE_PATH "Path to tensorflow C package installation dir")
set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests" FORCE)
#nsync tests failed on Mac Build
set(NSYNC_ENABLE_TESTS OFF CACHE BOOL "Build protobuf tests" FORCE)
set(ONNX_ML 1)
if(onnxruntime_ENABLE_LTO)
#LTO(or LTCG) is great, in our case it can reduce binary size by 1/3.
#cmake can only help us check if the compiler support LTO or not, it can't tell us if the feature works well
#Don't enable this option in Ubuntu 16.04, protoc will crash
include(CheckIPOSupported)
check_ipo_supported(RESULT ipo_enabled OUTPUT ipo_output)
#TODO: figure out why nsync doesn't work
if(NOT onnxruntime_USE_NSYNC)
if(ipo_enabled)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO ON)
else()
message(WARNING "IPO is not supported: ${ipo_output}")
endif()
endif()
endif()
set(REPO_ROOT ${PROJECT_SOURCE_DIR}/..)
set(ONNXRUNTIME_ROOT ${PROJECT_SOURCE_DIR}/../onnxruntime)
file (STRINGS "${REPO_ROOT}/VERSION_NUMBER" VERSION_NUMBER)
if(onnxruntime_USE_OPENMP)
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
add_definitions(-DUSE_OPENMP)
endif()
endif()
if(onnxruntime_CROSS_COMPILING)
set(CMAKE_CROSSCOMPILING ON)
endif()
#must after OpenMP settings
find_package(Threads)
if (MSVC)
if (onnxruntime_MSVC_STATIC_RUNTIME)
# set all of our submodules to static runtime
set(ONNX_USE_MSVC_STATIC_RUNTIME ON)
set(protobuf_MSVC_STATIC_RUNTIME ON)
set(gtest_force_shared_crt OFF)
# In case we are building static libraries, link also the runtime library statically
# so that MSVCR*.DLL is not required at runtime.
# https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
# This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
# https://gitlab.kitware.com/cmake/community/wikis/FAQ#how-can-i-build-my-msvc-application-with-a-static-runtime
foreach(flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MD")
endforeach(flag_var)
else()
set(ONNX_USE_MSVC_STATIC_RUNTIME OFF)
set(protobuf_WITH_ZLIB OFF CACHE BOOL "" FORCE)
set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link protobuf to static runtime libraries" FORCE)
set(gtest_force_shared_crt ON CACHE BOOL "Use shared (DLL) run-time lib for gtest" FORCE)
endif()
#Always enable exception handling, even for Windows ARM
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
#Disable 4100 globally. Too many this kind errors in protobuf
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4100")
if (NOT onnxruntime_USE_CUDA)
SET (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Gw /GL")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /Gw /GL")
endif()
check_cxx_compiler_flag(-Qspectre HAS_QSPECTRE)
if (HAS_QSPECTRE)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Qspectre")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qspectre")
endif()
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DYNAMICBASE")
check_cxx_compiler_flag(-guard:cf HAS_GUARD_CF)
if (HAS_GUARD_CF)
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /guard:cf")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /guard:cf")
SET(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /guard:cf")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /guard:cf")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /guard:cf")
endif()
else()
if(onnxruntime_BUILD_FOR_NATIVE_MACHINE)
string(APPEND CMAKE_CXX_FLAGS_RELEASE " -march=native -mtune=native")
string(APPEND CMAKE_C_FLAGS_RELEASE " -march=native -mtune=native")
string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -march=native -mtune=native")
string(APPEND CMAKE_C_FLAGS_RELWITHDEBINFO " -march=native -mtune=native")
endif()
if(onnxruntime_BUILD_x86)
set (CMAKE_SYSTEM_PROCESSOR "x86")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2 -mfpmath=sse -Wno-narrowing")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse2 -mfpmath=sse -Wno-narrowing")
endif()
endif()
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
#For Mac compliance
message("Adding flags for Mac builds")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-strong")
endif()
find_package(PNG)
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)
find_package(PythonInterp 3.5 REQUIRED)
find_package(PythonLibs 3.5 REQUIRED)
else()
find_package(PythonInterp 3.4 REQUIRED)
find_package(PythonLibs 3.4 REQUIRED)
endif()
endif()
#target begins from here
if(onnxruntime_BUILD_BENCHMARKS)
if(NOT TARGET benchmark)
# We will not need to test benchmark lib itself.
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable benchmark testing as we don't need it.")
# We will not need to install benchmark since we link it statically.
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "Disable benchmark install to avoid overwriting vendor install.")
add_subdirectory(${PROJECT_SOURCE_DIR}/external/onnx/third_party/benchmark EXCLUDE_FROM_ALL)
endif()
endif()
add_subdirectory(${PROJECT_SOURCE_DIR}/external/nsync EXCLUDE_FROM_ALL)
# External dependencies
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/external)
#Here we support two build mode:
#1. if ONNX_CUSTOM_PROTOC_EXECUTABLE is set, build Protobuf from source, except protoc.exe. This mode is mainly
# for cross-compiling
#2. if ONNX_CUSTOM_PROTOC_EXECUTABLE is not set, Compile everything(including protoc) from source code.
# use protobuf as a submodule
add_subdirectory(${PROJECT_SOURCE_DIR}/external/protobuf/cmake EXCLUDE_FROM_ALL)
set_target_properties(libprotobuf PROPERTIES FOLDER "External/Protobuf")
set_target_properties(libprotobuf-lite PROPERTIES FOLDER "External/Protobuf")
set_target_properties(libprotoc PROPERTIES FOLDER "External/Protobuf")
set_target_properties(protoc PROPERTIES FOLDER "External/Protobuf")
if (onnxruntime_USE_FULL_PROTOBUF)
add_library(protobuf::libprotobuf ALIAS libprotobuf)
else()
add_library(protobuf::libprotobuf ALIAS libprotobuf-lite)
endif()
add_executable(protobuf::protoc ALIAS protoc)
include(protobuf_function.cmake)
if (onnxruntime_USE_FULL_PROTOBUF)
add_definitions(-DUSE_FULL_PROTOBUF)
endif()
if (onnxruntime_DISABLE_CONTRIB_OPS)
add_definitions(-DDISABLE_CONTRIB_OPS)
endif()
if (onnxruntime_USE_CUDA AND "${onnxruntime_CUDNN_HOME}" STREQUAL "")
message(FATAL_ERROR "onnxruntime_CUDNN_HOME required for onnxruntime_USE_CUDA")
endif()
if (onnxruntime_USE_EIGEN_FOR_BLAS)
add_definitions(-DUSE_EIGEN_FOR_BLAS)
endif()
if (onnxruntime_USE_OPENBLAS AND "${onnxruntime_OPENBLAS_HOME}" STREQUAL "" AND WIN32)
# On linux we assume blas is installed via 'apt-get install libopenblas-dev'
message(FATAL_ERROR "onnxruntime_OPENBLAS_HOME required for onnxruntime_USE_OPENBLAS")
endif()
if (onnxruntime_USE_OPENBLAS AND onnxruntime_USE_EIGEN_FOR_BLAS)
message(FATAL_ERROR "use one of onnxruntime_USE_OPENBLAS, onnxruntime_USE_EIGEN_FOR_BLAS")
endif()
# if ON put all the unit tests in a single project so that code coverage is more comprehensive.
# defaulting to that and most likely removing option to have separate unit test projects in the near future.
set(SingleUnitTestProject ON)
if (onnxruntime_SPLIT_UNIT_TEST_PROJECTS)
set(SingleUnitTestProject OFF)
endif()
get_filename_component(ONNXRUNTIME_ROOT "${ONNXRUNTIME_ROOT}" ABSOLUTE)
get_filename_component(REPO_ROOT "${REPO_ROOT}" ABSOLUTE)
set(ONNXRUNTIME_INCLUDE_DIR ${REPO_ROOT}/include/onnxruntime)
add_subdirectory(external/date EXCLUDE_FROM_ALL)
add_subdirectory(external/gsl EXCLUDE_FROM_ALL)
add_subdirectory(external/re2 EXCLUDE_FROM_ALL)
set_target_properties(re2 PROPERTIES FOLDER "External/re2")
add_library(gsl ALIAS GSL)
# bounds checking behavior.
# throw instead of calling terminate if there's a bounds checking violation.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGSL_THROW_ON_CONTRACT_VIOLATION")
# no bounds checking in release build so no perf cost
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DGSL_UNENFORCED_ON_CONTRACT_VIOLATION")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -DGSL_UNENFORCED_ON_CONTRACT_VIOLATION")
include(eigen)
#onnxruntime_EXTERNAL_LIBRARIES could contain onnx, onnx_proto,libprotobuf, cuda/cudnn, jemalloc,
# mkldnn/mklml, openblas, onnxruntime_codegen_tvm, tvm, nnvm_compiler and pthread
# pthread is always at the last
set(onnxruntime_EXTERNAL_LIBRARIES onnx onnx_proto protobuf::libprotobuf re2)
function(onnxruntime_add_include_to_target dst_target)
foreach(src_target ${ARGN})
target_include_directories(${dst_target} PRIVATE $<TARGET_PROPERTY:${src_target},INTERFACE_INCLUDE_DIRECTORIES>)
target_compile_definitions(${dst_target} PRIVATE $<TARGET_PROPERTY:${src_target},INTERFACE_COMPILE_DEFINITIONS>)
endforeach()
endfunction()
set(onnxruntime_EXTERNAL_DEPENDENCIES gsl onnx_proto re2)
# TVM
if (onnxruntime_USE_TVM)
if (onnxruntime_USE_CUDA)
set(USE_CUDA ON)
endif()
if (onnxruntime_USE_LLVM)
set(USE_LLVM ON)
add_definitions(-DUSE_TVM_WITH_LLVM)
endif()
add_subdirectory(${PROJECT_SOURCE_DIR}/external/tvm EXCLUDE_FROM_ALL)
set_target_properties(tvm PROPERTIES FOLDER "External/tvm")
set_target_properties(tvm_topi PROPERTIES FOLDER "External/tvm")
set_target_properties(tvm_runtime PROPERTIES FOLDER "External/tvm")
set_target_properties(nnvm_compiler PROPERTIES FOLDER "External/tvm")
set(TVM_INCLUDES ${PROJECT_SOURCE_DIR}/external/tvm/include
${PROJECT_SOURCE_DIR}/external/tvm/3rdparty/dmlc-core/include
${PROJECT_SOURCE_DIR}/external/tvm/3rdparty/dlpack/include
$<TARGET_PROPERTY:tvm,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:tvm_topi,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:nnvm_compiler,INTERFACE_INCLUDE_DIRECTORIES>)
add_definitions(-DUSE_TVM)
set(onnxruntime_tvm_libs onnxruntime_codegen_tvm)
list(APPEND onnxruntime_EXTERNAL_LIBRARIES tvm nnvm_compiler)
list(APPEND onnxruntime_EXTERNAL_DEPENDENCIES tvm nnvm_compiler)
endif()
# ONNX
add_subdirectory(onnx)
#set_target_properties(gen_onnx_proto PROPERTIES FOLDER "External/ONNX")
# fix a warning in onnx code we can't do anything about
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEIGEN_HAS_C99_MATH") # required to be set explicitly to enable Eigen-Unsupported SpecialFunctions
endif()
if (onnxruntime_RUN_ONNX_TESTS)
add_definitions(-DORT_RUN_EXTERNAL_ONNX_TESTS)
endif()
if (onnxruntime_USE_MLAS)
add_definitions(-DUSE_MLAS)
endif()
#Adjust warning flags
if (WIN32)
add_definitions(-DPLATFORM_WINDOWS -DNOGDI -DNOMINMAX -D_USE_MATH_DEFINES)
# parallel build
# These compiler opitions cannot be forwarded to NVCC, so cannot use add_compiler_options
string(APPEND CMAKE_CXX_FLAGS " /MP")
string(APPEND CMAKE_CXX_FLAGS
" /wd4503" # Decorated name length exceeded.
" /wd4127" # conditional expression is constant.
" /wd4146" # unary minus operator applied to unsigned type. Needed for Protobuf
)
if (onnxruntime_ENABLE_STATIC_ANALYSIS)
string(APPEND CMAKE_CXX_FLAGS
" /analyze:WX- "
# disable warning because there are many occurrences from test macros
" /wd6326 " # potential comparison of a constant with another constant
)
endif()
# set compile warning level to 3 on CUDA build but 4 on CPU-only build
if(onnxruntime_USE_CUDA)
#CMake hardcoded /W3 in its 'Windows-NVIDIA-CUDA.cmake'. We'd better keep consistent with it.
#Change it to /W4 will result build failure
string(APPEND CMAKE_CXX_FLAGS " /W3")
else()
string(APPEND CMAKE_CXX_FLAGS " /W4")
endif()
# treat warning as error only on x64 platform.
# For x86 and cross-compiled ARM64 binaries, there are too many warnings to fix, hence ignore warnings for now
if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND onnxruntime_DEV_MODE AND NOT CMAKE_CROSSCOMPILING)
# treat warnings as errors
string(APPEND CMAKE_CXX_FLAGS " /WX")
foreach(type EXE STATIC SHARED)
set(CMAKE_${type}_LINKER_FLAGS "${CMAKE_${type}_LINKER_FLAGS} /WX")
endforeach()
endif()
# set linker flags to minimize the binary size.
if (MSVC)
foreach(type EXE SHARED)
set(CMAKE_${type}_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_${type}_LINKER_FLAGS_RELWITHDEBINFO} /OPT:REF,ICF,LBR")
set(CMAKE_${type}_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_${type}_LINKER_FLAGS_RELWITHDEBINFO} /INCREMENTAL:NO")
if (NOT onnxruntime_USE_CUDA)
set(CMAKE_${type}_LINKER_FLAGS_RELEASE "${CMAKE_${type}_LINKER_FLAGS_RELEASE} /LTCG")
set(CMAKE_${type}_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_${type}_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
endif()
endforeach()
endif()
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")
if(onnxruntime_DEV_MODE)
if(NOT onnxruntime_USE_TVM)
string(APPEND CMAKE_CXX_FLAGS " -Werror")
string(APPEND CMAKE_C_FLAGS " -Werror")
endif()
endif()
check_cxx_compiler_flag(-Wunused-but-set-variable HAS_UNUSED_BUT_SET_VARIABLE)
check_cxx_compiler_flag(-Wunused-parameter HAS_UNUSED_PARAMETER)
check_cxx_compiler_flag(-Wcast-function-type HAS_CAST_FUNCTION_TYPE)
check_cxx_compiler_flag(-Wparentheses HAS_PARENTHESES)
check_cxx_compiler_flag(-Wuseless-cast HAS_USELESS_CAST)
check_cxx_compiler_flag(-Wnonnull-compare HAS_NONNULL_COMPARE)
check_cxx_compiler_flag(-Wtautological-pointer-compare HAS_TAUTOLOGICAL_POINTER_COMPARE)
check_cxx_compiler_flag(-Wcatch-value HAS_CATCH_VALUE)
check_cxx_compiler_flag(-Wmissing-braces HAS_MISSING_BRACES)
check_cxx_compiler_flag(-Wignored-attributes HAS_IGNORED_ATTRIBUTES)
if(HAS_TAUTOLOGICAL_POINTER_COMPARE)
#we may have extra null pointer checkings in debug build, it's not an issue
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -Wno-tautological-pointer-compare")
string(APPEND CMAKE_C_FLAGS_DEBUG " -Wno-tautological-pointer-compare")
endif()
if(HAS_NONNULL_COMPARE)
#we may have extra null pointer checkings in debug build, it's not an issue
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -Wno-nonnull-compare")
string(APPEND CMAKE_C_FLAGS_DEBUG " -Wno-nonnull-compare")
endif()
string(APPEND CMAKE_CXX_FLAGS " -Wno-error=sign-compare")
if(HAS_PARENTHESES)
string(APPEND CMAKE_CXX_FLAGS " -Wno-parentheses")
endif()
endif()
if (onnxruntime_USE_JEMALLOC)
if (Win32)
message( FATAL_ERROR "Jemalloc is not supported on Windows." )
endif()
include(jemalloc)
add_definitions(-DUSE_JEMALLOC=1)
list(APPEND onnxruntime_EXTERNAL_LIBRARIES ${JEMALLOC_STATIC_LIBRARIES})
list(APPEND onnxruntime_EXTERNAL_DEPENDENCIES jemalloc)
endif()
include_directories(${ONNXRUNTIME_INCLUDE_DIR})
if (onnxruntime_USE_MKLDNN OR onnxruntime_USE_MKLML)
include(mkldnn)
endif()
if (onnxruntime_USE_MKLML)
add_definitions(-DUSE_MKLML=1 -DUSE_MKLML_FOR_BLAS=1)
if (WIN32 OR APPLE)
list(APPEND onnxruntime_EXTERNAL_LIBRARIES mklml)
else()
list(APPEND onnxruntime_EXTERNAL_LIBRARIES mklml_intel)
endif()
list(APPEND onnxruntime_EXTERNAL_DEPENDENCIES project_mklml)
include_directories(${MKLML_INCLUDE_DIR})
link_directories(${MKLML_LIB_DIR})
endif()
if (onnxruntime_USE_MKLDNN)
list(APPEND onnxruntime_EXTERNAL_LIBRARIES mkldnn)
list(APPEND onnxruntime_EXTERNAL_DEPENDENCIES project_mkldnn)
link_directories(${MKLDNN_LIB_DIR})
endif()
if (onnxruntime_USE_NGRAPH)
if (Win32)
message(FATAL_ERROR "nGraph is not currently supported on Windows.")
endif()
#if (onnxruntime_USE_OPENMP)
# message(FATAL_ERROR "Please set onnxruntime_USE_OPENMP=OFF for nGraph execution provider.")
#endif()
if (NOT onnxruntime_USE_FULL_PROTOBUF)
message(FATAL_ERROR "Please set onnxruntime_USE_FULL_PROTOBUF=ON for nGraph execution provider.")
endif()
add_definitions(-DUSE_NGRAPH=1)
include(ngraph)
list(APPEND onnxruntime_EXTERNAL_LIBRARIES ngraph)
list(APPEND onnxruntime_EXTERNAL_DEPENDENCIES ngraph)
endif()
if (onnxruntime_USE_OPENBLAS)
add_definitions(-DUSE_OPENBLAS=1)
if (WIN32)
include_directories(${onnxruntime_OPENBLAS_HOME})
list(APPEND onnxruntime_EXTERNAL_LIBRARIES ${onnxruntime_OPENBLAS_HOME}/lib/libopenblas.lib)
else()
# on linux we assume blas is installed via 'apt-get install libopenblas-dev'
list(APPEND onnxruntime_EXTERNAL_LIBRARIES openblas)
endif()
endif()
configure_file(onnxruntime_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/onnxruntime_config.h)
if (onnxruntime_USE_CUDA)
add_definitions(-DUSE_CUDA=1)
#The following 6 lines are copied from https://gitlab.kitware.com/cmake/cmake/issues/17559
set( CMAKE_CUDA_FLAGS "" CACHE STRING "" )
if ( CMAKE_CUDA_FLAGS )
list(REMOVE_ITEM CMAKE_CUDA_FLAGS "-cudart static")
endif()
string(APPEND CMAKE_CUDA_FLAGS "-cudart shared")
enable_language(CUDA)
set(CMAKE_CUDA_STANDARD 14)
file(TO_CMAKE_PATH ${onnxruntime_CUDNN_HOME} onnxruntime_CUDNN_HOME)
set(ONNXRUNTIME_CUDA_LIBRARIES ${CUDA_LIBRARIES})
list(APPEND ONNXRUNTIME_CUDA_LIBRARIES cublas cudnn)
if (WIN32)
link_directories(${onnxruntime_CUDNN_HOME}/lib/x64)
string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\1" onnxruntime_CUDA_VERSION_MAJOR ${CMAKE_CUDA_COMPILER_VERSION})
string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\2" onnxruntime_CUDA_VERSION_MINOR ${CMAKE_CUDA_COMPILER_VERSION})
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DELAYLOAD:cublas64_${onnxruntime_CUDA_VERSION_MAJOR}${onnxruntime_CUDA_VERSION_MINOR}.dll")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DELAYLOAD:cudart64_${onnxruntime_CUDA_VERSION_MAJOR}${onnxruntime_CUDA_VERSION_MINOR}.dll")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DELAYLOAD:cudnn64_7.dll")
else()
link_directories(${onnxruntime_CUDNN_HOME}/lib64)
endif()
list(APPEND onnxruntime_EXTERNAL_LIBRARIES ${ONNXRUNTIME_CUDA_LIBRARIES})
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -gencode=arch=compute_30,code=sm_30") # K series
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -gencode=arch=compute_50,code=sm_50") # M series
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -gencode=arch=compute_60,code=sm_60") # P series
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -gencode=arch=compute_70,code=sm_70") # V series
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --default-stream per-thread")
if (NOT WIN32)
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} --compiler-options -fPIC")
endif()
endif()
if (onnxruntime_USE_TENSORRT)
if (WIN32)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DELAYLOAD:nvinfer.dll")
endif()
endif()
if (onnxruntime_USE_TVM)
if (WIN32 AND MSVC)
# wd4100: identifier' : unreferenced formal parameter
# wd4244: conversion from 'int' to 'char', possible loss of data
# wd4251: class X needs to have dll-interface to be used by clients of class Y
# wd4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
# wd4275: non dll-interface class X used as base for dll-interface class Y
# wd4389: signed/unsigned mismatch
# wd4456: declaration of X hides previous local declaration
set(DISABLED_WARNINGS_FOR_TVM "/wd4100" "/wd4244" "/wd4251" "/wd4267" "/wd4275" "/wd4389" "/wd4456")
else()
set(DISABLED_WARNINGS_FOR_TVM "-Wno-error=ignored-qualifiers")
if(HAS_UNUSED_PARAMETER)
list(APPEND DISABLED_WARNINGS_FOR_TVM "-Wno-error=unused-parameter")
endif()
if(HAS_CATCH_VALUE)
#TODO: send a PR to TVM and fix it
list(APPEND DISABLED_WARNINGS_FOR_TVM "-Wno-error=catch-value")
endif()
endif()
include(onnxruntime_codegen.cmake)
endif()
if (onnxruntime_ENABLE_MICROSOFT_INTERNAL)
add_definitions(-DMICROSOFT_INTERNAL)
endif()
#names in this var must match the directory names under onnxruntime/core/providers
set(ONNXRUNTIME_PROVIDER_NAMES cpu)
include(onnxruntime_common.cmake)
include(onnxruntime_graph.cmake)
include(onnxruntime_framework.cmake)
include(onnxruntime_util.cmake)
#The next file will define 'onnxruntime_libs' cmake var
include(onnxruntime_providers.cmake)
include(onnxruntime_optimizer.cmake)
include(onnxruntime_session.cmake)
include(onnxruntime_mlas.cmake)
if(WIN32)
list(APPEND onnxruntime_EXTERNAL_LIBRARIES Shlwapi)
list(APPEND onnxruntime_EXTERNAL_LIBRARIES debug Dbghelp)
else()
if(onnxruntime_USE_NSYNC)
list(APPEND onnxruntime_EXTERNAL_LIBRARIES nsync_cpp)
endif()
list(APPEND onnxruntime_EXTERNAL_LIBRARIES ${CMAKE_DL_LIBS} Threads::Threads)
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
list(APPEND onnxruntime_EXTERNAL_LIBRARIES log)
endif()
#The following files may use the 'onnxruntime_libs' and 'onnxruntime_EXTERNAL_LIBRARIES' vars
if (onnxruntime_BUILD_SHARED_LIB)
include(onnxruntime.cmake)
endif()
if (onnxruntime_BUILD_SERVER)
include(onnxruntime_server.cmake)
endif()
# some of the tests rely on the shared libs to be
# built; hence the ordering
if (onnxruntime_BUILD_UNIT_TESTS)
if (onnxruntime_ENABLE_PYTHON)
if(UNIX)
set(CMAKE_SKIP_BUILD_RPATH ON)
endif()
include(onnxruntime_python.cmake)
endif()
# we need to make sure this is turned off since it
# turned ON by the previous step when building a shared lib
set(CMAKE_SKIP_BUILD_RPATH OFF)
# gtest and gmock
add_subdirectory(${PROJECT_SOURCE_DIR}/external/googletest EXCLUDE_FROM_ALL)
set_target_properties(gmock PROPERTIES FOLDER "External/GTest")
set_target_properties(gmock_main PROPERTIES FOLDER "External/GTest")
set_target_properties(gtest PROPERTIES FOLDER "External/GTest")
set_target_properties(gtest_main PROPERTIES FOLDER "External/GTest")
include(onnxruntime_unittests.cmake)
endif()
if (onnxruntime_BUILD_CSHARP)
message(STATUS "CSharp Build is enabled")
# set_property(GLOBAL PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "netstandard2.0")
include(onnxruntime_csharp.cmake)
endif()