static-compile-php.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #!/bin/sh
  2. _script_ver="1.2.1"
  3. _use_backup="no"
  4. _home_dir=$(pwd)"/"
  5. _source_dir="$_home_dir""source"
  6. _opt_dir="$_home_dir""opt"
  7. function download_file() {
  8. downloader="wget"
  9. type wget >/dev/null 2>&1 || { downloader="curl"; }
  10. if [ "$downloader" = "wget" ]; then
  11. _down_prefix="O"
  12. else
  13. _down_prefix="o"
  14. fi
  15. _down_symbol=0
  16. if [ ! -f "$2" ]; then
  17. $downloader "$1" -$_down_prefix "$2" >/dev/null 2>&1 && \
  18. echo "$1 完成! $2" && _down_symbol=1
  19. else
  20. echo "$2 已存在!" && _down_symbol=1
  21. fi
  22. if [ $_down_symbol == 0 ]; then
  23. echo "下载 $1 失败!请检查网络连接!"
  24. rm -rf "$2"
  25. return 1
  26. fi
  27. return 0
  28. }
  29. # 获取要下载的源码的版本号
  30. function lib_ver() {
  31. case $1 in
  32. "phpver"|"php") echo "7.3.28" ;;
  33. "swoole") echo "4.6.7" ;;
  34. "hash") echo "1.5" ;;
  35. "inotify") echo "3.0.0" ;;
  36. "redis") echo "5.3.4" ;;
  37. "libxml2") echo "2.9.10" ;;
  38. "curl") echo "7.76.1" ;;
  39. "liblzma") echo "master" ;;
  40. *) echo "unknown" ;;
  41. esac
  42. }
  43. # 获取解压后的源码根目录
  44. function lib_x_dirname() {
  45. case $1 in
  46. "php"|"swoole"|"hash"|"inotify"|"redis"|"libxml2"|"curl")
  47. if [ "$2" = "file" ]; then _name_prefix=".tar.gz"; else _name_prefix=""; fi
  48. echo "$1-$(lib_ver $1)$_name_prefix"
  49. ;;
  50. "liblzma")
  51. if [ "$_use_backup" = "yes" ]; then
  52. if [ "$2" = "file" ]; then _name_prefix=".zip"; else _name_prefix=""; fi
  53. echo "$1-$(lib_ver $1)$_name_prefix"
  54. else
  55. if [ "$2" = "file" ]; then _name_prefix=".tar.gz"; else _name_prefix=""; fi
  56. echo "$1""$_name_prefix"
  57. fi
  58. ;;
  59. *)
  60. echo "unknown"
  61. ;;
  62. esac
  63. }
  64. # 获取要下载的源码的链接地址
  65. function lib_download_link() {
  66. if [ "$_use_backup" = "yes" ]; then
  67. case $1 in
  68. "php") echo "https://www.php.net/distributions/php-$(lib_ver $1).tar.gz" ;;
  69. "swoole") echo "https://pecl.php.net/get/swoole-$(lib_ver $1).tgz" ;;
  70. "hash") echo "https://pecl.php.net/get/hash-$(lib_ver $1).tgz" ;;
  71. "inotify") echo "https://pecl.php.net/get/inotify-$(lib_ver $1).tgz" ;;
  72. "redis") echo "https://pecl.php.net/get/redis-$(lib_ver $1).tgz" ;;
  73. "libxml2") echo "http://xmlsoft.org/sources//libxml2-$(lib_ver $1).tar.gz" ;;
  74. "liblzma") echo "https://github.com/kobolabs/liblzma/archive/refs/heads/master.zip" ;;
  75. "curl") echo "https://curl.haxx.se/download/curl-$(lib_ver $1).tar.gz" ;;
  76. *) echo "unknown" ;;
  77. esac
  78. else
  79. case $1 in
  80. "php") echo "http://mirrors.zhamao.xin/php/php-$(lib_ver $1).tar.gz" ;;
  81. "swoole") echo "http://mirrors.zhamao.xin/pecl/swoole-$(lib_ver $1).tgz" ;;
  82. "hash") echo "http://mirrors.zhamao.xin/pecl/hash-$(lib_ver $1).tgz" ;;
  83. "inotify") echo "http://mirrors.zhamao.xin/pecl/inotify-$(lib_ver $1).tgz" ;;
  84. "redis") echo "http://mirrors.zhamao.xin/pecl/redis-$(lib_ver $1).tgz" ;;
  85. "libxml2") echo "https://dl.zhamao.me/libxml2/libxml2-$(lib_ver $1).tar.gz" ;;
  86. "liblzma") echo "https://dl.zhamao.me/liblzma/liblzma.tar.gz" ;;
  87. "curl") echo "https://dl.zhamao.me/curl/curl-$(lib_ver $1).tar.gz" ;;
  88. *) echo "unknown" ;;
  89. esac
  90. fi
  91. }
  92. # 获取下载后源码包的解压命令
  93. function lib_x_cmd() {
  94. case $1 in
  95. "php"|"swoole"|"hash"|"inotify"|"redis"|"libxml2"|"curl")
  96. _x_cmd="tar"
  97. ;;
  98. "liblzma") if [ "$_use_backup" = "yes" ]; then _x_cmd="unzip"; else _x_cmd="tar"; fi ;;
  99. *) _x_cmd="unknown" ;;
  100. esac
  101. case $2 in
  102. "cmd")
  103. echo $_x_cmd
  104. ;;
  105. "file-prefix")
  106. case $_x_cmd in
  107. "tar") echo "-xf" ;;
  108. "unzip") echo "" ;;
  109. esac
  110. ;;
  111. "out-prefix")
  112. case $_x_cmd in
  113. "tar") echo "-C" ;;
  114. "unzip") echo "-d" ;;
  115. esac
  116. ;;
  117. esac
  118. }
  119. _curl_override_1='
  120. AC_DEFUN([PHP_CHECK_LIBRARY], [
  121. $3
  122. ])
  123. '
  124. _curl_override_2='
  125. AC_DEFUN([PHP_CHECK_LIBRARY], [
  126. save_old_LDFLAGS=$LDFLAGS
  127. ac_stuff="$5"
  128. save_ext_shared=$ext_shared
  129. ext_shared=yes
  130. PHP_EVAL_LIBLINE([$]ac_stuff, LDFLAGS)
  131. AC_CHECK_LIB([$1],[$2],[
  132. LDFLAGS=$save_old_LDFLAGS
  133. ext_shared=$save_ext_shared
  134. $3
  135. ],[
  136. LDFLAGS=$save_old_LDFLAGS
  137. ext_shared=$save_ext_shared
  138. unset ac_cv_lib_$1[]_$2
  139. $4
  140. ])dnl
  141. ])
  142. '
  143. function lib_x() {
  144. $(lib_x_cmd $1 cmd) $(lib_x_cmd $1 file-prefix) "$_source_dir/$(lib_x_dirname $1 file)" $(lib_x_cmd $1 out-prefix) "$_source_dir/"
  145. }
  146. function lib_move_ext() {
  147. _src_dir="$_source_dir/$(lib_x_dirname $1)"
  148. _dst_dir="$_source_dir/$(lib_x_dirname php)/ext/$1"
  149. mv $_src_dir $_dst_dir
  150. }
  151. function download_all() {
  152. mkdir "$_source_dir" > /dev/null 2>&1
  153. for loop in "php" "swoole" "inotify" "hash" "redis" "libxml2" "liblzma" "curl"
  154. do
  155. echo "正在下载 $loop 源码 ..."
  156. download_file $(lib_download_link $loop) "$_source_dir/$(lib_x_dirname $loop file)" || { exit 1; } &
  157. done
  158. wait
  159. }
  160. function compile_liblzma() {
  161. if [ -f "$_opt_dir/liblzma/lib/liblzma.so" ]; then
  162. echo "已编译 liblzma!" && return
  163. fi
  164. tar -xf "$_source_dir/$(lib_x_dirname liblzma file)" -C "$_source_dir/" && \
  165. cd "$_source_dir/$(lib_x_dirname liblzma)" && \
  166. ./configure --prefix="$_opt_dir/liblzma" && \
  167. make -j4 && \
  168. make install && \
  169. echo "编译 liblzma 完成!"
  170. }
  171. function compile_curl() {
  172. if [ -f "$_opt_dir/curl/bin/curl" ]; then
  173. echo "已编译 curl!" && return
  174. fi
  175. lib_x curl && \
  176. cd "$_source_dir/$(lib_x_dirname curl)" && \
  177. CC=gcc CXX=g++ CFLAGS=-fPIC CPPFLAGS=-fPIC ./configure --prefix="$_opt_dir/curl" \
  178. --without-nghttp2 \
  179. --with-ssl=/usr \
  180. --with-pic=pic \
  181. --enable-ipv6 \
  182. --enable-shared=no \
  183. --without-libidn2 \
  184. --disable-ldap \
  185. --without-libpsl \
  186. --without-lber \
  187. --enable-ares && \
  188. make -j4 && \
  189. make install && \
  190. echo "编译 curl 完成!"
  191. }
  192. function compile_libxml2() {
  193. if [ -f "$_opt_dir/libxml2/lib/libxml2.so" ]; then
  194. echo "已编译 libxml2!" && return
  195. fi
  196. lib_x libxml2 && \
  197. cd "$_source_dir/$(lib_x_dirname libxml2)" && \
  198. ./configure --with-lzma="$_opt_dir/liblzma" --prefix="$_opt_dir/libxml2" --exec-prefix="$_opt_dir/libxml2" --without-python && \
  199. make -j4 && \
  200. make install && \
  201. echo "编译 libxml2 完成!"
  202. }
  203. function php_get_configure_args() {
  204. _php_arg="--prefix=$_home_dir""php-dist"
  205. _php_arg="$_php_arg --disable-all"
  206. _php_arg="$_php_arg --enable-shared=no"
  207. _php_arg="$_php_arg --enable-static=yes"
  208. _php_arg="$_php_arg --enable-inline-optimization"
  209. _php_arg="$_php_arg --with-layout=GNU"
  210. _php_arg="$_php_arg --enable-calendar"
  211. _php_arg="$_php_arg --enable-ctype"
  212. _php_arg="$_php_arg --enable-filter"
  213. _php_arg="$_php_arg --enable-openssl"
  214. _php_arg="$_php_arg --enable-bcmath"
  215. _php_arg="$_php_arg --with-openssl-dir=/usr"
  216. _php_arg="$_php_arg --enable-pcntl"
  217. _php_arg="$_php_arg --enable-openssl"
  218. _php_arg="$_php_arg --with-openssl"
  219. _php_arg="$_php_arg --with-iconv"
  220. _php_arg="$_php_arg --enable-json"
  221. _php_arg="$_php_arg --enable-mbstring"
  222. _php_arg="$_php_arg --enable-phar"
  223. _php_arg="$_php_arg --enable-pdo"
  224. _php_arg="$_php_arg --with-pdo-mysql=mysqlnd"
  225. _php_arg="$_php_arg --enable-sockets"
  226. _php_arg="$_php_arg --enable-swoole"
  227. _php_arg="$_php_arg --enable-inotify"
  228. _php_arg="$_php_arg --enable-redis"
  229. _php_arg="$_php_arg --disable-redis-session"
  230. _php_arg="$_php_arg --enable-simplexml"
  231. _php_arg="$_php_arg --enable-dom"
  232. _php_arg="$_php_arg --enable-xml"
  233. _php_arg="$_php_arg --enable-xmlwriter"
  234. _php_arg="$_php_arg --enable-xmlreader"
  235. _php_arg="$_php_arg --with-zlib"
  236. _php_arg="$_php_arg --enable-posix"
  237. _php_arg="$_php_arg --enable-mysqlnd"
  238. _php_arg="$_php_arg --enable-tokenizer"
  239. _php_arg="$_php_arg --with-curl"
  240. _php_arg="$_php_arg --with-pear=no"
  241. _php_arg="$_php_arg --disable-cgi"
  242. _php_arg="$_php_arg --disable-phpdbg"
  243. case $(lib_ver php) in
  244. 7.3.*|7.2.*)
  245. _php_arg="$_php_arg --with-gd"
  246. _php_arg="$_php_arg --enable-libxml"
  247. _php_arg="$_php_arg --with-libxml-dir=$_opt_dir/libxml2"
  248. _php_arg="$_php_arg --enable-hash"
  249. ;;
  250. *)
  251. _php_arg="$_php_arg --enable-gd"
  252. _php_arg="$_php_arg --with-libxml"
  253. ;;
  254. esac
  255. echo $_php_arg
  256. }
  257. function compile_php() {
  258. echo "正在编译 php ..." && \
  259. rm -rf "$_source_dir/$(lib_x_dirname php)" && \
  260. lib_x php && \
  261. lib_x swoole && \
  262. lib_move_ext swoole && \
  263. "$_source_dir/$(lib_x_dirname php)/ext/swoole/clear.sh" && \
  264. lib_x redis && \
  265. lib_move_ext redis && \
  266. lib_x inotify && \
  267. lib_move_ext inotify && \
  268. cd "$_source_dir/$(lib_x_dirname php)/" && \
  269. echo "$_curl_override_1" > "$_home_dir""ac_override_1" && \
  270. echo "$_curl_override_2" > "$_home_dir""ac_override_2" && \
  271. cat "$_home_dir""ac_override_1" "ext/curl/config.m4" "$_home_dir""ac_override_2" > /tmp/aa && \
  272. mv /tmp/aa "ext/curl/config.m4" && \
  273. rm -rf "$_home_dir""ac_override_1" "$_home_dir""ac_override_2" && \
  274. PKG_CONFIG_PATH="$PKG_CONFIG_PATH:""$_opt_dir/libxml2/lib/pkgconfig" && \
  275. PKG_CONFIG_PATH="$PKG_CONFIG_PATH:""$_opt_dir/curl/lib/pkgconfig" && \
  276. ./buildconf --force && \
  277. PKG_CONFIG_PATH=$PKG_CONFIG_PATH ./configure LDFLAGS=-static $(php_get_configure_args) && \
  278. sed -ie 's/-export-dynamic//g' "$_source_dir/$(lib_x_dirname php)/Makefile" && \
  279. sed -ie 's/-o $(SAPI_CLI_PATH)/-all-static -o $(SAPI_CLI_PATH)/g' "$_source_dir/$(lib_x_dirname php)/Makefile" && \
  280. sed -ie 's/swoole_clock_gettime(CLOCK_REALTIME/clock_gettime(CLOCK_REALTIME/g' "$_source_dir/$(lib_x_dirname php)/ext/swoole/include/swoole.h" && \
  281. make LDFLAGS=-ldl -j4 && \
  282. make install && \
  283. strip "$_home_dir""php-dist/bin/php" && \
  284. cd $_home_dir
  285. }
  286. #apk add g++ pkgconf autoconf nghttp2 libcurl \
  287. # git gcc zlib-dev libstdc++ ncurses zlib linux-headers \
  288. # readline make libssl1.1 libxml2 m4 libgcc vim binutils \
  289. # oniguruma-dev openssl openssl-dev
  290. # 编译必需的
  291. apk add gcc g++ autoconf libstdc++ linux-headers make m4 libgcc binutils ncurses
  292. # php的zlib支持
  293. apk add zlib-dev zlib-static
  294. # php的mbstring支持
  295. apk add oniguruma-dev
  296. # php的openssl支持
  297. apk add openssl-libs-static openssl-dev openssl
  298. # php的gd支持,如果不需要gd则去掉--enable-gd和下面的依赖
  299. apk add libpng-dev libpng-static
  300. # curl的c-ares支持,如果不需要curl则去掉
  301. apk add c-ares-static c-ares-dev
  302. download_all && \
  303. compile_liblzma && \
  304. compile_libxml2 && \
  305. compile_curl && \
  306. compile_php && \
  307. echo "完成!见 php-dist/bin/php"