From b7098f740885b1d74801852ec1ee2d4065c8bd39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 14 Sep 2023 01:10:20 +0200 Subject: [PATCH] feat: Apple Silicon static build and better version name (#205) * feat: Apple Silicon static build and better version name Build a static binary for macOS and Apple Silicon. Also add the PHP version to the version command. * fix CircleCI Composer cache key * fix export * fix and optims * cleanup --- .circleci/config.yml | 57 ++++++++++++++++++++++++++++++++++++ .github/workflows/static.yml | 25 +++++++++------- .github/workflows/tests.yml | 2 +- Dockerfile | 27 +++++++++-------- alpine.Dockerfile | 31 ++++++++++---------- static-builder.Dockerfile | 6 +++- 6 files changed, 108 insertions(+), 40 deletions(-) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..f24e926 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,57 @@ +version: 2.1 + +jobs: + build: + macos: + xcode: 14.3.1 + environment: + HOMEBREW_NO_AUTO_UPDATE: 1 + + steps: + - checkout + - run: brew install --formula go automake cmake composer + - run: + name: Clone static-php-cli + command: git clone --depth 1 https://github.com/crazywhalecc/static-php-cli + - restore_cache: + keys: + - spc-{{ checksum "static-php-cli/composer.json" }} + - run: + name: Install static-php-cli and fetch libraries sources + working_directory: static-php-cli/ + command: | + composer install --no-dev -a + ./bin/spc fetch --with-php=8.2 -A + - save_cache: + key: spc-{{ checksum "static-php-cli/composer.json" }} + paths: + - save_cache:tatic-php-cli/downloads/ + - static-php-cli/vendor/ + - run: + working_directory: static-php-cli/ + name: Build libphp.a + command: ./bin/spc build --enable-zts --build-embed --debug "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" + - run: + working_directory: static-php-cli/ + name: Set CGO flags + command: | + echo "export CGO_CFLAGS='$(./buildroot/bin/php-config --includes | sed s#-I/#-I$PWD/buildroot/#g)'" >> "$BASH_ENV" + echo "export CGO_LDFLAGS='-framework CoreFoundation -framework SystemConfiguration $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)'" >> "$BASH_ENV" + echo "export PHP_VERSION='$(./buildroot/bin/php-config --version)'" >> "$BASH_ENV" + - restore_cache: + keys: + - go-mod-v4-{{ checksum "caddy/go.sum" }} + - run: + name: Build FrankenPHP + working_directory: caddy/frankenphp/ + command: | + 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 $CIRCLE_SHA1 PHP $PHP_VERSION Caddy'" + ./frankenphp version + - store_artifacts: + path: caddy/frankenphp/frankenphp + destination: frankenphp-arm64-<> + - save_cache: + key: go-mod-v4-{{ checksum "caddy/go.sum" }} + paths: + - "~/go/pkg/mod" diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 0fce2df..f13baa9 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -34,7 +34,7 @@ jobs: *.cache-from=type=gha,scope=refs/heads/main-static-builder *.cache-to=type=gha,scope=${{github.ref}}-static-builder env: - VERSION: ${{github.ref_name}} + VERSION: ${{ github.sha }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Copy binary @@ -43,12 +43,14 @@ jobs: - name: Upload binary uses: actions/upload-artifact@v3 with: - name: frankenphp-linux-x86_64-dev + name: frankenphp-linux-x86_64-${{ github.sha }} path: frankenphp build-mac: - name: Build macOS binaries + name: Build macOS x86_64 binaries runs-on: macos-latest + env: + HOMEBREW_NO_AUTO_UPDATE: 1 steps: - uses: actions/checkout@v4 @@ -58,6 +60,10 @@ jobs: repository: crazywhalecc/static-php-cli path: static-php-cli + - name: Install missing system dependencies + run: brew install automake + working-directory: static-php-cli/ + - uses: actions/setup-go@v4 with: go-version: '1.21' @@ -66,17 +72,13 @@ jobs: working-directory: static-php-cli/ run: composer install --no-dev -a - - name: Install missing system dependencies - run: ./bin/spc doctor --auto-fix - working-directory: static-php-cli/ - - name: Fetch libraries sources working-directory: static-php-cli/ run: ./bin/spc fetch --with-php=8.2 -A env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Build static libphp + - name: Build libphp.a working-directory: static-php-cli/ run: ./bin/spc build --enable-zts --build-embed --debug "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" @@ -85,13 +87,16 @@ jobs: run: | echo "CGO_CFLAGS=$(./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" - 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" + 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 ${{ github.sha }} PHP $PHP_VERSION Caddy'" + ./frankenphp version - name: Upload binary uses: actions/upload-artifact@v3 with: - name: frankenphp-mac-x86_64-dev + name: frankenphp-mac-x86_64-${{ github.sha }} path: caddy/frankenphp/frankenphp diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 20bb1c1..2e87a10 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -22,7 +22,7 @@ jobs: env: phpts: ts - - name: Set include flags + - name: Set CGO flags run: echo "CGO_CFLAGS=$(php-config --includes)" >> "$GITHUB_ENV" - name: Build diff --git a/Dockerfile b/Dockerfile index c0cb8e4..d30124f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,17 +11,17 @@ ENV PATH /usr/local/go/bin:$PATH # This is required to link the FrankenPHP binary to the PHP binary RUN apt-get update && \ apt-get -y --no-install-recommends install \ - libargon2-dev \ - libcurl4-openssl-dev \ - libonig-dev \ - libreadline-dev \ - libsodium-dev \ - libsqlite3-dev \ - libssl-dev \ - libxml2-dev \ - zlib1g-dev \ - && \ - apt-get clean + libargon2-dev \ + libcurl4-openssl-dev \ + libonig-dev \ + libreadline-dev \ + libsodium-dev \ + libsqlite3-dev \ + libssl-dev \ + libxml2-dev \ + zlib1g-dev \ + && \ + apt-get clean WORKDIR /go/src/app @@ -44,9 +44,10 @@ COPY testdata testdata ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" CGO_CFLAGS=$PHP_CFLAGS CGO_CPPFLAGS=$PHP_CPPFLAGS RUN cd caddy/frankenphp && \ - go build -ldflags "-X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION Caddy'" && \ + go build -ldflags "-X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \ cp frankenphp /usr/local/bin && \ - cp /go/src/app/caddy/frankenphp/Caddyfile /etc/Caddyfile + cp Caddyfile /etc/Caddyfile && \ + frankenphp version ENTRYPOINT ["/bin/bash","-c"] diff --git a/alpine.Dockerfile b/alpine.Dockerfile index 50a8ec1..760eeaf 100644 --- a/alpine.Dockerfile +++ b/alpine.Dockerfile @@ -9,18 +9,18 @@ COPY --from=golang-base /usr/local/go /usr/local/go ENV PATH /usr/local/go/bin:$PATH RUN apk add --no-cache --virtual .build-deps \ - $PHPIZE_DEPS \ - argon2-dev \ - coreutils \ - curl-dev \ - gnu-libiconv-dev \ - libsodium-dev \ - libxml2-dev \ - linux-headers \ - oniguruma-dev \ - openssl-dev \ - readline-dev \ - sqlite-dev + $PHPIZE_DEPS \ + argon2-dev \ + coreutils \ + curl-dev \ + gnu-libiconv-dev \ + libsodium-dev \ + libxml2-dev \ + linux-headers \ + oniguruma-dev \ + openssl-dev \ + readline-dev \ + sqlite-dev WORKDIR /go/src/app @@ -43,9 +43,10 @@ COPY testdata testdata ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" CGO_CFLAGS=$PHP_CFLAGS CGO_CPPFLAGS=$PHP_CPPFLAGS RUN cd caddy/frankenphp && \ - go build -ldflags "-X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION Caddy'" && \ - cp frankenphp /usr/local/bin && \ - cp /go/src/app/caddy/frankenphp/Caddyfile /etc/Caddyfile + go build -ldflags "-X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \ + cp frankenphp /usr/local/bin && \ + cp Caddyfile /etc/Caddyfile && \ + frankenphp version ENTRYPOINT ["/bin/sh","-c"] diff --git a/static-builder.Dockerfile b/static-builder.Dockerfile index 60f0b32..5afbe90 100644 --- a/static-builder.Dockerfile +++ b/static-builder.Dockerfile @@ -28,12 +28,14 @@ RUN apk update; \ make \ php82 \ php82-common \ + php82-curl \ php82-dom \ php82-mbstring \ php82-openssl \ php82-pcntl \ php82-phar \ php82-posix \ + php82-sodium \ php82-tokenizer \ php82-xml \ php82-xmlwriter \ @@ -76,4 +78,6 @@ 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="$(/static-php-cli/buildroot/bin/php-config --ldflags) $(/static-php-cli/buildroot/bin/php-config --libs | sed -e 's/-lgcc_s//g')" \ - 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 Caddy'" + 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