From 8ead9a98a06b5ccae027678695e6a94b2b5774a5 Mon Sep 17 00:00:00 2001 From: Grzegorz Lyczba Date: Mon, 2 May 2022 16:30:31 +0200 Subject: [PATCH] Use port_compiler to build nif --- c_src/Makefile | 80 -------------------------------------------------- rebar.config | 46 ++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 90 deletions(-) delete mode 100644 c_src/Makefile diff --git a/c_src/Makefile b/c_src/Makefile deleted file mode 100644 index bc404bd..0000000 --- a/c_src/Makefile +++ /dev/null @@ -1,80 +0,0 @@ -# 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 -MEMTEST_OUTPUT ?= $(CURDIR)/../_build/memory_test - -.PHONY: memory-test - -# System type and C compiler/flags. - -UNAME_SYS := $(shell uname -s) -ifeq ($(UNAME_SYS), Darwin) - CXX ?= c++ - CXXFLAGS += -O3 -finline-functions - LDFLAGS += -flat_namespace -undefined suppress - PSOURCES = prometheus_process_info_macos.cc -else ifeq ($(UNAME_SYS), FreeBSD) - CXX ?= c++ - CXXEFLAGS += -D__STANDALONE_TEST__ -std=c++11 -Wall - CXXFLAGS += -O3 -finline-functions - PSOURCES = prometheus_process_info_freebsd.cc -else ifeq ($(UNAME_SYS), Linux) - CXX ?= g++ - CXXEFLAGS += -D__STANDALONE_TEST__ -std=c++11 -Wall - CXXFLAGS += -O3 -finline-functions - PSOURCES = prometheus_process_info_linux.cc -endif - -SOURCES = prometheus_process_collector_nif.cc $(PSOURCES) - -CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -std=c++11 -Wall -LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lei - -ifeq ($(UNAME_SYS), OpenBSD) - LDLIBS += -lestdc++ -else - LDLIBS += -lstdc++ -endif - -LDFLAGS += -shared - -# Verbosity. - -cpp_verbose_0 = @echo " CPP " $(?F); -cpp_verbose = $(cpp_verbose_$(V)) - -link_verbose_0 = @echo " LD " $(@F); -link_verbose = $(link_verbose_$(V)) - -OBJECTS = $(addsuffix .o, $(basename $(SOURCES))) - -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: %.cc - $(COMPILE_CPP) $(OUTPUT_OPTION) $< - -%.o: %.cpp - $(COMPILE_CPP) $(OUTPUT_OPTION) $< - -memory-test: - @mkdir -p $(BASEDIR)/_build/ - $(CXX) $(CXXEFLAGS) $(LDLIBS) prometheus_process_info_mt.cc $(PSOURCES) -o $(MEMTEST_OUTPUT) - valgrind --leak-check=full --error-exitcode=1 $(MEMTEST_OUTPUT) 2 - -clean: - @rm -f $(C_SRC_OUTPUT) $(OBJECTS) diff --git a/rebar.config b/rebar.config index 75ae1f8..c21b15e 100644 --- a/rebar.config +++ b/rebar.config @@ -1,12 +1,38 @@ -{erl_opts, [debug_info]}. +{erl_opts, [debug_info, + {platform_define, "^(R14|R15|R16B|17)", 'random_module_available'}, + {if_version_below, "23", {d, 'INCLUDE_ERL_INTERFACE'}} + ]}. {deps, [{prometheus, "~> 4.0"}]}. -{pre_hooks, - [{"linux|darwin", compile, "make -C c_src"}, - {"freebsd", compile, "gmake -C c_src"}]}. -{post_hooks, - [{"linux|darwin", clean, "make -C c_src clean"}, - {"freebsd", clean, "gmake -C c_src clean"}]}. - -{plugins, [{rebar3_archive_plugin, "0.0.2"}, - rebar3_hex]}. +{plugins, [ + {pc, {git, "https://github.com/blt/port_compiler.git", {branch, "master"}}} + ]}. +{port_specs, [ + {".*-apple-darwin.*", "priv/prometheus_process_collector.so", + ["c_src/prometheus_process_collector_nif.cc", "c_src/prometheus_process_info_macos.cc"] + }, + {".*-linux", "priv/prometheus_process_collector.so", + ["c_src/prometheus_process_collector_nif.cc", "c_src/prometheus_process_info_linux.cc"] + }, + {".*-freebsd.*", "priv/prometheus_process_collector.so", + ["c_src/prometheus_process_collector_nif.cc", "c_src/prometheus_process_info_freebsd.cc"] + } +]}. +{port_env, [ + {"CXXFLAGS", "-std=c++11 -Wall -O3 -arch x86_64 -finline-functions"}, + {".*-linux", "CXX", "g++"}, + {".*-apple-darwin.*", "CXX", "c++"}, + {".*-freebsd.*", "CXX", "c++"} +]}. +{so_name, "prometheus_process_collector.so"}. +{artifacts, ["priv/prometheus_process_collector.so"]}. +{provider_hooks, [ + {pre, [ + {compile, + {pc, compile} + }, + {clean, + {pc, clean} + } + ]} +]}.