@@ -20,6 +20,80 @@ if (__build_embeddings_included)
2020endif ()
2121set ( __build_embeddings_included YES )
2222
23+ set ( EMBEDDINGS_ORT_VERSION "" CACHE STRING "ONNX Runtime version used for local Linux embeddings builds; defaults to embeddings/Cargo.toml metadata" )
24+ set ( EMBEDDINGS_ORT_GLIBC "" CACHE STRING "ONNX Runtime glibc baseline used for local Linux embeddings builds; defaults to embeddings/Cargo.toml metadata" )
25+
26+ function (read_embeddings_ort_metadata OUT_ORT_VERSION OUT_ORT_GLIBC )
27+ set ( CARGO_TOML "${CMAKE_SOURCE_DIR} /embeddings/Cargo.toml" )
28+ if (NOT EXISTS "${CARGO_TOML} " )
29+ message ( FATAL_ERROR "embeddings Cargo.toml was not found: ${CARGO_TOML} " )
30+ endif ()
31+
32+ file ( READ "${CARGO_TOML} " CARGO_TOML_CONTENT )
33+ string ( REGEX MATCH "\\ [package\\ .metadata\\ .manticore\\ .ort\\ ][^\[ ]*" ORT_METADATA "${CARGO_TOML_CONTENT} " )
34+ if (NOT ORT_METADATA)
35+ message ( FATAL_ERROR "Missing [package.metadata.manticore.ort] version/linux-glibc in ${CARGO_TOML} " )
36+ endif ()
37+
38+ if (NOT ORT_METADATA MATCHES "version[ \t ]*=[ \t ]*\" ([^\" ]+)\" " )
39+ message ( FATAL_ERROR "Missing [package.metadata.manticore.ort] version in ${CARGO_TOML} " )
40+ endif ()
41+ set ( ORT_VERSION "${CMAKE_MATCH_1} " )
42+
43+ if (NOT ORT_METADATA MATCHES "linux-glibc[ \t ]*=[ \t ]*\" ([^\" ]+)\" " )
44+ message ( FATAL_ERROR "Missing [package.metadata.manticore.ort] linux-glibc in ${CARGO_TOML} " )
45+ endif ()
46+ set ( ORT_GLIBC "${CMAKE_MATCH_1} " )
47+
48+ if (EMBEDDINGS_ORT_VERSION)
49+ set ( ORT_VERSION "${EMBEDDINGS_ORT_VERSION} " )
50+ endif ()
51+ if (EMBEDDINGS_ORT_GLIBC)
52+ set ( ORT_GLIBC "${EMBEDDINGS_ORT_GLIBC} " )
53+ endif ()
54+
55+ set ( ${OUT_ORT_VERSION} "${ORT_VERSION} " PARENT_SCOPE )
56+ set ( ${OUT_ORT_GLIBC} "${ORT_GLIBC} " PARENT_SCOPE )
57+ endfunction ()
58+
59+ function (prepare_embeddings_ort )
60+ if (NOT UNIX OR APPLE )
61+ return ()
62+ endif ()
63+
64+ read_embeddings_ort_metadata ( ORT_VERSION ORT_GLIBC )
65+
66+ if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)$" )
67+ set ( ORT_ARCH "aarch64" )
68+ else ()
69+ set ( ORT_ARCH "x64" )
70+ endif ()
71+
72+ set ( ORT_ASSET "onnxruntime-linux-${ORT_ARCH} -static_lib-${ORT_VERSION} -glibc${ORT_GLIBC} " )
73+ set ( ORT_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v${ORT_VERSION} /${ORT_ASSET} .zip" )
74+ set ( ORT_ROOT "${CMAKE_CURRENT_BINARY_DIR} /embeddings/ort/${ORT_ASSET} " )
75+ set ( ORT_ZIP "${CMAKE_CURRENT_BINARY_DIR} /embeddings/ort/${ORT_ASSET} .zip" )
76+
77+ if (NOT EXISTS "${ORT_ROOT} /lib" )
78+ message ( STATUS "Downloading ONNX Runtime static library: ${ORT_ASSET} " )
79+ file ( MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR} /embeddings/ort" )
80+ file ( DOWNLOAD "${ORT_URL} " "${ORT_ZIP} " STATUS ORT_DOWNLOAD_STATUS SHOW_PROGRESS )
81+ list ( GET ORT_DOWNLOAD_STATUS 0 ORT_DOWNLOAD_CODE )
82+ if (NOT ORT_DOWNLOAD_CODE EQUAL 0)
83+ list ( GET ORT_DOWNLOAD_STATUS 1 ORT_DOWNLOAD_ERROR )
84+ message ( FATAL_ERROR "Failed to download ${ORT_URL} : ${ORT_DOWNLOAD_ERROR} " )
85+ endif ()
86+ file ( ARCHIVE_EXTRACT INPUT "${ORT_ZIP} " DESTINATION "${CMAKE_CURRENT_BINARY_DIR} /embeddings/ort" )
87+ endif ()
88+
89+ if (NOT EXISTS "${ORT_ROOT} /lib" )
90+ message ( FATAL_ERROR "ONNX Runtime lib directory was not found: ${ORT_ROOT} /lib" )
91+ endif ()
92+
93+ set ( ENV{ORT_LIB_PATH} "${ORT_ROOT} /lib" )
94+ message ( STATUS "Using ONNX Runtime from ORT_LIB_PATH=$ENV{ORT_LIB_PATH} " )
95+ endfunction ()
96+
2397function (build_embeddings_lib )
2498 message ( STATUS "building embeddings locally..." )
2599
@@ -49,21 +123,37 @@ function(build_embeddings_lib)
49123 # This matches the format used by other Manticore libraries for consistent version display
50124 set (ENV{GIT_COMMIT_ID} "${GIT_COMMIT_ID} " )
51125 set (ENV{GIT_TIMESTAMP_ID} "${GIT_TIMESTAMP_ID} " )
126+ prepare_embeddings_ort ()
52127
53- # Enable platform-specific BLAS acceleration for candle when available
54- set (EMBEDDINGS_CARGO_FEATURES "" )
55- if (APPLE )
56- set (EMBEDDINGS_CARGO_FEATURES "--features" "accelerate" )
57- elseif (UNIX )
58- # MKL provides multi-threaded BLAS on Linux; skip if not available
59- execute_process (COMMAND pkg-config --exists mkl-dynamic-lp64-seq RESULT_VARIABLE MKL_FOUND OUTPUT_QUIET ERROR_QUIET )
60- if (MKL_FOUND EQUAL 0)
61- set (EMBEDDINGS_CARGO_FEATURES "--features" "mkl" )
128+ # Enable platform-specific BLAS acceleration for candle when available.
129+ if (DEFINED EMBEDDINGS_CARGO_FEATURES)
130+ set (EMBEDDINGS_FEATURES_CSV "${EMBEDDINGS_CARGO_FEATURES} " )
131+ else ()
132+ set (EMBEDDINGS_FEATURE_LIST)
133+ if (APPLE )
134+ list (APPEND EMBEDDINGS_FEATURE_LIST accelerate)
135+ elseif (UNIX )
136+ # MKL provides multi-threaded BLAS on Linux; skip if not available
137+ execute_process (COMMAND pkg-config --exists mkl-dynamic-lp64-seq RESULT_VARIABLE MKL_FOUND OUTPUT_QUIET ERROR_QUIET )
138+ if (MKL_FOUND EQUAL 0)
139+ list (APPEND EMBEDDINGS_FEATURE_LIST mkl)
140+ endif ()
62141 endif ()
142+ list (JOIN EMBEDDINGS_FEATURE_LIST "," EMBEDDINGS_FEATURES_CSV)
143+ endif ()
144+
145+ if (UNIX AND NOT APPLE AND DEFINED ENV{ORT_LIB_PATH} AND NOT "$ENV{ORT_LIB_PATH} " STREQUAL "" AND EMBEDDINGS_FEATURES_CSV)
146+ string (REPLACE "," ";" EMBEDDINGS_FEATURE_LIST "${EMBEDDINGS_FEATURES_CSV} " )
147+ list (REMOVE_ITEM EMBEDDINGS_FEATURE_LIST download-ort)
148+ list (JOIN EMBEDDINGS_FEATURE_LIST "," EMBEDDINGS_FEATURES_CSV)
149+ endif ()
150+
151+ if (EMBEDDINGS_FEATURES_CSV)
152+ set (EMBEDDINGS_CARGO_FEATURE_ARGS "--features" "${EMBEDDINGS_FEATURES_CSV} " )
63153 endif ()
64154
65155 execute_process (
66- COMMAND cargo build --manifest-path ${CMAKE_SOURCE_DIR} /embeddings/Cargo.toml --lib --release ${EMBEDDINGS_CARGO_FEATURES } --target-dir ${CMAKE_CURRENT_BINARY_DIR} /embeddings
156+ COMMAND cargo build --manifest-path ${CMAKE_SOURCE_DIR} /embeddings/Cargo.toml --lib --release ${EMBEDDINGS_CARGO_FEATURE_ARGS } --target-dir ${CMAKE_CURRENT_BINARY_DIR} /embeddings
67157 RESULT_VARIABLE CMD_RESULT
68158 )
69159
@@ -86,4 +176,3 @@ function(build_embeddings_lib)
86176 file (RENAME "${CMAKE_CURRENT_BINARY_DIR} /embeddings/release/${EMBEDDINGS_LIB_NAME} .pdb" "${CMAKE_CURRENT_BINARY_DIR} /embeddings/release/lib_${EMBEDDINGS_LIB_NAME} .pdb" )
87177 endif ()
88178endfunction ()
89-
0 commit comments