2424 endif
2525endif
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
2831LLAMA ?=
2932WHISPER ?=
@@ -58,12 +61,8 @@ CURL_BUILD := $(BUILD_DIR)/curl
5861
5962SQLITE_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.
6766SIZE_CFLAGS := -ffunction-sections -fdata-sections -flto
6867
6968CFLAGS := -std=gnu11 -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE -Wall -Wextra -Wpedantic -O2 -fPIC $(SIZE_CFLAGS )
9695LDFLAGS := -lpthread
9796endif
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 ) ,)
10399LDFLAGS += -Wl,-dead_strip -flto
104100else
105101LDFLAGS += -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 ) , )
342338src/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 ) , )
392388TEST_MULDEF :=
393389else
394390TEST_MULDEF := -Wl,--allow-multiple-definition
@@ -400,7 +396,7 @@ endif
400396ifeq ($(PLATFORM ) ,android)
401397TEST_LD := $(ANDROID_NDK_BIN ) /$(NDK_TRIPLE ) -clang++
402398TEST_LD_EXTRA := -static-libstdc++
403- else ifneq (,$(filter $(PLATFORM),macos ios ios-sim) )
399+ else ifneq ($(IS_APPLE), )
404400TEST_LD := $(CC )
405401TEST_LD_EXTRA :=
406402else
@@ -522,36 +518,33 @@ ifeq ($(UNAME_S),Linux)
522518deps : build/mbedtls.stamp build/curl.stamp
523519endif
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
536526DEP_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.
666650EXT_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
669653endif
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.
675656STRIP_DIST ?= 1
676657ifeq ($(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
682663else
0 commit comments