Skip to content

Commit

Permalink
refactor: use build tags to disable, instead of to enable a feature (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas authored Oct 24, 2024
1 parent d53f909 commit afedeb9
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 53 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/sanitizers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ 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:
repository: e-dant/watcher
ref: ${{ env.EDANT_WATCHER_VERSION }}
path: 'edant/watcher'
-
if: matrix.sanitizer != 'msan'
name: Compile edant/watcher
run: |
cd edant/watcher/watcher-c/
Expand All @@ -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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build-static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion caddy/br-skip.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !brotli
//go:build nobrotli

package caddy

Expand Down
2 changes: 1 addition & 1 deletion caddy/br.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build brotli
//go:build !nobrotli

package caddy

Expand Down
2 changes: 1 addition & 1 deletion caddy/watcher_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build watcher
//go:build !nowatcher

package caddy_test

Expand Down
2 changes: 1 addition & 1 deletion dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
41 changes: 13 additions & 28 deletions docs/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -61,14 +61,24 @@ 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:

```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
Expand All @@ -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 \
Expand All @@ -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'"
```
2 changes: 1 addition & 1 deletion internal/watcher/watch_pattern.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build watcher
//go:build !nowatcher

package watcher

Expand Down
2 changes: 1 addition & 1 deletion internal/watcher/watch_pattern_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build watcher
//go:build !nowatcher

package watcher

Expand Down
11 changes: 0 additions & 11 deletions internal/watcher/watcher-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion internal/watcher/watcher-skip.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !watcher
//go:build nowatcher

package watcher

Expand Down
2 changes: 1 addition & 1 deletion internal/watcher/watcher.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// clang-format off
//go:build watcher
//go:build !nowatcher
// clang-format on
#include "_cgo_export.h"
#include "watcher-c.h"
Expand Down
4 changes: 3 additions & 1 deletion internal/watcher/watcher.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion watcher_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build watcher
//go:build !nowatcher

package frankenphp_test

Expand Down

0 comments on commit afedeb9

Please sign in to comment.