diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | build.config | 2 | ||||
-rw-r--r-- | erlang.mk | 81 | ||||
-rw-r--r-- | plugins/c_src.mk | 59 |
4 files changed, 120 insertions, 24 deletions
@@ -260,7 +260,7 @@ templates. C compiler plugin ----------------- -This plugin is not included by default. It is meant to +This plugin is available by default. It is meant to simplify the management of projects that include C source code, like NIFs. diff --git a/build.config b/build.config index 9df8520..067a0ad 100644 --- a/build.config +++ b/build.config @@ -10,7 +10,7 @@ core/erlc # # Comment to disable, uncomment to enable. plugins/bootstrap -#plugins/c_src +plugins/c_src plugins/ct plugins/dialyzer plugins/edoc @@ -575,6 +575,87 @@ endif list-templates: @echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES)))) +# Copyright (c) 2014, 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 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. + +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 +else ifeq ($(UNAME_SYS), FreeBSD) + CC ?= cc + CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes +else ifeq ($(UNAME_SYS), Linux) + CC ?= gcc + CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes +endif + +CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) + +LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei +LDFLAGS += -shared + +# Verbosity. + +c_src_verbose_0 = @echo " C_SRC " $(?F); +c_src_verbose = $(c_src_verbose_$(V)) + +# 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 + +else +SOURCE := $(shell find $(C_SRC_DIR) -type f -name \*.c) + +app:: $(C_SRC_ENV) $(C_SRC_OUTPUT) + +$(C_SRC_OUTPUT): $(SOURCE) + @mkdir -p priv/ + $(c_src_verbose) $(CC) $(CFLAGS) $(SOURCE) \ + $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT) $(C_SRC_OPTS) + +$(C_SRC_ENV): + @erl -noshell -noinput -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)])), \ + erlang:halt()." + +clean:: clean-c_src + +clean-c_src: + $(gen_verbose) rm -f $(C_SRC_OUTPUT) + +distclean:: distclean-c_src-env + +distclean-c_src-env: + $(gen_verbose) rm -f $(C_SRC_ENV) + +-include $(C_SRC_ENV) +endif + # Copyright (c) 2013-2014, Loïc Hoguin <[email protected]> # This file is part of erlang.mk and subject to the terms of the ISC License. diff --git a/plugins/c_src.mk b/plugins/c_src.mk index daee44e..0b4064a 100644 --- a/plugins/c_src.mk +++ b/plugins/c_src.mk @@ -1,7 +1,7 @@ # Copyright (c) 2014, 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. @@ -25,41 +25,56 @@ else ifeq ($(UNAME_SYS), Linux) CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes endif +CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) + +LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei +LDFLAGS += -shared + # Verbosity. c_src_verbose_0 = @echo " C_SRC " $(?F); -c_src_verbose = $(appsrc_verbose_$(V)) +c_src_verbose = $(c_src_verbose_$(V)) # Targets. -ifeq ($(wildcard $(C_SRC_DIR)/Makefile),) +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 + +else +SOURCE := $(shell find $(C_SRC_DIR) -type f -name \*.c) + +app:: $(C_SRC_ENV) $(C_SRC_OUTPUT) -app:: $(C_SRC_ENV) +$(C_SRC_OUTPUT): $(SOURCE) @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) + $(c_src_verbose) $(CC) $(CFLAGS) $(SOURCE) \ + $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT) $(C_SRC_OPTS) $(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)])), \ + @erl -noshell -noinput -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)])), \ erlang:halt()." --include $(C_SRC_ENV) +clean:: clean-c_src -else -ifneq ($(wildcard $(C_SRC_DIR)),) +clean-c_src: + $(gen_verbose) rm -f $(C_SRC_OUTPUT) -app:: - $(MAKE) -C $(C_SRC_DIR) +distclean:: distclean-c_src-env -clean:: - $(MAKE) -C $(C_SRC_DIR) clean +distclean-c_src-env: + $(gen_verbose) rm -f $(C_SRC_ENV) +-include $(C_SRC_ENV) endif -endif - -clean:: clean-c_src - -clean-c_src: - $(gen_verbose) rm -f $(C_SRC_ENV) $(C_SRC_OUTPUT) |