2024-04-21 18:48:53 +01:00
|
|
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
|
2024-06-26 18:33:02 +03:00
|
|
|
|
2025-12-10 15:28:59 +01:00
|
|
|
# server-context containing the core server logic, used by llama-server and CLI
|
|
|
|
|
|
|
|
|
|
set(TARGET server-context)
|
|
|
|
|
|
|
|
|
|
add_library(${TARGET} STATIC
|
2026-04-22 10:28:45 +02:00
|
|
|
server-chat.cpp
|
|
|
|
|
server-chat.h
|
2025-12-10 15:28:59 +01:00
|
|
|
server-task.cpp
|
|
|
|
|
server-task.h
|
|
|
|
|
server-queue.cpp
|
|
|
|
|
server-queue.h
|
|
|
|
|
server-common.cpp
|
|
|
|
|
server-common.h
|
|
|
|
|
server-context.cpp
|
|
|
|
|
server-context.h
|
2026-03-27 10:07:11 +01:00
|
|
|
server-tools.cpp
|
|
|
|
|
server-tools.h
|
2025-12-10 15:28:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (BUILD_SHARED_LIBS)
|
|
|
|
|
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
target_include_directories(${TARGET} PRIVATE ../mtmd)
|
|
|
|
|
target_include_directories(${TARGET} PRIVATE ${CMAKE_SOURCE_DIR})
|
2026-04-17 11:11:46 +03:00
|
|
|
target_link_libraries(${TARGET} PUBLIC llama-common mtmd ${CMAKE_THREAD_LIBS_INIT})
|
2025-12-10 15:28:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# llama-server executable
|
|
|
|
|
|
|
|
|
|
set(TARGET llama-server)
|
|
|
|
|
|
2024-04-21 18:48:53 +01:00
|
|
|
set(TARGET_SRCS
|
2024-03-21 11:50:43 +00:00
|
|
|
server.cpp
|
2025-11-17 22:05:44 +01:00
|
|
|
server-http.cpp
|
|
|
|
|
server-http.h
|
2025-12-01 19:41:04 +01:00
|
|
|
server-models.cpp
|
|
|
|
|
server-models.h
|
2024-03-21 11:50:43 +00:00
|
|
|
)
|
2024-06-26 18:33:02 +03:00
|
|
|
|
2026-05-14 13:21:41 +02:00
|
|
|
# Option to specify custom HF bucket for webui (defaults to llama-ui)
|
|
|
|
|
# Usage: cmake -B build -DLLAMA_WEBUI_HF_BUCKET=llama-ui
|
|
|
|
|
set(LLAMA_WEBUI_HF_BUCKET "llama-ui" CACHE STRING "Hugging Face bucket name for prebuilt webui assets")
|
2026-03-27 11:25:55 -05:00
|
|
|
|
|
|
|
|
if (LLAMA_BUILD_WEBUI)
|
|
|
|
|
set(PUBLIC_ASSETS
|
2026-03-31 15:44:26 +02:00
|
|
|
index.html
|
|
|
|
|
bundle.js
|
|
|
|
|
bundle.css
|
2026-03-27 11:25:55 -05:00
|
|
|
loading.html
|
2024-04-21 18:48:53 +01:00
|
|
|
)
|
2026-03-27 11:25:55 -05:00
|
|
|
|
2026-05-14 13:21:41 +02:00
|
|
|
# Determine source of webui assets (priority: local > HF Bucket)
|
|
|
|
|
set(WEBUI_SOURCE "")
|
|
|
|
|
set(WEBUI_SOURCE_DIR "")
|
|
|
|
|
|
|
|
|
|
# Priority 1: Check for local webui build output
|
|
|
|
|
set(LOCAL_WEBUI_DIR "${CMAKE_CURRENT_SOURCE_DIR}/public")
|
|
|
|
|
|
|
|
|
|
# Verify all required assets exist before declaring local source valid
|
|
|
|
|
set(ALL_ASSETS_PRESENT TRUE)
|
2026-03-27 11:25:55 -05:00
|
|
|
foreach(asset ${PUBLIC_ASSETS})
|
2026-05-14 13:21:41 +02:00
|
|
|
if(NOT EXISTS "${LOCAL_WEBUI_DIR}/${asset}")
|
|
|
|
|
set(ALL_ASSETS_PRESENT FALSE)
|
|
|
|
|
break()
|
|
|
|
|
endif()
|
2026-03-27 11:25:55 -05:00
|
|
|
endforeach()
|
2026-05-14 13:21:41 +02:00
|
|
|
|
|
|
|
|
if(ALL_ASSETS_PRESENT)
|
|
|
|
|
set(WEBUI_SOURCE "local")
|
|
|
|
|
set(WEBUI_SOURCE_DIR "${LOCAL_WEBUI_DIR}")
|
|
|
|
|
message(STATUS "WebUI: using local build from ${WEBUI_SOURCE_DIR}")
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
# Priority 2: Build-time asset provisioning (npm build → HF Bucket fallback)
|
|
|
|
|
if(NOT WEBUI_SOURCE_DIR)
|
2026-05-14 17:57:20 +02:00
|
|
|
# Environment variable takes precedence (e.g., from CI workflows)
|
|
|
|
|
if(DEFINED ENV{HF_WEBUI_VERSION})
|
|
|
|
|
set(HF_WEBUI_VERSION "$ENV{HF_WEBUI_VERSION}")
|
|
|
|
|
# Validate against allowed characters to prevent CMake list separator
|
|
|
|
|
# or path-traversal issues in stamp filenames and download URLs
|
|
|
|
|
if(NOT HF_WEBUI_VERSION MATCHES "^[A-Za-z0-9._-]+$")
|
|
|
|
|
message(FATAL_ERROR "WebUI: invalid HF_WEBUI_VERSION='${HF_WEBUI_VERSION}' - must match ^[A-Za-z0-9._-]+$")
|
|
|
|
|
endif()
|
|
|
|
|
message(STATUS "WebUI: using HF_WEBUI_VERSION from environment=${HF_WEBUI_VERSION}")
|
|
|
|
|
elseif(DEFINED LLAMA_BUILD_NUMBER)
|
|
|
|
|
set(HF_WEBUI_VERSION "b${LLAMA_BUILD_NUMBER}")
|
2026-05-14 13:21:41 +02:00
|
|
|
message(STATUS "WebUI: using LLAMA_BUILD_NUMBER=${HF_WEBUI_VERSION}")
|
|
|
|
|
else()
|
|
|
|
|
set(HF_WEBUI_VERSION "")
|
2026-05-14 17:57:20 +02:00
|
|
|
message(STATUS "WebUI: version not specified (will use HF 'latest')")
|
2026-05-14 13:21:41 +02:00
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
# Stamp file embeds the version tag so a changed build number triggers
|
|
|
|
|
# a fresh provision run on the next `cmake --build` without reconfiguring.
|
|
|
|
|
if("${HF_WEBUI_VERSION}" STREQUAL "")
|
|
|
|
|
set(WEBUI_VERSION_TAG "provisioned")
|
|
|
|
|
else()
|
|
|
|
|
set(WEBUI_VERSION_TAG "${HF_WEBUI_VERSION}")
|
|
|
|
|
endif()
|
|
|
|
|
set(WEBUI_STAMP "${CMAKE_CURRENT_BINARY_DIR}/.webui-${WEBUI_VERSION_TAG}.stamp")
|
|
|
|
|
|
2026-05-14 17:57:20 +02:00
|
|
|
# Join assets with + separator (safe across all platforms, unlike ; and |)
|
|
|
|
|
string(REPLACE ";" "+" PUBLIC_ASSETS_JOINED "${PUBLIC_ASSETS}")
|
2026-05-14 13:21:41 +02:00
|
|
|
|
|
|
|
|
add_custom_command(
|
|
|
|
|
OUTPUT ${WEBUI_STAMP}
|
|
|
|
|
COMMAND ${CMAKE_COMMAND}
|
|
|
|
|
"-DSOURCE_DIR=${PROJECT_SOURCE_DIR}"
|
|
|
|
|
"-DPUBLIC_DIR=${CMAKE_CURRENT_SOURCE_DIR}/public"
|
|
|
|
|
"-DHF_BUCKET=${LLAMA_WEBUI_HF_BUCKET}"
|
|
|
|
|
"-DHF_VERSION=${HF_WEBUI_VERSION}"
|
|
|
|
|
"-DHF_ENABLED=${LLAMA_USE_PREBUILT_WEBUI}"
|
2026-05-14 17:57:20 +02:00
|
|
|
"-DASSETS=${PUBLIC_ASSETS_JOINED}"
|
2026-05-14 13:21:41 +02:00
|
|
|
"-DSTAMP_FILE=${WEBUI_STAMP}"
|
|
|
|
|
"-DNPM_DIR=${CMAKE_CURRENT_SOURCE_DIR}/webui"
|
|
|
|
|
-P ${PROJECT_SOURCE_DIR}/scripts/webui-download.cmake
|
|
|
|
|
COMMENT "Building/provisioning WebUI assets (npm build -> HF Bucket fallback)"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
set(WEBUI_SOURCE "provisioned")
|
|
|
|
|
set(WEBUI_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/public")
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
# Process assets from the determined source
|
|
|
|
|
if(WEBUI_SOURCE_DIR)
|
|
|
|
|
foreach(asset ${PUBLIC_ASSETS})
|
|
|
|
|
set(input "${WEBUI_SOURCE_DIR}/${asset}")
|
|
|
|
|
set(output "${CMAKE_CURRENT_BINARY_DIR}/${asset}.hpp")
|
|
|
|
|
list(APPEND TARGET_SRCS ${output})
|
|
|
|
|
|
|
|
|
|
if(WEBUI_SOURCE STREQUAL "local")
|
|
|
|
|
# Local build: files exist at configure time
|
|
|
|
|
if(NOT EXISTS "${input}")
|
|
|
|
|
message(FATAL_ERROR "WebUI asset not found: ${input}")
|
|
|
|
|
endif()
|
|
|
|
|
set(dependency "${input}")
|
|
|
|
|
else()
|
|
|
|
|
# HF Bucket: files are downloaded at build time
|
|
|
|
|
set(dependency "${WEBUI_STAMP}")
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
add_custom_command(
|
|
|
|
|
DEPENDS ${dependency}
|
|
|
|
|
OUTPUT "${output}"
|
|
|
|
|
COMMAND "${CMAKE_COMMAND}" "-DINPUT=${input}" "-DOUTPUT=${output}" -P "${PROJECT_SOURCE_DIR}/scripts/xxd.cmake"
|
|
|
|
|
)
|
|
|
|
|
set_source_files_properties(${output} PROPERTIES GENERATED TRUE)
|
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
|
|
add_definitions(-DLLAMA_BUILD_WEBUI)
|
|
|
|
|
add_definitions(-DLLAMA_WEBUI_DEFAULT_ENABLED=1)
|
|
|
|
|
message(STATUS "WebUI: embedded with source: ${WEBUI_SOURCE}")
|
|
|
|
|
else()
|
|
|
|
|
# WebUI source not found - issue warning but don't fail the build
|
|
|
|
|
# The server will still build but without webui embedded
|
|
|
|
|
message(WARNING "WebUI: no source available. Neither local build (tools/server/public/) nor HF Bucket download succeeded.")
|
|
|
|
|
message(WARNING "WebUI: building server without embedded WebUI. Set LLAMA_BUILD_WEBUI=OFF to suppress this warning.")
|
|
|
|
|
add_definitions(-DLLAMA_WEBUI_DEFAULT_ENABLED=0)
|
|
|
|
|
endif()
|
2026-03-27 11:25:55 -05:00
|
|
|
else()
|
2026-05-14 13:21:41 +02:00
|
|
|
# WebUI is disabled at build time
|
|
|
|
|
add_definitions(-DLLAMA_WEBUI_DEFAULT_ENABLED=0)
|
2026-03-27 11:25:55 -05:00
|
|
|
endif()
|
2024-06-26 18:33:02 +03:00
|
|
|
|
2024-04-21 18:48:53 +01:00
|
|
|
add_executable(${TARGET} ${TARGET_SRCS})
|
2023-07-19 15:01:11 +08:00
|
|
|
install(TARGETS ${TARGET} RUNTIME)
|
2024-06-26 18:33:02 +03:00
|
|
|
|
2025-08-20 18:32:05 +08:00
|
|
|
target_include_directories(${TARGET} PRIVATE ../mtmd)
|
2024-12-24 21:33:04 +01:00
|
|
|
target_include_directories(${TARGET} PRIVATE ${CMAKE_SOURCE_DIR})
|
2026-04-17 11:11:46 +03:00
|
|
|
target_link_libraries(${TARGET} PRIVATE server-context PUBLIC llama-common cpp-httplib ${CMAKE_THREAD_LIBS_INIT})
|
2024-06-26 18:33:02 +03:00
|
|
|
|
2024-11-29 21:54:58 +01:00
|
|
|
target_compile_features(${TARGET} PRIVATE cxx_std_17)
|