|
| 1 | +# Makefile for SQLite Vector Extension |
| 2 | +# Supports compilation for Linux, macOS, Windows, Android and iOS |
| 3 | + |
| 4 | +# customize sqlite3 executable with |
| 5 | +# make test SQLITE3=/opt/homebrew/Cellar/sqlite/3.49.1/bin/sqlite3 |
| 6 | +SQLITE3 ?= sqlite3 |
| 7 | + |
| 8 | +# Set default platform if not specified |
| 9 | +ifeq ($(OS),Windows_NT) |
| 10 | + PLATFORM := windows |
| 11 | + HOST := windows |
| 12 | + CPUS := $(shell powershell -Command "[Environment]::ProcessorCount") |
| 13 | +else |
| 14 | + HOST = $(shell uname -s | tr '[:upper:]' '[:lower:]') |
| 15 | + ifeq ($(HOST),darwin) |
| 16 | + PLATFORM := macos |
| 17 | + CPUS := $(shell sysctl -n hw.ncpu) |
| 18 | + else |
| 19 | + PLATFORM := $(HOST) |
| 20 | + CPUS := $(shell nproc) |
| 21 | + endif |
| 22 | +endif |
| 23 | + |
| 24 | +# Speed up builds by using all available CPU cores |
| 25 | +MAKEFLAGS += -j$(CPUS) |
| 26 | + |
| 27 | +# Compiler and flags |
| 28 | +CC = gcc |
| 29 | +CFLAGS = -Wall -Wextra -Wno-unused-parameter -I$(SRC_DIR) -I$(LIB_DIR) -DSQLITE_CORE |
| 30 | + |
| 31 | +# Directories |
| 32 | +SRC_DIR = src |
| 33 | +DIST_DIR = dist |
| 34 | +LIB_DIR = libs |
| 35 | +VPATH = $(SRC_DIR):$(LIB_DIR) |
| 36 | +BUILD_DIR = build |
| 37 | + |
| 38 | +# Files |
| 39 | +SRC_FILES = $(wildcard $(SRC_DIR)/*.c) |
| 40 | +OBJ_FILES = $(patsubst %.c, $(BUILD_DIR)/%.o, $(notdir $(SRC_FILES))) |
| 41 | + |
| 42 | +# Platform-specific settings |
| 43 | +ifeq ($(PLATFORM),windows) |
| 44 | + TARGET := $(DIST_DIR)/vector.dll |
| 45 | + LDFLAGS += -shared |
| 46 | + # Create .def file for Windows |
| 47 | + DEF_FILE := $(BUILD_DIR)/vector.def |
| 48 | +else ifeq ($(PLATFORM),macos) |
| 49 | + TARGET := $(DIST_DIR)/vector.dylib |
| 50 | + LDFLAGS += -arch x86_64 -arch arm64 -dynamiclib -undefined dynamic_lookup |
| 51 | + CFLAGS += -arch x86_64 -arch arm64 |
| 52 | +else ifeq ($(PLATFORM),android) |
| 53 | + # Set ARCH to find Android NDK's Clang compiler, the user should set the ARCH |
| 54 | + ifeq ($(filter %,$(ARCH)),) |
| 55 | + $(error "Android ARCH must be set to ARCH=x86_64 or ARCH=arm64-v8a") |
| 56 | + endif |
| 57 | + # Set ANDROID_NDK path to find android build tools |
| 58 | + # e.g. on MacOS: export ANDROID_NDK=/Users/username/Library/Android/sdk/ndk/25.2.9519653 |
| 59 | + ifeq ($(filter %,$(ANDROID_NDK)),) |
| 60 | + $(error "Android NDK must be set") |
| 61 | + endif |
| 62 | + |
| 63 | + BIN = $(ANDROID_NDK)/toolchains/llvm/prebuilt/$(HOST)-x86_64/bin |
| 64 | + PATH := $(BIN):$(PATH) |
| 65 | + |
| 66 | + ifneq (,$(filter $(ARCH),arm64 arm64-v8a)) |
| 67 | + override ARCH := aarch64 |
| 68 | + endif |
| 69 | + |
| 70 | + CC = $(BIN)/$(ARCH)-linux-android26-clang |
| 71 | + TARGET := $(DIST_DIR)/vector.so |
| 72 | + LDFLAGS += -shared |
| 73 | +else ifeq ($(PLATFORM),ios) |
| 74 | + TARGET := $(DIST_DIR)/vector.dylib |
| 75 | + SDK := -isysroot $(shell xcrun --sdk iphoneos --show-sdk-path) -miphoneos-version-min=11.0 |
| 76 | + LDFLAGS += -dynamiclib $(SDK) |
| 77 | + CFLAGS += -arch arm64 $(SDK) |
| 78 | +else ifeq ($(PLATFORM),isim) |
| 79 | + TARGET := $(DIST_DIR)/vector.dylib |
| 80 | + SDK := -isysroot $(shell xcrun --sdk iphonesimulator --show-sdk-path) -miphonesimulator-version-min=11.0 |
| 81 | + LDFLAGS += -arch x86_64 -arch arm64 -dynamiclib $(SDK) |
| 82 | + CFLAGS += -arch x86_64 -arch arm64 $(SDK) |
| 83 | +else # linux |
| 84 | + TARGET := $(DIST_DIR)/vector.so |
| 85 | + LDFLAGS += -shared |
| 86 | +endif |
| 87 | + |
| 88 | +# Windows .def file generation |
| 89 | +$(DEF_FILE): |
| 90 | +ifeq ($(PLATFORM),windows) |
| 91 | + @echo "LIBRARY vector.dll" > $@ |
| 92 | + @echo "EXPORTS" >> $@ |
| 93 | + @echo " sqlite3_vector_init" >> $@ |
| 94 | +endif |
| 95 | + |
| 96 | +# Make sure the build and dist directories exist |
| 97 | +$(shell mkdir -p $(BUILD_DIR) $(DIST_DIR)) |
| 98 | + |
| 99 | +# Default target |
| 100 | +extension: $(TARGET) |
| 101 | +all: $(TARGET) |
| 102 | + |
| 103 | +# Loadable library |
| 104 | +$(TARGET): $(OBJ_FILES) $(DEF_FILE) |
| 105 | + $(CC) $(OBJ_FILES) $(DEF_FILE) -o $@ $(LDFLAGS) |
| 106 | +ifeq ($(PLATFORM),windows) |
| 107 | + # Generate import library for Windows |
| 108 | + dlltool -D $@ -d $(DEF_FILE) -l $(DIST_DIR)/vector.lib |
| 109 | +endif |
| 110 | + |
| 111 | +# Object files |
| 112 | +$(BUILD_DIR)/%.o: %.c |
| 113 | + $(CC) $(CFLAGS) -O3 -fPIC -c $< -o $@ |
| 114 | + |
| 115 | +test: $(TARGET) |
| 116 | + $(SQLITE3) ":memory:" -cmd ".bail on" ".load ./$<" "SELECT vector_version();" |
| 117 | + |
| 118 | +# Clean up generated files |
| 119 | +clean: |
| 120 | + rm -rf $(BUILD_DIR)/* $(DIST_DIR)/* *.gcda *.gcno *.gcov *.sqlite |
| 121 | + |
| 122 | +# Help message |
| 123 | +help: |
| 124 | + @echo "SQLite Vector Extension Makefile" |
| 125 | + @echo "Usage:" |
| 126 | + @echo " make [PLATFORM=platform] [ARCH=arch] [ANDROID_NDK=\$$ANDROID_HOME/ndk/26.1.10909125] [target]" |
| 127 | + @echo "" |
| 128 | + @echo "Platforms:" |
| 129 | + @echo " linux (default on Linux)" |
| 130 | + @echo " macos (default on macOS)" |
| 131 | + @echo " windows (default on Windows)" |
| 132 | + @echo " android (needs ARCH to be set to x86_64 or arm64-v8a and ANDROID_NDK to be set)" |
| 133 | + @echo " ios (only on macOS)" |
| 134 | + @echo " isim (only on macOS)" |
| 135 | + @echo "" |
| 136 | + @echo "Targets:" |
| 137 | + @echo " all - Build the extension (default)" |
| 138 | + @echo " clean - Remove built files" |
| 139 | + @echo " test - Test the extension" |
| 140 | + @echo " help - Display this help message" |
| 141 | + |
| 142 | +.PHONY: all clean test extension help |
0 commit comments