Skip to content

Commit

Permalink
Option to use precompiled libs
Browse files Browse the repository at this point in the history
  • Loading branch information
danemadsen committed Jul 30, 2024
1 parent ed82821 commit d27026d
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 23 deletions.
131 changes: 108 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d27026d

Please sign in to comment.