refactor: use build tags to disable, instead of to enable a feature (#1113)
Some checks failed
Lint Code Base / Lint Code Base (push) Has been cancelled
Sanitizers / ${{ matrix.sanitizer }} (asan) (push) Has been cancelled
Sanitizers / ${{ matrix.sanitizer }} (msan) (push) Has been cancelled
Tests / tests (8.2) (push) Has been cancelled
Tests / tests (8.3) (push) Has been cancelled
Tests / tests (8.4) (push) Has been cancelled

This commit is contained in:
Kévin Dunglas 2024-10-24 14:14:47 +02:00 committed by GitHub
parent d53f909d20
commit afedeb9d58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 31 additions and 53 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

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

View File

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

View File

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

View File

@ -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" ]

View File

@ -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'"
```

View File

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

View File

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

View File

@ -2,7 +2,6 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#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

View File

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

View File

@ -1,5 +1,5 @@
// clang-format off
//go:build watcher
//go:build !nowatcher
// clang-format on
#include "_cgo_export.h"
#include "watcher-c.h"

View File

@ -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

View File

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