diff options
Diffstat (limited to 'plugins/c_src.mk')
-rw-r--r-- | plugins/c_src.mk | 90 |
1 files changed, 65 insertions, 25 deletions
diff --git a/plugins/c_src.mk b/plugins/c_src.mk index 63ca6bc..e23c7b9 100644 --- a/plugins/c_src.mk +++ b/plugins/c_src.mk @@ -1,14 +1,13 @@ -# Copyright (c) 2014, Loïc Hoguin <[email protected]> +# Copyright (c) 2014-2015, Loïc Hoguin <[email protected]> # This file is part of erlang.mk and subject to the terms of the ISC License. -.PHONY: clean-c_src +.PHONY: clean-c_src distclean-c_src-env # todo # Configuration. C_SRC_DIR = $(CURDIR)/c_src C_SRC_ENV ?= $(C_SRC_DIR)/env.mk -C_SRC_OPTS ?= C_SRC_OUTPUT ?= $(CURDIR)/priv/$(PROJECT).so # System type and C compiler/flags. @@ -16,50 +15,91 @@ C_SRC_OUTPUT ?= $(CURDIR)/priv/$(PROJECT).so UNAME_SYS := $(shell uname -s) ifeq ($(UNAME_SYS), Darwin) CC ?= cc - CFLAGS ?= -O3 -std=c99 -arch x86_64 -flat_namespace -undefined suppress -finline-functions -Wall -Wmissing-prototypes + 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 -# Verbosity. +CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) +CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -c_src_verbose_0 = @echo " C_SRC " $(?F); -c_src_verbose = $(appsrc_verbose_$(V)) +LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei +LDFLAGS += -shared -# Targets. +# Verbosity. -ifeq ($(wildcard $(C_SRC_DIR)/Makefile),) +c_verbose_0 = @echo " C " $(?F); +c_verbose = $(c_verbose_$(V)) -app:: $(C_SRC_ENV) - @mkdir -p priv/ - $(c_src_verbose) $(CC) $(CFLAGS) $(C_SRC_DIR)/*.c -fPIC -shared -o $(C_SRC_OUTPUT) \ - -I $(ERTS_INCLUDE_DIR) $(C_SRC_OPTS) +cpp_verbose_0 = @echo " CPP " $(?F); +cpp_verbose = $(cpp_verbose_$(V)) -$(C_SRC_ENV): - erl -noshell -noinput -eval "file:write_file(\"$(C_SRC_ENV)\", \ - io_lib:format(\"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/\", \ - [code:root_dir(), erlang:system_info(version)])), \ - init:stop()." - --include $(C_SRC_ENV) +link_verbose_0 = @echo " LD " $(@F); +link_verbose = $(link_verbose_$(V)) -else -ifneq ($(wildcard $(C_SRC_DIR),)) +# Targets. +ifeq ($(wildcard $(C_SRC_DIR)),) +else ifneq ($(wildcard $(C_SRC_DIR)/Makefile),) app:: $(MAKE) -C $(C_SRC_DIR) clean:: $(MAKE) -C $(C_SRC_DIR) clean -endif -endif +else +SOURCES := $(shell find $(C_SRC_DIR) -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 + +app:: $(C_SRC_ENV) $(C_SRC_OUTPUT) + +$(C_SRC_OUTPUT): $(OBJECTS) + @mkdir -p 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) $< + +$(C_SRC_ENV): + @$(ERL) -eval "file:write_file(\"$(C_SRC_ENV)\", \ + io_lib:format( \ + \"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/~n\" \ + \"ERL_INTERFACE_INCLUDE_DIR ?= ~s~n\" \ + \"ERL_INTERFACE_LIB_DIR ?= ~s~n\", \ + [code:root_dir(), erlang:system_info(version), \ + code:lib_dir(erl_interface, include), \ + code:lib_dir(erl_interface, lib)])), \ + halt()." clean:: clean-c_src clean-c_src: - $(gen_verbose) rm -f $(C_SRC_ENV) $(C_SRC_OUTPUT) + $(gen_verbose) rm -f $(C_SRC_OUTPUT) $(OBJECTS) + +distclean:: distclean-c_src-env + +distclean-c_src-env: + $(gen_verbose) rm -f $(C_SRC_ENV) + +-include $(C_SRC_ENV) +endif |