From d27026dbb7a7e7d4be219c46a4cef2228f5c8d4d Mon Sep 17 00:00:00 2001 From: danemadsen Date: Tue, 30 Jul 2024 14:30:39 +1000 Subject: [PATCH] Option to use precompiled libs --- CMakeLists.txt | 131 ++++++++++++++++++++++++++++++++++++++++--------- README.md | 3 ++ 2 files changed, 111 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e6194d2..a776382 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,42 +38,127 @@ set_target_properties( LIBRARY_OUTPUT_DIRECTORY ${BABYLON_LIB_INSTALL_DIR} ) -set(onnxruntime_BUILD_SHARED_LIB ON) -set(onnxruntime_BUILD_UNIT_TESTS OFF) +if(BABYLON_BUILD_SOURCE OR ANDROID) + # Set ONNX Runtime source directory + set(ONNXRUNTIME_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/onnxruntime) -# Set ONNX Runtime source directory -set(ONNXRUNTIME_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/onnxruntime) + # Set ONNX Runtime options + set(onnxruntime_BUILD_SHARED_LIB ON) + set(onnxruntime_BUILD_UNIT_TESTS OFF) -# Add ONNX Runtime subdirectory -add_subdirectory(${ONNXRUNTIME_SOURCE_DIR}/cmake) + # Add ONNX Runtime subdirectory + add_subdirectory(${ONNXRUNTIME_SOURCE_DIR}/cmake) -# Set the output directory for the shared library -set_target_properties( - onnxruntime - PROPERTIES - LIBRARY_OUTPUT_DIRECTORY ${BABYLON_LIB_INSTALL_DIR} -) + # Set the output directory for the shared library + set_target_properties( + onnxruntime + PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${BABYLON_LIB_INSTALL_DIR} + ) -# Include ONNX Runtime headers -target_include_directories( - babylon - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include - ${ONNXRUNTIME_SOURCE_DIR}/include/onnxruntime/core/session -) + # Include ONNX Runtime headers + target_include_directories( + babylon + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include + ${ONNXRUNTIME_SOURCE_DIR}/include/onnxruntime/core/session + ) +else() + set(ONNXRUNTIME_VERSION "1.18.1") -# Link ONNX Runtime to babylon -target_link_libraries( - babylon - onnxruntime -) + if(WIN32) + # Windows x86-64 + set(ONNXRUNTIME_PREFIX "onnxruntime-win-x64-${ONNXRUNTIME_VERSION}") + set(ONNXRUNTIME_EXT "zip") + elseif (APPLE) + if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) + # MacOS x86-64 + set(ONNXRUNTIME_PREFIX "onnxruntime-osx-x86_64-${ONNXRUNTIME_VERSION}") + elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm64) + # MacOS Apple Silicon + set(ONNXRUNTIME_PREFIX "onnxruntime-osx-arm64-${ONNXRUNTIME_VERSION}") + else() + message(FATAL_ERROR "Unsupported architecture for onnxruntime") + endif() + + set(ONNXRUNTIME_EXT "tgz") + else() + if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) + # Linux x86-64 + set(ONNXRUNTIME_PREFIX "onnxruntime-linux-x64-${ONNXRUNTIME_VERSION}") + elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) + # Linux ARM 64-bit + set(ONNXRUNTIME_PREFIX "onnxruntime-linux-aarch64-${ONNXRUNTIME_VERSION}") + elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l) + # Linux ARM 32-bit + set(ONNXRUNTIME_PREFIX "onnxruntime-linux-arm32-${ONNXRUNTIME_VERSION}") + set(ONNXRUNTIME_URL "https://github.com/synesthesiam/prebuilt-apps/releases/download/v1.0/onnxruntime-linux-arm32-${ONNXRUNTIME_VERSION}.tgz") + else() + message(FATAL_ERROR "Unsupported architecture for onnxruntime") + endif() + + set(ONNXRUNTIME_EXT "tgz") + endif() + + if(NOT DEFINED ONNXRUNTIME_URL) + set(ONNXRUNTIME_URL "https://github.com/microsoft/onnxruntime/releases/download/v${ONNXRUNTIME_VERSION}/${ONNXRUNTIME_PREFIX}.${ONNXRUNTIME_EXT}") + endif() + + set(DOWNLOAD_DIR "${CMAKE_CURRENT_LIST_DIR}/download") + + set(ONNXRUNTIME_FILENAME "${DOWNLOAD_DIR}/${ONNXRUNTIME_PREFIX}.${ONNXRUNTIME_EXT}") + set(ONNXRUNTIME_SOURCE_DIR "${DOWNLOAD_DIR}/${ONNXRUNTIME_PREFIX}") + + if(NOT EXISTS "${ONNXRUNTIME_SOURCE_DIR}") + if(NOT EXISTS "${ONNXRUNTIME_FILENAME}") + # Download onnxruntime release + message("Downloading ${ONNXRUNTIME_URL}") + file(DOWNLOAD "${ONNXRUNTIME_URL}" "${ONNXRUNTIME_FILENAME}") + endif() + + # Extract .zip or .tgz to a directory like lib/onnxruntime-linux-x64-1.18.1/ + file(ARCHIVE_EXTRACT INPUT "${ONNXRUNTIME_FILENAME}" DESTINATION "${DOWNLOAD_DIR}") + endif() + + if(EXISTS "${ONNXRUNTIME_SOURCE_DIR}/lib/libonnxruntime.so.${ONNXRUNTIME_VERSION}") + # Delete ${ONNXRUNTIME_SOURCE_DIR}/lib/libonnxruntime.so + file(REMOVE "${ONNXRUNTIME_SOURCE_DIR}/lib/libonnxruntime.so") + + # Rename ${ONNXRUNTIME_SOURCE_DIR}/lib/libonnxruntime.so.1.18.1 to ${ONNXRUNTIME_SOURCE_DIR}/lib/libonnxruntime.so + file(RENAME "${ONNXRUNTIME_SOURCE_DIR}/lib/libonnxruntime.so.${ONNXRUNTIME_VERSION}" "${ONNXRUNTIME_SOURCE_DIR}/lib/libonnxruntime.so") + endif() + + add_library(onnxruntime SHARED IMPORTED) + + # Import options for onnxruntime + if(WIN32) + set_target_properties(onnxruntime PROPERTIES IMPORTED_LOCATION ${ONNXRUNTIME_SOURCE_DIR}/lib/onnxruntime.dll) + elseif(APPLE) + set_target_properties(onnxruntime PROPERTIES IMPORTED_LOCATION ${ONNXRUNTIME_SOURCE_DIR}/lib/libonnxruntime.dylib) + else() + set_target_properties(onnxruntime PROPERTIES IMPORTED_LOCATION ${ONNXRUNTIME_SOURCE_DIR}/lib/libonnxruntime.so) + endif() + + # Include ONNX Runtime headers + target_include_directories( + babylon + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include + ${ONNXRUNTIME_SOURCE_DIR}/include + ) +endif() # Link against CoreFoundation and Foundation on macOS if(APPLE) target_link_libraries( babylon + onnxruntime "-framework CoreFoundation" "-framework Foundation" ) +else() + target_link_libraries( + babylon + onnxruntime + ) endif() # Include example directory if EXAMPLES flag is set diff --git a/README.md b/README.md index 5e039b2..8857345 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,9 @@ make ./bin/babylon_example ``` +To reduce compile time by default the libary uses onnxruntime shared libraries provided by microsoft. +This can be overridden by setting `BABYLON_BUILD_SOURCE` to `ON`. + ## Usage ### C Example: