Skip to content

Commit 32e9e35

Browse files
committed
Added Makefile with common development tasks.
1 parent e37ca14 commit 32e9e35

10 files changed

+207
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
/objs-*

Makefile

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
MODULE = nginx_acme
2+
# build/build-%.mk: debug, debug-static, release, release-static, sanitize
3+
BUILD ?= debug
4+
TESTS ?= t/
5+
NGX_CARGO ?= cargo
6+
# will be used to print host-tuple
7+
NGX_RUSTC ?= rustc
8+
9+
NGINX_CONFIGURE_BASE = \
10+
auto/configure \
11+
--with-http_ssl_module \
12+
--with-http_v2_module \
13+
--with-pcre \
14+
--with-stream \
15+
--with-stream_ssl_module \
16+
--with-compat
17+
18+
NGINX_SOURCE_DIR ?= ../nginx
19+
NGINX_TESTS_DIR ?= $(NGINX_SOURCE_DIR)/tests
20+
NGINX_BUILD_DIR ?= $(CURDIR)/objs-$(BUILD)
21+
22+
TEST_NGINX_BINARY = $(NGINX_BUILD_DIR)/nginx
23+
24+
# "build" always calls cargo and causes relinking.
25+
# Clearing this var allows to skip the build step: "make test TEST_PREREQ="
26+
TEST_PREREQ = build
27+
28+
# Conditionals via include, compatible with most implementations of make
29+
30+
# GNU make 3.81 or earlier
31+
MAKE_FLAVOR:= gnu
32+
# POSIX 2024, BSD, GNU make 3.82+, etc
33+
MAKE_FLAVOR!= echo posix
34+
35+
include build/compat-$(MAKE_FLAVOR).mk
36+
include build/build-$(BUILD).mk
37+
38+
# Set up environment propagation
39+
40+
BUILD_ENV += NGINX_SOURCE_DIR="$(NGINX_SOURCE_DIR)"
41+
BUILD_ENV += NGINX_BUILD_DIR="$(NGINX_BUILD_DIR)"
42+
43+
TEST_ENV += RUST_BACKTRACE=1
44+
TEST_ENV += TEST_NGINX_BINARY="$(TEST_NGINX_BINARY)"
45+
TEST_ENV += TEST_NGINX_GLOBALS="$(TEST_NGINX_GLOBALS)"
46+
47+
# Build targets
48+
49+
.PHONY: all help build check test clean
50+
51+
all: ## Build, lint and test the module
52+
all: build check unittest test
53+
54+
help:
55+
@echo "Available targets:"
56+
@awk 'BEGIN {FS = ":.*?## "}; /^[ a-zA-Z_-]+:.*?## .*$$/ {printf "%-16s %s\n", $$1, $$2}' \
57+
Makefile $(MAKEFILE_LIST) | sort -u
58+
@echo "Pass BUILD=<configuration> to any target for desired build configuration."
59+
@echo "Pass NGINX_SOURCE_DIR to specify path to your NGINX source checkout."
60+
61+
# Always rebuild targets managed by external build tool
62+
.PHONY: target/debug/lib$(MODULE)$(NGX_MODEXT) \
63+
target/release/lib$(MODULE)$(NGX_MODEXT) \
64+
$(TEST_NGINX_BINARY)
65+
66+
$(NGINX_BUILD_DIR)/Makefile: config config.make auto/rust
67+
$(NGINX_BUILD_DIR)/Makefile: $(NGINX_SOURCE_DIR)/src/core/nginx.h
68+
# auto/configure unconditionally generates $NGINX_SOURCE_DIR/Makefile, even for
69+
# out-of-tree builds. Preserve the original Makefile and restore it later.
70+
@-cd $(NGINX_SOURCE_DIR) && rm -f Makefile.bak \
71+
&& test -f Makefile && mv -f Makefile Makefile.bak
72+
cd $(NGINX_SOURCE_DIR) \
73+
&& $(BUILD_ENV) $(NGINX_CONFIGURE) --builddir=$(NGINX_BUILD_DIR) \
74+
&& rm -f $(NGINX_SOURCE_DIR)/Makefile
75+
@-mv $(NGINX_SOURCE_DIR)/Makefile.bak $(NGINX_SOURCE_DIR)/Makefile
76+
77+
$(TEST_NGINX_BINARY): $(NGINX_BUILD_DIR)/Makefile
78+
cd $(NGINX_SOURCE_DIR) \
79+
&& $(BUILD_ENV) $(MAKE) -f $(NGINX_BUILD_DIR)/Makefile
80+
81+
target/debug/lib$(MODULE)$(NGX_MODEXT): $(NGINX_BUILD_DIR)/Makefile
82+
$(BUILD_ENV) $(NGX_CARGO) build
83+
84+
target/release/lib$(MODULE)$(NGX_MODEXT): $(NGINX_BUILD_DIR)/Makefile
85+
$(BUILD_ENV) $(NGX_CARGO) build --release
86+
87+
build: $(TEST_NGINX_BINARY) ## Build the module
88+
89+
check: $(NGINX_BUILD_DIR)/Makefile ## Check style and lint
90+
$(BUILD_ENV) $(NGX_CARGO) fmt --all -- --check
91+
$(BUILD_ENV) $(NGX_CARGO) clippy --all-targets --verbose -- -D warnings
92+
93+
unittest: $(NGINX_BUILD_DIR)/Makefile ## Run unit-tests
94+
$(BUILD_ENV) $(NGX_CARGO) test
95+
96+
test: $(TEST_PREREQ) ## Run the integration test suite
97+
env $(TEST_ENV) prove -I $(NGINX_TESTS_DIR)/lib --state=save $(TESTS) ||\
98+
env $(TEST_ENV) prove -I $(NGINX_TESTS_DIR)/lib --state=failed -v
99+
100+
clean: ## Cleanup everything
101+
rm -rf $(NGINX_BUILD_DIR)
102+
$(NGX_CARGO) clean
103+
104+
# GNU make convenience targets. Will be ignored by other implementations.
105+
106+
build-%: ## Build with the specified configuration. E.g. make build-sanitize.
107+
$(MAKE) build BUILD="$*"
108+
109+
test-%: ## Test with the specified configuration.
110+
$(MAKE) test BUILD="$*"

build/build-debug-static.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NGINX_CONFIGURE = \
2+
$(NGINX_CONFIGURE_BASE) \
3+
--with-debug \
4+
--add-module="$(CURDIR)"

build/build-debug.mk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
NGX_MODULE = target/debug/lib$(MODULE)$(NGX_MODEXT)
2+
TEST_NGINX_GLOBALS += load_module $(CURDIR)/$(NGX_MODULE);
3+
4+
NGINX_CONFIGURE = \
5+
$(NGINX_CONFIGURE_BASE) \
6+
--with-debug \
7+
--add-dynamic-module="$(CURDIR)"
8+
9+
# always rebuild targets managed by external build tool
10+
.PHONY: $(NGX_MODULE)
11+
12+
build: $(NGX_MODULE)

build/build-release-static.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
NGINX_CONFIGURE = \
2+
$(NGINX_CONFIGURE_BASE) \
3+
--add-module="$(CURDIR)"

build/build-release.mk

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
NGX_MODULE = target/release/lib$(MODULE)$(NGX_MODEXT)
2+
TEST_NGINX_GLOBALS += load_module $(CURDIR)/$(NGX_MODULE);
3+
4+
NGINX_CONFIGURE = \
5+
$(NGINX_CONFIGURE_BASE) \
6+
--add-dynamic-module="$(CURDIR)"
7+
8+
# always rebuild targets managed by external build tool
9+
.PHONY: $(NGX_MODULE)
10+
11+
build: $(NGX_MODULE)

build/build-sanitize.mk

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
CFLAGS_ASAN += -O1 -fsanitize=address -fno-omit-frame-pointer
2+
CFLAGS_ASAN += -DNGX_DEBUG_PALLOC=1 -DNGX_SUPPRESS_WARN=1
3+
LDFLAGS_ASAN += -fsanitize=address
4+
5+
RUSTFLAGS += -Cforce-frame-pointers=yes
6+
RUSTFLAGS += -Zsanitizer=address -Zexternal-clangrt
7+
8+
BUILD_ENV += RUSTFLAGS="$(RUSTFLAGS)"
9+
BUILD_ENV += RUSTC_BOOTSTRAP=1
10+
BUILD_ENV += NGX_RUSTC_OPT="-Zbuild-std"
11+
BUILD_ENV += NGX_RUST_TARGET="$(HOST_TUPLE)"
12+
13+
TEST_ENV += ASAN_OPTIONS=detect_stack_use_after_return=1:detect_odr_violation=0
14+
TEST_ENV += LSAN_OPTIONS="suppressions=$(CURDIR)/build/lsan-suppressions.txt"
15+
TEST_ENV += TEST_NGINX_CATLOG=1
16+
17+
NGINX_CONFIGURE = \
18+
$(NGINX_CONFIGURE_BASE) \
19+
--with-cc=clang \
20+
--with-cc-opt="$(CFLAGS_ASAN)" \
21+
--with-ld-opt="$(LDFLAGS_ASAN)" \
22+
--with-debug \
23+
--add-module="$(CURDIR)"

build/compat-gnu.mk

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
HOST_TUPLE := $(shell $(NGX_RUSTC) --print=host-tuple)
2+
3+
# extension for Rust cdylib targets
4+
ifeq ($(shell uname), Darwin)
5+
NGX_MODEXT = .dylib
6+
else
7+
NGX_MODEXT = .so
8+
endif
9+
10+
# resolve paths
11+
12+
NGINX_SOURCE_DIR := $(shell CDPATH='' cd $(NGINX_SOURCE_DIR) && pwd)
13+
NGINX_TESTS_DIR := $(shell CDPATH='' cd $(NGINX_TESTS_DIR) && pwd)
14+
NGINX_BUILD_DIR := $(shell CDPATH='' mkdir -p $(NGINX_BUILD_DIR) && cd $(NGINX_BUILD_DIR) && pwd)

build/compat-posix.mk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
HOST_TUPLE != $(NGX_RUSTC) --print=host-tuple
2+
3+
# bsd make compatibility
4+
CURDIR ?= $(.CURDIR)
5+
# extension for Rust cdylib targets
6+
NGX_MODEXT != if [ `uname` = Darwin ]; then echo ".dylib"; else echo ".so"; fi
7+
8+
# resolve paths
9+
10+
NGINX_SOURCE_DIR != CDPATH='' cd $(NGINX_SOURCE_DIR) && pwd
11+
NGINX_TESTS_DIR != CDPATH='' cd $(NGINX_TESTS_DIR) && pwd
12+
NGINX_BUILD_DIR != CDPATH='' mkdir -p $(NGINX_BUILD_DIR) && cd $(NGINX_BUILD_DIR) && pwd

build/lsan-suppressions.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# LeakSanitizer suppressions list for nginx
2+
#
3+
# To be used with -fsanitize=address and
4+
# LSAN_OPTIONS=suppressions=lsan-suppressions.txt.
5+
#
6+
# https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer#suppressions
7+
8+
# cycle->connections, cycle->read_events, cycle->write_events
9+
leak:ngx_event_process_init
10+
11+
# XXX: can silence leaks from nginx SSL callbacks
12+
leak:SSL_do_handshake
13+
leak:SSL_read
14+
15+
# rcf->ranges not freed at process exit
16+
leak:ngx_http_upstream_update_random
17+
leak:ngx_stream_upstream_update_random

0 commit comments

Comments
 (0)