Skip to content

Latest commit

 

History

History
71 lines (55 loc) · 2.62 KB

ONNX_Compilation_Notes.md

File metadata and controls

71 lines (55 loc) · 2.62 KB

Building ONNX Runtime

While ONNX requires cmake to build, bindgen (used to automatically build the bindings) requires llvm:

brew install llvm cmake

# bindgen needs this to find llvm/clang
export LLVM_CONFIG_PATH=/usr/local/opt/llvm/bin/llvm-config
# Or on macOS Big Sur:
export LLVM_CONFIG_PATH=/opt/homebrew/opt/llvm/bin/llvm-config

The build.rs script uses the ORT_LIB_LOCATION environment variable to find the built library and its headers. Make sure to point to the proper location:

export ORT_LIB_LOCATION=/full/path/to/onnxruntime

NOTE: The .cargo/config file assumes the library is installed in the target/onnxruntime directory.

❯ git clone https://github.com/microsoft/onnxruntime.git onnxruntime.git
❯ cd onnxruntime.git
❯ git checkout v1.3.1
# Debug build with install directory inside our own 'target' directory
# Takes ~1/2 hour on a macbook pro 2.9 GHz 16 GB
❯ ./build.sh --config Debug --build_shared_lib --parallel --cmake_extra_defines="CMAKE_INSTALL_PREFIX=/path/to/onnxruntime-rs/target/onnxruntime"cd build/Linux/Debug
❯ make install

NOTE: Cargo will link dynamically to libonnxruntime.{so,dylib}. If the shared library is located outsite of the target directory, cargo will use an rpath instead of putting the full path to the library in the built artifacts (see this comment).

For example:

❯ otool -L target/debug/examples/c_api_sample
target/debug/examples/c_api_sample:
        @rpath/libonnxruntime.1.3.1.dylib (compatibility version 0.0.0, current version 1.3.1)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)
        /usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)

If libonnxruntime.{so,dylib} is not in a standard directory (for example it was just compiled and not installed in a system directory) then the library will not be found. For example:

❯ target/debug/examples/c_api_sample
dyld: Library not loaded: @rpath/libonnxruntime.1.3.1.dylib
  Referenced from: [...]/target/debug/examples/c_api_sample
  Reason: image not found

You might need to use DYLD_LIBRARY_PATH (on macOS) to point to the directory where libonnxruntime.dylib is located to execute a binary (or debug it). Note that this is not the proper way as it messes System Integrity Protection.

Commit 4315b5a381faa11330fe554c69392f59 set the path in .cargo/config (for macOS) and should allow running the binaries without setting an environment variable.