Skip to content

Commit

Permalink
Put NIF build products under the _build directory
Browse files Browse the repository at this point in the history
The Makefile was storing build products in the source directory. This
causes issues on Nerves when switching target processors since the NIF
won't be built for the right architecture and the Makefile won't know to
rebuild it. Building under the _build directory fixes this since there's
a different subdirectory for every target processor (this uses the mix
targets feature).

This also addresses a source of confusion on Apple M1 Macs where you can
switch between Intel and M1 modes. Since mix targets aren't used, you
still need to be aware that you need to build clean when switching
modes. It seems less confusing that building clean involves deleting
files from `_build` rather both `_build` and `deps`.

Notes about this change:

1. The `Makefile` now depends on environment variables set by
   `elixir_make` for the location of the `_build` directory and `erl_nif.h`.
   This means that calling `make` to only build the NIF won't work. See
   next commit.
2. This change works with Nerves. The Nerves infrastructure uses the
   same `elixir_make` environment variables, so the `erl_nif.h` include
   directory will come from Erlang source directory that matches the
   Nerves target. This didn't happen before, but `erl_nif.h` must not
   change enough between Erlang versions to matter. (i.e., it was lucky)
  • Loading branch information
fhunleth committed Jan 5, 2021
1 parent 1cca197 commit ab40423
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CFLAGS = -g -O3 -Wall -Wno-format-truncation

ERLANG_PATH = $(shell erl -eval 'io:format("~s", [lists:concat([code:root_dir(), "/erts-", erlang:system_info(version), "/include"])])' -s init stop -noshell)
CFLAGS += -I"$(ERLANG_PATH)"
CFLAGS += -I"$(ERTS_INCLUDE_DIR)"
CFLAGS += -Ic_src

LIB_NAME = priv/bcrypt_nif.so
PRIV_DIR = $(MIX_APP_PATH)/priv
LIB_NAME = $(PRIV_DIR)/bcrypt_nif.so
ifneq ($(CROSSCOMPILE),)
# crosscompiling
CFLAGS += -fPIC
Expand All @@ -23,12 +23,14 @@ NIF_SRC=\
c_src/bcrypt_nif.c\
c_src/blowfish.c

all: $(LIB_NAME)
all: $(PRIV_DIR) $(LIB_NAME)

$(LIB_NAME): $(NIF_SRC)
mkdir -p priv
$(CC) $(CFLAGS) -shared $(LDFLAGS) $^ -o $@

$(PRIV_DIR):
mkdir -p $@

clean:
rm -f $(LIB_NAME)

Expand Down

0 comments on commit ab40423

Please # to comment.