yams/plugins/onnx/meson.build
trvon fe07469620
feat(plugins): prefer a system ONNX Runtime, fall back to a bundled copy
The ONNX plugin dlopen()ed ONNX Runtime from directories next to the
plugin first and only then from a few system prefixes, with no notion of
which copy it got or why, and the shipped packages contained no copy at
all, so on most hosts the plugin could only report "runtime not found".

The runtime resolution moves into plugins/ort_runtime/, built as a
hidden-visibility static library both ONNX-using plugins link (their
export maps still expose only the plugin C ABI). The policy is split out
as pure code (ort_runtime_resolution.{h,cpp}) so it is testable without a
runtime. Candidates are tried in this order and the first compatible one
wins:

  1. YAMS_ONNX_RUNTIME_LIB / YAMS_ONNX_RUNTIME_DIR (existing pin, kept),
     then the plugin config key runtime_library ([plugins.onnx] /
     [plugins.glint], a file or directory)
  2. a system copy: the loader search by soname, then the usual library
     directories (multiarch dir, /usr/lib64, /usr/lib, /usr/local/lib,
     /opt/onnxruntime, Homebrew) for versioned names such as Debian's
     libonnxruntime.so.1.23
  3. the private copy in <libdir>/yams/onnxruntime/
  4. legacy locations next to the plugin (build trees)

A candidate is accepted only if it exports OrtGetApiBase, GetApi() returns
an API table for the ORT_API_VERSION the plugins are compiled against, and
it meets the 1.23 minimum; otherwise it is skipped with a reason. A soname
lookup that returns a copy another plugin already loaded from the bundled
directory is reported as that copy. The choice ("Using system|bundled ONNX
Runtime <ver> from <path> (<why>)") and every skipped candidate go to the
plugin log and, through the libyams_onnx_resource host sink (new
OnnxConcurrencyRegistry::emitHostLog), to the daemon log; plugin health
reports runtime_source. With no compatible copy the plugin reports "ONNX
Runtime unavailable: ..." instead of failing.

Tests: tests/unit/plugins/ort_runtime_resolution_catch2_test.cpp covers
the order, the override pin, configured directories, system-over-bundled
preference, skipping an incompatible system copy, the not-found result
and the already-loaded classification.

Signed-off-by: trvon <me@trevon.dev>
2026-10-01 22:42:15 -06:00

119 lines
4.1 KiB
Meson

fs = import('fs')
onnx_dep = onnxruntime_resolved_dep
spdlog_dep = dependency('spdlog', required: false)
json_dep = dependency('nlohmann_json', required: false)
yams_core = dependency('yams_core', required: false)
yams_onnx_resource = dependency('yams_onnx_resource', required: false)
# Boost.Asio is header-only; its runtime needs (boost::system) are already
# in the top-level boost_dep. Use that variable directly to bypass meson's
# Windows Boost finder, which can't reconcile Conan's MSVC layout. See
# meson #14146 for the override_dependency+modules limitation.
win_extra_deps = []
if host_machine.system() == 'windows'
cpp = meson.get_compiler('cpp')
win_extra_deps += [cpp.find_library('dxgi', required: true)]
endif
onnx_opt = get_option('enable-onnx')
onnx_feature_requested = not onnx_opt.disabled()
if not get_option('plugin-onnx')
subdir_done()
endif
if not onnx_feature_requested
message('enable-onnx disabled; skipping yams_onnx_plugin build')
subdir_done()
endif
# Gracefully skip when ONNX Runtime is not discoverable in this environment.
if not onnx_dep.found()
message('ONNX Runtime not found in search paths; skipping yams_onnx_plugin')
subdir_done()
endif
yams_onnx_plugin_srcs = [
'onnx_plugin.cpp',
'model_provider.cpp',
'onnx_text_processing.cpp',
'onnx_model_pool.cpp',
'onnx_colbert_session.cpp',
]
plugin_cppargs = []
if get_option('build-tests') or get_option('build-benchmarks')
plugin_cppargs += ['-DYAMS_TEST_PROVIDER_CONTROLS=1']
endif
# GPU provider defines - these MUST be passed to the plugin for GPU acceleration to work.
# Conan sets these in pkg-config Cflags, but CMake method may not propagate them.
# Check if already present via onnx_dep, otherwise use platform detection.
onnx_cflags = onnx_dep.get_variable(pkgconfig: 'Cflags', default_value: '')
if onnx_cflags.contains('YAMS_ONNX_COREML_ENABLED')
plugin_cppargs += ['-DYAMS_ONNX_COREML_ENABLED=1']
message('ONNX CoreML GPU support enabled (from pkg-config)')
elif onnx_cflags.contains('YAMS_ONNX_CUDA_ENABLED')
plugin_cppargs += ['-DYAMS_ONNX_CUDA_ENABLED=1']
message('ONNX CUDA GPU support enabled (from pkg-config)')
elif onnx_cflags.contains('YAMS_ONNX_DIRECTML_ENABLED')
plugin_cppargs += ['-DYAMS_ONNX_DIRECTML_ENABLED=1']
message('ONNX DirectML GPU support enabled (from pkg-config)')
elif onnx_cflags.contains('YAMS_ONNX_MIGRAPHX_ENABLED')
plugin_cppargs += ['-DYAMS_ONNX_MIGRAPHX_ENABLED=1']
message('ONNX MIGraphX GPU support enabled (from pkg-config)')
elif host_machine.system() == 'darwin'
# Fallback: macOS always has CoreML support in prebuilt ONNX Runtime
plugin_cppargs += ['-DYAMS_ONNX_COREML_ENABLED=1']
message('ONNX CoreML GPU support enabled (macOS platform detection)')
endif
onnx_plugin_deps = [
spdlog_dep,
json_dep,
yams_core,
dependency('yams_vector'),
dependency('yams_tune_advisor'),
yams_onnx_resource,
boost_dep,
] + win_extra_deps
# ONNX Runtime is loaded at runtime by the shared loader (headers only here).
onnx_plugin_deps += [yams_ort_loader_dep]
onnx_plugin_link_args = []
if host_machine.system() == 'linux'
# Fail link on unresolved symbols to avoid runtime dlopen failures.
onnx_plugin_link_args += ['-Wl,-z,defs']
endif
onnx_plugin_install_rpath = ''
if host_machine.system() == 'darwin'
onnx_plugin_install_rpath = '@loader_path/..'
elif host_machine.system() == 'linux'
onnx_plugin_install_rpath = '$ORIGIN/..'
endif
yams_onnx_plugin = shared_module('yams_onnx_plugin',
yams_onnx_plugin_srcs,
include_directories: [include_directories('../../include')],
dependencies: onnx_plugin_deps,
link_args: onnx_plugin_link_args + plugin_export_link_args,
link_depends: plugin_export_link_depends,
gnu_symbol_visibility: 'hidden',
cpp_args: plugin_cppargs,
install: true,
install_dir: plugins_dir,
install_rpath: onnx_plugin_install_rpath,
)
set_variable('yams_onnx_plugin_target', [yams_onnx_plugin])
# --- Windows: Copy runtime DLLs next to the ONNX plugin ---
if host_machine.system() == 'windows' and windows_runtime_dlls.length() > 0
foreach dll_info : windows_runtime_dlls
dll_name = dll_info[0]
dll_src = dll_info[1]
fs.copyfile(dll_src, dll_name, install: false)
endforeach
endif