| 1 |
# Minimum CMake required |
| 2 |
cmake_minimum_required(VERSION 3.26) |
| 3 |
# Project |
| 4 |
project(onnx LANGUAGES CXX) |
| 5 |
|
| 6 |
include(cmake/Utils.cmake) |
| 7 |
# Honor visibility properties for all target types. |
| 8 |
cmake_policy(SET CMP0063 NEW) |
| 9 |
# Modules FindPython3 and FindPython use LOCATION for lookup strategy. |
| 10 |
cmake_policy(SET CMP0094 NEW) |
| 11 |
|
| 12 |
# Set default build type |
| 13 |
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) |
| 14 |
if(NOT isMultiConfig AND NOT CMAKE_BUILD_TYPE) |
| 15 |
message(STATUS "Build type not set - defaulting to Release") |
| 16 |
set( |
| 17 |
CMAKE_BUILD_TYPE "Release" |
| 18 |
CACHE |
| 19 |
STRING |
| 20 |
"Choose the type of build from: Debug Release RelWithDebInfo MinSizeRel Coverage." |
| 21 |
FORCE) |
| 22 |
endif() |
| 23 |
|
| 24 |
# https://reproducible-builds.org/docs/source-date-epoch/ |
| 25 |
if(DEFINED ENV{SOURCE_DATE_EPOCH}) |
| 26 |
execute_process( |
| 27 |
COMMAND "date" "-u" "-d" "@$ENV{SOURCE_DATE_EPOCH}" "+%Y-%m-%d" |
| 28 |
OUTPUT_VARIABLE BUILD_DATE |
| 29 |
OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 30 |
else() |
| 31 |
execute_process( |
| 32 |
COMMAND "date" "+%Y-%m-%d" |
| 33 |
OUTPUT_VARIABLE BUILD_DATE |
| 34 |
OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 35 |
endif() |
| 36 |
|
| 37 |
if(NOT BUILD_SHARED_LIBS) |
| 38 |
# by default, cmake builds static libraries |
| 39 |
set(BUILD_SHARED_LIBS OFF) |
| 40 |
endif() |
| 41 |
|
| 42 |
|
| 43 |
option(ONNX_BUILD_PYTHON "Build Python binaries" OFF) |
| 44 |
option(ONNX_BUILD_CUSTOM_PROTOBUF "Build and use ONNX's own protobuf" OFF) |
| 45 |
option(ONNX_USE_PROTOBUF_SHARED_LIBS "Build ONNX using protobuf shared library." OFF) |
| 46 |
option(ONNX_GEN_PB_TYPE_STUBS "Generate protobuf python type stubs" ON) |
| 47 |
option(ONNX_WERROR "Build with Werror" OFF) |
| 48 |
option(ONNX_COVERAGE "Build with coverage instrumentation" OFF) |
| 49 |
option(ONNX_BUILD_TESTS "Build ONNX C++ APIs Tests" OFF) |
| 50 |
option(ONNX_USE_ASAN "Build ONNX with ASAN" OFF) |
| 51 |
option(ONNX_USE_LITE_PROTO "Use lite protobuf instead of full." OFF) |
| 52 |
option(ONNX_DISABLE_EXCEPTIONS "Disable exception handling." OFF) |
| 53 |
option(ONNX_DISABLE_STATIC_REGISTRATION "Disable static registration for ONNX operator schemas." OFF) |
| 54 |
option(ONNX_USE_UNITY_BUILD "Enable Unity (Jumbo) build for" OFF) |
| 55 |
if(WIN32) |
| 56 |
option(ONNX_USE_MSVC_STATIC_RUNTIME "Build with MSVC static runtime" OFF) |
| 57 |
endif() |
| 58 |
|
| 59 |
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 60 |
|
| 61 |
if(NOT DEFINED ONNX_ML) |
| 62 |
if(DEFINED ENV{ONNX_ML}) |
| 63 |
set(DEFAULT_ONNX_ML $ENV{ONNX_ML}) |
| 64 |
else() |
| 65 |
set(DEFAULT_ONNX_ML ON) |
| 66 |
endif() |
| 67 |
option(ONNX_ML "Enable traditional ML API." ${DEFAULT_ONNX_ML}) |
| 68 |
endif() |
| 69 |
|
| 70 |
if(NOT DEFINED ONNX_VERIFY_PROTO3) |
| 71 |
if(DEFINED ENV{ONNX_VERIFY_PROTO3}) |
| 72 |
set(PROTO3_ENABLED $ENV{ONNX_VERIFY_PROTO3}) |
| 73 |
else() |
| 74 |
set(PROTO3_ENABLED OFF) |
| 75 |
endif() |
| 76 |
option(ONNX_VERIFY_PROTO3 "Generate code by proto3" ${PROTO3_ENABLED}) |
| 77 |
endif() |
| 78 |
|
| 79 |
if(NOT DEFINED CMAKE_CXX_STANDARD) |
| 80 |
set(CMAKE_CXX_STANDARD 17) |
| 81 |
else() |
| 82 |
if(CMAKE_CXX_STANDARD VERSION_LESS 17) |
| 83 |
message(FATAL_ERROR "At least C++17 is required.") |
| 84 |
endif() |
| 85 |
endif() |
| 86 |
|
| 87 |
include(GNUInstallDirs) |
| 88 |
|
| 89 |
set(ONNX_ROOT ${onnx_SOURCE_DIR}) |
| 90 |
|
| 91 |
# Read ONNX version |
| 92 |
file(READ "${ONNX_ROOT}/VERSION_NUMBER" ONNX_VERSION) |
| 93 |
string(STRIP "${ONNX_VERSION}" ONNX_VERSION) |
| 94 |
|
| 95 |
if(NOT MSVC) |
| 96 |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor") |
| 97 |
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") |
| 98 |
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") |
| 99 |
if(ONNX_COVERAGE) |
| 100 |
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") |
| 101 |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") |
| 102 |
endif() |
| 103 |
endif() |
| 104 |
|
| 105 |
if(NOT ONNX_NAMESPACE) |
| 106 |
set(ONNX_NAMESPACE "onnx") |
| 107 |
endif() |
| 108 |
|
| 109 |
if(MSVC) |
| 110 |
if(NOT ONNX_DISABLE_EXCEPTIONS) |
| 111 |
string(APPEND CMAKE_CXX_FLAGS " /EHsc /wd26812") |
| 112 |
string(APPEND CMAKE_C_FLAGS " /EHsc /wd26812") |
| 113 |
endif() |
| 114 |
add_compile_options(/MP /utf-8 /nologo) |
| 115 |
add_compile_options( |
| 116 |
/wd5287 # https://developercommunity.visualstudio.com/t/warning-C5287:-operands-are-different-e/10877942?sort=newest |
| 117 |
/Zc:lambda # https://developercommunity.visualstudio.com/t/fatal--error-C1001:-Internal-compiler-er/10906076 |
| 118 |
) |
| 119 |
endif() |
| 120 |
|
| 121 |
if(ONNX_DISABLE_EXCEPTIONS) |
| 122 |
add_compile_definitions("ONNX_NO_EXCEPTIONS") |
| 123 |
# Disable C++ exceptions. |
| 124 |
if(MSVC) |
| 125 |
string(REGEX REPLACE "/EHsc" "/EHs-c-" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 126 |
add_definitions(-D_HAS_EXCEPTIONS=0) |
| 127 |
else() |
| 128 |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables") |
| 129 |
endif() |
| 130 |
endif() |
| 131 |
|
| 132 |
if(ONNX_BUILD_PYTHON) |
| 133 |
set(python_dev_component Development.Module Development.SABIModule) |
| 134 |
endif() |
| 135 |
|
| 136 |
if(CMAKE_CROSSCOMPILING) |
| 137 |
# When cross-compiling, the interpreter and the compiling/linking steps |
| 138 |
# must use a different package. See the discussion about this at |
| 139 |
# https://gitlab.kitware.com/cmake/cmake/-/issues/25145 |
| 140 |
if(ONNX_BUILD_PYTHON) |
| 141 |
find_package(Python3 REQUIRED COMPONENTS ${python_dev_component}) |
| 142 |
endif() |
| 143 |
find_package(Python REQUIRED COMPONENTS Interpreter) |
| 144 |
set(ONNX_PYTHON_INTERPRETER Python::Interpreter) |
| 145 |
else() |
| 146 |
find_package(Python3 REQUIRED COMPONENTS Interpreter ${python_dev_component}) |
| 147 |
# Find Python for nanobind |
| 148 |
set(Python_EXECUTABLE ${Python3_EXECUTABLE}) |
| 149 |
find_package(Python REQUIRED COMPONENTS Interpreter ${python_dev_component}) |
| 150 |
set(ONNX_PYTHON_INTERPRETER Python3::Interpreter) |
| 151 |
endif() |
| 152 |
|
| 153 |
if(CMAKE_SYSTEM_NAME STREQUAL "AIX") |
| 154 |
set(CMAKE_NO_SYSTEM_FROM_IMPORTED 1) |
| 155 |
endif() |
| 156 |
|
| 157 |
# Build the libraries with -fPIC including the protobuf lib. |
| 158 |
if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE) |
| 159 |
set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 160 |
endif() |
| 161 |
|
| 162 |
list(APPEND CMAKE_MODULE_PATH ${ONNX_ROOT}/cmake/external) |
| 163 |
if(NOT ONNX_BUILD_CUSTOM_PROTOBUF) |
| 164 |
if((ONNX_USE_LITE_PROTO AND TARGET protobuf::libprotobuf-lite) OR ((NOT ONNX_USE_LITE_PROTO) AND TARGET protobuf::libprotobuf)) |
| 165 |
# Sometimes we need to use protoc compiled for host architecture while linking |
| 166 |
# libprotobuf against target architecture. See https://github.com/caffe2/caffe |
| 167 |
# 2/blob/96f35ad75480b25c1a23d6e9e97bccae9f7a7f9c/cmake/ProtoBuf.cmake#L92-L99 |
| 168 |
if(EXISTS "${ONNX_CUSTOM_PROTOC_EXECUTABLE}") |
| 169 |
message(STATUS "Using custom protoc executable") |
| 170 |
set(ONNX_PROTOC_EXECUTABLE ${ONNX_CUSTOM_PROTOC_EXECUTABLE}) |
| 171 |
else() |
| 172 |
if(TARGET protobuf::protoc) |
| 173 |
set(ONNX_PROTOC_EXECUTABLE $<TARGET_FILE:protobuf::protoc>) |
| 174 |
endif() |
| 175 |
endif() |
| 176 |
else() |
| 177 |
# Customized version of find Protobuf. We need to avoid situations mentioned |
| 178 |
# in https://github.com/caffe2/caffe2/blob/b7d983f255ef5496474f1ea188edb5e0ac4 |
| 179 |
# 42761/cmake/ProtoBuf.cmake#L82-L92 The following section is stolen from |
| 180 |
# cmake/ProtoBuf.cmake in Caffe2 |
| 181 |
find_program(Protobuf_PROTOC_EXECUTABLE |
| 182 |
NAMES protoc |
| 183 |
DOC "The Google Protocol Buffers Compiler") |
| 184 |
|
| 185 |
# Only if protoc was found, seed the include directories and libraries. We |
| 186 |
# assume that protoc is installed at PREFIX/bin. We use get_filename_component |
| 187 |
# to resolve PREFIX. |
| 188 |
if(Protobuf_PROTOC_EXECUTABLE) |
| 189 |
set(ONNX_PROTOC_EXECUTABLE ${Protobuf_PROTOC_EXECUTABLE}) |
| 190 |
get_filename_component(_PROTOBUF_INSTALL_PREFIX |
| 191 |
${Protobuf_PROTOC_EXECUTABLE} DIRECTORY) |
| 192 |
get_filename_component(_PROTOBUF_INSTALL_PREFIX |
| 193 |
${_PROTOBUF_INSTALL_PREFIX}/.. REALPATH) |
| 194 |
find_library(Protobuf_PROTOC_LIBRARY |
| 195 |
NAMES protoc |
| 196 |
PATHS ${_PROTOBUF_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} |
| 197 |
NO_DEFAULT_PATH) |
| 198 |
if(ONNX_USE_LITE_PROTO) |
| 199 |
find_library(Protobuf_LITE_LIBRARY |
| 200 |
NAMES protobuf-lite |
| 201 |
PATHS ${_PROTOBUF_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} |
| 202 |
NO_DEFAULT_PATH) |
| 203 |
else() |
| 204 |
find_library(Protobuf_LIBRARY |
| 205 |
NAMES protobuf |
| 206 |
PATHS ${_PROTOBUF_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} |
| 207 |
NO_DEFAULT_PATH) |
| 208 |
endif(ONNX_USE_LITE_PROTO) |
| 209 |
find_path(Protobuf_INCLUDE_DIR google/protobuf/service.h |
| 210 |
PATHS ${_PROTOBUF_INSTALL_PREFIX}/include |
| 211 |
NO_DEFAULT_PATH) |
| 212 |
if(ONNX_USE_PROTOBUF_SHARED_LIBS) |
| 213 |
set(Protobuf_USE_STATIC_LIBS OFF) |
| 214 |
else() |
| 215 |
set(Protobuf_USE_STATIC_LIBS ON) |
| 216 |
endif() |
| 217 |
find_package(Protobuf) |
| 218 |
if(Protobuf_FOUND) |
| 219 |
set(PROTOBUF_DIR "${_PROTOBUF_INSTALL_PREFIX}") |
| 220 |
set(Build_Protobuf OFF) |
| 221 |
if("${Protobuf_VERSION}" VERSION_GREATER_EQUAL "4.22.0") |
| 222 |
# There are extra dependencies for protobuf. |
| 223 |
find_package(absl REQUIRED) |
| 224 |
find_package(utf8_range) |
| 225 |
message(STATUS "absl_VERSION: ${absl_VERSION}") |
| 226 |
set(protobuf_ABSL_USED_TARGETS |
| 227 |
absl::absl_check |
| 228 |
absl::absl_log |
| 229 |
absl::algorithm |
| 230 |
absl::base |
| 231 |
absl::bind_front |
| 232 |
absl::bits |
| 233 |
absl::btree |
| 234 |
absl::cleanup |
| 235 |
absl::cord |
| 236 |
absl::core_headers |
| 237 |
absl::debugging |
| 238 |
absl::die_if_null |
| 239 |
absl::dynamic_annotations |
| 240 |
absl::flags |
| 241 |
absl::flat_hash_map |
| 242 |
absl::flat_hash_set |
| 243 |
absl::function_ref |
| 244 |
absl::hash |
| 245 |
absl::layout |
| 246 |
absl::log_initialize |
| 247 |
absl::log_severity |
| 248 |
absl::memory |
| 249 |
absl::node_hash_map |
| 250 |
absl::node_hash_set |
| 251 |
absl::optional |
| 252 |
absl::span |
| 253 |
absl::status |
| 254 |
absl::statusor |
| 255 |
absl::strings |
| 256 |
absl::synchronization |
| 257 |
absl::time |
| 258 |
absl::type_traits |
| 259 |
absl::utility |
| 260 |
absl::variant |
| 261 |
utf8_range::utf8_range |
| 262 |
utf8_range::utf8_validity |
| 263 |
) |
| 264 |
endif() |
| 265 |
endif() |
| 266 |
endif() |
| 267 |
endif() |
| 268 |
endif() |
| 269 |
if(NOT ONNX_PROTOC_EXECUTABLE) |
| 270 |
set(Build_Protobuf ON) |
| 271 |
set(protobuf_MSVC_STATIC_RUNTIME ${ONNX_USE_MSVC_STATIC_RUNTIME}) |
| 272 |
|
| 273 |
include(FetchContent) |
| 274 |
set(ABSL_PROPAGATE_CXX_STD 1) |
| 275 |
set(ONNX_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}) |
| 276 |
set(ONNX_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) |
| 277 |
# Use this setting to build third-party libs. |
| 278 |
set(BUILD_SHARED_LIBS ${ONNX_USE_PROTOBUF_SHARED_LIBS}) |
| 279 |
set(ProtobufURL https://github.com/protocolbuffers/protobuf/releases/download/v31.1/protobuf-31.1.tar.gz) |
| 280 |
set(ProtobufSHA1 da10aaa3bf779735a8a9acde1256a47ce5d148be) |
| 281 |
FetchContent_Declare( |
| 282 |
Protobuf |
| 283 |
URL ${ProtobufURL} |
| 284 |
URL_HASH SHA1=${ProtobufSHA1} |
| 285 |
) |
| 286 |
set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests" FORCE) |
| 287 |
message(STATUS "Download and build Protobuf from ${ProtobufURL}") |
| 288 |
FetchContent_MakeAvailable(Protobuf) |
| 289 |
set(ONNX_PROTOC_EXECUTABLE $<TARGET_FILE:protobuf::protoc>) |
| 290 |
set(Protobuf_VERSION "6.31.1") |
| 291 |
# Change back the BUILD_SHARED_LIBS to control the onnx project. |
| 292 |
set(BUILD_SHARED_LIBS ${ONNX_BUILD_SHARED_LIBS}) |
| 293 |
set(PROTOBUF_DIR "${protobuf_BINARY_DIR}") |
| 294 |
set(CMAKE_CXX_FLAGS ${ONNX_CMAKE_CXX_FLAGS}) |
| 295 |
endif() |
| 296 |
message(STATUS "ONNX_PROTOC_EXECUTABLE: ${ONNX_PROTOC_EXECUTABLE}") |
| 297 |
|
| 298 |
# function(RELATIVE_PROTOBUF_GENERATE_CPP SRCS HDRS ROOT_DIR) from https://githu |
| 299 |
# b.com/tensorflow/tensorflow/blob/d2c3b873c6f8ff999a2e4ee707a84ff00d9c15a5/tens |
| 300 |
# orflow/contrib/cmake/tf_core_framework.cmake to solve the problem that |
| 301 |
# customized dir can't be specified when calling PROTOBUF_GENERATE_CPP. |
| 302 |
function(RELATIVE_PROTOBUF_GENERATE_CPP SRCS) |
| 303 |
if(NOT ARGN) |
| 304 |
message( |
| 305 |
SEND_ERROR |
| 306 |
"Error: RELATIVE_PROTOBUF_GENERATE_CPP() called without any proto files" |
| 307 |
) |
| 308 |
return() |
| 309 |
endif() |
| 310 |
|
| 311 |
set(${SRCS}) |
| 312 |
|
| 313 |
set(GEN_PROTO_PY "${ONNX_ROOT}/onnx/gen_proto.py") |
| 314 |
set(GENERATED_FILES) |
| 315 |
foreach(INFILE ${ARGN}) |
| 316 |
set(ABS_FILE "${ONNX_ROOT}/${INFILE}") |
| 317 |
get_filename_component(FILE_DIR ${ABS_FILE} DIRECTORY) |
| 318 |
get_filename_component(FILE_WE ${INFILE} NAME_WE) |
| 319 |
# "onnx-data" check is because we do not want to create/compile an "onnx-data-ml.proto" file |
| 320 |
if(ONNX_ML AND NOT(FILE_WE STREQUAL "onnx-data")) |
| 321 |
if(ONNX_NAMESPACE STREQUAL "onnx") |
| 322 |
set(GENERATED_FILE_WE "${FILE_WE}-ml") |
| 323 |
else() |
| 324 |
set(GENERATED_FILE_WE "${FILE_WE}_${ONNX_NAMESPACE}-ml") |
| 325 |
endif() |
| 326 |
else() |
| 327 |
if(ONNX_NAMESPACE STREQUAL "onnx") |
| 328 |
set(GENERATED_FILE_WE "${FILE_WE}") |
| 329 |
else() |
| 330 |
set(GENERATED_FILE_WE "${FILE_WE}_${ONNX_NAMESPACE}") |
| 331 |
endif() |
| 332 |
endif() |
| 333 |
file(RELATIVE_PATH REL_DIR "${ONNX_ROOT}" "${FILE_DIR}") |
| 334 |
set(OUTPUT_PROTO_DIR "${CMAKE_CURRENT_BINARY_DIR}/${REL_DIR}") |
| 335 |
|
| 336 |
set(OUTPUT_PB_SRC "${OUTPUT_PROTO_DIR}/${GENERATED_FILE_WE}.pb.cc") |
| 337 |
set(GENERATED_PROTO "${OUTPUT_PROTO_DIR}/${GENERATED_FILE_WE}.proto") |
| 338 |
list(APPEND ${SRCS} "${OUTPUT_PB_SRC}") |
| 339 |
|
| 340 |
if(NOT EXISTS "${OUTPUT_PROTO_DIR}") |
| 341 |
file(MAKE_DIRECTORY "${OUTPUT_PROTO_DIR}") |
| 342 |
endif() |
| 343 |
|
| 344 |
set(GEN_PROTO_ARGS |
| 345 |
-p |
| 346 |
"${ONNX_NAMESPACE}" |
| 347 |
-o |
| 348 |
"${OUTPUT_PROTO_DIR}" |
| 349 |
"${FILE_WE}") |
| 350 |
if(ONNX_ML) |
| 351 |
list(APPEND GEN_PROTO_ARGS -m) |
| 352 |
endif() |
| 353 |
if(ONNX_USE_LITE_PROTO) |
| 354 |
list(APPEND GEN_PROTO_ARGS -l) |
| 355 |
endif() |
| 356 |
if(ONNX_VERIFY_PROTO3) |
| 357 |
if(NOT ONNX_PROTOC_EXECUTABLE) |
| 358 |
message(FATAL_ERROR "Protobuf compiler not found") |
| 359 |
endif() |
| 360 |
list(APPEND GEN_PROTO_ARGS --protoc_path) |
| 361 |
list(APPEND GEN_PROTO_ARGS "${ONNX_PROTOC_EXECUTABLE}") |
| 362 |
endif() |
| 363 |
|
| 364 |
# Use add_custom_command to avoid re-generate of PROTO files |
| 365 |
add_custom_command(OUTPUT "${GENERATED_PROTO}" |
| 366 |
COMMAND ${ONNX_PYTHON_INTERPRETER} "${GEN_PROTO_PY}" ${GEN_PROTO_ARGS} |
| 367 |
DEPENDS ${INFILE} |
| 368 |
COMMENT "Running gen_proto.py on ${INFILE}") |
| 369 |
message("Generated: ${GENERATED_PROTO}") |
| 370 |
|
| 371 |
set(PROTOC_ARGS |
| 372 |
${GENERATED_PROTO} |
| 373 |
-I |
| 374 |
${CMAKE_CURRENT_BINARY_DIR} |
| 375 |
--cpp_out |
| 376 |
${CMAKE_CURRENT_BINARY_DIR}) |
| 377 |
if(ONNX_BUILD_PYTHON) |
| 378 |
list(APPEND PROTOC_ARGS --python_out) |
| 379 |
if(ONNX_GEN_PB_TYPE_STUBS) |
| 380 |
list(APPEND PROTOC_ARGS pyi_out:${CMAKE_CURRENT_BINARY_DIR}) |
| 381 |
else() |
| 382 |
list(APPEND PROTOC_ARGS ${CMAKE_CURRENT_BINARY_DIR}) |
| 383 |
endif() |
| 384 |
endif() |
| 385 |
list(APPEND GENERATED_FILES "${GENERATED_PROTO}") |
| 386 |
add_custom_command(OUTPUT "${OUTPUT_PB_SRC}" |
| 387 |
COMMAND "${ONNX_PROTOC_EXECUTABLE}" ${PROTOC_ARGS} |
| 388 |
DEPENDS ${GENERATED_FILES} |
| 389 |
COMMENT "Running C++ protocol buffer compiler on ${GENERATED_PROTO}") |
| 390 |
endforeach() |
| 391 |
|
| 392 |
set(${SRCS} ${${SRCS}} PARENT_SCOPE) |
| 393 |
endfunction() |
| 394 |
|
| 395 |
relative_protobuf_generate_cpp(ONNX_PROTO_SRCS |
| 396 |
onnx/onnx.in.proto |
| 397 |
onnx/onnx-operators.in.proto |
| 398 |
onnx/onnx-data.in.proto) |
| 399 |
|
| 400 |
add_library(onnx_proto_object OBJECT ${ONNX_PROTO_SRCS}) |
| 401 |
|
| 402 |
file(GLOB_RECURSE __tmp_srcs "${ONNX_ROOT}/onnx/*.h" "${ONNX_ROOT}/onnx/*.cc") |
| 403 |
file(GLOB_RECURSE onnx_gtests_src "${ONNX_ROOT}/onnx/test/cpp/*.h" |
| 404 |
"${ONNX_ROOT}/onnx/test/cpp/*.cc" |
| 405 |
"${ONNX_ROOT}/onnx/backend/test/cpp/*.cc" |
| 406 |
"${ONNX_ROOT}/onnx/backend/test/cpp/*.h") |
| 407 |
list(REMOVE_ITEM __tmp_srcs "${ONNX_ROOT}/onnx/cpp2py_export.cc") |
| 408 |
list(REMOVE_ITEM __tmp_srcs ${onnx_gtests_src} "${ONNX_ROOT}/onnx/test/cmake/main.cc") |
| 409 |
list(APPEND ONNX_SRCS ${__tmp_srcs}) |
| 410 |
|
| 411 |
set(LINKED_PROTOBUF_TARGET protobuf::libprotobuf) |
| 412 |
if(ONNX_USE_LITE_PROTO) |
| 413 |
if(TARGET protobuf::libprotobuf-lite) |
| 414 |
set(LINKED_PROTOBUF_TARGET protobuf::libprotobuf-lite) |
| 415 |
endif() |
| 416 |
endif() |
| 417 |
add_onnx_global_defines(onnx_proto_object) |
| 418 |
target_include_directories(onnx_proto_object PUBLIC |
| 419 |
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>) |
| 420 |
if(MSVC) |
| 421 |
# For disabling Protobuf related warnings |
| 422 |
set(protobuf_warnings |
| 423 |
/wd4146 # unary minus operator applied to unsigned type, |
| 424 |
# result still unsigned |
| 425 |
/wd4244 # 'argument': conversion from 'google:: |
| 426 |
# protobuf::uint64' to 'int', possible |
| 427 |
# loss of data |
| 428 |
/wd4267 # Conversion from 'size_t' to 'int', |
| 429 |
# possible loss of data |
| 430 |
/wd4141 # 'inline': used more than once |
| 431 |
) |
| 432 |
endif() |
| 433 |
add_library(onnx_proto) |
| 434 |
target_link_libraries(onnx_proto PUBLIC $<BUILD_INTERFACE:onnx_proto_object>) |
| 435 |
if(ONNX_ML) |
| 436 |
target_compile_definitions(onnx_proto PUBLIC ONNX_ML=1) |
| 437 |
endif() |
| 438 |
target_compile_definitions(onnx_proto PUBLIC ONNX_NAMESPACE=${ONNX_NAMESPACE}) |
| 439 |
|
| 440 |
# onnx_object and onnx_proto_object are collections of C++ object files. |
| 441 |
# They are introduced to help onnx_cpp2py_export bypass library boundaries and use ONNX private symbols. |
| 442 |
add_library(onnx_object OBJECT ${ONNX_SRCS}) |
| 443 |
add_dependencies(onnx_object onnx_proto_object) |
| 444 |
target_include_directories(onnx_object PUBLIC |
| 445 |
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>) |
| 446 |
set_target_properties(onnx_object PROPERTIES CXX_VISIBILITY_PRESET hidden) |
| 447 |
set_target_properties(onnx_object PROPERTIES VISIBILITY_INLINES_HIDDEN ON) |
| 448 |
target_include_directories(onnx_object PUBLIC $<BUILD_INTERFACE:${ONNX_ROOT}>) |
| 449 |
add_onnx_global_defines(onnx_object) |
| 450 |
target_link_libraries(onnx_proto_object PUBLIC ${LINKED_PROTOBUF_TARGET}) |
| 451 |
target_link_libraries(onnx_object PUBLIC ${LINKED_PROTOBUF_TARGET}) |
| 452 |
foreach(ABSL_USED_TARGET IN LISTS protobuf_ABSL_USED_TARGETS) |
| 453 |
if(TARGET ${ABSL_USED_TARGET}) |
| 454 |
target_link_libraries(onnx_proto_object PUBLIC ${ABSL_USED_TARGET}) |
| 455 |
target_link_libraries(onnx_object PUBLIC ${ABSL_USED_TARGET}) |
| 456 |
endif() |
| 457 |
endforeach() |
| 458 |
add_library(onnx) |
| 459 |
target_link_libraries(onnx PUBLIC $<BUILD_INTERFACE:onnx_object> $<BUILD_INTERFACE:onnx_proto_object>) |
| 460 |
target_include_directories(onnx PUBLIC $<INSTALL_INTERFACE:include>) |
| 461 |
if(ONNX_ML) |
| 462 |
target_compile_definitions(onnx PUBLIC ONNX_ML=1) |
| 463 |
endif() |
| 464 |
target_compile_definitions(onnx PUBLIC ONNX_NAMESPACE=${ONNX_NAMESPACE}) |
| 465 |
|
| 466 |
target_compile_options(onnx_object PUBLIC ${protobuf_warnings}) |
| 467 |
target_compile_options(onnx_proto_object PUBLIC ${protobuf_warnings}) |
| 468 |
|
| 469 |
if(ONNX_BUILD_PYTHON) |
| 470 |
# find system nanobind |
| 471 |
find_package(nanobind) |
| 472 |
if(NOT nanobind_FOUND) |
| 473 |
include(FetchContent) |
| 474 |
|
| 475 |
FetchContent_Declare( |
| 476 |
nanobind |
| 477 |
GIT_REPOSITORY https://github.com/wjakob/nanobind.git |
| 478 |
GIT_TAG v2.8.0 |
| 479 |
GIT_SHALLOW TRUE |
| 480 |
) |
| 481 |
FetchContent_MakeAvailable(nanobind) |
| 482 |
endif() |
| 483 |
|
| 484 |
# Configure nanobind: https://nanobind.readthedocs.io/en/latest/api_cmake.html |
| 485 |
nanobind_add_module( |
| 486 |
onnx_cpp2py_export |
| 487 |
NB_STATIC NB_DOMAIN onnx STABLE_ABI FREE_THREADED LTO |
| 488 |
"${ONNX_ROOT}/onnx/cpp2py_export.cc") |
| 489 |
|
| 490 |
target_link_libraries(onnx_cpp2py_export PRIVATE $<BUILD_INTERFACE:onnx_object> $<BUILD_INTERFACE:onnx_proto_object>) |
| 491 |
# Prevent "undefined symbol: _ZNSt10filesystem7__cxx114path14_M_split_cmptsEv" |
| 492 |
# (std::filesystem::__cxx11::path::_M_split_cmpts()) on gcc 8 |
| 493 |
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0) |
| 494 |
target_link_libraries(onnx_cpp2py_export PRIVATE "-lstdc++fs") |
| 495 |
endif() |
| 496 |
endif() |
| 497 |
|
| 498 |
if(MSVC) |
| 499 |
add_msvc_runtime_flag(onnx_proto_object) |
| 500 |
add_msvc_runtime_flag(onnx_object) |
| 501 |
if(TARGET onnx_cpp2py_export) |
| 502 |
add_msvc_runtime_flag(onnx_cpp2py_export) |
| 503 |
endif() |
| 504 |
if(ONNX_WERROR) |
| 505 |
target_compile_options(onnx_object PRIVATE "/WX") |
| 506 |
endif() |
| 507 |
else() |
| 508 |
target_compile_options(onnx_object PRIVATE -Wall -Wextra) |
| 509 |
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13) |
| 510 |
target_compile_options(onnx_object PRIVATE "-Wno-stringop-overflow") |
| 511 |
endif() |
| 512 |
if(ONNX_WERROR) |
| 513 |
target_compile_options(onnx_object PRIVATE "-Werror") |
| 514 |
endif() |
| 515 |
endif() |
| 516 |
if(ONNX_USE_ASAN AND NOT MSVC) |
| 517 |
find_package(Sanitizer REQUIRED) |
| 518 |
if(TARGET Sanitizer::address) |
| 519 |
target_link_libraries(onnx PRIVATE Sanitizer::address) |
| 520 |
message(STATUS "Use ASAN") |
| 521 |
endif() |
| 522 |
if(TARGET Sanitizer::undefined) |
| 523 |
target_link_libraries(onnx PRIVATE Sanitizer::undefined) |
| 524 |
message(STATUS "Use UBSAN") |
| 525 |
endif() |
| 526 |
endif() |
| 527 |
|
| 528 |
install(DIRECTORY ${ONNX_ROOT}/onnx |
| 529 |
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
| 530 |
FILES_MATCHING |
| 531 |
PATTERN "*.h" |
| 532 |
PATTERN "backend/test/case" EXCLUDE |
| 533 |
PATTERN "backend/test/data" EXCLUDE) |
| 534 |
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/onnx |
| 535 |
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
| 536 |
FILES_MATCHING |
| 537 |
PATTERN "*.h") |
| 538 |
|
| 539 |
configure_file( |
| 540 |
${PROJECT_SOURCE_DIR}/cmake/ONNXConfigVersion.cmake.in |
| 541 |
${PROJECT_BINARY_DIR}/ONNXConfigVersion.cmake |
| 542 |
@ONLY) |
| 543 |
configure_file( |
| 544 |
${PROJECT_SOURCE_DIR}/cmake/ONNXConfig.cmake.in |
| 545 |
${PROJECT_BINARY_DIR}/ONNXConfig.cmake |
| 546 |
@ONLY) |
| 547 |
install(FILES |
| 548 |
${PROJECT_BINARY_DIR}/ONNXConfigVersion.cmake |
| 549 |
${PROJECT_BINARY_DIR}/ONNXConfig.cmake |
| 550 |
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ONNX |
| 551 |
COMPONENT dev) |
| 552 |
|
| 553 |
if(ONNX_USE_UNITY_BUILD) |
| 554 |
# If ONNX_USE_UNITY_BUILD is set to ON, set ONNX target to use Unity builds. |
| 555 |
# We set Unity build to use groups, it allows us to set some specific files to be compiled individually |
| 556 |
set_target_properties(onnx |
| 557 |
PROPERTIES |
| 558 |
UNITY_BUILD ON |
| 559 |
UNITY_BUILD_MODE GROUP |
| 560 |
) |
| 561 |
|
| 562 |
set(NEW_LIST __unity_src_files) |
| 563 |
list(APPEND __unity_src_files ${ONNX_SRCS}) |
| 564 |
# These files have an issue with template explicit specialization after instantiation: |
| 565 |
# We take them out of the unity group so that they are compiled individually. |
| 566 |
list(REMOVE_ITEM __unity_src_files "${ONNX_ROOT}/onnx/defs/schema.cc") |
| 567 |
list(REMOVE_ITEM __unity_src_files "${ONNX_ROOT}/onnx/defs/tensor_proto_util.cc") |
| 568 |
set_source_files_properties(${__unity_src_files} PROPERTIES UNITY_GROUP "Unity_Group" ) |
| 569 |
|
| 570 |
# With unity build object file could get big, need this switch in MSVC. |
| 571 |
if(MSVC) |
| 572 |
target_compile_options(onnx PRIVATE /bigobj) |
| 573 |
endif() |
| 574 |
# should be enabled for onnx_proto when protobuf can support Unity builds |
| 575 |
endif() |
| 576 |
|
| 577 |
if(ONNX_BUILD_TESTS) |
| 578 |
find_package(GTest) |
| 579 |
if(NOT GTest_FOUND) |
| 580 |
include(googletest) |
| 581 |
endif() |
| 582 |
endif() |
| 583 |
|
| 584 |
install(TARGETS |
| 585 |
onnx onnx_proto |
| 586 |
EXPORT ONNXTargets DESTINATION ${CMAKE_INSTALL_LIBDIR}) |
| 587 |
|
| 588 |
install(EXPORT ONNXTargets |
| 589 |
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ONNX" |
| 590 |
NAMESPACE ONNX:: |
| 591 |
) |
| 592 |
|
| 593 |
if(ONNX_BUILD_TESTS) |
| 594 |
include(${ONNX_ROOT}/cmake/unittest.cmake) |
| 595 |
endif() |
| 596 |
|
| 597 |
include(cmake/summary.cmake) |
| 598 |
onnx_print_configuration_summary() |