microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a761fc59b7ace6392d8782fbb2a74888353ecf65

Branches

Tags

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

Clone

HTTPS

Download ZIP

cmake/ext_tests.cmake

202lines · 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 if(OCOS_USE_CUDA)
63 target_link_directories(${ARG_TARGET} PRIVATE $ENV{CUDA_PATH}/lib64)
64 endif()
65
66 set(test_data_destination_root_directory ${onnxruntime_extensions_BINARY_DIR})
67
68 else()
69 # add a test that can run on iOS (simulator)
70 # it requires a dummy testee target
71
72 # create a dummy app to use as the testee target in xctest_add_bundle()
73 set(dummy_testee_target ${ARG_TARGET}_dummy_testee)
74 add_executable(${dummy_testee_target} "${TEST_SRC_DIR}/ios/dummy_testee_main.mm")
75 set(_dummy_testee_version 1)
76 set_target_properties(${dummy_testee_target} PROPERTIES
77 MACOSX_BUNDLE TRUE
78 MACOSX_BUNDLE_BUNDLE_NAME dummy_testee
79 MACOSX_BUNDLE_GUI_IDENTIFIER com.onnxruntime.unittest.dummy_testee
80 MACOSX_BUNDLE_LONG_VERSION_STRING ${_dummy_testee_version}
81 MACOSX_BUNDLE_BUNDLE_VERSION ${_dummy_testee_version}
82 MACOSX_BUNDLE_SHORT_VERSION_STRING ${_dummy_testee_version}
83 XCODE_ATTRIBUTE_CLANG_ENABLE_MODULES "YES"
84 XCODE_ATTRIBUTE_ENABLE_BITCODE "NO"
85 XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO")
86 target_link_libraries(${dummy_testee_target} PRIVATE "-framework UIKit")
87
88 xctest_add_bundle(${ARG_TARGET} ${dummy_testee_target})
89
90 xctest_add_test("${ARG_TARGET}_xctest" ${ARG_TARGET})
91
92 target_sources(${ARG_TARGET} PRIVATE
93 ${ARG_TEST_SOURCES}
94 "${TEST_SRC_DIR}/ios/xcgtest.mm")
95
96 target_link_libraries(${ARG_TARGET} PRIVATE
97 ${ARG_LIBRARIES}
98 gtest gmock)
99
100 set(test_data_destination_root_directory $<TARGET_FILE_DIR:${dummy_testee_target}>)
101
102 endif()
103
104 # copy any test data directories to the target directory
105 foreach(test_data_directory ${ARG_TEST_DATA_DIRECTORIES})
106 cmake_path(GET test_data_directory FILENAME test_data_directory_filename)
107 if(NOT test_data_directory_filename)
108 # There's no filename if the path has a trailing directory separator.
109 # In that case, try to get the filename of the parent path.
110 cmake_path(GET test_data_directory PARENT_PATH test_data_directory_parent)
111 cmake_path(GET test_data_directory_parent FILENAME test_data_directory_filename)
112 endif()
113 if(NOT test_data_directory_filename)
114 message(FATAL_ERROR "Failed to get filename for test data directory: ${test_data_directory}")
115 endif()
116
117 set(destination_directory ${test_data_destination_root_directory}/${test_data_directory_filename})
118
119 add_custom_command(
120 TARGET ${ARG_TARGET} POST_BUILD
121 COMMAND ${CMAKE_COMMAND} -E copy_directory
122 ${test_data_directory} ${destination_directory})
123 endforeach()
124
125endfunction(add_test_target)
126
127# -- static test --
128file(GLOB static_TEST_SRC "${TEST_SRC_DIR}/static_test/*.cc")
129
130add_test_target(TARGET ocos_test
131 TEST_SOURCES ${static_TEST_SRC}
132 LIBRARIES ortcustomops ${ocos_libraries})
133
134# -- shared test (needs onnxruntime) --
135SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
136find_library(ONNXRUNTIME onnxruntime HINTS "${ONNXRUNTIME_LIB_DIR}")
137
138if("${ONNXRUNTIME}" STREQUAL "ONNXRUNTIME-NOTFOUND")
139 message(WARNING "The prebuilt onnxruntime library was not found, extensions_test will be skipped.")
140else()
141 block()
142 if(NOT IOS)
143 set(use_extensions_shared_library 1)
144 endif()
145
146 set(extensions_target $<IF:$<BOOL:${use_extensions_shared_library}>,extensions_shared,ortcustomops>)
147
148 file(GLOB shared_TEST_SRC
149 "${TEST_SRC_DIR}/shared_test/*.cc"
150 "${TEST_SRC_DIR}/shared_test/*.hpp")
151
152 set(extensions_test_libraries ${extensions_target} ${ONNXRUNTIME})
153
154 if(use_extensions_shared_library)
155 list(APPEND extensions_test_libraries ${ocos_libraries})
156 endif()
157
158 # needs to link with stdc++fs in Linux
159 if(LINUX)
160 list(APPEND extensions_test_libraries stdc++fs -pthread)
161 endif()
162
163 add_test_target(TARGET extensions_test
164 TEST_SOURCES ${shared_TEST_SRC}
165 LIBRARIES ${extensions_test_libraries}
166 TEST_DATA_DIRECTORIES ${TEST_SRC_DIR}/data)
167
168 target_include_directories(extensions_test PRIVATE ${spm_INCLUDE_DIRS})
169
170 target_compile_definitions(extensions_test PUBLIC ${OCOS_COMPILE_DEFINITIONS})
171 if(use_extensions_shared_library)
172 target_compile_definitions(extensions_test PUBLIC ORT_EXTENSIONS_UNIT_TEST_USE_EXTENSIONS_SHARED_LIBRARY)
173 endif()
174
175 # FUTURE: This is required to use the ORT C++ API with delayed init which must be done conditionally using
176 # ifdef OCOS_BUILD_SHARED in RegisterCustomOps and where onnxruntime_cxx_api.h is included .
177 # ---
178 # We have to remove the OCOS_BUILD_SHARED when building the test code. It is used to delay population of the
179 # ORT api pointer until RegisterCustomOps is called, but the test code needs to create an ORT env which requires
180 # the pointer to exist.
181 # set(test_compile_definitions ${OCOS_COMPILE_DEFINITIONS})
182 # remove(test_compile_definitions "OCOS_SHARED_LIBRARY")
183 # target_compile_definitions(extensions_test PUBLIC ${test_compile_definitions})
184
185 # Copy onnxruntime DLL files into the same directory as the test binary.
186 if(WIN32)
187 file(TO_CMAKE_PATH "${ONNXRUNTIME_LIB_DIR}/*" ONNXRUNTIME_LIB_FILEPATTERN)
188 file(GLOB ONNXRUNTIME_LIB_FILES CONFIGURE_DEPENDS "${ONNXRUNTIME_LIB_FILEPATTERN}")
189 add_custom_command(
190 TARGET extensions_test POST_BUILD
191 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ONNXRUNTIME_LIB_FILES} $<TARGET_FILE_DIR:extensions_test>)
192 endif()
193
194 # Copy onnxruntime shared library to known location for easy access, e.g., for adb push to emulator or device.
195 if(ANDROID)
196 add_custom_command(
197 TARGET extensions_test POST_BUILD
198 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ONNXRUNTIME} ${CMAKE_BINARY_DIR}/lib
199 )
200 endif()
201 endblock()
202endif()
203