build-static.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/bin/bash
  2. set -o errexit
  3. set -x
  4. if ! type "git" >/dev/null 2>&1; 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 2>&1; 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. PHP_EXTENSIONS="$(composer check-platform-reqs --no-dev 2>/dev/null | grep ^ext | sed -e 's/^ext-//' -e 's/ .*//' | xargs | tr ' ' ',')"
  23. export PHP_EXTENSIONS
  24. cd -
  25. else
  26. 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"
  27. fi
  28. fi
  29. if [ -z "${PHP_EXTENSION_LIBS}" ]; then
  30. export PHP_EXTENSION_LIBS="bzip2,freetype,libavif,libjpeg,liblz4,libwebp,libzip,nghttp2"
  31. fi
  32. # The Brotli library must always be built as it is required by http://github.com/dunglas/caddy-cbrotli
  33. if ! echo "${PHP_EXTENSION_LIBS}" | grep -q "\bbrotli\b"; then
  34. export PHP_EXTENSION_LIBS="${PHP_EXTENSION_LIBS},brotli"
  35. fi
  36. if [ -z "${PHP_VERSION}" ]; then
  37. export PHP_VERSION="8.4"
  38. fi
  39. if [ -z "${FRANKENPHP_VERSION}" ]; then
  40. FRANKENPHP_VERSION="$(git rev-parse --verify HEAD)"
  41. export FRANKENPHP_VERSION
  42. elif [ -d ".git/" ]; then
  43. CURRENT_REF="$(git rev-parse --abbrev-ref HEAD)"
  44. export CURRENT_REF
  45. if echo "${FRANKENPHP_VERSION}" | grep -F -q "."; then
  46. # Tag
  47. # Trim "v" prefix if any
  48. FRANKENPHP_VERSION=${FRANKENPHP_VERSION#v}
  49. export FRANKENPHP_VERSION
  50. git checkout "v${FRANKENPHP_VERSION}"
  51. else
  52. git checkout "${FRANKENPHP_VERSION}"
  53. fi
  54. fi
  55. bin="frankenphp-${os}-${arch}"
  56. if [ -n "${CLEAN}" ]; then
  57. rm -Rf dist/
  58. go clean -cache
  59. fi
  60. # Build libphp if necessary
  61. if [ -f "dist/static-php-cli/buildroot/lib/libphp.a" ]; then
  62. cd dist/static-php-cli
  63. else
  64. mkdir -p dist/
  65. cd dist/
  66. if [ -d "static-php-cli/" ]; then
  67. cd static-php-cli/
  68. git pull
  69. else
  70. git clone --depth 1 https://github.com/crazywhalecc/static-php-cli
  71. cd static-php-cli/
  72. fi
  73. if type "brew" >/dev/null 2>&1; then
  74. if ! type "composer" >/dev/null; then
  75. packages="composer"
  76. fi
  77. if ! type "go" >/dev/null; then
  78. packages="${packages} go"
  79. fi
  80. if [ -n "${RELEASE}" ] && ! type "gh" >/dev/null 2>&1; then
  81. packages="${packages} gh"
  82. fi
  83. if [ -n "${packages}" ]; then
  84. # shellcheck disable=SC2086
  85. brew install --formula --quiet ${packages}
  86. fi
  87. fi
  88. composer install --no-dev -a
  89. if [ "${os}" = "linux" ]; then
  90. extraOpts="--disable-opcache-jit"
  91. fi
  92. if [ -n "${DEBUG_SYMBOLS}" ]; then
  93. extraOpts="${extraOpts} --no-strip"
  94. fi
  95. ./bin/spc doctor --auto-fix
  96. ./bin/spc download --with-php="${PHP_VERSION}" --for-extensions="${PHP_EXTENSIONS}" --for-libs="${PHP_EXTENSION_LIBS}" --ignore-cache-sources=php-src --prefer-pre-built
  97. # shellcheck disable=SC2086
  98. ./bin/spc build --debug --enable-zts --build-embed ${extraOpts} "${PHP_EXTENSIONS}" --with-libs="${PHP_EXTENSION_LIBS}"
  99. fi
  100. curlGitHubHeaders=(--header "X-GitHub-Api-Version: 2022-11-28")
  101. if [ "${GITHUB_TOKEN}" ]; then
  102. curlGitHubHeaders+=(--header "Authorization: Bearer ${GITHUB_TOKEN}")
  103. fi
  104. # Compile e-dant/watcher as a static library
  105. mkdir -p watcher
  106. cd watcher
  107. curl -f --retry 5 "${curlGitHubHeaders[@]}" https://api.github.com/repos/e-dant/watcher/releases/latest |
  108. grep tarball_url |
  109. awk '{ print $2 }' |
  110. sed 's/,$//' |
  111. sed 's/"//g' |
  112. xargs curl -fL --retry 5 "${curlGitHubHeaders[@]}" |
  113. tar xz --strip-components 1
  114. cd watcher-c
  115. cc -c -o libwatcher-c.o ./src/watcher-c.cpp -I ./include -I ../include -std=c++17 -Wall -Wextra -fPIC
  116. ar rcs libwatcher-c.a libwatcher-c.o
  117. cp libwatcher-c.a ../../buildroot/lib/libwatcher-c.a
  118. mkdir -p ../../buildroot/include/wtr
  119. cp -R include/wtr/watcher-c.h ../../buildroot/include/wtr/watcher-c.h
  120. cd ../../
  121. # See https://github.com/docker-library/php/blob/master/8.3/alpine3.20/zts/Dockerfile#L53-L55
  122. CGO_CFLAGS="-DFRANKENPHP_VERSION=${FRANKENPHP_VERSION} -I${PWD}/buildroot/include/ $(./buildroot/bin/php-config --includes | sed s#-I/#-I"${PWD}"/buildroot/#g)"
  123. if [ -n "${DEBUG_SYMBOLS}" ]; then
  124. CGO_CFLAGS="-g ${CGO_CFLAGS}"
  125. else
  126. CGO_CFLAGS="-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 ${CGO_CFLAGS}"
  127. fi
  128. export CGO_CFLAGS
  129. export CGO_CPPFLAGS="${CGO_CFLAGS}"
  130. if [ "${os}" = "mac" ]; then
  131. export CGO_LDFLAGS="-framework CoreFoundation -framework SystemConfiguration"
  132. elif [ "${os}" = "linux" ] && [ -z "${DEBUG_SYMBOLS}" ]; then
  133. CGO_LDFLAGS="-Wl,-O1 -pie"
  134. fi
  135. # Temporary workaround for https://github.com/crazywhalecc/static-php-cli/issues/560
  136. if [[ "${PHP_EXTENSIONS}" == *"pgsql"* ]]; then
  137. CGO_LDFLAGS="${CGO_LDFLAGS} ${PWD}/buildroot/lib/libpgcommon.a ${PWD}/buildroot/lib/libpgport.a ${PWD}/buildroot/lib/libpq.a"
  138. fi
  139. CGO_LDFLAGS="${CGO_LDFLAGS} ${PWD}/buildroot/lib/libbrotlicommon.a ${PWD}/buildroot/lib/libbrotlienc.a ${PWD}/buildroot/lib/libbrotlidec.a ${PWD}/buildroot/lib/libwatcher-c.a $(./buildroot/bin/php-config --ldflags || true) $(./buildroot/bin/php-config --libs | sed -e 's/-lgcc_s//g' || true)"
  140. if [ "${os}" = "linux" ]; then
  141. if echo "${PHP_EXTENSIONS}" | grep -qE "\b(intl|imagick|grpc|v8js|protobuf|mongodb|tbb)\b"; then
  142. CGO_LDFLAGS="${CGO_LDFLAGS} -lstdc++"
  143. fi
  144. fi
  145. export CGO_LDFLAGS
  146. #LIBPHP_VERSION="$(./buildroot/bin/php-config --version)"
  147. # Temporary workaround for https://github.com/crazywhalecc/static-php-cli/issues/563
  148. if [[ $(cat buildroot/include/php/main/php_version.h) =~ (define PHP_VERSION \"([0-9\.]+)) ]]; then
  149. export LIBPHP_VERSION=${BASH_REMATCH[2]}
  150. fi
  151. cd ../
  152. if [ "${os}" = "linux" ]; then
  153. if [ -n "${MIMALLOC}" ]; then
  154. # Replace musl's mallocng by mimalloc
  155. # The default musl allocator is slow, especially when used by multi-threaded apps,
  156. # and triggers weird bugs
  157. # Adapted from https://www.tweag.io/blog/2023-08-10-rust-static-link-with-mimalloc/
  158. echo 'The USE_MIMALLOC environment variable is EXPERIMENTAL.'
  159. echo 'This option can be removed or its behavior modified at any time.'
  160. if [ ! -f "mimalloc/out/libmimalloc.a" ]; then
  161. if [ -d "mimalloc" ]; then
  162. cd mimalloc/
  163. git reset --hard
  164. git clean -xdf
  165. git fetch --tags
  166. else
  167. git clone https://github.com/microsoft/mimalloc.git
  168. cd mimalloc/
  169. fi
  170. git checkout "$(git describe --tags "$(git rev-list --tags --max-count=1 || true)" || true)"
  171. curl -fL --retry 5 https://raw.githubusercontent.com/tweag/rust-alpine-mimalloc/b26002b49d466a295ea8b50828cb7520a71a872a/mimalloc.diff -o mimalloc.diff
  172. patch -p1 <mimalloc.diff
  173. mkdir -p out/
  174. cd out/
  175. if [ -n "${DEBUG_SYMBOLS}" ]; then
  176. cmake \
  177. -DCMAKE_BUILD_TYPE=Debug \
  178. -DMI_BUILD_SHARED=OFF \
  179. -DMI_BUILD_OBJECT=OFF \
  180. -DMI_BUILD_TESTS=OFF \
  181. ../
  182. else
  183. cmake \
  184. -DCMAKE_BUILD_TYPE=Release \
  185. -DMI_BUILD_SHARED=OFF \
  186. -DMI_BUILD_OBJECT=OFF \
  187. -DMI_BUILD_TESTS=OFF \
  188. ../
  189. fi
  190. make -j"$(nproc || true)"
  191. cd ../../
  192. fi
  193. if [ -n "${DEBUG_SYMBOLS}" ]; then
  194. libmimalloc_path=mimalloc/out/libmimalloc-debug.a
  195. else
  196. libmimalloc_path=mimalloc/out/libmimalloc.a
  197. fi
  198. # Patch musl library to use mimalloc
  199. for libc_path in "/usr/local/musl/lib/libc.a" "/usr/local/musl/$(uname -m)-linux-musl/lib/libc.a" "/usr/lib/libc.a"; do
  200. if [ ! -f "${libc_path}" ] || [ -f "${libc_path}.unpatched" ]; then
  201. continue
  202. fi
  203. {
  204. echo "CREATE libc.a"
  205. echo "ADDLIB ${libc_path}"
  206. 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"
  207. echo "ADDLIB ${libmimalloc_path}"
  208. echo "SAVE"
  209. } | ar -M
  210. mv "${libc_path}" "${libc_path}.unpatched"
  211. mv libc.a "${libc_path}"
  212. done
  213. fi
  214. # Increase the default stack size to prevents issues with code including many files such as Symfony containers
  215. extraExtldflags="-Wl,-z,stack-size=0x80000"
  216. fi
  217. if [ -z "${DEBUG_SYMBOLS}" ]; then
  218. extraLdflags="-w -s"
  219. fi
  220. cd ../
  221. # Embed PHP app, if any
  222. if [ -n "${EMBED}" ] && [ -d "${EMBED}" ]; then
  223. tar -cf app.tar -C "${EMBED}" .
  224. ${md5binary} app.tar | awk '{printf $1}' >app_checksum.txt
  225. fi
  226. cd caddy/frankenphp/
  227. go env
  228. go build -buildmode=pie -tags "cgo,netgo,osusergo,static_build,nobadger,nomysql,nopgx" -ldflags "-linkmode=external -extldflags '-static-pie ${extraExtldflags}' ${extraLdflags} -X 'github.com/dunglas/frankenphp.VersionString=${FRANKENPHP_VERSION}' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ${FRANKENPHP_VERSION} PHP ${LIBPHP_VERSION} Caddy'" -o "../../dist/${bin}"
  229. cd ../..
  230. if [ -d "${EMBED}" ]; then
  231. truncate -s 0 app.tar
  232. truncate -s 0 app_checksum.txt
  233. fi
  234. if type "upx" >/dev/null 2>&1 && [ -z "${DEBUG_SYMBOLS}" ] && [ -z "${NO_COMPRESS}" ]; then
  235. upx --best "dist/${bin}"
  236. fi
  237. "dist/${bin}" version
  238. if [ -n "${RELEASE}" ]; then
  239. gh release upload "v${FRANKENPHP_VERSION}" "dist/${bin}" --repo dunglas/frankenphp --clobber
  240. fi
  241. if [ -n "${CURRENT_REF}" ]; then
  242. git checkout "${CURRENT_REF}"
  243. fi