Skip to content

Commit d23089b

Browse files
committed
ci: add generic static build script
1 parent b32e738 commit d23089b

File tree

8 files changed

+125
-136
lines changed

8 files changed

+125
-136
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
!**/*.h
1010
!testdata/*.php
1111
!testdata/*.txt
12+
!build-static.sh

.github/workflows/static.yml

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5353
-
5454
name: Copy binary
55-
run: docker cp $(docker create --name static-builder dunglas/frankenphp:static-builder):/go/src/app/caddy/frankenphp/frankenphp frankenphp-linux-x86_64 ; docker rm static-builder
55+
run: docker cp $(docker create --name static-builder dunglas/frankenphp:static-builder):/go/src/app/dist/frankenphp-linux-x86_64 frankenphp-linux-x86_64 ; docker rm static-builder
5656
-
5757
name: Upload asset
5858
if: github.ref_type == 'tag'
@@ -74,54 +74,26 @@ jobs:
7474
runs-on: macos-latest
7575
env:
7676
HOMEBREW_NO_AUTO_UPDATE: 1
77-
PHP_EXTENSIONS: bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,gd,iconv,intl,mbstring,mbregex,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,pdo_mysql,pdo_pgsql,pdo_sqlite,pgsql,phar,posix,readline,redis,session,simplexml,sockets,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,apcu
7877
steps:
7978
-
8079
uses: actions/checkout@v4
81-
-
82-
uses: actions/checkout@v4
83-
with:
84-
repository: crazywhalecc/static-php-cli
85-
path: static-php-cli
86-
-
87-
name: Install missing system dependencies
88-
run: brew install automake
8980
-
9081
uses: actions/setup-go@v4
9182
with:
9283
go-version: '1.21'
9384
cache-dependency-path: |
9485
go.sum
95-
caddy/go.sum
96-
-
97-
name: Install static-php-cli dependencies
98-
working-directory: static-php-cli/
99-
run: composer install --no-dev -a
100-
-
101-
name: Fetch libraries sources
102-
working-directory: static-php-cli/
103-
run: ./bin/spc fetch --for-extensions="$PHP_EXTENSIONS"
104-
env:
105-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
106-
-
107-
name: Build libphp.a
108-
working-directory: static-php-cli/
109-
run: ./bin/spc build --enable-zts --build-embed "$PHP_EXTENSIONS"
86+
caddy/go.sum
11087
-
111-
name: Set CGO flags
112-
working-directory: static-php-cli/
88+
name: Set FRANKENPHP_VERSION
11389
run: |
114-
if [ "$GITHUB_REF_TYPE" == "tag" ]; then export FRANKENPHP_VERSION=${GITHUB_REF_NAME:1}; else export FRANKENPHP_VERSION=$GITHUB_SHA; fi
115-
echo "CGO_CFLAGS=-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $(./buildroot/bin/php-config --includes | sed s#-I/#-I$PWD/buildroot/#g)" >> "$GITHUB_ENV"
116-
echo "CGO_LDFLAGS=-framework CoreFoundation -framework SystemConfiguration $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)" >> "$GITHUB_ENV"
117-
echo "PHP_VERSION=$(./buildroot/bin/php-config --version)" >> "$GITHUB_ENV"
118-
echo "FRANKENPHP_VERSION=$FRANKENPHP_VERSION" >> "$GITHUB_ENV"
90+
if [ "$GITHUB_REF_TYPE" == "tag" ]; then export FRANKENPHP_VERSION=${GITHUB_REF_NAME:1}; else export FRANKENPHP_VERSION=$GITHUB_SHA; fi
91+
echo "FRANKENPHP_VERSION=$FRANKENPHP_VERSION" >> "$GITHUB_ENV"
11992
-
12093
name: Build FrankenPHP
121-
working-directory: caddy/frankenphp/
122-
run: |
123-
go build -buildmode=pie -tags "cgo netgo osusergo static_build" -ldflags "-linkmode=external -extldflags -static-pie -w -s -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" -o frankenphp-mac-x86_64
124-
./frankenphp-mac-x86_64 version
94+
run: ./build-static.sh
95+
env:
96+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12597
-
12698
name: Upload asset
12799
if: github.ref_type == 'tag'
@@ -131,9 +103,9 @@ jobs:
131103
allowUpdates: true
132104
omitBodyDuringUpdate: true
133105
omitNameDuringUpdate: true
134-
artifacts: caddy/frankenphp/frankenphp-mac-x86_64
106+
artifacts: dist/frankenphp-mac-x86_64
135107
-
136108
name: Upload binary
137109
uses: actions/upload-artifact@v3
138110
with:
139-
path: caddy/frankenphp/frankenphp-mac-x86_64
111+
path: dist/frankenphp-mac-x86_64

build-mac-arm.sh

Lines changed: 0 additions & 58 deletions
This file was deleted.

build-static.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR
5+
set -o xtrace
6+
7+
if ! type "git" > /dev/null; then
8+
echo "The \"git\" command must be installed."
9+
exit 1
10+
fi
11+
12+
if [ -z "$PHP_EXTENSIONS" ]; then
13+
export PHP_EXTENSIONS="apcu,bcmath,bz2,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,gd,iconv,intl,ldap,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,pdo_mysql,pdo_pgsql,pdo_sqlite,pgsql,phar,posix,readline,redis,session,simplexml,sockets,sqlite3,sysvsem,tokenizer,xml,xmlreader,xmlwriter,zip,zlib"
14+
fi
15+
16+
if [ -z "$PHP_EXTENSIONS_LIB" ]; then
17+
export PHP_EXTENSION_LIBS="freetype,libjpeg,libavif,libwebp,libzip,bzip2"
18+
fi
19+
20+
if [ -z "$PHP_VERSION" ]; then
21+
export PHP_VERSION="8.3"
22+
fi
23+
24+
if [ -z "$FRANKENPHP_VERSION" ]; then
25+
FRANKENPHP_VERSION="$(git rev-parse --verify HEAD)"
26+
export FRANKENPHP_VERSION
27+
elif [ -d ".git/" ]; then
28+
CURRENT_REF="$(git rev-parse --abbrev-ref HEAD)"
29+
export CURRENT_REF
30+
git checkout "$FRANKENPHP_VERSION"
31+
fi
32+
33+
arch="$(uname -m)"
34+
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
35+
if [ "$os" = "darwin" ]; then
36+
os="mac"
37+
fi
38+
bin="frankenphp-$os-$arch"
39+
40+
mkdir -p dist/
41+
cd dist/
42+
43+
if [ -d "static-php-cli/" ]; then
44+
cd static-php-cli/
45+
git pull
46+
else
47+
git clone --depth 1 https://github.com/crazywhalecc/static-php-cli
48+
cd static-php-cli/
49+
fi
50+
51+
composer install --no-dev -a
52+
53+
54+
if type "brew" > /dev/null; then
55+
packages="composer"
56+
if [ "$RELEASE" ]; then
57+
packages="$packages gh"
58+
fi
59+
60+
brew install --formula --quiet "$packages"
61+
fi
62+
63+
if [ "$os" = "linux" ]; then
64+
extraOpts="--disable-opcache-jit"
65+
fi
66+
67+
./bin/spc doctor
68+
./bin/spc fetch --with-php="$PHP_VERSION" --for-extensions="$PHP_EXTENSIONS"
69+
./bin/spc build --enable-zts --build-embed $extraOpts "$PHP_EXTENSIONS" --with-libs="$PHP_EXTENSION_LIBS"
70+
CGO_CFLAGS="-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $(./buildroot/bin/php-config --includes | sed s#-I/#-I"$PWD"/buildroot/#g)"
71+
export CGO_CFLAGS
72+
73+
if [ "$os" = "mac" ]; then
74+
export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration"
75+
fi
76+
77+
CGO_LDFLAGS="$CGO_LDFLAGS $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)"
78+
export CGO_LDFLAGS
79+
80+
LIBPHP_VERSION="$(./buildroot/bin/php-config --version)"
81+
export LIBPHP_VERSION
82+
83+
cd ../../caddy/frankenphp/
84+
go env
85+
go build -buildmode=pie -tags "cgo netgo osusergo static_build" -ldflags "-linkmode=external -extldflags -static-pie -w -s -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $LIBPHP_VERSION Caddy'" -o "../../dist/$bin"
86+
87+
cd ../../dist/
88+
"./$bin" version
89+
90+
if [ "$RELEASE" ]; then
91+
gh release upload "$FRANKENPHP_VERSION" "$bin" --repo dunglas/frankenphp --clobber
92+
fi
93+
94+
if [ "$CURRENT_REF" ]; then
95+
git checkout "$CURRENT_REF"
96+
fi

docs/docker.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ docker run -v $PWD:/app/public -p 80:80 -p 443:443 my-php-app
9393
With Docker Compose:
9494

9595
```yaml
96-
# compose.yml
97-
98-
version: '3.1'
96+
# compose.yaml
9997

10098
services:
10199
php:

docs/static.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ We provide a Docker image to build a Linux static binary:
1111

1212
```console
1313
docker buildx bake --load static-builder
14-
docker cp $(docker create --name static-builder dunglas/frankenphp:static-builder):/go/src/app/caddy/frankenphp/frankenphp frankenphp ; docker rm static-builder
14+
docker cp $(docker create --name static-builder dunglas/frankenphp:static-builder):/go/src/app/dist/frankenphp-linux-$(uname -m) frankenphp ; docker rm static-builder
1515
```
1616

1717
The resulting static binary is named `frankenphp` and is available in the current directory.
1818

19-
If you want to build the static binary without Docker, take a look to the `static-builder.Dockerfile` file.
19+
If you want to build the static binary without Docker, take a look at the macOS instructions, which also works for Linux.
2020

2121
### Custom Extensions
2222

@@ -54,22 +54,14 @@ GITHUB_TOKEN="xxx" docker --load buildx bake static-builder
5454

5555
## macOS
5656

57-
Run the following command to create a static binary for macOS:
57+
Run the following script to create a static binary for macOS (you must have [Homebrew](https://brew.sh/) installed):
5858

5959
```console
60-
git clone --depth=1 https://github.com/crazywhalecc/static-php-cli.git
61-
cd static-php-cli
62-
composer install --no-dev -a
63-
./bin/spc doctor --auto-fix
64-
export PHP_EXTENSIONS="bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,gd,iconv,intl,mbstring,mbregex,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,pdo_mysql,pdo_pgsql,pdo_sqlite,pgsql,phar,posix,readline,redis,session,simplexml,sockets,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,apcu"
65-
./bin/spc fetch --for-extensions="$PHP_EXTENSIONS"
66-
./bin/spc build --enable-zts --build-embed "$PHP_EXTENSIONS"
67-
export CGO_CFLAGS="$(./buildroot/bin/php-config --includes | sed s#-I/#-I$PWD/buildroot/#g)"
68-
export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)"
69-
70-
git clone --depth=1 https://github.com/dunglas/frankenphp.git
71-
cd frankenphp/caddy/frankenphp
72-
go build -buildmode=pie -tags "cgo netgo osusergo static_build" -ldflags "-linkmode=external -extldflags -static-pie"
60+
git clone https://github.com/dunglas/frankenphp
61+
cd frankenphp
62+
./build-static.sh
7363
```
7464

65+
Note: this script also works on Linux (and probably on other Unixes), and is used internally by the Docker based static builder we provide.
66+
7567
See [the list of supported extensions](https://static-php.dev/en/guide/extensions.html).

release.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ git commit -S -a -m "chore: prepare release $1" || echo "skip"
3737
git tag -s -m "Version $1" "v$1"
3838
git tag -s -m "Version $1" "caddy/v$1"
3939
git push --follow-tags
40+
41+
if [ "$(uname -s)" = "Darwin" ]; then
42+
FRANKENPHP_VERSION=$1 RELEASE=1 ./build-static.sh
43+
fi

static-builder.Dockerfile

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# syntax=docker/dockerfile:1
22
FROM golang-base
33

4-
ARG FRANKENPHP_VERSION='dev'
5-
ARG PHP_VERSION='8.3'
6-
ARG PHP_EXTENSIONS='apcu,bcmath,bz2,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,gd,iconv,intl,ldap,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,pdo_mysql,pdo_pgsql,pdo_sqlite,pgsql,phar,posix,readline,redis,session,simplexml,sockets,sqlite3,sysvsem,tokenizer,xml,xmlreader,xmlwriter,zip,zlib'
7-
ARG PHP_EXTENSION_LIBS='freetype,libjpeg,libavif,libwebp,libzip,bzip2'
4+
ARG FRANKENPHP_VERSION=''
5+
ARG PHP_VERSION=''
6+
ARG PHP_EXTENSIONS=''
7+
ARG PHP_EXTENSION_LIBS=''
88

99
RUN apk update; \
1010
apk add --no-cache \
@@ -55,17 +55,6 @@ ENV PATH="${PATH}:/root/.composer/vendor/bin"
5555

5656
COPY --from=composer/composer:2-bin --link /composer /usr/bin/composer
5757

58-
WORKDIR /static-php-cli
59-
60-
RUN git clone --depth=1 https://github.com/crazywhalecc/static-php-cli . && \
61-
composer install --no-cache --no-dev --classmap-authoritative
62-
63-
RUN --mount=type=secret,id=github-token GITHUB_TOKEN=$(cat /run/secrets/github-token) ./bin/spc download --with-php=$PHP_VERSION --for-extensions="$PHP_EXTENSIONS"
64-
65-
RUN ./bin/spc build --build-embed --enable-zts --disable-opcache-jit "$PHP_EXTENSIONS" --with-libs="$PHP_EXTENSION_LIBS"
66-
67-
ENV PATH="/static-php-cli/buildroot/bin:/static-php-cli/buildroot/usr/bin:$PATH"
68-
6958
WORKDIR /go/src/app
7059

7160
COPY go.mod go.sum ./
@@ -80,9 +69,4 @@ COPY *.* ./
8069
COPY caddy caddy
8170
COPY C-Thread-Pool C-Thread-Pool
8271

83-
RUN cd caddy/frankenphp && \
84-
CGO_CFLAGS="$(/static-php-cli/buildroot/bin/php-config --includes | sed s#-I/#-I/static-php-cli/buildroot/#g)" \
85-
CGO_LDFLAGS="-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $(/static-php-cli/buildroot/bin/php-config --ldflags) -Wl,--start-group $(/static-php-cli/buildroot/bin/php-config --libs | sed -e 's/-lgcc_s//g') -Wl,--end-group" \
86-
LIBPHP_VERSION="$(/static-php-cli/buildroot/bin/php-config --version)" \
87-
go build -buildmode=pie -tags "cgo netgo osusergo static_build" -ldflags "-linkmode=external -extldflags -static-pie -s -w -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $LIBPHP_VERSION Caddy'" && \
88-
./frankenphp version
72+
RUN --mount=type=secret,id=github-token GITHUB_TOKEN=$(cat /run/secrets/github-token) ./build-static.sh

0 commit comments

Comments
 (0)