build-static.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. md5binary="md5sum"
  10. if [ "${os}" = "darwin" ]; then
  11. os="mac"
  12. md5binary="md5 -q"
  13. fi
  14. if [ -z "${PHP_EXTENSIONS}" ]; then
  15. 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"
  16. fi
  17. if [ -z "${PHP_EXTENSION_LIBS}" ]; then
  18. export PHP_EXTENSION_LIBS="bzip2,freetype,libavif,libjpeg,liblz4,libwebp,libzip"
  19. fi
  20. # the Brotli library must always be built as it is required by http://github.com/dunglas/caddy-cbrotli
  21. if ! echo "${PHP_EXTENSION_LIBS}" | grep -q "\bbrotli\b"; then
  22. export PHP_EXTENSION_LIBS="${PHP_EXTENSION_LIBS},brotli"
  23. fi
  24. if [ -z "${PHP_VERSION}" ]; then
  25. export PHP_VERSION="8.3"
  26. fi
  27. if [ -z "${FRANKENPHP_VERSION}" ]; then
  28. FRANKENPHP_VERSION="$(git rev-parse --verify HEAD)"
  29. export FRANKENPHP_VERSION
  30. elif [ -d ".git/" ]; then
  31. CURRENT_REF="$(git rev-parse --abbrev-ref HEAD)"
  32. export CURRENT_REF
  33. if echo "${FRANKENPHP_VERSION}" | grep -F -q "."; then
  34. # Tag
  35. # Trim "v" prefix if any
  36. FRANKENPHP_VERSION=${FRANKENPHP_VERSION#v}
  37. export FRANKENPHP_VERSION
  38. git checkout "v${FRANKENPHP_VERSION}"
  39. else
  40. git checkout "${FRANKENPHP_VERSION}"
  41. fi
  42. fi
  43. bin="frankenphp-${os}-${arch}"
  44. if [ -n "${CLEAN}" ]; then
  45. rm -Rf dist/
  46. go clean -cache
  47. fi
  48. # Build libphp if necessary
  49. if [ -f "dist/static-php-cli/buildroot/lib/libphp.a" ]; then
  50. cd dist/static-php-cli
  51. else
  52. mkdir -p dist/
  53. cd dist/
  54. if [ -d "static-php-cli/" ]; then
  55. cd static-php-cli/
  56. git pull
  57. else
  58. git clone --depth 1 https://github.com/crazywhalecc/static-php-cli
  59. cd static-php-cli/
  60. fi
  61. if type "brew" > /dev/null; then
  62. if ! type "composer" > /dev/null; then
  63. packages="composer"
  64. fi
  65. if ! type "go" > /dev/null; then
  66. packages="${packages} go"
  67. fi
  68. if [ -n "${RELEASE}" ] && ! type "gh" > /dev/null; then
  69. packages="${packages} gh"
  70. fi
  71. if [ -n "${packages}" ]; then
  72. # shellcheck disable=SC2086
  73. brew install --formula --quiet ${packages}
  74. fi
  75. fi
  76. composer install --no-dev -a
  77. if [ "${os}" = "linux" ]; then
  78. extraOpts="--disable-opcache-jit"
  79. fi
  80. if [ -n "${DEBUG_SYMBOLS}" ]; then
  81. extraOpts="${extraOpts} --no-strip"
  82. fi
  83. ./bin/spc doctor --auto-fix
  84. ./bin/spc download --with-php="${PHP_VERSION}" --for-extensions="${PHP_EXTENSIONS}" --for-libs="${PHP_EXTENSION_LIBS}" --ignore-cache-sources=php-src
  85. # shellcheck disable=SC2086
  86. ./bin/spc build --debug --enable-zts --build-embed ${extraOpts} "${PHP_EXTENSIONS}" --with-libs="${PHP_EXTENSION_LIBS}"
  87. fi
  88. CGO_CFLAGS="-DFRANKENPHP_VERSION=${FRANKENPHP_VERSION} -I${PWD}/buildroot/include/ $(./buildroot/bin/php-config --includes | sed s#-I/#-I"${PWD}"/buildroot/#g)"
  89. if [ -n "${DEBUG_SYMBOLS}" ]; then
  90. CGO_CFLAGS="-g ${CGO_CFLAGS}"
  91. fi
  92. export CGO_CFLAGS
  93. if [ "${os}" = "mac" ]; then
  94. export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration"
  95. fi
  96. 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)"
  97. export CGO_LDFLAGS
  98. LIBPHP_VERSION="$(./buildroot/bin/php-config --version)"
  99. export LIBPHP_VERSION
  100. cd ../..
  101. # Embed PHP app, if any
  102. if [ -n "${EMBED}" ] && [ -d "${EMBED}" ]; then
  103. tar -cf app.tar -C "${EMBED}" .
  104. ${md5binary} app.tar > app_checksum.txt
  105. fi
  106. if [ "${os}" = "linux" ]; then
  107. extraExtldflags="-Wl,-z,stack-size=0x80000"
  108. fi
  109. if [ -z "${DEBUG_SYMBOLS}" ]; then
  110. extraLdflags="-w -s"
  111. fi
  112. cd caddy/frankenphp/
  113. go env
  114. 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}"
  115. cd ../..
  116. if [ -d "${EMBED}" ]; then
  117. truncate -s 0 app.tar
  118. truncate -s 0 app_checksum.txt
  119. fi
  120. if type "upx" > /dev/null && [ -z "${DEBUG_SYMBOLS}" ] && [ -z "${NO_COMPRESS}" ]; then
  121. upx --best "dist/${bin}"
  122. fi
  123. "dist/${bin}" version
  124. if [ -n "${RELEASE}" ]; then
  125. gh release upload "v${FRANKENPHP_VERSION}" "dist/${bin}" --repo dunglas/frankenphp --clobber
  126. fi
  127. if [ -n "${CURRENT_REF}" ]; then
  128. git checkout "${CURRENT_REF}"
  129. fi