Skip to content

Commit 17dbfd1

Browse files
committed
chore: Updates for more integration options.
1 parent 06816ef commit 17dbfd1

3 files changed

Lines changed: 165 additions & 26 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,46 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
66
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
77

88
option(AGENT_CPP_BUILD_TESTS "Build agent.cpp tests" OFF)
9+
option(AGENT_CPP_BUNDLED_LLAMA "Bundle llama.cpp (vs. find installed)" ON)
910

1011
set(LLAMA_CPP_DIR "" CACHE PATH "Path to existing llama.cpp directory (leave empty to use submodule)")
1112

12-
if(LLAMA_CPP_DIR)
13-
if(NOT EXISTS "${LLAMA_CPP_DIR}/CMakeLists.txt")
14-
message(FATAL_ERROR "LLAMA_CPP_DIR is set to '${LLAMA_CPP_DIR}' but no CMakeLists.txt found there.")
13+
if(AGENT_CPP_BUNDLED_LLAMA)
14+
# Use bundled llama.cpp (submodule or custom path)
15+
if(LLAMA_CPP_DIR)
16+
if(NOT EXISTS "${LLAMA_CPP_DIR}/CMakeLists.txt")
17+
message(FATAL_ERROR "LLAMA_CPP_DIR is set to '${LLAMA_CPP_DIR}' but no CMakeLists.txt found there.")
18+
endif()
19+
set(LLAMA_SOURCE_DIR "${LLAMA_CPP_DIR}")
20+
message(STATUS "Using custom llama.cpp from: ${LLAMA_SOURCE_DIR}")
21+
else()
22+
set(LLAMA_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/llama.cpp")
23+
if(NOT EXISTS "${LLAMA_SOURCE_DIR}/CMakeLists.txt")
24+
message(FATAL_ERROR
25+
"llama.cpp submodule not found at ${LLAMA_SOURCE_DIR}\n"
26+
"Please initialize submodules with:\n"
27+
" git submodule update --init --recursive\n"
28+
"Or provide a custom path with -DLLAMA_CPP_DIR=/path/to/llama.cpp"
29+
)
30+
endif()
31+
message(STATUS "Using llama.cpp submodule from: ${LLAMA_SOURCE_DIR}")
1532
endif()
16-
set(LLAMA_SOURCE_DIR "${LLAMA_CPP_DIR}")
17-
message(STATUS "Using custom llama.cpp from: ${LLAMA_SOURCE_DIR}")
18-
else()
19-
set(LLAMA_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/llama.cpp")
20-
if(NOT EXISTS "${LLAMA_SOURCE_DIR}/CMakeLists.txt")
21-
message(FATAL_ERROR
22-
"llama.cpp submodule not found at ${LLAMA_SOURCE_DIR}\n"
23-
"Please initialize submodules with:\n"
24-
" git submodule update --init --recursive\n"
25-
"Or provide a custom path with -DLLAMA_CPP_DIR=/path/to/llama.cpp"
26-
)
27-
endif()
28-
message(STATUS "Using llama.cpp submodule from: ${LLAMA_SOURCE_DIR}")
29-
endif()
3033

31-
set(LLAMA_BUILD_TESTS OFF CACHE BOOL "" FORCE)
32-
set(LLAMA_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
33-
set(LLAMA_BUILD_SERVER OFF CACHE BOOL "" FORCE)
34-
set(LLAMA_BUILD_TOOLS OFF CACHE BOOL "" FORCE)
35-
set(LLAMA_BUILD_COMMON ON CACHE BOOL "" FORCE)
36-
set(LLAMA_CURL OFF CACHE BOOL "" FORCE)
37-
set(LLAMA_HTTPLIB OFF CACHE BOOL "" FORCE)
38-
set(LLAMA_LLGUIDANCE OFF CACHE BOOL "" FORCE)
34+
set(LLAMA_BUILD_TESTS OFF CACHE BOOL "" FORCE)
35+
set(LLAMA_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
36+
set(LLAMA_BUILD_SERVER OFF CACHE BOOL "" FORCE)
37+
set(LLAMA_BUILD_TOOLS OFF CACHE BOOL "" FORCE)
38+
set(LLAMA_BUILD_COMMON ON CACHE BOOL "" FORCE)
39+
set(LLAMA_CURL OFF CACHE BOOL "" FORCE)
40+
set(LLAMA_HTTPLIB OFF CACHE BOOL "" FORCE)
41+
set(LLAMA_LLGUIDANCE OFF CACHE BOOL "" FORCE)
3942

40-
add_subdirectory(${LLAMA_SOURCE_DIR} ${CMAKE_BINARY_DIR}/llama.cpp)
43+
add_subdirectory(${LLAMA_SOURCE_DIR} ${CMAKE_BINARY_DIR}/llama.cpp)
44+
else()
45+
# Use system-installed llama.cpp
46+
find_package(llama REQUIRED)
47+
message(STATUS "Using system-installed llama.cpp")
48+
endif()
4149

4250
add_library(model STATIC src/model.cpp)
4351
target_include_directories(model
@@ -94,12 +102,53 @@ option(AGENT_CPP_INSTALL "Generate install target" OFF)
94102

95103
if(AGENT_CPP_INSTALL)
96104
include(GNUInstallDirs)
105+
include(CMakePackageConfigHelpers)
97106

107+
# Install public headers
98108
install(FILES
99109
src/agent.h
100110
src/model.h
101111
src/tool.h
102112
src/callbacks.h
103113
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/agent-cpp
104114
)
115+
116+
# Install libraries with export set
117+
install(TARGETS agent model
118+
EXPORT agent-cpp-targets
119+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
120+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
121+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
122+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
123+
)
124+
125+
# Generate and install the export targets file
126+
install(EXPORT agent-cpp-targets
127+
FILE agent-cpp-targets.cmake
128+
NAMESPACE agent-cpp::
129+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/agent-cpp
130+
)
131+
132+
# Generate the version file for find_package version checking
133+
write_basic_package_version_file(
134+
"${CMAKE_CURRENT_BINARY_DIR}/agent-cpp-config-version.cmake"
135+
VERSION ${PROJECT_VERSION}
136+
COMPATIBILITY SameMajorVersion
137+
)
138+
139+
# Generate the config file from template
140+
configure_package_config_file(
141+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/agent-cpp-config.cmake.in"
142+
"${CMAKE_CURRENT_BINARY_DIR}/agent-cpp-config.cmake"
143+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/agent-cpp
144+
)
145+
146+
# Install the CMake config files
147+
install(FILES
148+
"${CMAKE_CURRENT_BINARY_DIR}/agent-cpp-config.cmake"
149+
"${CMAKE_CURRENT_BINARY_DIR}/agent-cpp-config-version.cmake"
150+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/agent-cpp
151+
)
152+
153+
message(STATUS "Install target enabled. Install with: cmake --install build --prefix /your/path")
105154
endif()

‎README.md‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,80 @@ We define an `agent` by 5 building blocks:
2929
- [Model](./#model)
3030
- [Tools](./#tools)
3131

32+
## Integration
33+
34+
### Option 1: FetchContent (Recommended)
35+
36+
The easiest way to integrate agent.cpp into your CMake project:
37+
38+
```cmake
39+
include(FetchContent)
40+
FetchContent_Declare(
41+
agent-cpp
42+
GIT_REPOSITORY https://github.com/mozilla-ai/agent.cpp
43+
GIT_TAG main # or a specific release tag like v0.1.0
44+
)
45+
FetchContent_MakeAvailable(agent-cpp)
46+
47+
add_executable(my_app main.cpp)
48+
target_link_libraries(my_app PRIVATE agent model)
49+
```
50+
51+
### Option 2: Installed Package
52+
53+
Build and install agent.cpp, then use `find_package`:
54+
55+
```bash
56+
# Clone and build
57+
git clone --recursive https://github.com/mozilla-ai/agent.cpp
58+
cd agent.cpp
59+
cmake -B build -DAGENT_CPP_INSTALL=ON -DCMAKE_BUILD_TYPE=Release
60+
cmake --build build
61+
62+
# Install (use --prefix for custom location)
63+
cmake --install build --prefix ~/.local/agent-cpp
64+
```
65+
66+
Then in your project:
67+
68+
```cmake
69+
# If installed to a custom prefix, tell CMake where to find it
70+
list(APPEND CMAKE_PREFIX_PATH "~/.local/agent-cpp")
71+
72+
find_package(agent-cpp REQUIRED)
73+
74+
add_executable(my_app main.cpp)
75+
target_link_libraries(my_app PRIVATE agent-cpp::agent)
76+
```
77+
78+
### Option 3: Git Submodule
79+
80+
Add agent.cpp as a submodule and include it directly:
81+
82+
```bash
83+
git submodule add https://github.com/mozilla-ai/agent.cpp agent.cpp
84+
git submodule update --init --recursive
85+
```
86+
87+
```cmake
88+
add_subdirectory(agent.cpp)
89+
target_link_libraries(my_app PRIVATE agent model)
90+
```
91+
92+
## Hardware Acceleration
93+
94+
This project uses [llama.cpp](https://github.com/ggml-org/llama.cpp) as a submodule. You can enable hardware-specific acceleration by passing the appropriate CMake flags when building. For example:
95+
96+
```bash
97+
# CUDA (NVIDIA GPUs)
98+
cmake -B build -DGGML_CUDA=ON
99+
100+
# OpenBLAS (CPU)
101+
cmake -B build -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS
102+
```
103+
104+
For a complete list of build options and backend-specific instructions, see the [llama.cpp build documentation](https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md).
105+
32106
## Examples
33107

34108
- [Memory](./examples/memory/README.md)

‎cmake/agent-cpp-config.cmake.in‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@PACKAGE_INIT@
2+
3+
include(CMakeFindDependencyMacro)
4+
5+
# Check if agent-cpp was built with bundled llama.cpp
6+
set(AGENT_CPP_BUNDLED_LLAMA @AGENT_CPP_BUNDLED_LLAMA@)
7+
8+
if(NOT AGENT_CPP_BUNDLED_LLAMA)
9+
# agent-cpp was built against system llama.cpp, so consumers need it too
10+
find_dependency(llama REQUIRED)
11+
endif()
12+
13+
# Include the exported targets
14+
include("${CMAKE_CURRENT_LIST_DIR}/agent-cpp-targets.cmake")
15+
16+
check_required_components(agent-cpp)

0 commit comments

Comments
 (0)