build-static.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/278 (remove 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_pgsql,pdo_sqlite,pgsql,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 [ -n "${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 [ -n "${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. if [ -n "${DEBUG_SYMBOLS}" ]; then
  73. extraOpts="${extraOpts} --no-strip"
  74. fi
  75. ./bin/spc doctor
  76. ./bin/spc fetch --with-php="${PHP_VERSION}" --for-extensions="${PHP_EXTENSIONS}"
  77. # shellcheck disable=SC2086
  78. ./bin/spc build --debug --enable-zts --build-embed ${extraOpts} "${PHP_EXTENSIONS}" --with-libs="${PHP_EXTENSION_LIBS}"
  79. fi
  80. CGO_CFLAGS="-DFRANKENPHP_VERSION=${FRANKENPHP_VERSION} $(./buildroot/bin/php-config --includes | sed s#-I/#-I"${PWD}"/buildroot/#g)"
  81. if [ -n "${DEBUG_SYMBOLS}" ]; then
  82. CGO_CFLAGS="-g ${CGO_CFLAGS}"
  83. fi
  84. export CGO_CFLAGS
  85. if [ "${os}" = "mac" ]; then
  86. export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration"
  87. fi
  88. CGO_LDFLAGS="${CGO_LDFLAGS} $(./buildroot/bin/php-config --ldflags) $(./buildroot/bin/php-config --libs)"
  89. export CGO_LDFLAGS
  90. LIBPHP_VERSION="$(./buildroot/bin/php-config --version)"
  91. export LIBPHP_VERSION
  92. cd ../..
  93. # Embed PHP app, if any
  94. if [ -n "${EMBED}" ] && [ -d "${EMBED}" ]; then
  95. tar -cf app.tar -C "${EMBED}" .
  96. fi
  97. if [ "${os}" = "linux" ]; then
  98. extraExtldflags="-Wl,-z,stack-size=0x80000"
  99. fi
  100. if [ -z "${DEBUG_SYMBOLS}" ]; then
  101. extraLdflags="-w -s"
  102. fi
  103. cd caddy/frankenphp/
  104. go env
  105. 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}"
  106. cd ../..
  107. if [ -d "${EMBED}" ]; then
  108. truncate -s 0 app.tar
  109. fi
  110. "dist/${bin}" version
  111. if [ -n "${RELEASE}" ]; then
  112. gh release upload "v${FRANKENPHP_VERSION}" "dist/${bin}" --repo dunglas/frankenphp --clobber
  113. fi
  114. if [ -n "${CURRENT_REF}" ]; then
  115. git checkout "${CURRENT_REF}"
  116. fi