build-static.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/bin/sh
  2. set -o errexit
  3. set -x
  4. if ! type "git" > /dev/null; then
  5. echo "The \"git\" command must be installed."
  6. exit 1
  7. fi
  8. arch="$(uname -m)"
  9. os="$(uname -s | tr '[:upper:]' '[:lower:]')"
  10. md5binary="md5sum"
  11. if [ "${os}" = "darwin" ]; then
  12. os="mac"
  13. md5binary="md5 -q"
  14. fi
  15. if [ "${os}" = "linux" ] && ! type "cmake" > /dev/null; then
  16. echo "The \"cmake\" command must be installed."
  17. exit 1
  18. fi
  19. if [ -z "${PHP_EXTENSIONS}" ]; then
  20. if [ -n "${EMBED}" ] && [ -f "${EMBED}/composer.json" ]; then
  21. cd "${EMBED}"
  22. # Replace "libxml" by "xml" for compatibility with Static PHP CLI extension name
  23. PHP_EXTENSIONS="$(composer check-platform-reqs --no-dev 2>/dev/null | grep ^ext | sed -e 's/^ext-//' -e 's/ .*//' -e 's/^libxml$/xml/' | xargs | tr ' ' ',')"
  24. export PHP_EXTENSIONS
  25. cd -
  26. else
  27. export PHP_EXTENSIONS="apcu,bcmath,bz2,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,ftp,gd,gmp,gettext,iconv,igbinary,imagick,intl,ldap,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,parallel,pcntl,pdo,pdo_mysql,pdo_pgsql,pdo_sqlite,pgsql,phar,posix,protobuf,readline,redis,session,shmop,simplexml,soap,sockets,sodium,sqlite3,ssh2,sysvmsg,sysvsem,sysvshm,tidy,tokenizer,xlswriter,xml,xmlreader,xmlwriter,zip,zlib,yaml,zstd"
  28. fi
  29. fi
  30. if [ -z "${PHP_EXTENSION_LIBS}" ]; then
  31. export PHP_EXTENSION_LIBS="bzip2,freetype,libavif,libjpeg,liblz4,libwebp,libzip"
  32. fi
  33. # The Brotli library must always be built as it is required by http://github.com/dunglas/caddy-cbrotli
  34. if ! echo "${PHP_EXTENSION_LIBS}" | grep -q "\bbrotli\b"; then
  35. export PHP_EXTENSION_LIBS="${PHP_EXTENSION_LIBS},brotli"
  36. fi
  37. if [ -z "${PHP_VERSION}" ]; then
  38. export PHP_VERSION="8.3"
  39. fi
  40. if [ -z "${FRANKENPHP_VERSION}" ]; then
  41. FRANKENPHP_VERSION="$(git rev-parse --verify HEAD)"
  42. export FRANKENPHP_VERSION
  43. elif [ -d ".git/" ]; then
  44. CURRENT_REF="$(git rev-parse --abbrev-ref HEAD)"
  45. export CURRENT_REF
  46. if echo "${FRANKENPHP_VERSION}" | grep -F -q "."; then
  47. # Tag
  48. # Trim "v" prefix if any
  49. FRANKENPHP_VERSION=${FRANKENPHP_VERSION#v}
  50. export FRANKENPHP_VERSION
  51. git checkout "v${FRANKENPHP_VERSION}"
  52. else
  53. git checkout "${FRANKENPHP_VERSION}"
  54. fi
  55. fi
  56. bin="frankenphp-${os}-${arch}"
  57. if [ -n "${CLEAN}" ]; then
  58. rm -Rf dist/
  59. go clean -cache
  60. fi
  61. # Build libphp if necessary
  62. if [ -f "dist/static-php-cli/buildroot/lib/libphp.a" ]; then
  63. cd dist/static-php-cli
  64. else
  65. mkdir -p dist/
  66. cd dist/
  67. if [ -d "static-php-cli/" ]; then
  68. cd static-php-cli/
  69. git pull
  70. else
  71. git clone --depth 1 https://github.com/crazywhalecc/static-php-cli
  72. cd static-php-cli/
  73. fi
  74. if type "brew" > /dev/null; then
  75. if ! type "composer" > /dev/null; then
  76. packages="composer"
  77. fi
  78. if ! type "go" > /dev/null; then
  79. packages="${packages} go"
  80. fi
  81. if [ -n "${RELEASE}" ] && ! type "gh" > /dev/null; then
  82. packages="${packages} gh"
  83. fi
  84. if [ -n "${packages}" ]; then
  85. # shellcheck disable=SC2086
  86. brew install --formula --quiet ${packages}
  87. fi
  88. fi
  89. composer install --no-dev -a
  90. if [ "${os}" = "linux" ]; then
  91. extraOpts="--disable-opcache-jit"
  92. fi
  93. if [ -n "${DEBUG_SYMBOLS}" ]; then
  94. extraOpts="${extraOpts} --no-strip"
  95. fi
  96. ./bin/spc doctor --auto-fix
  97. ./bin/spc download --with-php="${PHP_VERSION}" --for-extensions="${PHP_EXTENSIONS}" --for-libs="${PHP_EXTENSION_LIBS}" --ignore-cache-sources=php-src
  98. # shellcheck disable=SC2086
  99. ./bin/spc build --debug --enable-zts --build-embed ${extraOpts} "${PHP_EXTENSIONS}" --with-libs="${PHP_EXTENSION_LIBS}"
  100. fi
  101. CGO_CFLAGS="-DFRANKENPHP_VERSION=${FRANKENPHP_VERSION} -I${PWD}/buildroot/include/ $(./buildroot/bin/php-config --includes | sed s#-I/#-I"${PWD}"/buildroot/#g)"
  102. if [ -n "${DEBUG_SYMBOLS}" ]; then
  103. CGO_CFLAGS="-g ${CGO_CFLAGS}"
  104. fi
  105. export CGO_CFLAGS
  106. if [ "${os}" = "mac" ]; then
  107. export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration"
  108. fi
  109. CGO_LDFLAGS="${CGO_LDFLAGS} ${PWD}/buildroot/lib/libbrotlicommon.a ${PWD}/buildroot/lib/libbrotlienc.a ${PWD}/buildroot/lib/libbrotlidec.a $(./buildroot/bin/php-config --ldflags || true) $(./buildroot/bin/php-config --libs || true)"
  110. export CGO_LDFLAGS
  111. LIBPHP_VERSION="$(./buildroot/bin/php-config --version)"
  112. export LIBPHP_VERSION
  113. cd ../
  114. if [ "${os}" = "linux" ]; then
  115. if [ -n "${MIMALLOC}" ]; then
  116. # Replace musl's mallocng by mimalloc
  117. # The default musl allocator is slow, especially when used by multi-threaded apps,
  118. # and triggers weird bugs
  119. # Adapted from https://www.tweag.io/blog/2023-08-10-rust-static-link-with-mimalloc/
  120. echo 'The USE_MIMALLOC environment variable is EXPERIMENTAL.'
  121. echo 'This option can be removed or its behavior modified at any time.'
  122. if [ ! -f "mimalloc/out/libmimalloc.a" ]; then
  123. if [ -d "mimalloc" ]; then
  124. cd mimalloc/
  125. git reset --hard
  126. git clean -xdf
  127. git fetch --tags
  128. else
  129. git clone https://github.com/microsoft/mimalloc.git
  130. cd mimalloc/
  131. fi
  132. git checkout "$(git describe --tags "$(git rev-list --tags --max-count=1 || true)" || true)"
  133. curl -f -L --retry 5 https://raw.githubusercontent.com/tweag/rust-alpine-mimalloc/b26002b49d466a295ea8b50828cb7520a71a872a/mimalloc.diff -o mimalloc.diff
  134. patch -p1 < mimalloc.diff
  135. mkdir -p out/
  136. cd out/
  137. if [ -n "${DEBUG_SYMBOLS}" ]; then
  138. cmake \
  139. -DCMAKE_BUILD_TYPE=Debug \
  140. -DMI_BUILD_SHARED=OFF \
  141. -DMI_BUILD_OBJECT=OFF \
  142. -DMI_BUILD_TESTS=OFF \
  143. ../
  144. else
  145. cmake \
  146. -DCMAKE_BUILD_TYPE=Release \
  147. -DMI_BUILD_SHARED=OFF \
  148. -DMI_BUILD_OBJECT=OFF \
  149. -DMI_BUILD_TESTS=OFF \
  150. ../
  151. fi
  152. make -j"$(nproc || true)"
  153. cd ../../
  154. fi
  155. if [ -n "${DEBUG_SYMBOLS}" ]; then
  156. libmimalloc_path=mimalloc/out/libmimalloc-debug.a
  157. else
  158. libmimalloc_path=mimalloc/out/libmimalloc.a
  159. fi
  160. # Patch musl library to use mimalloc
  161. for libc_path in "/usr/local/musl/lib/libc.a" "/usr/local/musl/$(uname -m)-linux-musl/lib/libc.a" "/usr/lib/libc.a"
  162. do
  163. if [ ! -f "${libc_path}" ] || [ -f "${libc_path}.unpatched" ]; then
  164. continue
  165. fi
  166. {
  167. echo "CREATE libc.a"
  168. echo "ADDLIB ${libc_path}"
  169. echo "DELETE aligned_alloc.lo calloc.lo donate.lo free.lo libc_calloc.lo lite_malloc.lo malloc.lo malloc_usable_size.lo memalign.lo posix_memalign.lo realloc.lo reallocarray.lo valloc.lo"
  170. echo "ADDLIB ${libmimalloc_path}"
  171. echo "SAVE"
  172. } | ar -M
  173. mv "${libc_path}" "${libc_path}.unpatched"
  174. mv libc.a "${libc_path}"
  175. done
  176. fi
  177. # Increase the default stack size to prevents issues with code including many files such as Symfony containers
  178. extraExtldflags="-Wl,-z,stack-size=0x80000"
  179. fi
  180. if [ -z "${DEBUG_SYMBOLS}" ]; then
  181. extraLdflags="-w -s"
  182. fi
  183. cd ../
  184. # Embed PHP app, if any
  185. if [ -n "${EMBED}" ] && [ -d "${EMBED}" ]; then
  186. tar -cf app.tar -C "${EMBED}" .
  187. ${md5binary} app.tar | awk '{printf $1}' > app_checksum.txt
  188. fi
  189. cd caddy/frankenphp/
  190. go env
  191. 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}"
  192. cd ../..
  193. if [ -d "${EMBED}" ]; then
  194. truncate -s 0 app.tar
  195. truncate -s 0 app_checksum.txt
  196. fi
  197. if type "upx" > /dev/null && [ -z "${DEBUG_SYMBOLS}" ] && [ -z "${NO_COMPRESS}" ]; then
  198. upx --best "dist/${bin}"
  199. fi
  200. "dist/${bin}" version
  201. if [ -n "${RELEASE}" ]; then
  202. gh release upload "v${FRANKENPHP_VERSION}" "dist/${bin}" --repo dunglas/frankenphp --clobber
  203. fi
  204. if [ -n "${CURRENT_REF}" ]; then
  205. git checkout "${CURRENT_REF}"
  206. fi