microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
CMakeLists.txt
692lines · modecode
| 1 | cmake_minimum_required(VERSION 3.20) |
| 2 | project(onnxruntime_extensions LANGUAGES C CXX) |
| 3 | |
| 4 | # set(CMAKE_VERBOSE_MAKEFILE ON) |
| 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 | function(read_version_file major_var minor_var patch_var) |
| 11 | set(version_file ${PROJECT_SOURCE_DIR}/version.txt) |
| 12 | file(READ ${version_file} version_file_content) |
| 13 | if(version_file_content MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)[\n]?$") |
| 14 | set(${major_var} ${CMAKE_MATCH_1} PARENT_SCOPE) |
| 15 | set(${minor_var} ${CMAKE_MATCH_2} PARENT_SCOPE) |
| 16 | set(${patch_var} ${CMAKE_MATCH_3} PARENT_SCOPE) |
| 17 | else() |
| 18 | message(FATAL_ERROR "Failed to parse version from file: ${version_file}") |
| 19 | endif() |
| 20 | endfunction() |
| 21 | |
| 22 | set(CPACK_PACKAGE_NAME "onnxruntime_extensions") |
| 23 | # Configure CPack to generate a NuGet package |
| 24 | set(CPACK_GENERATOR "NuGet") |
| 25 | set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "onnxruntime-extensions: a pre-/post-processing library for ONNX Runtime") |
| 26 | read_version_file(CPACK_PACKAGE_VERSION_MAJOR CPACK_PACKAGE_VERSION_MINOR CPACK_PACKAGE_VERSION_PATCH) |
| 27 | set(VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}) |
| 28 | |
| 29 | # Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24: |
| 30 | if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0") |
| 31 | cmake_policy(SET CMP0135 NEW) |
| 32 | cmake_policy(SET CMP0077 NEW) |
| 33 | endif() |
| 34 | |
| 35 | # Needed for Java |
| 36 | set(CMAKE_C_STANDARD 99) |
| 37 | |
| 38 | set(CMAKE_CXX_STANDARD 17) |
| 39 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 40 | set(CMAKE_CXX_EXTENSIONS OFF) |
| 41 | include(CheckCXXCompilerFlag) |
| 42 | include(CheckLanguage) |
| 43 | |
| 44 | option(CC_OPTIMIZE "Allow compiler optimizations, Set to OFF to disable" ON) |
| 45 | option(OCOS_ENABLE_PYTHON "Enable Python component building, (deprecated)" OFF) |
| 46 | option(OCOS_ENABLE_CTEST "Enable C++ test" OFF) |
| 47 | option(OCOS_ENABLE_CPP_EXCEPTIONS "Enable C++ Exception" ON) |
| 48 | option(OCOS_ENABLE_TF_STRING "Enable String Operator Set" ON) |
| 49 | option(OCOS_ENABLE_RE2_REGEX "Enable StringRegexReplace and StringRegexSplit" ON) |
| 50 | option(OCOS_ENABLE_GPT2_TOKENIZER "Enable the GPT2 tokenizer building" ON) |
| 51 | option(OCOS_ENABLE_SPM_TOKENIZER "Enable the SentencePiece tokenizer building" ON) |
| 52 | option(OCOS_ENABLE_WORDPIECE_TOKENIZER "Enable the WordpieceTokenizer building" ON) |
| 53 | option(OCOS_ENABLE_BERT_TOKENIZER "Enable the BertTokenizer building" ON) |
| 54 | option(OCOS_ENABLE_BLINGFIRE "Enable operators depending on the Blingfire library" ON) |
| 55 | option(OCOS_ENABLE_MATH "Enable math tensor operators building" ON) |
| 56 | option(OCOS_ENABLE_DLIB "Enable operators like Inverse depending on DLIB" ON) |
| 57 | option(OCOS_ENABLE_OPENCV_CODECS "Enable cv2 and vision operators that require opencv imgcodecs." ON) |
| 58 | option(OCOS_ENABLE_CV2 "Enable the operators in `operators/cv2`" ON) |
| 59 | option(OCOS_ENABLE_VISION "Enable the operators in `operators/vision`" ON) |
| 60 | option(OCOS_ENABLE_AUDIO "Enable the operators for audio processing" ON) |
| 61 | |
| 62 | option(OCOS_ENABLE_STATIC_LIB "Enable generating static library" OFF) |
| 63 | option(OCOS_ENABLE_SELECTED_OPLIST "Enable including the selected_ops tool file" OFF) |
| 64 | option(OCOS_BUILD_PYTHON "Enable building the Python package" OFF) |
| 65 | option(OCOS_BUILD_JAVA "Enable building the Java package" OFF) |
| 66 | option(OCOS_BUILD_ANDROID "Enable building the Android package" OFF) |
| 67 | option(OCOS_BUILD_APPLE_FRAMEWORK "Enable building of the MacOS/iOS framework" OFF) |
| 68 | |
| 69 | function(disable_all_operators) |
| 70 | set(OCOS_ENABLE_RE2_REGEX OFF CACHE INTERNAL "" FORCE) |
| 71 | set(OCOS_ENABLE_TF_STRING OFF CACHE INTERNAL "" FORCE) |
| 72 | set(OCOS_ENABLE_WORDPIECE_TOKENIZER OFF CACHE INTERNAL "" FORCE) |
| 73 | set(OCOS_ENABLE_GPT2_TOKENIZER OFF CACHE INTERNAL "" FORCE) |
| 74 | set(OCOS_ENABLE_SPM_TOKENIZER OFF CACHE INTERNAL "" FORCE) |
| 75 | set(OCOS_ENABLE_BERT_TOKENIZER OFF CACHE INTERNAL "" FORCE) |
| 76 | set(OCOS_ENABLE_BLINGFIRE OFF CACHE INTERNAL "" FORCE) |
| 77 | set(OCOS_ENABLE_MATH OFF CACHE INTERNAL "" FORCE) |
| 78 | set(OCOS_ENABLE_DLIB OFF CACHE INTERNAL "" FORCE) |
| 79 | set(OCOS_ENABLE_OPENCV_CODECS OFF CACHE INTERNAL "" FORCE) |
| 80 | set(OCOS_ENABLE_CV2 OFF CACHE INTERNAL "" FORCE) |
| 81 | set(OCOS_ENABLE_VISION OFF CACHE INTERNAL "" FORCE) |
| 82 | endfunction() |
| 83 | |
| 84 | if (CMAKE_GENERATOR_PLATFORM) |
| 85 | # Multi-platform generator |
| 86 | set(ocos_target_platform ${CMAKE_GENERATOR_PLATFORM}) |
| 87 | else() |
| 88 | set(ocos_target_platform ${CMAKE_SYSTEM_PROCESSOR}) |
| 89 | endif() |
| 90 | |
| 91 | if(NOT CC_OPTIMIZE) |
| 92 | message("!!!THE COMPILER OPTIMIZATION HAS BEEN DISABLED, DEBUG-ONLY!!!") |
| 93 | string(REGEX REPLACE "([\-\/]O[123])" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}") |
| 94 | string(REGEX REPLACE "([\-\/]O[123])" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") |
| 95 | string(REGEX REPLACE "([\-\/]O[123])" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") |
| 96 | string(REGEX REPLACE "([\-\/]O[123])" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") |
| 97 | |
| 98 | if(NOT WIN32) |
| 99 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0") |
| 100 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0") |
| 101 | else() |
| 102 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Od") |
| 103 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Od") |
| 104 | endif() |
| 105 | endif() |
| 106 | |
| 107 | if (MSVC) |
| 108 | check_cxx_compiler_flag(-sdl HAS_SDL) |
| 109 | check_cxx_compiler_flag(-Qspectre HAS_QSPECTRE) |
| 110 | if (HAS_QSPECTRE) |
| 111 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Qspectre") |
| 112 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qspectre") |
| 113 | endif() |
| 114 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DYNAMICBASE") |
| 115 | check_cxx_compiler_flag(-guard:cf HAS_GUARD_CF) |
| 116 | if (HAS_GUARD_CF) |
| 117 | set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /guard:cf") |
| 118 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /guard:cf") |
| 119 | set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /guard:cf") |
| 120 | set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /guard:cf") |
| 121 | set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /guard:cf") |
| 122 | set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /guard:cf") |
| 123 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /guard:cf") |
| 124 | endif() |
| 125 | endif() |
| 126 | |
| 127 | if(NOT OCOS_BUILD_PYTHON AND OCOS_ENABLE_PYTHON) |
| 128 | message("OCOS_ENABLE_PYTHON IS DEPRECATED, USE OCOS_BUILD_PYTHON INSTEAD") |
| 129 | set(OCOS_BUILD_PYTHON ON CACHE INTERNAL "") |
| 130 | endif() |
| 131 | |
| 132 | if(OCOS_BUILD_ANDROID) |
| 133 | if(NOT CMAKE_TOOLCHAIN_FILE MATCHES "android.toolchain.cmake") |
| 134 | message(FATAL_ERROR "CMAKE_TOOLCHAIN_FILE must be set to build/cmake/android.toolchain.cmake from the Android NDK.") |
| 135 | endif() |
| 136 | if(NOT ANDROID_PLATFORM OR NOT ANDROID_ABI) |
| 137 | message(FATAL_ERROR "The Android platform (ANDROID_PLATFORM) and ABI (ANDROID_ABI) must be specified.") |
| 138 | endif() |
| 139 | |
| 140 | set(OCOS_BUILD_JAVA ON CACHE INTERNAL "") |
| 141 | endif() |
| 142 | |
| 143 | # Build the libraries with -fPIC |
| 144 | set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 145 | |
| 146 | set_property(GLOBAL PROPERTY USE_FOLDERS ON) |
| 147 | |
| 148 | set(CMAKE_FIND_FRAMEWORK NEVER CACHE STRING "...") |
| 149 | |
| 150 | if(NOT "${CMAKE_FIND_FRAMEWORK}" STREQUAL "NEVER") |
| 151 | message(FATAL_ERROR "CMAKE_FIND_FRAMEWORK is not NEVER") |
| 152 | endif() |
| 153 | |
| 154 | # External dependencies |
| 155 | list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/externals ${PROJECT_SOURCE_DIR}/cmake) |
| 156 | |
| 157 | # PROJECT_IS_TOP_LEVEL is available since 3.21 |
| 158 | get_property(not_top DIRECTORY PROPERTY PARENT_DIRECTORY) |
| 159 | if(not_top AND ONNXRUNTIME_ROOT) |
| 160 | set(_ONNXRUNTIME_EMBEDDED TRUE) |
| 161 | endif() |
| 162 | |
| 163 | if(OCOS_ENABLE_SELECTED_OPLIST) |
| 164 | # Need to ensure _selectedoplist.cmake file is already generated in folder: ${PROJECT_SOURCE_DIR}/cmake/ |
| 165 | # You could run gen_selectedops.py in folder: tools/ to generate _selectedoplist.cmake |
| 166 | message(STATUS "Looking for the _selectedoplist.cmake") |
| 167 | disable_all_operators() |
| 168 | include(_selectedoplist) |
| 169 | endif() |
| 170 | |
| 171 | set(_OCOS_EXCEPTIONS_REQUIRED OFF) |
| 172 | if (OCOS_ENABLE_GPT2_TOKENIZER OR |
| 173 | OCOS_ENABLE_WORDPIECE_TOKENIZER OR |
| 174 | OCOS_ENABLE_BLINGFIRE OR |
| 175 | OCOS_ENABLE_SPM_TOKENIZER OR |
| 176 | (OCOS_ENABLE_CV2 OR OCOS_ENABLE_OPENCV_CODECS OR OCOS_ENABLE_VISION)) |
| 177 | set(_OCOS_EXCEPTIONS_REQUIRED ON) |
| 178 | endif() |
| 179 | |
| 180 | # Special case an embedded build with ORT exceptions disabled but custom ops that require exceptions. |
| 181 | # Allow using an override so we can do a direct build of ort-ext in a CI without having to embed it in an ORT build. |
| 182 | set(_OCOS_PREVENT_EXCEPTION_PROPAGATION OFF) |
| 183 | if (_OCOS_PREVENT_EXCEPTION_PROPAGATION_OVERRIDE) |
| 184 | set(_OCOS_PREVENT_EXCEPTION_PROPAGATION ${_OCOS_PREVENT_EXCEPTION_PROPAGATION_OVERRIDE}) |
| 185 | elseif(_ONNXRUNTIME_EMBEDDED AND onnxruntime_DISABLE_EXCEPTIONS AND _OCOS_EXCEPTIONS_REQUIRED) |
| 186 | set(_OCOS_PREVENT_EXCEPTION_PROPAGATION ON) |
| 187 | endif() |
| 188 | |
| 189 | if (_OCOS_PREVENT_EXCEPTION_PROPAGATION) |
| 190 | message(STATUS "Embedded build as part of ONNX Runtime with exceptions disabled. " |
| 191 | "Extensions will be built with exceptions enabled due to included custom ops " |
| 192 | "using 3rd party libraries that require exceptions.") |
| 193 | |
| 194 | if (NOT OCOS_ENABLE_CPP_EXCEPTIONS) |
| 195 | message(WARNING "Enabling C++ exception support as custom ops included in the build require them to be enabled.") |
| 196 | set(OCOS_ENABLE_CPP_EXCEPTIONS ON) |
| 197 | endif() |
| 198 | |
| 199 | # undo the flags that ORT has set to disable exceptions. |
| 200 | # see https://github.com/microsoft/onnxruntime/blob/b1abb8c656c597bf221bd85682ae3d9e350d9aba/cmake/adjust_global_compile_flags.cmake#L160-L169 |
| 201 | if(MSVC) |
| 202 | string(REPLACE "/EHs-c-" "/EHsc" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 203 | else() |
| 204 | string(REPLACE "-fno-exceptions" "-fexceptions" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) |
| 205 | string(REPLACE "-fno-unwind-tables" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) |
| 206 | string(REPLACE "-fno-asynchronous-unwind-tables" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) |
| 207 | endif() |
| 208 | |
| 209 | # the ort-ext code has to provide a barrier between the exception enabled custom op code and ORT. |
| 210 | add_compile_definitions(OCOS_PREVENT_EXCEPTION_PROPAGATION) |
| 211 | endif() |
| 212 | |
| 213 | if(NOT OCOS_ENABLE_CPP_EXCEPTIONS) |
| 214 | add_compile_definitions(OCOS_NO_EXCEPTIONS ORT_NO_EXCEPTIONS) |
| 215 | if (NOT _ONNXRUNTIME_EMBEDDED) |
| 216 | add_compile_definitions(_HAS_EXCEPTIONS=0) |
| 217 | endif() |
| 218 | endif() |
| 219 | |
| 220 | include(FetchContent) |
| 221 | |
| 222 | function(set_msvc_c_cpp_compiler_warning_level warning_level) |
| 223 | if (NOT "${warning_level}" MATCHES "^[0-4]$") |
| 224 | message(FATAL_ERROR "Expected warning_level value of 0-4, got '${warning_level}'.") |
| 225 | endif() |
| 226 | |
| 227 | if (MSVC) |
| 228 | set(warning_flag "/W${warning_level}") |
| 229 | get_property(opts DIRECTORY PROPERTY COMPILE_OPTIONS) |
| 230 | # only match the generator expression added by this function |
| 231 | list(FILTER opts |
| 232 | EXCLUDE REGEX "^\\$<\\$<OR:\\$<COMPILE_LANGUAGE:C>,\\$<COMPILE_LANGUAGE:CXX>>:/W[0-4]>$") |
| 233 | list(APPEND opts "$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:${warning_flag}>") |
| 234 | set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "${opts}") |
| 235 | endif() |
| 236 | endfunction() |
| 237 | |
| 238 | # set default MSVC warning level to 3 for external dependencies |
| 239 | set_msvc_c_cpp_compiler_warning_level(3) |
| 240 | include(ext_ortlib) |
| 241 | include(gsl) |
| 242 | |
| 243 | macro(standardize_output_folder bin_target) |
| 244 | set_target_properties(${bin_target} PROPERTIES |
| 245 | RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" |
| 246 | LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" |
| 247 | ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" |
| 248 | PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") |
| 249 | endmacro() |
| 250 | |
| 251 | if(OCOS_ENABLE_RE2_REGEX) |
| 252 | if(NOT TARGET re2::re2) |
| 253 | set(RE2_BUILD_TESTING OFF CACHE INTERNAL "") |
| 254 | message(STATUS "Fetch googlere2") |
| 255 | include(googlere2) |
| 256 | endif() |
| 257 | |
| 258 | if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten") |
| 259 | set_property(TARGET re2 PROPERTY COMPILE_OPTIONS) |
| 260 | endif() |
| 261 | endif() |
| 262 | |
| 263 | # ### scan all source files |
| 264 | file(GLOB TARGET_SRC_NOEXCEPTION "base/*.h" "base/*.cc") |
| 265 | file(GLOB TARGET_SRC "operators/*.cc" "operators/*.h" "includes/*.h*") |
| 266 | |
| 267 | if(OCOS_ENABLE_TF_STRING) |
| 268 | set(farmhash_SOURCE_DIR ${PROJECT_SOURCE_DIR}/cmake/externals/farmhash) |
| 269 | file(GLOB TARGET_SRC_KERNELS "operators/text/*.cc" "operators/text/*.h*") |
| 270 | file(GLOB TARGET_SRC_HASH "${farmhash_SOURCE_DIR}/src/farmhash.*") |
| 271 | list(APPEND TARGET_SRC_NOEXCEPTION ${TARGET_SRC_KERNELS} ${TARGET_SRC_HASH}) |
| 272 | endif() |
| 273 | |
| 274 | if(OCOS_ENABLE_AUDIO) |
| 275 | include(dr_libs) |
| 276 | file(GLOB TARGET_SRC_AUDIO "operators/audio/*.*") |
| 277 | list(APPEND TARGET_SRC_NOEXCEPTION ${TARGET_SRC_AUDIO}) |
| 278 | endif() |
| 279 | |
| 280 | if(OCOS_ENABLE_RE2_REGEX) |
| 281 | file(GLOB TARGET_SRC_RE2_KERNELS "operators/text/re2_strings/*.cc" "operators/text/re2_strings/*.h*") |
| 282 | list(APPEND TARGET_SRC_NOEXCEPTION ${TARGET_SRC_RE2_KERNELS}) |
| 283 | endif() |
| 284 | |
| 285 | if(OCOS_ENABLE_MATH) |
| 286 | if(OCOS_ENABLE_DLIB) |
| 287 | set(DLIB_ISO_CPP_ONLY ON CACHE INTERNAL "") |
| 288 | set(DLIB_NO_GUI_SUPPORT ON CACHE INTERNAL "") |
| 289 | set(DLIB_USE_CUDA OFF CACHE INTERNAL "") |
| 290 | set(DLIB_USE_LAPACK OFF CACHE INTERNAL "") |
| 291 | set(DLIB_USE_BLAS OFF CACHE INTERNAL "") |
| 292 | include(dlib) |
| 293 | |
| 294 | # Ideally, dlib should be included as |
| 295 | # file(GLOB TARGET_SRC_DLIB "${dlib_SOURCE_DIR}/dlib/all/source.cpp") |
| 296 | # To avoid the unintentional using some unwanted component, only include |
| 297 | file(GLOB TARGET_SRC_DLIB "${dlib_SOURCE_DIR}/dlib/test_for_odr_violations.cpp") |
| 298 | file(GLOB TARGET_SRC_INVERSE "operators/math/dlib/*.cc" "operators/math/dlib/*.h*") |
| 299 | endif() |
| 300 | |
| 301 | file(GLOB TARGET_SRC_MATH "operators/math/*.cc" "operators/math/*.h*") |
| 302 | list(APPEND TARGET_SRC ${TARGET_SRC_MATH} ${TARGET_SRC_DLIB} ${TARGET_SRC_INVERSE}) |
| 303 | endif() |
| 304 | |
| 305 | # enable the opencv dependency if we have ops that require it |
| 306 | if(OCOS_ENABLE_CV2 OR OCOS_ENABLE_VISION) |
| 307 | set(_ENABLE_OPENCV ON) |
| 308 | message(STATUS "Fetch opencv") |
| 309 | include(opencv) |
| 310 | endif() |
| 311 | |
| 312 | if(OCOS_ENABLE_CV2) |
| 313 | file(GLOB TARGET_SRC_CV2 "operators/cv2/*.cc" "operators/cv2/*.h*") |
| 314 | file(GLOB TARGET_SRC_CV2_IMGPROC_OPS "operators/cv2/imgproc/*.cc" "operators/cv2/imgproc/*.h*") |
| 315 | file(GLOB TARGET_SRC_CV2_IMGCODECS_OPS "operators/cv2/imgcodecs/*.cc" "operators/cv2/imgcodecs/*.h*") |
| 316 | |
| 317 | list(APPEND TARGET_SRC ${TARGET_SRC_CV2}) |
| 318 | list(APPEND TARGET_SRC ${TARGET_SRC_CV2_IMGPROC_OPS}) |
| 319 | if (OCOS_ENABLE_OPENCV_CODECS) |
| 320 | list(APPEND TARGET_SRC ${TARGET_SRC_CV2_IMGCODECS_OPS}) |
| 321 | endif() |
| 322 | endif() |
| 323 | |
| 324 | if(OCOS_ENABLE_VISION) |
| 325 | if(NOT OCOS_ENABLE_OPENCV_CODECS) |
| 326 | message(FATAL_ERROR "OCOS_ENABLE_VISION requires OCOS_ENABLE_OPENCV_CODECS to be ON") |
| 327 | endif() |
| 328 | |
| 329 | file(GLOB TARGET_SRC_VISION "operators/vision/*.cc" "operators/vision/*.h*") |
| 330 | list(APPEND TARGET_SRC ${TARGET_SRC_VISION}) |
| 331 | endif() |
| 332 | |
| 333 | set(_HAS_TOKENIZER OFF) |
| 334 | |
| 335 | if(OCOS_ENABLE_GPT2_TOKENIZER) |
| 336 | # GPT2 |
| 337 | set(_HAS_TOKENIZER ON) |
| 338 | file(GLOB tok_TARGET_SRC "operators/tokenizer/gpt*.cc" "operators/tokenizer/unicode*.*" "operators/tokenizer/clip*.cc" "operators/tokenizer/roberta*.cc") |
| 339 | list(APPEND TARGET_SRC ${tok_TARGET_SRC}) |
| 340 | endif() |
| 341 | |
| 342 | if(OCOS_ENABLE_SPM_TOKENIZER) |
| 343 | # SentencePiece |
| 344 | set(_HAS_TOKENIZER ON) |
| 345 | set(SPM_ENABLE_TCMALLOC OFF CACHE INTERNAL "") |
| 346 | set(SPM_ENABLE_SHARED OFF CACHE INTERNAL "") |
| 347 | message(STATUS "Fetch sentencepiece") |
| 348 | include(sentencepieceproject) |
| 349 | file(GLOB stpiece_TARGET_SRC "operators/tokenizer/sentencepiece/*.cc" "operators/tokenizer/sentencepiece*") |
| 350 | list(REMOVE_ITEM stpiece_TARGET_SRC INCLUDE REGEX ".*((spm)|(train)).*") |
| 351 | list(APPEND TARGET_SRC ${stpiece_TARGET_SRC}) |
| 352 | endif() |
| 353 | |
| 354 | if(OCOS_ENABLE_WORDPIECE_TOKENIZER) |
| 355 | set(_HAS_TOKENIZER ON) |
| 356 | file(GLOB wordpiece_TARGET_SRC "operators/tokenizer/wordpiece*.*") |
| 357 | list(APPEND TARGET_SRC ${wordpiece_TARGET_SRC}) |
| 358 | endif() |
| 359 | |
| 360 | if(OCOS_ENABLE_BERT_TOKENIZER) |
| 361 | # Bert |
| 362 | set(_HAS_TOKENIZER ON) |
| 363 | file(GLOB bert_TARGET_SRC "operators/tokenizer/basic_tokenizer.*" "operators/tokenizer/bert_tokenizer.*" "operators/tokenizer/bert_tokenizer_decoder.*") |
| 364 | list(APPEND TARGET_SRC ${bert_TARGET_SRC}) |
| 365 | endif() |
| 366 | |
| 367 | if(OCOS_ENABLE_BLINGFIRE) |
| 368 | # blingfire |
| 369 | set(_HAS_TOKENIZER ON) |
| 370 | file(GLOB blingfire_TARGET_SRC "operators/tokenizer/blingfire*.*") |
| 371 | list(APPEND TARGET_SRC ${blingfire_TARGET_SRC}) |
| 372 | endif() |
| 373 | |
| 374 | if(OCOS_ENABLE_GPT2_TOKENIZER OR OCOS_ENABLE_WORDPIECE_TOKENIZER) |
| 375 | message(STATUS "Fetch json") |
| 376 | include(json) |
| 377 | endif() |
| 378 | |
| 379 | if(_HAS_TOKENIZER) |
| 380 | message(STATUS "Tokenizer needed.") |
| 381 | file(GLOB tokenizer_TARGET_SRC "operators/tokenizer/tokenizers.*" "operators/tokenizer/*.hpp") |
| 382 | list(APPEND TARGET_SRC ${tokenizer_TARGET_SRC}) |
| 383 | endif() |
| 384 | |
| 385 | # ### make all compile options. |
| 386 | add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>") |
| 387 | add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>") |
| 388 | add_library(noexcep_operators STATIC ${TARGET_SRC_NOEXCEPTION}) |
| 389 | add_library(ocos_operators STATIC ${TARGET_SRC}) |
| 390 | # TODO: need to address the SDL warnings happens on custom operator code. |
| 391 | # if (HAS_SDL) |
| 392 | # target_compile_options(ocos_operators PRIVATE "/sdl") |
| 393 | # endif() |
| 394 | set_target_properties(noexcep_operators PROPERTIES FOLDER "operators") |
| 395 | set_target_properties(ocos_operators PROPERTIES FOLDER "operators") |
| 396 | |
| 397 | # filter out any files in ${TARGET_SRC} which don't have prefix of ${PROJECT_SOURCE_DIR} before calling source_group |
| 398 | set(_TARGET_SRC_FOR_SOURCE_GROUP) |
| 399 | foreach(_TARGET_SRC_FILE IN LISTS TARGET_SRC) |
| 400 | cmake_path(IS_PREFIX PROJECT_SOURCE_DIR ${_TARGET_SRC_FILE} |
| 401 | NORMALIZE |
| 402 | _is_prefix_result) |
| 403 | if(_is_prefix_result) |
| 404 | list(APPEND _TARGET_SRC_FOR_SOURCE_GROUP ${_TARGET_SRC_FILE}) |
| 405 | endif() |
| 406 | endforeach() |
| 407 | source_group(TREE ${PROJECT_SOURCE_DIR} FILES ${_TARGET_SRC_FOR_SOURCE_GROUP}) |
| 408 | |
| 409 | standardize_output_folder(noexcep_operators) |
| 410 | standardize_output_folder(ocos_operators) |
| 411 | |
| 412 | target_include_directories(noexcep_operators PUBLIC |
| 413 | ${ONNXRUNTIME_INCLUDE_DIR} |
| 414 | ${PROJECT_SOURCE_DIR}/includes |
| 415 | ${PROJECT_SOURCE_DIR}/base |
| 416 | ${PROJECT_SOURCE_DIR}/operators) |
| 417 | |
| 418 | target_include_directories(ocos_operators PUBLIC |
| 419 | ${ONNXRUNTIME_INCLUDE_DIR} |
| 420 | ${PROJECT_SOURCE_DIR}/includes |
| 421 | ${PROJECT_SOURCE_DIR}/base |
| 422 | ${PROJECT_SOURCE_DIR}/operators) |
| 423 | |
| 424 | set(ocos_libraries) |
| 425 | set(OCOS_COMPILE_DEFINITIONS) |
| 426 | |
| 427 | if(OCOS_ENABLE_DLIB) |
| 428 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_DLIB) |
| 429 | endif() |
| 430 | |
| 431 | if (OCOS_ENABLE_AUDIO) |
| 432 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_DR_LIBS) |
| 433 | target_include_directories(noexcep_operators PUBLIC ${dr_libs_SOURCE_DIR}) |
| 434 | endif() |
| 435 | |
| 436 | if(_HAS_TOKENIZER) |
| 437 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_TOKENIZER) |
| 438 | target_include_directories(ocos_operators PUBLIC |
| 439 | ${PROJECT_SOURCE_DIR}/operators/tokenizer) |
| 440 | endif() |
| 441 | |
| 442 | if(OCOS_ENABLE_TF_STRING) |
| 443 | target_include_directories(noexcep_operators PUBLIC |
| 444 | ${googlere2_SOURCE_DIR} |
| 445 | ${farmhash_SOURCE_DIR}/src) |
| 446 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_TF_STRING NOMINMAX FARMHASH_NO_BUILTIN_EXPECT FARMHASH_DEBUG=0) |
| 447 | target_link_libraries(noexcep_operators PRIVATE re2) |
| 448 | endif() |
| 449 | |
| 450 | if(OCOS_ENABLE_RE2_REGEX) |
| 451 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_RE2_REGEX) |
| 452 | endif() |
| 453 | |
| 454 | if(OCOS_ENABLE_MATH) |
| 455 | target_include_directories(ocos_operators PUBLIC ${dlib_SOURCE_DIR}) |
| 456 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_MATH) |
| 457 | endif() |
| 458 | |
| 459 | if(_ENABLE_OPENCV) |
| 460 | list(APPEND ocos_libraries ${opencv_LIBS}) |
| 461 | target_include_directories(ocos_operators PUBLIC ${opencv_INCLUDE_DIRS}) |
| 462 | endif() |
| 463 | |
| 464 | if(OCOS_ENABLE_OPENCV_CODECS) |
| 465 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_OPENCV_CODECS) |
| 466 | endif() |
| 467 | |
| 468 | if(OCOS_ENABLE_CV2) |
| 469 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_CV2) |
| 470 | endif() |
| 471 | |
| 472 | if(OCOS_ENABLE_VISION) |
| 473 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_VISION) |
| 474 | endif() |
| 475 | |
| 476 | if(OCOS_ENABLE_GPT2_TOKENIZER) |
| 477 | # GPT2 |
| 478 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_GPT2_TOKENIZER) |
| 479 | endif() |
| 480 | |
| 481 | if(OCOS_ENABLE_WORDPIECE_TOKENIZER) |
| 482 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_WORDPIECE_TOKENIZER) |
| 483 | endif() |
| 484 | |
| 485 | if(OCOS_ENABLE_BERT_TOKENIZER) |
| 486 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_BERT_TOKENIZER) |
| 487 | endif() |
| 488 | |
| 489 | if(OCOS_ENABLE_SPM_TOKENIZER) |
| 490 | # SentencePiece |
| 491 | target_include_directories(ocos_operators PUBLIC ${spm_INCLUDE_DIRS}) |
| 492 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_SPM_TOKENIZER) |
| 493 | list(APPEND ocos_libraries sentencepiece-static) |
| 494 | endif() |
| 495 | |
| 496 | if(OCOS_ENABLE_BLINGFIRE) |
| 497 | include(blingfire) |
| 498 | list(APPEND OCOS_COMPILE_DEFINITIONS ENABLE_BLINGFIRE) |
| 499 | list(APPEND ocos_libraries bingfirtinydll_static) |
| 500 | endif() |
| 501 | |
| 502 | if(OCOS_ENABLE_GPT2_TOKENIZER OR OCOS_ENABLE_WORDPIECE_TOKENIZER) |
| 503 | target_include_directories(ocos_operators PRIVATE ${nlohmann_json_SOURCE_DIR}/single_include) |
| 504 | list(APPEND ocos_libraries nlohmann_json::nlohmann_json) |
| 505 | endif() |
| 506 | |
| 507 | target_include_directories(noexcep_operators PUBLIC ${GSL_INCLUDE_DIR}) |
| 508 | list(APPEND ocos_libraries Microsoft.GSL::GSL) |
| 509 | |
| 510 | list(REMOVE_DUPLICATES OCOS_COMPILE_DEFINITIONS) |
| 511 | target_compile_definitions(noexcep_operators PRIVATE ${OCOS_COMPILE_DEFINITIONS}) |
| 512 | if(NOT OCOS_ENABLE_CPP_EXCEPTIONS) |
| 513 | if(MSVC) |
| 514 | get_target_property(_target_cxx_flags noexcep_operators COMPILE_OPTIONS) |
| 515 | list(REMOVE_ITEM _target_cxx_flags "/EHsc") |
| 516 | list(APPEND _target_cxx_flags "/EHs-c-") |
| 517 | set_target_properties(noexcep_operators PROPERTIES COMPILE_OPTIONS "${_target_cxx_flags}") |
| 518 | else() |
| 519 | target_compile_options(noexcep_operators PRIVATE -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables) |
| 520 | endif() |
| 521 | endif() |
| 522 | |
| 523 | list(APPEND ocos_libraries noexcep_operators) |
| 524 | target_compile_definitions(ocos_operators PRIVATE ${OCOS_COMPILE_DEFINITIONS}) |
| 525 | target_link_libraries(ocos_operators PRIVATE ${ocos_libraries}) |
| 526 | |
| 527 | file(GLOB shared_TARGET_LIB_SRC "shared/lib/*.cc" "shared/lib/*.h") |
| 528 | |
| 529 | if(NOT OCOS_ENABLE_STATIC_LIB AND CMAKE_SYSTEM_NAME STREQUAL "Emscripten") |
| 530 | add_executable(ortcustomops ${shared_TARGET_LIB_SRC}) |
| 531 | set_target_properties(ortcustomops PROPERTIES LINK_FLAGS " \ |
| 532 | -s WASM=1 \ |
| 533 | -s NO_EXIT_RUNTIME=0 \ |
| 534 | -s ALLOW_MEMORY_GROWTH=1 \ |
| 535 | -s SAFE_HEAP=0 \ |
| 536 | -s MODULARIZE=1 \ |
| 537 | -s SAFE_HEAP_LOG=0 \ |
| 538 | -s STACK_OVERFLOW_CHECK=0 \ |
| 539 | -s EXPORT_ALL=0 \ |
| 540 | -s VERBOSE=0 \ |
| 541 | --no-entry") |
| 542 | |
| 543 | if(CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 544 | set_property(TARGET ortcustomops APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=1 -s DEMANGLE_SUPPORT=1") |
| 545 | else() |
| 546 | set_property(TARGET ortcustomops APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=0 -s DEMANGLE_SUPPORT=0") |
| 547 | endif() |
| 548 | else() |
| 549 | add_library(ortcustomops STATIC ${shared_TARGET_LIB_SRC}) |
| 550 | if (HAS_SDL) |
| 551 | target_compile_options(ortcustomops PRIVATE "/sdl") |
| 552 | endif() |
| 553 | add_library(onnxruntime_extensions ALIAS ortcustomops) |
| 554 | standardize_output_folder(ortcustomops) |
| 555 | set(_BUILD_SHARED_LIBRARY TRUE) |
| 556 | endif() |
| 557 | |
| 558 | target_compile_definitions(ortcustomops PUBLIC ${OCOS_COMPILE_DEFINITIONS}) |
| 559 | target_include_directories(ortcustomops PUBLIC |
| 560 | "$<TARGET_PROPERTY:ocos_operators,INTERFACE_INCLUDE_DIRECTORIES>") |
| 561 | target_link_libraries(ortcustomops PUBLIC ocos_operators) |
| 562 | |
| 563 | if(_BUILD_SHARED_LIBRARY) |
| 564 | file(GLOB shared_TARGET_SRC "shared/*.cc" "shared/*.h" "shared/*.def") |
| 565 | add_library(extensions_shared SHARED ${shared_TARGET_SRC}) |
| 566 | source_group(TREE ${PROJECT_SOURCE_DIR} FILES ${shared_TARGET_SRC}) |
| 567 | standardize_output_folder(extensions_shared) |
| 568 | |
| 569 | if(CMAKE_SYSTEM_NAME STREQUAL "Android") |
| 570 | if(OCOS_ENABLE_SPM_TOKENIZER) |
| 571 | target_link_libraries(extensions_shared PUBLIC log) |
| 572 | endif() |
| 573 | endif() |
| 574 | |
| 575 | if(LINUX OR CMAKE_SYSTEM_NAME STREQUAL "Android") |
| 576 | set_property(TARGET extensions_shared APPEND_STRING PROPERTY LINK_FLAGS "-Wl,-s -Wl,--version-script -Wl,${PROJECT_SOURCE_DIR}/shared/ortcustomops.ver") |
| 577 | endif() |
| 578 | |
| 579 | target_include_directories(extensions_shared PUBLIC |
| 580 | "$<TARGET_PROPERTY:ortcustomops,INTERFACE_INCLUDE_DIRECTORIES>") |
| 581 | target_link_libraries(extensions_shared PRIVATE ortcustomops) |
| 582 | set_target_properties(extensions_shared PROPERTIES OUTPUT_NAME "ortextensions") |
| 583 | if(MSVC AND ocos_target_platform MATCHES "x86|x64") |
| 584 | target_link_options(extensions_shared PRIVATE "/CETCOMPAT") |
| 585 | endif() |
| 586 | |
| 587 | # Set some properties of your target |
| 588 | set_target_properties(extensions_shared PROPERTIES |
| 589 | VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH} |
| 590 | SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR} |
| 591 | ) |
| 592 | |
| 593 | # Install your target |
| 594 | install(TARGETS extensions_shared |
| 595 | RUNTIME DESTINATION bin |
| 596 | ) |
| 597 | |
| 598 | endif() |
| 599 | |
| 600 | if(OCOS_BUILD_PYTHON) |
| 601 | message(STATUS "Python Build is enabled") |
| 602 | include(ext_python) |
| 603 | endif() |
| 604 | |
| 605 | if(OCOS_BUILD_JAVA) |
| 606 | message(STATUS "Java Build is enabled") |
| 607 | include(ext_java) |
| 608 | endif() |
| 609 | |
| 610 | if(OCOS_BUILD_APPLE_FRAMEWORK) |
| 611 | include(ext_apple_framework) |
| 612 | endif() |
| 613 | |
| 614 | # clean up the requirements.txt files from 3rd party project folder to suppress the code security false alarms |
| 615 | file(GLOB_RECURSE NO_USE_FILES ${CMAKE_BINARY_DIR}/_deps/*requirements.txt) |
| 616 | message(STATUS "Found the following requirements.txt: ${NO_USE_FILES}") |
| 617 | |
| 618 | foreach(nf ${NO_USE_FILES}) |
| 619 | execute_process(COMMAND ${CMAKE_COMMAND} -E remove ${nf}) |
| 620 | endforeach() |
| 621 | |
| 622 | # Run CPack to generate the NuGet package |
| 623 | include(CPack) |
| 624 | |
| 625 | # test section |
| 626 | if(OCOS_ENABLE_CTEST) |
| 627 | if (OCOS_ENABLE_SELECTED_OPLIST) |
| 628 | # currently the tests don't handle operator exclusion cleanly. |
| 629 | message(FATAL_ERROR "Due to usage of OCOS_ENABLE_SELECTED_OPLIST excluding operators the tests are unable to be built and run") |
| 630 | endif() |
| 631 | |
| 632 | # Enable CTest |
| 633 | enable_testing() |
| 634 | message(STATUS "Fetch CTest") |
| 635 | include(CTest) |
| 636 | |
| 637 | set(TEST_SRC_DIR ${PROJECT_SOURCE_DIR}/test) |
| 638 | message(STATUS "Fetch googletest") |
| 639 | include(googletest) |
| 640 | file(GLOB static_TEST_SRC "${TEST_SRC_DIR}/static_test/*.cc") |
| 641 | add_executable(ocos_test ${static_TEST_SRC}) |
| 642 | standardize_output_folder(ocos_test) |
| 643 | target_link_libraries(ocos_test PRIVATE gtest_main ocos_operators ${ocos_libraries}) |
| 644 | add_test(NAME ocos_test COMMAND $<TARGET_FILE:ocos_test>) |
| 645 | |
| 646 | SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) |
| 647 | find_library(ONNXRUNTIME onnxruntime HINTS "${ONNXRUNTIME_LIB_DIR}") |
| 648 | |
| 649 | if(ONNXRUNTIME-NOTFOUND) |
| 650 | message(WARNING "The prebuilt onnxruntime libraries directory cannot found (via ONNXRUNTIME_LIB_DIR), the extensions_test will be skipped.") |
| 651 | else() |
| 652 | set(LINUX_CC_FLAGS "") |
| 653 | |
| 654 | # needs to link with stdc++fs in Linux |
| 655 | if(UNIX AND NOT APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "Android") |
| 656 | list(APPEND LINUX_CC_FLAGS stdc++fs -pthread) |
| 657 | endif() |
| 658 | |
| 659 | file(GLOB shared_TEST_SRC "${TEST_SRC_DIR}/shared_test/*.cc") |
| 660 | add_executable(extensions_test ${shared_TEST_SRC}) |
| 661 | standardize_output_folder(extensions_test) |
| 662 | target_include_directories(extensions_test PRIVATE ${spm_INCLUDE_DIRS} |
| 663 | "$<TARGET_PROPERTY:extensions_shared,INTERFACE_INCLUDE_DIRECTORIES>") |
| 664 | |
| 665 | if(ONNXRUNTIME_LIB_DIR) |
| 666 | target_link_directories(extensions_test PRIVATE ${ONNXRUNTIME_LIB_DIR}) |
| 667 | endif() |
| 668 | |
| 669 | target_link_libraries(extensions_test PRIVATE ocos_operators extensions_shared onnxruntime gtest_main gmock_main |
| 670 | ${ocos_libraries} ${LINUX_CC_FLAGS}) |
| 671 | |
| 672 | # Copy ONNXRuntime DLLs into bin folder for testing on Windows platform |
| 673 | if(WIN32) |
| 674 | file(TO_CMAKE_PATH "${ONNXRUNTIME_LIB_DIR}/*" ONNXRUNTIME_LIB_FILEPATTERN) |
| 675 | file(GLOB ONNXRUNTIME_LIB_FILES CONFIGURE_DEPENDS "${ONNXRUNTIME_LIB_FILEPATTERN}") |
| 676 | add_custom_command( |
| 677 | TARGET extensions_test POST_BUILD |
| 678 | COMMAND ${CMAKE_COMMAND} -E copy ${ONNXRUNTIME_LIB_FILES} $<TARGET_FILE_DIR:extensions_test>) |
| 679 | endif() |
| 680 | |
| 681 | set(TEST_DATA_SRC ${TEST_SRC_DIR}/data) |
| 682 | set(TEST_DATA_DES ${onnxruntime_extensions_BINARY_DIR}/data) |
| 683 | |
| 684 | # Copy test data from source to destination. |
| 685 | add_custom_command( |
| 686 | TARGET extensions_test POST_BUILD |
| 687 | COMMAND ${CMAKE_COMMAND} -E copy_directory |
| 688 | ${TEST_DATA_SRC} |
| 689 | ${TEST_DATA_DES}) |
| 690 | add_test(NAME extensions_test COMMAND $<TARGET_FILE:extensions_test>) |
| 691 | endif() |
| 692 | endif() |
| 693 | |