Skip to content

Commit 1d45b42

Browse files
authored
vendor: split httplib to cpp/h files (#17150)
* vendor: split httplib to cpp/h files * move defines * include httplib if curl is not used * add TODO * fix build ios * fix build visionos instead
1 parent ca48440 commit 1d45b42

File tree

8 files changed

+8277
-9779
lines changed

8 files changed

+8277
-9779
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ endif()
200200

201201
if (LLAMA_BUILD_COMMON)
202202
add_subdirectory(common)
203+
add_subdirectory(vendor/cpp-httplib)
203204
endif()
204205

205206
if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)

common/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,21 @@ if (BUILD_SHARED_LIBS)
7979
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
8080
endif()
8181

82+
# TODO: use list(APPEND LLAMA_COMMON_EXTRA_LIBS ...)
8283
set(LLAMA_COMMON_EXTRA_LIBS build_info)
8384

84-
# Use curl to download model url
8585
if (LLAMA_CURL)
86+
# Use curl to download model url
8687
find_package(CURL)
8788
if (NOT CURL_FOUND)
8889
message(FATAL_ERROR "Could NOT find CURL. Hint: to disable this feature, set -DLLAMA_CURL=OFF")
8990
endif()
9091
target_compile_definitions(${TARGET} PUBLIC LLAMA_USE_CURL)
9192
include_directories(${CURL_INCLUDE_DIRS})
9293
set(LLAMA_COMMON_EXTRA_LIBS ${LLAMA_COMMON_EXTRA_LIBS} ${CURL_LIBRARIES})
94+
else()
95+
# otherwise, use cpp-httplib
96+
set(LLAMA_COMMON_EXTRA_LIBS ${LLAMA_COMMON_EXTRA_LIBS} cpp-httplib)
9397
endif()
9498

9599
if (LLAMA_OPENSSL)

scripts/sync_vendor.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,20 @@
2020
for url, filename in vendor.items():
2121
print(f"downloading {url} to {filename}") # noqa: NP100
2222
urllib.request.urlretrieve(url, filename)
23+
24+
# split cpp/h files for httplib
25+
# see: https://github.com/yhirose/cpp-httplib/blob/master/split.py
26+
if 'httplib.h' in filename:
27+
border = '// ----------------------------------------------------------------------------'
28+
with open(filename, 'r') as f:
29+
content = f.read()
30+
header, implementation, footer = content.split(border, 2)
31+
fname_cpp = filename.replace('.h', '.cpp')
32+
with open(filename, 'w') as fh:
33+
fh.write(header)
34+
fh.write(footer)
35+
with open(fname_cpp, 'w') as fc:
36+
fc.write('#include "httplib.h"\n')
37+
fc.write('namespace httplib {\n')
38+
fc.write(implementation.replace('\ninline ', '\n'))
39+
fc.write('} // namespace httplib\n')

tools/server/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ install(TARGETS ${TARGET} RUNTIME)
3333

3434
target_include_directories(${TARGET} PRIVATE ../mtmd)
3535
target_include_directories(${TARGET} PRIVATE ${CMAKE_SOURCE_DIR})
36-
target_link_libraries(${TARGET} PRIVATE common mtmd ${CMAKE_THREAD_LIBS_INIT})
36+
target_link_libraries(${TARGET} PRIVATE common mtmd cpp-httplib ${CMAKE_THREAD_LIBS_INIT})
3737

3838
if (WIN32)
3939
TARGET_LINK_LIBRARIES(${TARGET} PRIVATE ws2_32)

tools/server/utils.hpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
#include "mtmd-helper.h"
1010
#include "chat.h"
1111

12-
// increase max payload length to allow use of larger context size
13-
#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 1048576
14-
// increase backlog size to avoid connection resets for >> 1 slots
15-
#define CPPHTTPLIB_LISTEN_BACKLOG 512
16-
// increase max URI length to handle longer prompts in query string
17-
#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 32768
18-
// disable Nagle's algorithm
19-
#define CPPHTTPLIB_TCP_NODELAY true
2012
#include <cpp-httplib/httplib.h>
2113

2214
#define JSON_ASSERT GGML_ASSERT

vendor/cpp-httplib/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
set(TARGET cpp-httplib)
2+
3+
find_package(Threads REQUIRED)
4+
5+
add_library(${TARGET} STATIC httplib.cpp httplib.h)
6+
if (NOT MSVC)
7+
# disable warnings in 3rd party code
8+
target_compile_options(${TARGET} PRIVATE -w)
9+
endif()
10+
11+
target_link_libraries (${TARGET} PRIVATE Threads::Threads)
12+
target_compile_features(${TARGET} PRIVATE cxx_std_17)
13+
14+
target_compile_definitions(${TARGET} PRIVATE
15+
# increase max payload length to allow use of larger context size
16+
CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH=1048576
17+
# increase backlog size to avoid connection resets for >> 1 slots
18+
CPPHTTPLIB_LISTEN_BACKLOG=512
19+
# increase max URI length to handle longer prompts in query string
20+
CPPHTTPLIB_REQUEST_URI_MAX_LENGTH=32768
21+
# disable Nagle's algorithm
22+
CPPHTTPLIB_TCP_NODELAY=1
23+
)
24+
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)
28+
endif()

0 commit comments

Comments
 (0)