From ea23c15990ea7b09cf501ef103020f05fc480511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Sat, 10 Jan 2026 09:46:24 +0100 Subject: [PATCH] common : add --license to display embedded licenses (#18696) This commit introduces a mechanism to embed all licenses directly into the compiled binaries. This eliminates the need to distribute separate LICENSE files alongside the executable, making the binaries self-contained and simplifying deployment. --- CMakeLists.txt | 16 ++++++++ cmake/license.cmake | 40 +++++++++++++++++++ common/CMakeLists.txt | 24 ----------- common/arg.cpp | 14 ++++++- licenses/LICENSE-curl | 21 ++++++++-- scripts/sync_vendor.py | 1 + vendor/cpp-httplib/CMakeLists.txt | 7 +++- .../cpp-httplib/LICENSE | 1 + 8 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 cmake/license.cmake rename licenses/LICENSE-httplib => vendor/cpp-httplib/LICENSE (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c231ec0e3f..44c2166210 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,6 +182,9 @@ if (NOT MSVC) endif() endif() +include("cmake/license.cmake") +license_add_file("llama.cpp" "LICENSE") + # # 3rd-party # @@ -235,6 +238,19 @@ if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TOOLS) add_subdirectory(tools) endif() +# 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) + license_generate(common) +endif() + # # install # diff --git a/cmake/license.cmake b/cmake/license.cmake new file mode 100644 index 0000000000..de066603ba --- /dev/null +++ b/cmake/license.cmake @@ -0,0 +1,40 @@ +define_property(GLOBAL PROPERTY LICENSE_TEXT + BRIEF_DOCS "Embedded licenses" + FULL_DOCS "Global string containing all aggregated licenses" +) + +function(license_add_file NAME FILE) + if(NOT IS_ABSOLUTE "${FILE}") + set(FILE "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}") + endif() + if(EXISTS "${FILE}") + set(TITLE "License for ${NAME}") + string(REGEX REPLACE "." "=" UNDERLINE "${TITLE}") + file(READ "${FILE}" TEXT) + get_property(TMP GLOBAL PROPERTY LICENSE_TEXT) + string(APPEND TMP "R\"=L=(${TITLE}\n${UNDERLINE}\n\n${TEXT})=L=\",\n") + set_property(GLOBAL PROPERTY LICENSE_TEXT "${TMP}") + else() + message(WARNING "License file '${FILE}' not found") + endif() +endfunction() + +function(license_generate TARGET_NAME) + message(STATUS "Generating embedded license file for target: ${TARGET_NAME}") + get_property(TEXT GLOBAL PROPERTY LICENSE_TEXT) + + set(CPP_CONTENT "// Generated by CMake\n\n") + string(APPEND CPP_CONTENT "const char* LICENSES[] = {\n") + string(APPEND CPP_CONTENT "${TEXT}") + string(APPEND CPP_CONTENT "nullptr\n") + string(APPEND CPP_CONTENT "};\n") + + set(CPP_FILE "${CMAKE_BINARY_DIR}/license.cpp") + file(WRITE "${CPP_FILE}" "${CPP_CONTENT}") + + if(TARGET ${TARGET_NAME}) + target_sources(${TARGET_NAME} PRIVATE "${CPP_FILE}") + else() + message(FATAL_ERROR "Target '${TARGET_NAME}' does not exist") + endif() +endfunction() diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index f7b99159e3..55222bdf61 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -155,27 +155,3 @@ if (LLAMA_LLGUIDANCE) endif () target_link_libraries(${TARGET} PRIVATE ${LLAMA_COMMON_EXTRA_LIBS} PUBLIC llama Threads::Threads) - - -# -# copy the license files -# - -# Check if running in GitHub Actions -if (DEFINED ENV{GITHUB_ACTIONS} AND "$ENV{GITHUB_ACTIONS}" STREQUAL "true") - message(STATUS "Running inside GitHub Actions - copying license files") - - # Copy all files from licenses/ to build/bin/ - file(GLOB LICENSE_FILES "${CMAKE_SOURCE_DIR}/licenses/*") - foreach(LICENSE_FILE ${LICENSE_FILES}) - get_filename_component(FILENAME ${LICENSE_FILE} NAME) - add_custom_command( - POST_BUILD - TARGET ${TARGET} - COMMAND ${CMAKE_COMMAND} -E copy_if_different - "${LICENSE_FILE}" - "$/${FILENAME}" - COMMENT "Copying ${FILENAME} to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") - message(STATUS "Copying ${LICENSE_FILE} to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${FILENAME}") - endforeach() -endif() diff --git a/common/arg.cpp b/common/arg.cpp index 72750a3cba..869ec545e6 100644 --- a/common/arg.cpp +++ b/common/arg.cpp @@ -2,10 +2,10 @@ #include "chat.h" #include "common.h" +#include "download.h" #include "json-schema-to-grammar.h" #include "log.h" #include "sampling.h" -#include "download.h" #include "preset.h" // fix problem with std::min and std::max @@ -48,6 +48,8 @@ #define LLAMA_MAX_URL_LENGTH 2084 // Maximum URL Length in Chrome: 2083 +extern const char * LICENSES[]; + using json = nlohmann::ordered_json; using namespace common_arg_utils; @@ -1030,6 +1032,16 @@ common_params_context common_params_parser_init(common_params & params, llama_ex exit(0); } )); + add_opt(common_arg( + {"--license"}, + "show source code license and dependencies", + [](common_params &) { + for (int i = 0; LICENSES[i]; ++i) { + printf("%s\n", LICENSES[i]); + } + exit(0); + } + )); add_opt(common_arg( {"-cl", "--cache-list"}, "show list of models in cache", diff --git a/licenses/LICENSE-curl b/licenses/LICENSE-curl index da9c038253..2f71d999a9 100644 --- a/licenses/LICENSE-curl +++ b/licenses/LICENSE-curl @@ -1,9 +1,22 @@ -Copyright (c) 1996 - 2025, Daniel Stenberg, daniel@haxx.se, and many contributors, see the THANKS file. +COPYRIGHT AND PERMISSION NOTICE + +Copyright (c) 1996 - 2026, Daniel Stenberg, , and many +contributors, see the THANKS file. All rights reserved. -Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. +Permission to use, copy, modify, and distribute this software for any purpose +with or without fee is hereby granted, provided that the above copyright +notice and this permission notice appear in all copies. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN +NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +OR OTHER DEALINGS IN THE SOFTWARE. -Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization of the copyright holder. +Except as contained in this notice, the name of a copyright holder shall not +be used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization of the copyright holder. diff --git a/scripts/sync_vendor.py b/scripts/sync_vendor.py index ed6bf1bf4e..95377b29f7 100755 --- a/scripts/sync_vendor.py +++ b/scripts/sync_vendor.py @@ -17,6 +17,7 @@ vendor = { "https://github.com/mackron/miniaudio/raw/669ed3e844524fcd883231b13095baee9f6de304/miniaudio.h": "vendor/miniaudio/miniaudio.h", "https://raw.githubusercontent.com/yhirose/cpp-httplib/refs/tags/v0.30.0/httplib.h": "vendor/cpp-httplib/httplib.h", + "https://raw.githubusercontent.com/yhirose/cpp-httplib/refs/tags/v0.30.0/LICENSE": "vendor/cpp-httplib/LICENSE", "https://raw.githubusercontent.com/sheredom/subprocess.h/b49c56e9fe214488493021017bf3954b91c7c1f5/subprocess.h": "vendor/sheredom/subprocess.h", } diff --git a/vendor/cpp-httplib/CMakeLists.txt b/vendor/cpp-httplib/CMakeLists.txt index 8f0d15d1fd..172b925453 100644 --- a/vendor/cpp-httplib/CMakeLists.txt +++ b/vendor/cpp-httplib/CMakeLists.txt @@ -1,4 +1,5 @@ set(TARGET cpp-httplib) +license_add_file("cpp-httplib" "LICENSE") find_package(Threads REQUIRED) @@ -8,7 +9,7 @@ if (NOT MSVC) target_compile_options(${TARGET} PRIVATE -w) endif() -target_link_libraries (${TARGET} PRIVATE Threads::Threads) +target_link_libraries(${TARGET} PRIVATE Threads::Threads) if (WIN32 AND NOT MSVC) target_link_libraries(${TARGET} PRIVATE ws2_32) @@ -67,6 +68,8 @@ if (LLAMA_BUILD_BORINGSSL) set(BUILD_SHARED_LIBS ${SAVED_BUILD_SHARED_LIBS}) set(BUILD_TESTING ${SAVED_BUILD_TESTING}) + license_add_file("BoringSSL" "${boringssl_SOURCE_DIR}/LICENSE") + set(CPPHTTPLIB_OPENSSL_SUPPORT TRUE) target_link_libraries(${TARGET} PUBLIC ssl crypto) @@ -108,6 +111,8 @@ elseif (LLAMA_BUILD_LIBRESSL) set(BUILD_SHARED_LIBS ${SAVED_BUILD_SHARED_LIBS}) set(BUILD_TESTING ${SAVED_BUILD_TESTING}) + license_add_file("LibreSSL" "${libressl_SOURCE_DIR}/COPYING") + set(CPPHTTPLIB_OPENSSL_SUPPORT TRUE) target_link_libraries(${TARGET} PUBLIC ssl crypto) diff --git a/licenses/LICENSE-httplib b/vendor/cpp-httplib/LICENSE similarity index 99% rename from licenses/LICENSE-httplib rename to vendor/cpp-httplib/LICENSE index 47c418e072..3e5ed359a2 100644 --- a/licenses/LICENSE-httplib +++ b/vendor/cpp-httplib/LICENSE @@ -19,3 +19,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +