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