microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b190dc20aa0a2a00a0b30e00dc1e19eb5d35e90a

Branches

Tags

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

Clone

HTTPS

Download ZIP

cmake/ext_tests.cmake

244lines · 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 xctest_add_test("${ARG_TARGET}_xctest" ${ARG_TARGET})
90
91 target_sources(${ARG_TARGET} PRIVATE
92 ${ARG_TEST_SOURCES}
93 "${TEST_SRC_DIR}/ios/xcgtest.mm")
94
95 target_link_libraries(${ARG_TARGET} PRIVATE
96 ${ARG_LIBRARIES}
97 gtest)
98
99 set(test_data_destination_root_directory $<TARGET_FILE_DIR:${dummy_testee_target}>)
100
101 endif()
102
103 # copy any test data directories to the target directory
104 foreach(test_data_directory ${ARG_TEST_DATA_DIRECTORIES})
105 cmake_path(GET test_data_directory FILENAME test_data_directory_filename)
106 if(NOT test_data_directory_filename)
107 # There's no filename if the path has a trailing directory separator.
108 # In that case, try to get the filename of the parent path.
109 cmake_path(GET test_data_directory PARENT_PATH test_data_directory_parent)
110 cmake_path(GET test_data_directory_parent FILENAME test_data_directory_filename)
111 endif()
112 if(NOT test_data_directory_filename)
113 message(FATAL_ERROR "Failed to get filename for test data directory: ${test_data_directory}")
114 endif()
115
116 set(destination_directory ${test_data_destination_root_directory}/${test_data_directory_filename})
117
118 add_custom_command(
119 TARGET ${ARG_TARGET} POST_BUILD
120 COMMAND ${CMAKE_COMMAND} -E copy_directory
121 ${test_data_directory} ${destination_directory})
122 endforeach()
123
124endfunction(add_test_target)
125
126# -- static test --
127file(GLOB static_TEST_SRC "${TEST_SRC_DIR}/static_test/*.cc")
128
129add_test_target(TARGET ocos_test
130 TEST_SOURCES ${static_TEST_SRC}
131 LIBRARIES ortcustomops ${ocos_libraries})
132target_compile_definitions(ocos_test PRIVATE ${OCOS_COMPILE_DEFINITIONS})
133
134if (OCOS_ENABLE_C_API AND OCOS_BUILD_SHARED_LIB)
135 file(GLOB pp_api_TEST_SRC
136 "${TEST_SRC_DIR}/pp_api_test/*.c"
137 "${TEST_SRC_DIR}/pp_api_test/*.cc"
138 "${TEST_SRC_DIR}/pp_api_test/*.h")
139
140 add_test_target(TARGET pp_api_test
141 TEST_SOURCES ${pp_api_TEST_SRC}
142 LIBRARIES onnxruntime_extensions ${ocos_libraries}
143 TEST_DATA_DIRECTORIES ${TEST_SRC_DIR}/data)
144
145 target_compile_definitions(pp_api_test PRIVATE ${OCOS_COMPILE_DEFINITIONS})
146 target_include_directories(pp_api_test PRIVATE
147 ${PROJECT_SOURCE_DIR}/
148 "$<TARGET_PROPERTY:ortcustomops,INTERFACE_INCLUDE_DIRECTORIES>"
149 "$<TARGET_PROPERTY:ocos_operators,INTERFACE_INCLUDE_DIRECTORIES>")
150
151 if (ORTX_DATA_PATH)
152 file(TO_NATIVE_PATH "${ORTX_DATA_PATH}/tests/data2" _TEST_DATA2)
153 add_custom_command(TARGET pp_api_test POST_BUILD
154 COMMAND ${CMAKE_COMMAND} -E create_symlink ${_TEST_DATA2} ${onnxruntime_extensions_BINARY_DIR}/data2)
155 endif()
156endif()
157
158
159# -- shared test (needs onnxruntime) --
160# avoid blindling searching for onnxruntime library
161# wbhich leads to a unpredictable result
162if (NOT ONNXRUNTIME_LIB_DIR)
163 set(ONNXRUNTIME "ONNXRUNTIME-NOTFOUND")
164else()
165 SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
166 find_library(ONNXRUNTIME onnxruntime HINTS "${ONNXRUNTIME_LIB_DIR}")
167endif()
168
169if (OCOS_BUILD_SHARED_LIB)
170 if("${ONNXRUNTIME}" STREQUAL "ONNXRUNTIME-NOTFOUND")
171 message(WARNING "The prebuilt onnxruntime library was not found, extensions_test will be skipped.")
172 else()
173 block()
174 if(NOT IOS)
175 set(use_extensions_shared_library 1)
176 endif()
177
178 set(extensions_target $<IF:$<BOOL:${use_extensions_shared_library}>,extensions_shared,ortcustomops>)
179
180 file(GLOB shared_TEST_SRC
181 "${TEST_SRC_DIR}/shared_test/*.cc"
182 "${TEST_SRC_DIR}/shared_test/*.hpp")
183
184 set(extensions_test_libraries ${extensions_target} ${ONNXRUNTIME})
185
186 if(use_extensions_shared_library)
187 list(APPEND extensions_test_libraries ${ocos_libraries})
188 endif()
189
190 # needs to link with stdc++fs in Linux
191 if(LINUX)
192 list(APPEND extensions_test_libraries stdc++fs -pthread)
193 endif()
194
195 if (NOT MSVC)
196 list(APPEND extensions_test_libraries ${CMAKE_DL_LIBS})
197 endif()
198
199 add_test_target(TARGET extensions_test
200 TEST_SOURCES ${shared_TEST_SRC}
201 LIBRARIES ${extensions_test_libraries}
202 TEST_DATA_DIRECTORIES ${TEST_SRC_DIR}/data)
203
204 target_include_directories(extensions_test PRIVATE ${spm_INCLUDE_DIRS})
205
206 target_compile_definitions(extensions_test PUBLIC ${OCOS_COMPILE_DEFINITIONS})
207 if(use_extensions_shared_library)
208 target_compile_definitions(extensions_test PUBLIC ORT_EXTENSIONS_UNIT_TEST_USE_EXTENSIONS_SHARED_LIBRARY)
209 endif()
210
211 # FUTURE: This is required to use the ORT C++ API with delayed init which must be done conditionally using
212 # ifdef OCOS_BUILD_SHARED in RegisterCustomOps and where onnxruntime_cxx_api.h is included .
213 # ---
214 # We have to remove the OCOS_BUILD_SHARED when building the test code. It is used to delay population of the
215 # ORT api pointer until RegisterCustomOps is called, but the test code needs to create an ORT env which requires
216 # the pointer to exist.
217 # set(test_compile_definitions ${OCOS_COMPILE_DEFINITIONS})
218 # remove(test_compile_definitions "OCOS_SHARED_LIBRARY")
219 # target_compile_definitions(extensions_test PUBLIC ${test_compile_definitions})
220
221 # Copy onnxruntime DLL files into the same directory as the test binary.
222 if(WIN32)
223 file(TO_CMAKE_PATH "${ONNXRUNTIME_LIB_DIR}/*" ONNXRUNTIME_LIB_FILEPATTERN)
224 file(GLOB ONNXRUNTIME_LIB_FILES CONFIGURE_DEPENDS "${ONNXRUNTIME_LIB_FILEPATTERN}")
225 add_custom_command(
226 TARGET extensions_test POST_BUILD
227 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ONNXRUNTIME_LIB_FILES} $<TARGET_FILE_DIR:extensions_test>)
228 endif()
229
230 # Copy onnxruntime shared library to known location for easy access, e.g., for adb push to emulator or device.
231 if(ANDROID)
232 add_custom_command(
233 TARGET extensions_test POST_BUILD
234 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ONNXRUNTIME} ${CMAKE_BINARY_DIR}/lib
235 )
236 endif()
237
238 if (OCOS_ENABLE_C_API)
239 # avoid copying the same data directory at the same time.
240 add_dependencies(extensions_test pp_api_test)
241 endif()
242 endblock()
243 endif()
244endif()