Skip to content

build: Unified precompiled headers, unity builds, and zccache for C/C++ - #1128

Merged
zackees merged 11 commits into
masterfrom
build/pch-unity
Oct 16, 2026
Merged

zackees merged 11 commits into
masterfrom
build/pch-unity

Conversation

@zackees

@zackees zackees commented Oct 16, 2026

Copy link
Copy Markdown
Member

Speeds up the C/C++ build three ways, measured on a fresh Linux tree (ccache off, -j16):

wall CPU
master 315 s 2416 s
+ precompiled headers ~322 s* 1798 s (-26%)
+ unity builds 198 s (-37%) 1053 s (-56%)
+ zccache, warm rebuild in the same tree 26 s

* measured under load average 30+; CPU time is the reliable number.

The frontend (obs-studio) was 68% of all compile time: 2799 s, now 726 s.

What changes

Precompiled headers (cmake/common/pch.cmake, cmake/pch/, ENABLE_PCH, default ON):

  • one unified header per target and language, applied to every first-party target (not deps/, test/, rust/) in one pass at the end of the top-level CMakeLists.txt;
  • profiles:
    • C standard library;
    • libobs (obs-internal.h);
    • plugins (obs-module.h + util/*);
    • Qt (all of QtCore/QtGui/QtWidgets, with or without libobs);
    • the frontend (plus OBSApp.hpp, OBSBasic.hpp, qt-wrappers.hpp).
  • sources that #define before their first #include compile without it; _GNU_SOURCE is defined in the PCH on Linux.

Unity builds (cmake/common/unity.cmake, ENABLE_UNITY_BUILD, default ON on Linux, OFF on Windows/macOS for now; batches of 8):

  • every file-local name collision on Linux is fixed:
    • colliding static functions/variables get a file-specific name (struct fields keep theirs);
    • file-scope macros that two sources define differently are #undef'd at the end of the source;
    • the frontend's model enums move into namespaces (resolving the existing TODO in MissingFilesModel.hpp);
    • duplicated declarations are declared once.
  • whole-file duplicates (gl-wayland-egl.c vs gl-x11-egl.c, legacy obs-slideshow.c) stay out of batches.
  • no behaviour change; no C test or public header edited. A test that includes a source keeps that source's names (test_expander_unity.c → the compressor's copies were renamed instead).

Caching:

Tier 2: validate.sh's "ported sources compiled into libobs, not libobs-with-rust" and obs-nvenc loader checks now also look inside unity batches.

Verification

  • Linux, full build (all targets except Cargo), PCH + unity: 0 failures; libobs exports identical between the C libobs and libobs-with-rust.
  • Tier 2 in Docker (unity on): 261/263 passed. The 2 failures are unrelated to this change:
    • test_ws_batch_order: the local plugins/obs-websocket submodule was at a different commit than master's pin;
    • test_vlc_playlist_gap: audio timing under load average 30+.
    • PR CI runs both on a clean checkout.

Follow-ups

ENABLE_PCH (default ON) gives each target one precompiled header per
language from cmake/pch/: the C standard library, the libobs headers
(obs-internal.h for libobs itself, obs-module.h and util/* for plugins), and
for UI targets all of QtCore/QtGui/QtWidgets, plus the frontend headers most
of obs-studio's sources include. set_target_properties_obs applies it at the
end of the target's directory, after its platform files add sources.

Sources that define a macro before their first #include keep compiling
without the PCH. _GNU_SOURCE is defined in the PCH on Linux, since several
sources request it after other includes.

Fresh Linux tree, ccache off, -j16: CPU time 2416 s -> 1798 s (-26%);
obs-studio 2799 s -> 1995 s of compile time.

ENABLE_UNITY_BUILD (default OFF) adds unity builds on the same hook, for
when the targets' file-local names are made unique.
A unity build compiles a target's sources as batches of one translation
unit, so two sources' file-local names meet. Every collision on Linux:

- static functions and variables with the same name in two sources of one
  target get a file-specific name (get_defaults in obs-encoder.c,
  obs-output.c, obs-service.c and obs-source.c; mix_a/mix_b in five
  transitions; the AV1 bit reader in rtmp-av1.c; nvenc-compat.c's copies of
  nvenc-properties.c's functions; ...). Struct fields keep their names.
- file-scope macros that two sources define differently (do_log, blog,
  warn, get_weak, the compressor/expander/limiter settings, ...) are
  #undef'd at the end of the source that defines them.
- frontend: the remux, importer and missing-files models' enums move into
  namespaces (their Count/State/EntryStateRole enumerators clashed, the TODO
  in MissingFilesModel.hpp), with using-declarations keeping the type names;
  MissingFilesPathItemDelegate.cpp includes MissingFilesModel.hpp instead of
  redeclaring its enum; the AutoConfig wizard's ListOpt is declared once in
  AutoConfigStreamPage.hpp; FillPropertyCombo is declared once, with its
  default argument, in ComboSelectToolbar.hpp.
- mp4-mux's codec enumerators get an MP4_ prefix (flv-mux.h has its own
  CODEC_H264/HEVC/AV1); decklink-captions.h gets #pragma once.

No behaviour change: renames of internal identifiers, #undefs at the end
of a source, and declarations moved without changing them.
PCH and unity settings are now applied in one pass at the end of the
top-level CMakeLists.txt to every target outside deps/, test/ and rust/,
including the shared static libraries (idian, the Qt wrappers, ...) that
don't go through set_target_properties_obs. Qt targets that don't link
libobs get a Qt-only precompiled header.

ENABLE_UNITY_BUILD defaults to ON on Linux, where every target builds with
it (batches of OBS_UNITY_BUILD_BATCH_SIZE=8). Sources that must be first in
their translation unit (macros before includes) and whole-file duplicates
(gl-wayland-egl.c next to gl-x11-egl.c, the legacy obs-slideshow.c) compile
on their own; generator-expression sources are matched by file name.
Windows and macOS keep it OFF until their own sources' collisions are
renamed.

Fresh Linux tree, ccache off, -j16 (baseline / PCH / PCH + unity):
  CPU time      2416 s / 1798 s / 1053 s  (-56%)
  wall time      315 s /  322 s /  198 s  (-37%)
  obs-studio    2799 s / 1995 s /  726 s of compile time
…unity

test/cmocka/test_expander_unity.c includes expander-filter.c and calls its
static gain_coefficient, so the expander keeps analyze_envelope,
resize_env_buffer and gain_coefficient, and the compressor's copies take
the compressor_ prefix instead.
With ENABLE_UNITY_BUILD a source is compiled inside a unity batch
(<target>.dir/Unity/unity_N_c.c includes it), not as its own object. The
ported-source check (libobs vs libobs-with-rust) and the obs-nvenc
loader check now accept either.
…PCH hits

zccache.cmake (Linux, ENABLE_ZCCACHE, default ON) uses soldr's host
compiler wrapper (zccache-soldr <compiler> ...) as the C/C++ launcher when
soldr is installed and nothing else chose a launcher; an explicit
CMAKE_<LANG>_COMPILER_LAUNCHER or ENABLE_CCACHE wins. It caches the
precompiled headers and unity batches as they are. Until soldr#3829 adds a
stable launcher command, the wrapper is found below the soldr root.

Fresh Linux tree, PCH + unity, -j16: no cache 198 s; zccache cold 191 s;
zccache warm (clean rebuild, same tree) 26 s.

With a ccache launcher, targets with a precompiled header now run it with
CCACHE_SLOPPINESS extended by pch_defines,time_macros (and GCC gets
-fpch-preprocess); without them ccache refuses those sources.
…y builds

#1125 added a static write_packet to obs-ffmpeg-output.c; obs-ffmpeg-mux.c
already defines a write_packet (declared in obs-ffmpeg-mux.h), which a unity
batch sees too. No test references the static.
@zackees
zackees enabled auto-merge October 16, 2026 21:20
@zackees
zackees disabled auto-merge October 16, 2026 21:25
Defining a feature macro in the PCH is not equivalent to the sources that
request it: with glibc 2.43, _GNU_SOURCE makes strstr() return const char *
for a const argument, and librtmp's parseurl.c failed the Linux CI build
with -Werror=discarded-qualifiers. threading-posix.c and obs-nix.c, which
define _GNU_SOURCE after other includes, compile without the PCH and outside
unity batches instead.
… plugins

ENABLE_UNITY_BUILD now defaults to ON everywhere. cmake/cross applies the
same precompiled headers and unity batches and compiles, besides libobs,
the platform-only plugins: win-capture (with graphics-hook and
get-graphics-offsets), win-wasapi, win-dshow, obs-nvenc and
coreaudio-encoder for win-x64; mac-capture, mac-avcapture,
mac-videotoolbox and coreaudio-encoder for mac-arm64.
OBS_CROSS_CMAKE_ARGS passes extra CMake arguments to cmake/cross/docker.sh;
OBS_UNITY_BUILD_BATCH_SIZE=0 puts each target in one batch, so every pair
of its sources meets. Windows, macOS and Linux build with batch size 0 and 8.

- PCH: Xcode gives a target one prefix header for all languages, so
  targets that mix C++ with C or Objective-C compile without a PCH there
  (the macOS build failed on decklink's C sources seeing <algorithm>). The
  PCH restores WIN32_LEAN_AND_MEAN after the libobs headers
  (util/sse-intrin.h defines it on non-x86 Windows and broke
  obs-browser's winsock use on Windows arm64). Targets can name their
  profile (OBS_PCH_PROFILE).
- Unity: graphics-hook and get-graphics-offsets opt out (one Direct3D
  version per source; d3d8.h and d3d9.h can't share a translation unit).
  Whole-file siblings compile on their own: window-capture.c,
  obs-nix-wayland.c, obs-ffmpeg-mpegts.c, media-playback's cache.c. Skip
  lists also match sources that INTERFACE libraries hand to a target.
…hing hid

Compiling each target as a single unity batch (Linux, and cmake/cross for
Windows and macOS) surfaced collisions the 8-source batches only kept apart
by chance:

- libobs: graphics.c #undef'd <windows.h>'s near/far for the rest of a
  batch (every later NEAR/FAR, DEFINE_GUID and commctrl.h use broke); it now
  saves and restores them with push_macro/pop_macro. obs-hotkey.c restores
  uthash's HASH_FUNCTION after switching it to an integer hash; effect.c
  undefines the min() it defines. Renamed statics: shader-parser,
  obs-source-transition, obs-scene, obs-source, dstr-libc, source-profiler
  (profiler.c keeps 'enabled' for the C oracle), obs-nix-hotkey-state,
  CoreAudio monitoring.
- plugins: renames in win-capture (monitor/window/duplicator capture),
  linux-pipewire (screencast vs camera portal; the camera portal's global
  'connection'), obs-filters (limiter, mask), obs-outputs (mp4-mux keeps
  free_packets for rtmp-stream.c's test), obs-ffmpeg (hls-mux), mac-capture
  (the two ScreenCaptureKit restart callbacks), obs-nvenc (nvenc-compat.c's
  copies of nvenc.c's create/name functions). obs-outputs' colour enums,
  duplicated in rtmp-stream.h and flv-output.c, move to color-enums.h.
  Include guards: rtmp-stream.h, mac-sck-common.h, window-utils.h and two
  mac-avcapture headers. enum-wasapi.hpp declares GUID_NULL (cguid.h)
  before audioclient.h pulls in ks.h, which redefines it as a macro.
- cross-only fixes: graphics-hook's dummy window thread takes LPVOID (its
  LPTHREAD_START_ROUTINE signature), wasapi-notify.hpp includes
  mmdeviceapi.h in the SDK's lowercase.
- frontend: per-file names for the duplicated color_from_int/color_to_int,
  GetCurrentScene/MoveItem, return_first_id, select_one, getNewSourceName,
  isHiddenInMixer, ... (BasicOutputHandler.cpp, which tests compile, keeps
  its names); MakeQFont keeps its default argument only in its definition.

Names tests or the C oracle use keep theirs; only internal identifiers
change.
The full Windows and macOS builds showed two kinds of source that must not
share a translation unit with ours:

- deps/ sources that reach a first-party target through an INTERFACE
  library (libdshowcapture in obs-virtualcam-module: its winsock2.h after
  another source's full <windows.h> redefined every winsock declaration on
  Windows arm64). They now compile on their own and without the PCH.
- Objective-C sources: without the OBJC language enabled, CMake files .m
  under C, so Xcode compiled obs-cocoa.m and platform-cocoa.m inside a C
  unity batch (Foundation parsed as C on macOS). cmake/cross enables OBJC,
  which is why it didn't see this.

Skip matching now also covers sources INTERFACE libraries hand a target.
@zackees
zackees added this pull request to the merge queue Oct 16, 2026
Merged via the queue into master with commit 66a48a2 Oct 16, 2026
49 of 57 checks passed
zackees added a commit that referenced this pull request Oct 16, 2026
#1128 turned ENABLE_UNITY_BUILD on for Windows and macOS, but their full
builds still hit collisions in targets cmake/cross doesn't compile
(libobs-d3d11's featureLevels, nv-filters, text-freetype2, win-dshow's
unguarded shared headers, frontend-tools' byte vs std::byte, obs-browser's
DispatchJSEvent). Off there by default again until those are fixed;
precompiled headers stay on everywhere.
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 17, 2026
…(unity skip lists, scripting, AJA) (obs-rust#1139)

* build: Match .cpp and .mm sources in skip lists; scripting out of unity; zccache opt-in

- The file-name extraction listed extensions as (c|cpp|cc|m|mm), and CMake's
  regex takes the first alternative that matches: foo.cpp was read as foo.c
  and foo.mm as foo.m, so no C++ or Objective-C++ source ever matched a skip
  rule. libdshowcapture's .cpp sources therefore still shared batches with
  obs-virtualcam-module's (winsock redefinitions on Windows arm64) and the
  DAL plugin's .mm files sat in a C++ batch (Foundation parsed as C++ on
  macOS). Longer extensions now come first.
- obs-scripting compiles both the Lua and the Python SWIG runtimes, which
  define the same swig_type_info helpers: no unity batches (the Flatpak
  build, which enables scripting, failed on it).
- ENABLE_ZCCACHE is now opt-in: with soldr 0.9.34 a compile can still wait
  indefinitely on a busy shared daemon (forge-ci/soldr#3815), which stalled a
  local build here, and the launcher is still found by path
  (forge-ci/soldr#3829).

* build: aja-common.hpp include guard; platform-windows.c outside unity batches

aja-common.hpp had no include guard, so a unity batch that includes it
twice redefined its constants and default arguments (macOS and Flatpak,
the builds that enable AJA). platform-windows.c needs the full <windows.h>
(NTSTATUS for RtlGetVersion); in a batch an earlier source may have included
it with WIN32_LEAN_AND_MEAN, which util/sse-intrin.h leaves defined on
Windows arm64, making its own include a no-op.

* build: Unity builds back to Linux-only by default

obs-rust#1128 turned ENABLE_UNITY_BUILD on for Windows and macOS, but their full
builds still hit collisions in targets cmake/cross doesn't compile
(libobs-d3d11's featureLevels, nv-filters, text-freetype2, win-dshow's
unguarded shared headers, frontend-tools' byte vs std::byte, obs-browser's
DispatchJSEvent). Off there by default again until those are fixed;
precompiled headers stay on everywhere.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant