onnxruntime
367 строк · 14.7 Кб
1# work around Android NDK bug which doesn't include -O flag for Release configuration
2# https://github.com/android/ndk/issues/1740
3# TODO: remove this when the NDK version(s) we support get fixed
4if (CMAKE_SYSTEM_NAME STREQUAL "Android")
5# NB: attempting to match the effects of this fix: https://android-review.googlesource.com/c/platform/ndk/+/2168845
6string(APPEND CMAKE_C_FLAGS_RELEASE " -O3")
7string(APPEND CMAKE_CXX_FLAGS_RELEASE " -O3")
8string(APPEND CMAKE_ASM_FLAGS_RELEASE " -O3")
9endif()
10
11# Suggested by https://gitlab.kitware.com/cmake/cmake/-/issues/20132
12# MacCatalyst is not well supported in CMake
13# The error that can emerge without this flag can look like:
14# "clang : error : overriding '-mmacosx-version-min=11.0' option with '-target x86_64-apple-ios14.0-macabi' [-Werror,-Woverriding-t-option]"
15if (PLATFORM_NAME STREQUAL "macabi")
16add_compile_options(-Wno-overriding-t-option)
17add_link_options(-Wno-overriding-t-option)
18endif()
19
20# Enable space optimization for gcc/clang
21# Cannot use "-ffunction-sections -fdata-sections" if we enable bitcode (iOS)
22if (NOT MSVC AND NOT onnxruntime_ENABLE_BITCODE)
23string(APPEND CMAKE_CXX_FLAGS " -ffunction-sections -fdata-sections")
24string(APPEND CMAKE_C_FLAGS " -ffunction-sections -fdata-sections")
25endif()
26
27if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
28set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s ALLOW_UNIMPLEMENTED_SYSCALLS=1 -s DEFAULT_TO_CXX=1")
29
30# Enable LTO for release single-thread build
31if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
32# NOTES:
33# (1) LTO does not work for WebAssembly multi-thread. (segment fault in worker)
34# (2) "-flto=thin" does not work correctly for wasm-ld.
35# we don't set onnxruntime_ENABLE_LTO because it appends flag "-flto=thin"
36# instead, we manually set CMAKE_CXX_FLAGS "-flto"
37string(APPEND CMAKE_C_FLAGS " -flto")
38string(APPEND CMAKE_CXX_FLAGS " -flto")
39endif()
40
41if (onnxruntime_ENABLE_WEBASSEMBLY_DEBUG_INFO)
42# "-g3" generates DWARF format debug info.
43# NOTE: With debug info enabled, web assembly artifacts will be very huge (>1GB). So we offer an option to build without debug info.
44set(CMAKE_CXX_FLAGS_DEBUG "-g3")
45else()
46set(CMAKE_CXX_FLAGS_DEBUG "-g2")
47endif()
48
49if (onnxruntime_ENABLE_WEBASSEMBLY_SIMD)
50string(APPEND CMAKE_C_FLAGS " -msimd128")
51string(APPEND CMAKE_CXX_FLAGS " -msimd128")
52endif()
53
54if (onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_CATCHING)
55string(APPEND CMAKE_C_FLAGS " -s DISABLE_EXCEPTION_CATCHING=0")
56string(APPEND CMAKE_CXX_FLAGS " -s DISABLE_EXCEPTION_CATCHING=0")
57endif()
58
59# Build WebAssembly with multi-threads support.
60if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
61string(APPEND CMAKE_C_FLAGS " -pthread -Wno-pthreads-mem-growth")
62string(APPEND CMAKE_CXX_FLAGS " -pthread -Wno-pthreads-mem-growth")
63endif()
64endif()
65
66if (onnxruntime_EXTERNAL_TRANSFORMER_SRC_PATH)
67add_definitions(-DORT_TRAINING_EXTERNAL_GRAPH_TRANSFORMERS=1)
68endif()
69
70# ORT build with as much excluded as possible. Supports ORT flatbuffers models only.
71if (onnxruntime_MINIMAL_BUILD)
72add_compile_definitions(ORT_MINIMAL_BUILD)
73
74if (onnxruntime_EXTENDED_MINIMAL_BUILD)
75# enable EPs that compile kernels at runtime
76add_compile_definitions(ORT_EXTENDED_MINIMAL_BUILD)
77endif()
78
79if (onnxruntime_MINIMAL_BUILD_CUSTOM_OPS)
80add_compile_definitions(ORT_MINIMAL_BUILD_CUSTOM_OPS)
81endif()
82
83if (MSVC)
84# undocumented internal flag to allow analysis of a minimal build binary size
85if (ADD_DEBUG_INFO_TO_MINIMAL_BUILD)
86string(APPEND CMAKE_CXX_FLAGS " /Zi")
87string(APPEND CMAKE_C_FLAGS " /Zi")
88string(APPEND CMAKE_SHARED_LINKER_FLAGS " /debug")
89endif()
90else()
91if (CMAKE_HOST_SYSTEM MATCHES "Darwin")
92add_link_options(-Wl, -dead_strip)
93else()
94add_link_options(-Wl,--gc-sections)
95endif()
96
97if (ADD_DEBUG_INFO_TO_MINIMAL_BUILD)
98string(APPEND CMAKE_CXX_FLAGS " -g")
99string(APPEND CMAKE_C_FLAGS " -g")
100endif()
101endif()
102endif()
103
104# Enable stream for all the non-minimal build
105if (NOT onnxruntime_MINIMAL_BUILD)
106add_compile_definitions(ORT_ENABLE_STREAM)
107endif()
108
109if (onnxruntime_ENABLE_LTO)
110include(CheckIPOSupported)
111check_ipo_supported(RESULT ipo_enabled OUTPUT ipo_output)
112if (NOT ipo_enabled)
113message(WARNING "IPO is not supported by this compiler")
114set(onnxruntime_ENABLE_LTO OFF)
115else()
116set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
117endif()
118endif()
119
120if (onnxruntime_REDUCED_OPS_BUILD)
121add_compile_definitions(REDUCED_OPS_BUILD)
122endif()
123
124if (onnxruntime_DISABLE_EXTERNAL_INITIALIZERS)
125add_definitions(-DDISABLE_EXTERNAL_INITIALIZERS=1)
126endif()
127
128if (onnxruntime_DISABLE_RTTI)
129add_compile_definitions(ORT_NO_RTTI)
130if (MSVC)
131# Disable RTTI and turn usage of dynamic_cast and typeid into errors
132add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/GR->" "$<$<COMPILE_LANGUAGE:CXX>:/we4541>")
133else()
134add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>")
135if (onnxruntime_USE_WEBNN)
136# Avoid unboundTypeError for WebNN EP since unbound type names are illegal with RTTI disabled
137# in Embind API, relevant issue: https://github.com/emscripten-core/emscripten/issues/7001
138add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:-DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0>")
139endif()
140endif()
141else()
142#MSVC RTTI flag /GR is not added to CMAKE_CXX_FLAGS by default. But, anyway VC++2019 treats "/GR" default on.
143#So we don't need the following three lines. But it's better to make it more explicit.
144if (MSVC)
145add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/GR>")
146endif()
147endif()
148
149# If this is only enabled in an onnxruntime_ORT_MODEL_FORMAT_ONLY build we don't need ONNX changes
150# as we (currently) only pull in data_type_utils.cc/h which doesn't throw
151if (onnxruntime_DISABLE_EXCEPTIONS)
152if (NOT onnxruntime_MINIMAL_BUILD)
153message(FATAL_ERROR "onnxruntime_MINIMAL_BUILD required for onnxruntime_DISABLE_EXCEPTIONS")
154endif()
155
156if (onnxruntime_ENABLE_PYTHON)
157# pybind11 highly depends on C++ exceptions.
158message(FATAL_ERROR "onnxruntime_ENABLE_PYTHON must be disabled for onnxruntime_DISABLE_EXCEPTIONS")
159endif()
160add_compile_definitions("ORT_NO_EXCEPTIONS")
161add_compile_definitions("MLAS_NO_EXCEPTION")
162add_compile_definitions("ONNX_NO_EXCEPTIONS")
163add_compile_definitions("JSON_NOEXCEPTION") # https://json.nlohmann.me/api/macros/json_noexception/
164
165if (MSVC)
166string(REGEX REPLACE "/EHsc" "/EHs-c-" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
167# Eigen throw_std_bad_alloc calls 'new' instead of throwing which results in a nodiscard warning.
168# It also has unreachable code as there's no good way to avoid EIGEN_EXCEPTIONS being set in macros.h
169# TODO: see if we can limit the code this is disabled for.
170string(APPEND CMAKE_CXX_FLAGS " /wd4834 /wd4702")
171add_compile_definitions("_HAS_EXCEPTIONS=0")
172else()
173set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables")
174endif()
175endif()
176
177
178# Guarantee that the Eigen code that you are #including is licensed
179# under the MPL2 and possibly more permissive licenses (like BSD).
180add_definitions(-DEIGEN_MPL2_ONLY)
181if (MSVC)
182add_definitions(-DEIGEN_HAS_CONSTEXPR -DEIGEN_HAS_VARIADIC_TEMPLATES -DEIGEN_HAS_CXX11_MATH -DEIGEN_HAS_CXX11_ATOMIC
183-DEIGEN_STRONG_INLINE=inline)
184endif()
185
186if ( onnxruntime_DONT_VECTORIZE )
187add_definitions(-DEIGEN_DONT_VECTORIZE=1)
188endif()
189
190if (onnxruntime_CROSS_COMPILING)
191set(CMAKE_CROSSCOMPILING ON)
192check_cxx_compiler_flag(-Wno-error HAS_NOERROR)
193if (HAS_NOERROR)
194string(APPEND CMAKE_CXX_FLAGS " -Wno-error=attributes")
195string(APPEND CMAKE_C_FLAGS " -Wno-error=attributes")
196endif()
197endif()
198
199if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
200check_cxx_compiler_flag(-Wno-error HAS_NOERROR)
201if (HAS_NOERROR)
202string(APPEND CMAKE_CXX_FLAGS " -Wno-error=attributes")
203string(APPEND CMAKE_C_FLAGS " -Wno-error=attributes")
204endif()
205endif()
206
207# Mark symbols to be invisible, for macOS/iOS/visionOS target only
208# Due to many dependencies have different symbol visibility settings, set global compile flags here.
209if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS|visionOS")
210foreach(flags CMAKE_CXX_FLAGS CMAKE_OBJC_FLAGS CMAKE_OBJCXX_FLAGS)
211string(APPEND ${flags} " -fvisibility=hidden -fvisibility-inlines-hidden")
212endforeach()
213endif()
214
215
216macro(check_nvcc_compiler_flag _FLAG _RESULT)
217execute_process(COMMAND ${CUDAToolkit_BIN_DIR}/nvcc "${_FLAG}" RESULT_VARIABLE NVCC_OUT ERROR_VARIABLE NVCC_ERROR)
218message("NVCC_ERROR = ${NVCC_ERROR}")
219message("NVCC_OUT = ${NVCC_OUT}")
220if ("${NVCC_OUT}" MATCHES "0")
221set(${_RESULT} 1)
222else()
223set(${_RESULT} 0)
224endif()
225endmacro()
226
227#Set global compile flags for all the source code(including third_party code like protobuf)
228#This section must be before any add_subdirectory, otherwise build may fail because /MD,/MT mismatch
229if (MSVC)
230if (CMAKE_VS_PLATFORM_NAME)
231# Multi-platform generator
232set(onnxruntime_target_platform ${CMAKE_VS_PLATFORM_NAME})
233else()
234set(onnxruntime_target_platform ${CMAKE_SYSTEM_PROCESSOR})
235endif()
236if (onnxruntime_target_platform STREQUAL "ARM64")
237set(onnxruntime_target_platform "ARM64")
238enable_language(ASM_MARMASM)
239elseif (onnxruntime_target_platform STREQUAL "ARM64EC")
240enable_language(ASM_MARMASM)
241elseif (onnxruntime_target_platform STREQUAL "ARM" OR CMAKE_GENERATOR MATCHES "ARM")
242set(onnxruntime_target_platform "ARM")
243enable_language(ASM_MARMASM)
244elseif (onnxruntime_target_platform STREQUAL "x64" OR onnxruntime_target_platform STREQUAL "x86_64" OR onnxruntime_target_platform STREQUAL "AMD64" OR CMAKE_GENERATOR MATCHES "Win64")
245set(onnxruntime_target_platform "x64")
246enable_language(ASM_MASM)
247elseif (onnxruntime_target_platform STREQUAL "Win32" OR onnxruntime_target_platform STREQUAL "x86" OR onnxruntime_target_platform STREQUAL "i386" OR onnxruntime_target_platform STREQUAL "i686")
248set(onnxruntime_target_platform "x86")
249enable_language(ASM_MASM)
250message("Enabling SAFESEH for x86 build")
251set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /safeseh")
252else()
253message(FATAL_ERROR "Unknown CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
254endif()
255
256
257#Always enable exception handling, even for Windows ARM
258if (NOT onnxruntime_DISABLE_EXCEPTIONS)
259string(APPEND CMAKE_CXX_FLAGS " /EHsc")
260string(APPEND CMAKE_C_FLAGS " /EHsc")
261
262string(APPEND CMAKE_CXX_FLAGS " /wd26812")
263string(APPEND CMAKE_C_FLAGS " /wd26812")
264endif()
265
266if (onnxruntime_USE_AVX)
267string(APPEND CMAKE_CXX_FLAGS " /arch:AVX")
268string(APPEND CMAKE_C_FLAGS " /arch:AVX")
269elseif (onnxruntime_USE_AVX2)
270string(APPEND CMAKE_CXX_FLAGS " /arch:AVX2")
271string(APPEND CMAKE_C_FLAGS " /arch:AVX2")
272elseif (onnxruntime_USE_AVX512)
273string(APPEND CMAKE_CXX_FLAGS " /arch:AVX512")
274string(APPEND CMAKE_C_FLAGS " /arch:AVX512")
275endif()
276
277if (onnxruntime_ENABLE_LTO AND NOT onnxruntime_USE_CUDA)
278set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Gw /GL")
279set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /Gw /GL")
280set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /Gw /GL")
281endif()
282else()
283if (NOT APPLE)
284#XXX: Sometimes the value of CMAKE_SYSTEM_PROCESSOR is set but it's wrong. For example, if you run an armv7 docker
285#image on an aarch64 machine with an aarch64 Ubuntu host OS, in the docker instance cmake may still report
286# CMAKE_SYSTEM_PROCESSOR as aarch64 by default. Given compiling this code may need more than 2GB memory, we do not
287# support compiling for ARM32 natively(only support cross-compiling), we will ignore this issue for now.
288if(NOT CMAKE_SYSTEM_PROCESSOR)
289message(WARNING "CMAKE_SYSTEM_PROCESSOR is not set. Please set it in your toolchain cmake file.")
290# Try to detect it
291if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
292execute_process(
293COMMAND "${CMAKE_C_COMPILER}" -dumpmachine
294OUTPUT_VARIABLE GCC_DUMP_MACHINE_OUT OUTPUT_STRIP_TRAILING_WHITESPACE
295ERROR_VARIABLE _err
296RESULT_VARIABLE _res
297)
298if(NOT _res EQUAL 0)
299message(SEND_ERROR "Failed to run 'gcc -dumpmachine':\n ${_res}")
300endif()
301string(REPLACE "-" ";" GCC_DUMP_MACHINE_OUT_LIST "${GCC_DUMP_MACHINE_OUT}")
302list(LENGTH GCC_DUMP_MACHINE_OUT_LIST GCC_TRIPLET_LEN)
303if(GCC_TRIPLET_LEN EQUAL 4)
304list(GET GCC_DUMP_MACHINE_OUT_LIST 0 CMAKE_SYSTEM_PROCESSOR)
305message("Setting CMAKE_SYSTEM_PROCESSOR to ${CMAKE_SYSTEM_PROCESSOR}")
306endif()
307endif()
308endif()
309set(onnxruntime_target_platform ${CMAKE_SYSTEM_PROCESSOR})
310endif()
311if (onnxruntime_BUILD_FOR_NATIVE_MACHINE)
312string(APPEND CMAKE_CXX_FLAGS " -march=native -mtune=native")
313string(APPEND CMAKE_C_FLAGS " -march=native -mtune=native")
314elseif (onnxruntime_USE_AVX)
315string(APPEND CMAKE_CXX_FLAGS " -mavx")
316string(APPEND CMAKE_C_FLAGS " -mavx")
317elseif (onnxruntime_USE_AVX2)
318string(APPEND CMAKE_CXX_FLAGS " -mavx2")
319string(APPEND CMAKE_C_FLAGS " -mavx2")
320elseif (onnxruntime_USE_AVX512)
321string(APPEND CMAKE_CXX_FLAGS " -mavx512f -mavx512cd -mavx512bw -mavx512dq -mavx512vl")
322string(APPEND CMAKE_C_FLAGS " -mavx512f -mavx512cd -mavx512bw -mavx512dq -mavx512vl")
323endif()
324if (CMAKE_SYSTEM_NAME STREQUAL "Android" AND onnxruntime_GCOV_COVERAGE)
325string(APPEND CMAKE_CXX_FLAGS " -g -O0 --coverage ")
326string(APPEND CMAKE_C_FLAGS " -g -O0 --coverage ")
327endif()
328if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
329# suppress warnings from flatbuffers
330string(APPEND CMAKE_CXX_FLAGS " -Wno-restrict ")
331string(APPEND CMAKE_C_FLAGS " -Wno-restrict ")
332endif()
333# Check support for AVX and f16c.
334include(CheckCXXCompilerFlag)
335check_cxx_compiler_flag("-mf16c" COMPILER_SUPPORT_MF16C)
336if (NOT COMPILER_SUPPORT_MF16C)
337message("F16C instruction set is not supported.")
338endif()
339
340check_cxx_compiler_flag("-mfma" COMPILER_SUPPORT_FMA)
341if (NOT COMPILER_SUPPORT_FMA)
342message("FMA instruction set is not supported.")
343endif()
344
345check_cxx_compiler_flag("-mavx" COMPILER_SUPPORT_AVX)
346if (NOT COMPILER_SUPPORT_AVX)
347message("AVX instruction set is not supported.")
348endif()
349
350if (CMAKE_SYSTEM_NAME STREQUAL "Android" AND onnxruntime_ENABLE_TRAINING_APIS)
351message("F16C, FMA and AVX flags are not supported on Android for ort training.")
352endif()
353
354if (NOT (COMPILER_SUPPORT_MF16C AND COMPILER_SUPPORT_FMA AND COMPILER_SUPPORT_AVX) OR
355(CMAKE_SYSTEM_NAME STREQUAL "Android" AND onnxruntime_ENABLE_TRAINING_APIS))
356message("One or more AVX/F16C instruction flags are not supported. ")
357set(onnxruntime_ENABLE_CPU_FP16_OPS FALSE)
358endif()
359
360endif()
361
362if (WIN32)
363# required to be set explicitly to enable Eigen-Unsupported SpecialFunctions
364string(APPEND CMAKE_CXX_FLAGS " -DEIGEN_HAS_C99_MATH")
365elseif(LINUX)
366add_compile_definitions("_GNU_SOURCE")
367endif()
368