Skip to content

Commit b4c8594

Browse files
committed
Refactor Makefile for platform-specific build configurations; streamline LLAMA options and link flags for macOS, iOS, and Android
1 parent 8a0aa7a commit b4c8594

File tree

1 file changed

+38
-97
lines changed

1 file changed

+38
-97
lines changed

Makefile

Lines changed: 38 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -170,37 +170,29 @@ ifeq ($(OMIT_LOCAL_ENGINE),0)
170170
# Platform-specific llama.cpp settings
171171
ifeq ($(PLATFORM),macos)
172172
LLAMA_OPTIONS += -DGGML_OPENMP=OFF -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0
173-
# Default macOS build uses Metal + Accelerate + BLAS when LLAMA is empty
174-
ifeq ($(LLAMA),)
175-
LLAMA_OPTIONS += -DGGML_NATIVE=OFF -DGGML_METAL=ON -DGGML_ACCELERATE=ON -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=Apple
176-
endif
173+
# Add Metal and BLAS libraries for macOS (cmake auto-detects and builds these)
174+
LLAMA_LIBS += $(LLAMA_BUILD)/ggml/src/ggml-metal/libggml-metal.a
175+
LLAMA_LIBS += $(LLAMA_BUILD)/ggml/src/ggml-blas/libggml-blas.a
177176
ifeq ($(ARCH),x86_64)
178177
LLAMA_OPTIONS += -DCMAKE_OSX_ARCHITECTURES="x86_64"
179-
LDFLAGS += -arch x86_64
180178
else ifeq ($(ARCH),arm64)
181179
LLAMA_OPTIONS += -DCMAKE_OSX_ARCHITECTURES="arm64"
182-
LDFLAGS += -arch arm64
183180
else
184181
LLAMA_OPTIONS += '-DCMAKE_OSX_ARCHITECTURES=x86_64;arm64'
182+
endif
183+
LDFLAGS := -dynamiclib -framework Metal -framework Foundation -framework Accelerate -framework Security
184+
ifeq ($(ARCH),x86_64)
185+
LDFLAGS += -arch x86_64
186+
else ifeq ($(ARCH),arm64)
187+
LDFLAGS += -arch arm64
188+
else
185189
LDFLAGS += -arch arm64 -arch x86_64
186190
endif
187191
else ifeq ($(PLATFORM),linux)
188192
LLAMA_OPTIONS += -DGGML_OPENMP=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON
189-
# Default Linux build uses CPU-only when LLAMA is empty
190-
ifeq ($(LLAMA),)
191-
LLAMA_OPTIONS += -DGGML_NATIVE=OFF -DGGML_CPU=ON
192-
ifeq ($(ARCH),x86_64)
193-
LLAMA_OPTIONS += -DGGML_AVX2=ON
194-
else ifeq ($(ARCH),arm64)
195-
LLAMA_OPTIONS += -DGGML_CPU_ARM_ARCH=armv8.2-a
196-
endif
197-
endif
198193
else ifeq ($(PLATFORM),windows)
199194
LLAMA_OPTIONS += -DGGML_OPENMP=OFF
200-
# Default Windows build uses CPU-only when LLAMA is empty
201-
ifeq ($(LLAMA),)
202-
LLAMA_OPTIONS += -DGGML_NATIVE=OFF -DGGML_CPU=ON -DGGML_AVX2=ON
203-
endif
195+
LDFLAGS := -shared -lbcrypt -static-libgcc -Wl,--push-state,-Bstatic,-lstdc++,-lwinpthread,--pop-state
204196
else ifeq ($(PLATFORM),android)
205197
# Android NDK cmake toolchain
206198
ANDROID_OPTIONS := -DCMAKE_TOOLCHAIN_FILE=$(ANDROID_NDK)/build/cmake/android.toolchain.cmake \
@@ -212,53 +204,26 @@ ifeq ($(OMIT_LOCAL_ENGINE),0)
212204
ifeq ($(ARCH),arm64-v8a)
213205
ANDROID_OPTIONS += -DGGML_CPU_ARM_ARCH=armv8.2-a+dotprod
214206
endif
215-
# Default Android build uses CPU-only when LLAMA is empty
216-
ifeq ($(LLAMA),)
217-
ANDROID_OPTIONS += -DGGML_NATIVE=OFF -DGGML_CPU=ON
218-
ifeq ($(ARCH),x86_64)
219-
ANDROID_OPTIONS += -DGGML_AVX2=ON
220-
endif
221-
endif
222207
LLAMA_OPTIONS += $(ANDROID_OPTIONS)
223208
else ifeq ($(PLATFORM),ios)
224209
LLAMA_OPTIONS += -DGGML_OPENMP=OFF -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0
225-
# Default iOS build uses Metal + Accelerate + BLAS when LLAMA is empty
226-
ifeq ($(LLAMA),)
227-
LLAMA_OPTIONS += -DGGML_NATIVE=OFF -DGGML_METAL=ON -DGGML_ACCELERATE=ON -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=Apple
228-
endif
210+
# Add Metal and BLAS libraries for iOS
211+
LLAMA_LIBS += $(LLAMA_BUILD)/ggml/src/ggml-metal/libggml-metal.a
212+
LLAMA_LIBS += $(LLAMA_BUILD)/ggml/src/ggml-blas/libggml-blas.a
213+
LDFLAGS := -dynamiclib -isysroot $(SDK) -arch arm64 -miphoneos-version-min=14.0 \
214+
-framework Metal -framework Foundation -framework Accelerate -framework CoreFoundation -framework Security \
215+
-ldl -lpthread -lm -headerpad_max_install_names
229216
else ifeq ($(PLATFORM),ios-sim)
230217
LLAMA_OPTIONS += -DGGML_OPENMP=OFF -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 '-DCMAKE_OSX_ARCHITECTURES=x86_64;arm64'
231-
# Default iOS simulator build uses Metal + Accelerate + BLAS when LLAMA is empty
232-
ifeq ($(LLAMA),)
233-
LLAMA_OPTIONS += -DGGML_NATIVE=OFF -DGGML_METAL=ON -DGGML_ACCELERATE=ON -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=Apple
234-
endif
235-
endif
236-
237-
# Backend-specific libraries (detected from LLAMA cmake flags)
238-
# On Apple platforms, if LLAMA is empty, use default settings (Metal + Accelerate + BLAS)
239-
ifneq (,$(filter $(PLATFORM),macos ios ios-sim))
240-
ifeq ($(LLAMA),)
241-
# Default Apple build: Metal + Accelerate + BLAS with Apple vendor
242-
LLAMA_LIBS += $(LLAMA_BUILD)/ggml/src/ggml-metal/libggml-metal.a
243-
LLAMA_LIBS += $(LLAMA_BUILD)/ggml/src/ggml-blas/libggml-blas.a
244-
FRAMEWORKS += -framework Metal -framework Foundation -framework Accelerate
245-
endif
246-
endif
247-
ifneq (,$(findstring GGML_METAL=ON,$(LLAMA)))
218+
# Add Metal and BLAS libraries for iOS simulator
248219
LLAMA_LIBS += $(LLAMA_BUILD)/ggml/src/ggml-metal/libggml-metal.a
249-
FRAMEWORKS += -framework Metal -framework Foundation
250-
endif
251-
ifneq (,$(findstring GGML_ACCELERATE=ON,$(LLAMA)))
252-
FRAMEWORKS += -framework Accelerate
253-
endif
254-
ifneq (,$(findstring GGML_BLAS=ON,$(LLAMA)))
255220
LLAMA_LIBS += $(LLAMA_BUILD)/ggml/src/ggml-blas/libggml-blas.a
256-
ifneq (,$(findstring Apple,$(LLAMA)))
257-
FRAMEWORKS += -framework Accelerate
258-
else
259-
LDFLAGS += -lblas
260-
endif
221+
LDFLAGS := -dynamiclib -isysroot $(SDK) -arch arm64 -arch x86_64 -miphonesimulator-version-min=14.0 \
222+
-framework Metal -framework Foundation -framework Accelerate -framework CoreFoundation -framework Security \
223+
-ldl -lpthread -lm -headerpad_max_install_names
261224
endif
225+
226+
# Backend-specific libraries (detected from LLAMA cmake flags for explicit overrides)
262227
ifneq (,$(findstring GGML_VULKAN=ON,$(LLAMA)))
263228
LLAMA_LIBS += $(LLAMA_BUILD)/ggml/src/ggml-vulkan/libggml-vulkan.a
264229
ifeq ($(PLATFORM),windows)
@@ -280,37 +245,6 @@ ifeq ($(OMIT_LOCAL_ENGINE),0)
280245
LDFLAGS += -lOpenCL
281246
endif
282247

283-
# Update LDFLAGS with frameworks for macOS
284-
ifeq ($(PLATFORM),macos)
285-
LDFLAGS := -dynamiclib $(FRAMEWORKS)
286-
ifeq ($(ARCH),x86_64)
287-
LDFLAGS += -arch x86_64
288-
else ifeq ($(ARCH),arm64)
289-
LDFLAGS += -arch arm64
290-
else
291-
LDFLAGS += -arch arm64 -arch x86_64
292-
endif
293-
endif
294-
295-
# Update LDFLAGS for Windows with llama.cpp
296-
ifeq ($(PLATFORM),windows)
297-
LDFLAGS := -shared -lbcrypt -static-libgcc -Wl,--push-state,-Bstatic,-lstdc++,-lwinpthread,--pop-state
298-
endif
299-
300-
# Update LDFLAGS for iOS with llama.cpp (Metal GPU acceleration)
301-
ifeq ($(PLATFORM),ios)
302-
LDFLAGS := -dynamiclib -isysroot $(SDK) -arch arm64 -miphoneos-version-min=14.0 \
303-
$(FRAMEWORKS) -framework CoreFoundation -framework Security \
304-
-ldl -lpthread -lm -headerpad_max_install_names
305-
endif
306-
307-
# Update LDFLAGS for iOS simulator with llama.cpp (Metal GPU acceleration)
308-
ifeq ($(PLATFORM),ios-sim)
309-
LDFLAGS := -dynamiclib -isysroot $(SDK) -arch arm64 -arch x86_64 -miphonesimulator-version-min=14.0 \
310-
$(FRAMEWORKS) -framework CoreFoundation -framework Security \
311-
-ldl -lpthread -lm -headerpad_max_install_names
312-
endif
313-
314248
# Use C++ linker when linking with llama.cpp
315249
LINKER := $(CXX)
316250
BUILD_DEPS := llama
@@ -393,6 +327,15 @@ test: $(BUILD_DEPS) $(TARGET) $(BUILD_DIR)/unittest
393327
# Unit test needs SQLITE_CORE to use direct SQLite calls (not extension API)
394328
TEST_DEFINES := -DSQLITE_CORE
395329

330+
# Test-specific link flags (includes all frameworks/libs needed for llama.cpp)
331+
ifeq ($(OMIT_LOCAL_ENGINE),0)
332+
ifeq ($(PLATFORM),macos)
333+
TEST_LINK_EXTRAS := -framework Metal -framework Foundation -framework Accelerate -lobjc
334+
endif
335+
else
336+
TEST_LINK_EXTRAS :=
337+
endif
338+
396339
# Compile test sources with SQLITE_CORE
397340
$(BUILD_DIR)/unittest.o: $(TEST_DIR)/unittest.c | $(BUILD_DIR)
398341
@echo "Compiling unittest.c..."
@@ -409,7 +352,7 @@ TEST_C_OBJECTS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/test-%.o,$(C_SOURCES))
409352
$(BUILD_DIR)/unittest: $(BUILD_DIR)/unittest.o $(TEST_C_OBJECTS) $(LLAMA_LIBS) | $(BUILD_DIR)
410353
@echo "Linking unittest..."
411354
@$(LINKER) $(BUILD_DIR)/unittest.o $(TEST_C_OBJECTS) $(LLAMA_LIBS) \
412-
$(TEST_LDFLAGS) $(FRAMEWORKS) \
355+
$(TEST_LDFLAGS) $(FRAMEWORKS) $(TEST_LINK_EXTRAS) \
413356
-o $@
414357

415358
# Build without local engine (remote-only builds)
@@ -535,26 +478,24 @@ xcframework:
535478

536479
AAR_ARM = packages/android/src/main/jniLibs/arm64-v8a/
537480
AAR_X86 = packages/android/src/main/jniLibs/x86_64/
538-
AAR_LLAMA_ARM = LLAMA="-DGGML_NATIVE=OFF -DGGML_CPU=ON -DGGML_CPU_ARM_ARCH=armv8.2-a+dotprod"
539-
AAR_LLAMA_X86 = LLAMA="-DGGML_NATIVE=OFF -DGGML_CPU=ON -DGGML_AVX2=ON"
540481

541482
.PHONY: aar
542483
aar:
543484
mkdir -p $(AAR_ARM) $(AAR_X86)
544485
# Build remote variant (no llama.cpp)
545-
$(MAKE) clean && $(MAKE) PLATFORM=android ARCH=arm64-v8a OMIT_LOCAL_ENGINE=1
486+
$(MAKE) distclean && $(MAKE) PLATFORM=android ARCH=arm64-v8a OMIT_LOCAL_ENGINE=1
546487
mv $(DIST_DIR)/memory.so $(AAR_ARM)/memory_remote.so
547488
$(MAKE) clean && $(MAKE) PLATFORM=android ARCH=x86_64 OMIT_LOCAL_ENGINE=1
548489
mv $(DIST_DIR)/memory.so $(AAR_X86)/memory_remote.so
549490
# Build local variant (llama.cpp only)
550-
$(MAKE) clean && $(MAKE) PLATFORM=android ARCH=arm64-v8a OMIT_REMOTE_ENGINE=1 $(AAR_LLAMA_ARM)
491+
$(MAKE) distclean && $(MAKE) PLATFORM=android ARCH=arm64-v8a OMIT_REMOTE_ENGINE=1
551492
mv $(DIST_DIR)/memory.so $(AAR_ARM)/memory_local.so
552-
$(MAKE) clean && $(MAKE) PLATFORM=android ARCH=x86_64 OMIT_REMOTE_ENGINE=1 $(AAR_LLAMA_X86)
493+
$(MAKE) distclean && $(MAKE) PLATFORM=android ARCH=x86_64 OMIT_REMOTE_ENGINE=1
553494
mv $(DIST_DIR)/memory.so $(AAR_X86)/memory_local.so
554495
# Build full variant (both)
555-
$(MAKE) clean && $(MAKE) PLATFORM=android ARCH=arm64-v8a $(AAR_LLAMA_ARM)
496+
$(MAKE) distclean && $(MAKE) PLATFORM=android ARCH=arm64-v8a
556497
mv $(DIST_DIR)/memory.so $(AAR_ARM)/memory_full.so
557-
$(MAKE) clean && $(MAKE) PLATFORM=android ARCH=x86_64 $(AAR_LLAMA_X86)
498+
$(MAKE) distclean && $(MAKE) PLATFORM=android ARCH=x86_64
558499
mv $(DIST_DIR)/memory.so $(AAR_X86)/memory_full.so
559500
# Build AAR
560501
cd packages/android && ./gradlew clean assembleRelease

0 commit comments

Comments
 (0)