Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 34 additions & 48 deletions c_src/Makefile
Original file line number Diff line number Diff line change
@@ -1,68 +1,54 @@
CURDIR := $(shell pwd)
BASEDIR := $(abspath $(CURDIR)/..)

ERL ?= erl
PKG_CONFIG ?= pkg-config

# System specific C compiler/flags.
UNAME_SYS := $(shell uname -s)
ifeq ($(UNAME_SYS), Darwin)
DARWIN_ARCH := $(shell uname -m)
CC ?= cc
CFLAGS ?= -O2 -arch $(DARWIN_ARCH) -finline-functions -Wall -Wmissing-prototypes
LDFLAGS ?= -arch $(DARWIN_ARCH) -flat_namespace -undefined dynamic_lookup
else ifeq ($(UNAME_SYS), FreeBSD)
CC ?= cc
CFLAGS ?= -O2 -finline-functions -Wall -Wmissing-prototypes
else ifeq ($(UNAME_SYS), Linux)
CC ?= gcc
CFLAGS ?= -O2 -finline-functions -Wall -Wmissing-prototypes
endif
BUILD_DIR := _build
INSTALL_DIR := ../priv

# Project specific C compiler/flags.
ifeq ($(ERTS_INCLUDE_DIR),)
ERTS_INCLUDE_DIR := $(shell "$(ERL)" -noshell -eval "io:format(\"~ts/erts-~ts/include/\", [code:root_dir(), erlang:system_info(version)])." -s erlang halt)
endif
CFLAGS ?= -O3 -finline-functions -Wall -Wmissing-prototypes

CFLAGS += -std=c11 -fPIC -I $(ERTS_INCLUDE_DIR)
CFLAGS += $(shell $(PKG_CONFIG) --cflags libusb-1.0)
ERTS_CFLAGS ?= -I$(shell "$(ERL)" -noshell -eval "io:format(\"~ts/erts-~ts/include/\", [code:root_dir(), erlang:system_info(version)])." -s erlang halt)
ERTS_LDFLAGS ?=

LDFLAGS += -shared
LDFLAGS += $(shell $(PKG_CONFIG) --libs libusb-1.0)
LIBUSB_CFLAGS ?= $(shell $(PKG_CONFIG) --cflags libusb-1.0)
LIBUSB_LDFLAGS ?= $(shell $(PKG_CONFIG) --libs libusb-1.0)

# Verbosity.
V ?= 0
# System specific compiler/linker flags.
UNAME_SYS := $(shell uname -s)
ifeq ($(UNAME_SYS), Darwin)
LDFLAGS ?= -flat_namespace -undefined dynamic_lookup
endif

c_verbose_0 = @echo " C " $(@F);
c_verbose = $(c_verbose_$(V))
CFLAGS += -std=c11 -fPIC $(ERTS_CFLAGS) $(LIBUSB_CFLAGS)
LDFLAGS += -shared $(ERTS_LDFLAGS) $(LIBUSB_LDFLAGS)

link_verbose_0 = @echo " LD " $(@F);
link_verbose = $(link_verbose_$(V))
#
# Targets
#

# Files
HEADERS := $(wildcard *.h)
.PHONY: all
all: $(BUILD_DIR)/usb_nif.so

# Rules
.PHONY: install
install: _build/usb_nif.so | $(BASEDIR)/priv/
cp $^ $(BASEDIR)/priv

.PHONY: all
all: _build/usb_nif.so
install: $(BUILD_DIR)/usb_nif.so | $(INSTALL_DIR)/
cp $(BUILD_DIR)/usb_nif.so $(INSTALL_DIR)/

.PHONY: clean
clean:
rm -f "$(BASEDIR)/priv/usb_nif.so"
rm -Rf "_build/"
rm -f "$(INSTALL_DIR)/usb_nif.so"
rm -Rf "$(BUILD_DIR)/"

_build/%.so: _build/%.o $(MAKEFILE_LIST) | _build/
$(link_verbose) $(CC) "$<" $(LDFLAGS) -o "$@"
#
# Build
#

_build/%.o: %.c $(HEADERS) $(MAKEFILE_LIST) | _build/
$(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c "$<" -o "$@"
SOURCES := usb_nif.c
HEADERS := khash.h

_build/:
mkdir -p "$@"
$(BUILD_DIR)/usb_nif.o: $(SOURCES) $(HEADERS) $(MAKEFILE_LIST) | $(BUILD_DIR)/
$(CC) $(CFLAGS) -c $(SOURCES) -o "$@"

$(BUILD_DIR)/usb_nif.so: $(BUILD_DIR)/usb_nif.o $(MAKEFILE_LIST) | $(BUILD_DIR)/
$(CC) $(BUILD_DIR)/usb_nif.o $(LDFLAGS) -o "$@"

$(BASEDIR)/priv/:
$(BUILD_DIR)/:
mkdir -p "$@"
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{deps, []}.

{pre_hooks, [
{"(linux|darwin|solaris)", compile, "make -C c_src"},
{"(freebsd)", compile, "gmake -C c_src"}
{"(linux|darwin|solaris)", compile, "make -C c_src install"},
{"(freebsd)", compile, "gmake -C c_src install"}
]}.
{post_hooks, [
{"(linux|darwin|solaris)", clean, "make -C c_src clean"},
Expand Down