Skip to content

Commit 8e85af2

Browse files
committed
fix: clean code and optimize extension flags
1 parent 380297f commit 8e85af2

3 files changed

Lines changed: 39 additions & 58 deletions

File tree

.github/workflows/main.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,17 @@ jobs:
237237
MINIAUDIO_HASH=$(git -C modules/miniaudio rev-parse HEAD)
238238
MBEDTLS_HASH=$(git -C modules/mbedtls rev-parse HEAD)
239239
CURL_HASH=$(git -C modules/curl rev-parse HEAD)
240+
# Hash both Makefiles together — extensions/sqlite/Makefile changes
241+
# the link step (CFLAGS / LDFLAGS) and so should invalidate dep caches.
240242
if command -v sha256sum >/dev/null 2>&1; then
241243
MAKE_HASH=$(echo "$MATRIX_MAKE" | sha256sum | cut -d' ' -f1)
242-
MAKEFILE_HASH=$(sha256sum Makefile | cut -d' ' -f1)
244+
MAKEFILE_HASH=$(cat Makefile extensions/sqlite/Makefile | sha256sum | cut -d' ' -f1)
243245
elif command -v shasum >/dev/null 2>&1; then
244246
MAKE_HASH=$(echo "$MATRIX_MAKE" | shasum -a 256 | cut -d' ' -f1)
245-
MAKEFILE_HASH=$(shasum -a 256 Makefile | cut -d' ' -f1)
247+
MAKEFILE_HASH=$(cat Makefile extensions/sqlite/Makefile | shasum -a 256 | cut -d' ' -f1)
246248
else
247249
MAKE_HASH=$(echo "$MATRIX_MAKE" | openssl dgst -sha256 | cut -d' ' -f2)
248-
MAKEFILE_HASH=$(openssl dgst -sha256 Makefile | cut -d' ' -f2)
250+
MAKEFILE_HASH=$(cat Makefile extensions/sqlite/Makefile | openssl dgst -sha256 | cut -d' ' -f2)
249251
fi
250252
echo "llama=$LLAMA_HASH" >> $GITHUB_OUTPUT
251253
echo "whisper=$WHISPER_HASH" >> $GITHUB_OUTPUT

Makefile

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ else
2424
endif
2525
endif
2626

27+
# Non-empty when building for an Apple platform (macos / ios / ios-sim).
28+
IS_APPLE := $(filter $(PLATFORM),macos ios ios-sim)
29+
2730
# CMake options pass-through from CI matrix
2831
LLAMA ?=
2932
WHISPER ?=
@@ -58,12 +61,8 @@ CURL_BUILD := $(BUILD_DIR)/curl
5861

5962
SQLITE_DIR := $(ADAM_ROOT)modules/sqlite
6063

61-
# Size-reduction flags. -ffunction-sections / -fdata-sections puts each
62-
# function/variable into its own ELF section so the linker can drop unused
63-
# ones via --gc-sections / -dead_strip. -flto lets the linker inline and
64-
# eliminate dead code across translation units. On Mach-O the section flags
65-
# are no-ops (Apple's linker uses subsection-via-symbols), but -flto and
66-
# -Wl,-dead_strip still apply.
64+
# Section flags are Mach-O no-ops (Apple's linker uses subsection-via-symbols
65+
# unconditionally), but -flto and -Wl,-dead_strip still apply there.
6766
SIZE_CFLAGS := -ffunction-sections -fdata-sections -flto
6867

6968
CFLAGS := -std=gnu11 -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE -Wall -Wextra -Wpedantic -O2 -fPIC $(SIZE_CFLAGS)
@@ -96,10 +95,7 @@ else
9695
LDFLAGS := -lpthread
9796
endif
9897

99-
# Dead-code elimination at link time. Pairs with -ffunction-sections /
100-
# -fdata-sections / -flto in CFLAGS. Apple's linker uses -dead_strip
101-
# (subsection-via-symbols); GNU ld + lld use --gc-sections.
102-
ifneq (,$(filter $(PLATFORM),macos ios ios-sim))
98+
ifneq ($(IS_APPLE),)
10399
LDFLAGS += -Wl,-dead_strip -flto
104100
else
105101
LDFLAGS += -Wl,--gc-sections -flto
@@ -141,7 +137,7 @@ WHISPER_LIBS := $(WHISPER_BUILD)/src/libwhisper.a
141137
# Apple platforms (macos, ios, ios-sim) all use NSURLSession.
142138
# _DARWIN_C_SOURCE re-exposes BSD extensions (e.g. memmem) that the
143139
# global _POSIX_C_SOURCE=200809L would otherwise hide.
144-
ifneq (,$(filter $(PLATFORM),macos ios ios-sim))
140+
ifneq ($(IS_APPLE),)
145141
CFLAGS += -DADAM_NO_CURL -D_DARWIN_C_SOURCE=1
146142
LDFLAGS += -framework Foundation
147143
LDFLAGS += -framework SystemConfiguration -framework Security
@@ -338,7 +334,7 @@ $(SQLITE_OBJ): $(SQLITE_DIR)/sqlite3.c
338334
# otherwise the explicit .m → .o rule overrides the .c pattern rule on Linux,
339335
# making gcc try to invoke cc1obj on adam_tts_system.m even though TTS_SYS_SRC
340336
# is set to adam_tts_system.c there.
341-
ifneq (,$(filter $(PLATFORM),macos ios ios-sim))
337+
ifneq ($(IS_APPLE),)
342338
src/adam_net_apple.o: src/adam_net_apple.m
343339
$(CC) $(CFLAGS) -c $< -o $@
344340

@@ -388,7 +384,7 @@ endif
388384
# definition and continue — only needed for the test_adam link (the
389385
# shared-extension link uses --gc-sections + archive semantics that avoid
390386
# pulling mtmd-helper.o on most platforms).
391-
ifneq (,$(filter $(PLATFORM),macos ios ios-sim))
387+
ifneq ($(IS_APPLE),)
392388
TEST_MULDEF :=
393389
else
394390
TEST_MULDEF := -Wl,--allow-multiple-definition
@@ -400,7 +396,7 @@ endif
400396
ifeq ($(PLATFORM),android)
401397
TEST_LD := $(ANDROID_NDK_BIN)/$(NDK_TRIPLE)-clang++
402398
TEST_LD_EXTRA := -static-libstdc++
403-
else ifneq (,$(filter $(PLATFORM),macos ios ios-sim))
399+
else ifneq ($(IS_APPLE),)
404400
TEST_LD := $(CC)
405401
TEST_LD_EXTRA :=
406402
else
@@ -522,36 +518,33 @@ ifeq ($(UNAME_S),Linux)
522518
deps: build/mbedtls.stamp build/curl.stamp
523519
endif
524520

525-
# Flags passed to every cmake dep build (llama / whisper / mbedtls / curl):
526-
# -fPIC archives are linked into a shared lib
527-
# -ffunction-sections / -fdata- let the final linker drop unused symbols
528-
# sections (gc-sections / dead_strip)
529-
# -fvisibility=hidden don't pollute the .so dynamic symbol table
530-
# (-fvisibility-inlines- with llama/ggml/whisper internals — we
531-
# hidden for C++) only need to export adam_* + sqlite3_*
532-
# -flto link-time inlining + dead code elimination
533-
# Combined with -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON cmake also enables
534-
# LTO at link time of any internal static archives.
535-
DEP_CFLAGS := -fPIC -ffunction-sections -fdata-sections -fvisibility=hidden -flto
521+
# Cmake dep builds get the same size flags as adam, plus -fvisibility=hidden
522+
# so llama/ggml/whisper/curl/mbedtls internals don't pollute the final .so's
523+
# dynamic symbol table (only adam_* and sqlite3_adam_init need to be exported).
524+
# Pairs with -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON for cross-archive LTO.
525+
DEP_CFLAGS := $(SIZE_CFLAGS) -fPIC -fvisibility=hidden
536526
DEP_CXXFLAGS := $(DEP_CFLAGS) -fvisibility-inlines-hidden
537527

528+
# Shared cmake options for every dep (llama / whisper / mbedtls / curl).
529+
# -DCMAKE_POSITION_INDEPENDENT_CODE=ON is required so the resulting .a
530+
# archives can be linked into the shared extension — GNU ld on Linux /
531+
# Windows rejects non-PIC TLS relocations; macOS arm64 is implicitly PIC.
532+
CMAKE_DEP_OPTS := \
533+
-DCMAKE_BUILD_TYPE=Release \
534+
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
535+
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON \
536+
-DCMAKE_C_FLAGS="$(DEP_CFLAGS)" \
537+
-DCMAKE_CXX_FLAGS="$(DEP_CXXFLAGS)"
538+
538539
# Stamp targets: cacheable in CI and idempotent for local dev.
539540
$(BUILD_DIR)/llama.cpp.stamp:
540541
@mkdir -p $(BUILD_DIR)
541-
@# -DCMAKE_POSITION_INDEPENDENT_CODE=ON: the resulting .a archives are
542-
@# linked into the shared extension (dist/adam.{dylib,so,dll}), so every
543-
@# object must be PIC. GNU ld on Linux/Windows rejects non-PIC TLS
544-
@# relocations; macOS arm64 is implicitly PIC; Android's PLATFORM_OPTS
545-
@# already includes this — but pass it unconditionally to be safe.
546542
cmake -B $(LLAMA_BUILD) -S $(LLAMA_DIR) \
547543
-DBUILD_SHARED_LIBS=OFF -DLLAMA_BUILD_TESTS=OFF \
548544
-DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_SERVER=OFF \
549-
-DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
550-
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON \
551545
-DCMAKE_STATIC_LIBRARY_PREFIX=lib \
552-
-DCMAKE_C_FLAGS="$(DEP_CFLAGS)" -DCMAKE_CXX_FLAGS="$(DEP_CXXFLAGS)" \
553546
-DGGML_OPENMP=OFF \
554-
$(PLATFORM_OPTS) $(LLAMA)
547+
$(CMAKE_DEP_OPTS) $(PLATFORM_OPTS) $(LLAMA)
555548
cmake --build $(LLAMA_BUILD) --config Release -j$(CPUS) --target llama --target ggml --target mtmd
556549
touch $@
557550

@@ -563,12 +556,9 @@ $(BUILD_DIR)/whisper.cpp.stamp: $(BUILD_DIR)/llama.cpp.stamp
563556
cmake -B $(WHISPER_BUILD) -S $(WHISPER_DIR) \
564557
-DBUILD_SHARED_LIBS=OFF -DWHISPER_BUILD_TESTS=OFF \
565558
-DWHISPER_BUILD_EXAMPLES=OFF \
566-
-DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
567-
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON \
568559
-DCMAKE_STATIC_LIBRARY_PREFIX=lib \
569-
-DCMAKE_C_FLAGS="$(DEP_CFLAGS)" -DCMAKE_CXX_FLAGS="$(DEP_CXXFLAGS)" \
570560
-DGGML_OPENMP=OFF \
571-
$(PLATFORM_OPTS) $(LLAMA) $(WHISPER)
561+
$(CMAKE_DEP_OPTS) $(PLATFORM_OPTS) $(LLAMA) $(WHISPER)
572562
cmake --build $(WHISPER_BUILD) --config Release -j$(CPUS) --target whisper
573563
touch $@
574564

@@ -588,10 +578,7 @@ $(BUILD_DIR)/mbedtls.stamp:
588578
cmake -B $(MBEDTLS_BUILD) -S $(MBEDTLS_DIR) \
589579
-DENABLE_TESTING=OFF -DENABLE_PROGRAMS=OFF \
590580
-DMBEDTLS_FATAL_WARNINGS=OFF \
591-
-DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
592-
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON \
593-
-DCMAKE_C_FLAGS="$(DEP_CFLAGS)" -DCMAKE_CXX_FLAGS="$(DEP_CXXFLAGS)" \
594-
$(PLATFORM_OPTS)
581+
$(CMAKE_DEP_OPTS) $(PLATFORM_OPTS)
595582
cmake --build $(MBEDTLS_BUILD) --config Release -j$(CPUS)
596583
touch $@
597584

@@ -602,14 +589,11 @@ $(BUILD_DIR)/curl.stamp: $(BUILD_DIR)/mbedtls.stamp
602589
-DCURL_BROTLI=OFF -DCURL_ZSTD=OFF -DUSE_NGHTTP2=OFF \
603590
-DUSE_LIBIDN2=OFF -DCURL_USE_LIBPSL=OFF -DCURL_USE_LIBSSH2=OFF \
604591
-DBUILD_SHARED_LIBS=OFF -DBUILD_CURL_EXE=OFF -DBUILD_TESTING=OFF \
605-
-DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
606-
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON \
607-
-DCMAKE_C_FLAGS="$(DEP_CFLAGS)" -DCMAKE_CXX_FLAGS="$(DEP_CXXFLAGS)" \
608592
-DMBEDTLS_INCLUDE_DIR=$(MBEDTLS_DIR)/include \
609593
-DMBEDTLS_LIBRARY=$(MBEDTLS_BUILD)/library/libmbedtls.a \
610594
-DMBEDX509_LIBRARY=$(MBEDTLS_BUILD)/library/libmbedx509.a \
611595
-DMBEDCRYPTO_LIBRARY=$(MBEDTLS_BUILD)/library/libmbedcrypto.a \
612-
$(PLATFORM_OPTS)
596+
$(CMAKE_DEP_OPTS) $(PLATFORM_OPTS)
613597
cmake --build $(CURL_BUILD) --config Release -j$(CPUS)
614598
touch $@
615599

@@ -664,19 +648,16 @@ endif
664648

665649
# Non-Apple platforms (linux, windows, android) need libcurl + mbedtls.
666650
EXT_DEPS := $(BUILD_DIR)/llama.cpp.stamp $(BUILD_DIR)/whisper.cpp.stamp $(BUILD_DIR)/miniaudio.stamp
667-
ifeq (,$(filter $(PLATFORM),macos ios ios-sim))
651+
ifeq ($(IS_APPLE),)
668652
EXT_DEPS += $(BUILD_DIR)/mbedtls.stamp $(BUILD_DIR)/curl.stamp
669653
endif
670654

671-
# Strip the final shared object to drop debug symbols and the static symbol
672-
# table. The dynamic symbol table (which holds sqlite3_adam_init) is in a
673-
# separate ELF/Mach-O section and is preserved. Set `STRIP_DIST=0` to keep
674-
# symbols (e.g., for local debugging of the shipped artifact).
655+
# Set STRIP_DIST=0 to keep symbols when debugging the shipped artifact.
675656
STRIP_DIST ?= 1
676657
ifeq ($(PLATFORM),android)
677658
STRIP := $(ANDROID_NDK_BIN)/llvm-strip
678659
STRIP_FLAGS := --strip-all
679-
else ifneq (,$(filter $(PLATFORM),macos ios ios-sim))
660+
else ifneq ($(IS_APPLE),)
680661
STRIP := strip
681662
STRIP_FLAGS := -x
682663
else

extensions/sqlite/Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ endif
5757
# glibc strict-C11 hides strdup / strcasecmp / clock_gettime; Apple needs
5858
# _DARWIN_C_SOURCE; MinGW needs _GNU_SOURCE for strndup. Defining all three
5959
# is safe — each platform only honors the ones it knows about.
60-
# Size-reduction flags applied uniformly to all SQLite-extension wrapper
61-
# objects (sqlite_adam.c + adam_ext_*.c). Pairs with -Wl,--gc-sections /
62-
# -Wl,-dead_strip + -flto in LDFLAGS below.
60+
# Section flags pair with -Wl,--gc-sections / -Wl,-dead_strip below.
6361
CFLAGS := -std=gnu11 -Wall -Wextra -O2 -fPIC -ffunction-sections -fdata-sections -flto
6462
CFLAGS += -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE -D_DARWIN_C_SOURCE=1
6563
CFLAGS += -I$(ADAM_ROOT)/src

0 commit comments

Comments
 (0)