microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c3c5f1cbb135fc9ccc673ae99b31e4ce6a9e2669

Branches

Tags

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

Clone

HTTPS

Download ZIP

cmake/ext_tests.cmake

221lines · modecode

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