Skip to content

Keep backend objects in MSVC static builds - #125

Merged
ethindp merged 6 commits into
ethindp:masterfrom
trypsynth:fix/msvc-static-backend-registration
Sep 18, 2026
Merged

ethindp merged 6 commits into
ethindp:masterfrom
trypsynth:fix/msvc-static-backend-registration

Conversation

@trypsynth

Copy link
Copy Markdown
Contributor

Fixes #124.

A backend registers itself through a file-scope static, so nothing outside its own object refers to it. [[gnu::used, gnu::retain]] stops GCC and Clang dropping it; MSVC honours neither, so a static build discards every backend object and prism_create_best reports "backend not available" with a screen reader running.

What this does

REGISTER_BACKEND now also emits an extern "C" anchor function, named by PRISM_BACKEND_ANCHOR, which cmake/PrismBackends.cmake defines per backend from the name it already has. The build then generates one /include: directive per backend that was actually built.

Those directives go in a header included by source/prism.cpp, not in a source file of their own. The linker reads directives only from objects it has already decided to link, so an object holding nothing but directives would be dropped for exactly the reason it exists to prevent. prism.cpp is always in the link because the consumer calls into it.

x86 gets the leading underscore that cdecl carries there; nothing else does. Non-MSVC builds are untouched, since the existing attributes already say this.

The second half

Retaining the objects then exposes their vendor imports, which a shared prism links for itself:

prism.lib(pc_talker.obj) : error LNK2019: unresolved external symbol __imp_PCTKStatus

The import libraries were already installed for static builds, but only prism-config.cmake named them, so consumers that do not build with CMake could not find out. The install step now also writes lib/prism-static-windows.txt, one line per backend giving the import library and the DLL behind it.

The DLL name matters as much as the library. Those DLLs are delay loaded in the shared build, and a static consumer that does not delay load them turns every screen reader into a hard dependency: the program will not start unless all of them are present.

Testing

Built static on MSVC x64 and linked into a Rust application through prism-sys:

  • before: create_best returned "backend not available", NVDA running
  • after: create_best returned NVDA, no prism.dll beside the executable, and the program starts on a machine with none of the vendor DLLs installed

Shared builds were rebuilt and behave as before. I have not been able to test x86 or MinGW, so the underscore branch and the non-MSVC paths are reasoned rather than run.

@trypsynth

Copy link
Copy Markdown
Contributor Author

The ARM64 lint job caught a real gap, thanks CI.

prism.cpp.obj : error LNK2001: unresolved external symbol prism_anchor_window_eyes

window_eyes.cpp guards its whole body on x86/x64, but its prism_declare_backend call carried no ARCH, so on ARM64 CMake built it as a backend while the source compiled to nothing. That was harmless before, since an empty object bothers nobody. With this change the build emits an /include: for it and the link fails.

zdsr and boy_pc_reader guard themselves the same way and both already declare ARCH x86 x64. window_eyes now does too, so CMake's view and the source agree and it is skipped on ARM64 like the other two.

Pushed as 85ece1b.

@ethindp
ethindp merged commit 8fe3f2e into ethindp:master Sep 18, 2026
63 of 65 checks passed
ethindp pushed a commit that referenced this pull request Sep 22, 2026
Fixes #130.

A static prism linked with GCC or Clang registers no backends unless the
archive is linked whole. `gnu::used` and `gnu::retain` keep a registrar
inside its own object, but nothing references a backend's object, so the
linker never pulls it out of the archive in the first place. #125 solved
this for MSVC with `/include:` directives. Every other toolchain was
still relying on the attributes.

## What this does

The anchors #125 added are already emitted on every compiler. The
generated `backend_anchors.h` now also covers GCC and Clang: it declares
each backend's anchor and takes its address in a `[[gnu::used]]` array.
The header is included by `prism.cpp`, which the linker always keeps
because the application calls `prism_init`, so those references pull
every backend object in. MSVC keeps its directives, unchanged.

The header is generated and included on every platform now, rather than
only under MSVC.

## Testing

The reproduction from #130, on GitHub's runners, linking `libprism.a`
with no `--whole-archive` or `-force_load`:

Ubuntu 24.04, GCC 13.3 (`speech_dispatcher=on`):

```
registered backends: 1
  Speech Dispatcher
```

macOS, Apple Clang (`avspeech=on voiceover=on`):

```
registered backends: 2
  VoiceOver
  AVSpeech
```

The MSVC static build was checked as well, in a .NET Native AOT
application that links `prism.lib` and lists its backends on x64, x86
and ARM64.
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.

MSVC static build registers no backends: REGISTER_BACKEND relies on [[gnu::used, gnu::retain]]

2 participants