diff --git a/.dockerignore b/.dockerignore index bfc4096580..5fc0bf3440 100644 --- a/.dockerignore +++ b/.dockerignore @@ -9,3 +9,4 @@ !**/*.h !testdata/*.php !testdata/*.txt +!build-static.sh diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 0eec065cda..2dd0da9e88 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -52,7 +52,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Copy binary - 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 + 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 - name: Upload asset if: github.ref_type == 'tag' @@ -74,54 +74,26 @@ jobs: runs-on: macos-latest env: HOMEBREW_NO_AUTO_UPDATE: 1 - 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 steps: - uses: actions/checkout@v4 - - - uses: actions/checkout@v4 - with: - repository: crazywhalecc/static-php-cli - path: static-php-cli - - - name: Install missing system dependencies - run: brew install automake - uses: actions/setup-go@v4 with: go-version: '1.21' cache-dependency-path: | go.sum - caddy/go.sum - - - name: Install static-php-cli dependencies - working-directory: static-php-cli/ - run: composer install --no-dev -a - - - name: Fetch libraries sources - working-directory: static-php-cli/ - run: ./bin/spc fetch --for-extensions="$PHP_EXTENSIONS" - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Build libphp.a - working-directory: static-php-cli/ - run: ./bin/spc build --enable-zts --build-embed "$PHP_EXTENSIONS" + caddy/go.sum - - name: Set CGO flags - working-directory: static-php-cli/ + name: Set FRANKENPHP_VERSION run: | - if [ "$GITHUB_REF_TYPE" == "tag" ]; then export FRANKENPHP_VERSION=${GITHUB_REF_NAME:1}; else export FRANKENPHP_VERSION=$GITHUB_SHA; fi - echo "CGO_CFLAGS=-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $(./buildroot/bin/php-config --includes | sed s#-I/#-I$PWD/buildroot/#g)" >> "$GITHUB_ENV" - echo "CGO_LDFLAGS=-framework CoreFoundation -framework SystemConfiguration $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)" >> "$GITHUB_ENV" - echo "PHP_VERSION=$(./buildroot/bin/php-config --version)" >> "$GITHUB_ENV" - echo "FRANKENPHP_VERSION=$FRANKENPHP_VERSION" >> "$GITHUB_ENV" + if [ "$GITHUB_REF_TYPE" == "tag" ]; then export FRANKENPHP_VERSION=${GITHUB_REF_NAME:1}; else export FRANKENPHP_VERSION=$GITHUB_SHA; fi + echo "FRANKENPHP_VERSION=$FRANKENPHP_VERSION" >> "$GITHUB_ENV" - name: Build FrankenPHP - working-directory: caddy/frankenphp/ - run: | - 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 - ./frankenphp-mac-x86_64 version + run: ./build-static.sh + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Upload asset if: github.ref_type == 'tag' @@ -131,9 +103,9 @@ jobs: allowUpdates: true omitBodyDuringUpdate: true omitNameDuringUpdate: true - artifacts: caddy/frankenphp/frankenphp-mac-x86_64 + artifacts: dist/frankenphp-mac-x86_64 - name: Upload binary uses: actions/upload-artifact@v3 with: - path: caddy/frankenphp/frankenphp-mac-x86_64 + path: dist/frankenphp-mac-x86_64 diff --git a/build-mac-arm.sh b/build-mac-arm.sh deleted file mode 100755 index c32e671566..0000000000 --- a/build-mac-arm.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/sh - -set -o errexit -trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR -set -o errtrace -set -o pipefail -set -o xtrace - -if ! type "git" > /dev/null; then - echo "The \"git\" command must be installed." - exit 1 -fi - -export CURRENT_REF="$(git rev-parse --abbrev-ref HEAD)" - -if [ -n "$1" ]; then - export TAG="1" - export FRANKENPHP_VERSION="$1" - git checkout $FRANKENPHP_VERSION -else - export TAG="0" - export FRANKENPHP_VERSION="$(git rev-parse --verify HEAD)" -fi - -export PHP_EXTENSIONS="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" - -brew install --formula --quiet automake cmake composer go gh - -mkdir -p dist/ -cd dist/ - -if [ -d "static-php-cli/" ]; then - cd static-php-cli/ - git pull -else - git clone --depth 1 https://github.com/crazywhalecc/static-php-cli - cd static-php-cli/ -fi - -composer install --no-dev -a -./bin/spc fetch --for-extensions="$PHP_EXTENSIONS" -./bin/spc build --enable-zts --build-embed "$PHP_EXTENSIONS" -export CGO_CFLAGS="-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $(./buildroot/bin/php-config --includes | sed s#-I/#-I$PWD/buildroot/#g)" -export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)" -export PHP_VERSION="$(./buildroot/bin/php-config --version)" - -cd ../../caddy/frankenphp/ -go env -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 ../../dist/frankenphp-mac-arm64 - -cd ../../dist/ -./frankenphp-mac-arm64 version - -if [ "$TAG" -eq "1" ]; then - gh release upload $FRANKENPHP_VERSION frankenphp-mac-arm64 --repo dunglas/frankenphp --clobber -fi - -git checkout "$CURRENT_REF" diff --git a/build-static.sh b/build-static.sh new file mode 100755 index 0000000000..74b8c97519 --- /dev/null +++ b/build-static.sh @@ -0,0 +1,90 @@ +#!/bin/sh + +set -o errexit +trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR +set -o xtrace + +if ! type "git" > /dev/null; then + echo "The \"git\" command must be installed." + exit 1 +fi + +if [ -z "$PHP_EXTENSIONS" ]; then + 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" +fi + +if [ -z "$PHP_EXTENSIONS_LIB" ]; then + export PHP_EXTENSION_LIBS="freetype,libjpeg,libavif,libwebp,libzip,bzip2" +fi + +if [ -z "$PHP_VERSION" ]; then + export PHP_VERSION="8.3" +fi + +if [ -z "$FRANKENPHP_VERSION" ]; then + FRANKENPHP_VERSION="$(git rev-parse --verify HEAD)" + export FRANKENPHP_VERSION +elif [ -d ".git/" ]; then + CURRENT_REF="$(git rev-parse --abbrev-ref HEAD)" + export CURRENT_REF + git checkout "$FRANKENPHP_VERSION" +fi + +arch="$(uname -m)" +os="$(uname -s | tr '[:upper:]' '[:lower:]')" +if [ "$os" = "darwin" ]; then + os="mac" +fi +bin="frankenphp-$os-$arch" + +mkdir -p dist/ +cd dist/ + +if [ -d "static-php-cli/" ]; then + cd static-php-cli/ + git pull +else + git clone --depth 1 https://github.com/crazywhalecc/static-php-cli + cd static-php-cli/ +fi + +composer install --no-dev -a + + +if type "brew" > /dev/null; then + if [ "$RELEASE" ]; then + extraPackages="gh" + fi + brew install --formula --quiet composer "$extraPackages" +fi + +./bin/spc doctor +./bin/spc fetch --with-php="$PHP_VERSION" --for-extensions="$PHP_EXTENSIONS" +./bin/spc build --enable-zts --build-embed "$PHP_EXTENSIONS" --with-libs="$PHP_EXTENSION_LIBS" +CGO_CFLAGS="-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $(./buildroot/bin/php-config --includes | sed s#-I/#-I"$PWD"/buildroot/#g)" +export CGO_CFLAGS + +if [ "$os" = "mac" ]; then + export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration" +fi + +CGO_LDFLAGS="$CGO_LDFLAGS $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)" +export CGO_LDFLAGS + +LIBPHP_VERSION="$(./buildroot/bin/php-config --version)" +export LIBPHP_VERSION + +cd ../../caddy/frankenphp/ +go env +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" + +cd ../../dist/ +"./$bin" version + +if [ "$RELEASE" ]; then + gh release upload "$FRANKENPHP_VERSION" "$bin" --repo dunglas/frankenphp --clobber +fi + +if [ "$CURRENT_REF" ]; then + git checkout "$CURRENT_REF" +fi diff --git a/docs/docker.md b/docs/docker.md index c960ee423a..3baa4c8dc0 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -93,9 +93,7 @@ docker run -v $PWD:/app/public -p 80:80 -p 443:443 my-php-app With Docker Compose: ```yaml -# compose.yml - -version: '3.1' +# compose.yaml services: php: diff --git a/docs/static.md b/docs/static.md index be001088ce..11458cb9bf 100644 --- a/docs/static.md +++ b/docs/static.md @@ -11,12 +11,12 @@ We provide a Docker image to build a Linux static binary: ```console docker buildx bake --load static-builder -docker cp $(docker create --name static-builder dunglas/frankenphp:static-builder):/go/src/app/caddy/frankenphp/frankenphp frankenphp ; docker rm static-builder +docker cp $(docker create --name static-builder dunglas/frankenphp:static-builder):/go/src/app/dist/frankenphp-linux-$(uname -m) frankenphp ; docker rm static-builder ``` The resulting static binary is named `frankenphp` and is available in the current directory. -If you want to build the static binary without Docker, take a look to the `static-builder.Dockerfile` file. +If you want to build the static binary without Docker, take a look at the macOS instructions, which also works for Linux. ### Custom Extensions @@ -54,22 +54,14 @@ GITHUB_TOKEN="xxx" docker --load buildx bake static-builder ## macOS -Run the following command to create a static binary for macOS: +Run the following script to create a static binary for macOS (you must have [Homebrew](https://brew.sh/) installed): ```console -git clone --depth=1 https://github.com/crazywhalecc/static-php-cli.git -cd static-php-cli -composer install --no-dev -a -./bin/spc doctor --auto-fix -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" -./bin/spc fetch --for-extensions="$PHP_EXTENSIONS" -./bin/spc build --enable-zts --build-embed "$PHP_EXTENSIONS" -export CGO_CFLAGS="$(./buildroot/bin/php-config --includes | sed s#-I/#-I$PWD/buildroot/#g)" -export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)" - -git clone --depth=1 https://github.com/dunglas/frankenphp.git -cd frankenphp/caddy/frankenphp -go build -buildmode=pie -tags "cgo netgo osusergo static_build" -ldflags "-linkmode=external -extldflags -static-pie" +git clone https://github.com/dunglas/frankenphp +cd frankenphp +./build-static.sh ``` +Note: this script also works on Linux (and probably on other Unixes), and is used internally by the Docker based static builder we provide. + See [the list of supported extensions](https://static-php.dev/en/guide/extensions.html). diff --git a/release.sh b/release.sh index 8669538376..6343cb1d59 100755 --- a/release.sh +++ b/release.sh @@ -37,3 +37,7 @@ git commit -S -a -m "chore: prepare release $1" || echo "skip" git tag -s -m "Version $1" "v$1" git tag -s -m "Version $1" "caddy/v$1" git push --follow-tags + +if [ "$(uname -s)" = "Darwin" ]; then + FRANKENPHP_VERSION=$1 RELEASE=1 ./build-static.sh +fi diff --git a/static-builder.Dockerfile b/static-builder.Dockerfile index c9f2399fa8..b68aed9b8d 100644 --- a/static-builder.Dockerfile +++ b/static-builder.Dockerfile @@ -1,10 +1,10 @@ # syntax=docker/dockerfile:1 FROM golang-base -ARG FRANKENPHP_VERSION='dev' -ARG PHP_VERSION='8.3' -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' -ARG PHP_EXTENSION_LIBS='freetype,libjpeg,libavif,libwebp,libzip,bzip2' +ARG FRANKENPHP_VERSION='' +ARG PHP_VERSION='' +ARG PHP_EXTENSIONS='' +ARG PHP_EXTENSION_LIBS='' RUN apk update; \ apk add --no-cache \ @@ -55,17 +55,6 @@ ENV PATH="${PATH}:/root/.composer/vendor/bin" COPY --from=composer/composer:2-bin --link /composer /usr/bin/composer -WORKDIR /static-php-cli - -RUN git clone --depth=1 https://github.com/crazywhalecc/static-php-cli . && \ - composer install --no-cache --no-dev --classmap-authoritative - -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" - -RUN ./bin/spc build --build-embed --enable-zts --disable-opcache-jit "$PHP_EXTENSIONS" --with-libs="$PHP_EXTENSION_LIBS" - -ENV PATH="/static-php-cli/buildroot/bin:/static-php-cli/buildroot/usr/bin:$PATH" - WORKDIR /go/src/app COPY go.mod go.sum ./ @@ -80,9 +69,4 @@ COPY *.* ./ COPY caddy caddy COPY C-Thread-Pool C-Thread-Pool -RUN cd caddy/frankenphp && \ - CGO_CFLAGS="$(/static-php-cli/buildroot/bin/php-config --includes | sed s#-I/#-I/static-php-cli/buildroot/#g)" \ - 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" \ - LIBPHP_VERSION="$(/static-php-cli/buildroot/bin/php-config --version)" \ - 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'" && \ - ./frankenphp version +RUN --mount=type=secret,id=github-token GITHUB_TOKEN=$(cat /run/secrets/github-token) ./build-static.sh