2026-02-20 16:40:00 -08:00
|
|
|
cmake_minimum_required(VERSION 3.14...3.28) # for add_link_options and implicit target directories.
|
2023-03-21 09:37:16 +09:00
|
|
|
project("llama.cpp" C CXX)
|
2024-01-28 21:26:23 +05:30
|
|
|
include(CheckIncludeFileCXX)
|
2023-03-13 14:12:33 -03:00
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
#set(CMAKE_WARN_DEPRECATED YES)
|
|
|
|
|
set(CMAKE_WARN_UNUSED_CLI YES)
|
|
|
|
|
|
2023-03-21 17:29:41 +02:00
|
|
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
|
|
|
2023-03-13 21:22:15 +02:00
|
|
|
if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE)
|
|
|
|
|
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
|
|
|
|
|
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
|
|
|
|
|
endif()
|
|
|
|
|
|
2025-08-14 03:03:57 -07:00
|
|
|
message("CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
|
|
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
# Add path to modules
|
|
|
|
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
|
|
|
|
|
|
2023-03-21 17:29:41 +02:00
|
|
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
2025-01-24 18:41:30 +02:00
|
|
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
2023-03-21 17:29:41 +02:00
|
|
|
|
2023-11-05 08:03:09 +00:00
|
|
|
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
|
2023-03-21 17:29:41 +02:00
|
|
|
set(LLAMA_STANDALONE ON)
|
|
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
include(git-vars)
|
|
|
|
|
|
2023-03-21 17:29:41 +02:00
|
|
|
# configure project version
|
|
|
|
|
# TODO
|
|
|
|
|
else()
|
|
|
|
|
set(LLAMA_STANDALONE OFF)
|
|
|
|
|
endif()
|
|
|
|
|
|
2025-03-17 10:05:23 +01:00
|
|
|
option(LLAMA_USE_SYSTEM_GGML "Use system libggml" OFF)
|
|
|
|
|
|
2025-12-03 01:25:34 -08:00
|
|
|
option(LLAMA_WASM_MEM64 "llama: use 64-bit memory in WASM builds" ON)
|
|
|
|
|
|
2023-03-21 17:29:41 +02:00
|
|
|
if (EMSCRIPTEN)
|
|
|
|
|
set(BUILD_SHARED_LIBS_DEFAULT OFF)
|
|
|
|
|
|
2025-12-03 01:25:34 -08:00
|
|
|
# Use 64-bit memory to support backend_get_memory queries
|
|
|
|
|
# TODO: analyze performance impact, see https://spidermonkey.dev/blog/2025/01/15/is-memory64-actually-worth-using
|
|
|
|
|
if (LLAMA_WASM_MEM64)
|
|
|
|
|
add_compile_options("-sMEMORY64=1")
|
|
|
|
|
add_link_options("-sMEMORY64=1")
|
|
|
|
|
endif()
|
|
|
|
|
add_link_options("-sALLOW_MEMORY_GROWTH=1")
|
|
|
|
|
|
|
|
|
|
option(LLAMA_WASM_SINGLE_FILE "llama: embed WASM inside the generated llama.js" OFF)
|
|
|
|
|
option(LLAMA_BUILD_HTML "llama: build HTML file" ON)
|
|
|
|
|
if (LLAMA_BUILD_HTML)
|
|
|
|
|
set(CMAKE_EXECUTABLE_SUFFIX ".html")
|
|
|
|
|
endif()
|
2023-03-21 17:29:41 +02:00
|
|
|
else()
|
|
|
|
|
if (MINGW)
|
|
|
|
|
set(BUILD_SHARED_LIBS_DEFAULT OFF)
|
|
|
|
|
else()
|
|
|
|
|
set(BUILD_SHARED_LIBS_DEFAULT ON)
|
|
|
|
|
endif()
|
|
|
|
|
endif()
|
|
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
option(BUILD_SHARED_LIBS "build shared libraries" ${BUILD_SHARED_LIBS_DEFAULT})
|
2023-03-21 17:29:41 +02:00
|
|
|
|
2024-07-04 12:53:42 +02:00
|
|
|
if (WIN32)
|
|
|
|
|
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
|
|
|
|
endif()
|
|
|
|
|
|
2024-12-09 09:15:13 +02:00
|
|
|
if (MSVC)
|
|
|
|
|
add_compile_options("$<$<COMPILE_LANGUAGE:C>:/utf-8>")
|
|
|
|
|
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/utf-8>")
|
2025-01-25 20:10:03 -06:00
|
|
|
add_compile_options("$<$<COMPILE_LANGUAGE:C>:/bigobj>")
|
|
|
|
|
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/bigobj>")
|
2024-11-20 01:42:00 +08:00
|
|
|
endif()
|
|
|
|
|
|
2025-12-03 22:42:29 -06:00
|
|
|
if (LLAMA_STANDALONE)
|
|
|
|
|
# enable parallel builds for msbuild
|
|
|
|
|
list(APPEND CMAKE_VS_GLOBALS UseMultiToolTask=true)
|
|
|
|
|
list(APPEND CMAKE_VS_GLOBALS EnforceProcessCountAcrossBuilds=true)
|
|
|
|
|
endif()
|
|
|
|
|
|
2025-09-15 22:54:44 -04:00
|
|
|
if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
|
|
|
|
|
set(LLAMA_TOOLS_INSTALL_DEFAULT OFF)
|
|
|
|
|
else()
|
|
|
|
|
set(LLAMA_TOOLS_INSTALL_DEFAULT ${LLAMA_STANDALONE})
|
|
|
|
|
endif()
|
|
|
|
|
|
2023-03-21 09:37:16 +09:00
|
|
|
#
|
2024-06-26 18:33:02 +03:00
|
|
|
# option list
|
2023-03-21 09:37:16 +09:00
|
|
|
#
|
2023-03-13 14:12:33 -03:00
|
|
|
|
2023-03-21 09:37:16 +09:00
|
|
|
# debug
|
2024-06-26 18:33:02 +03:00
|
|
|
option(LLAMA_ALL_WARNINGS "llama: enable all compiler warnings" ON)
|
|
|
|
|
option(LLAMA_ALL_WARNINGS_3RD_PARTY "llama: enable all compiler warnings in 3rd party libs" OFF)
|
2023-03-21 09:37:16 +09:00
|
|
|
|
2024-02-17 16:03:14 -05:00
|
|
|
# build
|
2024-06-26 18:33:02 +03:00
|
|
|
option(LLAMA_FATAL_WARNINGS "llama: enable -Werror flag" OFF)
|
2024-02-17 16:03:14 -05:00
|
|
|
|
2023-03-21 09:37:16 +09:00
|
|
|
# sanitizers
|
2024-06-26 18:33:02 +03:00
|
|
|
option(LLAMA_SANITIZE_THREAD "llama: enable thread sanitizer" OFF)
|
|
|
|
|
option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer" OFF)
|
|
|
|
|
option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF)
|
2023-03-21 09:37:16 +09:00
|
|
|
|
2024-09-27 10:42:06 +03:00
|
|
|
# utils
|
2024-10-09 18:49:52 +02:00
|
|
|
option(LLAMA_BUILD_COMMON "llama: build common utils library" ${LLAMA_STANDALONE})
|
2024-09-27 10:42:06 +03:00
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
# extra artifacts
|
2026-05-14 13:21:41 +02:00
|
|
|
option(LLAMA_BUILD_TESTS "llama: build tests" ${LLAMA_STANDALONE})
|
|
|
|
|
option(LLAMA_BUILD_TOOLS "llama: build tools" ${LLAMA_STANDALONE})
|
|
|
|
|
option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})
|
|
|
|
|
option(LLAMA_BUILD_SERVER "llama: build server example" ${LLAMA_STANDALONE})
|
2026-05-16 02:02:40 +02:00
|
|
|
option(LLAMA_BUILD_UI "llama: build the embedded Web UI for server" ON)
|
|
|
|
|
option(LLAMA_USE_PREBUILT_UI "llama: use prebuilt UI from HF Bucket when available (requires LLAMA_BUILD_UI=ON)" ON)
|
|
|
|
|
|
|
|
|
|
# Backward compat: when old var is set but new one isn't, forward the value
|
2026-05-17 14:42:26 -04:00
|
|
|
if(DEFINED LLAMA_BUILD_WEBUI)
|
2026-05-16 02:02:40 +02:00
|
|
|
set(LLAMA_BUILD_UI ${LLAMA_BUILD_WEBUI})
|
|
|
|
|
message(DEPRECATION "LLAMA_BUILD_WEBUI is deprecated, use LLAMA_BUILD_UI instead")
|
|
|
|
|
endif()
|
2026-05-17 14:42:26 -04:00
|
|
|
if(DEFINED LLAMA_USE_PREBUILT_WEBUI)
|
2026-05-16 02:02:40 +02:00
|
|
|
set(LLAMA_USE_PREBUILT_UI ${LLAMA_USE_PREBUILT_WEBUI})
|
|
|
|
|
message(DEPRECATION "LLAMA_USE_PREBUILT_WEBUI is deprecated, use LLAMA_USE_PREBUILT_UI instead")
|
|
|
|
|
endif()
|
2026-05-14 13:21:41 +02:00
|
|
|
option(LLAMA_TOOLS_INSTALL "llama: install tools" ${LLAMA_TOOLS_INSTALL_DEFAULT})
|
|
|
|
|
option(LLAMA_TESTS_INSTALL "llama: install tests" ON)
|
2024-01-14 00:41:44 -08:00
|
|
|
|
2023-03-21 09:37:16 +09:00
|
|
|
# 3rd party libs
|
2026-01-14 18:02:47 +01:00
|
|
|
option(LLAMA_OPENSSL "llama: use openssl to support HTTPS" ON)
|
2025-02-01 23:55:32 -08:00
|
|
|
option(LLAMA_LLGUIDANCE "llama-common: include LLGuidance library for structured output in common utils" OFF)
|
2023-03-21 17:29:41 +02:00
|
|
|
|
2026-01-14 18:02:47 +01:00
|
|
|
|
2023-11-27 15:25:42 -04:00
|
|
|
# Required for relocatable CMake package
|
2024-06-26 18:33:02 +03:00
|
|
|
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/build-info.cmake)
|
2024-11-26 14:18:08 +02:00
|
|
|
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/common.cmake)
|
2023-11-27 15:25:42 -04:00
|
|
|
|
2025-06-13 08:38:52 +00:00
|
|
|
if (NOT DEFINED LLAMA_BUILD_NUMBER)
|
|
|
|
|
set(LLAMA_BUILD_NUMBER ${BUILD_NUMBER})
|
|
|
|
|
endif()
|
|
|
|
|
if (NOT DEFINED LLAMA_BUILD_COMMIT)
|
|
|
|
|
set(LLAMA_BUILD_COMMIT ${BUILD_COMMIT})
|
|
|
|
|
endif()
|
2025-06-24 15:05:31 +02:00
|
|
|
set(LLAMA_INSTALL_VERSION 0.0.${LLAMA_BUILD_NUMBER})
|
2025-06-13 08:38:52 +00:00
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
# override ggml options
|
2025-01-18 16:18:15 +02:00
|
|
|
set(GGML_ALL_WARNINGS ${LLAMA_ALL_WARNINGS})
|
|
|
|
|
set(GGML_FATAL_WARNINGS ${LLAMA_FATAL_WARNINGS})
|
2024-06-28 12:37:45 +02:00
|
|
|
|
|
|
|
|
# change the default for these ggml options
|
|
|
|
|
if (NOT DEFINED GGML_LLAMAFILE)
|
2024-09-16 10:27:50 +03:00
|
|
|
set(GGML_LLAMAFILE_DEFAULT ON)
|
2024-06-28 12:37:45 +02:00
|
|
|
endif()
|
|
|
|
|
|
2024-09-16 10:27:50 +03:00
|
|
|
if (NOT DEFINED GGML_CUDA_GRAPHS)
|
|
|
|
|
set(GGML_CUDA_GRAPHS_DEFAULT ON)
|
2024-06-28 12:37:45 +02:00
|
|
|
endif()
|
2024-02-16 19:05:56 +02:00
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
# transition helpers
|
2026-02-16 16:06:48 +01:00
|
|
|
function (llama_option_depr TYPE OLD)
|
2024-06-26 18:33:02 +03:00
|
|
|
if (${OLD})
|
2026-02-16 16:06:48 +01:00
|
|
|
set(NEW "${ARGV2}")
|
|
|
|
|
if(NEW)
|
|
|
|
|
message(${TYPE} "${OLD} is deprecated, use ${NEW} instead")
|
|
|
|
|
set(${NEW} ON PARENT_SCOPE)
|
|
|
|
|
else()
|
|
|
|
|
message(${TYPE} "${OLD} is deprecated and will be ignored")
|
|
|
|
|
endif()
|
2023-03-13 14:12:33 -03:00
|
|
|
endif()
|
2023-12-13 12:10:10 -05:00
|
|
|
endfunction()
|
2023-03-21 09:37:16 +09:00
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
llama_option_depr(FATAL_ERROR LLAMA_CUBLAS GGML_CUDA)
|
|
|
|
|
llama_option_depr(WARNING LLAMA_CUDA GGML_CUDA)
|
|
|
|
|
llama_option_depr(WARNING LLAMA_METAL GGML_METAL)
|
|
|
|
|
llama_option_depr(WARNING LLAMA_METAL_EMBED_LIBRARY GGML_METAL_EMBED_LIBRARY)
|
|
|
|
|
llama_option_depr(WARNING LLAMA_NATIVE GGML_NATIVE)
|
|
|
|
|
llama_option_depr(WARNING LLAMA_RPC GGML_RPC)
|
|
|
|
|
llama_option_depr(WARNING LLAMA_SYCL GGML_SYCL)
|
|
|
|
|
llama_option_depr(WARNING LLAMA_SYCL_F16 GGML_SYCL_F16)
|
2024-07-17 19:23:50 +08:00
|
|
|
llama_option_depr(WARNING LLAMA_CANN GGML_CANN)
|
2026-02-16 16:06:48 +01:00
|
|
|
llama_option_depr(WARNING LLAMA_CURL)
|
2023-12-12 04:27:26 -05:00
|
|
|
|
2026-01-10 09:46:24 +01:00
|
|
|
include("cmake/license.cmake")
|
|
|
|
|
license_add_file("llama.cpp" "LICENSE")
|
|
|
|
|
|
2023-09-08 17:58:07 +03:00
|
|
|
#
|
2025-01-18 16:18:15 +02:00
|
|
|
# 3rd-party
|
2023-09-08 17:58:07 +03:00
|
|
|
#
|
|
|
|
|
|
2025-03-17 10:05:23 +01:00
|
|
|
if (LLAMA_USE_SYSTEM_GGML)
|
|
|
|
|
message(STATUS "Using system-provided libggml, skipping ggml build")
|
|
|
|
|
find_package(ggml REQUIRED)
|
|
|
|
|
add_library(ggml ALIAS ggml::ggml)
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
if (NOT TARGET ggml AND NOT LLAMA_USE_SYSTEM_GGML)
|
2025-06-13 08:38:52 +00:00
|
|
|
set(GGML_BUILD_NUMBER ${LLAMA_BUILD_NUMBER})
|
|
|
|
|
set(GGML_BUILD_COMMIT ${LLAMA_BUILD_COMMIT})
|
2024-07-09 11:38:00 +03:00
|
|
|
add_subdirectory(ggml)
|
|
|
|
|
# ... otherwise assume ggml is added by a parent CMakeLists.txt
|
|
|
|
|
endif()
|
2025-01-18 16:18:15 +02:00
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# build the library
|
|
|
|
|
#
|
|
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
add_subdirectory(src)
|
2023-09-14 14:04:40 -03:00
|
|
|
|
2025-01-18 16:18:15 +02:00
|
|
|
#
|
|
|
|
|
# utils, programs, examples and tests
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
if (LLAMA_BUILD_COMMON)
|
|
|
|
|
add_subdirectory(common)
|
2026-02-15 15:38:50 +01:00
|
|
|
add_subdirectory(vendor/cpp-httplib)
|
2025-01-18 16:18:15 +02:00
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)
|
|
|
|
|
include(CTest)
|
|
|
|
|
add_subdirectory(tests)
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_EXAMPLES)
|
|
|
|
|
add_subdirectory(examples)
|
|
|
|
|
add_subdirectory(pocs)
|
|
|
|
|
endif()
|
|
|
|
|
|
2025-05-02 20:27:13 +02:00
|
|
|
if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TOOLS)
|
|
|
|
|
add_subdirectory(tools)
|
|
|
|
|
endif()
|
|
|
|
|
|
2026-01-10 09:46:24 +01:00
|
|
|
# Automatically add all files from the 'licenses' directory
|
|
|
|
|
file(GLOB EXTRA_LICENSES "${CMAKE_SOURCE_DIR}/licenses/LICENSE-*")
|
|
|
|
|
|
|
|
|
|
foreach(FILE_PATH ${EXTRA_LICENSES})
|
|
|
|
|
get_filename_component(FILE_NAME "${FILE_PATH}" NAME)
|
|
|
|
|
string(REGEX REPLACE "^LICENSE-" "" NAME "${FILE_NAME}")
|
|
|
|
|
license_add_file("${NAME}" "${FILE_PATH}")
|
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
|
|
if (LLAMA_BUILD_COMMON)
|
2026-04-17 11:11:46 +03:00
|
|
|
license_generate(llama-common)
|
2026-01-10 09:46:24 +01:00
|
|
|
endif()
|
|
|
|
|
|
2023-08-21 23:07:43 +03:00
|
|
|
#
|
|
|
|
|
# install
|
|
|
|
|
#
|
|
|
|
|
|
2023-07-19 15:01:11 +08:00
|
|
|
include(GNUInstallDirs)
|
2023-09-14 14:04:40 -03:00
|
|
|
include(CMakePackageConfigHelpers)
|
|
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
set(LLAMA_INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Location of header files")
|
|
|
|
|
set(LLAMA_LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Location of library files")
|
|
|
|
|
set(LLAMA_BIN_INSTALL_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Location of binary files")
|
|
|
|
|
|
2024-11-25 16:56:24 -05:00
|
|
|
set(LLAMA_PUBLIC_HEADERS
|
|
|
|
|
${CMAKE_CURRENT_SOURCE_DIR}/include/llama.h
|
|
|
|
|
${CMAKE_CURRENT_SOURCE_DIR}/include/llama-cpp.h)
|
2025-01-26 12:07:48 -04:00
|
|
|
|
|
|
|
|
set_target_properties(llama
|
|
|
|
|
PROPERTIES
|
|
|
|
|
PUBLIC_HEADER "${LLAMA_PUBLIC_HEADERS}")
|
|
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
install(TARGETS llama LIBRARY PUBLIC_HEADER)
|
|
|
|
|
|
2026-04-17 11:11:46 +03:00
|
|
|
if (LLAMA_BUILD_COMMON)
|
|
|
|
|
install(TARGETS llama-common LIBRARY)
|
|
|
|
|
endif()
|
|
|
|
|
|
2023-09-14 14:04:40 -03:00
|
|
|
configure_package_config_file(
|
2024-06-26 18:33:02 +03:00
|
|
|
${CMAKE_CURRENT_SOURCE_DIR}/cmake/llama-config.cmake.in
|
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/llama-config.cmake
|
|
|
|
|
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/llama
|
2023-09-14 14:04:40 -03:00
|
|
|
PATH_VARS LLAMA_INCLUDE_INSTALL_DIR
|
|
|
|
|
LLAMA_LIB_INSTALL_DIR
|
|
|
|
|
LLAMA_BIN_INSTALL_DIR )
|
|
|
|
|
|
|
|
|
|
write_basic_package_version_file(
|
2024-06-26 18:33:02 +03:00
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/llama-version.cmake
|
2023-09-14 14:04:40 -03:00
|
|
|
VERSION ${LLAMA_INSTALL_VERSION}
|
|
|
|
|
COMPATIBILITY SameMajorVersion)
|
|
|
|
|
|
2024-06-26 18:33:02 +03:00
|
|
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/llama-config.cmake
|
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/llama-version.cmake
|
|
|
|
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/llama)
|
2023-09-14 14:04:40 -03:00
|
|
|
|
2024-06-03 01:06:24 -07:00
|
|
|
configure_file(cmake/llama.pc.in
|
|
|
|
|
"${CMAKE_CURRENT_BINARY_DIR}/llama.pc"
|
|
|
|
|
@ONLY)
|
|
|
|
|
|
|
|
|
|
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/llama.pc"
|
2025-02-06 12:08:13 +01:00
|
|
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
|