LLM inference in C/C++
C++ ggml
cmake : use PROJECT_SOURCE_DIR instead of CMAKE_SOURCE_DIR (#28771)
Daniel Bevenius69eb250670f4
diff --git a/.ecrc b/.ecrcindex c68877ec21..0338e4faa4 100644--- a/.ecrc+++ b/.ecrc@@ -1,5 +1,5 @@{- "Exclude": ["^\\.gitmodules$", "stb_image\\.h"],+ "Exclude": ["^\\.gitmodules$", "stb_image\\.h", "examples/test-cmake/build/", "examples/test-cmake/build-subdir/"],"Disable": {"IndentSize": true}diff --git a/app/CMakeLists.txt b/app/CMakeLists.txtindex 3450ff4900..0b044228af 100644--- a/app/CMakeLists.txt+++ b/app/CMakeLists.txt@@ -16,7 +16,7 @@ target_link_libraries(${TARGET} PRIVATEtarget_compile_features(${TARGET} PRIVATE cxx_std_17)# Automatically add all files from the 'licenses' directory-file(GLOB EXTRA_LICENSES "${CMAKE_SOURCE_DIR}/licenses/LICENSE-*")+file(GLOB EXTRA_LICENSES "${PROJECT_SOURCE_DIR}/licenses/LICENSE-*")foreach(FILE_PATH ${EXTRA_LICENSES})get_filename_component(FILE_NAME "${FILE_PATH}" NAME)diff --git a/examples/eval-callback/CMakeLists.txt b/examples/eval-callback/CMakeLists.txtindex 63fbe59dce..96e1e1b353 100644--- a/examples/eval-callback/CMakeLists.txt+++ b/examples/eval-callback/CMakeLists.txt@@ -18,7 +18,7 @@ if(LLAMA_BUILD_TESTS)-DDEST=${MODEL_DEST}-DNAME=${MODEL_NAME}-DHASH=${MODEL_HASH}- -P ${CMAKE_SOURCE_DIR}/cmake/download-models.cmake+ -P ${PROJECT_SOURCE_DIR}/cmake/download-models.cmake)set_tests_properties(${TEST_TARGET}-download-model PROPERTIES FIXTURES_SETUP ${TEST_TARGET}-download-model)add_test(NAME ${TEST_TARGET} COMMAND llama-eval-callback -m "${MODEL_DEST}" --prompt hello --seed 42 -ngl 0)diff --git a/examples/test-cmake/.gitignore b/examples/test-cmake/.gitignoreindex 0ddff317a4..b630ddb7d6 100644--- a/examples/test-cmake/.gitignore+++ b/examples/test-cmake/.gitignore@@ -1,3 +1,4 @@llama-build-installinstallbuild+build-subdirdiff --git a/examples/test-cmake/CMakeLists.txt b/examples/test-cmake/CMakeLists.txtindex ed5cb1f3c2..6ceb3359e5 100644--- a/examples/test-cmake/CMakeLists.txt+++ b/examples/test-cmake/CMakeLists.txt@@ -3,11 +3,20 @@ project(llama-simple)set(CMAKE_CXX_STANDARD 17)-find_package(llama 0.1.0 REQUIRED)+option(LLAMA_TEST_USE_SUBDIR "Use add_subdirectory instead of find_package" OFF)++if(LLAMA_TEST_USE_SUBDIR)+ add_subdirectory(../../ llama.cpp)+else()+ find_package(llama 0.1.0 REQUIRED)+endif()add_executable(test-cmake test-cmake.cpp)target_link_libraries(test-cmake PRIVATE llama)-target_compile_definitions(test-cmake PRIVATE- LLAMA_BUILD_NUMBER=${LLAMA_BUILD_NUMBER}- LLAMA_BUILD_COMMIT="${LLAMA_BUILD_COMMIT}"-)++if(DEFINED LLAMA_BUILD_NUMBER)+ target_compile_definitions(test-cmake PRIVATE+ LLAMA_BUILD_NUMBER=${LLAMA_BUILD_NUMBER}+ LLAMA_BUILD_COMMIT="${LLAMA_BUILD_COMMIT}"+ )+endif()diff --git a/examples/test-cmake/README.md b/examples/test-cmake/README.mdindex 2f6a2fcfe9..03895abfb9 100644--- a/examples/test-cmake/README.md+++ b/examples/test-cmake/README.md@@ -5,17 +5,18 @@ enable troubleshooting issues and exploration. The idea is that this can be usedafter making changes to llama.cpp installation cmake configuration and thenverify it locally.-### Usage-The following will configure, build, and install llama.cpp+### find_package+The following will configure, build, and install llama.cpp, and the build a+project that uses find_package to use the installation.Configuring/build/install:```console./build-install.sh```The above command will create a directory named `install` in the current directory-which will have the follwing files in its lib directory:+which will have the following files in its lib directory:```console-(venv) $ ls install/lib/+$ ls install/lib/cmake libggml.so libllama-common.so.0 libllama.so.0.1.0 llama.cpplibggml-base.so libggml.so.0 libllama-common.so.0.1.0 libmtmd.so pkgconfiglibggml-base.so.0 libggml.so.0.19.0 libllama.so libmtmd.so.0@@ -24,7 +25,7 @@ libggml-base.so.0.19.0 libllama-common.so libllama.so.0 libmtmd.soBuild/run this project using the installation created above:```console-(venv) $ ./build.sh+$ ./build.sh-- Configuring done (0.0s)-- Generating done (0.0s)-- Build files have been written to: /path/to/llama.cpp/examples/test-cmake/build@@ -34,3 +35,11 @@ Build/run this project using the installation created above:load_backend: loaded CPU backend from /path/to/llama.cpp/examples/test-cmake/install/lib/llama.cpp/libggml-cpu-alderlake.so[test-cmake] Backend initialized.```++### add_subdirectory+The following will use add_subdirectory to include llama.cpp in a cmake project+and is intended to simulate projects that build llama.cpp in this way.++```console+$ USE_SUBDIR=ON ./build.sh+```diff --git a/examples/test-cmake/build.sh b/examples/test-cmake/build.shindex a212732b89..869a64160e 100755--- a/examples/test-cmake/build.sh+++ b/examples/test-cmake/build.sh@@ -2,6 +2,17 @@set -e-cmake -S . -B build -DCMAKE_PREFIX_PATH="${PWD}/install"-cmake --build build-LD_LIBRARY_PATH="${PWD}/install/lib/llama.cpp:${PWD}/install/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" ./build/test-cmake+if [ "${USE_SUBDIR:-OFF}" = "ON" ]; then+ BUILD_DIR="build-subdir"+ CMAKE_ARGS="-DLLAMA_TEST_USE_SUBDIR=ON -DLLAMA_BUILD_COMMON=ON -DLLAMA_BUILD_TOOLS=ON -DLLAMA_BUILD_SERVER=ON-DLLAMA_BUILD_TESTS=ON"+ LIB_PATH="${PWD}/${BUILD_DIR}/bin"+else+ BUILD_DIR="build"+ CMAKE_ARGS="-DCMAKE_PREFIX_PATH=${PWD}/install"+ LIB_PATH="${PWD}/install/lib/llama.cpp"+fi++cmake --fresh -S . -B "${BUILD_DIR}" ${CMAKE_ARGS}+cmake --build "${BUILD_DIR}" -j 8++LD_LIBRARY_PATH="${LIB_PATH}:${PWD}/install/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" "./${BUILD_DIR}/test-cmake"diff --git a/examples/test-cmake/test-cmake.cpp b/examples/test-cmake/test-cmake.cppindex dc1a9ae605..fea27c7e87 100644--- a/examples/test-cmake/test-cmake.cpp+++ b/examples/test-cmake/test-cmake.cpp@@ -2,8 +2,12 @@#include <cstdio>int main(void) {+#ifdef LLAMA_BUILD_NUMBERprintf("[test-cmake] llama.cpp version: %s, build: %d (%s)\n",llama_version(), LLAMA_BUILD_NUMBER, LLAMA_BUILD_COMMIT);+#else+ printf("[test-cmake] llama.cpp version: %s\n", llama_version());+#endifprintf("[test-cmake] ggml version: %s, commit: %s\n", ggml_version(), ggml_commit());printf("[test-cmake] Initializing backend...\n");llama_backend_init();diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txtindex cca90ef305..a398344c89 100644--- a/tests/CMakeLists.txt+++ b/tests/CMakeLists.txt@@ -289,7 +289,7 @@ add_test(NAME test-download-model COMMAND ${CMAKE_COMMAND}-DDEST=${MODEL_DEST}-DNAME=${MODEL_NAME}-DHASH=${MODEL_HASH}- -P ${CMAKE_SOURCE_DIR}/cmake/download-models.cmake+ -P ${PROJECT_SOURCE_DIR}/cmake/download-models.cmake)set_tests_properties(test-download-model PROPERTIES FIXTURES_SETUP test-download-model)diff --git a/tools/server/CMakeLists.txt b/tools/server/CMakeLists.txtindex 43c2456333..4adaaceefd 100644--- a/tools/server/CMakeLists.txt+++ b/tools/server/CMakeLists.txt@@ -30,7 +30,7 @@ if (BUILD_SHARED_LIBS)endif()target_include_directories(${TARGET} PRIVATE ../mtmd)-target_include_directories(${TARGET} PRIVATE ${CMAKE_SOURCE_DIR})+target_include_directories(${TARGET} PRIVATE ${PROJECT_SOURCE_DIR})target_link_libraries(${TARGET} PUBLIC llama-common mtmd ${CMAKE_THREAD_LIBS_INIT})# llama-server-impl: server logic, reusable by app@@ -47,7 +47,7 @@ add_library(${TARGET}set_target_properties(${TARGET} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)target_include_directories(${TARGET} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})-target_include_directories(${TARGET} PRIVATE ../mtmd ${CMAKE_SOURCE_DIR})+target_include_directories(${TARGET} PRIVATE ../mtmd ${PROJECT_SOURCE_DIR})target_link_libraries(${TARGET} PUBLIC server-context llama-ui cpp-httplib ${CMAKE_THREAD_LIBS_INIT})add_dependencies(${TARGET} llama-ui-assets)diff --git a/tools/tuning/CMakeLists.txt b/tools/tuning/CMakeLists.txtindex 39ff001802..f079838826 100644--- a/tools/tuning/CMakeLists.txt+++ b/tools/tuning/CMakeLists.txt@@ -3,7 +3,7 @@ set(TARGET ggml-metal-tuning)add_executable(${TARGET} main.cpp bench.cpp fa-vec.cpp)target_link_libraries(${TARGET} PRIVATE ggml ${CMAKE_THREAD_LIBS_INIT})target_compile_features(${TARGET} PRIVATE cxx_std_17)-target_include_directories(${TARGET} PRIVATE ${CMAKE_SOURCE_DIR}/ggml/src/ggml-metal)+target_include_directories(${TARGET} PRIVATE ${PROJECT_SOURCE_DIR}/ggml/src/ggml-metal)if(LLAMA_TOOLS_INSTALL)install(TARGETS ${TARGET} RUNTIME)