onnxruntime
1803 строки · 83.3 Кб
1# Copyright (c) Microsoft Corporation. All rights reserved.
2# Licensed under the MIT License.
3if (IOS)
4find_package(XCTest REQUIRED)
5endif()
6
7set(TEST_SRC_DIR ${ONNXRUNTIME_ROOT}/test)
8set(TEST_INC_DIR ${ONNXRUNTIME_ROOT})
9if (onnxruntime_ENABLE_TRAINING)
10list(APPEND TEST_INC_DIR ${ORTTRAINING_ROOT})
11endif()
12if (onnxruntime_USE_TVM)
13list(APPEND TEST_INC_DIR ${TVM_INCLUDES})
14endif()
15
16set(disabled_warnings)
17function(AddTest)
18cmake_parse_arguments(_UT "DYN" "TARGET" "LIBS;SOURCES;DEPENDS;TEST_ARGS" ${ARGN})
19list(REMOVE_DUPLICATES _UT_SOURCES)
20
21if (IOS)
22onnxruntime_add_executable(${_UT_TARGET} ${TEST_SRC_DIR}/xctest/orttestmain.m)
23else()
24onnxruntime_add_executable(${_UT_TARGET} ${_UT_SOURCES})
25endif()
26if (_UT_DEPENDS)
27list(REMOVE_DUPLICATES _UT_DEPENDS)
28endif(_UT_DEPENDS)
29
30if(_UT_LIBS)
31list(REMOVE_DUPLICATES _UT_LIBS)
32endif()
33
34source_group(TREE ${REPO_ROOT} FILES ${_UT_SOURCES})
35
36if (MSVC AND NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
37#TODO: fix the warnings, they are dangerous
38target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd4244>"
39"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd4244>")
40endif()
41if (MSVC)
42target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd6330>"
43"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd6330>")
44#Abseil has a lot of C4127/C4324 warnings.
45target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd4127>"
46"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd4127>")
47target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd4324>"
48"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd4324>")
49endif()
50
51set_target_properties(${_UT_TARGET} PROPERTIES FOLDER "ONNXRuntimeTest")
52
53if (MSVC)
54# set VS debugger working directory to the test program's directory
55set_target_properties(${_UT_TARGET} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY $<TARGET_FILE_DIR:${_UT_TARGET}>)
56endif()
57
58if (_UT_DEPENDS)
59add_dependencies(${_UT_TARGET} ${_UT_DEPENDS})
60endif(_UT_DEPENDS)
61
62if(_UT_DYN)
63target_link_libraries(${_UT_TARGET} PRIVATE ${_UT_LIBS} GTest::gtest GTest::gmock onnxruntime ${CMAKE_DL_LIBS}
64Threads::Threads)
65target_compile_definitions(${_UT_TARGET} PRIVATE -DUSE_ONNXRUNTIME_DLL)
66else()
67if(onnxruntime_USE_CUDA)
68#XXX: we should not need to do this. onnxruntime_test_all.exe should not have direct dependency on CUDA DLLs,
69# otherwise it will impact when CUDA DLLs can be unloaded.
70target_link_libraries(${_UT_TARGET} PRIVATE CUDA::cudart cudnn_frontend)
71endif()
72target_link_libraries(${_UT_TARGET} PRIVATE ${_UT_LIBS} GTest::gtest GTest::gmock ${onnxruntime_EXTERNAL_LIBRARIES})
73endif()
74
75onnxruntime_add_include_to_target(${_UT_TARGET} date::date flatbuffers::flatbuffers)
76target_include_directories(${_UT_TARGET} PRIVATE ${TEST_INC_DIR})
77if (onnxruntime_USE_CUDA)
78target_include_directories(${_UT_TARGET} PRIVATE ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES} ${CUDNN_INCLUDE_DIR})
79if (onnxruntime_USE_NCCL)
80target_include_directories(${_UT_TARGET} PRIVATE ${NCCL_INCLUDE_DIRS})
81endif()
82if(onnxruntime_CUDA_MINIMAL)
83target_compile_definitions(${_UT_TARGET} PRIVATE -DUSE_CUDA_MINIMAL)
84endif()
85endif()
86if (onnxruntime_USE_TENSORRT)
87# used for instantiating placeholder TRT builder to mitigate TRT library load/unload overhead
88target_include_directories(${_UT_TARGET} PRIVATE ${TENSORRT_INCLUDE_DIR})
89endif()
90
91if(MSVC)
92target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>"
93"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
94endif()
95
96if (WIN32)
97# include dbghelp in case tests throw an ORT exception, as that exception includes a stacktrace, which requires dbghelp.
98target_link_libraries(${_UT_TARGET} PRIVATE debug dbghelp)
99
100if (MSVC)
101# warning C6326: Potential comparison of a constant with another constant.
102# Lot of such things came from gtest
103target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd6326>"
104"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd6326>")
105# Raw new and delete. A lot of such things came from googletest.
106target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26409>"
107"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26409>")
108# "Global initializer calls a non-constexpr function."
109target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26426>"
110"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26426>")
111endif()
112target_compile_options(${_UT_TARGET} PRIVATE ${disabled_warnings})
113else()
114target_compile_options(${_UT_TARGET} PRIVATE ${DISABLED_WARNINGS_FOR_TVM})
115target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options -Wno-error=sign-compare>"
116"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:-Wno-error=sign-compare>")
117if (${HAS_NOERROR})
118target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:-Wno-error=uninitialized>")
119endif()
120endif()
121
122set(TEST_ARGS ${_UT_TEST_ARGS})
123if (onnxruntime_GENERATE_TEST_REPORTS)
124# generate a report file next to the test program
125if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
126# WebAssembly use a memory file system, so we do not use full path
127list(APPEND TEST_ARGS
128"--gtest_output=xml:$<TARGET_FILE_NAME:${_UT_TARGET}>.$<CONFIG>.results.xml")
129else()
130list(APPEND TEST_ARGS
131"--gtest_output=xml:$<SHELL_PATH:$<TARGET_FILE:${_UT_TARGET}>.$<CONFIG>.results.xml>")
132endif()
133endif(onnxruntime_GENERATE_TEST_REPORTS)
134
135if (IOS)
136# target_sources(${_UT_TARGET} PRIVATE ${TEST_SRC_DIR}/xctest/orttestmain.m)
137set_target_properties(${_UT_TARGET} PROPERTIES FOLDER "ONNXRuntimeTest"
138MACOSX_BUNDLE_BUNDLE_NAME ${_UT_TARGET}
139MACOSX_BUNDLE_GUI_IDENTIFIER com.onnxruntime.utest.${_UT_TARGET}
140MACOSX_BUNDLE_LONG_VERSION_STRING ${ORT_VERSION}
141MACOSX_BUNDLE_BUNDLE_VERSION ${ORT_VERSION}
142MACOSX_BUNDLE_SHORT_VERSION_STRING ${ORT_VERSION}
143XCODE_ATTRIBUTE_CLANG_ENABLE_MODULES "YES"
144XCODE_ATTRIBUTE_ENABLE_BITCODE "NO"
145XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO")
146
147xctest_add_bundle(${_UT_TARGET}_xc ${_UT_TARGET}
148${TEST_SRC_DIR}/xctest/ortxctest.m
149${TEST_SRC_DIR}/xctest/xcgtest.mm
150${_UT_SOURCES})
151onnxruntime_configure_target(${_UT_TARGET}_xc)
152if(_UT_DYN)
153target_link_libraries(${_UT_TARGET}_xc PRIVATE ${_UT_LIBS} GTest::gtest GTest::gmock onnxruntime ${CMAKE_DL_LIBS}
154Threads::Threads)
155target_compile_definitions(${_UT_TARGET}_xc PRIVATE USE_ONNXRUNTIME_DLL)
156else()
157target_link_libraries(${_UT_TARGET}_xc PRIVATE ${_UT_LIBS} GTest::gtest GTest::gmock ${onnxruntime_EXTERNAL_LIBRARIES})
158endif()
159onnxruntime_add_include_to_target(${_UT_TARGET}_xc date::date flatbuffers::flatbuffers)
160target_include_directories(${_UT_TARGET}_xc PRIVATE ${TEST_INC_DIR})
161get_target_property(${_UT_TARGET}_DEFS ${_UT_TARGET} COMPILE_DEFINITIONS)
162target_compile_definitions(${_UT_TARGET}_xc PRIVATE ${_UT_TARGET}_DEFS)
163
164set_target_properties(${_UT_TARGET}_xc PROPERTIES FOLDER "ONNXRuntimeXCTest"
165MACOSX_BUNDLE_BUNDLE_NAME ${_UT_TARGET}_xc
166MACOSX_BUNDLE_GUI_IDENTIFIER com.onnxruntime.utest.${_UT_TARGET}
167MACOSX_BUNDLE_LONG_VERSION_STRING ${ORT_VERSION}
168MACOSX_BUNDLE_BUNDLE_VERSION ${ORT_VERSION}
169MACOSX_BUNDLE_SHORT_VERSION_STRING ${ORT_VERSION}
170XCODE_ATTRIBUTE_ENABLE_BITCODE "NO")
171
172xctest_add_test(xctest.${_UT_TARGET} ${_UT_TARGET}_xc)
173else()
174if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
175# We might have already executed the following "find_program" code when we build ORT nodejs binding.
176# Then the program is found the result is stored in the variable and the search will not be repeated.
177find_program(NPM_CLI
178NAMES "npm.cmd" "npm"
179DOC "NPM command line client"
180REQUIRED
181)
182
183if (onnxruntime_WEBASSEMBLY_RUN_TESTS_IN_BROWSER)
184add_custom_command(TARGET ${_UT_TARGET} POST_BUILD
185COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TEST_SRC_DIR}/wasm/package.json $<TARGET_FILE_DIR:${_UT_TARGET}>
186COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TEST_SRC_DIR}/wasm/package-lock.json $<TARGET_FILE_DIR:${_UT_TARGET}>
187COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TEST_SRC_DIR}/wasm/karma.conf.js $<TARGET_FILE_DIR:${_UT_TARGET}>
188COMMAND ${NPM_CLI} ci
189WORKING_DIRECTORY $<TARGET_FILE_DIR:${_UT_TARGET}>
190)
191
192set(TEST_NPM_FLAGS)
193if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
194list(APPEND TEST_NPM_FLAGS "--wasm-threads")
195endif()
196add_test(NAME ${_UT_TARGET}
197COMMAND ${NPM_CLI} test -- ${TEST_NPM_FLAGS} --entry=${_UT_TARGET} ${TEST_ARGS}
198WORKING_DIRECTORY $<TARGET_FILE_DIR:${_UT_TARGET}>
199)
200else()
201set(TEST_NODE_FLAGS)
202if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
203list(APPEND TEST_NODE_FLAGS "--experimental-wasm-threads")
204endif()
205if (onnxruntime_ENABLE_WEBASSEMBLY_SIMD)
206list(APPEND TEST_NODE_FLAGS "--experimental-wasm-simd")
207endif()
208
209# prefer Node from emsdk so the version is more deterministic
210if (DEFINED ENV{EMSDK_NODE})
211set(NODE_EXECUTABLE $ENV{EMSDK_NODE})
212else()
213# warning as we don't know what node version is being used and whether things like the TEST_NODE_FLAGS
214# will be valid. e.g. "--experimental-wasm-simd" is not valid with node v20 or later.
215message(WARNING "EMSDK_NODE environment variable was not set. Falling back to system `node`.")
216set(NODE_EXECUTABLE node)
217endif()
218
219add_test(NAME ${_UT_TARGET}
220COMMAND ${NODE_EXECUTABLE} ${TEST_NODE_FLAGS} ${_UT_TARGET}.js ${TEST_ARGS}
221WORKING_DIRECTORY $<TARGET_FILE_DIR:${_UT_TARGET}>
222)
223endif()
224# Set test timeout to 3 hours.
225set_tests_properties(${_UT_TARGET} PROPERTIES TIMEOUT 7200)
226else()
227add_test(NAME ${_UT_TARGET}
228COMMAND ${_UT_TARGET} ${TEST_ARGS}
229WORKING_DIRECTORY $<TARGET_FILE_DIR:${_UT_TARGET}>
230)
231# Set test timeout to 3 hours.
232set_tests_properties(${_UT_TARGET} PROPERTIES TIMEOUT 7200)
233endif()
234endif()
235endfunction(AddTest)
236
237# general program entrypoint for C++ unit tests
238set(onnxruntime_unittest_main_src "${TEST_SRC_DIR}/unittest_main/test_main.cc")
239
240#Do not add '${TEST_SRC_DIR}/util/include' to your include directories directly
241#Use onnxruntime_add_include_to_target or target_link_libraries, so that compile definitions
242#can propagate correctly.
243
244file(GLOB onnxruntime_test_utils_src CONFIGURE_DEPENDS
245"${TEST_SRC_DIR}/util/include/*.h"
246"${TEST_SRC_DIR}/util/*.cc"
247)
248
249file(GLOB onnxruntime_test_common_src CONFIGURE_DEPENDS
250"${TEST_SRC_DIR}/common/*.cc"
251"${TEST_SRC_DIR}/common/*.h"
252"${TEST_SRC_DIR}/common/logging/*.cc"
253"${TEST_SRC_DIR}/common/logging/*.h"
254)
255
256file(GLOB onnxruntime_test_quantization_src CONFIGURE_DEPENDS
257"${TEST_SRC_DIR}/quantization/*.cc"
258"${TEST_SRC_DIR}/quantization/*.h"
259)
260
261file(GLOB onnxruntime_test_flatbuffers_src CONFIGURE_DEPENDS
262"${TEST_SRC_DIR}/flatbuffers/*.cc"
263"${TEST_SRC_DIR}/flatbuffers/*.h"
264)
265
266if(NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_REDUCED_OPS_BUILD)
267
268file(GLOB onnxruntime_test_ir_src CONFIGURE_DEPENDS
269"${TEST_SRC_DIR}/ir/*.cc"
270"${TEST_SRC_DIR}/ir/*.h"
271)
272
273file(GLOB onnxruntime_test_optimizer_src CONFIGURE_DEPENDS
274"${TEST_SRC_DIR}/optimizer/*.cc"
275"${TEST_SRC_DIR}/optimizer/*.h"
276)
277
278set(onnxruntime_test_framework_src_patterns
279"${TEST_SRC_DIR}/framework/*.cc"
280"${TEST_SRC_DIR}/framework/*.h"
281"${TEST_SRC_DIR}/platform/*.cc"
282)
283
284else() # minimal and/or reduced ops build
285
286set(onnxruntime_test_framework_src_patterns
287"${TEST_SRC_DIR}/platform/*.cc"
288)
289
290if (onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_REDUCED_OPS_BUILD)
291list(APPEND onnxruntime_test_framework_src_patterns
292"${TEST_SRC_DIR}/framework/ort_model_only_test.cc"
293)
294endif()
295
296if (NOT onnxruntime_MINIMAL_BUILD)
297file(GLOB onnxruntime_test_ir_src CONFIGURE_DEPENDS
298"${TEST_SRC_DIR}/ir/*.cc"
299"${TEST_SRC_DIR}/ir/*.h"
300)
301endif()
302endif()
303
304if((NOT onnxruntime_MINIMAL_BUILD OR onnxruntime_EXTENDED_MINIMAL_BUILD)
305AND NOT onnxruntime_REDUCED_OPS_BUILD)
306list(APPEND onnxruntime_test_optimizer_src
307"${TEST_SRC_DIR}/optimizer/runtime_optimization/graph_runtime_optimization_test.cc")
308endif()
309
310file(GLOB onnxruntime_test_training_src
311"${ORTTRAINING_SOURCE_DIR}/test/model/*.h"
312"${ORTTRAINING_SOURCE_DIR}/test/model/*.cc"
313"${ORTTRAINING_SOURCE_DIR}/test/gradient/*.h"
314"${ORTTRAINING_SOURCE_DIR}/test/gradient/*.cc"
315"${ORTTRAINING_SOURCE_DIR}/test/graph/*.h"
316"${ORTTRAINING_SOURCE_DIR}/test/graph/*.cc"
317"${ORTTRAINING_SOURCE_DIR}/test/session/*.h"
318"${ORTTRAINING_SOURCE_DIR}/test/session/*.cc"
319"${ORTTRAINING_SOURCE_DIR}/test/optimizer/*.h"
320"${ORTTRAINING_SOURCE_DIR}/test/optimizer/*.cc"
321"${ORTTRAINING_SOURCE_DIR}/test/framework/*.cc"
322"${ORTTRAINING_SOURCE_DIR}/test/distributed/*.h"
323"${ORTTRAINING_SOURCE_DIR}/test/distributed/*.cc"
324)
325
326# TODO (baijumeswani): Remove the minimal build check here.
327# The training api tests should be runnable even on a minimal build.
328# This requires converting all the *.onnx files to ort format.
329if (NOT onnxruntime_MINIMAL_BUILD)
330if (onnxruntime_ENABLE_TRAINING_APIS)
331file(GLOB onnxruntime_test_training_api_src
332"${ORTTRAINING_SOURCE_DIR}/test/training_api/common/*.cc"
333"${ORTTRAINING_SOURCE_DIR}/test/training_api/common/*.h"
334"${ORTTRAINING_SOURCE_DIR}/test/training_api/core/*.cc"
335"${ORTTRAINING_SOURCE_DIR}/test/training_api/core/*.h"
336)
337endif()
338endif()
339
340if(WIN32)
341list(APPEND onnxruntime_test_framework_src_patterns
342"${TEST_SRC_DIR}/platform/windows/*.cc"
343"${TEST_SRC_DIR}/platform/windows/logging/*.cc" )
344endif()
345
346if(NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_REDUCED_OPS_BUILD)
347
348if(onnxruntime_USE_CUDA)
349list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/framework/cuda/*)
350endif()
351
352set(onnxruntime_test_providers_src_patterns
353"${TEST_SRC_DIR}/providers/*.h"
354"${TEST_SRC_DIR}/providers/*.cc"
355"${TEST_SRC_DIR}/opaque_api/test_opaque_api.cc"
356"${TEST_SRC_DIR}/framework/TestAllocatorManager.cc"
357"${TEST_SRC_DIR}/framework/TestAllocatorManager.h"
358"${TEST_SRC_DIR}/framework/test_utils.cc"
359"${TEST_SRC_DIR}/framework/test_utils.h"
360)
361
362if(NOT onnxruntime_DISABLE_CONTRIB_OPS)
363list(APPEND onnxruntime_test_providers_src_patterns
364"${TEST_SRC_DIR}/contrib_ops/*.h"
365"${TEST_SRC_DIR}/contrib_ops/*.cc"
366"${TEST_SRC_DIR}/contrib_ops/math/*.h"
367"${TEST_SRC_DIR}/contrib_ops/math/*.cc")
368endif()
369
370else()
371set(onnxruntime_test_providers_src_patterns
372"${TEST_SRC_DIR}/framework/test_utils.cc"
373"${TEST_SRC_DIR}/framework/test_utils.h"
374# TODO: Add anything that is needed for testing a minimal build
375)
376endif()
377
378file(GLOB onnxruntime_test_providers_src CONFIGURE_DEPENDS ${onnxruntime_test_providers_src_patterns})
379
380if(NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_REDUCED_OPS_BUILD)
381file(GLOB_RECURSE onnxruntime_test_providers_cpu_src CONFIGURE_DEPENDS
382"${TEST_SRC_DIR}/providers/cpu/*"
383)
384endif()
385
386if(onnxruntime_DISABLE_ML_OPS)
387list(FILTER onnxruntime_test_providers_cpu_src EXCLUDE REGEX ".*/ml/.*")
388endif()
389
390list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_cpu_src})
391
392if (onnxruntime_USE_CUDA AND NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_REDUCED_OPS_BUILD)
393file(GLOB onnxruntime_test_providers_cuda_src CONFIGURE_DEPENDS
394"${TEST_SRC_DIR}/providers/cuda/*"
395)
396list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_cuda_src})
397
398if (onnxruntime_USE_CUDA_NHWC_OPS AND CUDNN_MAJOR_VERSION GREATER 8)
399file(GLOB onnxruntime_test_providers_cuda_nhwc_src CONFIGURE_DEPENDS
400"${TEST_SRC_DIR}/providers/cuda/nhwc/*.cc"
401)
402list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_cuda_nhwc_src})
403endif()
404endif()
405
406if (onnxruntime_USE_CANN)
407file(GLOB_RECURSE onnxruntime_test_providers_cann_src CONFIGURE_DEPENDS
408"${TEST_SRC_DIR}/providers/cann/*"
409)
410list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_cann_src})
411endif()
412
413# Disable training ops test for minimal build as a lot of these depend on loading an onnx model.
414if (NOT onnxruntime_MINIMAL_BUILD)
415if (onnxruntime_ENABLE_TRAINING_OPS)
416file(GLOB_RECURSE orttraining_test_trainingops_cpu_src CONFIGURE_DEPENDS
417"${ORTTRAINING_SOURCE_DIR}/test/training_ops/compare_provider_test_utils.cc"
418"${ORTTRAINING_SOURCE_DIR}/test/training_ops/function_op_test_utils.cc"
419"${ORTTRAINING_SOURCE_DIR}/test/training_ops/cpu/*"
420)
421
422if (NOT onnxruntime_ENABLE_TRAINING)
423list(REMOVE_ITEM orttraining_test_trainingops_cpu_src
424"${ORTTRAINING_SOURCE_DIR}/test/training_ops/cpu/tensorboard/summary_op_test.cc"
425)
426endif()
427
428list(APPEND onnxruntime_test_providers_src ${orttraining_test_trainingops_cpu_src})
429
430if (onnxruntime_USE_CUDA OR onnxruntime_USE_ROCM)
431file(GLOB_RECURSE orttraining_test_trainingops_cuda_src CONFIGURE_DEPENDS
432"${ORTTRAINING_SOURCE_DIR}/test/training_ops/cuda/*"
433)
434list(APPEND onnxruntime_test_providers_src ${orttraining_test_trainingops_cuda_src})
435endif()
436endif()
437endif()
438
439if (onnxruntime_USE_DNNL)
440file(GLOB_RECURSE onnxruntime_test_providers_dnnl_src CONFIGURE_DEPENDS
441"${TEST_SRC_DIR}/providers/dnnl/*"
442)
443list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_dnnl_src})
444endif()
445
446if (onnxruntime_USE_NNAPI_BUILTIN)
447file(GLOB_RECURSE onnxruntime_test_providers_nnapi_src CONFIGURE_DEPENDS
448"${TEST_SRC_DIR}/providers/nnapi/*"
449)
450list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_nnapi_src})
451endif()
452
453if (onnxruntime_USE_RKNPU)
454file(GLOB_RECURSE onnxruntime_test_providers_rknpu_src CONFIGURE_DEPENDS
455"${TEST_SRC_DIR}/providers/rknpu/*"
456)
457list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_rknpu_src})
458endif()
459
460if (NOT onnxruntime_MINIMAL_BUILD OR onnxruntime_EXTENDED_MINIMAL_BUILD)
461file(GLOB_RECURSE onnxruntime_test_providers_internal_testing_src CONFIGURE_DEPENDS
462"${TEST_SRC_DIR}/providers/internal_testing/*"
463)
464list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_internal_testing_src})
465endif()
466
467set (ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR "${TEST_SRC_DIR}/shared_lib")
468set (ONNXRUNTIME_GLOBAL_THREAD_POOLS_TEST_SRC_DIR "${TEST_SRC_DIR}/global_thread_pools")
469set (ONNXRUNTIME_CUSTOM_OP_REGISTRATION_TEST_SRC_DIR "${TEST_SRC_DIR}/custom_op_registration")
470set (ONNXRUNTIME_LOGGING_APIS_TEST_SRC_DIR "${TEST_SRC_DIR}/logging_apis")
471
472set (onnxruntime_shared_lib_test_SRC
473${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_fixture.h
474${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_session_options.cc
475${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_run_options.cc
476${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_allocator.cc
477${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_nontensor_types.cc
478${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_model_loading.cc
479${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_ort_format_models.cc
480${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/utils.h
481${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/utils.cc
482${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/custom_op_utils.h
483${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/custom_op_utils.cc)
484
485if (NOT onnxruntime_MINIMAL_BUILD)
486list(APPEND onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_inference.cc)
487endif()
488
489if(onnxruntime_RUN_ONNX_TESTS)
490list(APPEND onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_io_types.cc)
491endif()
492
493set (onnxruntime_global_thread_pools_test_SRC
494${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_fixture.h
495${ONNXRUNTIME_GLOBAL_THREAD_POOLS_TEST_SRC_DIR}/test_main.cc
496${ONNXRUNTIME_GLOBAL_THREAD_POOLS_TEST_SRC_DIR}/test_inference.cc)
497
498# tests from lowest level library up.
499# the order of libraries should be maintained, with higher libraries being added first in the list
500
501set(onnxruntime_test_common_libs
502onnxruntime_test_utils
503onnxruntime_common
504)
505
506set(onnxruntime_test_ir_libs
507onnxruntime_test_utils
508onnxruntime_graph
509onnxruntime_common
510)
511
512set(onnxruntime_test_optimizer_libs
513onnxruntime_test_utils
514onnxruntime_framework
515onnxruntime_util
516onnxruntime_graph
517onnxruntime_common
518)
519
520set(onnxruntime_test_framework_libs
521onnxruntime_test_utils
522onnxruntime_framework
523onnxruntime_util
524onnxruntime_graph
525${ONNXRUNTIME_MLAS_LIBS}
526onnxruntime_common
527)
528
529set(onnxruntime_test_server_libs
530onnxruntime_test_utils
531onnxruntime_test_utils_for_server
532)
533
534if(WIN32)
535list(APPEND onnxruntime_test_framework_libs Advapi32)
536endif()
537
538set (onnxruntime_test_providers_dependencies ${onnxruntime_EXTERNAL_DEPENDENCIES})
539
540if(onnxruntime_USE_CUDA)
541list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_cuda)
542endif()
543
544if(onnxruntime_USE_CANN)
545list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_cann)
546endif()
547
548if(onnxruntime_USE_NNAPI_BUILTIN)
549list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_nnapi)
550endif()
551
552if(onnxruntime_USE_VSINPU)
553list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_vsinpu)
554endif()
555
556if(onnxruntime_USE_JSEP)
557list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_js)
558endif()
559
560if(onnxruntime_USE_RKNPU)
561list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_rknpu)
562endif()
563
564if(onnxruntime_USE_DML)
565list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_dml)
566endif()
567
568if(onnxruntime_USE_DNNL)
569list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_dnnl)
570endif()
571
572if(onnxruntime_USE_MIGRAPHX)
573list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_migraphx)
574list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_migraphx onnxruntime_providers_shared)
575endif()
576
577if(onnxruntime_USE_ROCM)
578list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_rocm)
579endif()
580
581if(onnxruntime_USE_COREML)
582list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_coreml coreml_proto)
583endif()
584
585if(onnxruntime_USE_ACL)
586list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_acl)
587endif()
588
589if(onnxruntime_USE_ARMNN)
590list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_armnn)
591endif()
592
593set(ONNXRUNTIME_TEST_LIBS
594onnxruntime_session
595${ONNXRUNTIME_INTEROP_TEST_LIBS}
596${onnxruntime_libs}
597# CUDA, ROCM, TENSORRT, MIGRAPHX, DNNL, and OpenVINO are dynamically loaded at runtime
598${PROVIDERS_NNAPI}
599${PROVIDERS_VSINPU}
600${PROVIDERS_JS}
601${PROVIDERS_QNN}
602${PROVIDERS_SNPE}
603${PROVIDERS_RKNPU}
604${PROVIDERS_DML}
605${PROVIDERS_ACL}
606${PROVIDERS_ARMNN}
607${PROVIDERS_COREML}
608# ${PROVIDERS_TVM}
609${PROVIDERS_XNNPACK}
610${PROVIDERS_AZURE}
611onnxruntime_optimizer
612onnxruntime_providers
613onnxruntime_util
614${onnxruntime_tvm_libs}
615onnxruntime_framework
616onnxruntime_util
617onnxruntime_graph
618${ONNXRUNTIME_MLAS_LIBS}
619onnxruntime_common
620onnxruntime_flatbuffers
621)
622
623if (onnxruntime_ENABLE_TRAINING)
624set(ONNXRUNTIME_TEST_LIBS onnxruntime_training_runner onnxruntime_training ${ONNXRUNTIME_TEST_LIBS})
625endif()
626
627set(onnxruntime_test_providers_libs
628onnxruntime_test_utils
629${ONNXRUNTIME_TEST_LIBS}
630)
631
632if(onnxruntime_USE_TENSORRT)
633list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/tensorrt/*)
634list(APPEND onnxruntime_test_framework_src_patterns "${ONNXRUNTIME_ROOT}/core/providers/tensorrt/tensorrt_execution_provider_utils.h")
635list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_tensorrt)
636list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_tensorrt onnxruntime_providers_shared)
637list(APPEND onnxruntime_test_providers_libs ${TENSORRT_LIBRARY_INFER})
638endif()
639
640if(onnxruntime_USE_MIGRAPHX)
641list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/migraphx/*)
642list(APPEND onnxruntime_test_framework_src_patterns "${ONNXRUNTIME_ROOT}/core/providers/migraphx/migraphx_execution_provider_utils.h")
643list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_migraphx)
644list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_migraphx onnxruntime_providers_shared)
645endif()
646
647if(onnxruntime_USE_NNAPI_BUILTIN)
648list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/nnapi/*)
649list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_nnapi)
650list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_nnapi)
651list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_nnapi)
652endif()
653
654if(onnxruntime_USE_JSEP)
655list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/js/*)
656list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_js)
657list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_js)
658list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_js)
659endif()
660
661# QNN EP tests require CPU EP op implementations for accuracy evaluation, so disable on minimal
662# or reduced op builds.
663if(onnxruntime_USE_QNN AND NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_REDUCED_OPS_BUILD)
664list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/qnn/*)
665list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_qnn)
666list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_qnn)
667list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_qnn)
668endif()
669
670if(onnxruntime_USE_SNPE)
671list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/snpe/*)
672list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_snpe)
673list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_snpe)
674list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_snpe)
675endif()
676
677if(onnxruntime_USE_RKNPU)
678list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/rknpu/*)
679list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_rknpu)
680list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_rknpu)
681list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_rknpu)
682endif()
683
684if(onnxruntime_USE_COREML)
685list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/coreml/*.cc)
686if(APPLE)
687list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/coreml/*.mm)
688endif()
689list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_coreml coreml_proto)
690list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_coreml coreml_proto)
691list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_coreml coreml_proto)
692endif()
693
694if(onnxruntime_USE_XNNPACK)
695list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/xnnpack/*)
696list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_xnnpack)
697list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_xnnpack)
698list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_xnnpack)
699endif()
700
701if(onnxruntime_USE_AZURE)
702list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/azure/*)
703list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_azure)
704list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_azure)
705list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_azure)
706endif()
707
708if(WIN32)
709if (onnxruntime_USE_TVM)
710list(APPEND disabled_warnings ${DISABLED_WARNINGS_FOR_TVM})
711endif()
712endif()
713
714file(GLOB onnxruntime_test_framework_src CONFIGURE_DEPENDS
715${onnxruntime_test_framework_src_patterns}
716)
717
718#This is a small wrapper library that shouldn't use any onnxruntime internal symbols(except onnxruntime_common).
719#Because it could dynamically link to onnxruntime. Otherwise you will have two copies of onnxruntime in the same
720#process and you won't know which one you are testing.
721onnxruntime_add_static_library(onnxruntime_test_utils ${onnxruntime_test_utils_src})
722if(MSVC)
723target_compile_options(onnxruntime_test_utils PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>"
724"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
725target_compile_options(onnxruntime_test_utils PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd6326>"
726"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd6326>")
727else()
728target_compile_definitions(onnxruntime_test_utils PUBLIC -DNSYNC_ATOMIC_CPP11)
729target_include_directories(onnxruntime_test_utils PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT})
730onnxruntime_add_include_to_target(onnxruntime_test_utils nsync::nsync_cpp)
731endif()
732if (onnxruntime_USE_NCCL)
733target_include_directories(onnxruntime_test_utils PRIVATE ${NCCL_INCLUDE_DIRS})
734endif()
735if (onnxruntime_USE_ROCM)
736target_include_directories(onnxruntime_test_utils PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/amdgpu/onnxruntime ${CMAKE_CURRENT_BINARY_DIR}/amdgpu/orttraining)
737endif()
738onnxruntime_add_include_to_target(onnxruntime_test_utils onnxruntime_common onnxruntime_framework onnxruntime_session GTest::gtest GTest::gmock onnx onnx_proto flatbuffers::flatbuffers nlohmann_json::nlohmann_json Boost::mp11 safeint_interface)
739
740
741
742if (onnxruntime_USE_DML)
743target_add_dml(onnxruntime_test_utils)
744endif()
745add_dependencies(onnxruntime_test_utils ${onnxruntime_EXTERNAL_DEPENDENCIES})
746target_include_directories(onnxruntime_test_utils PUBLIC "${TEST_SRC_DIR}/util/include" PRIVATE
747${eigen_INCLUDE_DIRS} ${ONNXRUNTIME_ROOT})
748set_target_properties(onnxruntime_test_utils PROPERTIES FOLDER "ONNXRuntimeTest")
749source_group(TREE ${TEST_SRC_DIR} FILES ${onnxruntime_test_utils_src})
750
751if(NOT IOS)
752set(onnx_test_runner_src_dir ${TEST_SRC_DIR}/onnx)
753file(GLOB onnx_test_runner_common_srcs CONFIGURE_DEPENDS
754${onnx_test_runner_src_dir}/*.h
755${onnx_test_runner_src_dir}/*.cc)
756
757list(REMOVE_ITEM onnx_test_runner_common_srcs ${onnx_test_runner_src_dir}/main.cc)
758
759onnxruntime_add_static_library(onnx_test_runner_common ${onnx_test_runner_common_srcs})
760if(MSVC)
761target_compile_options(onnx_test_runner_common PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>"
762"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
763else()
764target_compile_definitions(onnx_test_runner_common PUBLIC -DNSYNC_ATOMIC_CPP11)
765target_include_directories(onnx_test_runner_common PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT})
766onnxruntime_add_include_to_target(onnx_test_runner_common nsync::nsync_cpp)
767endif()
768if (MSVC AND NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
769#TODO: fix the warnings, they are dangerous
770target_compile_options(onnx_test_runner_common PRIVATE "/wd4244")
771endif()
772onnxruntime_add_include_to_target(onnx_test_runner_common onnxruntime_common onnxruntime_framework
773onnxruntime_test_utils onnx onnx_proto re2::re2 flatbuffers::flatbuffers Boost::mp11 safeint_interface)
774
775add_dependencies(onnx_test_runner_common onnx_test_data_proto ${onnxruntime_EXTERNAL_DEPENDENCIES})
776target_include_directories(onnx_test_runner_common PRIVATE ${eigen_INCLUDE_DIRS}
777${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT})
778
779set_target_properties(onnx_test_runner_common PROPERTIES FOLDER "ONNXRuntimeTest")
780set(onnx_test_runner_common_lib onnx_test_runner_common)
781endif()
782
783set(all_tests ${onnxruntime_test_common_src} ${onnxruntime_test_ir_src} ${onnxruntime_test_optimizer_src}
784${onnxruntime_test_framework_src} ${onnxruntime_test_providers_src} ${onnxruntime_test_quantization_src}
785${onnxruntime_test_flatbuffers_src})
786
787if (onnxruntime_ENABLE_CUDA_EP_INTERNAL_TESTS)
788file(GLOB onnxruntime_test_providers_cuda_ut_src CONFIGURE_DEPENDS
789"${TEST_SRC_DIR}/providers/cuda/test_cases/*"
790)
791# onnxruntime_providers_cuda_ut is only for unittests.
792onnxruntime_add_shared_library_module(onnxruntime_providers_cuda_ut ${onnxruntime_test_providers_cuda_ut_src} $<TARGET_OBJECTS:onnxruntime_providers_cuda_obj>)
793config_cuda_provider_shared_module(onnxruntime_providers_cuda_ut)
794onnxruntime_add_include_to_target(onnxruntime_providers_cuda_ut GTest::gtest GTest::gmock)
795add_dependencies(onnxruntime_providers_cuda_ut onnxruntime_test_utils onnxruntime_common)
796target_include_directories(onnxruntime_providers_cuda_ut PRIVATE ${ONNXRUNTIME_ROOT}/core/mickey)
797target_link_libraries(onnxruntime_providers_cuda_ut PRIVATE GTest::gtest GTest::gmock ${ONNXRUNTIME_MLAS_LIBS} onnxruntime_test_utils onnxruntime_common)
798if (MSVC)
799# Cutlass code has an issue with the following:
800# warning C4100: 'magic': unreferenced formal parameter
801target_compile_options(onnxruntime_providers_cuda_ut PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd4100>"
802"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd4100>")
803endif()
804
805list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_cuda_ut)
806endif()
807
808set(all_dependencies ${onnxruntime_test_providers_dependencies} )
809
810if (onnxruntime_ENABLE_TRAINING)
811list(APPEND all_tests ${onnxruntime_test_training_src})
812endif()
813
814if (onnxruntime_ENABLE_TRAINING_APIS)
815list(APPEND all_tests ${onnxruntime_test_training_api_src})
816endif()
817
818if (onnxruntime_USE_TVM)
819list(APPEND all_tests ${onnxruntime_test_tvm_src})
820endif()
821
822if (onnxruntime_USE_OPENVINO)
823list(APPEND all_tests ${onnxruntime_test_openvino_src})
824endif()
825
826# this is only added to onnxruntime_test_framework_libs above, but we use onnxruntime_test_providers_libs for the onnxruntime_test_all target.
827# for now, add it here. better is probably to have onnxruntime_test_providers_libs use the full onnxruntime_test_framework_libs
828# list given it's built on top of that library and needs all the same dependencies.
829if(WIN32)
830list(APPEND onnxruntime_test_providers_libs Advapi32)
831endif()
832
833if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
834if (NOT onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
835list(REMOVE_ITEM all_tests
836"${TEST_SRC_DIR}/framework/execution_frame_test.cc"
837"${TEST_SRC_DIR}/framework/inference_session_test.cc"
838"${TEST_SRC_DIR}/platform/barrier_test.cc"
839"${TEST_SRC_DIR}/platform/threadpool_test.cc"
840"${TEST_SRC_DIR}/providers/cpu/controlflow/loop_test.cc"
841"${TEST_SRC_DIR}/providers/cpu/nn/string_normalizer_test.cc"
842"${TEST_SRC_DIR}/providers/memcpy_test.cc"
843)
844endif()
845list(REMOVE_ITEM all_tests "${TEST_SRC_DIR}/providers/cpu/reduction/reduction_ops_test.cc"
846"${TEST_SRC_DIR}/providers/cpu/tensor/grid_sample_test.cc")
847endif()
848
849if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten" OR IOS)
850# Because we do not run these model tests in our web or iOS CI build pipelines, and some test code uses C++17
851# filesystem functions that are not available in the iOS version we target.
852message("Disable model tests in onnxruntime_test_all")
853list(REMOVE_ITEM all_tests
854"${TEST_SRC_DIR}/providers/cpu/model_tests.cc"
855)
856endif()
857
858set(test_all_args)
859if (onnxruntime_USE_TENSORRT)
860# TRT EP CI takes much longer time when updating to TRT 8.2
861# So, we only run trt ep and exclude other eps to reduce CI test time.
862#
863# The test names of model tests were using sequential number in the past.
864# This PR https://github.com/microsoft/onnxruntime/pull/10220 (Please see ExpandModelName function in model_tests.cc for more details)
865# made test name contain the "ep" and "model path" information, so we can easily filter the tests using cuda ep or other ep with *cpu_* or *xxx_*.
866list(APPEND test_all_args "--gtest_filter=-*cpu_*:*cuda_*" )
867endif ()
868if(NOT onnxruntime_ENABLE_CUDA_EP_INTERNAL_TESTS)
869list(REMOVE_ITEM all_tests ${TEST_SRC_DIR}/providers/cuda/cuda_provider_test.cc)
870endif()
871AddTest(
872TARGET onnxruntime_test_all
873SOURCES ${all_tests} ${onnxruntime_unittest_main_src}
874LIBS
875${onnx_test_runner_common_lib} ${onnxruntime_test_providers_libs} ${onnxruntime_test_common_libs}
876onnx_test_data_proto
877DEPENDS ${all_dependencies}
878TEST_ARGS ${test_all_args}
879)
880
881if (MSVC)
882# The warning means the type of two integral values around a binary operator is narrow than their result.
883# If we promote the two input values first, it could be more tolerant to integer overflow.
884# However, this is test code. We are less concerned.
885target_compile_options(onnxruntime_test_all PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26451>"
886"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26451>")
887target_compile_options(onnxruntime_test_all PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd4244>"
888"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd4244>")
889
890# Avoid this compile error in graph_transform_test.cc and qdq_transformer_test.cc:
891# fatal error C1128: number of sections exceeded object file format limit: compile with /bigobj
892set_property(SOURCE "${TEST_SRC_DIR}/optimizer/graph_transform_test.cc"
893"${TEST_SRC_DIR}/optimizer/qdq_transformer_test.cc"
894APPEND PROPERTY COMPILE_OPTIONS "/bigobj")
895set_property(SOURCE "${TEST_SRC_DIR}/optimizer/qdq_transformer_test.cc"
896APPEND PROPERTY COMPILE_OPTIONS "/bigobj")
897else()
898target_compile_options(onnxruntime_test_all PRIVATE "-Wno-parentheses")
899endif()
900
901# TODO fix shorten-64-to-32 warnings
902# there are some in builds where sizeof(size_t) != sizeof(int64_t), e.g., in 'ONNX Runtime Web CI Pipeline'
903if (HAS_SHORTEN_64_TO_32 AND NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
904target_compile_options(onnxruntime_test_all PRIVATE -Wno-error=shorten-64-to-32)
905endif()
906
907if (UNIX AND onnxruntime_USE_TENSORRT)
908# The test_main.cc includes NvInfer.h where it has many deprecated declarations
909# simply ignore them for TensorRT EP build
910set_property(TARGET onnxruntime_test_all APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-deprecated-declarations")
911endif()
912
913if (MSVC AND onnxruntime_ENABLE_STATIC_ANALYSIS)
914# attention_op_test.cc: Function uses '49152' bytes of stack: exceeds /analyze:stacksize '16384'..
915target_compile_options(onnxruntime_test_all PRIVATE "/analyze:stacksize 131072")
916endif()
917
918#In AIX + gcc compiler ,crash is observed with the usage of googletest EXPECT_THROW,
919#because some needed symbol is garbaged out by linker.
920#So, fix is to exports the symbols from executable.
921#Another way is to use -Wl,-bkeepfile for each object file where EXPECT_THROW is used like below
922#target_link_options(onnxruntime_test_all PRIVATE "-Wl,-bkeepfile:CMakeFiles/onnxruntime_test_all.dir${TEST_SRC_DIR}/framework/tensor_test.cc.o")
923if (CMAKE_SYSTEM_NAME MATCHES "AIX" AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
924set_target_properties(onnxruntime_test_all PROPERTIES ENABLE_EXPORTS 1)
925endif()
926
927# the default logger tests conflict with the need to have an overall default logger
928# so skip in this type of
929target_compile_definitions(onnxruntime_test_all PUBLIC -DSKIP_DEFAULT_LOGGER_TESTS)
930if (IOS)
931target_compile_definitions(onnxruntime_test_all_xc PUBLIC -DSKIP_DEFAULT_LOGGER_TESTS)
932endif()
933if(onnxruntime_RUN_MODELTEST_IN_DEBUG_MODE)
934target_compile_definitions(onnxruntime_test_all PUBLIC -DRUN_MODELTEST_IN_DEBUG_MODE)
935endif()
936if (onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS)
937target_compile_definitions(onnxruntime_test_all PRIVATE DEBUG_NODE_INPUTS_OUTPUTS)
938endif()
939if (onnxruntime_USE_ROCM)
940if (onnxruntime_USE_COMPOSABLE_KERNEL)
941target_compile_definitions(onnxruntime_test_all PRIVATE USE_COMPOSABLE_KERNEL)
942if (onnxruntime_USE_COMPOSABLE_KERNEL_CK_TILE)
943target_compile_definitions(onnxruntime_test_all PRIVATE USE_COMPOSABLE_KERNEL_CK_TILE)
944endif()
945endif()
946target_compile_options(onnxruntime_test_all PRIVATE -D__HIP_PLATFORM_AMD__=1 -D__HIP_PLATFORM_HCC__=1)
947target_include_directories(onnxruntime_test_all PRIVATE ${onnxruntime_ROCM_HOME}/hipfft/include ${onnxruntime_ROCM_HOME}/include ${onnxruntime_ROCM_HOME}/hiprand/include ${onnxruntime_ROCM_HOME}/rocrand/include ${CMAKE_CURRENT_BINARY_DIR}/amdgpu/onnxruntime ${CMAKE_CURRENT_BINARY_DIR}/amdgpu/orttraining)
948endif()
949if (onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)
950target_link_libraries(onnxruntime_test_all PRIVATE Python::Python)
951endif()
952if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
953set_target_properties(onnxruntime_test_all PROPERTIES LINK_DEPENDS ${TEST_SRC_DIR}/wasm/onnxruntime_test_all_adapter.js)
954set_target_properties(onnxruntime_test_all PROPERTIES LINK_DEPENDS ${ONNXRUNTIME_ROOT}/wasm/pre.js)
955set_target_properties(onnxruntime_test_all PROPERTIES LINK_FLAGS "-s STACK_SIZE=5242880 -s INITIAL_MEMORY=536870912 -s ALLOW_MEMORY_GROWTH=1 -s MAXIMUM_MEMORY=4294967296 -s INCOMING_MODULE_JS_API=[preRun,locateFile,arguments,onExit,wasmMemory,buffer,instantiateWasm] --pre-js \"${TEST_SRC_DIR}/wasm/onnxruntime_test_all_adapter.js\" --pre-js \"${ONNXRUNTIME_ROOT}/wasm/pre.js\" -s \"EXPORTED_RUNTIME_METHODS=['FS']\" --preload-file ${CMAKE_CURRENT_BINARY_DIR}/testdata@/testdata -s EXIT_RUNTIME=1")
956if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
957set_property(TARGET onnxruntime_test_all APPEND_STRING PROPERTY LINK_FLAGS " -s DEFAULT_PTHREAD_STACK_SIZE=131072 -s PROXY_TO_PTHREAD=1")
958endif()
959if (onnxruntime_USE_JSEP)
960set_target_properties(onnxruntime_test_all PROPERTIES LINK_DEPENDS ${ONNXRUNTIME_ROOT}/wasm/pre-jsep.js)
961set_property(TARGET onnxruntime_test_all APPEND_STRING PROPERTY LINK_FLAGS " --pre-js \"${ONNXRUNTIME_ROOT}/wasm/pre-jsep.js\"")
962endif()
963
964###
965### if you want to investigate or debug a test failure in onnxruntime_test_all, replace the following line.
966### those flags slow down the CI test significantly, so we don't use them by default.
967###
968# set_property(TARGET onnxruntime_test_all APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=2 -s SAFE_HEAP=1 -s STACK_OVERFLOW_CHECK=2")
969set_property(TARGET onnxruntime_test_all APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=0 -s SAFE_HEAP=0 -s STACK_OVERFLOW_CHECK=1")
970endif()
971
972if (onnxruntime_ENABLE_ATEN)
973target_compile_definitions(onnxruntime_test_all PRIVATE ENABLE_ATEN)
974endif()
975
976set(test_data_target onnxruntime_test_all)
977
978onnxruntime_add_static_library(onnx_test_data_proto ${TEST_SRC_DIR}/proto/tml.proto)
979add_dependencies(onnx_test_data_proto onnx_proto ${onnxruntime_EXTERNAL_DEPENDENCIES})
980#onnx_proto target should mark this definition as public, instead of private
981target_compile_definitions(onnx_test_data_proto PRIVATE "-DONNX_API=")
982onnxruntime_add_include_to_target(onnx_test_data_proto onnx_proto)
983target_include_directories(onnx_test_data_proto PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
984set_target_properties(onnx_test_data_proto PROPERTIES FOLDER "ONNXRuntimeTest")
985onnxruntime_protobuf_generate(APPEND_PATH IMPORT_DIRS ${onnx_SOURCE_DIR} TARGET onnx_test_data_proto)
986
987#
988# onnxruntime_ir_graph test data
989#
990set(TEST_DATA_SRC ${TEST_SRC_DIR}/testdata)
991set(TEST_DATA_DES $<TARGET_FILE_DIR:${test_data_target}>/testdata)
992
993set(TEST_SAMPLES_SRC ${REPO_ROOT}/samples)
994set(TEST_SAMPLES_DES $<TARGET_FILE_DIR:${test_data_target}>/samples)
995
996# Copy test data from source to destination.
997add_custom_command(
998TARGET ${test_data_target} PRE_BUILD
999COMMAND ${CMAKE_COMMAND} -E copy_directory
1000${TEST_DATA_SRC}
1001${TEST_DATA_DES})
1002
1003# Copy test samples from source to destination.
1004add_custom_command(
1005TARGET ${test_data_target} PRE_BUILD
1006COMMAND ${CMAKE_COMMAND} -E copy_directory
1007${TEST_SAMPLES_SRC}
1008${TEST_SAMPLES_DES})
1009
1010if (NOT onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)
1011if (onnxruntime_USE_SNPE)
1012add_custom_command(
1013TARGET ${test_data_target} POST_BUILD
1014COMMAND ${CMAKE_COMMAND} -E copy ${SNPE_SO_FILES} $<TARGET_FILE_DIR:${test_data_target}>
1015)
1016endif()
1017
1018if (onnxruntime_USE_QNN)
1019if (MSVC OR ${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
1020add_custom_command(
1021TARGET ${test_data_target} POST_BUILD
1022COMMAND ${CMAKE_COMMAND} -E copy ${QNN_LIB_FILES} $<TARGET_FILE_DIR:${test_data_target}>
1023)
1024endif()
1025if (EXISTS "${onnxruntime_QNN_HOME}/Qualcomm AI Hub Proprietary License.pdf")
1026add_custom_command(
1027TARGET ${test_data_target} POST_BUILD
1028COMMAND ${CMAKE_COMMAND} -E copy "${onnxruntime_QNN_HOME}/Qualcomm AI Hub Proprietary License.pdf" $<TARGET_FILE_DIR:${test_data_target}>
1029)
1030endif()
1031endif()
1032
1033if (onnxruntime_USE_DNNL)
1034if(onnxruntime_DNNL_GPU_RUNTIME STREQUAL "ocl" AND onnxruntime_DNNL_OPENCL_ROOT STREQUAL "")
1035message(FATAL_ERROR "--dnnl_opencl_root required")
1036elseif(onnxruntime_DNNL_GPU_RUNTIME STREQUAL "" AND NOT (onnxruntime_DNNL_OPENCL_ROOT STREQUAL ""))
1037message(FATAL_ERROR "--dnnl_gpu_runtime required")
1038elseif(onnxruntime_DNNL_GPU_RUNTIME STREQUAL "ocl" AND NOT (onnxruntime_DNNL_OPENCL_ROOT STREQUAL ""))
1039#file(TO_CMAKE_PATH ${onnxruntime_DNNL_OPENCL_ROOT} onnxruntime_DNNL_OPENCL_ROOT)
1040#set(DNNL_OCL_INCLUDE_DIR ${onnxruntime_DNNL_OPENCL_ROOT}/include)
1041#set(DNNL_GPU_CMAKE_ARGS "-DDNNL_GPU_RUNTIME=OCL " "-DOPENCLROOT=${onnxruntime_DNNL_OPENCL_ROOT}")
1042target_compile_definitions(onnxruntime_test_all PUBLIC -DDNNL_GPU_RUNTIME=OCL)
1043endif()
1044list(APPEND onnx_test_libs dnnl)
1045add_custom_command(
1046TARGET ${test_data_target} POST_BUILD
1047COMMAND ${CMAKE_COMMAND} -E copy ${DNNL_DLL_PATH} $<TARGET_FILE_DIR:${test_data_target}>
1048)
1049endif()
1050if(WIN32)
1051if (onnxruntime_USE_TVM)
1052add_custom_command(
1053TARGET ${test_data_target} POST_BUILD
1054COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:tvm> $<TARGET_FILE_DIR:${test_data_target}>
1055)
1056endif()
1057endif()
1058
1059if(WIN32)
1060set(wide_get_opt_src_dir ${TEST_SRC_DIR}/win_getopt/wide)
1061onnxruntime_add_static_library(win_getopt_wide ${wide_get_opt_src_dir}/getopt.cc ${wide_get_opt_src_dir}/include/getopt.h)
1062target_include_directories(win_getopt_wide INTERFACE ${wide_get_opt_src_dir}/include)
1063set_target_properties(win_getopt_wide PROPERTIES FOLDER "ONNXRuntimeTest")
1064set(onnx_test_runner_common_srcs ${onnx_test_runner_common_srcs})
1065set(GETOPT_LIB_WIDE win_getopt_wide)
1066endif()
1067endif()
1068
1069
1070set(onnx_test_libs
1071onnxruntime_test_utils
1072${ONNXRUNTIME_TEST_LIBS}
1073onnx_test_data_proto
1074${onnxruntime_EXTERNAL_LIBRARIES})
1075
1076if (NOT IOS)
1077onnxruntime_add_executable(onnx_test_runner ${onnx_test_runner_src_dir}/main.cc)
1078if(MSVC)
1079target_compile_options(onnx_test_runner PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>"
1080"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
1081endif()
1082if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
1083if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
1084set_target_properties(onnx_test_runner PROPERTIES LINK_FLAGS "-s NODERAWFS=1 -s ALLOW_MEMORY_GROWTH=1 -s PROXY_TO_PTHREAD=1 -s EXIT_RUNTIME=1")
1085else()
1086set_target_properties(onnx_test_runner PROPERTIES LINK_FLAGS "-s NODERAWFS=1 -s ALLOW_MEMORY_GROWTH=1")
1087endif()
1088endif()
1089
1090target_link_libraries(onnx_test_runner PRIVATE onnx_test_runner_common ${GETOPT_LIB_WIDE} ${onnx_test_libs} nlohmann_json::nlohmann_json)
1091target_include_directories(onnx_test_runner PRIVATE ${ONNXRUNTIME_ROOT})
1092if (onnxruntime_USE_ROCM)
1093target_include_directories(onnx_test_runner PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/amdgpu/onnxruntime ${CMAKE_CURRENT_BINARY_DIR}/amdgpu/orttraining)
1094endif()
1095if (onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)
1096target_link_libraries(onnx_test_runner PRIVATE Python::Python)
1097endif()
1098set_target_properties(onnx_test_runner PROPERTIES FOLDER "ONNXRuntimeTest")
1099
1100if (onnxruntime_USE_TVM)
1101if (WIN32)
1102target_link_options(onnx_test_runner PRIVATE "/STACK:4000000")
1103endif()
1104endif()
1105
1106install(TARGETS onnx_test_runner
1107ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
1108LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
1109BUNDLE DESTINATION ${CMAKE_INSTALL_LIBDIR}
1110RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
1111endif()
1112
1113if (NOT onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)
1114if(onnxruntime_BUILD_BENCHMARKS)
1115SET(BENCHMARK_DIR ${TEST_SRC_DIR}/onnx/microbenchmark)
1116onnxruntime_add_executable(onnxruntime_benchmark
1117${BENCHMARK_DIR}/main.cc
1118${BENCHMARK_DIR}/modeltest.cc
1119${BENCHMARK_DIR}/pooling.cc
1120${BENCHMARK_DIR}/resize.cc
1121${BENCHMARK_DIR}/batchnorm.cc
1122${BENCHMARK_DIR}/batchnorm2.cc
1123${BENCHMARK_DIR}/tptest.cc
1124${BENCHMARK_DIR}/eigen.cc
1125${BENCHMARK_DIR}/copy.cc
1126${BENCHMARK_DIR}/gelu.cc
1127${BENCHMARK_DIR}/activation.cc
1128${BENCHMARK_DIR}/quantize.cc
1129${BENCHMARK_DIR}/reduceminmax.cc)
1130target_include_directories(onnxruntime_benchmark PRIVATE ${ONNXRUNTIME_ROOT} ${onnxruntime_graph_header} ${ONNXRUNTIME_ROOT}/core/mlas/inc)
1131target_compile_definitions(onnxruntime_benchmark PRIVATE BENCHMARK_STATIC_DEFINE)
1132if(WIN32)
1133target_compile_options(onnxruntime_benchmark PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd4141>"
1134"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd4141>")
1135# Avoid using new and delete. But this is a benchmark program, it's ok if it has a chance to leak.
1136target_compile_options(onnxruntime_benchmark PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26409>"
1137"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26409>")
1138target_compile_options(onnxruntime_benchmark PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26400>"
1139"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26400>")
1140target_compile_options(onnxruntime_benchmark PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26814>"
1141"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26814>")
1142target_compile_options(onnxruntime_benchmark PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26814>"
1143"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26497>")
1144target_compile_options(onnxruntime_benchmark PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26426>"
1145"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26426>")
1146target_compile_options(onnxruntime_benchmark PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>"
1147"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
1148endif()
1149target_link_libraries(onnxruntime_benchmark PRIVATE onnx_test_runner_common benchmark::benchmark ${onnx_test_libs})
1150add_dependencies(onnxruntime_benchmark ${onnxruntime_EXTERNAL_DEPENDENCIES})
1151set_target_properties(onnxruntime_benchmark PROPERTIES FOLDER "ONNXRuntimeTest")
1152
1153SET(MLAS_BENCH_DIR ${TEST_SRC_DIR}/mlas/bench)
1154file(GLOB_RECURSE MLAS_BENCH_SOURCE_FILES "${MLAS_BENCH_DIR}/*.cpp" "${MLAS_BENCH_DIR}/*.h")
1155onnxruntime_add_executable(onnxruntime_mlas_benchmark ${MLAS_BENCH_SOURCE_FILES})
1156target_include_directories(onnxruntime_mlas_benchmark PRIVATE ${ONNXRUNTIME_ROOT}/core/mlas/inc)
1157target_link_libraries(onnxruntime_mlas_benchmark PRIVATE benchmark::benchmark onnxruntime_util onnxruntime_framework ${ONNXRUNTIME_MLAS_LIBS} onnxruntime_common ${CMAKE_DL_LIBS})
1158target_compile_definitions(onnxruntime_mlas_benchmark PRIVATE BENCHMARK_STATIC_DEFINE)
1159if(WIN32)
1160target_link_libraries(onnxruntime_mlas_benchmark PRIVATE debug Dbghelp)
1161# Avoid using new and delete. But this is a benchmark program, it's ok if it has a chance to leak.
1162target_compile_options(onnxruntime_mlas_benchmark PRIVATE /wd26409)
1163# "Global initializer calls a non-constexpr function." BENCHMARK_CAPTURE macro needs this.
1164target_compile_options(onnxruntime_mlas_benchmark PRIVATE /wd26426)
1165else()
1166target_link_libraries(onnxruntime_mlas_benchmark PRIVATE nsync::nsync_cpp ${CMAKE_DL_LIBS})
1167endif()
1168if (CPUINFO_SUPPORTED AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
1169target_link_libraries(onnxruntime_mlas_benchmark PRIVATE cpuinfo)
1170endif()
1171set_target_properties(onnxruntime_mlas_benchmark PROPERTIES FOLDER "ONNXRuntimeTest")
1172endif()
1173
1174if(WIN32)
1175target_compile_options(onnx_test_runner_common PRIVATE -D_CRT_SECURE_NO_WARNINGS)
1176endif()
1177
1178if (NOT onnxruntime_REDUCED_OPS_BUILD AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
1179add_test(NAME onnx_test_pytorch_converted
1180COMMAND onnx_test_runner ${onnx_SOURCE_DIR}/onnx/backend/test/data/pytorch-converted)
1181add_test(NAME onnx_test_pytorch_operator
1182COMMAND onnx_test_runner ${onnx_SOURCE_DIR}/onnx/backend/test/data/pytorch-operator)
1183endif()
1184
1185if (CMAKE_SYSTEM_NAME STREQUAL "Android")
1186list(APPEND android_shared_libs log android)
1187endif()
1188endif()
1189
1190
1191if (NOT onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)
1192if(NOT IOS)
1193#perf test runner
1194set(onnxruntime_perf_test_src_dir ${TEST_SRC_DIR}/perftest)
1195set(onnxruntime_perf_test_src_patterns
1196"${onnxruntime_perf_test_src_dir}/*.cc"
1197"${onnxruntime_perf_test_src_dir}/*.h")
1198
1199if(WIN32)
1200list(APPEND onnxruntime_perf_test_src_patterns
1201"${onnxruntime_perf_test_src_dir}/windows/*.cc"
1202"${onnxruntime_perf_test_src_dir}/windows/*.h" )
1203else ()
1204list(APPEND onnxruntime_perf_test_src_patterns
1205"${onnxruntime_perf_test_src_dir}/posix/*.cc"
1206"${onnxruntime_perf_test_src_dir}/posix/*.h" )
1207endif()
1208
1209file(GLOB onnxruntime_perf_test_src CONFIGURE_DEPENDS
1210${onnxruntime_perf_test_src_patterns}
1211)
1212onnxruntime_add_executable(onnxruntime_perf_test ${onnxruntime_perf_test_src} ${ONNXRUNTIME_ROOT}/core/platform/path_lib.cc)
1213if(MSVC)
1214target_compile_options(onnxruntime_perf_test PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>"
1215"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
1216endif()
1217target_include_directories(onnxruntime_perf_test PRIVATE ${onnx_test_runner_src_dir} ${ONNXRUNTIME_ROOT}
1218${eigen_INCLUDE_DIRS} ${onnxruntime_graph_header} ${onnxruntime_exec_src_dir}
1219${CMAKE_CURRENT_BINARY_DIR})
1220if (onnxruntime_USE_ROCM)
1221target_include_directories(onnxruntime_perf_test PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/amdgpu/onnxruntime ${CMAKE_CURRENT_BINARY_DIR}/amdgpu/orttraining)
1222endif()
1223if (WIN32)
1224target_compile_options(onnxruntime_perf_test PRIVATE ${disabled_warnings})
1225if (NOT DEFINED SYS_PATH_LIB)
1226set(SYS_PATH_LIB shlwapi)
1227endif()
1228endif()
1229
1230if (onnxruntime_BUILD_SHARED_LIB)
1231#It will dynamically link to onnxruntime. So please don't add onxruntime_graph/onxruntime_framework/... here.
1232#onnxruntime_common is kind of ok because it is thin, tiny and totally stateless.
1233set(onnxruntime_perf_test_libs
1234onnx_test_runner_common onnxruntime_test_utils onnxruntime_common
1235onnxruntime onnxruntime_flatbuffers onnx_test_data_proto
1236${onnxruntime_EXTERNAL_LIBRARIES}
1237${GETOPT_LIB_WIDE} ${SYS_PATH_LIB} ${CMAKE_DL_LIBS})
1238if(NOT WIN32)
1239list(APPEND onnxruntime_perf_test_libs nsync::nsync_cpp)
1240if(onnxruntime_USE_SNPE)
1241list(APPEND onnxruntime_perf_test_libs onnxruntime_providers_snpe)
1242endif()
1243endif()
1244if (CMAKE_SYSTEM_NAME STREQUAL "Android")
1245list(APPEND onnxruntime_perf_test_libs ${android_shared_libs})
1246endif()
1247if (${CMAKE_SYSTEM_NAME} MATCHES "AIX")
1248list(APPEND onnxruntime_perf_test_libs onnxruntime_graph onnxruntime_session onnxruntime_providers onnxruntime_framework onnxruntime_util onnxruntime_mlas onnxruntime_optimizer onnxruntime_flatbuffers iconv re2 gtest absl_failure_signal_handler absl_examine_stack absl_flags_parse absl_flags_usage absl_flags_usage_internal)
1249endif()
1250target_link_libraries(onnxruntime_perf_test PRIVATE ${onnxruntime_perf_test_libs} Threads::Threads)
1251if(WIN32)
1252target_link_libraries(onnxruntime_perf_test PRIVATE debug dbghelp advapi32)
1253endif()
1254else()
1255target_link_libraries(onnxruntime_perf_test PRIVATE onnx_test_runner_common ${GETOPT_LIB_WIDE} ${onnx_test_libs})
1256endif()
1257set_target_properties(onnxruntime_perf_test PROPERTIES FOLDER "ONNXRuntimeTest")
1258
1259if (onnxruntime_USE_TVM)
1260if (WIN32)
1261target_link_options(onnxruntime_perf_test PRIVATE "/STACK:4000000")
1262endif()
1263endif()
1264endif()
1265# shared lib
1266if (onnxruntime_BUILD_SHARED_LIB)
1267onnxruntime_add_static_library(onnxruntime_mocked_allocator ${TEST_SRC_DIR}/util/test_allocator.cc)
1268target_include_directories(onnxruntime_mocked_allocator PUBLIC ${TEST_SRC_DIR}/util/include)
1269target_link_libraries(onnxruntime_mocked_allocator PRIVATE ${GSL_TARGET})
1270set_target_properties(onnxruntime_mocked_allocator PROPERTIES FOLDER "ONNXRuntimeTest")
1271
1272#################################################################
1273# test inference using shared lib
1274set(onnxruntime_shared_lib_test_LIBS onnxruntime_mocked_allocator onnxruntime_test_utils onnxruntime_common onnx_proto)
1275if(NOT WIN32)
1276list(APPEND onnxruntime_shared_lib_test_LIBS nsync::nsync_cpp)
1277if(onnxruntime_USE_SNPE)
1278list(APPEND onnxruntime_shared_lib_test_LIBS onnxruntime_providers_snpe)
1279endif()
1280endif()
1281if (CPUINFO_SUPPORTED AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
1282list(APPEND onnxruntime_shared_lib_test_LIBS cpuinfo)
1283endif()
1284if (onnxruntime_USE_CUDA)
1285list(APPEND onnxruntime_shared_lib_test_LIBS CUDA::cudart)
1286endif()
1287if (onnxruntime_USE_ROCM)
1288list(APPEND onnxruntime_shared_lib_test_LIBS hip::host)
1289endif()
1290if (onnxruntime_USE_TENSORRT)
1291list(APPEND onnxruntime_shared_lib_test_LIBS ${TENSORRT_LIBRARY_INFER})
1292endif()
1293if (onnxruntime_USE_DML)
1294list(APPEND onnxruntime_shared_lib_test_LIBS d3d12.lib)
1295endif()
1296if (CMAKE_SYSTEM_NAME STREQUAL "Android")
1297list(APPEND onnxruntime_shared_lib_test_LIBS ${android_shared_libs})
1298endif()
1299
1300if (${CMAKE_SYSTEM_NAME} MATCHES "AIX")
1301list(APPEND onnxruntime_shared_lib_test_LIBS onnxruntime_graph onnxruntime_session onnxruntime_providers onnxruntime_framework onnxruntime_util onnxruntime_mlas onnxruntime_optimizer onnxruntime_flatbuffers iconv re2)
1302endif()
1303
1304AddTest(DYN
1305TARGET onnxruntime_shared_lib_test
1306SOURCES ${onnxruntime_shared_lib_test_SRC} ${onnxruntime_unittest_main_src}
1307LIBS ${onnxruntime_shared_lib_test_LIBS}
1308DEPENDS ${all_dependencies}
1309)
1310if (onnxruntime_USE_CUDA)
1311target_include_directories(onnxruntime_shared_lib_test PRIVATE ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
1312target_sources(onnxruntime_shared_lib_test PRIVATE ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/cuda_ops.cu)
1313endif()
1314if (onnxruntime_USE_ROCM)
1315target_include_directories(onnxruntime_shared_lib_test PRIVATE ${onnxruntime_ROCM_HOME}/include)
1316target_compile_definitions(onnxruntime_shared_lib_test PRIVATE __HIP_PLATFORM_AMD__)
1317endif()
1318if (CMAKE_SYSTEM_NAME STREQUAL "Android")
1319target_sources(onnxruntime_shared_lib_test PRIVATE
1320"${ONNXRUNTIME_ROOT}/core/platform/android/cxa_demangle.cc"
1321"${TEST_SRC_DIR}/platform/android/cxa_demangle_test.cc"
1322)
1323target_compile_definitions(onnxruntime_shared_lib_test PRIVATE USE_DUMMY_EXA_DEMANGLE=1)
1324endif()
1325
1326if (IOS)
1327add_custom_command(
1328TARGET onnxruntime_shared_lib_test POST_BUILD
1329COMMAND ${CMAKE_COMMAND} -E copy_directory
1330${TEST_DATA_SRC}
1331$<TARGET_FILE_DIR:onnxruntime_shared_lib_test>/testdata)
1332endif()
1333
1334if (UNIX AND onnxruntime_USE_TENSORRT)
1335# The test_main.cc includes NvInfer.h where it has many deprecated declarations
1336# simply ignore them for TensorRT EP build
1337set_property(TARGET onnxruntime_shared_lib_test APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-deprecated-declarations")
1338endif()
1339
1340# test inference using global threadpools
1341if (NOT CMAKE_SYSTEM_NAME MATCHES "Android|iOS" AND NOT onnxruntime_MINIMAL_BUILD)
1342AddTest(DYN
1343TARGET onnxruntime_global_thread_pools_test
1344SOURCES ${onnxruntime_global_thread_pools_test_SRC}
1345LIBS ${onnxruntime_shared_lib_test_LIBS}
1346DEPENDS ${all_dependencies}
1347)
1348endif()
1349endif()
1350
1351# the debug node IO functionality uses static variables, so it is best tested
1352# in its own process
1353if(onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS)
1354AddTest(
1355TARGET onnxruntime_test_debug_node_inputs_outputs
1356SOURCES
1357"${TEST_SRC_DIR}/debug_node_inputs_outputs/debug_node_inputs_outputs_utils_test.cc"
1358"${TEST_SRC_DIR}/framework/TestAllocatorManager.cc"
1359"${TEST_SRC_DIR}/framework/test_utils.cc"
1360"${TEST_SRC_DIR}/providers/base_tester.h"
1361"${TEST_SRC_DIR}/providers/base_tester.cc"
1362"${TEST_SRC_DIR}/providers/checkers.h"
1363"${TEST_SRC_DIR}/providers/checkers.cc"
1364"${TEST_SRC_DIR}/providers/op_tester.h"
1365"${TEST_SRC_DIR}/providers/op_tester.cc"
1366"${TEST_SRC_DIR}/providers/provider_test_utils.h"
1367"${TEST_SRC_DIR}/providers/tester_types.h"
1368${onnxruntime_unittest_main_src}
1369LIBS ${onnxruntime_test_providers_libs} ${onnxruntime_test_common_libs}
1370DEPENDS ${all_dependencies}
1371)
1372
1373if (onnxruntime_USE_ROCM)
1374target_include_directories(onnxruntime_test_debug_node_inputs_outputs PRIVATE ${onnxruntime_ROCM_HOME}/hipfft/include ${onnxruntime_ROCM_HOME}/include ${onnxruntime_ROCM_HOME}/hipcub/include ${onnxruntime_ROCM_HOME}/hiprand/include ${onnxruntime_ROCM_HOME}/rocrand/include)
1375target_include_directories(onnxruntime_test_debug_node_inputs_outputs PRIVATE ${ONNXRUNTIME_ROOT} ${CMAKE_CURRENT_BINARY_DIR}/amdgpu/onnxruntime)
1376endif(onnxruntime_USE_ROCM)
1377
1378target_compile_definitions(onnxruntime_test_debug_node_inputs_outputs
1379PRIVATE DEBUG_NODE_INPUTS_OUTPUTS)
1380endif(onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS)
1381
1382#some ETW tools
1383if(WIN32 AND onnxruntime_ENABLE_INSTRUMENT)
1384onnxruntime_add_executable(generate_perf_report_from_etl ${ONNXRUNTIME_ROOT}/tool/etw/main.cc
1385${ONNXRUNTIME_ROOT}/tool/etw/eparser.h ${ONNXRUNTIME_ROOT}/tool/etw/eparser.cc
1386${ONNXRUNTIME_ROOT}/tool/etw/TraceSession.h ${ONNXRUNTIME_ROOT}/tool/etw/TraceSession.cc)
1387target_compile_definitions(generate_perf_report_from_etl PRIVATE "_CONSOLE" "_UNICODE" "UNICODE")
1388target_link_libraries(generate_perf_report_from_etl PRIVATE tdh Advapi32)
1389
1390onnxruntime_add_executable(compare_two_sessions ${ONNXRUNTIME_ROOT}/tool/etw/compare_two_sessions.cc
1391${ONNXRUNTIME_ROOT}/tool/etw/eparser.h ${ONNXRUNTIME_ROOT}/tool/etw/eparser.cc
1392${ONNXRUNTIME_ROOT}/tool/etw/TraceSession.h ${ONNXRUNTIME_ROOT}/tool/etw/TraceSession.cc)
1393target_compile_definitions(compare_two_sessions PRIVATE "_CONSOLE" "_UNICODE" "UNICODE")
1394target_link_libraries(compare_two_sessions PRIVATE ${GETOPT_LIB_WIDE} tdh Advapi32)
1395endif()
1396
1397if(NOT onnxruntime_target_platform STREQUAL "ARM64EC")
1398file(GLOB onnxruntime_mlas_test_src CONFIGURE_DEPENDS
1399"${TEST_SRC_DIR}/mlas/unittest/*.h"
1400"${TEST_SRC_DIR}/mlas/unittest/*.cpp"
1401)
1402onnxruntime_add_executable(onnxruntime_mlas_test ${onnxruntime_mlas_test_src})
1403if(MSVC)
1404target_compile_options(onnxruntime_mlas_test PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26409>"
1405"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26409>")
1406target_compile_options(onnxruntime_mlas_test PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>"
1407"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
1408target_compile_options(onnxruntime_mlas_test PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd6326>"
1409"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd6326>")
1410target_compile_options(onnxruntime_mlas_test PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26426>"
1411"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26426>")
1412endif()
1413if(IOS)
1414set_target_properties(onnxruntime_mlas_test PROPERTIES
1415XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO"
1416)
1417endif()
1418target_include_directories(onnxruntime_mlas_test PRIVATE ${ONNXRUNTIME_ROOT}/core/mlas/inc ${ONNXRUNTIME_ROOT}
1419${CMAKE_CURRENT_BINARY_DIR})
1420target_link_libraries(onnxruntime_mlas_test PRIVATE GTest::gtest GTest::gmock ${ONNXRUNTIME_MLAS_LIBS} onnxruntime_common)
1421if (CPUINFO_SUPPORTED AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
1422target_link_libraries(onnxruntime_mlas_test PRIVATE cpuinfo)
1423endif()
1424if(NOT WIN32)
1425target_link_libraries(onnxruntime_mlas_test PRIVATE nsync::nsync_cpp ${CMAKE_DL_LIBS})
1426endif()
1427if (CMAKE_SYSTEM_NAME STREQUAL "Android")
1428target_link_libraries(onnxruntime_mlas_test PRIVATE ${android_shared_libs})
1429endif()
1430if(WIN32)
1431target_link_libraries(onnxruntime_mlas_test PRIVATE debug Dbghelp Advapi32)
1432endif()
1433if (onnxruntime_LINK_LIBATOMIC)
1434target_link_libraries(onnxruntime_mlas_test PRIVATE atomic)
1435endif()
1436target_link_libraries(onnxruntime_mlas_test PRIVATE Threads::Threads)
1437set_target_properties(onnxruntime_mlas_test PROPERTIES FOLDER "ONNXRuntimeTest")
1438if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
1439if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
1440set_target_properties(onnxruntime_mlas_test PROPERTIES LINK_FLAGS "-s ALLOW_MEMORY_GROWTH=1 -s PROXY_TO_PTHREAD=1 -s EXIT_RUNTIME=1")
1441else()
1442set_target_properties(onnxruntime_mlas_test PROPERTIES LINK_FLAGS "-s ALLOW_MEMORY_GROWTH=1")
1443endif()
1444endif()
1445endif()
1446# Training API Tests
1447# Disabling training_api_test_trainer. CXXOPT generates a ton of warnings because of which nuget pipeline is failing.
1448# TODO(askhade): Fix the warnings.
1449# This has no impact on the release as the release package and the pipeline, both do not use this.
1450# This is used by devs for testing training apis.
1451#if (onnxruntime_ENABLE_TRAINING_APIS)
1452if (0)
1453# Only files in the trainer and common folder will be compiled into test trainer.
1454file(GLOB training_api_test_trainer_src
1455"${ORTTRAINING_SOURCE_DIR}/test/training_api/common/*.cc"
1456"${ORTTRAINING_SOURCE_DIR}/test/training_api/common/*.h"
1457"${ORTTRAINING_SOURCE_DIR}/test/training_api/trainer/*.cc"
1458"${ORTTRAINING_SOURCE_DIR}/test/training_api/trainer/*.h"
1459)
1460onnxruntime_add_executable(onnxruntime_test_trainer ${training_api_test_trainer_src})
1461
1462onnxruntime_add_include_to_target(onnxruntime_test_trainer onnxruntime_session
1463onnxruntime_framework onnxruntime_common onnx onnx_proto ${PROTOBUF_LIB} flatbuffers::flatbuffers)
1464
1465set(CXXOPTS ${cxxopts_SOURCE_DIR}/include)
1466target_include_directories(onnxruntime_test_trainer PRIVATE
1467${CMAKE_CURRENT_BINARY_DIR}
1468${ONNXRUNTIME_ROOT}
1469${ORTTRAINING_ROOT}
1470${eigen_INCLUDE_DIRS}
1471${CXXOPTS}
1472${extra_includes}
1473${onnxruntime_graph_header}
1474${onnxruntime_exec_src_dir}
1475)
1476
1477set(ONNXRUNTIME_TEST_LIBS
1478onnxruntime_session
1479${onnxruntime_libs}
1480# CUDA is dynamically loaded at runtime
1481onnxruntime_optimizer
1482onnxruntime_providers
1483onnxruntime_util
1484onnxruntime_framework
1485onnxruntime_util
1486onnxruntime_graph
1487${ONNXRUNTIME_MLAS_LIBS}
1488onnxruntime_common
1489onnxruntime_flatbuffers
1490)
1491
1492target_link_libraries(onnxruntime_test_trainer PRIVATE
1493${ONNXRUNTIME_TEST_LIBS}
1494${onnxruntime_EXTERNAL_LIBRARIES}
1495)
1496set_target_properties(onnxruntime_test_trainer PROPERTIES FOLDER "ONNXRuntimeTest")
1497endif()
1498endif()
1499
1500if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
1501
1502set(custom_op_src_patterns
1503"${TEST_SRC_DIR}/testdata/custom_op_library/*.h"
1504"${TEST_SRC_DIR}/testdata/custom_op_library/*.cc"
1505"${TEST_SRC_DIR}/testdata/custom_op_library/cpu/cpu_ops.*"
1506)
1507
1508set(custom_op_lib_include ${REPO_ROOT}/include)
1509set(custom_op_lib_option)
1510set(custom_op_lib_link ${GSL_TARGET})
1511
1512if (onnxruntime_USE_CUDA)
1513list(APPEND custom_op_src_patterns
1514"${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/cuda_ops.cu"
1515"${TEST_SRC_DIR}/testdata/custom_op_library/cuda/cuda_ops.*")
1516list(APPEND custom_op_lib_include ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES} ${CUDNN_INCLUDE_DIR})
1517if (HAS_QSPECTRE)
1518list(APPEND custom_op_lib_option "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /Qspectre>")
1519endif()
1520endif()
1521
1522if (onnxruntime_USE_ROCM)
1523list(APPEND custom_op_src_patterns
1524"${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/rocm_ops.hip"
1525"${TEST_SRC_DIR}/testdata/custom_op_library/rocm/rocm_ops.*")
1526list(APPEND custom_op_lib_include ${onnxruntime_ROCM_HOME}/include)
1527list(APPEND custom_op_lib_option "-D__HIP_PLATFORM_AMD__=1 -D__HIP_PLATFORM_HCC__=1")
1528endif()
1529
1530file(GLOB custom_op_src ${custom_op_src_patterns})
1531onnxruntime_add_shared_library(custom_op_library ${custom_op_src})
1532target_compile_options(custom_op_library PRIVATE ${custom_op_lib_option})
1533target_include_directories(custom_op_library PRIVATE ${REPO_ROOT}/include ${custom_op_lib_include})
1534target_link_libraries(custom_op_library PRIVATE ${GSL_TARGET} ${custom_op_lib_link})
1535
1536if(UNIX)
1537if (APPLE)
1538set(ONNXRUNTIME_CUSTOM_OP_LIB_LINK_FLAG "-Xlinker -dead_strip")
1539elseif(NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
1540set(ONNXRUNTIME_CUSTOM_OP_LIB_LINK_FLAG "-Xlinker --version-script=${TEST_SRC_DIR}/testdata/custom_op_library/custom_op_library.lds -Xlinker --no-undefined -Xlinker --gc-sections -z noexecstack")
1541endif()
1542else()
1543set(ONNXRUNTIME_CUSTOM_OP_LIB_LINK_FLAG "-DEF:${TEST_SRC_DIR}/testdata/custom_op_library/custom_op_library.def")
1544if (NOT onnxruntime_USE_CUDA)
1545target_compile_options(custom_op_library PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /wd26409>"
1546"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd26409>")
1547endif()
1548endif()
1549set_property(TARGET custom_op_library APPEND_STRING PROPERTY LINK_FLAGS ${ONNXRUNTIME_CUSTOM_OP_LIB_LINK_FLAG})
1550
1551if (NOT onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)
1552if (onnxruntime_BUILD_JAVA AND NOT onnxruntime_ENABLE_STATIC_ANALYSIS)
1553message(STATUS "Running Java tests")
1554# native-test is added to resources so custom_op_lib can be loaded
1555# and we want to symlink it there
1556set(JAVA_NATIVE_TEST_DIR ${JAVA_OUTPUT_DIR}/native-test)
1557file(MAKE_DIRECTORY ${JAVA_NATIVE_TEST_DIR})
1558
1559# delegate to gradle's test runner
1560if(WIN32)
1561add_custom_command(TARGET custom_op_library POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:custom_op_library>
1562${JAVA_NATIVE_TEST_DIR}/$<TARGET_FILE_NAME:custom_op_library>)
1563# On windows ctest requires a test to be an .exe(.com) file
1564# With gradle wrapper we get gradlew.bat. We delegate execution to a separate .cmake file
1565# That can handle both .exe and .bat
1566add_test(NAME onnxruntime4j_test COMMAND ${CMAKE_COMMAND}
1567-DGRADLE_EXECUTABLE=${GRADLE_EXECUTABLE}
1568-DBIN_DIR=${CMAKE_CURRENT_BINARY_DIR}
1569-DREPO_ROOT=${REPO_ROOT}
1570${ORT_PROVIDER_FLAGS}
1571-P ${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime_java_unittests.cmake)
1572else()
1573add_custom_command(TARGET custom_op_library POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:custom_op_library>
1574${JAVA_NATIVE_TEST_DIR}/$<TARGET_LINKER_FILE_NAME:custom_op_library>)
1575if (onnxruntime_ENABLE_TRAINING_APIS)
1576message(STATUS "Running Java inference and training tests")
1577add_test(NAME onnxruntime4j_test COMMAND ${GRADLE_EXECUTABLE} cmakeCheck -DcmakeBuildDir=${CMAKE_CURRENT_BINARY_DIR} ${ORT_PROVIDER_FLAGS} -DENABLE_TRAINING_APIS=1
1578WORKING_DIRECTORY ${REPO_ROOT}/java)
1579else()
1580message(STATUS "Running Java inference tests only")
1581add_test(NAME onnxruntime4j_test COMMAND ${GRADLE_EXECUTABLE} cmakeCheck -DcmakeBuildDir=${CMAKE_CURRENT_BINARY_DIR} ${ORT_PROVIDER_FLAGS}
1582WORKING_DIRECTORY ${REPO_ROOT}/java)
1583endif()
1584endif()
1585set_property(TEST onnxruntime4j_test APPEND PROPERTY DEPENDS onnxruntime4j_jni)
1586endif()
1587endif()
1588
1589if (onnxruntime_BUILD_SHARED_LIB AND (NOT onnxruntime_MINIMAL_BUILD OR onnxruntime_MINIMAL_BUILD_CUSTOM_OPS))
1590set (onnxruntime_customopregistration_test_SRC
1591${ONNXRUNTIME_CUSTOM_OP_REGISTRATION_TEST_SRC_DIR}/test_registercustomops.cc)
1592
1593set(onnxruntime_customopregistration_test_LIBS custom_op_library onnxruntime_common onnxruntime_test_utils)
1594if (NOT WIN32)
1595list(APPEND onnxruntime_customopregistration_test_LIBS nsync::nsync_cpp)
1596endif()
1597if (CPUINFO_SUPPORTED AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
1598list(APPEND onnxruntime_customopregistration_test_LIBS cpuinfo)
1599endif()
1600if (onnxruntime_USE_TENSORRT)
1601list(APPEND onnxruntime_customopregistration_test_LIBS ${TENSORRT_LIBRARY_INFER})
1602endif()
1603if (${CMAKE_SYSTEM_NAME} MATCHES "AIX")
1604list(APPEND onnxruntime_customopregistration_test_LIBS onnxruntime_graph onnxruntime_session onnxruntime_providers onnxruntime_framework onnxruntime_util onnxruntime_mlas onnxruntime_optimizer onnxruntime_flatbuffers iconv re2 libprotobuf-lite onnx_proto nsync_cpp)
1605endif()
1606AddTest(DYN
1607TARGET onnxruntime_customopregistration_test
1608SOURCES ${onnxruntime_customopregistration_test_SRC} ${onnxruntime_unittest_main_src}
1609LIBS ${onnxruntime_customopregistration_test_LIBS}
1610DEPENDS ${all_dependencies}
1611)
1612
1613if (IOS)
1614add_custom_command(
1615TARGET onnxruntime_customopregistration_test POST_BUILD
1616COMMAND ${CMAKE_COMMAND} -E copy_directory
1617${TEST_DATA_SRC}
1618$<TARGET_FILE_DIR:onnxruntime_customopregistration_test>/testdata)
1619endif()
1620
1621if (UNIX AND onnxruntime_USE_TENSORRT)
1622# The test_main.cc includes NvInfer.h where it has many deprecated declarations
1623# simply ignore them for TensorRT EP build
1624set_property(TARGET onnxruntime_customopregistration_test APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-deprecated-declarations")
1625endif()
1626
1627endif()
1628endif()
1629
1630# Build custom op library that returns an error OrtStatus when the exported RegisterCustomOps function is called.
1631if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND (NOT onnxruntime_MINIMAL_BUILD OR onnxruntime_MINIMAL_BUILD_CUSTOM_OPS))
1632onnxruntime_add_shared_library_module(custom_op_invalid_library
1633${TEST_SRC_DIR}/testdata/custom_op_invalid_library/custom_op_library.h
1634${TEST_SRC_DIR}/testdata/custom_op_invalid_library/custom_op_library.cc)
1635target_include_directories(custom_op_invalid_library PRIVATE ${REPO_ROOT}/include/onnxruntime/core/session)
1636
1637if(UNIX)
1638if (APPLE)
1639set(ONNXRUNTIME_CUSTOM_OP_INVALID_LIB_LINK_FLAG "-Xlinker -dead_strip")
1640elseif (NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
1641string(CONCAT ONNXRUNTIME_CUSTOM_OP_INVALID_LIB_LINK_FLAG
1642"-Xlinker --version-script=${TEST_SRC_DIR}/testdata/custom_op_invalid_library/custom_op_library.lds "
1643"-Xlinker --no-undefined -Xlinker --gc-sections -z noexecstack")
1644endif()
1645else()
1646set(ONNXRUNTIME_CUSTOM_OP_INVALID_LIB_LINK_FLAG
1647"-DEF:${TEST_SRC_DIR}/testdata/custom_op_invalid_library/custom_op_library.def")
1648endif()
1649
1650set_property(TARGET custom_op_invalid_library APPEND_STRING PROPERTY LINK_FLAGS
1651${ONNXRUNTIME_CUSTOM_OP_INVALID_LIB_LINK_FLAG})
1652endif()
1653
1654if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND (NOT onnxruntime_MINIMAL_BUILD OR onnxruntime_MINIMAL_BUILD_CUSTOM_OPS))
1655
1656file(GLOB_RECURSE custom_op_get_const_input_test_library_src
1657"${TEST_SRC_DIR}/testdata/custom_op_get_const_input_test_library/custom_op_lib.cc"
1658"${TEST_SRC_DIR}/testdata/custom_op_get_const_input_test_library/custom_op.h"
1659"${TEST_SRC_DIR}/testdata/custom_op_get_const_input_test_library/custom_op.cc"
1660)
1661
1662onnxruntime_add_shared_library_module(custom_op_get_const_input_test_library ${custom_op_get_const_input_test_library_src})
1663
1664onnxruntime_add_include_to_target(custom_op_get_const_input_test_library onnxruntime_common GTest::gtest GTest::gmock)
1665target_include_directories(custom_op_get_const_input_test_library PRIVATE ${REPO_ROOT}/include/onnxruntime/core/session
1666${REPO_ROOT}/include/onnxruntime/core/common)
1667
1668if(UNIX)
1669if (APPLE)
1670set(ONNXRUNTIME_CUSTOM_OP_GET_CONST_INPUT_TEST_LIB_LINK_FLAG "-Xlinker -dead_strip")
1671elseif(NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
1672string(CONCAT ONNXRUNTIME_CUSTOM_OP_GET_CONST_INPUT_TEST_LIB_LINK_FLAG
1673"-Xlinker --version-script=${TEST_SRC_DIR}/testdata/custom_op_get_const_input_test_library/custom_op_lib.lds "
1674"-Xlinker --no-undefined -Xlinker --gc-sections -z noexecstack")
1675endif()
1676else()
1677set(ONNXRUNTIME_CUSTOM_OP_GET_CONST_INPUT_TEST_LIB_LINK_FLAG
1678"-DEF:${TEST_SRC_DIR}/testdata/custom_op_get_const_input_test_library/custom_op_lib.def")
1679endif()
1680
1681set_property(TARGET custom_op_get_const_input_test_library APPEND_STRING PROPERTY LINK_FLAGS
1682${ONNXRUNTIME_CUSTOM_OP_GET_CONST_INPUT_TEST_LIB_LINK_FLAG})
1683endif()
1684
1685if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND (NOT onnxruntime_MINIMAL_BUILD OR onnxruntime_MINIMAL_BUILD_CUSTOM_OPS))
1686
1687file(GLOB_RECURSE custom_op_local_function_test_library_src
1688"${TEST_SRC_DIR}/testdata/custom_op_local_function/custom_op_local_function.cc"
1689"${TEST_SRC_DIR}/testdata/custom_op_local_function/custom_op_local_function.h"
1690"${TEST_SRC_DIR}/testdata/custom_op_local_function/dummy_gemm.cc"
1691"${TEST_SRC_DIR}/testdata/custom_op_local_function/dummy_gemm.h"
1692)
1693
1694onnxruntime_add_shared_library_module(custom_op_local_function ${custom_op_local_function_test_library_src})
1695
1696onnxruntime_add_include_to_target(custom_op_local_function onnxruntime_common GTest::gtest GTest::gmock)
1697target_include_directories(custom_op_local_function PRIVATE ${REPO_ROOT}/include/onnxruntime/core/session
1698${REPO_ROOT}/include/onnxruntime/core/common)
1699
1700if(UNIX)
1701if (APPLE)
1702set(ONNXRUNTIME_CUSTOM_OP_lOCAL_FUNCTION_TEST_LIB_LINK_FLAG "-Xlinker -dead_strip")
1703elseif(NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
1704string(CONCAT ONNXRUNTIME_CUSTOM_OP_lOCAL_FUNCTION_TEST_LIB_LINK_FLAG
1705"-Xlinker --version-script=${TEST_SRC_DIR}/testdata/custom_op_local_function/custom_op_local_function.lds "
1706"-Xlinker --no-undefined -Xlinker --gc-sections -z noexecstack")
1707endif()
1708else()
1709set(ONNXRUNTIME_CUSTOM_OP_lOCAL_FUNCTION_TEST_LIB_LINK_FLAG
1710"-DEF:${TEST_SRC_DIR}/testdata/custom_op_local_function/custom_op_local_function.def")
1711endif()
1712
1713set_property(TARGET custom_op_local_function APPEND_STRING PROPERTY LINK_FLAGS
1714${ONNXRUNTIME_CUSTOM_OP_lOCAL_FUNCTION_TEST_LIB_LINK_FLAG})
1715endif()
1716
1717if (onnxruntime_BUILD_SHARED_LIB AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NOT onnxruntime_MINIMAL_BUILD)
1718set (onnxruntime_logging_apis_test_SRC
1719${ONNXRUNTIME_LOGGING_APIS_TEST_SRC_DIR}/test_logging_apis.cc)
1720
1721set(onnxruntime_logging_apis_test_LIBS onnxruntime_common onnxruntime_test_utils)
1722if (${CMAKE_SYSTEM_NAME} MATCHES "AIX")
1723list(APPEND onnxruntime_logging_apis_test_LIBS onnxruntime_session onnxruntime_util onnxruntime_framework onnxruntime_common onnxruntime_graph onnxruntime_providers onnxruntime_mlas onnxruntime_optimizer onnxruntime_flatbuffers iconv re2 libprotobuf-lite onnx_proto nsync_cpp)
1724endif()
1725
1726if(NOT WIN32)
1727list(APPEND onnxruntime_logging_apis_test_LIBS nsync::nsync_cpp ${CMAKE_DL_LIBS})
1728endif()
1729
1730AddTest(DYN
1731TARGET onnxruntime_logging_apis_test
1732SOURCES ${onnxruntime_logging_apis_test_SRC}
1733LIBS ${onnxruntime_logging_apis_test_LIBS}
1734DEPENDS ${all_dependencies}
1735)
1736endif()
1737
1738if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND onnxruntime_USE_OPENVINO AND (NOT onnxruntime_MINIMAL_BUILD OR
1739onnxruntime_MINIMAL_BUILD_CUSTOM_OPS))
1740onnxruntime_add_shared_library_module(custom_op_openvino_wrapper_library
1741${TEST_SRC_DIR}/testdata/custom_op_openvino_wrapper_library/custom_op_lib.cc
1742${TEST_SRC_DIR}/testdata/custom_op_openvino_wrapper_library/openvino_wrapper.cc)
1743target_include_directories(custom_op_openvino_wrapper_library PRIVATE ${REPO_ROOT}/include/onnxruntime/core/session)
1744target_link_libraries(custom_op_openvino_wrapper_library PRIVATE openvino::runtime)
1745
1746if(UNIX)
1747if (APPLE)
1748set(ONNXRUNTIME_CUSTOM_OP_OPENVINO_WRAPPER_LIB_LINK_FLAG "-Xlinker -dead_strip")
1749else()
1750string(CONCAT ONNXRUNTIME_CUSTOM_OP_OPENVINO_WRAPPER_LIB_LINK_FLAG
1751"-Xlinker --version-script=${TEST_SRC_DIR}/testdata/custom_op_openvino_wrapper_library/custom_op_lib.lds "
1752"-Xlinker --no-undefined -Xlinker --gc-sections -z noexecstack")
1753endif()
1754else()
1755set(ONNXRUNTIME_CUSTOM_OP_OPENVINO_WRAPPER_LIB_LINK_FLAG
1756"-DEF:${TEST_SRC_DIR}/testdata/custom_op_openvino_wrapper_library/custom_op_lib.def")
1757endif()
1758
1759set_property(TARGET custom_op_openvino_wrapper_library APPEND_STRING PROPERTY LINK_FLAGS
1760${ONNXRUNTIME_CUSTOM_OP_OPENVINO_WRAPPER_LIB_LINK_FLAG})
1761endif()
1762
1763# limit to only test on windows first, due to a runtime path issue on linux
1764if (NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_EXTENDED_MINIMAL_BUILD
1765AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS|visionOS"
1766AND NOT CMAKE_SYSTEM_NAME STREQUAL "Android"
1767AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten"
1768AND NOT onnxruntime_USE_ROCM)
1769file(GLOB_RECURSE test_execution_provider_srcs
1770"${REPO_ROOT}/onnxruntime/test/testdata/custom_execution_provider_library/*.h"
1771"${REPO_ROOT}/onnxruntime/test/testdata/custom_execution_provider_library/*.cc"
1772"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.h"
1773"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.cc"
1774)
1775
1776onnxruntime_add_shared_library_module(test_execution_provider ${test_execution_provider_srcs})
1777add_dependencies(test_execution_provider onnxruntime_providers_shared onnx ${ABSEIL_LIBS})
1778if (CMAKE_SYSTEM_NAME MATCHES "AIX")
1779target_link_options(test_execution_provider PRIVATE -Wl,-brtl -lonnxruntime_providers_shared)
1780target_link_libraries(test_execution_provider PRIVATE ${ABSEIL_LIBS} Boost::mp11)
1781else()
1782target_link_libraries(test_execution_provider PRIVATE onnxruntime_providers_shared ${ABSEIL_LIBS} Boost::mp11)
1783endif()
1784target_include_directories(test_execution_provider PRIVATE $<TARGET_PROPERTY:onnx,INTERFACE_INCLUDE_DIRECTORIES>)
1785target_include_directories(test_execution_provider PRIVATE $<TARGET_PROPERTY:onnxruntime_common,INTERFACE_INCLUDE_DIRECTORIES>)
1786target_include_directories(test_execution_provider PRIVATE ${ONNXRUNTIME_ROOT} ${CMAKE_CURRENT_BINARY_DIR} ${ORTTRAINING_ROOT})
1787if (onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)
1788target_link_libraries(test_execution_provider PRIVATE Python::Python)
1789endif()
1790if(APPLE)
1791set_property(TARGET test_execution_provider APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker -exported_symbols_list ${REPO_ROOT}/onnxruntime/test/testdata/custom_execution_provider_library/exported_symbols.lst")
1792elseif(UNIX)
1793if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
1794set_property(TARGET test_execution_provider APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker --version-script=${REPO_ROOT}/onnxruntime/test/testdata/custom_execution_provider_library/version_script.lds -Xlinker --gc-sections -Xlinker -rpath=\\$ORIGIN")
1795endif()
1796elseif(WIN32)
1797set_property(TARGET test_execution_provider APPEND_STRING PROPERTY LINK_FLAGS "-DEF:${REPO_ROOT}/onnxruntime/test/testdata/custom_execution_provider_library/symbols.def")
1798else()
1799message(FATAL_ERROR "test_execution_provider unknown platform, need to specify shared library exports for it")
1800endif()
1801endif()
1802
1803include(onnxruntime_fuzz_test.cmake)
1804