Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Ggml org Llama cpp Server CMake Build

From Leeroopedia
Field Value
Implementation Name Server CMake Build
Doc Type External Tool Doc
Domain Build Systems, CMake
Description CMake build configuration for compiling the llama-server executable and its server-context static library
Related Workflow OpenAI_Compatible_Server

Overview

Description

The Server CMake Build implementation defines the CMake build rules for producing the llama-server executable. The build is organized into two targets: a static library (server-context) containing core server logic, and the executable (llama-server) that adds HTTP transport and the main entry point. Static web assets are embedded into the binary at build time through CMake custom commands.

Usage

Build the server from the repository root:

cmake -B build
cmake --build build --target llama-server

The LLAMA_HTTPLIB option must be enabled (it is on by default). To skip building the server entirely:

cmake -B build -DLLAMA_BUILD_SERVER=OFF

Code Reference

Field Value
Source Location tools/server/CMakeLists.txt:1-71
Build Command cmake --build . --target llama-server
Static Library Target server-context
Executable Target llama-server

Static library target (server-context):

set(TARGET server-context)

add_library(${TARGET} STATIC
    server-task.cpp
    server-task.h
    server-queue.cpp
    server-queue.h
    server-common.cpp
    server-common.h
    server-context.cpp
    server-context.h
)

target_include_directories(${TARGET} PRIVATE ../mtmd)
target_include_directories(${TARGET} PRIVATE ${CMAKE_SOURCE_DIR})
target_link_libraries(${TARGET} PUBLIC common mtmd ${CMAKE_THREAD_LIBS_INIT})

Executable target (llama-server):

set(TARGET llama-server)

set(TARGET_SRCS
    server.cpp
    server-http.cpp
    server-http.h
    server-models.cpp
    server-models.h
)

add_executable(${TARGET} ${TARGET_SRCS})
target_link_libraries(${TARGET} PRIVATE server-context PUBLIC common cpp-httplib ${CMAKE_THREAD_LIBS_INIT})
target_compile_features(${TARGET} PRIVATE cxx_std_17)

Asset embedding via custom command:

set(PUBLIC_ASSETS
    index.html.gz
    loading.html
)

foreach(asset ${PUBLIC_ASSETS})
    set(input "${CMAKE_CURRENT_SOURCE_DIR}/public/${asset}")
    set(output "${CMAKE_CURRENT_BINARY_DIR}/${asset}.hpp")
    add_custom_command(
        DEPENDS "${input}"
        OUTPUT "${output}"
        COMMAND "${CMAKE_COMMAND}" "-DINPUT=${input}" "-DOUTPUT=${output}" -P "${PROJECT_SOURCE_DIR}/scripts/xxd.cmake"
    )
endforeach()

I/O Contract

Direction Description
Input C++ source files in tools/server/, public HTML assets in tools/server/public/, dependent libraries (common, mtmd, cpp-httplib)
Output llama-server executable binary with embedded web assets
Preconditions LLAMA_HTTPLIB must be ON (fatal error otherwise); C++17 compiler required
Side Effects Generated .hpp header files for embedded assets in the build directory

Usage Examples

Basic build:

# Configure and build
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --target llama-server -j$(nproc)

Build with CUDA acceleration:

cmake -B build -DGGML_CUDA=ON
cmake --build build --target llama-server -j$(nproc)

Build with shared libraries:

cmake -B build -DBUILD_SHARED_LIBS=ON
cmake --build build --target llama-server -j$(nproc)

When BUILD_SHARED_LIBS is enabled, the server-context static library is compiled with POSITION_INDEPENDENT_CODE ON to ensure compatibility with shared library linking.

Windows build (additional ws2_32 dependency):

cmake -B build -G "Visual Studio 17 2022"
cmake --build build --target llama-server --config Release

On Windows, the target additionally links against ws2_32 for Winsock2 socket support.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment