Skip to content

Build the library on Windows with clang-cl - #210

Merged
kahrendt merged 1 commit into
mainfrom
windows-clang-cl
Oct 21, 2026
Merged

kahrendt merged 1 commit into
mainfrom
windows-clang-cl

Conversation

@chrisuthe

Copy link
Copy Markdown
Member

The host build only compiles on macOS and Linux. With clang-cl on Windows it fails in four places, and clang-cl reads the target's -Wall as /Wall, which is -Weverything (about 40,000 warnings).

  • src/host/network_info.cpp uses getifaddrs(). A _WIN32 branch now walks GetAdaptersAddresses() with the same filter: adapter up, not loopback, a 6-byte hardware address, and a routable unicast address. It reuses is_routable_ip() and format_mac().
  • <windows.h>, reached through IXWebSocket's winsock include, defines min/max and wingdi's ERROR as macros, which break std::min/std::max and LogLevel::ERROR. The library target defines NOMINMAX, WIN32_LEAN_AND_MEAN and NOGDI privately; no public header includes <windows.h>, so consumers' GDI and min/max settings are untouched.
  • micro-opus's default THREADSAFE_PSEUDOSTACK keeps its per-thread state in pthread TLS. On Windows OPUS_ALLOCATION_MODE defaults to USE_ALLOCA, which stays thread-safe between the player's decoder and the source's encoder; a consumer that sets the mode keeps its own.
  • On MSVC-style compilers the target uses /W4 -Wpedantic -Wno-language-extension-token. opus_types.h uses the __int32/__int64 keywords on Windows, which -Wpedantic reports as an extension.

Notes

  • Only the library builds on Windows. The tests and examples still use POSIX sockets, pthread.h and unistd.h, and the sanitizer options pass GCC-style flags.
  • -DENABLE_WERROR=ON still fails on Windows inside micro-flac: its host.cmake adds -Wall and honours the same ENABLE_WERROR cache variable. Every sendspin object compiles cleanly with -Werror; the fix belongs in micro-flac.
  • The docs still describe host builds as macOS/Linux; I left them until Windows support is accepted in scope.

Testing

  • Windows 11, clang-cl 22.1.3 from Visual Studio 2026 (Ninja, Release, -DBUILD_EXAMPLES=OFF): sendspin.lib builds with zero warnings from the library's own sources; with -DENABLE_WERROR=ON only micro-flac's objects fail.
  • A probe linking sendspin.lib called platform_get_interface_mac() and returned the physical NIC's MAC, skipping the Hyper-V and WSL virtual adapters listed ahead of it.
  • clang-format 18 (pre-commit) clean. The Linux/macOS path only had its includes regrouped; this PR's CI covers it.

- Detect the MAC address on Windows with GetAdaptersAddresses(), applying the same
  up, non-loopback, routable-IP filter as the getifaddrs() path
- Use /W4 instead of -Wall on MSVC-style compilers, where clang-cl reads -Wall as
  -Weverything; quiet -Wpedantic's report of opus_types.h's __int32/__int64 keywords
- Define NOMINMAX, WIN32_LEAN_AND_MEAN and NOGDI privately, since <windows.h> (reached
  through IXWebSocket) defines min/max and wingdi's ERROR as macros
- Default micro-opus to USE_ALLOCA on Windows, whose default thread-safe pseudostack
  needs pthread TLS

Tests and examples remain POSIX-only.

@kahrendt kahrendt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kahrendt
kahrendt merged commit 985fba5 into main Oct 21, 2026
16 checks passed
@kahrendt
kahrendt deleted the windows-clang-cl branch October 21, 2026 18:10
kahrendt pushed a commit that referenced this pull request Oct 28, 2026
On Windows every socket call fails until `WSAStartup()` runs, and
IXWebSocket leaves that to the application through
`ix::initNetSystem()`. The client could neither dial a server ("Either
the application has not called WSAStartup") nor listen.

## Summary

- `SendspinWsServer::start()` and `SendspinClientConnection::start()`
call `ix::initNetSystem()` once, through a function-local static, before
their first socket. It is never undone; Windows releases it at process
exit.
- The two handle a failure differently on purpose. The server's
`start()` returns a bool, so it logs and returns false. The client's
`start()` has no failure return, so it logs and lets the attempt go
ahead; its first socket call then fails and IXWebSocket reports it as
the connection's `Error`, which already reports the close.

## Notes

- A no-op on POSIX (`initNetSystem()` returns true without doing
anything there).
- Follows #210, which made the library compile on Windows with clang-cl.
Split out of #211.

## Testing

- Windows 11, clang-cl 22.1.3 (from #211): `basic_client` dials Music
Assistant with `-u` and listens on its own port; before this change both
failed with the WSAStartup error.
- Linux, GCC 16: host build clean; `sendspin_tests` all pass. The
ASan/UBSan and TSan runs are this PR's CI (no sanitizer runtime on the
local machine).
kahrendt pushed a commit that referenced this pull request Nov 4, 2026
A Windows consumer that includes `<windows.h>` before the sendspin
headers, as most Win32 code does, fails to compile them. The library's
`NOMINMAX` / `WIN32_LEAN_AND_MEAN` / `NOGDI` from #210 are private, so
they never covered the public headers.

## Summary

- **`LogLevel::ERROR`.** wingdi.h defines `ERROR` as `0`, which turned
the enumerator into `0 = 1`. `client.h` now sets the macro aside around
`LogLevel` with `#pragma push_macro("ERROR")` / `#undef ERROR` /
`#pragma pop_macro("ERROR")`, so the consumer's own `ERROR` is back
after it. The pragmas are supported by GCC, Clang and MSVC, so the
header needs no `#ifdef _WIN32`, and they are no-ops where `ERROR` is
not defined.
- **`std::numeric_limits<uint16_t>::max()`** in
`PlayerRoleConfig::pipeline_lead_time_ms()` was expanded by windows.h's
function-like `max()` macro. It is now
`(std::numeric_limits<uint16_t>::max)()`, with a comment so the
parentheses are not tidied away.
- **`tests/test_windows_header_macros.cpp`**, a compile-only translation
unit in `sendspin_tests` that guards both on every host. It includes the
standard headers the public headers use, then defines `ERROR`, `min()`
and `max()` as `<windows.h>` does, then includes every public header:
`client.h`, `persistence_codec.h` and each enabled role's header
(`client.h` only forward-declares the roles). A `static_assert` checks
that `ERROR` is restored. The standard headers come first because
libstdc++ and libc++ don't guard themselves against a function-like
`max()` the way the MSVC STL does. A new standard include in a public
header has to be added there too, as its comment says.

## Notes

- Consumer code that itself names `LogLevel::ERROR` after `<windows.h>`
still needs `NOGDI` or its own `#undef ERROR`; the comment above
`LogLevel` says so. Renaming the enumerator would remove that, but it
would break the API for every consumer, so it is left alone.
- The new test simulates the macros rather than including the real
`<windows.h>`. A Windows CI job is planned as a follow-up. It would
compile the library and `basic_client` on Windows, and with them the
public headers after the real `<windows.h>`.
- The saturated lead time isn't re-checked in the new test: the macro
can only break the build, not change the value, and
`test_player_role.cpp` already pins `pipeline_lead_time_ms()`.

## Testing

- Windows 11, clang-cl 22.1.3 (Visual Studio 2026), `/W4`, every role
enabled, one translation unit including every public header:
- `<windows.h>` first: failed with 3 errors before this change, compiles
cleanly after it.
- `<windows.h>` after the sendspin headers, with `NOMINMAX` +
`WIN32_LEAN_AND_MEAN`, and without `<windows.h>`: all compile cleanly.
- `static_assert(ERROR == 0)` after the headers holds, so `pop_macro`
restores wingdi's macro.
- With `NOGDI`, consumer code can name `LogLevel::ERROR` after
`<windows.h>`; without it, it fails in the consumer's own line, as the
header comment says.
- Windows 11, clang-cl: the `sendspin` target builds with no warnings
from the library's own sources.
- Linux (WSL), GCC 13, `-Wall -Wextra -Wpedantic`: both headers compile,
a consumer's own `ERROR` macro survives the include, and
`pipeline_lead_time_ms(0xFFFF)` still saturates at `0xFFFF`.
- Mutations against `test_windows_header_macros.cpp` (GCC 13, every role
enabled). Each one alone fails the build:
  - no parentheses around `max` in `config.h`: fails at `config.h:418`;
- no `push_macro` / `#undef` in `client.h`: "expected identifier before
numeric constant";
- no `pop_macro`: the `static_assert` fails ("ERROR was not declared");
- a bare `std::numeric_limits<int>::max()` added to `artwork_role.h`, or
separately to `visualizer_role.h`: fails at that line. The first version
of the test (`1e8e506`), which included only `client.h` and
`persistence_codec.h`, still compiled with either.
- CI on `1c24d4a`: all checks green; the unit-test job builds the new
translation unit and passes 707/707.
- clang-format: no changes.
kahrendt added a commit that referenced this pull request Nov 4, 2026
Depends on #213 (Winsock init): this branch is stacked on it, and
`basic_client` cannot dial or listen on Windows without it. Review only
the top commit; the base switches to `main` once #213 merges.

## Summary

- **PortAudio on Windows.** Windows has no pkg-config or system
PortAudio, so `examples/basic_client/CMakeLists.txt` fetches PortAudio
v19.7.0 on `WIN32` and builds it as a static library. POSIX keeps the
pkg-config lookup.
- **`getopt()` for Windows.** `examples/common/win_getopt.h` is a
minimal stand-in for the missing one. It covers grouped flags, `-x val`
/ `-xval`, and stops at the first non-option or `--`. Errors return `?`
without a message, since the example prints its usage on that.
- **The pairing-window gesture.** Console keys `p` / `c` stand in for
`SIGUSR1` / `SIGUSR2`. A single `#ifdef _WIN32` block defines the key
poll (a no-op on POSIX) and the hint strings, so the usage text and the
pairing-window prompt name the right gesture on each platform.
- **Persistence.** The provider writes through `_open` / `_write` /
`_commit` and replaces the file with `std::filesystem::rename()`,
because `std::rename()` won't replace an existing file on Windows. It
falls back to `%USERPROFILE%` when `HOME` is unset. Each `write_file()`
variant has its own doc comment.
- **README.** `examples/basic_client/README.md` gets a Windows section
covering the build, no mDNS (so dial with `-u`), the `p`/`c` keys, and
the persistence file's permissions.
- **The default build.** `tui_client` includes POSIX `<getopt.h>`, so
the top-level `CMakeLists.txt` skips it on `WIN32` with a status
message, the way `source_client` already skips itself without PortAudio.
A standalone Windows build with the examples on (the default) now
succeeds.
- **CRT warnings.** `basic_client` defines `_CRT_SECURE_NO_WARNINGS` on
`WIN32`, since the persistence provider's `getenv()` and `_open()`
otherwise draw deprecation warnings pointing at `_dupenv_s` /
`_sopen_s`.

## Notes

- The persistence file can't be created 0600 on Windows. It inherits its
directory's ACL, which under `%USERPROFILE%` grants only the user,
SYSTEM and Administrators.
- `tui_client`, `source_client` and the tests remain POSIX-only. The
top-level README's "macOS/Linux" wording is left alone, since #210
deferred the library's Windows support scope.
- Split out of #211.

## Testing

- Windows 11, clang-cl 22.1.3 (Visual Studio 2026, Ninja, Release), from
#211: `sendspin` and `basic_client` built with no warnings from the
library's own sources. `basic_client.exe -p 8929 -u
ws://<music-assistant>:8927/sendspin` completed the Noise handshake, was
admitted (unpaired) and listened locally. Pairing and playback were not
exercised.
- Windows 11, clang-cl (Visual Studio 2026, Ninja), on this branch: a
clean configure and build with the default `BUILD_EXAMPLES` succeeds,
skipping `tui_client` and `source_client`, with no warnings from the
repo's own sources. `basic_client.exe -h` names the `p`/`c` keys, an
unknown option prints the usage, and `basic_client.exe -p 8931` starts
and listens on 8931.
- The trimmed `win_getopt.h` was checked on Linux against
`basic_client`'s optstring: grouped flags, `-pVAL`, `-p VAL`, `--`, a
lone `-`, an unknown option, a missing argument and `-:`.
- Linux, GCC 16: host build including the examples is clean, and
`sendspin_tests` passes 705/705. The sanitizer runs are this PR's CI.

---------

Co-authored-by: Kevin Ahrendt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants