Skip to content

Commit 22ea422

Browse files
hans00claude
andcommitted
build(barbet): apply llama.cpp patch via CMake instead of a manual step
Add cmake/SetupBarbetLlama.cmake, included when the tts-cli example is enabled (CODEC_BUILD_TTS_CLI=ON, default). At configure time it applies patches/barbet-llamacpp.patch to the pinned llama.cpp submodule idempotently: no-op if already applied, applies if clean, and FATALs if upstream drifted past the pinned base so the patch no longer applies. Skips silently when the submodule isn't checked out (no forced download for plain codec builds). CODEC_BARBET_PATCH gates the step; CODEC_BUILD_TTS_CLI gates the whole TTS path. README updated to point at the CMake automation. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent bd31cce commit 22ea422

3 files changed

Lines changed: 103 additions & 7 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ add_executable(codec-batch-decode
150150
target_include_directories(codec-batch-decode PRIVATE ${CMAKE_SOURCE_DIR}/include)
151151
target_link_libraries(codec-batch-decode PRIVATE codec codec-example-utils ggml ggml-cpu)
152152

153-
add_executable(tts-cli
154-
examples/tts-cli.cpp
155-
)
156-
target_link_libraries(tts-cli PRIVATE codec_common codec-example-utils)
153+
option(CODEC_BUILD_TTS_CLI "Build the tts-cli example (drives the Barbet llama.cpp backbone)" ON)
154+
if(CODEC_BUILD_TTS_CLI)
155+
# The TTS path uses the Barbet semantic LM in the pinned llama.cpp submodule;
156+
# apply its arch patch (idempotent, skips when the submodule isn't checked out).
157+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/SetupBarbetLlama.cmake)
158+
159+
add_executable(tts-cli
160+
examples/tts-cli.cpp
161+
)
162+
target_link_libraries(tts-cli PRIVATE codec_common codec-example-utils)
163+
endif()

‎cmake/SetupBarbetLlama.cmake‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# SetupBarbetLlama.cmake — apply the Barbet arch patch to the pinned llama.cpp
2+
# submodule at configure time, idempotently.
3+
#
4+
# llama.cpp lives at common/third-party/llama.cpp as a submodule pinned to the
5+
# commit below (see patches/barbet-llamacpp-README.md). The Barbet changes are
6+
# kept as patches/barbet-llamacpp.patch rather than committed into the submodule;
7+
# this module is what actually applies them, so the patch is part of the build
8+
# instead of a manual README step.
9+
#
10+
# Behaviour:
11+
# - submodule not checked out -> status message, skip (no forced download).
12+
# - patch already applied -> no-op (idempotent across re-configures).
13+
# - patch applies cleanly -> apply it.
14+
# - patch no longer applies -> FATAL: upstream drifted past the pin,
15+
# re-pin + regenerate the patch.
16+
#
17+
# Set -DCODEC_BARBET_PATCH=OFF to disable entirely.
18+
19+
option(CODEC_BARBET_PATCH "Apply the Barbet patch to the llama.cpp submodule at configure time" ON)
20+
if(NOT CODEC_BARBET_PATCH)
21+
return()
22+
endif()
23+
24+
set(_barbet_base "f708a5b2caaee0226c0af220e366785699ba41e2")
25+
set(_barbet_dir "${CMAKE_CURRENT_SOURCE_DIR}/common/third-party/llama.cpp")
26+
set(_barbet_patch "${CMAKE_CURRENT_SOURCE_DIR}/patches/barbet-llamacpp.patch")
27+
28+
find_package(Git QUIET)
29+
if(NOT Git_FOUND)
30+
message(STATUS "Barbet: git not found — skipping llama.cpp patch setup")
31+
return()
32+
endif()
33+
34+
# Submodule checked out? (cheap content probe — its top-level CMakeLists.)
35+
if(NOT EXISTS "${_barbet_dir}/CMakeLists.txt")
36+
message(STATUS "Barbet: llama.cpp submodule not initialized — skipping patch.\n"
37+
" Run: git submodule update --init -- common/third-party/llama.cpp")
38+
return()
39+
endif()
40+
41+
# Warn (don't fail) if HEAD has drifted off the pinned base — the patch may still
42+
# apply, but the build is no longer reproducing the recorded combination.
43+
execute_process(
44+
COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD
45+
WORKING_DIRECTORY "${_barbet_dir}"
46+
OUTPUT_VARIABLE _barbet_head OUTPUT_STRIP_TRAILING_WHITESPACE
47+
ERROR_QUIET RESULT_VARIABLE _rc)
48+
if(_rc EQUAL 0 AND NOT _barbet_head STREQUAL _barbet_base)
49+
message(WARNING "Barbet: llama.cpp submodule HEAD is ${_barbet_head},\n"
50+
" expected pinned base ${_barbet_base}.")
51+
endif()
52+
53+
# Already applied? `git apply --reverse --check` succeeds iff the patch is present.
54+
execute_process(
55+
COMMAND "${GIT_EXECUTABLE}" apply --reverse --check "${_barbet_patch}"
56+
WORKING_DIRECTORY "${_barbet_dir}"
57+
RESULT_VARIABLE _reverse_rc ERROR_QUIET OUTPUT_QUIET)
58+
if(_reverse_rc EQUAL 0)
59+
message(STATUS "Barbet: patch already applied to llama.cpp submodule")
60+
return()
61+
endif()
62+
63+
# Not applied — verify it still applies cleanly to this checkout.
64+
execute_process(
65+
COMMAND "${GIT_EXECUTABLE}" apply --check "${_barbet_patch}"
66+
WORKING_DIRECTORY "${_barbet_dir}"
67+
RESULT_VARIABLE _check_rc ERROR_QUIET OUTPUT_QUIET)
68+
if(NOT _check_rc EQUAL 0)
69+
message(FATAL_ERROR
70+
"Barbet: ${_barbet_patch}\n"
71+
" does not apply to llama.cpp at ${_barbet_head}.\n"
72+
" Upstream has likely drifted past the pinned base ${_barbet_base};\n"
73+
" re-pin the submodule and regenerate the patch, or set -DCODEC_BARBET_PATCH=OFF.")
74+
endif()
75+
76+
execute_process(
77+
COMMAND "${GIT_EXECUTABLE}" apply "${_barbet_patch}"
78+
WORKING_DIRECTORY "${_barbet_dir}"
79+
RESULT_VARIABLE _apply_rc)
80+
if(NOT _apply_rc EQUAL 0)
81+
message(FATAL_ERROR "Barbet: failed to apply ${_barbet_patch} (rc=${_apply_rc})")
82+
endif()
83+
message(STATUS "Barbet: applied barbet-llamacpp.patch to llama.cpp submodule")

‎patches/barbet-llamacpp-README.md‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ plumbing the patch may stop applying, at which point re-pin to a newer commit.
1515
- Repo: `https://github.com/ggml-org/llama.cpp`
1616
- Base commit: `f708a5b2caaee0226c0af220e366785699ba41e2` (2026-06-30, "vulkan: roll bk loop in matmul for asahi linux (#24663)")
1717

18-
Set up the working tree after a fresh clone of codec.cpp:
18+
The patch is applied **by CMake**, not by hand: configuring codec.cpp with the
19+
`tts-cli` example enabled (`CODEC_BUILD_TTS_CLI=ON`, the default) runs
20+
`cmake/SetupBarbetLlama.cmake`, which applies `barbet-llamacpp.patch` to the
21+
submodule idempotently and fails the configure if upstream has drifted so the
22+
patch no longer applies. You only need to check out the submodule once:
1923

2024
```bash
2125
git submodule update --init common/third-party/llama.cpp
22-
cd common/third-party/llama.cpp
23-
git apply ../../../patches/barbet-llamacpp.patch
26+
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release # applies the patch
2427
```
2528

29+
Pass `-DCODEC_BARBET_PATCH=OFF` to skip the patch step, or
30+
`-DCODEC_BUILD_TTS_CLI=OFF` to drop the TTS path (and its Barbet setup) entirely.
31+
2632
Check how far the pin has drifted from upstream master:
2733

2834
```bash

0 commit comments

Comments
 (0)