Skip to content

Commit 78010a0

Browse files
angtngxson
andauthored
cmake : move OpenSSL linking to vendor/cpp-httplib (#17177)
* cmake : move OpenSSL linking to vendor/cpp-httplib Signed-off-by: Adrien Gallouët <angt@huggingface.co> * bring back httplib 0.27.0 * add -DLLAMA_HTTPLIB * update cmake config for visionos --------- Signed-off-by: Adrien Gallouët <angt@huggingface.co> Co-authored-by: Xuan Son Nguyen <son@huggingface.co>
1 parent 655cddd commit 78010a0

File tree

9 files changed

+2866
-1292
lines changed

9 files changed

+2866
-1292
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ option(LLAMA_TOOLS_INSTALL "llama: install tools" ${LLAMA_TOOLS_INSTALL_
9292

9393
# 3rd party libs
9494
option(LLAMA_CURL "llama: use libcurl to download model from an URL" ON)
95+
option(LLAMA_HTTPLIB "llama: if libcurl is disabled, use httplib to download model from an URL" ON)
9596
option(LLAMA_OPENSSL "llama: use openssl to support HTTPS" OFF)
9697
option(LLAMA_LLGUIDANCE "llama-common: include LLGuidance library for structured output in common utils" OFF)
9798

@@ -200,7 +201,9 @@ endif()
200201

201202
if (LLAMA_BUILD_COMMON)
202203
add_subdirectory(common)
203-
add_subdirectory(vendor/cpp-httplib)
204+
if (LLAMA_HTTPLIB)
205+
add_subdirectory(vendor/cpp-httplib)
206+
endif()
204207
endif()
205208

206209
if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)

build-xcframework.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ cmake -B build-visionos -G Xcode \
454454
-DCMAKE_C_FLAGS="-D_XOPEN_SOURCE=700 ${COMMON_C_FLAGS}" \
455455
-DCMAKE_CXX_FLAGS="-D_XOPEN_SOURCE=700 ${COMMON_CXX_FLAGS}" \
456456
-DLLAMA_CURL=OFF \
457+
-DLLAMA_HTTPLIB=OFF \
458+
-DLLAMA_BUILD_SERVER=OFF \
457459
-S .
458460
cmake --build build-visionos --config Release -- -quiet
459461

@@ -468,6 +470,8 @@ cmake -B build-visionos-sim -G Xcode \
468470
-DCMAKE_C_FLAGS="-D_XOPEN_SOURCE=700 ${COMMON_C_FLAGS}" \
469471
-DCMAKE_CXX_FLAGS="-D_XOPEN_SOURCE=700 ${COMMON_CXX_FLAGS}" \
470472
-DLLAMA_CURL=OFF \
473+
-DLLAMA_HTTPLIB=OFF \
474+
-DLLAMA_BUILD_SERVER=OFF \
471475
-S .
472476
cmake --build build-visionos-sim --config Release -- -quiet
473477

common/CMakeLists.txt

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -91,47 +91,13 @@ if (LLAMA_CURL)
9191
target_compile_definitions(${TARGET} PUBLIC LLAMA_USE_CURL)
9292
include_directories(${CURL_INCLUDE_DIRS})
9393
set(LLAMA_COMMON_EXTRA_LIBS ${LLAMA_COMMON_EXTRA_LIBS} ${CURL_LIBRARIES})
94-
else()
94+
elseif (LLAMA_HTTPLIB)
9595
# otherwise, use cpp-httplib
96+
message(FATAL "test")
97+
target_compile_definitions(${TARGET} PUBLIC LLAMA_USE_HTTPLIB)
9698
set(LLAMA_COMMON_EXTRA_LIBS ${LLAMA_COMMON_EXTRA_LIBS} cpp-httplib)
9799
endif()
98100

99-
if (LLAMA_OPENSSL)
100-
find_package(OpenSSL)
101-
if (OpenSSL_FOUND)
102-
include(CheckCSourceCompiles)
103-
set(SAVED_CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES})
104-
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
105-
check_c_source_compiles("
106-
#include <openssl/opensslv.h>
107-
#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
108-
# if OPENSSL_VERSION_NUMBER < 0x1010107f
109-
# error bad version
110-
# endif
111-
#else
112-
# if OPENSSL_VERSION_NUMBER < 0x30000000L
113-
# error bad version
114-
# endif
115-
#endif
116-
int main() { return 0; }
117-
" OPENSSL_VERSION_SUPPORTED)
118-
set(CMAKE_REQUIRED_INCLUDES ${SAVED_CMAKE_REQUIRED_INCLUDES})
119-
if (OPENSSL_VERSION_SUPPORTED)
120-
message(STATUS "OpenSSL found: ${OPENSSL_VERSION}")
121-
target_compile_definitions(${TARGET} PUBLIC CPPHTTPLIB_OPENSSL_SUPPORT)
122-
target_link_libraries(${TARGET} PUBLIC OpenSSL::SSL OpenSSL::Crypto)
123-
if (APPLE AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
124-
target_compile_definitions(${TARGET} PUBLIC CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
125-
find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation REQUIRED)
126-
find_library(SECURITY_FRAMEWORK Security REQUIRED)
127-
target_link_libraries(${TARGET} PUBLIC ${CORE_FOUNDATION_FRAMEWORK} ${SECURITY_FRAMEWORK})
128-
endif()
129-
endif()
130-
else()
131-
message(STATUS "OpenSSL not found, SSL support disabled")
132-
endif()
133-
endif()
134-
135101
if (LLAMA_LLGUIDANCE)
136102
include(ExternalProject)
137103
set(LLGUIDANCE_SRC ${CMAKE_BINARY_DIR}/llguidance/source)

common/download.cpp

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#if defined(LLAMA_USE_CURL)
2121
#include <curl/curl.h>
2222
#include <curl/easy.h>
23-
#else
23+
#elif defined(LLAMA_USE_HTTPLIB)
2424
#include "http.h"
2525
#endif
2626

@@ -467,7 +467,7 @@ std::pair<long, std::vector<char>> common_remote_get_content(const std::string &
467467
return { res_code, std::move(res_buffer) };
468468
}
469469

470-
#else
470+
#elif defined(LLAMA_USE_HTTPLIB)
471471

472472
static bool is_output_a_tty() {
473473
#if defined(_WIN32)
@@ -713,6 +713,8 @@ std::pair<long, std::vector<char>> common_remote_get_content(const std::string
713713

714714
#endif // LLAMA_USE_CURL
715715

716+
#if defined(LLAMA_USE_CURL) || defined(LLAMA_USE_HTTPLIB)
717+
716718
static bool common_download_file_single(const std::string & url,
717719
const std::string & path,
718720
const std::string & bearer_token,
@@ -907,33 +909,6 @@ common_hf_file_res common_get_hf_file(const std::string & hf_repo_with_tag, cons
907909
return { hf_repo, ggufFile, mmprojFile };
908910
}
909911

910-
std::vector<common_cached_model_info> common_list_cached_models() {
911-
std::vector<common_cached_model_info> models;
912-
const std::string cache_dir = fs_get_cache_directory();
913-
const std::vector<common_file_info> files = fs_list_files(cache_dir);
914-
for (const auto & file : files) {
915-
if (string_starts_with(file.name, "manifest=") && string_ends_with(file.name, ".json")) {
916-
common_cached_model_info model_info;
917-
model_info.manifest_path = file.path;
918-
std::string fname = file.name;
919-
string_replace_all(fname, ".json", ""); // remove extension
920-
auto parts = string_split<std::string>(fname, '=');
921-
if (parts.size() == 4) {
922-
// expect format: manifest=<user>=<model>=<tag>=<other>
923-
model_info.user = parts[1];
924-
model_info.model = parts[2];
925-
model_info.tag = parts[3];
926-
} else {
927-
// invalid format
928-
continue;
929-
}
930-
model_info.size = 0; // TODO: get GGUF size, not manifest size
931-
models.push_back(model_info);
932-
}
933-
}
934-
return models;
935-
}
936-
937912
//
938913
// Docker registry functions
939914
//
@@ -1052,3 +1027,46 @@ std::string common_docker_resolve_model(const std::string & docker) {
10521027
throw;
10531028
}
10541029
}
1030+
1031+
#else
1032+
1033+
common_hf_file_res common_get_hf_file(const std::string &, const std::string &, bool) {
1034+
throw std::runtime_error("download functionality is not enabled in this build");
1035+
}
1036+
1037+
bool common_download_model(const common_params_model &, const std::string &, bool) {
1038+
throw std::runtime_error("download functionality is not enabled in this build");
1039+
}
1040+
1041+
std::string common_docker_resolve_model(const std::string &) {
1042+
throw std::runtime_error("download functionality is not enabled in this build");
1043+
}
1044+
1045+
#endif // LLAMA_USE_CURL || LLAMA_USE_HTTPLIB
1046+
1047+
std::vector<common_cached_model_info> common_list_cached_models() {
1048+
std::vector<common_cached_model_info> models;
1049+
const std::string cache_dir = fs_get_cache_directory();
1050+
const std::vector<common_file_info> files = fs_list_files(cache_dir);
1051+
for (const auto & file : files) {
1052+
if (string_starts_with(file.name, "manifest=") && string_ends_with(file.name, ".json")) {
1053+
common_cached_model_info model_info;
1054+
model_info.manifest_path = file.path;
1055+
std::string fname = file.name;
1056+
string_replace_all(fname, ".json", ""); // remove extension
1057+
auto parts = string_split<std::string>(fname, '=');
1058+
if (parts.size() == 4) {
1059+
// expect format: manifest=<user>=<model>=<tag>=<other>
1060+
model_info.user = parts[1];
1061+
model_info.model = parts[2];
1062+
model_info.tag = parts[3];
1063+
} else {
1064+
// invalid format
1065+
continue;
1066+
}
1067+
model_info.size = 0; // TODO: get GGUF size, not manifest size
1068+
models.push_back(model_info);
1069+
}
1070+
}
1071+
return models;
1072+
}

scripts/sync_vendor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"https://github.com/mackron/miniaudio/raw/refs/tags/0.11.22/miniaudio.h": "vendor/miniaudio/miniaudio.h",
1616

17-
"https://raw.githubusercontent.com/yhirose/cpp-httplib/refs/tags/v0.20.1/httplib.h": "vendor/cpp-httplib/httplib.h",
17+
"https://raw.githubusercontent.com/yhirose/cpp-httplib/refs/tags/v0.27.0/httplib.h": "vendor/cpp-httplib/httplib.h",
1818
}
1919

2020
for url, filename in vendor.items():

tools/server/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ if (MINGW)
77
add_compile_definitions(_WIN32_WINNT=${GGML_WIN_VER})
88
endif()
99

10+
if (NOT LLAMA_HTTPLIB)
11+
message(FATAL_ERROR "LLAMA_HTTPLIB is OFF, cannot build llama-server. Hint: to skip building server, set -DLLAMA_BUILD_SERVER=OFF")
12+
endif()
13+
1014
set(TARGET_SRCS
1115
server.cpp
1216
utils.hpp

vendor/cpp-httplib/CMakeLists.txt

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,39 @@ target_compile_definitions(${TARGET} PRIVATE
2222
CPPHTTPLIB_TCP_NODELAY=1
2323
)
2424

25-
if (${CMAKE_SYSTEM_NAME} MATCHES "visionOS")
26-
# quick fix for https://github.com/ggml-org/llama.cpp/actions/runs/19247291428/job/55024294176?pr=17150
27-
target_compile_definitions(${TARGET} PRIVATE NI_MAXHOST=1025)
25+
if (LLAMA_OPENSSL)
26+
find_package(OpenSSL)
27+
if (OpenSSL_FOUND)
28+
include(CheckCSourceCompiles)
29+
set(SAVED_CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES})
30+
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
31+
check_c_source_compiles("
32+
#include <openssl/opensslv.h>
33+
#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
34+
# if OPENSSL_VERSION_NUMBER < 0x1010107f
35+
# error bad version
36+
# endif
37+
#else
38+
# if OPENSSL_VERSION_NUMBER < 0x30000000L
39+
# error bad version
40+
# endif
41+
#endif
42+
int main() { return 0; }
43+
" OPENSSL_VERSION_SUPPORTED)
44+
set(CMAKE_REQUIRED_INCLUDES ${SAVED_CMAKE_REQUIRED_INCLUDES})
45+
if (OPENSSL_VERSION_SUPPORTED)
46+
message(STATUS "OpenSSL found: ${OPENSSL_VERSION}")
47+
target_compile_definitions(${TARGET} PUBLIC CPPHTTPLIB_OPENSSL_SUPPORT)
48+
target_link_libraries(${TARGET} PUBLIC OpenSSL::SSL OpenSSL::Crypto)
49+
if (APPLE AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
50+
target_compile_definitions(${TARGET} PUBLIC CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
51+
find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation REQUIRED)
52+
find_library(SECURITY_FRAMEWORK Security REQUIRED)
53+
target_link_libraries(${TARGET} PUBLIC ${CORE_FOUNDATION_FRAMEWORK} ${SECURITY_FRAMEWORK})
54+
endif()
55+
endif()
56+
else()
57+
message(STATUS "OpenSSL not found, SSL support disabled")
58+
endif()
2859
endif()
60+

0 commit comments

Comments
 (0)