microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
cmake/ext_tests.cmake
235lines · modecode
| 1 | if (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") |
| 4 | endif() |
| 5 | |
| 6 | enable_testing() |
| 7 | |
| 8 | # Enable CTest |
| 9 | message(STATUS "Fetch CTest") |
| 10 | include(CTest) |
| 11 | |
| 12 | set(TEST_SRC_DIR ${PROJECT_SOURCE_DIR}/test) |
| 13 | message(STATUS "Fetch googletest") |
| 14 | include(googletest) |
| 15 | |
| 16 | if(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") |
| 28 | endif() |
| 29 | |
| 30 | function(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 | standardize_output_folder(${ARG_TARGET}) |
| 49 | add_test(NAME ${ARG_TARGET} |
| 50 | COMMAND ${ARG_TARGET}) |
| 51 | target_sources(${ARG_TARGET} PRIVATE |
| 52 | ${ARG_TEST_SOURCES} |
| 53 | "${TEST_SRC_DIR}/unittest_main/test_main.cc") |
| 54 | target_link_libraries(${ARG_TARGET} PRIVATE |
| 55 | ${ARG_LIBRARIES} |
| 56 | gtest) |
| 57 | |
| 58 | if(OCOS_USE_CUDA) |
| 59 | target_link_directories(${ARG_TARGET} PRIVATE ${CUDAToolkit_LIBRARY_DIR}) |
| 60 | endif() |
| 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) |
| 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 | |
| 121 | endfunction(add_test_target) |
| 122 | |
| 123 | # -- static test -- |
| 124 | file(GLOB static_TEST_SRC "${TEST_SRC_DIR}/static_test/*.cc") |
| 125 | |
| 126 | add_test_target(TARGET ocos_test |
| 127 | TEST_SOURCES ${static_TEST_SRC} |
| 128 | LIBRARIES ortcustomops ${ocos_libraries}) |
| 129 | target_compile_definitions(ocos_test PRIVATE ${OCOS_COMPILE_DEFINITIONS}) |
| 130 | |
| 131 | if (OCOS_ENABLE_C_API) |
| 132 | file(GLOB pp_api_TEST_SRC |
| 133 | "${TEST_SRC_DIR}/pp_api_test/*.c" |
| 134 | "${TEST_SRC_DIR}/pp_api_test/*.cc" |
| 135 | "${TEST_SRC_DIR}/pp_api_test/*.h") |
| 136 | |
| 137 | add_test_target(TARGET pp_api_test |
| 138 | TEST_SOURCES ${pp_api_TEST_SRC} |
| 139 | LIBRARIES onnxruntime_extensions ${ocos_libraries} |
| 140 | TEST_DATA_DIRECTORIES ${TEST_SRC_DIR}/data) |
| 141 | |
| 142 | target_compile_definitions(pp_api_test PRIVATE ${OCOS_COMPILE_DEFINITIONS}) |
| 143 | target_include_directories(pp_api_test PRIVATE |
| 144 | ${PROJECT_SOURCE_DIR}/ |
| 145 | "$<TARGET_PROPERTY:ortcustomops,INTERFACE_INCLUDE_DIRECTORIES>" |
| 146 | "$<TARGET_PROPERTY:ocos_operators,INTERFACE_INCLUDE_DIRECTORIES>") |
| 147 | |
| 148 | if (ORTX_DATA_PATH) |
| 149 | file(TO_NATIVE_PATH "${ORTX_DATA_PATH}/tests/data2" _TEST_DATA2) |
| 150 | add_custom_command(TARGET pp_api_test POST_BUILD |
| 151 | COMMAND ${CMAKE_COMMAND} -E create_symlink ${_TEST_DATA2} ${onnxruntime_extensions_BINARY_DIR}/data2) |
| 152 | endif() |
| 153 | endif() |
| 154 | |
| 155 | |
| 156 | # -- shared test (needs onnxruntime) -- |
| 157 | # avoid blindling searching for onnxruntime library |
| 158 | # wbhich leads to a unpredictable result |
| 159 | if (NOT ONNXRUNTIME_LIB_DIR) |
| 160 | set(ONNXRUNTIME "ONNXRUNTIME-NOTFOUND") |
| 161 | else() |
| 162 | SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) |
| 163 | find_library(ONNXRUNTIME onnxruntime HINTS "${ONNXRUNTIME_LIB_DIR}") |
| 164 | endif() |
| 165 | |
| 166 | if("${ONNXRUNTIME}" STREQUAL "ONNXRUNTIME-NOTFOUND") |
| 167 | message(WARNING "The prebuilt onnxruntime library was not found, extensions_test will be skipped.") |
| 168 | else() |
| 169 | block() |
| 170 | if(NOT IOS) |
| 171 | set(use_extensions_shared_library 1) |
| 172 | endif() |
| 173 | |
| 174 | set(extensions_target $<IF:$<BOOL:${use_extensions_shared_library}>,extensions_shared,ortcustomops>) |
| 175 | |
| 176 | file(GLOB shared_TEST_SRC |
| 177 | "${TEST_SRC_DIR}/shared_test/*.cc" |
| 178 | "${TEST_SRC_DIR}/shared_test/*.hpp") |
| 179 | |
| 180 | set(extensions_test_libraries ${extensions_target} ${ONNXRUNTIME}) |
| 181 | |
| 182 | if(use_extensions_shared_library) |
| 183 | list(APPEND extensions_test_libraries ${ocos_libraries}) |
| 184 | endif() |
| 185 | |
| 186 | # needs to link with stdc++fs in Linux |
| 187 | if(LINUX) |
| 188 | list(APPEND extensions_test_libraries stdc++fs -pthread) |
| 189 | endif() |
| 190 | |
| 191 | add_test_target(TARGET extensions_test |
| 192 | TEST_SOURCES ${shared_TEST_SRC} |
| 193 | LIBRARIES ${extensions_test_libraries} |
| 194 | TEST_DATA_DIRECTORIES ${TEST_SRC_DIR}/data) |
| 195 | |
| 196 | target_include_directories(extensions_test PRIVATE ${spm_INCLUDE_DIRS}) |
| 197 | |
| 198 | target_compile_definitions(extensions_test PUBLIC ${OCOS_COMPILE_DEFINITIONS}) |
| 199 | if(use_extensions_shared_library) |
| 200 | target_compile_definitions(extensions_test PUBLIC ORT_EXTENSIONS_UNIT_TEST_USE_EXTENSIONS_SHARED_LIBRARY) |
| 201 | endif() |
| 202 | |
| 203 | # FUTURE: This is required to use the ORT C++ API with delayed init which must be done conditionally using |
| 204 | # ifdef OCOS_BUILD_SHARED in RegisterCustomOps and where onnxruntime_cxx_api.h is included . |
| 205 | # --- |
| 206 | # We have to remove the OCOS_BUILD_SHARED when building the test code. It is used to delay population of the |
| 207 | # ORT api pointer until RegisterCustomOps is called, but the test code needs to create an ORT env which requires |
| 208 | # the pointer to exist. |
| 209 | # set(test_compile_definitions ${OCOS_COMPILE_DEFINITIONS}) |
| 210 | # remove(test_compile_definitions "OCOS_SHARED_LIBRARY") |
| 211 | # target_compile_definitions(extensions_test PUBLIC ${test_compile_definitions}) |
| 212 | |
| 213 | # Copy onnxruntime DLL files into the same directory as the test binary. |
| 214 | if(WIN32) |
| 215 | file(TO_CMAKE_PATH "${ONNXRUNTIME_LIB_DIR}/*" ONNXRUNTIME_LIB_FILEPATTERN) |
| 216 | file(GLOB ONNXRUNTIME_LIB_FILES CONFIGURE_DEPENDS "${ONNXRUNTIME_LIB_FILEPATTERN}") |
| 217 | add_custom_command( |
| 218 | TARGET extensions_test POST_BUILD |
| 219 | COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ONNXRUNTIME_LIB_FILES} $<TARGET_FILE_DIR:extensions_test>) |
| 220 | endif() |
| 221 | |
| 222 | # Copy onnxruntime shared library to known location for easy access, e.g., for adb push to emulator or device. |
| 223 | if(ANDROID) |
| 224 | add_custom_command( |
| 225 | TARGET extensions_test POST_BUILD |
| 226 | COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ONNXRUNTIME} ${CMAKE_BINARY_DIR}/lib |
| 227 | ) |
| 228 | endif() |
| 229 | |
| 230 | if (OCOS_ENABLE_C_API) |
| 231 | # avoid copying the same data directory at the same time. |
| 232 | add_dependencies(extensions_test pp_api_test) |
| 233 | endif() |
| 234 | endblock() |
| 235 | endif() |
| 236 | |