build-static.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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" ] && [ "${arch}" = "x86_64" ]; then
  14. # Temporary fix for https://github.com/crazywhalecc/static-php-cli/issues/280 (remove ldap)
  15. export PHP_EXTENSIONS="apcu,bcmath,bz2,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,gd,iconv,igbinary,intl,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,pdo_mysql,pdo_pgsql,pdo_sqlite,pgsql,phar,posix,readline,redis,session,simplexml,sockets,sodium,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,igbinary,intl,ldap,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,pdo_mysql,pdo_pgsql,pdo_sqlite,pgsql,phar,posix,readline,redis,session,simplexml,sockets,sodium,sqlite3,sysvsem,tokenizer,xml,xmlreader,xmlwriter,zip,zlib"
  18. fi
  19. fi
  20. if [ -z "${PHP_EXTENSION_LIBS}" ]; then
  21. export PHP_EXTENSION_LIBS="bzip2,freetype,libavif,libjpeg,liblz4,libwebp,libzip"
  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. # Trim "v" prefix if any
  35. FRANKENPHP_VERSION=${FRANKENPHP_VERSION#v}
  36. export FRANKENPHP_VERSION
  37. git checkout "v${FRANKENPHP_VERSION}"
  38. else
  39. git checkout "${FRANKENPHP_VERSION}"
  40. fi
  41. fi
  42. bin="frankenphp-${os}-${arch}"
  43. if [ -n "${CLEAN}" ]; then
  44. rm -Rf dist/
  45. go clean -cache
  46. fi
  47. # Build libphp if ncessary
  48. if [ -f "dist/static-php-cli/buildroot/lib/libphp.a" ]; then
  49. cd dist/static-php-cli
  50. else
  51. mkdir -p dist/
  52. cd dist/
  53. if [ -d "static-php-cli/" ]; then
  54. cd static-php-cli/
  55. git pull
  56. else
  57. git clone --depth 1 https://github.com/crazywhalecc/static-php-cli
  58. cd static-php-cli/
  59. fi
  60. if type "brew" > /dev/null; then
  61. packages="composer"
  62. if ! type "go" > /dev/null; then
  63. packages="${packages} go"
  64. fi
  65. if [ -n "${RELEASE}" ]; then
  66. packages="${packages} gh"
  67. fi
  68. # shellcheck disable=SC2086
  69. brew install --formula --quiet ${packages}
  70. fi
  71. composer install --no-dev -a
  72. if [ "${os}" = "linux" ]; then
  73. extraOpts="--disable-opcache-jit"
  74. fi
  75. if [ -n "${DEBUG_SYMBOLS}" ]; then
  76. extraOpts="${extraOpts} --no-strip"
  77. fi
  78. ./bin/spc doctor
  79. ./bin/spc fetch --with-php="${PHP_VERSION}" --for-extensions="${PHP_EXTENSIONS}"
  80. # the Brotli library must always be built as it is required by http://github.com/dunglas/caddy-cbrotli
  81. # shellcheck disable=SC2086
  82. ./bin/spc build --enable-zts --build-embed ${extraOpts} "${PHP_EXTENSIONS}" --with-libs="brotli,${PHP_EXTENSION_LIBS}"
  83. fi
  84. CGO_CFLAGS="-DFRANKENPHP_VERSION=${FRANKENPHP_VERSION} -I${PWD}/buildroot/include/ $(./buildroot/bin/php-config --includes | sed s#-I/#-I"${PWD}"/buildroot/#g)"
  85. if [ -n "${DEBUG_SYMBOLS}" ]; then
  86. CGO_CFLAGS="-g ${CGO_CFLAGS}"
  87. fi
  88. export CGO_CFLAGS
  89. if [ "${os}" = "mac" ]; then
  90. export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration"
  91. fi
  92. CGO_LDFLAGS="${CGO_LDFLAGS} ${PWD}/buildroot/lib/libbrotlicommon.a ${PWD}/buildroot/lib/libbrotlienc.a ${PWD}/buildroot/lib/libbrotlidec.a $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)"
  93. export CGO_LDFLAGS
  94. LIBPHP_VERSION="$(./buildroot/bin/php-config --version)"
  95. export LIBPHP_VERSION
  96. cd ../..
  97. # Embed PHP app, if any
  98. if [ -n "${EMBED}" ] && [ -d "${EMBED}" ]; then
  99. tar -cf app.tar -C "${EMBED}" .
  100. md5 -q app.tar > app_checksum.txt
  101. fi
  102. if [ "${os}" = "linux" ]; then
  103. extraExtldflags="-Wl,-z,stack-size=0x80000"
  104. fi
  105. if [ -z "${DEBUG_SYMBOLS}" ]; then
  106. extraLdflags="-w -s"
  107. fi
  108. cd caddy/frankenphp/
  109. go env
  110. go build -buildmode=pie -tags "cgo netgo osusergo static_build" -ldflags "-linkmode=external -extldflags '-static-pie ${extraExtldflags}' ${extraLdflags} -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ${FRANKENPHP_VERSION} PHP ${LIBPHP_VERSION} Caddy'" -o "../../dist/${bin}"
  111. cd ../..
  112. if [ -d "${EMBED}" ]; then
  113. truncate -s 0 app.tar
  114. truncate -s 0 app_checksum.txt
  115. fi
  116. "dist/${bin}" version
  117. if [ -n "${RELEASE}" ]; then
  118. gh release upload "v${FRANKENPHP_VERSION}" "dist/${bin}" --repo dunglas/frankenphp --clobber
  119. fi
  120. if [ -n "${CURRENT_REF}" ]; then
  121. git checkout "${CURRENT_REF}"
  122. fi