microsoft/onnxruntime-extensions

Public

mirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
f6940f355009d9e29b08f3185191fa0ab9a8187f

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

cmake/ext_tests.cmake

250lines · modecode

1# Copyright (c) Microsoft Corporation. All rights reserved.
2# Licensed under the MIT License.
3
4if (OCOS_ENABLE_SELECTED_OPLIST)
5 # currently the tests don't handle operator exclusion cleanly.
6 message(FATAL_ERROR "Due to usage of OCOS_ENABLE_SELECTED_OPLIST excluding operators the tests are unable to be built and run")
7endif()
8
9enable_testing()
10
11# Enable CTest
12message(STATUS "Fetch CTest")
13include(CTest)
14
15set(TEST_SRC_DIR ${PROJECT_SOURCE_DIR}/test)
16message(STATUS "Fetch googletest")
17include(googletest)
18
19if(IOS)
20 find_package(XCTest REQUIRED)
21
22 enable_language(OBJCXX)
23
24 # enable ARC on Objective-C++ source files
25 set_property(
26 SOURCE
27 "${TEST_SRC_DIR}/ios/dummy_testee_main.mm"
28 "${TEST_SRC_DIR}/ios/xcgtest.mm"
29 APPEND
30 PROPERTY COMPILE_OPTIONS "-fobjc-arc")
31endif()
32
33function(add_test_target)
34 set(optional_args)
35 set(single_value_args
36 # test target name
37 TARGET)
38 set(multi_value_args
39 # test source files
40 TEST_SOURCES
41 # libraries to link to test target
42 LIBRARIES
43 # test data directories to copy to target directory
44 TEST_DATA_DIRECTORIES)
45 cmake_parse_arguments(ARG "${optional_args}" "${single_value_args}" "${multi_value_args}" ${ARGN})
46
47 if(NOT IOS)
48 # add a test executable
49
50 add_executable(${ARG_TARGET})
51 standardize_output_folder(${ARG_TARGET})
52 add_test(NAME ${ARG_TARGET}
53 COMMAND ${ARG_TARGET})
54 target_sources(${ARG_TARGET} PRIVATE
55 ${ARG_TEST_SOURCES}
56 "${TEST_SRC_DIR}/unittest_main/test_main.cc")
57 target_link_libraries(${ARG_TARGET} PRIVATE
58 ${ARG_LIBRARIES}
59 gtest)
60
61 if(OCOS_USE_CUDA)
62 target_link_directories(${ARG_TARGET} PRIVATE ${CUDAToolkit_LIBRARY_DIR})
63 endif()
64
65 set(test_data_destination_root_directory ${onnxruntime_extensions_BINARY_DIR})
66
67 else()
68 # add a test that can run on iOS (simulator)
69 # it requires a dummy testee target
70
71 # create a dummy app to use as the testee target in xctest_add_bundle()
72 set(dummy_testee_target ${ARG_TARGET}_dummy_testee)
73 add_executable(${dummy_testee_target} "${TEST_SRC_DIR}/ios/dummy_testee_main.mm")
74 set(_dummy_testee_version 1)
75 set_target_properties(${dummy_testee_target} PROPERTIES
76 MACOSX_BUNDLE TRUE
77 MACOSX_BUNDLE_BUNDLE_NAME dummy_testee
78 MACOSX_BUNDLE_GUI_IDENTIFIER com.onnxruntime.unittest.dummy_testee
79 MACOSX_BUNDLE_LONG_VERSION_STRING ${_dummy_testee_version}
80 MACOSX_BUNDLE_BUNDLE_VERSION ${_dummy_testee_version}
81 MACOSX_BUNDLE_SHORT_VERSION_STRING ${_dummy_testee_version}
82 XCODE_ATTRIBUTE_CLANG_ENABLE_MODULES "YES"
83 XCODE_ATTRIBUTE_ENABLE_BITCODE "NO"
84 XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO")
85 target_link_libraries(${dummy_testee_target} PRIVATE "-framework UIKit")
86
87 xctest_add_bundle(${ARG_TARGET} ${dummy_testee_target})
88
89 # Clear LIBRARY_OUTPUT_DIRECTORY to avoid duplicate PlugIns directory issue
90 # See: https://gitlab.kitware.com/cmake/cmake/-/issues/26301
91 set_property(TARGET ${ARG_TARGET} PROPERTY LIBRARY_OUTPUT_DIRECTORY)
92
93 target_sources(${ARG_TARGET} PRIVATE
94 ${ARG_TEST_SOURCES}
95 "${TEST_SRC_DIR}/ios/xcgtest.mm")
96
97 target_link_libraries(${ARG_TARGET} PRIVATE
98 ${ARG_LIBRARIES}
99 gtest)
100
101 set(test_data_destination_root_directory $<TARGET_FILE_DIR:${dummy_testee_target}>)
102
103 endif()
104
105 # copy any test data directories to the target directory
106 foreach(test_data_directory ${ARG_TEST_DATA_DIRECTORIES})
107 cmake_path(GET test_data_directory FILENAME test_data_directory_filename)
108 if(NOT test_data_directory_filename)
109 # There's no filename if the path has a trailing directory separator.
110 # In that case, try to get the filename of the parent path.
111 cmake_path(GET test_data_directory PARENT_PATH test_data_directory_parent)
112 cmake_path(GET test_data_directory_parent FILENAME test_data_directory_filename)
113 endif()
114 if(NOT test_data_directory_filename)
115 message(FATAL_ERROR "Failed to get filename for test data directory: ${test_data_directory}")
116 endif()
117
118 set(destination_directory ${test_data_destination_root_directory}/${test_data_directory_filename})
119
120 add_custom_command(
121 TARGET ${ARG_TARGET} POST_BUILD
122 COMMAND ${CMAKE_COMMAND} -E copy_directory
123 ${test_data_directory} ${destination_directory})
124 endforeach()
125
126endfunction(add_test_target)
127
128# -- static test --
129file(GLOB static_TEST_SRC "${TEST_SRC_DIR}/static_test/*.cc")
130
131if(NOT OCOS_ENABLE_AUDIO)
132 list(FILTER static_TEST_SRC EXCLUDE REGEX "test_nemo_mel")
133endif()
134
135add_test_target(TARGET ocos_test
136 TEST_SOURCES ${static_TEST_SRC}
137 LIBRARIES ortcustomops ${ocos_libraries})
138target_compile_definitions(ocos_test PRIVATE ${OCOS_COMPILE_DEFINITIONS})
139
140if (OCOS_ENABLE_C_API AND OCOS_BUILD_SHARED_LIB)
141 file(GLOB pp_api_TEST_SRC
142 "${TEST_SRC_DIR}/pp_api_test/*.c"
143 "${TEST_SRC_DIR}/pp_api_test/*.cc"
144 "${TEST_SRC_DIR}/pp_api_test/*.h")
145
146 add_test_target(TARGET pp_api_test
147 TEST_SOURCES ${pp_api_TEST_SRC}
148 LIBRARIES onnxruntime_extensions ${ocos_libraries}
149 TEST_DATA_DIRECTORIES ${TEST_SRC_DIR}/data)
150
151 target_compile_definitions(pp_api_test PRIVATE ${OCOS_COMPILE_DEFINITIONS})
152 target_include_directories(pp_api_test PRIVATE
153 ${PROJECT_SOURCE_DIR}/
154 "$<TARGET_PROPERTY:ortcustomops,INTERFACE_INCLUDE_DIRECTORIES>"
155 "$<TARGET_PROPERTY:ocos_operators,INTERFACE_INCLUDE_DIRECTORIES>")
156
157 if (ORTX_DATA_PATH)
158 file(TO_NATIVE_PATH "${ORTX_DATA_PATH}/tests/data2" _TEST_DATA2)
159 add_custom_command(TARGET pp_api_test POST_BUILD
160 COMMAND ${CMAKE_COMMAND} -E create_symlink ${_TEST_DATA2} ${onnxruntime_extensions_BINARY_DIR}/data2)
161 endif()
162endif()
163
164
165# -- shared test (needs onnxruntime) --
166# avoid blindling searching for onnxruntime library
167# wbhich leads to a unpredictable result
168if (NOT ONNXRUNTIME_LIB_DIR)
169 set(ONNXRUNTIME "ONNXRUNTIME-NOTFOUND")
170else()
171 SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
172 find_library(ONNXRUNTIME onnxruntime HINTS "${ONNXRUNTIME_LIB_DIR}")
173endif()
174
175if (OCOS_BUILD_SHARED_LIB)
176 if("${ONNXRUNTIME}" STREQUAL "ONNXRUNTIME-NOTFOUND")
177 message(WARNING "The prebuilt onnxruntime library was not found, extensions_test will be skipped.")
178 else()
179 block()
180 if(NOT IOS)
181 set(use_extensions_shared_library 1)
182 endif()
183
184 set(extensions_target $<IF:$<BOOL:${use_extensions_shared_library}>,extensions_shared,ortcustomops>)
185
186 file(GLOB shared_TEST_SRC
187 "${TEST_SRC_DIR}/shared_test/*.cc"
188 "${TEST_SRC_DIR}/shared_test/*.hpp")
189
190 set(extensions_test_libraries ${extensions_target} ${ONNXRUNTIME})
191
192 if(use_extensions_shared_library)
193 list(APPEND extensions_test_libraries ${ocos_libraries})
194 endif()
195
196 # needs to link with stdc++fs in Linux
197 if(LINUX)
198 list(APPEND extensions_test_libraries stdc++fs -pthread)
199 endif()
200
201 if (NOT MSVC)
202 list(APPEND extensions_test_libraries ${CMAKE_DL_LIBS})
203 endif()
204
205 add_test_target(TARGET extensions_test
206 TEST_SOURCES ${shared_TEST_SRC}
207 LIBRARIES ${extensions_test_libraries}
208 TEST_DATA_DIRECTORIES ${TEST_SRC_DIR}/data)
209
210 target_include_directories(extensions_test PRIVATE ${spm_INCLUDE_DIRS})
211
212 target_compile_definitions(extensions_test PUBLIC ${OCOS_COMPILE_DEFINITIONS})
213 if(use_extensions_shared_library)
214 target_compile_definitions(extensions_test PUBLIC ORT_EXTENSIONS_UNIT_TEST_USE_EXTENSIONS_SHARED_LIBRARY)
215 endif()
216
217 # FUTURE: This is required to use the ORT C++ API with delayed init which must be done conditionally using
218 # ifdef OCOS_BUILD_SHARED in RegisterCustomOps and where onnxruntime_cxx_api.h is included .
219 # ---
220 # We have to remove the OCOS_BUILD_SHARED when building the test code. It is used to delay population of the
221 # ORT api pointer until RegisterCustomOps is called, but the test code needs to create an ORT env which requires
222 # the pointer to exist.
223 # set(test_compile_definitions ${OCOS_COMPILE_DEFINITIONS})
224 # remove(test_compile_definitions "OCOS_SHARED_LIBRARY")
225 # target_compile_definitions(extensions_test PUBLIC ${test_compile_definitions})
226
227 # Copy onnxruntime DLL files into the same directory as the test binary.
228 if(WIN32)
229 file(TO_CMAKE_PATH "${ONNXRUNTIME_LIB_DIR}/*" ONNXRUNTIME_LIB_FILEPATTERN)
230 file(GLOB ONNXRUNTIME_LIB_FILES CONFIGURE_DEPENDS "${ONNXRUNTIME_LIB_FILEPATTERN}")
231 add_custom_command(
232 TARGET extensions_test POST_BUILD
233 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ONNXRUNTIME_LIB_FILES} $<TARGET_FILE_DIR:extensions_test>)
234 endif()
235
236 # Copy onnxruntime shared library to known location for easy access, e.g., for adb push to emulator or device.
237 if(ANDROID)
238 add_custom_command(
239 TARGET extensions_test POST_BUILD
240 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ONNXRUNTIME} ${CMAKE_BINARY_DIR}/lib
241 )
242 endif()
243
244 if (OCOS_ENABLE_C_API)
245 # avoid copying the same data directory at the same time.
246 add_dependencies(extensions_test pp_api_test)
247 endif()
248 endblock()
249 endif()
250endif()
251