From 9bd6f4ed52e15af99a21005aabff912997128609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sun, 25 Oct 2026 17:03:11 +0100 Subject: [PATCH] feat(static): pass extra Go build flags, ldflags and tags Static builds couldn't set Go linker variables such as EmbeddedAppPath, or add build tags, since static-php-cli generates the Go flags. Expose GO_BUILD_FLAGS, GO_LDFLAGS and GO_TAGS, forwarded to static-php-cli which merges them into its own flags, and document how to fix the embedded app extraction path. --- build-static.sh | 12 ++++++++++++ craft.yml | 3 +++ docs/embed.md | 14 ++++++++++++++ docs/static.md | 14 ++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/build-static.sh b/build-static.sh index 5aa4a04c8c..4261edd356 100755 --- a/build-static.sh +++ b/build-static.sh @@ -16,6 +16,9 @@ set -o pipefail # - PHP_EXTENSIONS: comma-separated extensions (default: the list in craft.yml) # - PHP_EXTENSION_LIBS: comma-separated extra libraries (default: the list in craft.yml) # - XCADDY_ARGS: extra Caddy modules to pass to xcaddy (default: the list in craft.yml) +# - GO_BUILD_FLAGS: extra flags to pass to go build (default: none) +# - GO_LDFLAGS: extra Go linker flags, without double quotes (default: none) +# - GO_TAGS: extra comma-separated Go build tags (default: none) # - FRANKENPHP_VERSION: FrankenPHP version (default: current Git commit) # - EMBED: path to the PHP app to embed (default: none) # - CLEAN: when set, rebuild everything from scratch (default: none) @@ -213,6 +216,15 @@ while IFS= read -r line || [ -n "${line}" ]; do *"SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES:"*) [ -n "${XCADDY_ARGS}" ] && line=" SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES: \"${XCADDY_ARGS}\"" ;; + *"SPC_CMD_VAR_FRANKENPHP_GO_BUILD_FLAGS:"*) + line=" SPC_CMD_VAR_FRANKENPHP_GO_BUILD_FLAGS: \"${GO_BUILD_FLAGS}\"" + ;; + *"SPC_CMD_VAR_FRANKENPHP_GO_LDFLAGS:"*) + line=" SPC_CMD_VAR_FRANKENPHP_GO_LDFLAGS: \"${GO_LDFLAGS}\"" + ;; + *"SPC_CMD_VAR_FRANKENPHP_GO_TAGS:"*) + line=" SPC_CMD_VAR_FRANKENPHP_GO_TAGS: \"${GO_TAGS}\"" + ;; esac printf '%s\n' "${line}" >>"${craft}" diff --git a/craft.yml b/craft.yml index a844f9c517..029115153d 100644 --- a/craft.yml +++ b/craft.yml @@ -30,6 +30,9 @@ download-options: extra-env: SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES: "--with github.com/dunglas/caddy-cbrotli --with github.com/dunglas/mercure/caddy --with github.com/dunglas/vulcain/caddy" + SPC_CMD_VAR_FRANKENPHP_GO_BUILD_FLAGS: "" + SPC_CMD_VAR_FRANKENPHP_GO_LDFLAGS: "" + SPC_CMD_VAR_FRANKENPHP_GO_TAGS: "" EXTENSION_DIR: "/usr/lib/frankenphp/modules" OPENSSLDIR: "/etc/ssl" diff --git a/docs/embed.md b/docs/embed.md index 8acf6a0965..b144c38fec 100644 --- a/docs/embed.md +++ b/docs/embed.md @@ -131,6 +131,20 @@ You can also run the PHP CLI scripts embedded in your binary: ./my-app php-cli bin/console ``` +## Extraction path + +At startup, the embedded app is extracted to a private temporary directory whose name changes +with the content of the app. Some apps need a fixed path, for instance because they ship +precompiled code referencing absolute paths, such as an OPcache file cache. +Set it at build time with Go linker flags: + +```console +EMBED=/path/to/your/app GO_LDFLAGS="-X github.com/dunglas/frankenphp.EmbeddedAppPath=/opt/my-app" ./build-static.sh +``` + +Files already present at this path are kept as is: it must only be writable by the user +running the binary. + ## PHP extensions By default, the script will build extensions required by the `composer.json` file of your project, if any. diff --git a/docs/static.md b/docs/static.md index bf79e2888b..2a3f7c8740 100644 --- a/docs/static.md +++ b/docs/static.md @@ -97,6 +97,20 @@ In this example, we add the [Souin](https://souin.io) HTTP cache module for Cadd > The cbrotli, Mercure, and Vulcain modules are included by default if `XCADDY_ARGS` is empty or not set. > If you customize the value of `XCADDY_ARGS`, you must include them explicitly if you want them to be included. +### Go build flags + +To pass extra flags to the Go build, use the `GO_BUILD_FLAGS`, `GO_LDFLAGS` (linker flags, without double quotes) and `GO_TAGS` (comma-separated build tags) variables. +Linker flags and tags are merged with the ones FrankenPHP needs: + +```console +docker run --rm -v "$PWD/output:/output" -e OUTPUT_DIR=/output -e SPC_LOGS_DIR=/output/log \ + -e GO_BUILD_FLAGS=-trimpath \ + -e GO_LDFLAGS="-X github.com/dunglas/frankenphp.EmbeddedAppPath=/opt/my-app" \ + dunglas/frankenphp:static-builder-musl +``` + +These variables are applied by static-php-cli (`SPC_CMD_VAR_FRANKENPHP_GO_*`), older versions ignore them. + See also how to [customize the FrankenPHP static build](#customizing-the-frankenphp-static-build) ### GitHub token