microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
31d2f64786f624be44355241f8740a7b32e212a2

Branches

Tags

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

Clone

HTTPS

Download ZIP

cmake/ext_tests.cmake

198lines · modecode

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