@gjsify/sherpa-native 0.0.0-stage → 0.64.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/meson.build ADDED
@@ -0,0 +1,563 @@
1
+ # GjsifySherpa — sherpa-onnx's speech engines behind a GObject API: a Silero
2
+ # voice activity detector and two transducer recognizers (ADR 0127 decision 5).
3
+ #
4
+ # `buildtype=plain` = NO `-g`, the same choice as the sibling Vala bridges. ld64
5
+ # emits a DEBUG MAP into any image whose objects carry DWARF — one `N_OSO` stab
6
+ # per object, holding its MTIME — so with `-g` every rebuild of one commit
7
+ # differs in bytes and `commit-prebuilds` re-commits the lot. It matters more
8
+ # here than anywhere else in this repo: sherpa-onnx and onnxruntime are compiled
9
+ # INTO this library, so their debug info would be this prebuild's size.
10
+ # Why: docs/prebuilds.md.
11
+ #
12
+ # `cpp` is in the language list although this package contains no C++ source:
13
+ # sherpa-onnx and onnxruntime are C++ static archives, and a target whose own
14
+ # sources are only C and Vala would be linked with the C driver — which does not
15
+ # add the C++ runtime, so the link fails on a few thousand undefined
16
+ # `std::` symbols. `link_language: 'cpp'` on the library below is the fix, and it
17
+ # requires the language to be declared here.
18
+ project('GjsifySherpa', ['c', 'cpp', 'vala'], version: '1.0',
19
+ meson_version: '>= 1.3.0',
20
+ default_options: ['c_std=c11', 'cpp_std=c++17', 'buildtype=plain'])
21
+
22
+ root_dir = meson.current_source_dir()
23
+
24
+ cc = meson.get_compiler('c')
25
+ cmake = import('cmake')
26
+ fs = import('fs')
27
+
28
+ host_system = host_machine.system()
29
+ is_darwin = host_system == 'darwin'
30
+ is_linux = host_system == 'linux'
31
+
32
+ if not (is_darwin or is_linux)
33
+ # Not "unsupported forever": sherpa-onnx builds on Windows and the C API is
34
+ # the same. It is that nobody has done the work, and package.json says so in
35
+ # `gjsify.platformsUncommitted` rather than letting a build here imply it.
36
+ error('GjsifySherpa is built for linux and darwin. ' + host_system +
37
+ ' has had no portability work — see package.json gjsify.platformsUncommitted.')
38
+ endif
39
+
40
+ # ----------------------------------------------------------------------------
41
+ # § onnxruntime — the inference engine, SHARED, by an OVERLAY on the wrap.
42
+ #
43
+ # Everything else in this prebuild is static; this one library cannot be.
44
+ # subprojects/sherpa-onnx.wrap carries the measurement: the `libonnxruntime.a`
45
+ # that upstream's static branch links was built by an old GCC and collides,
46
+ # symbol for symbol, with the host GCC's own vague-linkage instantiations of
47
+ # `std::__detail::_Scanner<char>` — same mangled name, different layout, because
48
+ # libstdc++ puts `regex_traits` in `std::__cxx11` and `_Scanner` in plain
49
+ # `std::__detail`. The result is not a link error but a corrupted heap on the
50
+ # first session onnxruntime opens.
51
+ #
52
+ # HOW THE SHARED ENGINE GETS IN, and the two ways that did not work:
53
+ #
54
+ # 1. `BUILD_SHARED_LIBS=ON` — makes sherpa-onnx itself eleven shared libraries
55
+ # too, and this package's premise is one `.so` behind a version script with
56
+ # nothing of the engines visible. Rejected on that, not on difficulty.
57
+ # 2. A separate onnxruntime wrap handed over as
58
+ # `SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE=ON` plus
59
+ # `location_onnxruntime_header_dir` / `location_onnxruntime_lib` cache
60
+ # defines. Configures cleanly and is the arrangement upstream documents —
61
+ # but meson then DROPPED the include directory it was handed:
62
+ # `WARNING: CMake: path ... is inside the root project but not inside the
63
+ # subproject -> Ignoring`, and sherpa-onnx's own compile died on
64
+ # `fatal error: onnxruntime_cxx_api.h: No such file or directory`. meson's
65
+ # cmake module REGENERATES the subproject's targets as meson targets rather
66
+ # than delegating to cmake, and it will only carry include paths that live
67
+ # inside the subproject's own build directory.
68
+ #
69
+ # Which is what settled it: FetchContent lands its download inside the
70
+ # subproject's build directory, so letting sherpa-onnx fetch the SHARED archive
71
+ # through its own FetchContent is the one route whose include path meson keeps.
72
+ # The overlay in subprojects/packagefiles/ is exactly that and nothing more.
73
+ #
74
+ # Coverage is per OVERLAY FILE, one per architecture, since each pins a binary
75
+ # release archive. A linux arch with no overlay is an ERROR rather than a
76
+ # fallback to upstream's static file — that fallback IS the broken arrangement,
77
+ # and finding out by way of a corrupted heap is what this check exists to prevent.
78
+ # ----------------------------------------------------------------------------
79
+ onnxruntime_overlays = {'x86_64': 'onnxruntime-linux-x86_64-static.cmake'}
80
+
81
+ if is_linux and not onnxruntime_overlays.has_key(host_machine.cpu_family())
82
+ error(
83
+ 'No shared-onnxruntime overlay for linux/' + host_machine.cpu_family() + '.\n' +
84
+ ' Copy subprojects/sherpa-onnx/cmake/onnxruntime-linux-<arch>.cmake (the SHARED\n' +
85
+ ' one) to subprojects/packagefiles/sherpa-onnx-shared-onnxruntime/cmake/ under the\n' +
86
+ ' name of the -static one, drop its BUILD_SHARED_LIBS guard, and name it in\n' +
87
+ ' `onnxruntime_overlays` here. Do NOT drop this check and let sherpa-onnx link its\n' +
88
+ ' static archive instead — see subprojects/sherpa-onnx.wrap for what that does to a\n' +
89
+ ' build by a current GCC.',
90
+ )
91
+ endif
92
+
93
+ # ----------------------------------------------------------------------------
94
+ # sherpa-onnx — pinned by subprojects/sherpa-onnx.wrap, consumed as a CMAKE
95
+ # subproject because it has no meson build. Same arrangement as `sdl3.wrap` in
96
+ # packages/web/gamepad-native.
97
+ #
98
+ # STATIC for sherpa-onnx itself (`BUILD_SHARED_LIBS=OFF`), so its eleven archives
99
+ # and this shim become one `libgjsifysherpa.so` behind a version script.
100
+ # onnxruntime is the one exception and is linked SHARED — it has to be, because
101
+ # the static archive upstream would otherwise select cannot be linked into C++
102
+ # built by a current GCC without corrupting memory. The mechanism, the crash it
103
+ # produced and why the shared library is the correct arrangement rather than a
104
+ # workaround are in subprojects/sherpa-onnx.wrap; § onnxruntime above is how
105
+ # this build gets there.
106
+ #
107
+ # Everything not reached from the C API's VAD and transducer paths is off:
108
+ # PortAudio and the websocket server are programs, not libraries; TTS would drag
109
+ # in espeak-ng, piper-phonemize and cppinyin; speaker diarization is another
110
+ # model family nobody here runs. What stays, and still gets fetched by
111
+ # sherpa-onnx's own CMake, is kaldi-native-fbank, kaldi-decoder, utf8proc,
112
+ # simple-sentencepiece and nlohmann/json.
113
+ # ----------------------------------------------------------------------------
114
+ sherpa_defines = {
115
+ 'BUILD_SHARED_LIBS': false,
116
+ 'SHERPA_ONNX_ENABLE_C_API': true,
117
+ 'SHERPA_ONNX_ENABLE_PORTAUDIO': false,
118
+ 'SHERPA_ONNX_ENABLE_WEBSOCKET': false,
119
+ 'SHERPA_ONNX_ENABLE_BINARY': false,
120
+ 'SHERPA_ONNX_ENABLE_TTS': false,
121
+ 'SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION': false,
122
+ 'SHERPA_ONNX_ENABLE_TESTS': false,
123
+ 'SHERPA_ONNX_ENABLE_PYTHON': false,
124
+ 'SHERPA_ONNX_ENABLE_JNI': false,
125
+ 'SHERPA_ONNX_BUILD_C_API_EXAMPLES': false,
126
+ # The static archives are linked into a shared library.
127
+ 'CMAKE_POSITION_INDEPENDENT_CODE': true,
128
+ }
129
+
130
+ # ----------------------------------------------------------------------------
131
+ # THE SYSTEM-ONNXRUNTIME TRAP, disarmed.
132
+ #
133
+ # OFF is already the upstream default, and it is set here anyway because ON is
134
+ # the one thing that would quietly undo § onnxruntime. Left to search, that
135
+ # option runs `find_path`/`find_library` over `/usr/include/onnxruntime` and
136
+ # `/usr/lib`: Fedora 44 HAS libonnxruntime.so.1 (1.22.2) and NOT its headers, so
137
+ # the find half succeeds on some hosts and the compile fails on them, and where
138
+ # it does work the prebuild carries a dependency on whatever onnxruntime the
139
+ # build machine happened to have. OFF means the version is the wrap's, always.
140
+ # ----------------------------------------------------------------------------
141
+ sherpa_defines += {'SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE': false}
142
+
143
+ # ----------------------------------------------------------------------------
144
+ # THE LIBSTDC++ DECISION, and why it is probed rather than chosen.
145
+ #
146
+ # sherpa-onnx defaults `SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY` to ON for a static
147
+ # build, and for a REDISTRIBUTABLE prebuild that default is the one worth having.
148
+ # Linking libstdc++ dynamically gives the `.so` a DT_NEEDED on libstdc++.so.6
149
+ # together with a GLIBCXX_ version floor set by whatever GCC built it — this host
150
+ # is Fedora 44 with GCC 16, so a prebuild from here would refuse to load on
151
+ # Debian 12 (GLIBCXX_3.4.30). Static libstdc++ has no floor, and it is safe here
152
+ # precisely because the version script exports only this library's C API: no C++
153
+ # type, exception or `std::` symbol crosses the boundary, so there is nothing for
154
+ # a second C++ runtime in the process to disagree with.
155
+ #
156
+ # It needs `libstdc++.a`, which Fedora ships in `libstdc++-static` and does NOT
157
+ # install by default. Without it the build does not fail here — it fails four
158
+ # dependencies deep, in simple-sentencepiece's `check_cxx_compiler_flag`, as
159
+ # "requires a compiler supporting at least C++14. If you are using GCC, please
160
+ # upgrade it to at least version 7.0". That message blames the compiler for a
161
+ # missing archive and sent this build down the wrong path once already, so the
162
+ # probe below answers the question up front and names the package.
163
+ #
164
+ # A host without it still gets a WORKING build, just not a shippable one, so a
165
+ # contributor's laptop gets a warning. The prebuild legs are the place where a
166
+ # non-portable artifact is worse than no artifact, and they pass
167
+ # `-Drequire_static_libstdcxx=true` to make the same probe a hard error.
168
+ # ----------------------------------------------------------------------------
169
+ libstdcxx_a = run_command(cc.cmd_array() + ['-print-file-name=libstdc++.a'], check: false)
170
+ # gcc echoes the bare filename back when it cannot find it, so "is the answer an
171
+ # absolute path that exists" is the test — not the exit status, which is 0 either way.
172
+ libstdcxx_a_path = libstdcxx_a.stdout().strip()
173
+ have_static_libstdcxx = libstdcxx_a.returncode() == 0 \
174
+ and libstdcxx_a_path.startswith('/') \
175
+ and fs.exists(libstdcxx_a_path)
176
+
177
+ if not have_static_libstdcxx
178
+ libstdcxx_a_message = 'libstdc++.a not found — building against the SHARED libstdc++.\n' + \
179
+ ' The resulting library carries a DT_NEEDED on libstdc++.so.6 plus this\n' + \
180
+ ' compiler\'s GLIBCXX_ floor, so it is NOT a portable prebuild. Install it:\n' + \
181
+ ' Fedora/RHEL dnf install libstdc++-static\n' + \
182
+ ' Debian/Ubuntu already present in libstdc++-*-dev\n' + \
183
+ ' Alpine apk add libstdc++-dev'
184
+ if get_option('require_static_libstdcxx')
185
+ error(libstdcxx_a_message)
186
+ endif
187
+ warning(libstdcxx_a_message)
188
+ endif
189
+
190
+ sherpa_defines += {'SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY': have_static_libstdcxx}
191
+
192
+ sherpa_options = cmake.subproject_options()
193
+ sherpa_options.add_cmake_defines(sherpa_defines)
194
+ if is_linux
195
+ # Lets --gc-sections (below) drop what the shim never reaches. sherpa-onnx
196
+ # compiles every model family it supports, and this bridge calls into three
197
+ # of them.
198
+ sherpa_options.append_compile_args('c', ['-ffunction-sections', '-fdata-sections'])
199
+ sherpa_options.append_compile_args('cpp', ['-ffunction-sections', '-fdata-sections'])
200
+ endif
201
+
202
+ sherpa = cmake.subproject('sherpa-onnx', options: sherpa_options)
203
+ sherpa_dep = sherpa.dependency('sherpa-onnx-c-api')
204
+
205
+ # sherpa-onnx sets its include path with a directory-scoped
206
+ # `include_directories()`, which is not a target property and so is not
207
+ # something meson's cmake module can read off the target. The header this shim
208
+ # includes is `<sherpa-onnx/c-api/c-api.h>`, relative to the wrap checkout root.
209
+ # The checkout exists by now: cmake.subproject() above is what fetches it.
210
+ sherpa_inc = include_directories('subprojects' / 'sherpa-onnx')
211
+
212
+ # ----------------------------------------------------------------------------
213
+ # Staging the engine beside the library.
214
+ #
215
+ # libonnxruntime.so has to sit NEXT TO libgjsifysherpa.so in the prebuild:
216
+ # DT_NEEDED records its SONAME, and RUNPATH `$ORIGIN` resolves that against the
217
+ # library's own directory. `scripts/stage-prebuild.mjs` stages the `.so`/`.gir`/
218
+ # `.typelib` files it finds in the TOP LEVEL of the build directory, so the copy
219
+ # is a build step rather than something a packaging script has to know how to go
220
+ # looking for.
221
+ #
222
+ # The source path is a meson INTERNAL — where its cmake module puts the CMake
223
+ # build tree, and inside that where sherpa-onnx's FetchContent puts the archive
224
+ # it unpacked. Both are stable but neither is API, so `fs.exists()` checks it and
225
+ # says what to look for when it moves. `cp` through a `custom_target` rather than
226
+ # `fs.copyfile`: taking a file out of a nested subproject is a sandbox violation
227
+ # there (rightly, in general — but this is a byte copy of a binary release
228
+ # artifact), and the path therefore goes in as a plain string, which also means
229
+ # there is no dependency edge back to it.
230
+ #
231
+ # AND IT IS STRIPPED ON THE WAY. The upstream release is shipped "not stripped":
232
+ # 27,026,609 bytes, of which a `.symtab` plus a 4.9 MB `.strtab` are a debug
233
+ # symbol table nothing in this prebuild can use — `strip --strip-all` measured
234
+ # 21,007,808, so 6.0 MB (22 %) of the biggest file in the artifact is dead weight
235
+ # that `commit-prebuilds` would write into git on every rebuild.
236
+ # `libgjsifysherpa.so` is already stripped by `-Wl,--strip-all` at link time; this
237
+ # is the same decision applied to the one file in the prebuild that is not ours to
238
+ # link.
239
+ #
240
+ # `strip` is PROBED rather than assumed, like libstdc++.a above: a host without
241
+ # binutils must still get a working build, so staging falls back to the plain `cp`
242
+ # with a warning. The `elf-deps` test gets `--require-stripped-engine` only when
243
+ # the probe found it, so the assertion is made exactly where the saving was taken.
244
+ # ----------------------------------------------------------------------------
245
+ strip_prog = find_program('strip', required: false)
246
+
247
+ if is_linux and not strip_prog.found()
248
+ warning(
249
+ 'strip not found — staging the engine unstripped.\n' +
250
+ ' libonnxruntime.so stays ~6 MB larger than it needs to be, which costs nothing\n' +
251
+ ' locally and 6 MB per rebuild in a committed prebuild. Install binutils.',
252
+ )
253
+ endif
254
+
255
+ if is_linux
256
+ onnxruntime_built = meson.project_build_root() / 'subprojects' / 'sherpa-onnx' \
257
+ / '__CMake_build' / '_deps' / 'onnxruntime-src' / 'lib' / 'libonnxruntime.so'
258
+
259
+ if not fs.exists(onnxruntime_built)
260
+ error(
261
+ 'The shared onnxruntime is not where this build expects it:\n' +
262
+ ' ' + onnxruntime_built + '\n' +
263
+ ' That path is two meson/cmake internals deep — where the cmake module puts the\n' +
264
+ ' CMake build tree, and where sherpa-onnx\'s FetchContent unpacks onnxruntime.\n' +
265
+ ' Find the real one with `find ' + meson.project_build_root() +
266
+ ' -name libonnxruntime.so`\n' +
267
+ ' and correct the path here. If there is no such file at all, the overlay in\n' +
268
+ ' subprojects/packagefiles/ did not apply — `rm -rf subprojects/sherpa-onnx` and\n' +
269
+ ' reconfigure, since meson only patches a checkout it creates itself.',
270
+ )
271
+ endif
272
+
273
+ # `strip -o` writes a stripped COPY and leaves the input alone, so this is
274
+ # still one command producing one output — the subproject's file is never
275
+ # modified in place, which would make the build non-idempotent.
276
+ onnxruntime_staged = custom_target(
277
+ 'libonnxruntime.so',
278
+ output: 'libonnxruntime.so',
279
+ command: strip_prog.found()
280
+ ? [strip_prog, '--strip-all', '-o', '@OUTPUT@', onnxruntime_built]
281
+ : [find_program('cp'), onnxruntime_built, '@OUTPUT@'],
282
+ build_by_default: true,
283
+ )
284
+ endif
285
+
286
+ # ----------------------------------------------------------------------------
287
+ # On darwin every GNOME library resolves against the HOST PROCESS and is NOT
288
+ # recorded as an `@rpath/` dependency. A Mach-O keys each dependency by the path
289
+ # that resolves it, so an `LC_RPATH` entry carrying its own copy gives the
290
+ # process a SECOND GLib: two GType registries, then SIGSEGV.
291
+ # docs/prebuilds.md § One GLib.
292
+ #
293
+ # `glib_deps` keeps the unstripped form — the test executables below are their
294
+ # own host process and link GLib for real.
295
+ # ----------------------------------------------------------------------------
296
+ host_provided = ['glib-2.0', 'gobject-2.0']
297
+
298
+ glib_deps = []
299
+ vala_deps = []
300
+ foreach pkg : host_provided
301
+ dep = dependency(pkg)
302
+ glib_deps += dep
303
+ vala_deps += is_darwin \
304
+ ? dep.partial_dependency(compile_args: true, includes: true) \
305
+ : dep
306
+ endforeach
307
+
308
+ c_args = []
309
+ link_args = []
310
+
311
+ if is_darwin
312
+ link_args += [
313
+ # The other half of the host-GLib arrangement: the GNOME symbols the
314
+ # stripped dependencies no longer link stay undefined here and are bound
315
+ # by dyld to the copy the host already loaded.
316
+ '-Wl,-undefined,dynamic_lookup',
317
+ # Only this library's own API leaves it. Mach-O has a two-level
318
+ # namespace, so this is about size and tidiness rather than the
319
+ # correctness problem it is on ELF (below) — `-dead_strip` can drop
320
+ # every sherpa-onnx and onnxruntime function the shim does not reach
321
+ # once they are not exported, and that is most of the image.
322
+ '-Wl,-exported_symbol,_gjsify_sherpa_*',
323
+ '-Wl,-dead_strip',
324
+ '-Wl,-dead_strip_dylibs',
325
+ # Drops the debug map, whose N_OSO entries carry object-file mtimes.
326
+ '-Wl,-S',
327
+ # Room for the rpaths stage-prebuild.mjs rewrites; fixed at link time.
328
+ '-Wl,-headerpad_max_install_names',
329
+ ]
330
+ else
331
+ # ELF HAS ONE FLAT SYMBOL NAMESPACE, so hiding the vendored engines is not
332
+ # an optimisation here, it is correctness. An exported `OrtGetApiBase` in
333
+ # this library would interpose on — or be interposed by — any other
334
+ # onnxruntime the process loads, and the development host ships
335
+ # libonnxruntime.so.1 (1.22.2) against the 1.28.2 compiled in here.
336
+ # --exclude-libs makes every symbol from a static archive local, the version
337
+ # script exports this library's API and nothing else, and
338
+ # test/check-elf-deps.py reads both back from the built file.
339
+ version_script = root_dir / 'src' / 'c' / 'gjsify-sherpa.map'
340
+ link_args += [
341
+ '-Wl,--exclude-libs,ALL',
342
+ '-Wl,--version-script=' + version_script,
343
+ '-Wl,--gc-sections',
344
+ '-Wl,--as-needed',
345
+ # `--strip-all`, where the sibling bridges use `--strip-debug`, and the
346
+ # difference is worth 6 MB of the 33 this library weighs: the engines are
347
+ # compiled in, so its `.symtab` is sherpa-onnx's and onnxruntime's
348
+ # complete static symbol table, and nothing reads it. `.dynsym` is
349
+ # untouched by this — it is what the dynamic linker binds and what
350
+ # test/check-elf-deps.py reads back — so the exports and the
351
+ # engine-hiding this file is careful about are measured exactly as
352
+ # before. This is the largest native artifact in the repo by some way,
353
+ # and it is the npm tarball a consumer downloads.
354
+ '-Wl,--strip-all',
355
+ ]
356
+ c_args += ['-ffunction-sections', '-fdata-sections']
357
+ vala_deps += [cc.find_library('m'), cc.find_library('dl', required: false)]
358
+
359
+ # The final link is ours, not sherpa-onnx's, so the C++ runtime choice made
360
+ # above has to be repeated here — the CMake subproject's flags apply to its
361
+ # own objects. See § THE LIBSTDC++ DECISION.
362
+ #
363
+ # `-static-libstdc++` alone is not enough: the CMake dependency appends a
364
+ # bare `-lstdc++` (CMake's implicit C++ link libraries), and `ld` resolves
365
+ # that to `libstdc++.so` wherever the `.so` and the `.a` share a directory —
366
+ # which is exactly the Fedora layout once `libstdc++-static` is installed. The
367
+ # result was a DT_NEEDED on libstdc++.so.6 in CI while a laptop with the
368
+ # archive unpacked elsewhere linked clean. A `-L` of our own cannot win: the
369
+ # same dependency puts the compiler's implicit directories first. These args
370
+ # land inside its `--start-group`, ahead of that `-lstdc++`, so naming the
371
+ # probed archive here resolves every C++ symbol from it, and `--as-needed`
372
+ # then drops the `.so`, which no longer satisfies anything.
373
+ if have_static_libstdcxx
374
+ link_args += [libstdcxx_a_path, '-static-libstdc++', '-static-libgcc']
375
+ endif
376
+ endif
377
+
378
+ vala_deps += dependency('threads')
379
+
380
+ # The library has to be findable beside its typelib — what
381
+ # check-prebuild-loader-path.mjs enforces on every shipped prebuild.
382
+ rpath = is_darwin ? '@loader_path' : '$ORIGIN'
383
+
384
+ shim_inc = include_directories('src/c')
385
+
386
+ # ----------------------------------------------------------------------------
387
+ # The shim as its own static library, so that its OBJECTS exist as a thing both
388
+ # the shared library and the C test can pull in.
389
+ #
390
+ # `extract_all_objects()` is why. The smoke test links the shim's objects rather
391
+ # than the shared library, to get underneath the version script that exists to
392
+ # hide the engines (test/shim-smoke.c says so at the top). Extracting from a
393
+ # target with VALA sources does not work: meson lists the Vala-generated
394
+ # `.c.o` files among the objects, but ninja has no rule that builds them outside
395
+ # the library target, so the test fails to configure with "missing and no known
396
+ # rule to make it". A C-only target has no such problem — which is how
397
+ # packages/web/gamepad-native gets away with extracting from its library
398
+ # directly.
399
+ #
400
+ # The shared library takes the objects, NOT `link_whole`. An archive on the link
401
+ # line is what `-Wl,--exclude-libs,ALL` (above) acts on, and it would make every
402
+ # shim symbol local; as plain objects they stay subject to the version script,
403
+ # which is the single place this library's exports are decided.
404
+ # ----------------------------------------------------------------------------
405
+ shim = static_library('gjsify-sherpa-shim', 'src/c/gjsify-sherpa-shim.c',
406
+ dependencies: glib_deps + [sherpa_dep],
407
+ include_directories: [sherpa_inc, shim_inc],
408
+ c_args: c_args,
409
+ pic: true,
410
+ )
411
+
412
+ sources = files(
413
+ 'src/vala/sherpa.vala',
414
+ 'src/vala/vad.vala',
415
+ 'src/vala/online-recognizer.vala',
416
+ 'src/vala/offline-recognizer.vala',
417
+ )
418
+
419
+ libGjsifySherpa = library('gjsifysherpa', sources,
420
+ objects: shim.extract_all_objects(recursive: false),
421
+ dependencies: vala_deps + [sherpa_dep],
422
+ include_directories: [sherpa_inc, shim_inc],
423
+ c_args: c_args,
424
+ link_args: link_args,
425
+ link_depends: is_linux ? files('src/c/gjsify-sherpa.map') : [],
426
+ vala_args: [
427
+ # The shim's VAPI is not installed and not a pkg-config package: it
428
+ # describes C that is compiled into this very library (src/c), so valac
429
+ # is pointed at the source directory rather than at a vapi search path.
430
+ '--vapidir=' + root_dir / 'src/vala',
431
+ '--pkg=sherpa-shim',
432
+ # valac emits `#include "gjsify-sherpa-shim.h"` from the VAPI's
433
+ # cheader_filename; src/c is on the include path above.
434
+ ],
435
+ vala_gir: meson.project_name() + '-1.0.gir',
436
+ # The engines are C++ archives; without this meson links with the C driver
437
+ # and the C++ runtime is simply absent. See the note on project() above.
438
+ link_language: 'cpp',
439
+ install: true,
440
+ install_dir: [true, true, true, true],
441
+ install_rpath: rpath,
442
+ build_rpath: rpath,
443
+ )
444
+
445
+ # ----------------------------------------------------------------------------
446
+ # Host-dependent shared-library leaf name.
447
+ #
448
+ # The typelib records the exact filename g_module_open() resolves at load time,
449
+ # and meson names the library per host convention. Recording the wrong leaf
450
+ # yields a typelib that IS found via GI_TYPELIB_PATH but whose library never
451
+ # loads — a silent failure. Mirrors packages/node/sqlite-native/meson.build.
452
+ # ----------------------------------------------------------------------------
453
+ shared_lib_name = is_darwin ? 'libgjsifysherpa.dylib' : 'libgjsifysherpa.so'
454
+
455
+ g_ir_compiler = find_program('g-ir-compiler', 'gi-compile-repository')
456
+
457
+ typelib = custom_target(meson.project_name() + '-1.0.typelib',
458
+ command: [
459
+ g_ir_compiler,
460
+ '--shared-library', shared_lib_name,
461
+ '--output', '@OUTPUT@',
462
+ meson.current_build_dir() / meson.project_name() + '-1.0.gir',
463
+ ],
464
+ output: meson.project_name() + '-1.0.typelib',
465
+ depends: libGjsifySherpa,
466
+ install: true,
467
+ install_dir: get_option('libdir') / 'girepository-1.0',
468
+ )
469
+
470
+ # ----------------------------------------------------------------------------
471
+ # Tests.
472
+ #
473
+ # NOTHING HERE DOWNLOADS A MODEL and nothing here opens an audio device. The
474
+ # models are hundreds of megabytes under licences that are not this repo's to
475
+ # redistribute (ADR 0127 decision 14), so what a model-less runner can prove is
476
+ # what runs by default: the engines are linked and answer, a missing model is
477
+ # refused by path rather than by crash, and — on ELF — that the vendored
478
+ # onnxruntime is invisible from outside the library.
479
+ #
480
+ # The one test that needs a real Silero model is registered too and SKIPS (exit
481
+ # 77) unless one is cached, so the gap is visible in the test report instead of
482
+ # being absent from it. It exists because opening a session is the ONLY thing
483
+ # that saw the static-onnxruntime heap corruption — see test/vad-model.gjs.js.
484
+ # ----------------------------------------------------------------------------
485
+ # `$ORIGIN` for the same reason the library has it: the engine is recorded by
486
+ # its unversioned SONAME, so the only thing that resolves it is the staged copy
487
+ # sitting beside the binary — and this executable is in the build root next to
488
+ # it. meson adds the build directory to an executable's rpath for the LIBRARY
489
+ # TARGETS it links; libonnxruntime.so arrives as a custom_target output, which
490
+ # meson has no way to know is a library, hence both the rpath and the `depends`.
491
+ smoke = executable('shim-smoke', 'test/shim-smoke.c',
492
+ objects: shim.extract_all_objects(recursive: false),
493
+ dependencies: glib_deps + vala_deps + [sherpa_dep],
494
+ include_directories: [sherpa_inc, shim_inc],
495
+ link_language: 'cpp',
496
+ build_rpath: rpath,
497
+ )
498
+ test('shim-smoke', smoke,
499
+ timeout: 120,
500
+ depends: is_linux ? [onnxruntime_staged] : [])
501
+
502
+ python3 = find_program('python3', required: false)
503
+ if is_linux and python3.found()
504
+ # The export and dependency read-back. python3 is `required: false` for the
505
+ # reason every optional tool in this repo's meson files is: a host without
506
+ # it must get a tree that builds with a test SKIPPED, not a configure error.
507
+ #
508
+ # The flag is passed only when libstdc++.a was actually found, so this
509
+ # asserts "no libstdc++.so.6 in DT_NEEDED" exactly on the builds where that
510
+ # is supposed to be true — and does not red a working non-shippable build.
511
+ elf_deps_args = [files('test/check-elf-deps.py'), libGjsifySherpa.full_path()]
512
+ if have_static_libstdcxx
513
+ elf_deps_args += ['--require-static-libstdcxx']
514
+ endif
515
+ # Same shape, one level down: the engine is only expected to be stripped on a
516
+ # build that had a `strip` to do it with.
517
+ if strip_prog.found()
518
+ elf_deps_args += ['--require-stripped-engine']
519
+ endif
520
+ # `onnxruntime_staged` is in `depends` because this test reads that file too
521
+ # — the sibling, its exports, its GLIBCXX floor and now its `.symtab`. It is
522
+ # `build_by_default`, so a full build has it either way; naming it is what
523
+ # makes `meson test elf-deps` on its own true as well.
524
+ test('elf-deps', python3,
525
+ args: elf_deps_args,
526
+ depends: [libGjsifySherpa, onnxruntime_staged])
527
+ endif
528
+
529
+ # The same engines through the typelib under gjs — i.e. the GIR this package's
530
+ # TypeScript actually imports. The build directory holds both the typelib and
531
+ # the library its `shared-library` names.
532
+ gjs = find_program('gjs', required: false)
533
+ if gjs.found()
534
+ # LD_LIBRARY_PATH as well as the rpath: the typelib names
535
+ # `libgjsifysherpa.so` and gjs resolves that from the process, not from a
536
+ # $ORIGIN the process does not have. It is also what finds the staged
537
+ # libonnxruntime.so beside it.
538
+ gjs_env = {
539
+ 'GI_TYPELIB_PATH': meson.current_build_dir(),
540
+ 'DYLD_LIBRARY_PATH': meson.current_build_dir(),
541
+ 'LD_LIBRARY_PATH': meson.current_build_dir(),
542
+ }
543
+
544
+ gjs_depends = [libGjsifySherpa, typelib]
545
+ if is_linux
546
+ gjs_depends += onnxruntime_staged
547
+ endif
548
+
549
+ test('api-gjs', gjs,
550
+ args: ['-m', files('test/api.gjs.js')],
551
+ depends: gjs_depends,
552
+ timeout: 120,
553
+ env: gjs_env)
554
+
555
+ # SKIPs (exit 77) without a cached model. `env` ADDS to the environment the
556
+ # test runner inherits, so GJSIFY_SHERPA_VAD_MODEL reaches the script
557
+ # without being named here.
558
+ test('vad-model-gjs', gjs,
559
+ args: ['-m', files('test/vad-model.gjs.js')],
560
+ depends: gjs_depends,
561
+ timeout: 180,
562
+ env: gjs_env)
563
+ endif
package/package.json CHANGED
@@ -1,6 +1,94 @@
1
1
  {
2
- "name": "@gjsify/sherpa-native",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@gjsify/sherpa-native",
3
+ "version": "0.64.16",
4
+ "description": "Optional Vala/GObject bridge over sherpa-onnx's C API, exposing the parts that cannot be expressed in JavaScript: a Silero voice activity detector and streaming/whole-utterance transducer recognizers, each an opaque handle configured through nested C structs. sherpa-onnx and onnxruntime are linked in statically, so the prebuild needs only the host GLib. Models are never bundled. Used by @gjsify/vad.",
5
+ "type": "module",
6
+ "main": "lib/esm/index.js",
7
+ "module": "lib/esm/index.js",
8
+ "types": "lib/types/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./lib/types/index.d.ts",
12
+ "default": "./lib/esm/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "lib",
17
+ "meson.build",
18
+ "src",
19
+ "subprojects",
20
+ "test"
21
+ ],
22
+ "gjsify": {
23
+ "platforms": [
24
+ "linux-x64"
25
+ ],
26
+ "runtimes": {
27
+ "gjs": "polyfill",
28
+ "node": "none",
29
+ "browser": "none",
30
+ "nativescript": "none"
31
+ },
32
+ "os": {
33
+ "linux": "partial",
34
+ "darwin": "none",
35
+ "win32": "none"
36
+ },
37
+ "osNotes": {
38
+ "linux": "Builds for linux-x64 against the host GLib and GObject only. sherpa-onnx 2.0.1 is linked in as a static archive behind a version script, but onnxruntime 1.28.2 is NOT: the prebuilt static archive and a current GCC define the same vague-linkage `std::__detail::_Scanner<char>` symbols under identical mangled names with incompatible layouts, and the deduped result corrupts the heap on the first inference session, so a wrap overlay switches it to the shared release and `libonnxruntime.so` ships beside the bridge, resolved through RUNPATH $ORIGIN and bound by version node VERS_1.28.2 — measured, and test/check-elf-deps.py reads both files back on every run. Measured on Fedora 44 / x86_64 / GCC 16: the engines report their versions, a missing model is refused by path, `gi://GjsifySherpa` drives the detector and both recognizers under gjs, and the detector finds the expected single segment in each of two real 16 kHz recordings. The RECOGNIZERS have not been run against real weights at all. No model may be committed or downloaded in the suite (ADR 0127 decision 14), so CI covers the model-less paths and the real-model VAD leg exits 77 (skip). linux-arm64 is not declared yet because nothing has built it, and the onnxruntime overlay is per architecture, so adding it means adding an aarch64 overlay file rather than only a matrix entry.",
39
+ "darwin": "Not built yet. sherpa-onnx builds on macOS and the C API is identical, so this is undone work rather than a limitation: it needs the CoreML provider decision, an `-exported_symbol` check to replace test/check-elf-deps.py (Mach-O, not ELF), and a runner. meson.build already carries the darwin branches.",
40
+ "win32": "Not built. sherpa-onnx supports MSVC, but neither the version-script equivalent nor a GIR/typelib path for this bridge has been done on win32."
41
+ },
42
+ "prebuildLicense": "MIT AND Apache-2.0",
43
+ "tier": 3
44
+ },
45
+ "scripts": {
46
+ "clear": "gjsify clear lib build tsconfig.tsbuildinfo",
47
+ "check": "gjsify tsc --noEmit",
48
+ "init:meson": "meson setup build .",
49
+ "init:meson:wipe": "gjsify run init:meson --wipe",
50
+ "build": "gjsify run build:gjsify && gjsify run build:types",
51
+ "build:gjsify": "gjsify build --library 'src/ts/**/*.{ts,js}'",
52
+ "build:types": "gjsify tsc -b --force",
53
+ "build:meson": "gjsify run init:meson && meson compile -C build",
54
+ "build:prebuilds": "gjsify run build:meson && node ../../../scripts/stage-prebuild.mjs .",
55
+ "build:gir-types": "ts-for-gir generate --externalDeps --allowMissingDeps --girDirectories=build --girDirectories=/usr/share/gir-1.0 --modules=GjsifySherpa-1.0 --outdir=src/ts --npmScope=@girs --package=false --ignoreVersionConflicts=true",
56
+ "test": "echo 'native only — see test:meson (runs on every prebuild leg)'",
57
+ "test:meson": "meson test -C build --print-errorlogs"
58
+ },
59
+ "keywords": [
60
+ "gjs",
61
+ "sherpa-onnx",
62
+ "vad",
63
+ "silero",
64
+ "speech-recognition",
65
+ "asr",
66
+ "vala",
67
+ "native"
68
+ ],
69
+ "dependencies": {
70
+ "@girs/gjs": "5.4.0",
71
+ "@girs/glib-2.0": "5.4.0",
72
+ "@girs/gobject-2.0": "5.4.0",
73
+ "@gjsify/utils": "^0.64.16"
74
+ },
75
+ "devDependencies": {
76
+ "@gjsify/cli": "^0.64.16",
77
+ "@ts-for-gir/cli": "^4.1.0",
78
+ "@types/node": "^25.9.2",
79
+ "typescript": "^6.0.3"
80
+ },
81
+ "license": "MIT",
82
+ "repository": {
83
+ "type": "git",
84
+ "url": "git+https://github.com/gjsify/gjsify.git",
85
+ "directory": "packages/web/sherpa-native"
86
+ },
87
+ "bugs": {
88
+ "url": "https://github.com/gjsify/gjsify/issues"
89
+ },
90
+ "homepage": "https://github.com/gjsify/gjsify/tree/main/packages/web/sherpa-native#readme",
91
+ "optionalDependencies": {
92
+ "@gjsify/sherpa-native-linux-x64": "0.64.16"
93
+ }
94
+ }