yams/plugins/onnx/meson.build

217 lines
6.9 KiB
Meson

fs = import('fs')
conan_gen_opt = get_option('conan-generators-dir')
conan_candidate_dirs = []
if conan_gen_opt != ''
conan_candidate_dirs += [join_paths(meson.project_source_root(), conan_gen_opt)]
endif
base_build = meson.project_build_root()
default_candidates = [
base_build,
join_paths(base_build, 'conan'),
join_paths(base_build, 'conan', 'generators'),
join_paths(base_build, 'generators'),
]
foreach variant : ['build-debug', 'build-release', 'debug', 'release']
variant_path = join_paths(base_build, variant)
if fs.exists(variant_path)
default_candidates += [variant_path]
default_candidates += [join_paths(variant_path, 'conan')]
default_candidates += [join_paths(variant_path, 'conan', 'generators')]
default_candidates += [join_paths(variant_path, 'generators')]
endif
endforeach
foreach cand : default_candidates
if fs.exists(cand)
conan_candidate_dirs += [cand]
endif
endforeach
onnx_cmake_paths = []
foreach base : conan_candidate_dirs
if fs.exists(base)
onnx_cmake_paths += base
foreach sub : ['onnxruntime', 'onnxruntime/Release', 'onnxruntime/Debug']
cand = join_paths(base, sub)
if fs.exists(cand)
onnx_cmake_paths += cand
endif
endforeach
endif
endforeach
# Allow explicit override from project option
onnx_opt_path = get_option('onnxruntime-cmake-path')
if onnx_opt_path != ''
onnx_cmake_paths += onnx_opt_path
endif
# Try pkg-config first to get compile definitions (like YAMS_ONNX_COREML_ENABLED),
# then fall back to CMake if not found
onnx_dep = dependency('onnxruntime', method: 'pkg-config', required: false)
if not onnx_dep.found()
onnx_dep = dependency('onnxruntime', method: 'cmake', cmake_module_path: onnx_cmake_paths, required: false)
endif
if not onnx_dep.found()
onnx_dep = dependency('onnxruntime', required: false)
endif
spdlog_dep = dependency('spdlog', required: false)
json_dep = dependency('nlohmann_json', required: false)
yams_core = dependency('yams_core', required: false)
yams_vector = dependency('yams_vector', required: false)
yams_onnx_resource = dependency('yams_onnx_resource', required: false)
boost_dep = dependency('boost', modules: ['asio'], required: false)
if not boost_dep.found()
boost_dep = dependency('boost', required: false)
endif
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()
onnx_genai_headers_present = false
onnx_include_dirs = []
if onnx_dep.found()
prefix = onnx_dep.get_variable(pkgconfig: 'prefix', default_value: '')
if prefix != ''
foreach key : ['includedir', 'includedir1', 'includedir2']
dirval = onnx_dep.get_variable(pkgconfig: key, default_value: '')
if dirval != ''
dirval = dirval.replace('${prefix}', prefix)
onnx_include_dirs += dirval
endif
endforeach
endif
genai_header_candidates = [
'ort_genai.h',
'onnxruntime/genai/ort_genai.h',
'onnxruntime/genai/embedding.h',
'onnxruntime/genai/genai_api.h',
'onnxruntime/genai/pipeline.h',
]
foreach incdir : onnx_include_dirs
if incdir == ''
continue
endif
foreach hdr : genai_header_candidates
if fs.exists(join_paths(incdir, hdr))
onnx_genai_headers_present = true
break
endif
endforeach
if onnx_genai_headers_present
break
endif
endforeach
endif
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_model_pool.cpp',
'onnx_reranker_session.cpp',
'onnx_colbert_session.cpp',
'../../src/genai/onnx_genai_adapter.cpp',
]
# GenAI adapter disabled - not used
plugin_cppargs = []
if onnx_genai_headers_present
plugin_cppargs += ['-DYAMS_GENAI_RUNTIME_PRESENT']
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 = [
onnx_dep,
spdlog_dep,
json_dep,
yams_core,
yams_vector,
yams_onnx_resource,
dependency('yams_daemon_client'),
boost_dep,
] + win_extra_deps
# In-process daemon fallback paths inside yams_daemon_client reference symbols
# implemented by yams_daemon (for example StreamingRequestProcessor). Link
# yams_daemon explicitly on all platforms so unresolved symbols fail at build
# time instead of at plugin dlopen time.
onnx_plugin_deps += dependency('yams_daemon')
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,
cpp_args: plugin_cppargs,
install: true,
install_dir: plugins_dir,
install_rpath: onnx_plugin_install_rpath,
)
# --- 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