From 230111ab3d51883b1be6e96aad10e16e000087b0 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Wed, 4 Mar 2015 16:20:01 +0000 Subject: [PATCH] Add support for rebar3 - remove use of rebar2 port compiler config Rebar3 doesn't include a port compiler because it turns out it is too brittle and unportable. Switch to a Makefile via a compile hook instead to make things compatible with both rebar2 and rebar3. --- c_src/Makefile | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ rebar.config | 11 ++------ 2 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 c_src/Makefile diff --git a/c_src/Makefile b/c_src/Makefile new file mode 100644 index 00000000..1eda565b --- /dev/null +++ b/c_src/Makefile @@ -0,0 +1,74 @@ +# Based on c_src.mk from erlang.mk by Loic Hoguin + +CURDIR := $(shell pwd) +BASEDIR := $(abspath $(CURDIR)/..) + +PROJECT ?= $(notdir $(BASEDIR)) +PROJECT := $(strip $(PROJECT)) + +ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)]).") +ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)]).") +ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)]).") + +C_SRC_DIR = $(CURDIR) +C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so + +# System type and C compiler/flags. + +UNAME_SYS := $(shell uname -s) +ifeq ($(UNAME_SYS), Darwin) + CC ?= cc + CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall + LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress +else ifeq ($(UNAME_SYS), FreeBSD) + CC ?= cc + CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -finline-functions -Wall +else ifeq ($(UNAME_SYS), Linux) + CC ?= gcc + CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -finline-functions -Wall +endif + +CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -I leveldb/include +CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -I leveldb/include + +LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei +LDFLAGS += -shared leveldb/libleveldb.a system/lib/libsnappy.a -lstdc++ + +# Verbosity. + +c_verbose_0 = @echo " C " $(?F); +c_verbose = $(c_verbose_$(V)) + +cpp_verbose_0 = @echo " CPP " $(?F); +cpp_verbose = $(cpp_verbose_$(V)) + +link_verbose_0 = @echo " LD " $(@F); +link_verbose = $(link_verbose_$(V)) + +SOURCES := $(shell find $(C_SRC_DIR) -maxdepth 1 -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \)) +OBJECTS = $(addsuffix .o, $(basename $(SOURCES))) + +COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c +COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c + +$(C_SRC_OUTPUT): $(OBJECTS) + @mkdir -p $(BASEDIR)/priv/ + $(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT) + +%.o: %.c + $(COMPILE_C) $(OUTPUT_OPTION) $< + +%.o: %.cc + $(COMPILE_CPP) $(OUTPUT_OPTION) $< + +%.o: %.C + $(COMPILE_CPP) $(OUTPUT_OPTION) $< + +%.o: %.cpp + $(COMPILE_CPP) $(OUTPUT_OPTION) $< + +clean: + @rm $(C_SRC_OUTPUT) $(OBJECTS) diff --git a/rebar.config b/rebar.config index f02d3680..171e0511 100644 --- a/rebar.config +++ b/rebar.config @@ -12,15 +12,8 @@ {cuttlefish, ".*", {git, "git://github.com/basho/cuttlefish.git", {branch, "develop"}}} ]}. -{port_env, [ - %% Make sure to set -fPIC when compiling leveldb - {"CFLAGS", "$CFLAGS -Wall -O3 -fPIC"}, - {"CXXFLAGS", "$CXXFLAGS -Wall -O3 -fPIC"}, - {"DRV_CFLAGS", "$DRV_CFLAGS -O3 -Wall -I c_src/leveldb/include"}, - {"DRV_LDFLAGS", "$DRV_LDFLAGS c_src/leveldb/libleveldb.a c_src/system/lib/libsnappy.a -lstdc++"} - ]}. - {pre_hooks, [{'get-deps', "c_src/build_deps.sh get-deps"}, - {compile, "c_src/build_deps.sh"}]}. + {compile, "c_src/build_deps.sh"}, + {compile, "make -C c_src"}]}. {post_hooks, [{clean, "c_src/build_deps.sh clean"}]}.