Skip to content

Commit

Permalink
build against sqlite amalgamation
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Oct 22, 2024
1 parent eab3955 commit 8104c8c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 31 deletions.
25 changes: 17 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
EXT = .so
SQLITE_VERSION ?= version-3.46.1
CFLAGS ?= -I deps/$(SQLITE_VERSION)/ext/fts5 -Os -Wall -Wextra -Werror -Wno-error=type-limits

SQLITE_TARBALL_URL = https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=${SQLITE_VERSION}
SQLITE_SRC = deps/$(SQLITE_VERSION)/src
SQLITE_AMALGAMATION_URL = https://sqlite.org/2024/sqlite-amalgamation-3460100.zip
SQLITE_AMALGAMATION_PATH = deps/sqlite-amalgamation-3460100

CONDITIONAL_CFLAGS =
CFLAGS ?= -Ideps/$(SQLITE_VERSION)/ext/fts5 -I$(SQLITE_AMALGAMATION_PATH) -Os -Wall -Wextra
CONDITIONAL_CFLAGS = -lm

ifeq ($(OS),Windows_NT)
EXT = .dll
Expand All @@ -19,30 +21,37 @@ endif

.PHONY: all clean test

all: $(SQLITE_SRC)/sqlite3ext.h better-trigram$(EXT)
all: better-trigram$(EXT)

clean:
rm -rf deps
rm -f better-trigram$(EXT)
rm -rf better-trigram$(EXT).dSYM
rm -rf fts5$(EXT)

$(SQLITE_SRC)/sqlite3ext.h:
$(SQLITE_SRC):
mkdir -p deps/$(SQLITE_VERSION)
curl -LsS $(SQLITE_TARBALL_URL) | tar -xzf - -C deps/$(SQLITE_VERSION)/ --strip-components=1

better-trigram$(EXT): better-trigram.c
$(CC) $(CFLAGS) $(CONDITIONAL_CFLAGS) -g -shared -fPIC -o $@ $<
$(SQLITE_AMALGAMATION_PATH):
@echo Downloading SQLite amalgamation...
wget -q $(SQLITE_AMALGAMATION_URL) -O sqlite.zip
@echo Extracting SQLite amalgamation...
unzip sqlite.zip -d deps/
rm -f sqlite.zip

better-trigram$(EXT): $(SQLITE_SRC) $(SQLITE_AMALGAMATION_PATH)
$(CC) $(CFLAGS) $(CONDITIONAL_CFLAGS) -shared -fPIC -o $@ better-trigram.c

fts5$(EXT): SHELL := /bin/bash -e
fts5$(EXT): $(SQLITE_SRC)/sqlite3ext.h
fts5$(EXT): $(SQLITE_SRC) $(SQLITE_AMALGAMATION_PATH)
dir=deps/$(SQLITE_VERSION) \
cwd=$$(pwd); \
lemon $$dir/ext/fts5/fts5parse.y; \
cd $$dir/ext/fts5; \
tclsh $$cwd/$$dir/ext/fts5/tool/mkfts5c.tcl; \
cd $$cwd; \
$(CC) $(CONDITIONAL_CFLAGS) -DSQLITE_TEST -g -shared -fPIC $$dir/ext/fts5/fts5.c -o $@; \
$(CC) $(CFLAGS) $(CONDITIONAL_CFLAGS) -DSQLITE_TEST -shared -fPIC -o $@ $$dir/ext/fts5/fts5.c; \

test: fts5$(EXT) better-trigram$(EXT)
bun test
36 changes: 13 additions & 23 deletions better-trigram.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@
**
*/

#include "better-trigram.h"
#include "tokenizer.c"
#include <stdio.h>
#include <string.h>

#ifdef SQLITE_OMIT_LOAD_EXTENSION
#define SQLITE_OMIT_LOAD_EXTENSION_PREINCLUDE
#endif

#ifndef SQLITE_CORE
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1

#if defined(SQLITE_OMIT_LOAD_EXTENSION) && \
!defined(SQLITE_OMIT_LOAD_EXTENSION_PREINCLUDE)
/* note: if you are on macOS - do not use included SQLite sqlite3ext.h */
#error "The sqlite3ext.h header defines SQLITE_OMIT_LOAD_EXTENSION"
#else
#include "sqlite3.h"
#endif

#define UNUSED_PARAM(x) (void)(x)
Expand Down Expand Up @@ -140,22 +135,17 @@ static int fts5BetterTrigramInit(sqlite3 *db) {
}
}

#ifdef _WIN32
__declspec(dllexport)
#endif

#ifndef SQLITE_CORE
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_bettertrigram_init(sqlite3 *db, char **error, const sqlite3_api_routines *api) {
SQLITE_EXTENSION_INIT2(api);
UNUSED_PARAM(error);

#ifdef SQLITE_CORE
SQLITE_PRIVATE int sqlite3Fts5BetterTrigramInit(sqlite3 *db) {
return fts5BetterTrigramInit(db);
}
#else
SQLITE_PRIVATE int sqlite3Fts5BetterTrigramInit(sqlite3 *db) {
SQLITE_BETTER_TRIGRAM_API int
sqlite3_bettertrigram_init(sqlite3 *db, char **error,
const sqlite3_api_routines *api) {
SQLITE_EXTENSION_INIT2(api);
UNUSED_PARAM(error);

return fts5BetterTrigramInit(db);
}
#endif
#endif
41 changes: 41 additions & 0 deletions better-trigram.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef SQLITE_BETTER_TRIGRAM_H
#define SQLITE_BETTER_TRIGRAM_H

#ifndef SQLITE_CORE
#include "sqlite3ext.h"
#else
#include "sqlite3.h"
#endif

#ifndef SQLITE_PRIVATE
#define SQLITE_PRIVATE static
#endif

#ifdef SQLITE_BETTER_TRIGRAM_STATIC
#define SQLITE_BETTER_TRIGRAM_API
#else
#ifdef _WIN32
#define SQLITE_BETTER_TRIGRAM_API __declspec(dllexport)
#else
#define SQLITE_BETTER_TRIGRAM_API
#endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

#ifdef SQLITE_CORE
SQLITE_PRIVATE int sqlite3Fts5BetterTrigramInit(sqlite3 *db);
#else
SQLITE_BETTER_TRIGRAM_API int
sqlite3_bettertrigram_init(sqlite3 *db, char **pzErrMsg,
const sqlite3_api_routines *pApi);

#endif

#ifdef __cplusplus
} /* end of the 'extern "C"' block */
#endif

#endif /* ifndef SQLITE_BETTER_TRIGRAM_H */

0 comments on commit 8104c8c

Please sign in to comment.