Skip to content

Commit ffe1fc4

Browse files
committed
Refactor Makefile and Workflow file
1 parent 18c5935 commit ffe1fc4

File tree

2 files changed

+176
-88
lines changed

2 files changed

+176
-88
lines changed

.github/workflows/release.yml

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,84 @@ permissions:
88
jobs:
99
build:
1010
runs-on: ${{ matrix.os }}
11-
name: ${{ matrix.name }}${{ matrix.arch && format('-{0}', matrix.arch) || '' }} build${{ matrix.arch != 'arm64-v8a' && matrix.name != 'isim' && matrix.name != 'ios' && ' + test' || ''}}
11+
container: ${{ matrix.container && matrix.container || '' }}
12+
name: ${{ matrix.name }}${{ matrix.arch && format('-{0}', matrix.arch) || '' }} build${{ matrix.arch != 'arm64-v8a' && matrix.name != 'ios-sim' && matrix.name != 'ios' && matrix.name != 'apple-xcframework' && ' + test' || ''}}
1213
timeout-minutes: 20
1314
strategy:
1415
fail-fast: false
1516
matrix:
1617
include:
17-
- os: macos-latest
18-
name: isim
19-
make: PLATFORM=isim
20-
- os: macos-latest
21-
name: ios
22-
make: PLATFORM=ios
23-
- os: ubuntu-latest
24-
arch: arm64-v8a
25-
name: android
26-
make:
27-
PLATFORM=android
28-
CC=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android26-clang
29-
- os: ubuntu-latest
30-
arch: x86_64
31-
name: android
32-
make:
33-
PLATFORM=android
34-
CC=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android26-clang
35-
sqlite-amalgamation-zip: https://sqlite.org/2025/sqlite-amalgamation-3490100.zip
36-
- os: ubuntu-latest
18+
- os: ubuntu-22.04
3719
arch: x86_64
3820
name: linux
39-
- os: LinuxARM64
21+
- os: ubuntu-22.04-arm
4022
arch: arm64
4123
name: linux
42-
- os: macos-latest
24+
- os: ubuntu-22.04
25+
arch: x86_64
26+
name: linux-musl
27+
container: alpine:latest
28+
- os: ubuntu-22.04-arm
29+
arch: arm64
30+
name: linux-musl
31+
- os: macos-15
4332
name: macos
44-
- os: windows-latest
33+
- os: windows-2022
4534
arch: x86_64
4635
name: windows
36+
- os: ubuntu-22.04
37+
arch: arm64-v8a
38+
name: android
39+
make: PLATFORM=android ARCH=arm64-v8a
40+
- os: ubuntu-22.04
41+
arch: x86_64
42+
name: android
43+
make: PLATFORM=android ARCH=x86_64
44+
sqlite-amalgamation-zip: https://sqlite.org/2025/sqlite-amalgamation-3490100.zip
45+
- os: macos-15
46+
name: ios
47+
make: PLATFORM=ios
48+
- os: macos-15
49+
name: ios-sim
50+
make: PLATFORM=ios-sim
51+
- os: macos-15
52+
name: apple-xcframework
53+
make: xcframework
4754

4855
defaults:
4956
run:
50-
shell: bash
51-
env:
52-
MAKEFLAGS: -j 8
57+
shell: ${{ matrix.container && 'sh' || 'bash' }}
5358

5459
steps:
5560

5661
- uses: actions/checkout@v4.2.2
5762

58-
- name: build sqlite-js
59-
run: make ${{ matrix.make && matrix.make || ''}}
60-
61-
- name: windows install sqlite3
62-
if: matrix.os == 'windows-latest'
63+
- name: windows install dependencies
64+
if: matrix.name == 'windows'
6365
run: choco install sqlite -y
6466

65-
- name: macos install sqlite3 without SQLITE_OMIT_LOAD_EXTENSION
67+
- name: macos install dependencies
6668
if: matrix.name == 'macos'
6769
run: brew link sqlite --force
6870

71+
- name: linux-musl x86_64 install dependencies
72+
if: matrix.name == 'linux-musl' && matrix.arch == 'x86_64'
73+
run: apk update && apk add --no-cache gcc make sqlite musl-dev linux-headers
74+
75+
- name: linux-musl arm64 setup container
76+
if: matrix.name == 'linux-musl' && matrix.arch == 'arm64'
77+
run: |
78+
docker run -d --name alpine \
79+
--platform linux/arm64 \
80+
-v ${{ github.workspace }}:/workspace \
81+
-w /workspace \
82+
alpine:latest \
83+
tail -f /dev/null
84+
docker exec alpine sh -c "apk update && apk add --no-cache gcc make sqlite musl-dev linux-headers"
85+
86+
- name: build sqlite-js
87+
run: ${{ matrix.name == 'linux-musl' && matrix.arch == 'arm64' && 'docker exec alpine' || '' }} make extension ${{ matrix.make && matrix.make || ''}}
88+
6989
- name: android setup test environment
7090
if: matrix.name == 'android' && matrix.arch != 'arm64-v8a'
7191
run: |
@@ -80,16 +100,16 @@ jobs:
80100
curl -O ${{ matrix.sqlite-amalgamation-zip }}
81101
unzip sqlite-amalgamation-*.zip
82102
export ${{ matrix.make }}
83-
$CC sqlite-amalgamation-*/shell.c sqlite-amalgamation-*/sqlite3.c -o sqlite3 -ldl
103+
$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/${{ matrix.arch }}-linux-android26-clang sqlite-amalgamation-*/shell.c sqlite-amalgamation-*/sqlite3.c -o sqlite3 -ldl
84104
rm -rf sqlite-amalgamation-*.zip sqlite-amalgamation-*
85105
echo "::endgroup::"
86106
87107
echo "::group::prepare the test script"
88-
make test CC=$CC PLATFORM=$PLATFORM || echo "It should fail. Running remaining commands in the emulator"
108+
make test PLATFORM=$PLATFORM ARCH=$ARCH || echo "It should fail. Running remaining commands in the emulator"
89109
cat > commands.sh << EOF
90110
mv -f /data/local/tmp/sqlite3 /system/xbin
91111
cd /data/local/tmp
92-
$(make test CC=$CC PLATFORM=$PLATFORM -n)
112+
$(make test PLATFORM=$PLATFORM ARCH=$ARCH -n)
93113
EOF
94114
echo "::endgroup::"
95115
@@ -106,8 +126,8 @@ jobs:
106126
adb shell "sh /data/local/tmp/commands.sh"
107127
108128
- name: test sqlite-js
109-
if: matrix.name == 'linux' || matrix.name == 'macos' || matrix.name == 'windows'
110-
run: make test
129+
if: contains(matrix.name, 'linux') || matrix.name == 'windows' || matrix.name == 'macos'
130+
run: ${{ matrix.name == 'linux-musl' && matrix.arch == 'arm64' && 'docker exec alpine' || '' }} make test ${{ matrix.make && matrix.make || ''}}
111131

112132
- uses: actions/upload-artifact@v4.6.2
113133
with:
@@ -116,7 +136,7 @@ jobs:
116136
if-no-files-found: error
117137

118138
release:
119-
runs-on: ubuntu-latest
139+
runs-on: ubuntu-22.04
120140
name: release
121141
needs: build
122142
if: github.ref == 'refs/heads/main'
@@ -154,8 +174,7 @@ jobs:
154174
for folder in "artifacts"/*; do
155175
if [ -d "$folder" ]; then
156176
name=$(basename "$folder")
157-
zip -jq "${name}-${{ steps.tag.outputs.version }}.zip" "$folder"/*
158-
tar -cJf "${name}-${{ steps.tag.outputs.version }}.tar.xz" -C "$folder" .
177+
(cd "$folder" && zip -rq "../../${name}-${{ steps.tag.outputs.version }}.zip" .)
159178
tar -czf "${name}-${{ steps.tag.outputs.version }}.tar.gz" -C "$folder" .
160179
fi
161180
done
@@ -167,6 +186,5 @@ jobs:
167186
tag_name: ${{ steps.tag.outputs.version }}
168187
files: |
169188
js-*-${{ steps.tag.outputs.version }}.zip
170-
js-*-${{ steps.tag.outputs.version }}.tar.xz
171189
js-*-${{ steps.tag.outputs.version }}.tar.gz
172190
make_latest: true

Makefile

Lines changed: 116 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33

44
# Set default platform if not specified
55
ifeq ($(OS),Windows_NT)
6-
PLATFORM := windows
6+
PLATFORM := windows
7+
HOST := windows
8+
CPUS := $(shell powershell -Command "[Environment]::ProcessorCount")
79
else
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
1418
endif
1519

20+
# Speed up builds by using all available CPU cores
21+
MAKEFLAGS += -j$(CPUS)
22+
1623
# Directories
1724
SRC_DIR := src
1825
LIB_DIR := libs
@@ -31,40 +38,50 @@ CFLAGS := -Wall -Wextra -fPIC -g -O2 -DQJS_BUILD_LIBC $(INCLUDES)
3138

3239
# Platform-specific settings
3340
ifeq ($(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 $@
3846
else 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 $@
4352
else 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 $@
5367
else 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 $@
6581
else # 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 $@
6885
endif
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
85102
endif
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
142212
help:
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

Comments
 (0)