From afedeb9d58fd23ff3d90cb7747719f06f70a9d7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 24 Oct 2024 14:14:47 +0200 Subject: [PATCH] refactor: use build tags to disable, instead of to enable a feature (#1113) --- .github/workflows/sanitizers.yaml | 4 ++- Dockerfile | 2 +- alpine.Dockerfile | 2 +- build-static.sh | 2 +- caddy/br-skip.go | 2 +- caddy/br.go | 2 +- caddy/watcher_test.go | 2 +- dev.Dockerfile | 2 +- docs/compile.md | 41 ++++++++------------------ internal/watcher/watch_pattern.go | 2 +- internal/watcher/watch_pattern_test.go | 2 +- internal/watcher/watcher-c.h | 11 ------- internal/watcher/watcher-skip.go | 2 +- internal/watcher/watcher.c | 2 +- internal/watcher/watcher.go | 4 ++- watcher_test.go | 2 +- 16 files changed, 31 insertions(+), 53 deletions(-) diff --git a/.github/workflows/sanitizers.yaml b/.github/workflows/sanitizers.yaml index 131b624..486cf13 100644 --- a/.github/workflows/sanitizers.yaml +++ b/.github/workflows/sanitizers.yaml @@ -97,6 +97,7 @@ jobs: name: Add PHP to the PATH run: echo "$(pwd)/php/target/bin" >> "$GITHUB_PATH" - + if: matrix.sanitizer != 'msan' uses: actions/checkout@v4 name: Checkout watcher with: @@ -104,6 +105,7 @@ jobs: ref: ${{ env.EDANT_WATCHER_VERSION }} path: 'edant/watcher' - + if: matrix.sanitizer != 'msan' name: Compile edant/watcher run: | cd edant/watcher/watcher-c/ @@ -119,7 +121,7 @@ jobs: } >> "$GITHUB_ENV" - name: Compile tests - run: go test -${{ matrix.sanitizer }} -v -x -c + run: go test ${{ matrix.sanitizer == 'msan' && '-tags=nowatcher' || '' }} -${{ matrix.sanitizer }} -v -x -c - name: Run tests run: ./frankenphp.test -test.v diff --git a/Dockerfile b/Dockerfile index c21d432..cb16e1c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -104,7 +104,7 @@ ENV CGO_CPPFLAGS=$PHP_CPPFLAGS ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" WORKDIR /go/src/app/caddy/frankenphp -RUN GOBIN=/usr/local/bin go install -tags 'brotli,watcher,nobadger,nomysql,nopgx' -ldflags "-w -s -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \ +RUN GOBIN=/usr/local/bin go install -tags 'nobadger,nomysql,nopgx' -ldflags "-w -s -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \ setcap cap_net_bind_service=+ep /usr/local/bin/frankenphp && \ cp Caddyfile /etc/caddy/Caddyfile && \ frankenphp version diff --git a/alpine.Dockerfile b/alpine.Dockerfile index fefdef0..41be2dc 100644 --- a/alpine.Dockerfile +++ b/alpine.Dockerfile @@ -125,7 +125,7 @@ ENV CGO_CPPFLAGS=$PHP_CPPFLAGS ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" WORKDIR /go/src/app/caddy/frankenphp -RUN GOBIN=/usr/local/bin go install -tags 'brotli,watcher,nobadger,nomysql,nopgx' -ldflags "-w -s -extldflags '-Wl,-z,stack-size=0x80000' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \ +RUN GOBIN=/usr/local/bin go install -tags 'nobadger,nomysql,nopgx' -ldflags "-w -s -extldflags '-Wl,-z,stack-size=0x80000' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \ setcap cap_net_bind_service=+ep /usr/local/bin/frankenphp && \ upx --best /usr/local/bin/frankenphp && \ frankenphp version diff --git a/build-static.sh b/build-static.sh index 8ba1731..5e7c6ac 100755 --- a/build-static.sh +++ b/build-static.sh @@ -249,7 +249,7 @@ fi cd caddy/frankenphp/ go env -go build -buildmode=pie -tags "cgo,netgo,osusergo,static_build,brotli,watcher,nobadger,nomysql,nopgx" -ldflags "-linkmode=external -extldflags '-static-pie ${extraExtldflags}' ${extraLdflags} -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ${FRANKENPHP_VERSION} PHP ${LIBPHP_VERSION} Caddy'" -o "../../dist/${bin}" +go build -buildmode=pie -tags "cgo,netgo,osusergo,static_build,nobadger,nomysql,nopgx" -ldflags "-linkmode=external -extldflags '-static-pie ${extraExtldflags}' ${extraLdflags} -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ${FRANKENPHP_VERSION} PHP ${LIBPHP_VERSION} Caddy'" -o "../../dist/${bin}" cd ../.. if [ -d "${EMBED}" ]; then diff --git a/caddy/br-skip.go b/caddy/br-skip.go index 7ff7f96..1c0dbe0 100644 --- a/caddy/br-skip.go +++ b/caddy/br-skip.go @@ -1,4 +1,4 @@ -//go:build !brotli +//go:build nobrotli package caddy diff --git a/caddy/br.go b/caddy/br.go index 8b303bb..6522cb6 100644 --- a/caddy/br.go +++ b/caddy/br.go @@ -1,4 +1,4 @@ -//go:build brotli +//go:build !nobrotli package caddy diff --git a/caddy/watcher_test.go b/caddy/watcher_test.go index 581f23c..acbacc1 100644 --- a/caddy/watcher_test.go +++ b/caddy/watcher_test.go @@ -1,4 +1,4 @@ -//go:build watcher +//go:build !nowatcher package caddy_test diff --git a/dev.Dockerfile b/dev.Dockerfile index 5646861..4dc9e65 100644 --- a/dev.Dockerfile +++ b/dev.Dockerfile @@ -78,7 +78,7 @@ WORKDIR /go/src/app COPY . . WORKDIR /go/src/app/caddy/frankenphp -RUN go build -buildvcs=false -tags 'brotli,watcher,nobadger,nomysql,nopgx' +RUN go build -buildvcs=false -tags 'nobadger,nomysql,nopgx' WORKDIR /go/src/app CMD [ "zsh" ] diff --git a/docs/compile.md b/docs/compile.md index 6084808..696326f 100644 --- a/docs/compile.md +++ b/docs/compile.md @@ -35,7 +35,7 @@ Use the [Homebrew](https://brew.sh/) package manager to install `libiconv`, `bison`, `re2c` and `pkg-config`: ```console -brew install libiconv bison re2c pkg-config +brew install libiconv bison brotli re2c pkg-config echo 'export PATH="/opt/homebrew/opt/bison/bin:$PATH"' >> ~/.zshrc ``` @@ -61,6 +61,16 @@ make -j"$(getconf _NPROCESSORS_ONLN)" sudo make install ``` +## Install Optional Dependencies + +Some FrankenPHP features depend on optional system dependencies that must be installed. +Alternatively, these features can be disabled by passing build tags to the Go compiler. + +| Feature | Dependency | Build tag to disable it | +|--------------------------------|-----------------------------------------------------------------------|-------------------------| +| Brotli compression | [Brotli](https://github.com/google/brotli) | nobrotli | +| Restart workers on file change | [Watcher C](https://github.com/e-dant/watcher/tree/release/watcher-c) | nowatcher | + ## Compile the Go App You can now build the final binary: @@ -68,7 +78,7 @@ You can now build the final binary: ```console curl -L https://github.com/dunglas/frankenphp/archive/refs/heads/main.tar.gz | tar xz cd frankenphp-main/caddy/frankenphp -CGO_CFLAGS=$(php-config --includes) CGO_LDFLAGS="$(php-config --ldflags) $(php-config --libs)" go build +CGO_CFLAGS=$(php-config --includes) CGO_LDFLAGS="$(php-config --ldflags) $(php-config --libs)" go build -tags=nobadger,nomysql,nopgx ``` ### Using xcaddy @@ -77,7 +87,7 @@ Alternatively, use [xcaddy](https://github.com/caddyserver/xcaddy) to compile Fr ```console CGO_ENABLED=1 \ -XCADDY_GO_BUILD_FLAGS="-ldflags '-w -s'" \ +XCADDY_GO_BUILD_FLAGS="-ldflags='-w -s' -tags=nobadger,nomysql,nopgx" \ xcaddy build \ --output frankenphp \ --with github.com/dunglas/frankenphp/caddy \ @@ -95,28 +105,3 @@ xcaddy build \ > To do so, change the `XCADDY_GO_BUILD_FLAGS` environment variable to something like > `XCADDY_GO_BUILD_FLAGS=$'-ldflags "-w -s -extldflags \'-Wl,-z,stack-size=0x80000\'"'` > (change the value of the stack size according to your app needs). - -## Build Tags - -Additional features can be enabled if the required C libraries are installed by -passing additional build tags to the Go compiler: - -| Tag | Dependencies | Description | -|---------|----------------------------------------------|--------------------------------| -| brotli | [Brotli](https://github.com/google/brotli) | Brotli compression | -| watcher | [Watcher](https://github.com/e-dant/watcher) | Restart workers on file change | - -When using `go build` directly, pass the additional `-tags` option followed by the comma-separated list of tags: - -```console -go build -tags 'nobadger,nomysql,nopgx,brotli,watcher' -``` - -> [!NOTE] -> You should always pass the `nobadger,nomysql,nopgx` tags to disable unused features of the SmallStep nosql dependency. - -When using `xcaddy`, set the `-tags` option in the `XCADDY_GO_BUILD_FLAGS` environment variable: - -```console -XCADDY_GO_BUILD_FLAGS="-tags 'brotli,watcher'" -``` diff --git a/internal/watcher/watch_pattern.go b/internal/watcher/watch_pattern.go index 333f1bf..e460a8c 100644 --- a/internal/watcher/watch_pattern.go +++ b/internal/watcher/watch_pattern.go @@ -1,4 +1,4 @@ -//go:build watcher +//go:build !nowatcher package watcher diff --git a/internal/watcher/watch_pattern_test.go b/internal/watcher/watch_pattern_test.go index ead1c03..482b410 100644 --- a/internal/watcher/watch_pattern_test.go +++ b/internal/watcher/watch_pattern_test.go @@ -1,4 +1,4 @@ -//go:build watcher +//go:build !nowatcher package watcher diff --git a/internal/watcher/watcher-c.h b/internal/watcher/watcher-c.h index 5d6163c..621cfde 100644 --- a/internal/watcher/watcher-c.h +++ b/internal/watcher/watcher-c.h @@ -2,7 +2,6 @@ #include #include -#include #ifdef __cplusplus extern "C" { @@ -64,16 +63,6 @@ void *wtr_watcher_open(char const *const path, wtr_watcher_callback callback, bool wtr_watcher_close(void *watcher); -/* The user, or the language we're working with, - might not prefer a callback-style API. - We provide a pipe-based API for these cases. - Instead of forwarding events to a callback, - we write json-serialized events to a pipe. */ -void *wtr_watcher_open_pipe(char const *const path, int *read_fd, - int *write_fd); - -bool wtr_watcher_close_pipe(void *watcher, int read_fd, int write_fd); - #ifdef __cplusplus } #endif diff --git a/internal/watcher/watcher-skip.go b/internal/watcher/watcher-skip.go index 8f59dba..fbfdc09 100644 --- a/internal/watcher/watcher-skip.go +++ b/internal/watcher/watcher-skip.go @@ -1,4 +1,4 @@ -//go:build !watcher +//go:build nowatcher package watcher diff --git a/internal/watcher/watcher.c b/internal/watcher/watcher.c index efb3f64..51d10a2 100644 --- a/internal/watcher/watcher.c +++ b/internal/watcher/watcher.c @@ -1,5 +1,5 @@ // clang-format off -//go:build watcher +//go:build !nowatcher // clang-format on #include "_cgo_export.h" #include "watcher-c.h" diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 7cfb4bf..153e4c3 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -1,4 +1,6 @@ -//go:build watcher +//go:build !nowatcher + +//go:generate curl --silent https://raw.githubusercontent.com/e-dant/watcher/refs/heads/release/watcher-c/include/wtr/watcher-c.h -o watcher-c.h package watcher diff --git a/watcher_test.go b/watcher_test.go index bddbb68..7f46b4b 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -1,4 +1,4 @@ -//go:build watcher +//go:build !nowatcher package frankenphp_test