Skip to content

Commit 755ad20

Browse files
committed
Added Makefile with common development tasks.
1 parent 37cad21 commit 755ad20

10 files changed

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