Skip to content

Commit de5de14

Browse files
authored
ci: add generic static build script (#322)
1 parent b32e738 commit de5de14

8 files changed

+131
-136
lines changed

.dockerignore

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

+10-38
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

-58
This file was deleted.

build-static.sh

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
arch="$(uname -m)"
13+
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
14+
if [ "$os" = "darwin" ]; then
15+
os="mac"
16+
fi
17+
18+
if [ -z "$PHP_EXTENSIONS" ]; then
19+
if [ "$os" = "mac" ]; then
20+
# Temporary fix for https://github.com/crazywhalecc/static-php-cli/issues/278 (remove pdo_pgsql, pgsql and ldap)
21+
export PHP_EXTENSIONS="apcu,bcmath,bz2,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,gd,iconv,intl,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,pdo_mysql,pdo_sqlite,phar,posix,readline,redis,session,simplexml,sockets,sqlite3,sysvsem,tokenizer,xml,xmlreader,xmlwriter,zip,zlib"
22+
else
23+
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"
24+
fi
25+
fi
26+
27+
if [ -z "$PHP_EXTENSIONS_LIB" ]; then
28+
export PHP_EXTENSION_LIBS="freetype,libjpeg,libavif,libwebp,libzip,bzip2"
29+
fi
30+
31+
if [ -z "$PHP_VERSION" ]; then
32+
export PHP_VERSION="8.3"
33+
fi
34+
35+
if [ -z "$FRANKENPHP_VERSION" ]; then
36+
FRANKENPHP_VERSION="$(git rev-parse --verify HEAD)"
37+
export FRANKENPHP_VERSION
38+
elif [ -d ".git/" ]; then
39+
CURRENT_REF="$(git rev-parse --abbrev-ref HEAD)"
40+
export CURRENT_REF
41+
git checkout "$FRANKENPHP_VERSION"
42+
fi
43+
44+
bin="frankenphp-$os-$arch"
45+
46+
mkdir -p dist/
47+
cd dist/
48+
49+
if [ -d "static-php-cli/" ]; then
50+
cd static-php-cli/
51+
git pull
52+
else
53+
git clone --depth 1 https://github.com/crazywhalecc/static-php-cli
54+
cd static-php-cli/
55+
fi
56+
57+
composer install --no-dev -a
58+
59+
60+
if type "brew" > /dev/null; then
61+
packages="composer"
62+
if [ "$RELEASE" ]; then
63+
packages="$packages gh"
64+
fi
65+
66+
brew install --formula --quiet "$packages"
67+
fi
68+
69+
if [ "$os" = "linux" ]; then
70+
extraOpts="--disable-opcache-jit"
71+
fi
72+
73+
./bin/spc doctor
74+
./bin/spc fetch --with-php="$PHP_VERSION" --for-extensions="$PHP_EXTENSIONS"
75+
./bin/spc build --enable-zts --build-embed $extraOpts "$PHP_EXTENSIONS" --with-libs="$PHP_EXTENSION_LIBS"
76+
CGO_CFLAGS="-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $(./buildroot/bin/php-config --includes | sed s#-I/#-I"$PWD"/buildroot/#g)"
77+
export CGO_CFLAGS
78+
79+
if [ "$os" = "mac" ]; then
80+
export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration"
81+
fi
82+
83+
CGO_LDFLAGS="$CGO_LDFLAGS $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)"
84+
export CGO_LDFLAGS
85+
86+
LIBPHP_VERSION="$(./buildroot/bin/php-config --version)"
87+
export LIBPHP_VERSION
88+
89+
cd ../../caddy/frankenphp/
90+
go env
91+
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"
92+
93+
cd ../../dist/
94+
"./$bin" version
95+
96+
if [ "$RELEASE" ]; then
97+
gh release upload "$FRANKENPHP_VERSION" "$bin" --repo dunglas/frankenphp --clobber
98+
fi
99+
100+
if [ "$CURRENT_REF" ]; then
101+
git checkout "$CURRENT_REF"
102+
fi

docs/docker.md

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

+8-16
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

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

+5-21
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)