|
| 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 |
0 commit comments