Skip to content

Commit fdc0db2

Browse files
committed
Added sqlite amalgamation and ability to use system-provided SQLite
1 parent 5674495 commit fdc0db2

File tree

11 files changed

+243715
-161
lines changed

11 files changed

+243715
-161
lines changed

.gitignore

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
.rebar3
2-
_*
3-
.eunit
4-
*.o
5-
*.beam
6-
*.plt
7-
*.swp
8-
*.swo
9-
.erlang.cookie
10-
ebin
11-
log
12-
erl_crash.dump
13-
.rebar
14-
logs
151
_build
2+
erl_crash.dump
3+
rebar3.crashdump
164
.idea
175
*.iml
18-
rebar3.crashdump
19-
*~
206
.vscode
7+
8+

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22
sqlite3 NIF implementation.
33

44
## Not implemented
5-
* Types mapping
5+
* tests!
66
* performance (benchmarking) - especially in the memory allocation
7-
* performance - concurrent access to the same connection/statement
7+
* performance - concurrent access to the same connection/statement (locking efficiency)
88
* performance - see what can run on normal scheduler (yielding?)
99
* memory leaks tests
10+
* tests for "garbage collector process" started with on_load, and the process shutdown
11+
* better/proper monitoring for ROWID changes
12+
* documentation
13+
* OS support (Linux, MacOS, Windows, FreeBSD)
1014

1115
Extra Features
1216
* Online Backup support
1317
* Snapshot support
1418
* Commit and Rollback hooks support
1519
* Database serialisation (sqlite3_serialize)
20+
* Explicit Step/Bind APIs
21+
* More type conversions
1622

1723
## Cleanup
18-
* debug settings from c_src Makefile
19-
* sqlite amalgamation (provide with a NIF)
20-
* debug scripts (place somewhere?)
24+
* cleanup code from "memory assertions" and test errors (out of memory etc)
2125

2226

2327
## Extended error structure (cause)
@@ -30,20 +34,17 @@ Extra Features
3034
}.
3135
```
3236

33-
## Build
34-
-----
35-
36-
$ rebar3 compile
37-
3837
## Erlang to sqlite data type mapping
3938
Following primitive types in Erlang are mapped to corresponding sqlite types:
4039
* binary() <-> sqlite3 blob
4140
* string() <-> sqlite3 text
42-
* atom() <-> sqlite text
4341
* integer() <-> integer
4442
* float() <-> float
4543
* undefined <-> null
46-
* map() <-> JSON
44+
45+
Potentially, in the future:
46+
* atom() <-> text?
47+
* map() <-> JSON?
4748

4849
## Failing to load
4950

c_src/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.o

c_src/Makefile

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,70 @@
1-
# Based on c_src.mk from erlang.mk by Loic Hoguin <[email protected]>
2-
3-
CURDIR := $(shell pwd)
4-
BASEDIR := $(abspath $(CURDIR)/..)
5-
6-
PROJECT ?= $(notdir $(BASEDIR))
7-
PROJECT := $(strip $(PROJECT))
8-
9-
ERTS_INCLUDE_DIR ?= $(shell erl -noshell -eval "io:format(\"~ts/erts-~ts/include/\", [code:root_dir(), erlang:system_info(version)])." -s init stop)
10-
ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -eval "io:format(\"~ts\", [code:lib_dir(erl_interface, include)])." -s init stop)
11-
ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -eval "io:format(\"~ts\", [code:lib_dir(erl_interface, lib)])." -s init stop)
12-
13-
C_SRC_DIR = $(CURDIR)
14-
C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so
15-
16-
# System type and C compiler/flags.
17-
18-
UNAME_SYS := $(shell uname -s)
19-
ifeq ($(UNAME_SYS), Darwin)
20-
CC ?= cc
21-
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
22-
CXXFLAGS ?= -O3 -finline-functions -Wall
23-
LDFLAGS ?= -flat_namespace -undefined suppress
24-
else ifeq ($(UNAME_SYS), FreeBSD)
25-
CC ?= cc
26-
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
27-
CXXFLAGS ?= -O3 -finline-functions -Wall
28-
else ifeq ($(UNAME_SYS), Linux)
29-
CC ?= gcc
30-
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
31-
CXXFLAGS ?= -O3 -finline-functions -Wall
1+
# General variables
2+
C_SRC_DIR := $(shell pwd)
3+
PROJECT_DIR := $(abspath $(C_SRC_DIR)/..)
4+
5+
# Figure out OS-specific details: library extension
6+
ifeq ($(OS),Windows_NT)
7+
TARGET := $(PROJECT_DIR)/priv/sqlite.dll
8+
else
9+
TARGET := $(PROJECT_DIR)/priv/sqlite.so
10+
UNAME_SYS := $(shell uname -s)
11+
12+
ifeq ($(UNAME_SYS), Darwin)
13+
CC ?= cc
14+
LDFLAGS ?= -flat_namespace -undefined suppress
15+
else ifeq ($(UNAME_SYS), FreeBSD)
16+
CC ?= cc
17+
else ifeq ($(UNAME_SYS), Linux)
18+
CC ?= gcc
19+
endif
3220
endif
3321

34-
CFLAGS = -g -O1 -finline-functions -Wall -Wmissing-prototypes -std=c99 -fno-omit-frame-pointer
35-
CXXFLAGS = -g -O1 -finline-functions -Wall -Wmissing-prototypes -std=c99 -fno-omit-frame-pointer
22+
# Figure out Erlang Library paths.
23+
# Do a single call to "erl" printing all the information, and split it in words later.
24+
# This saves quite an amount of time spawning extra BEAMs
25+
DIRS = $(shell erl -noshell -eval "io:format(\"~ts/erts-~ts/include/~n~ts~n~ts\", [code:root_dir(), erlang:system_info(version), code:lib_dir(erl_interface, include), code:lib_dir(erl_interface, lib)])." -s init stop)
3626

37-
CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
38-
CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
27+
ERTS_INCLUDE_DIR ?= $(word 1, $(DIRS))
28+
ERL_INTERFACE_INCLUDE_DIR ?= $(word 2, $(DIRS))
29+
ERL_INTERFACE_LIB_DIR ?= $(word 3, $(DIRS))
3930

31+
CFLAGS += -std=c99 -finline-functions -Wall -Wmissing-prototypes -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
4032
LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lei -lsqlite3
41-
LDFLAGS += -shared
4233

43-
# Verbosity.
44-
45-
c_verbose_0 = @echo " C " $(?F);
46-
c_verbose = $(c_verbose_$(V))
34+
# Set up DEBUG flags if DEBUG environment variable is set
35+
# The "variable trick" below allows using "DEBUG=1"
36+
ifdef DEBUG
37+
CFLAGS += -O0 -g3 -fno-omit-frame-pointer -DSQLITE_DEBUG
38+
else
39+
CFLAGS += -O3
40+
endif
4741

48-
cpp_verbose_0 = @echo " CPP " $(?F);
49-
cpp_verbose = $(cpp_verbose_$(V))
42+
# Always build a shared binary (so, dll)
43+
LDFLAGS += -shared
5044

51-
link_verbose_0 = @echo " LD " $(@F);
52-
link_verbose = $(link_verbose_$(V))
45+
# Sources and object files in C_SRC
46+
#SRC := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" \))
47+
ifdef USE_SYSTEM_SQLITE
48+
SRC := ${C_SRC_DIR}/sqlite_nif.c
49+
CFLAGS += -DUSE_SYSTEM_SQLITE -DNO_SQLITE3_ERROR_OFFSET
50+
else
51+
# SQLite amalgamation is a part of this project, but it's possible
52+
# that someone wants to use the system-provided one
53+
SRC := ${C_SRC_DIR}/sqlite_nif.c ${C_SRC_DIR}/sqlite3.c
54+
endif
5355

54-
SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
55-
OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))
56+
OBJ = $(addsuffix .o, $(basename $(SRC)))
5657

57-
COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
58-
COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
58+
# Disable implicit rules, they aren't helpful for this Makefile, polluting debug output
59+
.SUFFIXES:
5960

60-
$(C_SRC_OUTPUT): $(OBJECTS)
61-
@mkdir -p $(BASEDIR)/priv/
62-
$(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT)
61+
$(TARGET): $(OBJ)
62+
$(CC) $(OBJ) $(LDFLAGS) $(LDLIBS) -o $(TARGET)
6363

6464
%.o: %.c
65-
$(COMPILE_C) $(OUTPUT_OPTION) $<
66-
67-
%.o: %.cc
68-
$(COMPILE_CPP) $(OUTPUT_OPTION) $<
69-
70-
%.o: %.C
71-
$(COMPILE_CPP) $(OUTPUT_OPTION) $<
65+
$(CC) $(CFLAGS) -o $@ -c $<
7266

73-
%.o: %.cpp
74-
$(COMPILE_CPP) $(OUTPUT_OPTION) $<
67+
.PHONY: clean
7568

7669
clean:
77-
@rm -f $(C_SRC_OUTPUT) $(OBJECTS)
70+
@rm -f $(OBJ) $(TARGET)

0 commit comments

Comments
 (0)