From 74060cbf278b26747a3fef0488ae099249af396c Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Mon, 16 May 2022 14:01:00 +0800 Subject: [PATCH] add micro php build --- .github/workflows/build-php.yml | 17 ++++++++++++++- docker/Dockerfile | 16 +++++++++++--- docker/check-extensions.sh | 1 + docker/compile-micro.sh | 38 +++++++++++++++++++++++++++++++++ docker/fast-compiler.sh | 8 ++++--- 5 files changed, 73 insertions(+), 7 deletions(-) create mode 100755 docker/compile-micro.sh diff --git a/.github/workflows/build-php.yml b/.github/workflows/build-php.yml index a8034662..fefb6f75 100644 --- a/.github/workflows/build-php.yml +++ b/.github/workflows/build-php.yml @@ -30,10 +30,25 @@ jobs: fi - name: Build PHP ${{ matrix.php-versions }} for ${{ matrix.arch }} run: cd docker/ && docker build . --file Dockerfile --tag static-php --build-arg USE_BACKUP_ADDRESS=yes --build-arg COMPILE_PHP_VERSION=${{ matrix.php-versions }} + - name: Build micro PHP distribution ${{ matrix.php-versions }} for ${{ matrix.arch }} + run: | + if [[ "${{ matrix.arch }}" = "x86_64" ]]; then + MAIN_VERSION=$(echo "${{ matrix.php-versions }}" | awk -F. '{print $1}') + if [[ "$MAIN_VERSION" = "8" ]]; then + cd docker/ && docker build . --file Dockerfile --tag static-micro --build-arg USE_BACKUP_ADDRESS=yes --build-arg COMPILE_PHP_VERSION=${{ matrix.php-versions }} --build-arg COMPILE_MICRO=yes + fi + fi - name: Push PHP ${{ matrix.php-versions }} to dist run: | mkdir dist && docker run --rm -v $(pwd)/dist:/dist/ static-php cp php-dist/bin/php /dist/ - cd dist && tar -zcvf "php-${{ matrix.php-versions }}-static-bin-${{ matrix.arch }}.tar.gz" ./php && rm ./php + cd dist && tar -zcvf "php-${{ matrix.php-versions }}-static-bin-${{ matrix.arch }}.tar.gz" ./php && rm ./php && cd .. + if [[ "${{ matrix.arch }}" = "x86_64" ]]; then + MAIN_VERSION=$(echo "${{ matrix.php-versions }}" | awk -F. '{print $1}') + if [[ "$MAIN_VERSION" = "8" ]]; then + docker run --rm -v $(pwd)/dist:/dist static-micro cp /app/source/php-"${{ matrix.php-versions }}"/sapi/micro/micro.sfx /dist/ + cd dist && tar -zcvf "micro-${{ matrix.php-versions }}-${{ matrix.arch }}.tar.gz" ./micro.sfx && rm ./micro.sfx && cd .. + fi + fi - name: Deploy to Zhamao Server uses: easingthemes/ssh-deploy@main env: diff --git a/docker/Dockerfile b/docker/Dockerfile index 1ac0b275..11aa0ad6 100755 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -2,11 +2,12 @@ FROM alpine:latest # define script basic information # Version of this Dockerfile -ENV SCRIPT_VERSION=1.4.3 +ENV SCRIPT_VERSION=1.5.0 # Download address uses backup address ARG USE_BACKUP_ADDRESS ARG COMPILE_PHP_VERSION +ARG COMPILE_MICRO # (if downloading slowly, consider set it to yes) ENV USE_BACKUP="${USE_BACKUP_ADDRESS}" @@ -14,6 +15,8 @@ ENV USE_BACKUP="${USE_BACKUP_ADDRESS}" # Version of some manually-installed package ENV VER_PHP="${COMPILE_PHP_VERSION}" +ENV ENABLE_MICRO="${COMPILE_MICRO}" + # APK repositories mirror address, if u r not in China, consider set USE_BACKUP=yes to boost ENV LINK_APK_REPO='mirrors.ustc.edu.cn' ENV LINK_APK_REPO_BAK='dl-cdn.alpinelinux.org' @@ -45,6 +48,8 @@ RUN apk add libevent libevent-dev libevent-static > /dev/null RUN apk add sqlite sqlite-dev sqlite-libs sqlite-static > /dev/null # php libzip dependencies RUN apk add bzip2-dev bzip2-static bzip2 +# php micro ffi dependencies +RUN apk add libffi libffi-dev RUN mkdir /app @@ -76,10 +81,15 @@ COPY check-extensions.sh /app/ COPY compile-php.sh /app/ RUN chmod +x ./check-extensions.sh && \ - chmod +x ./compile-php.sh + chmod +x ./compile-php.sh && \ + chmod +x ./compile-micro.sh RUN echo "Checking and Compiling Dependencies ..." RUN ./check-extensions.sh check_before_configure > /dev/null RUN echo "Compiling PHP ..." -RUN ./compile-php.sh ${VER_PHP} > /dev/null 2>&1 +RUN if [ "${ENABLE_MICRO}" = "yes" ]; then \ + ./compile-micro.sh ${VER_PHP} > /dev/null 2>&1 ; \ + else \ + ./compile-php.sh ${VER_PHP} > /dev/null 2>&1 ; \ + fi diff --git a/docker/check-extensions.sh b/docker/check-extensions.sh index 7a1e44c0..30b2e8b4 100755 --- a/docker/check-extensions.sh +++ b/docker/check-extensions.sh @@ -260,6 +260,7 @@ function check_after_configure() { case $loop in swoole) sed -ie 's/swoole_clock_gettime(CLOCK_REALTIME/clock_gettime(CLOCK_REALTIME/g' "$php_dir/ext/swoole/include/swoole.h" + sed -ie 's/strcmp("cli", sapi_module.name) == 0/strcmp("cli", sapi_module.name) == 0 || strcmp("micro", sapi_module.name) == 0/g' "$php_dir/ext/swoole/ext-src/php_swoole.cc" ;; esac done diff --git a/docker/compile-micro.sh b/docker/compile-micro.sh new file mode 100755 index 00000000..423150d0 --- /dev/null +++ b/docker/compile-micro.sh @@ -0,0 +1,38 @@ +#!/bin/sh + +VER_PHP="$1" + +self_dir=$(cd "$(dirname "$0")";pwd) +php_dir=$(find $self_dir/source -name "php-$VER_PHP" -type d | tail -n1) + +function php_compile_args() { + _php_arg="--prefix=$self_dir/php-dist" + _php_arg="$_php_arg --disable-all" + _php_arg="$_php_arg --enable-shared=no" + _php_arg="$_php_arg --enable-static=yes" + _php_arg="$_php_arg --enable-inline-optimization" + _php_arg="$_php_arg --with-layout=GNU" + _php_arg="$_php_arg --with-pear=no" + _php_arg="$_php_arg --disable-cgi" + _php_arg="$_php_arg --disable-phpdbg" + _php_arg="$_php_arg --with-ffi" + _php_arg="$_php_arg --enable-micro" + _php_arg="$_php_arg $($self_dir/check-extensions.sh check_in_configure $1)" + echo $_php_arg +} + +php_compile_args && sleep 1s + +cd $php_dir && \ + git clone https://github.com/dixyes/phpmicro.git --depth=1 sapi/micro && \ + ./buildconf --force && \ + ./configure LDFLAGS=-static $(php_compile_args $VER_PHP) && \ + $self_dir/check-extensions.sh check_after_configure && \ + sed -ie 's/-export-dynamic//g' "Makefile" && \ + sed -ie 's/-o $(SAPI_CLI_PATH)/-all-static -o $(SAPI_CLI_PATH)/g' "Makefile" && \ + sed -ie 's/-o $(SAPI_MICRO_PATH)/-all-static -o $(SAPI_MICRO_PATH)/g' "Makefile" && \ + sed -ie 's/$(PHP_GLOBAL_OBJS) $(PHP_BINARY_OBJS) $(PHP_MICRO_OBJS)/$(PHP_GLOBAL_OBJS:.lo=.o) $(PHP_BINARY_OBJS:.lo=.o) $(PHP_MICRO_OBJS:.lo=.o)/g' "Makefile" && \ + sed -ie 's/$(EXTRA_LIBS:-lresolv=-Wl,-Bstatic,-lresolv,-Bdynamic)/$(EXTRA_LIBS)/g' "Makefile" && \ + make micro LDFLAGS="-static" -j$(cat /proc/cpuinfo | grep processor | wc -l) + #make install + #strip $self_dir/php-dist/bin/php diff --git a/docker/fast-compiler.sh b/docker/fast-compiler.sh index 6acc0c18..62b21bc2 100755 --- a/docker/fast-compiler.sh +++ b/docker/fast-compiler.sh @@ -2,7 +2,7 @@ # This script needs alpine linux system. -test "$VER_PHP" = "" && VER_PHP="7.4.28" +test "$VER_PHP" = "" && VER_PHP="8.1.6" test "$USE_BACKUP" = "" && USE_BACKUP="no" LINK_APK_REPO='mirrors.ustc.edu.cn' @@ -35,8 +35,10 @@ apk add libevent libevent-dev libevent-static apk add sqlite sqlite-dev sqlite-libs sqlite-static # php libzip dependencies apk add bzip2-dev bzip2-static bzip2 +# php micro ffi dependencies +apk add libffi libffi-dev -chmod +x download.sh check-extensions.sh compile-php.sh +chmod +x download.sh check-extensions.sh compile-php.sh compile-micro.sh ./download.sh swoole ${USE_BACKUP} && \ ./download.sh inotify ${USE_BACKUP} && \ @@ -49,5 +51,5 @@ chmod +x download.sh check-extensions.sh compile-php.sh ./download.sh libzip ${USE_BACKUP} && \ ./download.sh php ${USE_BACKUP} ${VER_PHP} && \ ./check-extensions.sh check_before_configure && \ - ./compile-php.sh ${VER_PHP} + ./compile-micro.sh ${VER_PHP}