compile-php.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env bash
  2. self_dir=$(cd "$(dirname "$0")";pwd)
  3. # 通过 extensions.txt 生成一个 dialog 命令
  4. function generate_ext_dialog_cmd() {
  5. list=$(cat "$self_dir/extensions.txt" | grep -v "^#" | grep -v "^$")
  6. echo -n "dialog --backtitle \"static-php-cli Compile Options\" --checklist \"Please select the extension you don't want to compile.\n\nNOTE: Use <space> to select or deselect items\n\n** Default is compiling all **\" 24 60 20 " > $self_dir/.ask_cmd.sh
  7. for loop in $list
  8. do
  9. case $loop in
  10. ^*)
  11. loop=$(echo ${loop:1} | xargs)
  12. echo -n "$loop '$loop Extension' off " >> $self_dir/.ask_cmd.sh
  13. ;;
  14. *) echo -n "$loop '$loop Extension' on " >> $self_dir/.ask_cmd.sh ;;
  15. esac
  16. done
  17. echo "2>$self_dir/extensions_install.txt" >> $self_dir/.ask_cmd.sh
  18. }
  19. # PHP 编译参数生成
  20. function php_compile_args() {
  21. _php_arg="--prefix=$self_dir/php-dist"
  22. _php_arg="$_php_arg --disable-all"
  23. _php_arg="$_php_arg --enable-shared=no"
  24. _php_arg="$_php_arg --enable-static=yes"
  25. _php_arg="$_php_arg --enable-inline-optimization"
  26. _php_arg="$_php_arg --with-layout=GNU"
  27. _php_arg="$_php_arg --with-pear=no"
  28. _php_arg="$_php_arg --disable-cgi"
  29. _php_arg="$_php_arg --disable-phpdbg"
  30. _php_arg="$_php_arg --with-config-file-path=/etc"
  31. _php_arg="$_php_arg $($self_dir/check-extensions.sh check_in_configure $1)"
  32. echo $_php_arg
  33. }
  34. # 第一个参数用于使用镜像地址还是原地址(mirror为镜像地址,original为原地址)
  35. if [ "$1" = "" ]; then
  36. dialog --backtitle "static-php-cli Compile Options" --yesno "<Yes>: Use mirror download address, mainland China users recommended.\n\n<No>: Use original address, global users recommended." 10 50
  37. test $? == 0 && USE_BACKUP="no" || USE_BACKUP="yes"
  38. else
  39. test "$1" != "mirror" && USE_BACKUP="yes" || USE_BACKUP="no"
  40. fi
  41. # 第二个参数用于规定编译的 PHP 版本
  42. if [ "$2" = "" ]; then
  43. dialog --backtitle "static-php-cli Compile Options" --inputbox "Please input your PHP version to compile" 10 50 "8.1.7" 2>$self_dir/.phpver
  44. if [ $? != 0 ]; then
  45. clear
  46. echo "canceled Compiling PHP." && rm -f $self_dir/.phpver
  47. exit 1
  48. else
  49. VER_PHP=$(cat $self_dir/.phpver)
  50. rm -f $self_dir/.phpver
  51. fi
  52. else
  53. VER_PHP=$2
  54. fi
  55. # 第三个参数用于是否直接安装,如果留空则询问编译的扩展,如果填入 all,则直接编译所有的扩展
  56. if [ "$3" != "all" ]; then
  57. generate_ext_dialog_cmd && cat $self_dir/.ask_cmd.sh && chmod +x $self_dir/.ask_cmd.sh && $self_dir/.ask_cmd.sh
  58. if [ $? != 0 ]; then
  59. clear
  60. echo "canceled Compiling PHP while selecting extensions." && rm -rf $self_dir/.ask_cmd.sh
  61. exit 1
  62. fi
  63. rm -f $self_dir/.ask_cmd.sh
  64. else
  65. cp $self_dir/extensions.txt $self_dir/extensions_install.txt
  66. fi
  67. # 第四个参数用于输出 PHP 和 micro 二进制文件的位置
  68. if [ "$4" = "" ]; then
  69. dialog --backtitle "static-php-cli Compile Options" --inputbox "Please input compiled output directory" 10 50 "/dist/" 2>$self_dir/.outdir
  70. if [ $? != 0 ]; then
  71. clear
  72. echo "canceled setting output dir, compiling PHP stopped." && rm -f $self_dir/.outdir
  73. exit 1
  74. else
  75. OUT_DIR=$(cat $self_dir/.outdir)
  76. rm -f $self_dir/.outdir
  77. fi
  78. else
  79. OUT_DIR=$4
  80. fi
  81. if [ ! -d "$OUT_DIR" ]; then
  82. mkdir -p "$OUT_DIR"
  83. fi
  84. # 下载 PHP
  85. echo "All done. Downloading PHP ..."
  86. if [ -d "$self_dir/source/php-$VER_PHP" ]; then
  87. rm -rf "$self_dir/source/php-$VER_PHP"
  88. fi
  89. $self_dir/download.sh php ${USE_BACKUP} ${VER_PHP} || { echo "Download PHP failed!" && exit 1 ; }
  90. # 选择性编译依赖的库、移动需要安装的扩展到 PHP 目录
  91. $self_dir/check-extensions.sh check_before_configure ${VER_PHP} || { echo "Install required library failed!" && exit 1 ; }
  92. # 编译 PHP
  93. echo "Compiling PHP ..."
  94. php_dir=$(find $self_dir/source -name "php-$VER_PHP" -type d | tail -n1)
  95. cd $php_dir && \
  96. ./buildconf --force && \
  97. ./configure LDFLAGS=-static $(php_compile_args $VER_PHP) && \
  98. $self_dir/check-extensions.sh check_after_configure ${VER_PHP} && \
  99. sed -ie 's/-export-dynamic//g' "Makefile" && \
  100. sed -ie 's/-o $(SAPI_CLI_PATH)/-all-static -o $(SAPI_CLI_PATH)/g' "Makefile" && \
  101. #sed -ie 's/$(PHP_GLOBAL_OBJS) $(PHP_BINARY_OBJS) $(PHP_MICRO_OBJS)/$(PHP_GLOBAL_OBJS:.lo=.o) $(PHP_BINARY_OBJS:.lo=.o) $(PHP_MICRO_OBJS:.lo=.o)/g' "Makefile" && \
  102. make LDFLAGS="-ldl" -j$(cat /proc/cpuinfo | grep processor | wc -l) && \
  103. make install-cli && \
  104. $self_dir/check-extensions.sh finish_compile && \
  105. strip $self_dir/php-dist/bin/php
  106. if [ $? != 0 ]; then
  107. exit 1
  108. fi
  109. # 将 PHP 和 micro 输出到指定目录
  110. echo "Copying php binary to $OUT_DIR ..." && \
  111. cp $self_dir/php-dist/bin/php $OUT_DIR/ && \
  112. test -f $php_dir/sapi/micro/micro.sfx && \
  113. echo "Copying micro.sfx binary to $OUT_DIR ..." && \
  114. cp $php_dir/sapi/micro/micro.sfx $OUT_DIR/ || { exit 0 ; }