build-static.sh 4.5 KB

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