Skip to content

Commit 3de9115

Browse files
committed
feat: add curl build infrastructure for remote embedding support
1 parent 1a2ece6 commit 3de9115

File tree

18 files changed

+9340
-5
lines changed

18 files changed

+9340
-5
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ jobs:
242242

243243
- name: linux-musl x86_64 install dependencies
244244
if: matrix.name == 'linux-musl' && matrix.arch == 'x86_64' && matrix.container
245-
run: apk update && apk add --no-cache gcc g++ make cmake sqlite sqlite-dev musl-dev linux-headers git
245+
run: apk update && apk add --no-cache gcc g++ make cmake sqlite sqlite-dev musl-dev linux-headers git curl openssl-dev
246246

247247
- uses: actions/checkout@v4.2.2
248248
with:
@@ -259,6 +259,10 @@ jobs:
259259
mingw-w64-x86_64-cc
260260
mingw-w64-x86_64-cmake
261261
262+
- name: linux install dependencies
263+
if: matrix.name == 'linux'
264+
run: sudo apt-get update && sudo apt-get install -y libssl-dev
265+
262266
- name: macos install dependencies
263267
if: matrix.name == 'macos'
264268
run: brew link sqlite --force
@@ -272,7 +276,7 @@ jobs:
272276
-w /workspace \
273277
alpine:latest \
274278
tail -f /dev/null
275-
docker exec alpine sh -c "apk update && apk add --no-cache gcc g++ make cmake sqlite sqlite-dev musl-dev linux-headers git"
279+
docker exec alpine sh -c "apk update && apk add --no-cache gcc g++ make cmake sqlite sqlite-dev musl-dev linux-headers git curl openssl-dev"
276280
277281
- name: android-aar setup java
278282
if: matrix.name == 'android-aar'

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ dist/
88
# llama.cpp build
99
modules/llama.cpp/build/
1010

11+
# curl build artifacts (curl/include/ headers remain tracked)
12+
curl/src/
13+
curl/*/
14+
!curl/include/
15+
mbedtls/
16+
1117
# Test artifacts
1218
test/unittest
1319
test/unittest.dSYM/

Makefile

Lines changed: 146 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ OMIT_LOCAL_ENGINE ?= 0
66
OMIT_REMOTE_ENGINE ?= 0
77
OMIT_IO ?= 0
88
LLAMA ?=
9+
CURL_VERSION ?= 8.12.1
10+
MBEDTLS_VERSION ?= 3.6.5
911

1012
PLATFORM ?= $(shell uname -s | tr '[:upper:]' '[:lower:]')
1113
ARCH ?= $(shell uname -m)
@@ -26,6 +28,11 @@ DIST_DIR := dist
2628
LLAMA_DIR := modules/llama.cpp
2729
LLAMA_BUILD := $(LLAMA_DIR)/build
2830
TEST_DIR := test
31+
CURL_DIR := curl
32+
CURL_SRC := $(CURL_DIR)/src/curl-$(CURL_VERSION)
33+
CURL_ZIP := $(CURL_DIR)/src/curl-$(CURL_VERSION).zip
34+
CURL_LIB := $(CURL_DIR)/$(PLATFORM)/libcurl.a
35+
MBEDTLS_DIR := mbedtls
2936

3037
VERSION := $(shell grep 'SQLITE_DBMEMORY_VERSION' $(SRC_DIR)/sqlite-memory.h | head -1 | sed 's/.*"\(.*\)".*/\1/')
3138

@@ -55,15 +62,20 @@ ifeq ($(PLATFORM),macos)
5562
INCLUDES += -I/opt/homebrew/include -I/usr/local/include
5663
TEST_LDFLAGS := -L/opt/homebrew/lib -L/usr/local/lib -lsqlite3
5764

65+
CURL_SSL_LIBS := -framework CoreFoundation
66+
5867
ifeq ($(ARCH),x86_64)
5968
CFLAGS += -arch x86_64
6069
LDFLAGS += -arch x86_64
70+
CURL_CONFIG := --with-secure-transport CFLAGS="-arch x86_64"
6171
else ifeq ($(ARCH),arm64)
6272
CFLAGS += -arch arm64
6373
LDFLAGS += -arch arm64
74+
CURL_CONFIG := --with-secure-transport CFLAGS="-arch arm64"
6475
else
6576
CFLAGS += -arch arm64 -arch x86_64
6677
LDFLAGS += -arch arm64 -arch x86_64
78+
CURL_CONFIG := --with-secure-transport CFLAGS="-arch x86_64 -arch arm64"
6779
endif
6880

6981
else ifeq ($(PLATFORM),linux)
@@ -72,6 +84,8 @@ else ifeq ($(PLATFORM),linux)
7284
CXX := g++
7385
LDFLAGS := -shared -lpthread -lm -ldl
7486
TEST_LDFLAGS := -lsqlite3 -lpthread -lm -ldl
87+
CURL_CONFIG := --with-openssl
88+
CURL_SSL_LIBS := -lssl -lcrypto
7589

7690
else ifeq ($(PLATFORM),windows)
7791
EXT := dll
@@ -80,6 +94,8 @@ else ifeq ($(PLATFORM),windows)
8094
LDFLAGS := -shared -static-libgcc -lbcrypt
8195
OUTPUT_NAME := memory
8296
TEST_LDFLAGS := -lsqlite3 -lbcrypt
97+
CURL_CONFIG := --with-schannel CFLAGS="-DCURL_STATICLIB"
98+
CURL_SSL_LIBS := -lcrypt32 -lsecur32 -lws2_32
8399

84100
else ifeq ($(PLATFORM),android)
85101
EXT := so
@@ -91,18 +107,28 @@ else ifeq ($(PLATFORM),android)
91107
TOOLCHAIN := $(ANDROID_NDK)/toolchains/llvm/prebuilt/$(NDK_HOST)
92108

93109
ANDROID_ARCH := $(ARCH)
110+
ANDROID_ABI_SUFFIX := android26
94111
ifeq ($(ARCH),arm64-v8a)
95112
ANDROID_ARCH := aarch64
96113
CC := $(TOOLCHAIN)/bin/aarch64-linux-android26-clang
97114
CXX := $(TOOLCHAIN)/bin/aarch64-linux-android26-clang++
98115
else ifeq ($(ARCH),armeabi-v7a)
116+
ANDROID_ARCH := armv7a
117+
ANDROID_ABI_SUFFIX := androideabi26
99118
CC := $(TOOLCHAIN)/bin/armv7a-linux-androideabi26-clang
100119
CXX := $(TOOLCHAIN)/bin/armv7a-linux-androideabi26-clang++
101120
else ifeq ($(ARCH),x86_64)
102121
CC := $(TOOLCHAIN)/bin/x86_64-linux-android26-clang
103122
CXX := $(TOOLCHAIN)/bin/x86_64-linux-android26-clang++
104123
endif
105124

125+
CURL_LIB := $(CURL_DIR)/$(PLATFORM)/$(ANDROID_ARCH)/libcurl.a
126+
MBEDTLS_INSTALL_DIR := $(MBEDTLS_DIR)/$(PLATFORM)/$(ANDROID_ARCH)
127+
MBEDTLS := $(MBEDTLS_INSTALL_DIR)/lib/libmbedtls.a
128+
CFLAGS += -I$(MBEDTLS_INSTALL_DIR)/include
129+
CURL_CONFIG := --host $(ANDROID_ARCH)-linux-$(ANDROID_ABI_SUFFIX) --with-mbedtls=$(CURDIR)/$(MBEDTLS_INSTALL_DIR) LDFLAGS="-L$(CURDIR)/$(MBEDTLS_INSTALL_DIR)/lib" LIBS="-lmbedtls -lmbedx509 -lmbedcrypto" AR=$(TOOLCHAIN)/bin/llvm-ar AS=$(TOOLCHAIN)/bin/llvm-as CC=$(CC) CXX=$(CXX) LD=$(TOOLCHAIN)/bin/ld RANLIB=$(TOOLCHAIN)/bin/llvm-ranlib STRIP=$(TOOLCHAIN)/bin/llvm-strip
130+
CURL_SSL_LIBS := -L$(MBEDTLS_INSTALL_DIR)/lib -lmbedtls -lmbedx509 -lmbedcrypto
131+
106132
LDFLAGS := -shared -static-libstdc++ -llog -Wl,-z,max-page-size=16384
107133
TEST_LDFLAGS := -ldl -llog -lm
108134

@@ -114,6 +140,8 @@ else ifeq ($(PLATFORM),ios)
114140
CXX := $(shell xcrun --sdk iphoneos -f clang++)
115141
CFLAGS += -isysroot $(SDK) -arch arm64 -miphoneos-version-min=14.0
116142
LDFLAGS := -dynamiclib -isysroot $(SDK) -arch arm64 -miphoneos-version-min=14.0 -framework Security
143+
CURL_CONFIG := --host=arm64-apple-darwin --with-secure-transport CFLAGS="-arch arm64 -isysroot $$(xcrun --sdk iphoneos --show-sdk-path) -miphoneos-version-min=14.0"
144+
CURL_SSL_LIBS := -framework CoreFoundation
117145

118146
else ifeq ($(PLATFORM),ios-sim)
119147
EXT := dylib
@@ -123,6 +151,8 @@ else ifeq ($(PLATFORM),ios-sim)
123151
CXX := $(shell xcrun --sdk iphonesimulator -f clang++)
124152
CFLAGS += -isysroot $(SDK) -arch arm64 -arch x86_64 -miphonesimulator-version-min=14.0
125153
LDFLAGS := -dynamiclib -isysroot $(SDK) -arch arm64 -arch x86_64 -miphonesimulator-version-min=14.0 -framework Security
154+
CURL_CONFIG := --host=arm64-apple-darwin --with-secure-transport CFLAGS="-arch x86_64 -arch arm64 -isysroot $$(xcrun --sdk iphonesimulator --show-sdk-path) -miphonesimulator-version-min=14.0"
155+
CURL_SSL_LIBS := -framework CoreFoundation
126156
endif
127157

128158
LLAMA_OPTIONS := $(LLAMA) \
@@ -238,8 +268,15 @@ endif
238268

239269
ifeq ($(OMIT_REMOTE_ENGINE),0)
240270
C_SOURCES += $(SRC_DIR)/dbmem-rembed.c
271+
INCLUDES += -I$(CURL_DIR)/include
272+
CURL_DEPS := $(CURL_LIB)
273+
LDFLAGS += $(CURL_SSL_LIBS)
274+
ifeq ($(PLATFORM),windows)
275+
CFLAGS += -DCURL_STATICLIB
276+
endif
241277
else
242278
DEFINES += -DDBMEM_OMIT_REMOTE_ENGINE
279+
CURL_DEPS :=
243280
endif
244281

245282
ifeq ($(OMIT_IO),1)
@@ -297,9 +334,9 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR)
297334
@echo "Compiling $<..."
298335
@$(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -c $< -o $@
299336

300-
$(TARGET): $(C_OBJECTS) $(LLAMA_LIBS) | $(DIST_DIR)
337+
$(TARGET): $(C_OBJECTS) $(LLAMA_LIBS) $(CURL_DEPS) | $(DIST_DIR)
301338
@echo "Linking $(TARGET)..."
302-
@$(LINKER) $(C_OBJECTS) $(LLAMA_LIBS) $(LDFLAGS) -o $(TARGET)
339+
@$(LINKER) $(C_OBJECTS) $(LLAMA_LIBS) $(CURL_DEPS) $(LDFLAGS) -o $(TARGET)
303340
@echo "Build complete: $(TARGET)"
304341

305342
.PHONY: test
@@ -328,6 +365,9 @@ ifeq ($(OMIT_LOCAL_ENGINE),0)
328365
TEST_LINK_EXTRAS := -static-libstdc++
329366
endif
330367
endif
368+
ifeq ($(OMIT_REMOTE_ENGINE),0)
369+
TEST_LINK_EXTRAS += $(CURL_LIB) $(CURL_SSL_LIBS)
370+
endif
331371

332372
# Android: compile SQLite amalgamation into unittest (set SQLITE_AMALGAM=path/to/sqlite3.c)
333373
SQLITE_AMALGAM ?=
@@ -350,7 +390,7 @@ $(BUILD_DIR)/test-sqlite3.o: $(SQLITE_AMALGAM) | $(BUILD_DIR)
350390

351391
TEST_C_OBJECTS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/test-%.o,$(C_SOURCES))
352392

353-
$(BUILD_DIR)/unittest: $(BUILD_DIR)/unittest.o $(TEST_C_OBJECTS) $(TEST_SQLITE_OBJ) $(LLAMA_LIBS) | $(BUILD_DIR)
393+
$(BUILD_DIR)/unittest: $(BUILD_DIR)/unittest.o $(TEST_C_OBJECTS) $(TEST_SQLITE_OBJ) $(LLAMA_LIBS) $(CURL_DEPS) | $(BUILD_DIR)
354394
@echo "Linking unittest..."
355395
@$(LINKER) $(BUILD_DIR)/unittest.o $(TEST_C_OBJECTS) $(TEST_SQLITE_OBJ) $(LLAMA_LIBS) \
356396
$(TEST_LDFLAGS) $(FRAMEWORKS) $(TEST_LINK_EXTRAS) \
@@ -383,6 +423,109 @@ distclean: clean
383423
@echo "Cleaning llama.cpp build..."
384424
@rm -rf $(LLAMA_BUILD)
385425

426+
# mbedTLS for Android - minimal TLS library
427+
MBEDTLS_TARBALL := $(MBEDTLS_DIR)/mbedtls-$(MBEDTLS_VERSION).tar.bz2
428+
429+
$(MBEDTLS_TARBALL):
430+
@mkdir -p $(MBEDTLS_DIR)
431+
curl -L -o $(MBEDTLS_TARBALL) https://github.com/Mbed-TLS/mbedtls/releases/download/mbedtls-$(MBEDTLS_VERSION)/mbedtls-$(MBEDTLS_VERSION).tar.bz2
432+
433+
$(MBEDTLS): $(MBEDTLS_TARBALL)
434+
@mkdir -p $(MBEDTLS_DIR)
435+
tar -xjf $(MBEDTLS_TARBALL) -C $(MBEDTLS_DIR)
436+
mkdir -p $(MBEDTLS_DIR)/mbedtls-$(MBEDTLS_VERSION)/build
437+
cd $(MBEDTLS_DIR)/mbedtls-$(MBEDTLS_VERSION)/build && \
438+
cmake .. \
439+
-DCMAKE_TOOLCHAIN_FILE=$(ANDROID_NDK)/build/cmake/android.toolchain.cmake \
440+
-DANDROID_ABI=$(if $(filter aarch64,$(ANDROID_ARCH)),arm64-v8a,$(if $(filter armv7a,$(ANDROID_ARCH)),armeabi-v7a,x86_64)) \
441+
-DANDROID_PLATFORM=android-26 \
442+
-DCMAKE_BUILD_TYPE=MinSizeRel \
443+
-DCMAKE_INSTALL_PREFIX=$(CURDIR)/$(MBEDTLS_INSTALL_DIR) \
444+
-DENABLE_PROGRAMS=OFF \
445+
-DENABLE_TESTING=OFF \
446+
-DUSE_STATIC_MBEDTLS_LIBRARY=ON \
447+
-DUSE_SHARED_MBEDTLS_LIBRARY=OFF \
448+
-DCMAKE_C_FLAGS="-Os -ffunction-sections -fdata-sections" && \
449+
$(MAKE) && $(MAKE) install
450+
rm -rf $(MBEDTLS_DIR)/mbedtls-$(MBEDTLS_VERSION)
451+
452+
# Download and build libcurl
453+
$(CURL_ZIP):
454+
@mkdir -p $(CURL_DIR)/src
455+
curl -L -o $(CURL_ZIP) "https://github.com/curl/curl/releases/download/curl-$(subst .,_,$(CURL_VERSION))/curl-$(CURL_VERSION).zip"
456+
457+
ifeq ($(PLATFORM),android)
458+
$(CURL_LIB): $(MBEDTLS) $(CURL_ZIP)
459+
else
460+
$(CURL_LIB): $(CURL_ZIP)
461+
endif
462+
ifeq ($(PLATFORM),windows)
463+
powershell -Command "Expand-Archive -Path '$(CURL_ZIP)' -DestinationPath '$(CURL_DIR)\src\'"
464+
else
465+
unzip -o $(CURL_ZIP) -d $(CURL_DIR)/src/.
466+
endif
467+
cd $(CURL_SRC) && ./configure \
468+
--without-libpsl \
469+
--disable-alt-svc \
470+
--disable-ares \
471+
--disable-cookies \
472+
--disable-basic-auth \
473+
--disable-digest-auth \
474+
--disable-kerberos-auth \
475+
--disable-negotiate-auth \
476+
--disable-aws \
477+
--disable-dateparse \
478+
--disable-dnsshuffle \
479+
--disable-doh \
480+
--disable-form-api \
481+
--disable-hsts \
482+
--disable-ipv6 \
483+
--disable-libcurl-option \
484+
--disable-manual \
485+
--disable-mime \
486+
--disable-netrc \
487+
--disable-ntlm \
488+
--disable-ntlm-wb \
489+
--disable-progress-meter \
490+
--disable-proxy \
491+
--disable-pthreads \
492+
--disable-socketpair \
493+
--disable-threaded-resolver \
494+
--disable-tls-srp \
495+
--disable-verbose \
496+
--disable-versioned-symbols \
497+
--enable-symbol-hiding \
498+
--without-brotli \
499+
--without-zstd \
500+
--without-libidn2 \
501+
--without-librtmp \
502+
--without-zlib \
503+
--without-nghttp2 \
504+
--without-ngtcp2 \
505+
--disable-shared \
506+
--disable-ftp \
507+
--disable-file \
508+
--disable-ipfs \
509+
--disable-ldap \
510+
--disable-ldaps \
511+
--disable-rtsp \
512+
--disable-dict \
513+
--disable-telnet \
514+
--disable-tftp \
515+
--disable-pop3 \
516+
--disable-imap \
517+
--disable-smb \
518+
--disable-smtp \
519+
--disable-gopher \
520+
--disable-mqtt \
521+
--disable-docs \
522+
--enable-static \
523+
$(CURL_CONFIG)
524+
cd $(CURL_SRC) && $(MAKE)
525+
@mkdir -p $(dir $(CURL_LIB))
526+
mv $(CURL_SRC)/lib/.libs/libcurl.a $(CURL_LIB)
527+
rm -rf $(CURL_DIR)/src/curl-$(CURL_VERSION)
528+
386529
define PLIST
387530
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
388531
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\

curl/include/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!--
2+
Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3+
4+
SPDX-License-Identifier: curl
5+
-->
6+
7+
# include
8+
9+
Public include files for libcurl, external users.
10+
11+
They are all placed in the curl subdirectory here for better fit in any kind of
12+
environment. You must include files from here using...
13+
14+
#include <curl/curl.h>
15+
16+
... style and point the compiler's include path to the directory holding the
17+
curl subdirectory. It makes it more likely to survive future modifications.
18+
19+
The public curl include files can be shared freely between different platforms
20+
and different architectures.
21+
22+
# build
23+
24+
Curl must be statically compiled, and to keep the library as small as possible, we need to remove all the not-needed extra functionalities.
25+
26+
The following configuration is based on curl-8.11.0
27+
Download and extract curl-8.11.0
28+
29+
```bash
30+
31+
cd curl-8.11.0
32+
33+
./configure --with-secure-transport --without-libpsl --disable-alt-svc --disable-ares --disable-cookies --disable-basic-auth --disable-digest-auth --disable-kerberos-auth --disable-negotiate-auth --disable-aws --disable-dateparse --disable-dnsshuffle --disable-doh --disable-form-api --disable-hsts --disable-ipv6 --disable-libcurl-option --disable-manual --disable-mime --disable-netrc --disable-ntlm --disable-ntlm-wb --disable-progress-meter --disable-proxy --disable-pthreads -disable-socketpair --disable-threaded-resolver --disable-tls-srp --disable-verbose --disable-versioned-symbols --enable-symbol-hiding --without-brotli --without-zstd --without-libidn2 --without-librtmp --without-zlib --without-nghttp2 --without-ngtcp2 --disable-shared --disable-ftp --disable-file --disable-ipfs --disable-ldap --disable-ldaps --disable-rtsp --disable-dict --disable-telnet -disable-tftp --disable-pop3 --disable-imap --disable-smb -disable-smtp --disable-gopher --disable-mqtt --disable-docs --enable-static
34+
35+
make
36+
open lib/.libs
37+
38+
```

0 commit comments

Comments
 (0)