build-static.sh 9.8 KB

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