|
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 |
32 | 20 | endif
|
33 | 21 |
|
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) |
36 | 26 |
|
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)) |
39 | 30 |
|
| 31 | +CFLAGS += -std=c99 -finline-functions -Wall -Wmissing-prototypes -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) |
40 | 32 | LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lei -lsqlite3
|
41 |
| -LDFLAGS += -shared |
42 | 33 |
|
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 |
47 | 41 |
|
48 |
| -cpp_verbose_0 = @echo " CPP " $(?F); |
49 |
| -cpp_verbose = $(cpp_verbose_$(V)) |
| 42 | +# Always build a shared binary (so, dll) |
| 43 | +LDFLAGS += -shared |
50 | 44 |
|
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 |
53 | 55 |
|
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))) |
56 | 57 |
|
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: |
59 | 60 |
|
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) |
63 | 63 |
|
64 | 64 | %.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 $< |
72 | 66 |
|
73 |
| -%.o: %.cpp |
74 |
| - $(COMPILE_CPP) $(OUTPUT_OPTION) $< |
| 67 | +.PHONY: clean |
75 | 68 |
|
76 | 69 | clean:
|
77 |
| - @rm -f $(C_SRC_OUTPUT) $(OBJECTS) |
| 70 | + @rm -f $(OBJ) $(TARGET) |
0 commit comments