build-static.sh 4.4 KB

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