microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
CMakeLists.txt
452lines · modecode
| 1 | cmake_minimum_required(VERSION 3.16.0) |
| 2 | project(onnxruntime_extensions LANGUAGES C CXX) |
| 3 | # set(CMAKE_VERBOSE_MAKEFILE ON) |
| 4 | |
| 5 | if(NOT CMAKE_BUILD_TYPE) |
| 6 | message(STATUS "Build type not set - using RelWithDebInfo") |
| 7 | set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose build type: Debug Release RelWithDebInfo." FORCE) |
| 8 | endif() |
| 9 | |
| 10 | |
| 11 | set(CPACK_PACKAGE_NAME "onnxruntime_extensions") |
| 12 | set(CPACK_PACKAGE_VERSION_MAJOR "0") |
| 13 | set(CPACK_PACKAGE_VERSION_MINOR "5") |
| 14 | set(CPACK_PACKAGE_VERSION_PATCH "0") |
| 15 | set(VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}) |
| 16 | |
| 17 | |
| 18 | set(CMAKE_CXX_STANDARD 17) |
| 19 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 20 | set(CMAKE_CXX_EXTENSIONS OFF) |
| 21 | include(CheckCXXCompilerFlag) |
| 22 | include(CheckLanguage) |
| 23 | |
| 24 | option(CC_OPTIMIZE "Allow compiler optimizations, Set to OFF to disable" ON) |
| 25 | option(OCOS_ENABLE_PYTHON "Enable Python component building" OFF) |
| 26 | option(OCOS_ENABLE_CTEST "Enable C++ test" OFF) |
| 27 | option(OCOS_ENABLE_CPP_EXCEPTIONS "Enable C++ Exception" ON) |
| 28 | option(OCOS_ENABLE_TF_STRING "Enable String Operator Set" ON) |
| 29 | option(OCOS_ENABLE_RE2_REGEX "Enable StringRegexReplace and StringRegexSplit" ON) |
| 30 | option(OCOS_ENABLE_GPT2_TOKENIZER "Enable the GPT2 tokenizer building" ON) |
| 31 | option(OCOS_ENABLE_SPM_TOKENIZER "Enable the SentencePiece tokenizer building" ON) |
| 32 | option(OCOS_ENABLE_WORDPIECE_TOKENIZER "Enable the WordpieceTokenizer building" ON) |
| 33 | option(OCOS_ENABLE_BERT_TOKENIZER "Enable the BertTokenizer building" ON) |
| 34 | option(OCOS_ENABLE_BLINGFIRE "Enable operators depending on the Blingfire library" ON) |
| 35 | option(OCOS_ENABLE_MATH "Enable math tensor operators building" ON) |
| 36 | option(OCOS_ENABLE_DLIB "Enable operators like Inverse depending on DLIB" ON) |
| 37 | option(OCOS_ENABLE_OPENCV "Enable operators depending on opencv" ON) |
| 38 | option(OCOS_ENABLE_OPENCV_CODECS "Enable operators depending on opencv imgcodecs" OFF) |
| 39 | option(OCOS_ENABLE_STATIC_LIB "Enable generating static library" OFF) |
| 40 | option(OCOS_ENABLE_SELECTED_OPLIST "Enable including the selected_ops tool file" OFF) |
| 41 | |
| 42 | |
| 43 | function(disable_all_operators) |
| 44 | set(OCOS_ENABLE_RE2_REGEX OFF CACHE INTERNAL "") |
| 45 | set(OCOS_ENABLE_TF_STRING OFF CACHE INTERNAL "") |
| 46 | set(OCOS_ENABLE_WORDPIECE_TOKENIZER OFF CACHE INTERNAL "") |
| 47 | set(OCOS_ENABLE_GPT2_TOKENIZER OFF CACHE INTERNAL "") |
| 48 | set(OCOS_ENABLE_SPM_TOKENIZER OFF CACHE INTERNAL "") |
| 49 | set(OCOS_ENABLE_BERT_TOKENIZER OFF CACHE INTERNAL "") |
| 50 | set(OCOS_ENABLE_BLINGFIRE OFF CACHE INTERNAL "") |
| 51 | set(OCOS_ENABLE_MATH OFF CACHE INTERNAL "") |
| 52 | set(OCOS_ENABLE_DLIB OFF CACHE INTERNAL "") |
| 53 | set(OCOS_ENABLE_OPENCV OFF CACHE INTERNAL "") |
| 54 | endfunction() |
| 55 | |
| 56 | if(NOT CC_OPTIMIZE) |
| 57 | message("!!!THE COMPILER OPTIMIZATION HAS BEEN DISABLED, DEBUG-ONLY!!!") |
| 58 | string(REGEX REPLACE "([\-\/]O[123])" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}") |
| 59 | string(REGEX REPLACE "([\-\/]O[123])" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") |
| 60 | string(REGEX REPLACE "([\-\/]O[123])" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") |
| 61 | string(REGEX REPLACE "([\-\/]O[123])" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") |
| 62 | |
| 63 | if (NOT WIN32) |
| 64 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0") |
| 65 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0") |
| 66 | else() |
| 67 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Od") |
| 68 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Od") |
| 69 | endif() |
| 70 | endif() |
| 71 | |
| 72 | # Build the libraries with -fPIC |
| 73 | set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 74 | |
| 75 | set_property(GLOBAL PROPERTY USE_FOLDERS ON) |
| 76 | |
| 77 | set(CMAKE_FIND_FRAMEWORK NEVER CACHE STRING "...") |
| 78 | if(NOT "${CMAKE_FIND_FRAMEWORK}" STREQUAL "NEVER") |
| 79 | message(FATAL_ERROR "CMAKE_FIND_FRAMEWORK is not NEVER") |
| 80 | endif() |
| 81 | |
| 82 | # External dependencies |
| 83 | list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/externals ${PROJECT_SOURCE_DIR}/cmake) |
| 84 | |
| 85 | if (OCOS_ENABLE_SELECTED_OPLIST) |
| 86 | # Need to ensure _selectedoplist.cmake file is already generated in folder: ${PROJECT_SOURCE_DIR}/cmake/ |
| 87 | # You could run gen_selectedops.py in folder: tools/ to generate _selectedoplist.cmake |
| 88 | message(STATUS "Looking for the _selectedoplist.cmake") |
| 89 | disable_all_operators() |
| 90 | include(_selectedoplist) |
| 91 | endif() |
| 92 | |
| 93 | if(NOT OCOS_ENABLE_CPP_EXCEPTIONS) |
| 94 | include(noexcep_ops) |
| 95 | add_compile_definitions(OCOS_NO_EXCEPTIONS ORT_NO_EXCEPTIONS) |
| 96 | if(MSVC) |
| 97 | string(REGEX REPLACE "/EHsc" "/EHs-c-" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 98 | add_compile_definitions("_HAS_EXCEPTIONS=0") |
| 99 | else() |
| 100 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables") |
| 101 | endif() |
| 102 | endif() |
| 103 | |
| 104 | include(FetchContent) |
| 105 | |
| 106 | if (OCOS_ENABLE_RE2_REGEX) |
| 107 | if (NOT TARGET re2::re2) |
| 108 | set(RE2_BUILD_TESTING OFF CACHE INTERNAL "") |
| 109 | message(STATUS "Fetch googlere2") |
| 110 | include(googlere2) |
| 111 | endif() |
| 112 | |
| 113 | if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten") |
| 114 | set_property(TARGET re2 PROPERTY COMPILE_OPTIONS) |
| 115 | endif() |
| 116 | endif() |
| 117 | |
| 118 | macro(standardize_output_folder bin_target) |
| 119 | set_target_properties(${bin_target} PROPERTIES |
| 120 | RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" |
| 121 | LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" |
| 122 | ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" |
| 123 | PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") |
| 124 | endmacro() |
| 125 | |
| 126 | |
| 127 | #### scan all source files |
| 128 | |
| 129 | file(GLOB TARGET_SRC "operators/*.cc" "operators/*.h") |
| 130 | if (OCOS_ENABLE_TF_STRING) |
| 131 | set(farmhash_SOURCE_DIR ${PROJECT_SOURCE_DIR}/cmake/externals/farmhash) |
| 132 | file(GLOB TARGET_SRC_KERNELS "operators/text/*.cc" "operators/text/*.h*") |
| 133 | file(GLOB TARGET_SRC_HASH "${farmhash_SOURCE_DIR}/src/farmhash.*") |
| 134 | list(APPEND TARGET_SRC ${TARGET_SRC_KERNELS} ${TARGET_SRC_HASH}) |
| 135 | endif() |
| 136 | |
| 137 | if (OCOS_ENABLE_RE2_REGEX) |
| 138 | file(GLOB TARGET_SRC_RE2_KERNELS "operators/text/re2_strings/*.cc" "operators/text/re2_strings/*.h*") |
| 139 | list(APPEND TARGET_SRC ${TARGET_SRC_RE2_KERNELS}) |
| 140 | endif() |
| 141 | |
| 142 | if (OCOS_ENABLE_MATH) |
| 143 | if(OCOS_ENABLE_DLIB) |
| 144 | set(DLIB_ISO_CPP_ONLY ON CACHE INTERNAL "") |
| 145 | set(DLIB_NO_GUI_SUPPORT ON CACHE INTERNAL "") |
| 146 | set(DLIB_USE_CUDA OFF CACHE INTERNAL "") |
| 147 | set(DLIB_USE_LAPACK OFF CACHE INTERNAL "") |
| 148 | set(DLIB_USE_BLAS OFF CACHE INTERNAL "") |
| 149 | include(dlib) |
| 150 | # Ideally, dlib should be included as |
| 151 | # file(GLOB TARGET_SRC_DLIB "${dlib_SOURCE_DIR}/dlib/all/source.cpp") |
| 152 | # To avoid the unintentional using some unwanted component, only include |
| 153 | file(GLOB TARGET_SRC_DLIB "${dlib_SOURCE_DIR}/dlib/test_for_odr_violations.cpp") |
| 154 | file(GLOB TARGET_SRC_INVERSE "operators/math/dlib/*.cc" "operators/math/dlib/*.h*") |
| 155 | endif() |
| 156 | file(GLOB TARGET_SRC_MATH "operators/math/*.cc" "operators/math/*.h*") |
| 157 | list(APPEND TARGET_SRC ${TARGET_SRC_MATH} ${TARGET_SRC_DLIB} ${TARGET_SRC_INVERSE}) |
| 158 | endif() |
| 159 | |
| 160 | if (OCOS_ENABLE_OPENCV) |
| 161 | message(STATUS "Fetch opencv") |
| 162 | include(opencv) |
| 163 | file(GLOB TARGET_SRC_CV "operators/cv2/*.cc" "operators/cv2/*.h*") |
| 164 | list(APPEND TARGET_SRC ${TARGET_SRC_CV}) |
| 165 | endif() |
| 166 | |
| 167 | set(_HAS_TOKENIZER OFF) |
| 168 | if (OCOS_ENABLE_GPT2_TOKENIZER) |
| 169 | # GPT2 |
| 170 | set(_HAS_TOKENIZER ON) |
| 171 | file(GLOB tok_TARGET_SRC "operators/tokenizer/gpt*.cc" "operators/tokenizer/unicode*.*") |
| 172 | list(APPEND TARGET_SRC ${tok_TARGET_SRC}) |
| 173 | endif() |
| 174 | |
| 175 | if (OCOS_ENABLE_SPM_TOKENIZER) |
| 176 | # SentencePiece |
| 177 | set(_HAS_TOKENIZER ON) |
| 178 | set(SPM_ENABLE_TCMALLOC OFF CACHE INTERNAL "") |
| 179 | set(SPM_ENABLE_SHARED OFF CACHE INTERNAL "") |
| 180 | message(STATUS "Fetch sentencepiece") |
| 181 | include(sentencepieceproject) |
| 182 | file(GLOB stpiece_TARGET_SRC "operators/tokenizer/sentencepiece/*.cc" "operators/tokenizer/sentencepiece*") |
| 183 | list(REMOVE_ITEM stpiece_TARGET_SRC INCLUDE REGEX ".*((spm)|(train)).*") |
| 184 | list(APPEND TARGET_SRC ${stpiece_TARGET_SRC}) |
| 185 | endif() |
| 186 | |
| 187 | if (OCOS_ENABLE_WORDPIECE_TOKENIZER) |
| 188 | set(_HAS_TOKENIZER ON) |
| 189 | file(GLOB wordpiece_TARGET_SRC "operators/tokenizer/wordpiece*.*") |
| 190 | list(APPEND TARGET_SRC ${wordpiece_TARGET_SRC}) |
| 191 | endif() |
| 192 | |
| 193 | if (OCOS_ENABLE_BERT_TOKENIZER) |
| 194 | # Bert |
| 195 | set(_HAS_TOKENIZER ON) |
| 196 | file(GLOB bert_TARGET_SRC "operators/tokenizer/basic_tokenizer.*" "operators/tokenizer/bert_tokenizer.*" "operators/tokenizer/bert_tokenizer_decoder.*") |
| 197 | list(APPEND TARGET_SRC ${bert_TARGET_SRC}) |
| 198 | endif() |
| 199 | |
| 200 | if (OCOS_ENABLE_BLINGFIRE) |
| 201 | # blingfire |
| 202 | set(_HAS_TOKENIZER ON) |
| 203 | file(GLOB blingfire_TARGET_SRC "operators/tokenizer/blingfire*.*") |
| 204 | list(APPEND TARGET_SRC ${blingfire_TARGET_SRC}) |
| 205 | endif() |
| 206 | |
| 207 | if (OCOS_ENABLE_GPT2_TOKENIZER OR OCOS_ENABLE_WORDPIECE_TOKENIZER) |
| 208 | if (NOT TARGET nlohmann_json) |
| 209 | set(JSON_BuildTests OFF CACHE INTERNAL "") |
| 210 | message(STATUS "Fetch json") |
| 211 | include(json) |
| 212 | endif() |
| 213 | endif() |
| 214 | |
| 215 | if (_HAS_TOKENIZER) |
| 216 | message(STATUS "Tokenizer needed.") |
| 217 | file(GLOB tokenizer_TARGET_SRC "operators/tokenizer/tokenizers.*") |
| 218 | list(APPEND TARGET_SRC ${tokenizer_TARGET_SRC}) |
| 219 | endif() |
| 220 | |
| 221 | #### make all compile options. |
| 222 | |
| 223 | add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>") |
| 224 | add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>") |
| 225 | add_library(ocos_operators STATIC ${TARGET_SRC}) |
| 226 | standardize_output_folder(ocos_operators) |
| 227 | |
| 228 | target_include_directories(ocos_operators PUBLIC |
| 229 | ${PROJECT_SOURCE_DIR}/includes |
| 230 | ${PROJECT_SOURCE_DIR}/includes/onnxruntime |
| 231 | ${PROJECT_SOURCE_DIR}/operators |
| 232 | ${PROJECT_SOURCE_DIR}/operators/tokenizer) |
| 233 | set(ocos_libraries "") |
| 234 | set(OCOS_COMPILE_DEFINITIONS "") |
| 235 | |
| 236 | if (OCOS_ENABLE_DLIB) |
| 237 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_DLIB) |
| 238 | endif() |
| 239 | |
| 240 | if (_HAS_TOKENIZER) |
| 241 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_TOKENIZER) |
| 242 | endif() |
| 243 | |
| 244 | if (OCOS_ENABLE_TF_STRING) |
| 245 | target_include_directories(ocos_operators PUBLIC |
| 246 | ${googlere2_SOURCE_DIR} |
| 247 | ${farmhash_SOURCE_DIR}/src) |
| 248 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_TF_STRING NOMINMAX FARMHASH_NO_BUILTIN_EXPECT FARMHASH_DEBUG=0) |
| 249 | list(APPEND ocos_libraries re2) |
| 250 | endif() |
| 251 | |
| 252 | if (OCOS_ENABLE_RE2_REGEX) |
| 253 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_RE2_REGEX) |
| 254 | endif() |
| 255 | |
| 256 | if (OCOS_ENABLE_MATH) |
| 257 | target_include_directories(ocos_operators PUBLIC ${dlib_SOURCE_DIR}) |
| 258 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_MATH) |
| 259 | # The dlib matrix implementation is all in the headers, no library compiling needed. |
| 260 | endif() |
| 261 | |
| 262 | if (OCOS_ENABLE_OPENCV) |
| 263 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_OPENCV) |
| 264 | if (OCOS_ENABLE_OPENCV_CODECS) |
| 265 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_OPENCV ENABLE_OPENCV_CODEC) |
| 266 | endif() |
| 267 | list(APPEND ocos_libraries ${opencv_LIBS}) |
| 268 | target_include_directories(ocos_operators PRIVATE ${opencv_INCLUDE_DIRS}) |
| 269 | endif() |
| 270 | |
| 271 | if (OCOS_ENABLE_GPT2_TOKENIZER) |
| 272 | # GPT2 |
| 273 | target_include_directories(ocos_operators PRIVATE ${json_SOURCE_DIR}/single_include) |
| 274 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_GPT2_TOKENIZER) |
| 275 | list(APPEND ocos_libraries nlohmann_json::nlohmann_json) |
| 276 | endif() |
| 277 | |
| 278 | if (OCOS_ENABLE_WORDPIECE_TOKENIZER) |
| 279 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_WORDPIECE_TOKENIZER) |
| 280 | endif() |
| 281 | |
| 282 | if (OCOS_ENABLE_BERT_TOKENIZER) |
| 283 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_BERT_TOKENIZER) |
| 284 | endif() |
| 285 | |
| 286 | if (OCOS_ENABLE_SPM_TOKENIZER) |
| 287 | # SentencePiece |
| 288 | target_include_directories(ocos_operators PUBLIC ${spm_INCLUDE_DIRS}) |
| 289 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_SPM_TOKENIZER) |
| 290 | list(APPEND ocos_libraries sentencepiece-static) |
| 291 | endif() |
| 292 | |
| 293 | if (OCOS_ENABLE_BLINGFIRE) |
| 294 | include(blingfire) |
| 295 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_BLINGFIRE) |
| 296 | list(APPEND ocos_libraries bingfirtinydll_static) |
| 297 | endif() |
| 298 | |
| 299 | if (OCOS_ENABLE_GPT2_TOKENIZER OR OCOS_ENABLE_WORDPIECE_TOKENIZER) |
| 300 | target_include_directories(ocos_operators PRIVATE ${json_SOURCE_DIR}/single_include) |
| 301 | list(APPEND ocos_libraries nlohmann_json::nlohmann_json) |
| 302 | endif() |
| 303 | |
| 304 | list(REMOVE_DUPLICATES OCOS_COMPILE_DEFINITIONS) |
| 305 | target_compile_definitions(ocos_operators PRIVATE ${OCOS_COMPILE_DEFINITIONS}) |
| 306 | target_link_libraries(ocos_operators PRIVATE ${ocos_libraries}) |
| 307 | |
| 308 | file(GLOB shared_TARGET_SRC "shared/*.cc" "shared/*.h") |
| 309 | if(OCOS_ENABLE_PYTHON) |
| 310 | set(Python3_FIND_REGISTRY NEVER CACHE STRING "...") |
| 311 | if(NOT "${Python3_FIND_REGISTRY}" STREQUAL "NEVER") |
| 312 | message(FATAL_ERROR "Python3_FIND_REGISTRY is not NEVER") |
| 313 | endif() |
| 314 | find_package(Python3 COMPONENTS Interpreter Development.Module NumPy) |
| 315 | if (NOT Python3_FOUND) |
| 316 | message(FATAL_ERROR "Python3 or NumPy not found!") |
| 317 | endif() |
| 318 | if (WIN32) |
| 319 | list(APPEND shared_TARGET_SRC "${PROJECT_SOURCE_DIR}/onnxruntime_extensions/ortcustomops.def") |
| 320 | endif() |
| 321 | |
| 322 | file(GLOB TARGET_SRC_PYOPS "pyop/*.cc" "pyop/*.h") |
| 323 | add_library(ortcustomops SHARED ${TARGET_SRC_PYOPS} ${shared_TARGET_SRC}) |
| 324 | standardize_output_folder(ortcustomops) |
| 325 | list(APPEND OCOS_COMPILE_DEFINITIONS PYTHON_OP_SUPPORT) |
| 326 | # building static lib has higher priority |
| 327 | elseif(OCOS_ENABLE_STATIC_LIB) |
| 328 | add_library(ortcustomops STATIC ${shared_TARGET_SRC}) |
| 329 | add_library(onnxruntime_extensions ALIAS ortcustomops) |
| 330 | elseif(CMAKE_SYSTEM_NAME STREQUAL "Emscripten") |
| 331 | add_executable(ortcustomops ${shared_TARGET_SRC}) |
| 332 | set_target_properties(ortcustomops PROPERTIES LINK_FLAGS " \ |
| 333 | -s WASM=1 \ |
| 334 | -s NO_EXIT_RUNTIME=0 \ |
| 335 | -s ALLOW_MEMORY_GROWTH=1 \ |
| 336 | -s SAFE_HEAP=0 \ |
| 337 | -s MODULARIZE=1 \ |
| 338 | -s SAFE_HEAP_LOG=0 \ |
| 339 | -s STACK_OVERFLOW_CHECK=0 \ |
| 340 | -s EXPORT_ALL=0 \ |
| 341 | -s VERBOSE=0 \ |
| 342 | --no-entry") |
| 343 | if (CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 344 | set_property(TARGET ortcustomops APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=1 -s DEMANGLE_SUPPORT=1") |
| 345 | else() |
| 346 | set_property(TARGET ortcustomops APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=0 -s DEMANGLE_SUPPORT=0") |
| 347 | endif() |
| 348 | else() |
| 349 | list(APPEND shared_TARGET_SRC "${PROJECT_SOURCE_DIR}/shared/ortcustomops.def") |
| 350 | add_library(ortcustomops SHARED ${shared_TARGET_SRC}) |
| 351 | if (CMAKE_SYSTEM_NAME STREQUAL "Android") |
| 352 | if (OCOS_ENABLE_SPM_TOKENIZER) |
| 353 | target_link_libraries(ortcustomops PUBLIC log) |
| 354 | set_property(TARGET ortcustomops APPEND_STRING PROPERTY LINK_FLAGS "-Wl,-s -Wl,--version-script -Wl,${PROJECT_SOURCE_DIR}/shared/ortcustomops.ver") |
| 355 | endif() |
| 356 | endif() |
| 357 | standardize_output_folder(ortcustomops) |
| 358 | endif() |
| 359 | |
| 360 | target_compile_definitions(ortcustomops PRIVATE ${OCOS_COMPILE_DEFINITIONS} ${GTEST_CXX_FLAGS}) |
| 361 | target_link_libraries(ortcustomops PUBLIC ocos_operators) |
| 362 | |
| 363 | if(OCOS_ENABLE_PYTHON) |
| 364 | message(STATUS "Fetch pybind11") |
| 365 | include(pybind11) |
| 366 | target_include_directories(ortcustomops PRIVATE |
| 367 | $<TARGET_PROPERTY:Python3::Module,INTERFACE_INCLUDE_DIRECTORIES> |
| 368 | $<TARGET_PROPERTY:Python3::NumPy,INTERFACE_INCLUDE_DIRECTORIES> |
| 369 | ${pybind11_INCLUDE_DIRS} |
| 370 | ) |
| 371 | |
| 372 | target_compile_definitions(ortcustomops PRIVATE |
| 373 | $<TARGET_PROPERTY:Python3::Module,INTERFACE_COMPILE_DEFINITIONS>) |
| 374 | |
| 375 | target_link_libraries(ortcustomops PRIVATE Python3::Module) |
| 376 | |
| 377 | if(NOT "${OCOS_EXTENTION_NAME}" STREQUAL "") |
| 378 | if(NOT WIN32) |
| 379 | set_target_properties(ortcustomops PROPERTIES |
| 380 | LIBRARY_OUTPUT_NAME ${OCOS_EXTENTION_NAME} |
| 381 | PREFIX "" |
| 382 | SUFFIX "") |
| 383 | endif() |
| 384 | endif() |
| 385 | endif() |
| 386 | |
| 387 | # clean up the requirements.txt files from 3rd party project folder to suppress the code security false alarms |
| 388 | file(GLOB_RECURSE NO_USE_FILES ${CMAKE_BINARY_DIR}/_deps/*requirements.txt) |
| 389 | message("Found the follow requirements.txt: ${NO_USE_FILES}") |
| 390 | foreach(nf ${NO_USE_FILES}) |
| 391 | file(TO_NATIVE_PATH ${nf} nf_native) |
| 392 | if (CMAKE_SYSTEM_NAME MATCHES "Windows") |
| 393 | execute_process(COMMAND cmd /c "del ${nf_native}") |
| 394 | else() |
| 395 | execute_process(COMMAND bash -c "rm ${nf_native}") |
| 396 | endif() |
| 397 | endforeach() |
| 398 | |
| 399 | # test section |
| 400 | if (OCOS_ENABLE_CTEST) |
| 401 | # Enable CTest |
| 402 | SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) |
| 403 | find_library(ONNXRUNTIME onnxruntime HINTS "${ONNXRUNTIME_LIB_DIR}") |
| 404 | if (NOT ONNXRUNTIME) |
| 405 | message(FATAL_ERROR "The ctest needs the prebuilt onnxruntime libraries directory, please specify it by ONNXRUNTIME_LIB_DIR.") |
| 406 | endif() |
| 407 | |
| 408 | enable_testing() |
| 409 | message(STATUS "Fetch CTest") |
| 410 | include(CTest) |
| 411 | |
| 412 | set(TEST_SRC_DIR ${PROJECT_SOURCE_DIR}/test) |
| 413 | message(STATUS "Fetch googletest") |
| 414 | include(googletest) |
| 415 | file(GLOB static_TEST_SRC "${TEST_SRC_DIR}/static_test/*.cc") |
| 416 | add_executable(operators_test ${static_TEST_SRC}) |
| 417 | standardize_output_folder(operators_test) |
| 418 | target_link_libraries(operators_test PRIVATE gtest_main ocos_operators ${ocos_libraries}) |
| 419 | add_test(NAME operators_test COMMAND $<TARGET_FILE:operators_test>) |
| 420 | |
| 421 | set(LINUX_CC_FLAGS "") |
| 422 | # needs to link with stdc++fs in Linux |
| 423 | if(UNIX AND NOT APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "Android") |
| 424 | list(APPEND LINUX_CC_FLAGS stdc++fs -pthread) |
| 425 | endif() |
| 426 | |
| 427 | file(GLOB shared_TEST_SRC "${TEST_SRC_DIR}/shared_test/*.cc") |
| 428 | add_executable(ortcustomops_test ${shared_TEST_SRC}) |
| 429 | standardize_output_folder(ortcustomops_test) |
| 430 | if (ONNXRUNTIME_LIB_DIR) |
| 431 | target_link_directories(ortcustomops_test PRIVATE ${ONNXRUNTIME_LIB_DIR}) |
| 432 | endif() |
| 433 | target_link_libraries(ortcustomops_test PRIVATE ortcustomops onnxruntime gtest_main ${ocos_libraries} ${LINUX_CC_FLAGS}) |
| 434 | if (WIN32) |
| 435 | file(TO_CMAKE_PATH "${ONNXRUNTIME_LIB_DIR}/*" ONNXRUNTIME_LIB_FILEPATTERN) |
| 436 | file(GLOB ONNXRUNTIME_LIB_FILES CONFIGURE_DEPENDS "${ONNXRUNTIME_LIB_FILEPATTERN}") |
| 437 | add_custom_command( |
| 438 | TARGET ortcustomops_test POST_BUILD |
| 439 | COMMAND ${CMAKE_COMMAND} -E copy ${ONNXRUNTIME_LIB_FILES} $<TARGET_FILE_DIR:ortcustomops_test>) |
| 440 | endif() |
| 441 | |
| 442 | set(TEST_DATA_SRC ${TEST_SRC_DIR}/data) |
| 443 | set(TEST_DATA_DES ${onnxruntime_extensions_BINARY_DIR}/data) |
| 444 | |
| 445 | # Copy test data from source to destination. |
| 446 | add_custom_command( |
| 447 | TARGET ortcustomops_test POST_BUILD |
| 448 | COMMAND ${CMAKE_COMMAND} -E copy_directory |
| 449 | ${TEST_DATA_SRC} |
| 450 | ${TEST_DATA_DES}) |
| 451 | add_test(NAME ortcustomops_test COMMAND $<TARGET_FILE:ortcustomops_test>) |
| 452 | endif() |
| 453 | |