Felipe97/llama-cpp-compiled
01.1k
1cmake_minimum_required(VERSION 3.14...3.28) # for add_link_options and implicit target directories.2project("llama.cpp" C CXX)3include(CheckIncludeFileCXX)4 5### llama.cpp version6set(LLAMA_VERSION_MAJOR 0)7set(LLAMA_VERSION_MINOR 4)8set(LLAMA_VERSION_PATCH 1)9set(LLAMA_VERSION_BASE "${LLAMA_VERSION_MAJOR}.${LLAMA_VERSION_MINOR}.${LLAMA_VERSION_PATCH}")10 11# whether this is a development/nightly build12# set this to OFF when making a release from a release tag (vX.Y.Z)13# ref: https://github.com/ggml-org/ggml/discussions/157914option(LLAMA_BUILD_IS_DEV "llama: dev build" ON)15 16if (LLAMA_BUILD_IS_DEV)17 set(LLAMA_VERSION "${LLAMA_VERSION_BASE}-dev")18else()19 # TODO: check that the current commit is tagged correctly according to the version specified above20 set(LLAMA_VERSION "${LLAMA_VERSION_BASE}")21endif()22 23message(STATUS "llama.cpp version: ${LLAMA_VERSION}")24 25#set(CMAKE_WARN_DEPRECATED YES)26set(CMAKE_WARN_UNUSED_CLI YES)27 28set(CMAKE_EXPORT_COMPILE_COMMANDS ON)29 30if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE)31 set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)32 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")33endif()34 35message("CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")36 37# Add path to modules38list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")39 40set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)41set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)42 43if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)44 set(LLAMA_STANDALONE ON)45 46 include(git-vars)47else()48 set(LLAMA_STANDALONE OFF)49endif()50 51option(LLAMA_USE_SYSTEM_GGML "Use system libggml" OFF)52 53option(LLAMA_WASM_MEM64 "llama: use 64-bit memory in WASM builds" ON)54 55if (EMSCRIPTEN)56 set(BUILD_SHARED_LIBS_DEFAULT OFF)57 58 # Use 64-bit memory to support backend_get_memory queries59 # TODO: analyze performance impact, see https://spidermonkey.dev/blog/2025/01/15/is-memory64-actually-worth-using60 if (LLAMA_WASM_MEM64)61 add_compile_options("-sMEMORY64=1")62 add_link_options("-sMEMORY64=1")63 endif()64 add_link_options("-sALLOW_MEMORY_GROWTH=1")65 66 option(LLAMA_WASM_SINGLE_FILE "llama: embed WASM inside the generated llama.js" OFF)67 option(LLAMA_BUILD_HTML "llama: build HTML file" ON)68 if (LLAMA_BUILD_HTML)69 set(CMAKE_EXECUTABLE_SUFFIX ".html")70 endif()71else()72 if (MINGW)73 set(BUILD_SHARED_LIBS_DEFAULT OFF)74 else()75 set(BUILD_SHARED_LIBS_DEFAULT ON)76 endif()77endif()78 79option(BUILD_SHARED_LIBS "build shared libraries" ${BUILD_SHARED_LIBS_DEFAULT})80 81if (WIN32)82 add_compile_definitions(_CRT_SECURE_NO_WARNINGS)83endif()84 85if (MSVC)86 add_compile_options("$<$<COMPILE_LANGUAGE:C>:/utf-8>")87 add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/utf-8>")88 add_compile_options("$<$<COMPILE_LANGUAGE:C>:/bigobj>")89 add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/bigobj>")90endif()91 92if (LLAMA_STANDALONE)93 # enable parallel builds for msbuild94 list(APPEND CMAKE_VS_GLOBALS UseMultiToolTask=true)95 list(APPEND CMAKE_VS_GLOBALS EnforceProcessCountAcrossBuilds=true)96endif()97 98if (CMAKE_SYSTEM_NAME STREQUAL "iOS")99 set(LLAMA_TOOLS_INSTALL_DEFAULT OFF)100else()101 set(LLAMA_TOOLS_INSTALL_DEFAULT ${LLAMA_STANDALONE})102endif()103 104# subprocess spawning isn't a supported/sandbox-friendly operation on mobile OSes or in WASM105if (CMAKE_SYSTEM_NAME STREQUAL "iOS" OR CMAKE_SYSTEM_NAME STREQUAL "Android" OR ANDROID106 OR CMAKE_SYSTEM_NAME STREQUAL "Emscripten" OR EMSCRIPTEN)107 set(LLAMA_SUBPROCESS_DEFAULT OFF)108else()109 set(LLAMA_SUBPROCESS_DEFAULT ON)110endif()111 112#113# option list114#115 116# debug117option(LLAMA_ALL_WARNINGS "llama: enable all compiler warnings" ON)118option(LLAMA_ALL_WARNINGS_3RD_PARTY "llama: enable all compiler warnings in 3rd party libs" OFF)119 120# build121option(LLAMA_FATAL_WARNINGS "llama: enable -Werror flag" OFF)122 123# sanitizers124option(LLAMA_SANITIZE_THREAD "llama: enable thread sanitizer" OFF)125option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer" OFF)126option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF)127 128# utils129option(LLAMA_BUILD_COMMON "llama: build common utils library" ${LLAMA_STANDALONE})130 131# extra artifacts132option(LLAMA_BUILD_TESTS "llama: build tests" ${LLAMA_STANDALONE})133option(LLAMA_BUILD_TOOLS "llama: build tools" ${LLAMA_STANDALONE})134option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})135option(LLAMA_BUILD_SERVER "llama: build server example" ${LLAMA_STANDALONE})136option(LLAMA_BUILD_APP "llama: build the unified binary" ${LLAMA_STANDALONE})137option(LLAMA_BUILD_UI "llama: build the embedded Web UI for server" OFF)138option(LLAMA_USE_PREBUILT_UI "llama: use prebuilt UI from HF Bucket when available" ON)139 140option(LLAMA_TOOLS_INSTALL "llama: install tools" ${LLAMA_TOOLS_INSTALL_DEFAULT})141option(LLAMA_TESTS_INSTALL "llama: install tests" ON)142 143# 3rd party libs144option(LLAMA_OPENSSL "llama: use openssl to support HTTPS" ON)145option(LLAMA_SUBPROCESS "llama-common: use subprocess, required by server tools and server router mode" ${LLAMA_SUBPROCESS_DEFAULT})146option(LLAMA_LLGUIDANCE "llama-common: include LLGuidance library for structured output in common utils" OFF)147 148 149# Required for relocatable CMake package150include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/build-info.cmake)151include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/common.cmake)152 153if (NOT DEFINED LLAMA_BUILD_NUMBER)154 set(LLAMA_BUILD_NUMBER ${BUILD_NUMBER})155endif()156if (NOT DEFINED LLAMA_BUILD_COMMIT)157 set(LLAMA_BUILD_COMMIT ${BUILD_COMMIT})158endif()159 160# override ggml options161set(GGML_ALL_WARNINGS ${LLAMA_ALL_WARNINGS})162set(GGML_FATAL_WARNINGS ${LLAMA_FATAL_WARNINGS})163 164# change the default for these ggml options165if (NOT DEFINED GGML_LLAMAFILE)166 set(GGML_LLAMAFILE_DEFAULT ON)167endif()168 169if (NOT DEFINED GGML_CUDA_GRAPHS)170 set(GGML_CUDA_GRAPHS_DEFAULT ON)171endif()172 173# transition helpers174function (llama_option_depr TYPE OLD)175 if (${OLD})176 set(NEW "${ARGV2}")177 if(NEW)178 message(${TYPE} "${OLD} is deprecated, use ${NEW} instead")179 set(${NEW} ON PARENT_SCOPE)180 else()181 message(${TYPE} "${OLD} is deprecated and will be ignored")182 endif()183 endif()184endfunction()185 186llama_option_depr(FATAL_ERROR LLAMA_CUBLAS GGML_CUDA)187llama_option_depr(WARNING LLAMA_CUDA GGML_CUDA)188llama_option_depr(WARNING LLAMA_METAL GGML_METAL)189llama_option_depr(WARNING LLAMA_METAL_EMBED_LIBRARY GGML_METAL_EMBED_LIBRARY)190llama_option_depr(WARNING LLAMA_NATIVE GGML_NATIVE)191llama_option_depr(WARNING LLAMA_RPC GGML_RPC)192llama_option_depr(WARNING LLAMA_SYCL GGML_SYCL)193llama_option_depr(WARNING LLAMA_SYCL_F16 GGML_SYCL_F16)194llama_option_depr(WARNING LLAMA_CANN GGML_CANN)195llama_option_depr(WARNING LLAMA_CURL)196 197include("cmake/license.cmake")198license_add_file("llama.cpp" "LICENSE")199 200#201# compile options202#203 204# clang stores the modification time of the precompiled header sources inside the205# header and rejects it when they differ, so the timestamp is left out of it206add_compile_options(207 "$<$<COMPILE_LANG_AND_ID:C,Clang,IntelLLVM>:SHELL:-Xclang -fno-pch-timestamp>"208 "$<$<COMPILE_LANG_AND_ID:CXX,Clang,IntelLLVM>:SHELL:-Xclang -fno-pch-timestamp>")209 210#211# 3rd-party212#213 214if (LLAMA_USE_SYSTEM_GGML)215 message(STATUS "Using system-provided libggml, skipping ggml build")216 find_package(ggml REQUIRED)217 add_library(ggml ALIAS ggml::ggml)218endif()219 220if (NOT TARGET ggml AND NOT LLAMA_USE_SYSTEM_GGML)221 set(GGML_BUILD_NUMBER ${LLAMA_BUILD_NUMBER})222 set(GGML_BUILD_COMMIT ${LLAMA_BUILD_COMMIT})223 add_subdirectory(ggml)224 # ... otherwise assume ggml is added by a parent CMakeLists.txt225endif()226 227#228# build the library229#230 231add_subdirectory(src)232 233#234# utils, programs, examples and tests235#236 237add_subdirectory(vendor)238 239if (LLAMA_BUILD_COMMON)240 add_subdirectory(common)241endif()242 243if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)244 include(CTest)245 add_subdirectory(tests)246endif()247 248if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_EXAMPLES)249 add_subdirectory(examples)250 add_subdirectory(pocs)251endif()252 253if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TOOLS)254 add_subdirectory(tools)255endif()256 257if (LLAMA_BUILD_APP)258 add_subdirectory(app)259endif()260 261# Standalone libmtmd build without pulling in the rest of the tools/ tree.262# Useful when packaging just the mtmd library for language bindings (e.g. an263# Apple XCFramework, or a WASM build). When the full tools build is enabled,264# mtmd is already built by the tools/ subdirectory above; this hook only fires265# when LLAMA_BUILD_TOOLS is OFF to avoid double-adding the target.266option(LLAMA_BUILD_MTMD "llama: build tools/mtmd library standalone" OFF)267if (LLAMA_BUILD_MTMD AND NOT (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TOOLS))268 add_subdirectory(tools/mtmd)269endif()270 271#272# install273#274 275include(GNUInstallDirs)276include(CMakePackageConfigHelpers)277 278set(LLAMA_INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Location of header files")279set(LLAMA_LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Location of library files")280set(LLAMA_BIN_INSTALL_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Location of binary files")281 282set(LLAMA_PUBLIC_HEADERS283 ${CMAKE_CURRENT_SOURCE_DIR}/include/llama.h284 ${CMAKE_CURRENT_SOURCE_DIR}/include/llama-cpp.h)285 286set_target_properties(llama287 PROPERTIES288 PUBLIC_HEADER "${LLAMA_PUBLIC_HEADERS}")289 290install(TARGETS llama LIBRARY PUBLIC_HEADER)291 292if (LLAMA_BUILD_COMMON)293 install(TARGETS llama-common LIBRARY)294endif()295 296configure_package_config_file(297 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/llama-config.cmake.in298 ${CMAKE_CURRENT_BINARY_DIR}/llama-config.cmake299 INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/llama300 PATH_VARS LLAMA_INCLUDE_INSTALL_DIR301 LLAMA_LIB_INSTALL_DIR302 LLAMA_BIN_INSTALL_DIR )303 304write_basic_package_version_file(305 ${CMAKE_CURRENT_BINARY_DIR}/llama-config-version.cmake306 VERSION ${LLAMA_VERSION}307 COMPATIBILITY SameMajorVersion)308 309install(FILES ${CMAKE_CURRENT_BINARY_DIR}/llama-config.cmake310 ${CMAKE_CURRENT_BINARY_DIR}/llama-config-version.cmake311 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/llama)312 313configure_file(cmake/llama.pc.in314 "${CMAKE_CURRENT_BINARY_DIR}/llama.pc"315 @ONLY)316 317install(FILES "${CMAKE_CURRENT_BINARY_DIR}/llama.pc"318 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)319 