commit dfa262736b770f0f3857302b43afe12ef0caee17
| author | 斟酌 鵬兄 <tgckpg@gmail.com> |
| date | 2026-05-26T11:18:02Z |
| subject | Static config |
commit dfa262736b770f0f3857302b43afe12ef0caee17
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date: 2026-05-26T11:18:02Z
Static config
---
Dockerfile | 14 +++++++++-----
Makefile | 14 ++++++++++----
2 files changed, 19 insertions(+), 9 deletions(-)
diff --git a/Dockerfile b/Dockerfile
index 62e9594..b846dd6 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,6 +1,7 @@
-FROM alpine:3.23 AS build
+FROM --platform=$BUILDPLATFORM alpine:3.23 AS build
-RUN apk add --no-cache build-base libevent-dev
+RUN --mount=type=cache,target=/var/cache/apk,sharing=locked \
+ apk add --update-cache build-base libevent-dev libevent-static
RUN mkdir /src
WORKDIR /src
@@ -10,12 +11,15 @@ COPY [ "*.c", "*.h", "*.conf", "Makefile", "/src" ]
RUN make all
COPY tests ./tests
-RUN make test
+RUN STATIC=1 make test
-FROM alpine:3.23
+RUN cp /src/tinyproxy.conf /etc/ && cp /src/bin/tinyproxy /usr/bin/
+
+FROM scratch
-RUN apk add --no-cache libevent
COPY --from=build /src/bin/tinyproxy /usr/bin/tinyproxy
COPY --from=build /src/tinyproxy.conf /etc/tinyproxy.conf
+RUN /usr/bin/tinyproxy
+
CMD [ "tinyproxy", "-c", "/etc/tinyproxy.conf" ]
diff --git a/Makefile b/Makefile
index 50a34f8..69433c7 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,7 @@ CC ?= cc
AR := /usr/bin/ar
RANLIB := /usr/bin/ranlib
STRIP ?= strip
+STATIC ?= 1
PROJECT_ROOT := $(CURDIR)
BIN_DIR := $(CURDIR)/bin
@@ -29,11 +30,16 @@ LIBEVENT_CORE_A := $(LIBEVENT_PREFIX)/lib/libevent_core.a
CFLAGS ?= -Os -Wall -Wextra -ffunction-sections -fdata-sections
+ifeq ($(STATIC),1)
+LDFLAGS += -static
+PKG_CONFIG_STATIC := --static
+endif
+
ifeq ($(strip $(LIBEVENT_SRC)),)
-# System libevent, e.g. Alpine libevent-dev
+# System libevent, statically linked
LIBEVENT_CPPFLAGS := $(shell pkg-config --cflags libevent_core)
-LIBEVENT_LDFLAGS := $(shell pkg-config --libs-only-L libevent_core)
-LIBEVENT_LDLIBS := $(shell pkg-config --libs-only-l --libs-only-other libevent_core)
+LIBEVENT_LDFLAGS :=
+LIBEVENT_LDLIBS := $(shell pkg-config --static --libs libevent_core)
LIBEVENT_DEPS :=
else
# Vendored libevent
@@ -44,7 +50,7 @@ LIBEVENT_DEPS := $(LIBEVENT_CORE_A)
endif
CPPFLAGS += $(LIBEVENT_CPPFLAGS)
-LDFLAGS += $(LIBEVENT_LDFLAGS)
+LDFLAGS += -static $(LIBEVENT_LDFLAGS)
LDLIBS += $(LIBEVENT_LDLIBS)
$(BIN): $(SRC) $(LIBEVENT_DEPS)