cmake : skip project() when consumed as a subdirectory (#20415)#22151
Draft
jinweihan-ai wants to merge 1 commit intoggml-org:masterfrom
Draft
cmake : skip project() when consumed as a subdirectory (#20415)#22151jinweihan-ai wants to merge 1 commit intoggml-org:masterfrom
jinweihan-ai wants to merge 1 commit intoggml-org:masterfrom
Conversation
When llama.cpp or ggml is pulled into a parent CMake project via add_subdirectory / FetchContent / CPM, the unconditional project() calls re-run toolchain detection in the nested scope. For C/CXX this is mostly a no-op, but ggml additionally enables ASM, which triggers an ASM compiler identification pass that can override parent-scope settings (e.g. CMAKE_ASM_COMPILER) and, on cross-compilation toolchains such as iOS or the Android NDK, can pick a different sysroot / architecture than the parent configured. Guard both project() calls behind the standard `CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR` check so they only fire for standalone builds. The existing `LLAMA_STANDALONE` and `GGML_STANDALONE` variables continue to work because they are computed with the same condition. All assembly use in the tree is gated behind `enable_language(ASM)` in the backend that needs it (Metal, Hexagon HTP), so dropping ASM from the embedded project() call is safe. Closes ggml-org#20415
|
Hi @jinweihan-ai, 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #20415.
Both
CMakeLists.txt(llama) andggml/CMakeLists.txtcallproject()unconditionally. When llama.cpp is pulled into another CMake project viaadd_subdirectory/FetchContent/CPM, those calls re-run toolchain detection inside the nested scope. ForC/CXXthis is mostly a no-op, but ggml'sproject(... ASM)triggers an ASM compiler identification pass that can overwrite parent-scope toolchain variables and, on cross-compilation toolchains (iOS, Android NDK, arm64↔x86_64 hosts), pick a different sysroot/architecture than the parent configured.The fix follows the CMake convention of guarding
project()with the standalone check so it only fires for top-level builds. The existingLLAMA_STANDALONE/GGML_STANDALONEvariables continue to work because they are computed with the same predicate. No assembly sources live at the top-level of ggml — all ASM usage is gated behindenable_language(ASM)inside the backend that needs it (Metal, Hexagon HTP), so droppingASMfrom the embeddedproject()call is safe.Repro (before the fix)
Minimal parent project:
After the fix those two lines no longer appear — the parent's toolchain state is preserved.
Test plan
cmake -B build && cmake --build build --target llama-cli), version reported correctly (built with AppleClang … for Darwin arm64).add_subdirectory(llama.cpp)+-DCMAKE_OSX_ARCHITECTURES=x86_64now configures without triggering ASM identification and without clobberingCMAKE_ASM_COMPILERin the parent scope.GGML_SYSTEM_ARCHis still detected correctly (x86in the cross-compile repro).ggml-ci, Android build, and standalone builds should remain green — only theproject()invocation is guarded; everything below it runs unchanged.Requirements