Skip to content

Commit c5af5a0

Browse files
committed
feat: add onnxruntime package
1 parent baa32ff commit c5af5a0

5 files changed

Lines changed: 218 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# ONNX Runtime 1.31.0 compat package
2+
3+
- Branch: `add-onnxruntime`.
4+
- Source: [microsoft/onnxruntime](https://github.com/microsoft/onnxruntime),
5+
release `v1.31.0`, MIT licensed.
6+
- Shape: Form B with an external CMake build hook. ONNX Runtime cannot be
7+
represented by a source glob: the upstream build generates protobuf and
8+
flatbuffers sources and fetches its pinned dependency graph from
9+
`cmake/external/deps.txt`.
10+
- Build profile: CPU execution provider only. CUDA, TensorRT, OpenVINO,
11+
CoreML, DNNL, XNNPACK, mimalloc, language bindings, tests, and optional
12+
provider plugins are disabled. The upstream shared library is installed
13+
into the package prefix, and the mcpp descriptor links it through a small C
14+
anchor target.
15+
- Platforms: Linux, macOS, and Windows use the same source archive and the
16+
upstream CMake platform selection. `xim:cmake` is declared as the build
17+
dependency on each platform.
18+
- Test: `tests/examples/onnxruntime` loads the public C API and verifies the
19+
runtime version string begins with `1.31.`.
20+
- CN mirror: not declared because no mcpp-res upload is available; the
21+
descriptor uses the permitted upstream URL fallback. The release archive
22+
SHA-256 is
23+
`a7e84230b5d509e48fbd6b73c2453a56345ccb22da34a46dac1f0cb4a2d6507b`.

‎mcpp.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ members = [
106106
"tests/examples/glm",
107107
"tests/examples/libaio",
108108
"tests/examples/libserial",
109+
"tests/examples/onnxruntime",
109110
"tests/examples/apriltag",
110111
"tests/examples/libassert",
111112
"tests/examples/libcoro",

‎pkgs/c/compat.onnxruntime.lua‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
-- compat.onnxruntime — ONNX Runtime 1.31.0, built by the upstream CMake
2+
-- project with the CPU execution provider.
3+
--
4+
-- ONNX Runtime is not a source-glob compat package: its build generates
5+
-- protobuf/flatbuffers sources and fetches a fixed dependency graph from
6+
-- cmake/external/deps.txt. install() therefore runs the upstream CMake build,
7+
-- then the mcpp target links the installed shared library.
8+
package = {
9+
spec = "1",
10+
namespace = "compat",
11+
name = "onnxruntime",
12+
description = "ONNX Runtime CPU inference runtime",
13+
licenses = {"MIT"},
14+
repo = "https://github.com/microsoft/onnxruntime",
15+
type = "package",
16+
17+
xpm = {
18+
linux = {
19+
deps = { "xim:cmake@latest" },
20+
["1.31.0"] = {
21+
url = "https://github.com/microsoft/onnxruntime/archive/refs/tags/v1.31.0.tar.gz",
22+
sha256 = "a7e84230b5d509e48fbd6b73c2453a56345ccb22da34a46dac1f0cb4a2d6507b",
23+
},
24+
},
25+
macosx = {
26+
deps = { "xim:cmake@latest" },
27+
["1.31.0"] = {
28+
url = "https://github.com/microsoft/onnxruntime/archive/refs/tags/v1.31.0.tar.gz",
29+
sha256 = "a7e84230b5d509e48fbd6b73c2453a56345ccb22da34a46dac1f0cb4a2d6507b",
30+
},
31+
},
32+
windows = {
33+
deps = { "xim:cmake@latest" },
34+
["1.31.0"] = {
35+
url = "https://github.com/microsoft/onnxruntime/archive/refs/tags/v1.31.0.tar.gz",
36+
sha256 = "a7e84230b5d509e48fbd6b73c2453a56345ccb22da34a46dac1f0cb4a2d6507b",
37+
},
38+
},
39+
},
40+
41+
mcpp = {
42+
language = "c++23",
43+
import_std = false,
44+
include_dirs = { "include" },
45+
generated_files = {
46+
["mcpp_generated/onnxruntime_anchor.c"] =
47+
"int mcpp_compat_onnxruntime_anchor(void) { return 0; }\n",
48+
},
49+
sources = { "mcpp_generated/onnxruntime_anchor.c" },
50+
targets = { ["onnxruntime"] = { kind = "lib" } },
51+
deps = {},
52+
linux = {
53+
ldflags = { "-Llib", "-lonnxruntime" },
54+
},
55+
macosx = {
56+
ldflags = { "-Llib", "-lonnxruntime" },
57+
},
58+
windows = {
59+
ldflags = { "-Llib", "-lonnxruntime" },
60+
},
61+
runtime = {
62+
library_dirs = { "lib" },
63+
},
64+
},
65+
}
66+
67+
import("xim.libxpkg.pkginfo")
68+
import("xim.libxpkg.log")
69+
70+
local function quote(value)
71+
return '"' .. tostring(value):gsub('"', '\\"') .. '"'
72+
end
73+
74+
local function resolve_cmake()
75+
local dep = pkginfo.build_dep("xim:cmake")
76+
if dep then
77+
local candidates = {}
78+
if dep.bin then
79+
candidates[#candidates + 1] = path.join(dep.bin, "cmake")
80+
candidates[#candidates + 1] = path.join(dep.bin, "cmake.exe")
81+
end
82+
if dep.path then
83+
candidates[#candidates + 1] =
84+
path.join(dep.path, "CMake.app", "Contents", "bin", "cmake")
85+
end
86+
for _, candidate in ipairs(candidates) do
87+
if os.isfile(candidate) then return candidate end
88+
end
89+
end
90+
return nil
91+
end
92+
93+
function install()
94+
local wrap = "onnxruntime-" .. pkginfo.version()
95+
local source = path.join(wrap, "cmake")
96+
if not os.isfile(path.join(source, "CMakeLists.txt")) then
97+
log.error("compat.onnxruntime: expected %s/CMakeLists.txt", source)
98+
return false
99+
end
100+
101+
local cmake = resolve_cmake()
102+
if not cmake then
103+
log.error("compat.onnxruntime: xim:cmake executable not found")
104+
return false
105+
end
106+
107+
local prefix = pkginfo.install_dir()
108+
local build = path.join(wrap, "mcpp-build")
109+
os.tryrm(prefix)
110+
os.mkdir(prefix)
111+
os.tryrm(build)
112+
113+
local jobs = (os.default_njob and os.default_njob()) or 4
114+
local configure = table.concat({
115+
quote(cmake), "-S", quote(source), "-B", quote(build),
116+
"-DCMAKE_BUILD_TYPE=Release",
117+
"-DCMAKE_INSTALL_PREFIX=" .. quote(prefix),
118+
"-Donnxruntime_BUILD_SHARED_LIB=ON",
119+
"-Donnxruntime_BUILD_UNIT_TESTS=OFF",
120+
"-Donnxruntime_ENABLE_PYTHON=OFF",
121+
"-Donnxruntime_BUILD_CSHARP=OFF",
122+
"-Donnxruntime_BUILD_JAVA=OFF",
123+
"-Donnxruntime_BUILD_NODEJS=OFF",
124+
"-Donnxruntime_BUILD_OBJC=OFF",
125+
"-Donnxruntime_BUILD_APPLE_FRAMEWORK=OFF",
126+
"-Donnxruntime_USE_CUDA=OFF",
127+
"-Donnxruntime_USE_TENSORRT=OFF",
128+
"-Donnxruntime_USE_OPENVINO=OFF",
129+
"-Donnxruntime_USE_COREML=OFF",
130+
"-Donnxruntime_USE_DNNL=OFF",
131+
"-Donnxruntime_USE_XNNPACK=OFF",
132+
"-Donnxruntime_USE_MIMALLOC=OFF",
133+
"-Donnxruntime_USE_VCPKG=OFF",
134+
}, " ")
135+
local function run(command)
136+
if os.host() == "windows" then
137+
local bat = path.join(prefix, "mcpp_onnxruntime_command.bat")
138+
io.writefile(bat, "@echo off\r\n" .. command
139+
.. "\r\nif errorlevel 1 exit /b 1\r\n")
140+
command = "cmd /c " .. quote(bat)
141+
end
142+
return pcall(os.exec, command)
143+
end
144+
145+
local invoked, result = run(configure)
146+
if not invoked or not result then
147+
log.error("compat.onnxruntime: CMake configure failed")
148+
return false
149+
end
150+
151+
local build_cmd = string.format("%s --build %s --parallel %d",
152+
quote(cmake), quote(build), jobs)
153+
invoked, result = run(build_cmd)
154+
if not invoked or not result then
155+
log.error("compat.onnxruntime: CMake build failed")
156+
return false
157+
end
158+
159+
local install_cmd = string.format("%s --install %s",
160+
quote(cmake), quote(build))
161+
invoked, result = run(install_cmd)
162+
if not invoked or not result then
163+
log.error("compat.onnxruntime: CMake install failed")
164+
return false
165+
end
166+
167+
if not os.isfile(path.join(prefix, "include", "onnxruntime", "core",
168+
"session", "onnxruntime_c_api.h")) then
169+
log.error("compat.onnxruntime: installed C API header not found")
170+
return false
171+
end
172+
return os.isfile(path.join(prefix, "lib", "libonnxruntime.so"))
173+
or os.isfile(path.join(prefix, "lib", "libonnxruntime.dylib"))
174+
or os.isfile(path.join(prefix, "lib", "onnxruntime.lib"))
175+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "onnxruntime-tests"
3+
version = "0.1.0"
4+
5+
[dependencies.compat]
6+
onnxruntime = "1.31.0"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <cassert>
2+
#include <cstring>
3+
4+
#include <onnxruntime/core/session/onnxruntime_c_api.h>
5+
6+
int main() {
7+
const OrtApiBase* base = OrtGetApiBase();
8+
assert(base != nullptr);
9+
const char* version = base->GetVersionString();
10+
assert(version != nullptr);
11+
assert(std::strncmp(version, "1.31.", 5) == 0);
12+
return 0;
13+
}

0 commit comments

Comments
 (0)