elixir-sqlite / sqlitex

An Elixir wrapper around esqlite. Allows access to sqlite3 databases.
https://hex.pm/packages/sqlitex
120 stars 35 forks source link

Elixir 1.8 + Nerves issues #75

Open ConnorRigby opened 5 years ago

ConnorRigby commented 5 years ago

With the introduction of Elixir 1.8, Mix.target() was added. This had the side effect of breaking packages that build C code that is not built by an updated version of elixir_make including this project's dependency on esqlite. esqlite is built by pc (port compiler, a rebar3 plugin). I believe it is unreasonable to ask Rebar to support the Elixir Mix.target(), but any thing that needs to be cross compiled is now broken.

I've drafted up one solution that i am incredibly unhappy with.

1) add {:elixir_make, "~> 0.5", runtime: false} to the deps 2) add a Makefile that basically recompiles the same assets that pc has already emitted:

# Set Erlang-specific compile and linker flags
ERL_CFLAGS ?= -I$(ERL_EI_INCLUDE_DIR)
ERL_LDFLAGS ?= -L$(ERL_EI_LIBDIR) -lei

CFLAGS += -fPIC --std=c11
LDFLAGS += -fPIC -shared

ifeq ($(ERL_EI_INCLUDE_DIR),)
$(warning ERL_EI_INCLUDE_DIR not set. Invoke via mix)
endif

ESQLITE_SRC = $(MIX_DEPS_PATH)/esqlite/c_src
ESQLITE_BUILD = $(MIX_BUILD_PATH)/lib/esqlite/obj
ESQLITE_PREFIX = $(MIX_BUILD_PATH)/lib/esqlite/priv

.PHONY: all clean

SQLITE_CFLAGS := -DSQLITE_THREADSAFE=1 \
-DSQLITE_USE_URI \
-DSQLITE_ENABLE_FTS3 \
-DSQLITE_ENABLE_FTS3_PARENTHESIS

all: $(ESQLITE_BUILD) \
        $(ESQLITE_PREFIX) \
        $(ESQLITE_PREFIX)/esqlite3_nif.so

clean:
    $(RM) $(ESQLITE_PREFIX)/*.so

## ESQLITE NIF HACK

$(ESQLITE_PREFIX)/esqlite3_nif.so: $(ESQLITE_BUILD)/sqlite3.o $(ESQLITE_BUILD)/queue.o $(ESQLITE_BUILD)/esqlite3_nif.o
    $(CC) -o $@ $(ERL_LDFLAGS) $(LDFLAGS) $^

$(ESQLITE_BUILD)/esqlite3_nif.o: $(ESQLITE_SRC)/esqlite3_nif.c
    $(CC) -c $(ERL_CFLAGS) $(CFLAGS) $(SQLITE_CFLAGS) -o $@ $<

$(ESQLITE_BUILD)/queue.o: $(ESQLITE_SRC)/queue.c
    $(CC) -c $(ERL_CFLAGS) $(CFLAGS) $(SQLITE_CFLAGS) -o $@ $<

$(ESQLITE_BUILD)/sqlite3.o: $(ESQLITE_SRC)/sqlite3.c
    $(CC) -c $(CFLAGS) $(SQLITE_CFLAGS) -o $@ $<

## DIRECTORIES

$(ESQLITE_BUILD):
    mkdir -p $(ESQLITE_BUILD)

$(ESQLITE_PREFIX):
    mkdir -p $(ESQLITE_PREFIX)

This has 1 positive side effect of allowing us to control the compiler options to sqlite independently of esqlite. This may or may not be a good thing.

There is another potential solution in #53 on esqlite

laibulle commented 5 years ago

As a workaround I was able to run sqlite_ecto2 on nerves with by fixing sqlitex version to

{:sqlitex, "1.5.1"},

it will use sqlite 0.3.0