33
44# Set default platform if not specified
55ifeq ($(OS ) ,Windows_NT)
6- PLATFORM := windows
6+ PLATFORM := windows
7+ HOST := windows
8+ CPUS := $(shell powershell -Command "[Environment]::ProcessorCount")
79else
8- UNAME_S := $(shell uname -s)
9- ifeq ($(UNAME_S),Darwin)
10- PLATFORM := macos
11- else
12- PLATFORM := linux
13- endif
10+ HOST = $(shell uname -s | tr '[:upper:]' '[:lower:]')
11+ ifeq ($(HOST),darwin)
12+ PLATFORM := macos
13+ CPUS := $(shell sysctl -n hw.ncpu)
14+ else
15+ PLATFORM := $(HOST)
16+ CPUS := $(shell nproc)
17+ endif
1418endif
1519
20+ # Speed up builds by using all available CPU cores
21+ MAKEFLAGS += -j$(CPUS )
22+
1623# Directories
1724SRC_DIR := src
1825LIB_DIR := libs
@@ -31,40 +38,50 @@ CFLAGS := -Wall -Wextra -fPIC -g -O2 -DQJS_BUILD_LIBC $(INCLUDES)
3138
3239# Platform-specific settings
3340ifeq ($(PLATFORM ) ,windows)
34- TARGET := $(DIST_DIR ) /js.dll
35- LDFLAGS := -shared
36- # Create .def file for Windows
37- DEF_FILE := $(BUILD_DIR ) /js.def
41+ TARGET := $(DIST_DIR)/js.dll
42+ LDFLAGS := -shared
43+ # Create .def file for Windows
44+ DEF_FILE := $(BUILD_DIR)/js.def
45+ STRIP = strip --strip-unneeded $@
3846else ifeq ($(PLATFORM),macos)
39- TARGET := $(DIST_DIR ) /js.dylib
40- LDFLAGS := -arch x86_64 -arch arm64 -dynamiclib -undefined dynamic_lookup
41- # macOS-specific flags
42- CFLAGS += -arch x86_64 -arch arm64
47+ TARGET := $(DIST_DIR)/js.dylib
48+ LDFLAGS := -arch x86_64 -arch arm64 -dynamiclib -undefined dynamic_lookup
49+ # macOS-specific flags
50+ CFLAGS += -arch x86_64 -arch arm64
51+ STRIP = strip -x -S $@
4352else ifeq ($(PLATFORM),android)
44- # Use Android NDK's Clang compiler, the user should set the CC
45- # example CC=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android26-clang
46- ifeq ($(filter %-clang,$(CC)),)
47- $(error "CC must be set to the Android NDK's Clang compiler")
48- endif
49- TARGET := $(DIST_DIR ) /js.so
50- LDFLAGS := -shared -lm
51- # Android-specific flags
52- CFLAGS += -D__ANDROID__
53+ ifndef ARCH # Set ARCH to find Android NDK's Clang compiler, the user should set the ARCH
54+ $(error "Android ARCH must be set to ARCH=x86_64 or ARCH=arm64-v8a")
55+ endif
56+ ifndef ANDROID_NDK # 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
57+ $(error "Android NDK must be set")
58+ endif
59+ BIN = $(ANDROID_NDK)/toolchains/llvm/prebuilt/$(HOST)-x86_64/bin
60+ ifneq (,$(filter $(ARCH),arm64 arm64-v8a))
61+ override ARCH := aarch64
62+ endif
63+ CC = $(BIN)/$(ARCH)-linux-android26-clang
64+ TARGET := $(DIST_DIR)/js.so
65+ LDFLAGS := -lm -shared
66+ STRIP = $(BIN)/llvm-strip --strip-unneeded $@
5367else ifeq ($(PLATFORM),ios)
54- TARGET := $(DIST_DIR ) /js.dylib
55- SDK := -isysroot $(shell xcrun --sdk iphoneos --show-sdk-path) -miphoneos-version-min=11.0
56- LDFLAGS := -dynamiclib $(SDK )
57- # iOS-specific flags
58- CFLAGS += -arch arm64 $(SDK )
59- else ifeq ($(PLATFORM),isim)
60- TARGET := $(DIST_DIR ) /js.dylib
61- SDK := -isysroot $(shell xcrun --sdk iphonesimulator --show-sdk-path) -miphonesimulator-version-min=11.0
62- LDFLAGS := -arch x86_64 -arch arm64 -dynamiclib $(SDK )
63- # iphonesimulator-specific flags
64- CFLAGS += -arch x86_64 -arch arm64 $(SDK )
68+ TARGET := $(DIST_DIR)/js.dylib
69+ SDK := -isysroot $(shell xcrun --sdk iphoneos --show-sdk-path) -miphoneos-version-min=11.0
70+ LDFLAGS := -dynamiclib $(SDK)
71+ # iOS-specific flags
72+ CFLAGS += -arch arm64 $(SDK)
73+ STRIP = strip -x -S $@
74+ else ifeq ($(PLATFORM),ios-sim)
75+ TARGET := $(DIST_DIR)/js.dylib
76+ SDK := -isysroot $(shell xcrun --sdk iphonesimulator --show-sdk-path) -miphonesimulator-version-min=11.0
77+ LDFLAGS := -arch x86_64 -arch arm64 -dynamiclib $(SDK)
78+ # iphonesimulator-specific flags
79+ CFLAGS += -arch x86_64 -arch arm64 $(SDK)
80+ STRIP = strip -x -S $@
6581else # linux
66- TARGET := $(DIST_DIR ) /js.so
67- LDFLAGS := -lm -shared
82+ TARGET := $(DIST_DIR)/js.so
83+ LDFLAGS := -lm -shared
84+ STRIP = strip --strip-unneeded $@
6885endif
6986
7087# Object files
@@ -83,6 +100,8 @@ ifeq ($(PLATFORM),windows)
83100 # Generate import library for Windows
84101 dlltool -D $@ -d $(DEF_FILE) -l $(DIST_DIR)/js.lib
85102endif
103+ # Strip debug symbols
104+ $(STRIP)
86105
87106# Compile source files
88107$(BUILD_DIR ) /% .o : $(SRC_DIR ) /% .c
@@ -138,25 +157,76 @@ test: $(TARGET) $(TEST_TARGET)
138157 sqlite3 " :memory:" -cmd " .bail on" " .load ./$<" " SELECT js_eval('console.log(\" hello, world\nToday is\" , new Date().toLocaleDateString())');"
139158 ./$(TEST_TARGET )
140159
160+ .NOTPARALLEL : % .dylib
161+ % .dylib :
162+ rm -rf $(BUILD_DIR ) && $(MAKE ) PLATFORM=$*
163+ mv $(DIST_DIR ) /js.dylib $(DIST_DIR ) /$@
164+
165+ define PLIST
166+ <?xml version=\"1.0\" encoding=\"UTF-8\"?>\
167+ <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\
168+ <plist version=\"1.0\">\
169+ <dict>\
170+ <key>CFBundleDevelopmentRegion</key>\
171+ <string>en</string>\
172+ <key>CFBundleExecutable</key>\
173+ <string>js</string>\
174+ <key>CFBundleIdentifier</key>\
175+ <string>ai.sqlite.js</string>\
176+ <key>CFBundleInfoDictionaryVersion</key>\
177+ <string>6.0</string>\
178+ <key>CFBundlePackageType</key>\
179+ <string>FMWK</string>\
180+ <key>CFBundleSignature</key>\
181+ <string>????</string>\
182+ <key>CFBundleVersion</key>\
183+ <string>$(shell make version) </string>\
184+ <key>CFBundleShortVersionString</key>\
185+ <string>$(shell make version) </string>\
186+ <key>MinimumOSVersion</key>\
187+ <string>11.0</string>\
188+ </dict>\
189+ </plist>
190+ endef
191+
192+ LIB_NAMES = ios.dylib ios-sim.dylib macos.dylib
193+ FMWK_NAMES = ios-arm64 ios-arm64_x86_64-simulator macos-arm64_x86_64
194+ $(DIST_DIR ) /% .xcframework : $(LIB_NAMES )
195+ @$(foreach i,1 2 3,\
196+ lib=$(word $(i ) ,$(LIB_NAMES ) ) ; \
197+ fmwk=$(word $(i ) ,$(FMWK_NAMES ) ) ; \
198+ mkdir -p $(DIST_DIR ) /$$ fmwk/js.framework; \
199+ printf " $( PLIST) " > $(DIST_DIR ) /$$ fmwk/js.framework/Info.plist; \
200+ mv $(DIST_DIR ) /$$ lib $(DIST_DIR ) /$$ fmwk/js.framework/js; \
201+ install_name_tool -id " @rpath/js.framework/js" $(DIST_DIR ) /$$ fmwk/js.framework/js; \
202+ )
203+ xcodebuild -create-xcframework $(foreach fmwk,$(FMWK_NAMES ) ,-framework $(DIST_DIR ) /$(fmwk ) /js.framework) -output $@
204+ rm -rf $(foreach fmwk,$(FMWK_NAMES ) ,$(DIST_DIR ) /$(fmwk ) )
205+
206+ xcframework : $(DIST_DIR ) /js.xcframework
207+
208+ version :
209+ @echo $(shell sed -n 's/^#define SQLITE_JS_VERSION[[:space:]]* "\([^"]* \) " .*/\1/p' src/sqlitejs.h)
210+
141211# Help message
142212help :
143213 @echo " SQLite JavaScript Extension Makefile"
144214 @echo " Usage:"
145- @echo " make [PLATFORM=platform] [target]"
215+ @echo " make [PLATFORM=platform] [ARCH=arch] [ANDROID_NDK= \$ $ANDROID_HOME /ndk/26.1.10909125 ] [target]"
146216 @echo " "
147217 @echo " Platforms:"
148218 @echo " linux (default on Linux)"
149219 @echo " macos (default on macOS)"
150220 @echo " windows (default on Windows)"
151- @echo " android (needs CC to be set to Android NDK's Clang compiler )"
221+ @echo " android (needs ARCH to be set to x86_64 or arm64-v8a and ANDROID_NDK to be set )"
152222 @echo " ios (only on macOS)"
153- @echo " isim (only on macOS)"
223+ @echo " ios-sim (only on macOS)"
154224 @echo " "
155225 @echo " Targets:"
156- @echo " all - Build the extension (default)"
157- @echo " clean - Remove built files"
158- @echo " install - Install the extension"
159- @echo " test - Test the extension"
160- @echo " help - Display this help message"
226+ @echo " all - Build the extension (default)"
227+ @echo " clean - Remove built files"
228+ @echo " install - Install the extension"
229+ @echo " test - Test the extension"
230+ @echo " help - Display this help message"
161231
162- .PHONY : all clean install test help
232+ .PHONY : all clean install test help version xcframework
0 commit comments