From 2beb1498fa22837b1ecee67172bd6e9a2ea7983e Mon Sep 17 00:00:00 2001 From: legop3 <46182676+legop3@users.noreply.github.com> Date: Tue, 7 Jul 2026 00:14:33 -0400 Subject: [PATCH] Preload compiler runtimes for Chrome TTS --- pi/bin/chromegtts-daemon.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/pi/bin/chromegtts-daemon.py b/pi/bin/chromegtts-daemon.py index 2eb7bd1f..1b842032 100644 --- a/pi/bin/chromegtts-daemon.py +++ b/pi/bin/chromegtts-daemon.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import ctypes +import ctypes.util import json import os import struct @@ -33,6 +34,40 @@ MAX_PITCH = 2.0 MIN_SPEED = 0.5 MAX_SPEED = 2.0 +_runtime_handles = [] + + +def load_shared_library(path): + mode = ctypes.RTLD_GLOBAL | getattr(os, "RTLD_NOW", 0) + return ctypes.CDLL(path, mode=mode) + + +def preload_runtime_libraries(): + # Some ChromeOS libchrometts builds reference compiler helper symbols such + # as __udivmodti4 without declaring the runtime library as an ELF dependency. + # Loading common compiler runtimes globally first makes those symbols visible + # before ctypes loads /opt/roverd/googletts/libchrometts.so. + candidates = [ + "gcc_s", + "atomic", + "stdc++", + "c++", + "c++abi", + ] + for name in candidates: + lib = ctypes.util.find_library(name) + if not lib: + continue + try: + _runtime_handles.append(load_shared_library(lib)) + except OSError: + pass + + +# Load the runtime helpers at module import time so daemon readiness fails only +# when libchrometts itself is still unusable after the best-effort preloads. +preload_runtime_libraries() + def varint(value): out = bytearray() @@ -64,7 +99,7 @@ def build_speaker(name, gender): class ChromeTTS: def __init__(self): - self.lib = ctypes.CDLL(LIB_PATH) + self.lib = load_shared_library(LIB_PATH) self.lib.GoogleTtsInit.argtypes = [ctypes.c_char_p, ctypes.c_char_p] self.lib.GoogleTtsInit.restype = ctypes.c_bool self.lib.GoogleTtsInitBuffered.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int, ctypes.c_int]