|
| 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") |
0 commit comments