-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
136 lines (110 loc) · 3.78 KB
/
makefile
File metadata and controls
136 lines (110 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
CC := gcc
BINARY := stdio-proxy
SRC := stdio-proxy.c
SRCS ?= $(SRC)
IMAGE = stdio-proxy-builder
BUILDDIR ?= ./build
PROGS := stdio-proxy
archs = amd64 arm64
arch ?= $(shell arch)
# Detect host architecture and pick the appropriate generic baseline flags.
# The intent is a binary that runs on any CPU of the same ISA family —
# no host-specific tuning, but optimised for the generic case.
#
# x86_64 → -march=x86-64 (SSE2 baseline, aka x86-64-v1)
# aarch64 → -march=armv8-a (ARMv8.0 baseline)
# others → no -march flag (let the compiler decide)
#
ARCH := $(shell uname -m)
ifeq ($(ARCH),x86_64)
MARCH := -march=x86-64 -mtune=generic
else ifeq ($(ARCH),aarch64)
MARCH := -march=armv8-a -mtune=generic
else
MARCH :=
endif
# -O2 : solid speed/size trade-off
# -ffunction/data-sections + --gc-sections : strip dead code from the binary
# -flto : link-time optimisation (whole-program inlining, DCE)
# -pipe : use pipes instead of temp files between cc stages
CFLAGS := -std=c11 \
-O2 \
$(MARCH) \
-ffunction-sections \
-fdata-sections \
-fstack-protector-strong \
-flto=auto \
-pipe \
-Wall \
-Wextra \
-Wpedantic
LDFLAGS := -static \
-flto=auto \
-Wl,--gc-sections \
-Wl,-s
# Static linking requires the C runtime static archives.
#
# glibc (default):
# Debian/Ubuntu : apt install libc6-dev (usually already present)
# apt install libc-static # or glibc-static on RHEL
# RHEL/AlmaLinux: dnf install glibc-static (from CRB repo)
#
# musl (recommended — no NSS resolver quirks at runtime):
# Debian/Ubuntu : apt install musl-tools → provides musl-gcc
# RHEL/AlmaLinux: dnf install musl-libc-static → use with CC=musl-gcc
#
# make CC=musl-gcc
# make static-musl (auto-selects musl-gcc if present)
MUSL_GCC := $(shell command -v musl-gcc 2>/dev/null)
.PHONY: all static-musl test clean
all: builddir $(BINARY)
$(BINARY): $(SRC)
$(CC) $(CFLAGS) -o $(BUILDDIR)/$@ $< $(LDFLAGS)
# Convenience target: build with musl-gcc when available.
static-musl: $(SRC)
ifdef MUSL_GCC
$(MUSL_GCC) $(CFLAGS) -o $(BUILDDIR)/$(BINARY) $< $(LDFLAGS)
else
$(error musl-gcc not found. Install musl-tools (Debian/Ubuntu) or musl-libc-static (RHEL))
endif
# Run Go integration tests. The test harness (proxy_test.go) compiles the C
# source itself (non-static, -O0 -g) so that no static libc is required.
# Requires Go in PATH. Run from the repo root or this directory.
.PHONE: test
test: go.mod
go test -v -count=1 ./test
go.mod:
go mod init $(BINARY)
clean:
rm -f $(BINARY) stdio-proxy-test
distclean: clean
rm -f $(addprefix $(BUILDDIR)/,$(PROGS)) $(addprefix $(BUILDDIR)/,$(addsuffix -static,$(PROGS)))
builddir:
if [ ! -d $(BUILDDIR) ]; then mkdir -p $(BUILDDIR); fi
$(archs):
mkdir -p release
$(MAKE) build-docker arch=$@
export COPYFILE_DISABLE=true; \
for PROG in $(PROGS); do \
tar -czf ./release/$$PROG-linux-$@.tgz LICENSE -C ./build $$PROG; \
done
fmt:
docker run --rm -v "$$PWD":/src -w /src alpine:latest sh -c "apk add --no-cache indent && indent -linux $(SRCS) && indent -linux $(SRCS)"
fmt-all:
$(MAKE) fmt SRCS="*.c"
release-all:
$(MAKE) release
.PHONY: release
release: $(archs)
build-image:
docker build -t $(IMAGE):$(arch) --platform linux/$(arch) -f build.dockerfile .
build-docker: build-image
$(MAKE) distclean
docker run --rm -v "$$PWD":/src -w /src -e PROGS="$(PROGS)" \
--platform linux/$(arch) $(IMAGE):$(arch) make all
build-test-image:
docker build -t $(IMAGE)-test:$(arch) --platform linux/$(arch) -f test.dockerfile .
test-docker: build-test-image
docker run --rm -v "$$PWD":/test \
--platform linux/$(arch) $(IMAGE)-test:$(arch) \
make test