build-static.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/sh
  2. set -o errexit
  3. if ! type "git" > /dev/null; then
  4. echo "The \"git\" command must be installed."
  5. exit 1
  6. fi
  7. arch="$(uname -m)"
  8. os="$(uname -s | tr '[:upper:]' '[:lower:]')"
  9. if [ "$os" = "darwin" ]; then
  10. os="mac"
  11. fi
  12. if [ -z "$PHP_EXTENSIONS" ]; then
  13. if [ "$os" = "mac" ]; then
  14. # Temporary fix for https://github.com/crazywhalecc/static-php-cli/issues/278 (remove pdo_pgsql, pgsql and ldap)
  15. export PHP_EXTENSIONS="apcu,bcmath,bz2,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,gd,iconv,intl,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,pdo_mysql,pdo_sqlite,phar,posix,readline,redis,session,simplexml,sockets,sqlite3,sysvsem,tokenizer,xml,xmlreader,xmlwriter,zip,zlib"
  16. else
  17. 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"
  18. fi
  19. fi
  20. if [ -z "$PHP_EXTENSIONS_LIB" ]; then
  21. export PHP_EXTENSION_LIBS="freetype,libjpeg,libavif,libwebp,libzip,bzip2"
  22. fi
  23. if [ -z "$PHP_VERSION" ]; then
  24. export PHP_VERSION="8.3"
  25. fi
  26. if [ -z "$FRANKENPHP_VERSION" ]; then
  27. FRANKENPHP_VERSION="$(git rev-parse --verify HEAD)"
  28. export FRANKENPHP_VERSION
  29. elif [ -d ".git/" ]; then
  30. CURRENT_REF="$(git rev-parse --abbrev-ref HEAD)"
  31. export CURRENT_REF
  32. if echo "$FRANKENPHP_VERSION" | grep -F -q "."; then
  33. # Tag
  34. git checkout "v$FRANKENPHP_VERSION"
  35. else
  36. git checkout "$FRANKENPHP_VERSION"
  37. fi
  38. fi
  39. bin="frankenphp-$os-$arch"
  40. if [ "$CLEAN" ]; then
  41. rm -Rf dist/
  42. go clean -cache
  43. fi
  44. # Build libphp if ncessary
  45. if [ -f "dist/static-php-cli/buildroot/lib/libphp.a" ]; then
  46. cd dist/static-php-cli
  47. else
  48. mkdir -p dist/
  49. cd dist/
  50. if [ -d "static-php-cli/" ]; then
  51. cd static-php-cli/
  52. git pull
  53. else
  54. git clone --depth 1 https://github.com/crazywhalecc/static-php-cli
  55. cd static-php-cli/
  56. fi
  57. if type "brew" > /dev/null; then
  58. packages="composer"
  59. if ! type "go" > /dev/null; then
  60. packages="$packages go"
  61. fi
  62. if [ "$RELEASE" ]; then
  63. packages="$packages gh"
  64. fi
  65. # shellcheck disable=SC2086
  66. brew install --formula --quiet $packages
  67. fi
  68. composer install --no-dev -a
  69. if [ "$os" = "linux" ]; then
  70. extraOpts="--disable-opcache-jit"
  71. fi
  72. ./bin/spc doctor
  73. ./bin/spc fetch --with-php="$PHP_VERSION" --for-extensions="$PHP_EXTENSIONS"
  74. # shellcheck disable=SC2086
  75. ./bin/spc build --enable-zts --build-embed $extraOpts "$PHP_EXTENSIONS" --with-libs="$PHP_EXTENSION_LIBS"
  76. fi
  77. CGO_CFLAGS="-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $(./buildroot/bin/php-config --includes | sed s#-I/#-I"$PWD"/buildroot/#g)"
  78. export CGO_CFLAGS
  79. if [ "$os" = "mac" ]; then
  80. export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration"
  81. fi
  82. CGO_LDFLAGS="$CGO_LDFLAGS $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)"
  83. export CGO_LDFLAGS
  84. LIBPHP_VERSION="$(./buildroot/bin/php-config --version)"
  85. export LIBPHP_VERSION
  86. cd ../..
  87. # Embed PHP app, if any
  88. if [ -d "$EMBED" ]; then
  89. tar -cf app.tar -C "$EMBED" .
  90. fi
  91. cd caddy/frankenphp/
  92. go env
  93. 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"
  94. cd ../..
  95. if [ -d "$EMBED" ]; then
  96. truncate -s 0 app.tar
  97. fi
  98. "dist/$bin" version
  99. if [ "$RELEASE" ]; then
  100. gh release upload "v$FRANKENPHP_VERSION" "dist/$bin" --repo dunglas/frankenphp --clobber
  101. fi
  102. if [ "$CURRENT_REF" ]; then
  103. git checkout "$CURRENT_REF"
  104. fi