cmake : only take build info from the llama.cpp source tree - #28462
apollo-2006 wants to merge 2 commits into
Conversation
git searches parent directories for a repository, so a source tree with none of its own (a release tarball, or vendored sources) picked up whatever repository sat above it and reported that one's HEAD and commit count as llama.cpp's. The result is a plausible-looking hash that resolves to nothing. Verify the repository git finds is this source tree before trusting it, and locate that tree from CMAKE_CURRENT_LIST_DIR rather than CMAKE_CURRENT_SOURCE_DIR, which is the invoking working directory when build-info.cmake is reached through `cmake -P`, as scripts/ui-assets.cmake does.
There was a problem hiding this comment.
🟡 Changes recommended
Several new/modified execute_process(WORKING_DIRECTORY ...) uses pass an unquoted path variable, which can break configuration when the source path contains spaces (notably on Windows).
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR hardens CMake build metadata generation so cmake/build-info.cmake only uses Git information when the Git repository discovered by git is actually the llama.cpp source tree (preventing foreign commit/count values when building from release tarballs inside unrelated Git working trees, including cmake -P invocation paths).
Changes:
- Derive the source root for build-info from
CMAKE_CURRENT_LIST_DIR(file location) instead ofCMAKE_CURRENT_SOURCE_DIR(invocation cwd incmake -P). - Validate
git rev-parse --show-toplevelmatches the llama.cpp source tree before trustingrev-parse/rev-listresults. - Fall back to
unknown/0when the repository is missing or does not match the source tree.
File summaries
| File | Description |
|---|---|
| cmake/build-info.cmake | Anchors Git lookup to the build-info file's source tree and rejects Git data from unrelated parent repositories. |
Review details
Suppressed comments (2)
cmake/build-info.cmake:55
- WORKING_DIRECTORY should quote BUILD_INFO_SOURCE_DIR to avoid breaking when the source path contains spaces (CMake otherwise tokenizes it into multiple arguments).
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
WORKING_DIRECTORY ${BUILD_INFO_SOURCE_DIR}
OUTPUT_VARIABLE HEAD
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RES
cmake/build-info.cmake:66
- WORKING_DIRECTORY should quote BUILD_INFO_SOURCE_DIR to avoid breaking when the source path contains spaces (CMake otherwise tokenizes it into multiple arguments).
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-list --count HEAD
WORKING_DIRECTORY ${BUILD_INFO_SOURCE_DIR}
OUTPUT_VARIABLE COUNT
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RES
)
- Files reviewed: 1/1 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| execute_process( | ||
| COMMAND ${GIT_EXECUTABLE} rev-parse --show-toplevel | ||
| WORKING_DIRECTORY ${BUILD_INFO_SOURCE_DIR} | ||
| OUTPUT_VARIABLE GIT_TOPLEVEL | ||
| OUTPUT_STRIP_TRAILING_WHITESPACE | ||
| ERROR_QUIET | ||
| RESULT_VARIABLE RES | ||
| ) |
| message(STATUS "Git repository at ${GIT_TOPLEVEL} is not the llama.cpp source tree " | ||
| "(${BUILD_INFO_SOURCE_DIR}). Build info will not be accurate.") |
|
Hi @apollo-2006, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
An unquoted variable reference is expanded as a CMake list, so a source path containing a semicolon is split into several arguments and execute_process fails with "given unknown argument". Paths containing spaces were never affected.
|
Both flags were correct when they fired. Both are resolved now. PR Template not respected: the body was missing Multiple open PRs from a new contributor: I had two open at the time. #28463 was closed on 09-06 and #28261 on 09-08, so this is now my only open PR here. |
|
@angt #28445 landed shortly after I opened this and moved both references in my Second site section: The bug survives the move. On cmake 4.4.3, Your change passes |
Overview
Fixes #28397.
cmake/build-info.cmakerangit rev-parse --short HEADandgit rev-list --count HEADand accepted the result onRES EQUAL 0alone. git searches parent directories, so a source tree with no repository of its own picks up whatever repository sits above it. A release tarball unpacked inside any unrelated git working tree therefore stamps that repository's HEAD into--version. The failure is silent: you get a well-formed 7-hex string that resolves to nothing, which reads like a broken lookup rather than a wrong hash.Both values were affected, not just the commit.
BUILD_NUMBERcame from the same unverified repository, so the wholeb<number>-<commit>string could be fabricated.Second site:
cmake -PThe issue reports the tarball case. There is a second one.
scripts/ui-assets.cmake:421includes this file, and it is run in script mode fromtools/ui/CMakeLists.txt:56(cmake ... -P "${PROJECT_SOURCE_DIR}/scripts/ui-assets.cmake"). In script modeCMAKE_CURRENT_SOURCE_DIRis the invoking working directory, not the source tree, so the git lookup ran from the build directory and walked up from there.resolve_version()uses the resultingBUILD_NUMBERto choose which prebuilt UI bundle to fetch, so this one is not only cosmetic. Pinning the lookup toCMAKE_CURRENT_LIST_DIRfixes both sites at once and needs no change inui-assets.cmake.Verification
Real
git archivetarball of74a7c897fextracted inside an unrelated repository whose HEAD is0c71b95, configured with-DLLAMA_CURL=OFF -DGGML_CUDA=OFF:LLAMA_COMMITLLAMA_BUILD_NUMBER0c71b95(the outer repo)1(the outer repo)unknown0The real checkout is unchanged, still
74a7c897f/10820.Placement matrix, run against both the old and the new file:
unknown/0unknown/0unknown/0cmake -P, cwd inside an unrelated repogit rev-parse --show-toplevelandCMAKE_CURRENT_LIST_DIRare both passed throughREALPATHbefore comparison, so a symlinked source or build path does not produce a spurious mismatch.Behavior change worth flagging
A tree that is genuinely vendored into a larger repository, copied in rather than added as a submodule, previously reported the parent repository's commit and now reports
unknown. Submodules andgit worktreecheckouts are unaffected, since--show-toplevelreturns their own path. FetchContent withGIT_REPOSITORYis also unaffected, as the fetched tree carries its own.git. The only case that loses a value is the one where that value named a repository that has never contained llama.cpp, andunknownis the honest answer there. This repo's issue templates ask for the--versionstring, so a wrong hash costs more than a missing one.Requirements