Skip to content

Commit 65b0d8b

Browse files
committed
Replace main-cmake-pkg with simple-cmake-pkg
1 parent 1760b92 commit 65b0d8b

File tree

7 files changed

+59
-70
lines changed

7 files changed

+59
-70
lines changed

examples/main-cmake-pkg/CMakeLists.txt

Lines changed: 0 additions & 32 deletions
This file was deleted.

examples/main-cmake-pkg/README.md

Lines changed: 0 additions & 31 deletions
This file was deleted.
File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project(llama-simple-cmake-pkg)
3+
4+
set(TARGET llama-simple-cmake-pkg)
5+
6+
find_package(Llama REQUIRED)
7+
8+
add_executable(${TARGET} ${CMAKE_CURRENT_LIST_DIR}/../simple/simple.cpp)
9+
install(TARGETS ${TARGET} RUNTIME)
10+
target_link_libraries(${TARGET} PRIVATE llama ggml::all ${CMAKE_THREAD_LIBS_INIT})
11+
target_compile_features(${TARGET} PRIVATE cxx_std_17)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# llama.cpp/example/simple-cmake-pkg
2+
3+
This program builds [simple](../simple) using a relocatable CMake package. It serves as an example of using the `find_package()` CMake command to conveniently include [llama.cpp](https://github.com/ggerganov/llama.cpp) in projects which live outside of the source tree.
4+
5+
## Building
6+
7+
Because this example is "outside of the source tree", it is important to first build/install llama.cpp using CMake. An example is provided here, but please see the [llama.cpp build instructions](../..) for more detailed build instructions.
8+
9+
### Considerations
10+
11+
When hardware acceleration libraries are used (e.g. CUDA, Metal, Vulkan, etc.), the appropriate dependencies will be searched for automatically. So, for example, when finding a package
12+
13+
### Build llama.cpp and install to llama.cpp/inst
14+
15+
```sh
16+
git clone https://github.com/ggerganov/llama.cpp
17+
cd llama.cpp
18+
mkdir build
19+
mkdir inst
20+
cmake -S . -B build
21+
cmake --build build
22+
cmake --install build --prefix inst
23+
```
24+
25+
### Build simple-cmake-pkg
26+
27+
```sh
28+
cd examples\simple-cmake-pkg
29+
mkdir build
30+
cmake -S . -B build -DCMAKE_PREFIX_PATH=../../inst/lib/cmake
31+
cmake --build build
32+
```
33+
34+
### Run simple-cmake-pkg
35+
36+
```sh
37+
./build/llama-simple-cmake-pkg -m ./models/llama-7b-v2/ggml-model-f16.gguf "Hello my name is"
38+
```

ggml/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ endif()
269269
# Create CMake package
270270
#
271271

272-
# * Generate version info based on git commit.
272+
# Generate version info based on git commit.
273273

274274
find_program(GIT_EXE NAMES git git.exe REQUIRED NO_CMAKE_FIND_ROOT_PATH)
275275
execute_process(COMMAND ${GIT_EXE} rev-list --count HEAD
@@ -288,7 +288,7 @@ execute_process(COMMAND ${GIT_EXE} rev-parse --short HEAD
288288
OUTPUT_STRIP_TRAILING_WHITESPACE
289289
)
290290

291-
# * Capture variables prefixed with GGML_.
291+
# Capture variables prefixed with GGML_.
292292

293293
set(variable_set_statements
294294
"
@@ -297,6 +297,8 @@ set(variable_set_statements
297297
298298
")
299299

300+
set(GGML_SHARED_LIB ${BUILD_SHARED_LIBS})
301+
300302
get_cmake_property(all_variables VARIABLES)
301303
foreach(variable_name IN LISTS all_variables)
302304
if(variable_name MATCHES "^GGML_")
@@ -310,7 +312,7 @@ endforeach()
310312

311313
set(GGML_VARIABLES_EXPANDED ${variable_set_statements})
312314

313-
# * Create the CMake package and set install location.
315+
# Create the CMake package and set install location.
314316

315317
set(GGML_INSTALL_VERSION 0.0.${GGML_BUILD_NUMBER})
316318
set(GGML_INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Location of header files")

ggml/cmake/ggml-config.cmake.in

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ find_library(GGML_LIBRARY ggml
1717
add_library(ggml::ggml UNKNOWN IMPORTED)
1818
set_target_properties(ggml::ggml
1919
PROPERTIES
20-
INTERFACE_LINK_LIBRARIES "${GGML_LIBRARY}")
20+
IMPORTED_LOCATION "${GGML_LIBRARY}")
2121

2222
find_library(GGML_BASE_LIBRARY ggml-base
2323
REQUIRED
@@ -27,7 +27,7 @@ find_library(GGML_BASE_LIBRARY ggml-base
2727
add_library(ggml::ggml-base UNKNOWN IMPORTED)
2828
set_target_properties(ggml::ggml-base
2929
PROPERTIES
30-
INTERFACE_LINK_LIBRARIES "${GGML_BASE_LIBRARY}")
30+
IMPORTED_LOCATION "${GGML_BASE_LIBRARY}")
3131

3232
if (NOT GGML_SHARED_LIB)
3333
if (APPLE AND GGML_ACCELERATE)
@@ -91,7 +91,8 @@ endif()
9191

9292
set(_ggml_all_targets "")
9393
foreach(_ggml_backend ${GGML_AVAILABLE_BACKENDS})
94-
string(replace "-" "_" _ggml_backend_pfx "${_ggml_backend}")
94+
string(REPLACE "-" "_" _ggml_backend_pfx "${_ggml_backend}")
95+
string(TOUPPER "${_ggml_backend_pfx}" _ggml_backend_pfx)
9596

9697
find_library(${_ggml_backend_pfx}_LIBRARY ${_ggml_backend}
9798
REQUIRED
@@ -105,7 +106,7 @@ foreach(_ggml_backend ${GGML_AVAILABLE_BACKENDS})
105106
PROPERTIES
106107
INTERFACE_INCLUDE_DIRECTORIES "${GGML_INCLUDE_DIR}"
107108
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
108-
IMPORTED_LOCATION "${_ggml_backend_pfx}_LIBRARY"
109+
IMPORTED_LOCATION "${${_ggml_backend_pfx}_LIBRARY}"
109110
INTERFACE_COMPILE_FEATURES cxx_std_17
110111
POSITION_INDEPENDENT_CODE ON)
111112

0 commit comments

Comments
 (0)