|
| 1 | +# Makefile for SQLite JavaScript Extension |
| 2 | +# Supports compilation for Linux, macOS, and Windows |
| 3 | + |
| 4 | +# Set default platform if not specified |
| 5 | +ifeq ($(OS),Windows_NT) |
| 6 | + PLATFORM := windows |
| 7 | +else |
| 8 | + UNAME_S := $(shell uname -s) |
| 9 | + ifeq ($(UNAME_S),Darwin) |
| 10 | + PLATFORM := macos |
| 11 | + else |
| 12 | + PLATFORM := linux |
| 13 | + endif |
| 14 | +endif |
| 15 | + |
| 16 | +# Directories |
| 17 | +SRC_DIR := src |
| 18 | +LIB_DIR := libs |
| 19 | +BUILD_DIR := build |
| 20 | +DIST_DIR := dist |
| 21 | + |
| 22 | +# Source files |
| 23 | +SRC_FILES := $(SRC_DIR)/sqlitejs.c $(LIB_DIR)/quickjs.c |
| 24 | + |
| 25 | +# Include directories |
| 26 | +INCLUDES := -I$(SRC_DIR) -I$(LIB_DIR) |
| 27 | + |
| 28 | +# Compiler and flags |
| 29 | +CC := gcc |
| 30 | +CFLAGS := -Wall -Wextra -fPIC -g -O2 $(INCLUDES) |
| 31 | + |
| 32 | +# Platform-specific settings |
| 33 | +ifeq ($(PLATFORM),windows) |
| 34 | + TARGET := $(DIST_DIR)/js.dll |
| 35 | + LDFLAGS := -shared |
| 36 | + CC := gcc |
| 37 | + # Windows-specific flags |
| 38 | + CFLAGS += -D_WIN32 |
| 39 | + # Create .def file for Windows |
| 40 | + DEF_FILE := $(BUILD_DIR)/js.def |
| 41 | +else ifeq ($(PLATFORM),macos) |
| 42 | + TARGET := $(DIST_DIR)/js.dylib |
| 43 | + LDFLAGS := -dynamiclib -undefined dynamic_lookup |
| 44 | + # macOS-specific flags |
| 45 | + CFLAGS += -arch x86_64 -arch arm64 |
| 46 | +else # linux |
| 47 | + TARGET := $(DIST_DIR)/js.so |
| 48 | + LDFLAGS := -shared |
| 49 | + # Linux-specific flags |
| 50 | + CFLAGS += -fvisibility=hidden |
| 51 | +endif |
| 52 | + |
| 53 | +# Object files |
| 54 | +OBJ_FILES := $(patsubst %.c,$(BUILD_DIR)/%.o,$(notdir $(SRC_FILES))) |
| 55 | + |
| 56 | +# Make sure the build and dist directories exist |
| 57 | +$(shell mkdir -p $(BUILD_DIR) $(DIST_DIR)) |
| 58 | + |
| 59 | +# Main target |
| 60 | +all: $(TARGET) |
| 61 | + |
| 62 | +# Link the final target |
| 63 | +$(TARGET): $(OBJ_FILES) |
| 64 | + $(CC) $(LDFLAGS) -o $@ $^ |
| 65 | +ifeq ($(PLATFORM),windows) |
| 66 | + # Generate import library for Windows |
| 67 | + dlltool -D $@ -d $(DEF_FILE) -l $(DIST_DIR)/js.lib |
| 68 | +endif |
| 69 | + |
| 70 | +# Compile source files |
| 71 | +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c |
| 72 | + $(CC) $(CFLAGS) -c -o $@ $< |
| 73 | + |
| 74 | +$(BUILD_DIR)/%.o: $(LIB_DIR)/%.c |
| 75 | + $(CC) $(CFLAGS) -c -o $@ $< |
| 76 | + |
| 77 | +# Windows .def file generation |
| 78 | +$(DEF_FILE): |
| 79 | +ifeq ($(PLATFORM),windows) |
| 80 | + @echo "LIBRARY js.dll" > $@ |
| 81 | + @echo "EXPORTS" >> $@ |
| 82 | + @echo " sqlite3_js_init" >> $@ |
| 83 | +endif |
| 84 | + |
| 85 | +# Clean up |
| 86 | +clean: |
| 87 | + rm -rf $(BUILD_DIR)/* $(DIST_DIR)/* |
| 88 | + |
| 89 | +# Install the extension (adjust paths as needed) |
| 90 | +install: $(TARGET) |
| 91 | +ifeq ($(PLATFORM),windows) |
| 92 | + mkdir -p $(DESTDIR)/usr/local/lib/sqlite3 |
| 93 | + cp $(TARGET) $(DESTDIR)/usr/local/lib/sqlite3/ |
| 94 | + cp $(DIST_DIR)/js.lib $(DESTDIR)/usr/local/lib/ |
| 95 | +else ifeq ($(PLATFORM),macos) |
| 96 | + mkdir -p $(DESTDIR)/usr/local/lib/sqlite3 |
| 97 | + cp $(TARGET) $(DESTDIR)/usr/local/lib/sqlite3/ |
| 98 | +else # linux |
| 99 | + mkdir -p $(DESTDIR)/usr/local/lib/sqlite3 |
| 100 | + cp $(TARGET) $(DESTDIR)/usr/local/lib/sqlite3/ |
| 101 | +endif |
| 102 | + |
| 103 | +# Testing the extension |
| 104 | +test: $(TARGET) |
| 105 | + sqlite3 ":memory:" -cmd ".load ./$(TARGET)" "SELECT 1;" |
| 106 | + |
| 107 | +# Help message |
| 108 | +help: |
| 109 | + @echo "SQLite JavaScript Extension Makefile" |
| 110 | + @echo "Usage:" |
| 111 | + @echo " make [PLATFORM=platform] [target]" |
| 112 | + @echo "" |
| 113 | + @echo "Platforms:" |
| 114 | + @echo " linux (default on Linux)" |
| 115 | + @echo " macos (default on macOS)" |
| 116 | + @echo " windows (default on Windows)" |
| 117 | + @echo "" |
| 118 | + @echo "Targets:" |
| 119 | + @echo " all - Build the extension (default)" |
| 120 | + @echo " clean - Remove built files" |
| 121 | + @echo " install - Install the extension" |
| 122 | + @echo " test - Test the extension" |
| 123 | + @echo " help - Display this help message" |
| 124 | + |
| 125 | +.PHONY: all clean install test help |
0 commit comments