Skip to content

c/BUILD: add public shared-library target (libLiteRtLm.so / .dylib / .dll)#2155

Open
DenisovAV wants to merge 1 commit intogoogle-ai-edge:mainfrom
DenisovAV:feat/shared-library-target
Open

c/BUILD: add public shared-library target (libLiteRtLm.so / .dylib / .dll)#2155
DenisovAV wants to merge 1 commit intogoogle-ai-edge:mainfrom
DenisovAV:feat/shared-library-target

Conversation

@DenisovAV
Copy link
Copy Markdown

Summary

Adds a cc_binary(linkshared=True, name="LiteRtLm") target to c/BUILD that produces a flat shared library with only LiteRt* / litert_lm_* symbols exported. This unblocks every cross-language consumer of LiteRT-LM that wants to ship through a native FFI (Dart, Python, Node, Rust, .NET) without pulling Bazel into their build pipeline.

Closes #2154.

What's added

Three files, all under c/:

  1. c/BUILD — new `cc_binary` target named `LiteRtLm`. Bazel adds the platform-specific suffix automatically:
    • `libLiteRtLm.so` on Linux / Android
    • `libLiteRtLm.dylib` on macOS / iOS
    • `LiteRtLm.dll` on Windows
  2. `c/dynamic_list.lds` — Linux linker dynamic-list. Extends the default visibility set with `LiteRt*` / `litert_lm_*` so plugin-internal `dlsym(RTLD_DEFAULT)` lookups (e.g. WebGPU accelerator auto-registration) keep resolving — important: it does NOT replace the default set, so absl/protobuf internals stay visible to internal-internal traffic but not to external consumers (consumers see only the C API).
  3. `c/windows_exports.def` — Windows DLL whitelist. One entry per public C API function; new C API additions to `engine.h` need a corresponding entry here or the build fails with LNK2019.

Per-platform export mechanism in the `linkopts` select:

Platform Mechanism Notes
macOS / iOS `-Wl,-exported_symbol,_LiteRt*` + `litert_lm*` Two glob patterns, matches the C API surface
Linux `-Wl,--dynamic-list=...` + `.lds` Extends visibility, doesn't replace
Windows `win_def_file` attribute Bazel native; `/DEF:` in linkopts gets clobbered by the MSVC link.exe action

What's NOT changed

  • Existing `:engine` and `:engine_cpu` cc_library targets — unchanged.
  • C API surface — no new functions; everything in the .def / dynamic_list is already in `engine.h`.
  • Android NDK build — still uses its own `runtime/engine/litert_lm_android` path.

Test plan

  • flutter_gemma carries this exact pattern downstream since v0.14.0 (`patch_c_api.sh` section 1) on all five platforms; the produced `libLiteRtLm.so` / `.dylib` / `.dll` work for Dart FFI `DynamicLibrary.open` + the C API symbol surface.
  • WebGPU accelerator plugin auto-registration works on Linux with `--dynamic-list` (it relies on `dlsym(RTLD_DEFAULT)` resolving `LiteRt*` symbols against the loaded `libLiteRtLm.so` because `Dart's DynamicLibrary.open` uses RTLD_LOCAL — verified on Linux T4 + L4 GPU VMs).
  • App Store distribution works on iOS/macOS — symbol whitelist keeps `Frameworks/` clean.
  • Upstream CI on this branch.

Cross-references

CLA

Will sign Individual CLA before merge if needed; flagging upfront.

Existing c/BUILD only exposes the C API via cc_library, which forces
every cross-language consumer (Dart FFI, Python ctypes, Node N-API,
Rust bindgen, .NET P/Invoke) to either carry a downstream patch that
adds a cc_binary(linkshared=True) target, or pull Bazel and rebuild
LiteRT-LM from source per platform.

Add a new `:LiteRtLm` cc_binary target that produces:
  - libLiteRtLm.so on Linux / Android
  - libLiteRtLm.dylib on macOS / iOS
  - LiteRtLm.dll on Windows

with only `LiteRt*` / `litert_lm_*` symbols exported. Per-platform
export mechanism:
  - macOS / iOS: -Wl,-exported_symbol,_LiteRt* / _litert_lm_*
  - Linux: --dynamic-list extending the default visibility set with
    LiteRt* / litert_lm_* (so plugins that dlsym(RTLD_DEFAULT) keep
    working — e.g. WebGPU accelerator auto-registration)
  - Windows: native win_def_file attribute pointing at the .def
    whitelist (Bazel's MSVC link.exe action owns the linker
    invocation, so /DEF: in linkopts gets clobbered)

The other symbols (absl, protobuf, internal helpers) stay hidden so
consumer-side dlopen of LiteRtLm doesn't collide with whatever the
consumer also links.

Closes google-ai-edge#2154.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Public shared-library build target for the C API (libLiteRtLm.so / .dylib / .dll)

1 participant