Skip to content

Commit 0bbd4c6

Browse files
henderkesdunglas
andcommitted
fix: build-static.sh consecutive builds (php#1496)
* fix consecutive builds * use minor version in PHP_VERSION * install jq in centos container * fix "arm64" download arch for spc binary * jq is not available as a rpm download * linter * specify php 8.4 default specify 8.4 so we manually switch to 8.5 when we make sure it works allows to run without jq installed * Apply suggestions from code review Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> --------- Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent 9929383 commit 0bbd4c6

2 files changed

Lines changed: 86 additions & 68 deletions

File tree

build-static.sh

Lines changed: 83 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,20 @@ if [ -n "${DEBUG_SYMBOLS}" ]; then
5757
fi
5858
# php version to build
5959
if [ -z "${PHP_VERSION}" ]; then
60-
export PHP_VERSION="8.4"
60+
get_latest_php_version() {
61+
input="$1"
62+
json=$(curl -s "https://www.php.net/releases/index.php?json&version=$input")
63+
latest=$(echo "$json" | jq -r '.version')
64+
65+
if [[ "$latest" == "$input"* ]]; then
66+
echo "$latest"
67+
else
68+
echo "$input"
69+
fi
70+
}
71+
72+
PHP_VERSION="$(get_latest_php_version "8.4")"
73+
export PHP_VERSION
6174
fi
6275
# default extension set
6376
defaultExtensions="apcu,bcmath,bz2,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,ftp,gd,gmp,gettext,iconv,igbinary,imagick,intl,ldap,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,parallel,pcntl,pdo,pdo_mysql,pdo_pgsql,pdo_sqlite,pgsql,phar,posix,protobuf,readline,redis,session,shmop,simplexml,soap,sockets,sodium,sqlite3,ssh2,sysvmsg,sysvsem,sysvshm,tidy,tokenizer,xlswriter,xml,xmlreader,xmlwriter,zip,zlib,yaml,zstd"
@@ -116,88 +129,90 @@ if [ -n "${CLEAN}" ]; then
116129
go clean -cache
117130
fi
118131

119-
cache_key="${PHP_VERSION}-${PHP_EXTENSIONS}-${PHP_EXTENSION_LIBS}"
132+
mkdir -p dist/
133+
cd dist/
120134

121-
# Build libphp if necessary
122-
if [ -f dist/cache_key ] && [ "$(cat dist/cache_key)" = "${cache_key}" ] && [ -f "dist/static-php-cli/buildroot/lib/libphp.a" ]; then
123-
cd dist/static-php-cli
124-
125-
if [ -f "./spc" ]; then
126-
spcCommand="./spc"
127-
elif [ -f "bin/spc" ]; then
128-
spcCommand="./bin/spc"
135+
if type "brew" >/dev/null 2>&1; then
136+
if ! type "composer" >/dev/null; then
137+
packages="composer"
129138
fi
130-
else
131-
mkdir -p dist/
132-
cd dist/
133-
echo -n "${cache_key}" >cache_key
134-
135-
if type "brew" >/dev/null 2>&1; then
136-
if ! type "composer" >/dev/null; then
137-
packages="composer"
138-
fi
139-
if ! type "go" >/dev/null 2>&1; then
140-
packages="${packages} go"
141-
fi
142-
if [ -n "${RELEASE}" ] && ! type "gh" >/dev/null 2>&1; then
143-
packages="${packages} gh"
144-
fi
145-
146-
if [ -n "${packages}" ]; then
147-
# shellcheck disable=SC2086
148-
brew install --formula --quiet ${packages}
149-
fi
139+
if ! type "go" >/dev/null 2>&1; then
140+
packages="${packages} go"
150141
fi
151-
152-
if [ "${SPC_REL_TYPE}" = "binary" ] && [[ ! "${arch}" =~ arm ]]; then
153-
mkdir -p static-php-cli/
154-
cd static-php-cli/
155-
curl -o spc -fsSL "https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-${arch}"
156-
chmod +x spc
157-
spcCommand="./spc"
158-
elif [ -d "static-php-cli/src" ]; then
159-
cd static-php-cli/
160-
git pull
161-
composer install --no-dev -a
162-
spcCommand="./bin/spc"
163-
else
164-
git clone --depth 1 https://github.com/crazywhalecc/static-php-cli --branch main
165-
cd static-php-cli/
166-
composer install --no-dev -a
167-
spcCommand="./bin/spc"
142+
if [ -n "${RELEASE}" ] && ! type "gh" >/dev/null 2>&1; then
143+
packages="${packages} gh"
168144
fi
169145

170-
# extensions to build
171-
if [ -z "${PHP_EXTENSIONS}" ]; then
172-
# enable EMBED mode, first check if project has dumped extensions
173-
if [ -n "${EMBED}" ] && [ -f "${EMBED}/composer.json" ] && [ -f "${EMBED}/composer.lock" ] && [ -f "${EMBED}/vendor/installed.json" ]; then
174-
cd "${EMBED}"
175-
# read the extensions using spc dump-extensions
176-
PHP_EXTENSIONS=$(${spcCommand} dump-extensions "${EMBED}" --format=text --no-dev --no-ext-output="${defaultExtensions}")
177-
else
178-
PHP_EXTENSIONS="${defaultExtensions}"
179-
fi
146+
if [ -n "${packages}" ]; then
147+
# shellcheck disable=SC2086
148+
brew install --formula --quiet ${packages}
180149
fi
181-
# additional libs to build
182-
if [ -z "${PHP_EXTENSION_LIBS}" ]; then
183-
PHP_EXTENSION_LIBS="${defaultExtensionLibs}"
150+
fi
151+
152+
if [ "${SPC_REL_TYPE}" = "binary" ]; then
153+
mkdir -p static-php-cli/
154+
cd static-php-cli/
155+
if [[ "${arch}" =~ "arm" ]]; then
156+
dl_arch="aarch64"
157+
else
158+
dl_arch="${arch}"
184159
fi
185-
# The Brotli library must always be built as it is required by http://github.com/dunglas/caddy-cbrotli
186-
if ! echo "${PHP_EXTENSION_LIBS}" | grep -q "\bbrotli\b"; then
187-
PHP_EXTENSION_LIBS="${PHP_EXTENSION_LIBS},brotli"
160+
curl -o spc -fsSL "https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-${dl_arch}"
161+
chmod +x spc
162+
spcCommand="./spc"
163+
elif [ -d "static-php-cli/src" ]; then
164+
cd static-php-cli/
165+
git pull
166+
composer install --no-dev -a --no-interaction
167+
spcCommand="./bin/spc"
168+
else
169+
git clone --depth 1 https://github.com/crazywhalecc/static-php-cli --branch main
170+
cd static-php-cli/
171+
composer install --no-dev -a --no-interaction
172+
spcCommand="./bin/spc"
173+
fi
174+
175+
# Extensions to build
176+
if [ -z "${PHP_EXTENSIONS}" ]; then
177+
# enable EMBED mode, first check if project has dumped extensions
178+
if [ -n "${EMBED}" ] && [ -f "${EMBED}/composer.json" ] && [ -f "${EMBED}/composer.lock" ] && [ -f "${EMBED}/vendor/installed.json" ]; then
179+
cd "${EMBED}"
180+
# read the extensions using spc dump-extensions
181+
PHP_EXTENSIONS=$(${spcCommand} dump-extensions "${EMBED}" --format=text --no-dev --no-ext-output="${defaultExtensions}")
182+
else
183+
PHP_EXTENSIONS="${defaultExtensions}"
188184
fi
189-
# The mimalloc library must be built if MIMALLOC is true
190-
if [ -n "${MIMALLOC}" ]; then
191-
if ! echo "${PHP_EXTENSION_LIBS}" | grep -q "\bmimalloc\b"; then
192-
PHP_EXTENSION_LIBS="${PHP_EXTENSION_LIBS},mimalloc"
193-
fi
185+
fi
186+
187+
# Additional libraries to build
188+
if [ -z "${PHP_EXTENSION_LIBS}" ]; then
189+
PHP_EXTENSION_LIBS="${defaultExtensionLibs}"
190+
fi
191+
192+
# The Brotli library must always be built as it is required by http://github.com/dunglas/caddy-cbrotli
193+
if ! echo "${PHP_EXTENSION_LIBS}" | grep -q "\bbrotli\b"; then
194+
PHP_EXTENSION_LIBS="${PHP_EXTENSION_LIBS},brotli"
195+
fi
196+
197+
# The mimalloc library must be built if MIMALLOC is true
198+
if [ -n "${MIMALLOC}" ]; then
199+
if ! echo "${PHP_EXTENSION_LIBS}" | grep -q "\bmimalloc\b"; then
200+
PHP_EXTENSION_LIBS="${PHP_EXTENSION_LIBS},mimalloc"
194201
fi
202+
fi
195203

204+
# Build libphp if necessary
205+
cache_key="${PHP_VERSION}-${PHP_EXTENSIONS}-${PHP_EXTENSION_LIBS}"
206+
if [ -f ../cache_key ] && [ "$(cat ../cache_key)" = "${cache_key}" ] && [ -f "buildroot/lib/libphp.a" ]; then
207+
echo "Hit cache, skipping libphp build."
208+
else
196209
${spcCommand} doctor --auto-fix
197210
# shellcheck disable=SC2086
198211
${spcCommand} download --with-php="${PHP_VERSION}" --for-extensions="${PHP_EXTENSIONS}" --for-libs="${PHP_EXTENSION_LIBS}" ${SPC_OPT_DOWNLOAD_ARGS}
199212
# shellcheck disable=SC2086
200213
${spcCommand} build --enable-zts --build-embed ${SPC_OPT_BUILD_ARGS} "${PHP_EXTENSIONS}" --with-libs="${PHP_EXTENSION_LIBS}"
214+
215+
echo -n "${cache_key}" >../cache_key
201216
fi
202217

203218
if ! type "go" >/dev/null 2>&1; then

static-builder-gnu.Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ RUN yum install -y \
9494
else \
9595
GO_ARCH="amd64" ; \
9696
fi ; \
97+
curl -o jq -fsSL https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-${GO_ARCH} && \
98+
chmod +x jq && \
99+
mv jq /usr/local/bin/jq && \
97100
curl -o go.tgz -fsSL https://go.dev/dl/go${GO_VERSION}.linux-${GO_ARCH}.tar.gz && \
98101
rm -rf /usr/local/go && \
99102
tar -C /usr/local -xzf go.tgz && \

0 commit comments

Comments
 (0)