Browse Source

refactor: use build tags to disable, instead of to enable a feature (#1113)

Kévin Dunglas 4 months ago
parent
commit
afedeb9d58

+ 3 - 1
.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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
caddy/br-skip.go

@@ -1,4 +1,4 @@
-//go:build !brotli
+//go:build nobrotli
 
 package caddy
 

+ 1 - 1
caddy/br.go

@@ -1,4 +1,4 @@
-//go:build brotli
+//go:build !nobrotli
 
 package caddy
 

+ 1 - 1
caddy/watcher_test.go

@@ -1,4 +1,4 @@
-//go:build watcher
+//go:build !nowatcher
 
 package caddy_test
 

+ 1 - 1
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" ]

+ 13 - 28
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'"
-```

+ 1 - 1
internal/watcher/watch_pattern.go

@@ -1,4 +1,4 @@
-//go:build watcher
+//go:build !nowatcher
 
 package watcher
 

Some files were not shown because too many files changed in this diff