build 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/env bash
  2. # GNU shell script to compile PHP
  3. # --------------------------------------------------------------------
  4. # Copyleft 2018 Yoander Valdés (sedlav) Rodríguez <http://www.librebyte.net/>
  5. # This script is released under GNU GPLV3 licence
  6. # --------------------------------------------------------------------
  7. # It's intended to use as helper for PHP compilation process.
  8. # Enable the most used extensions as: curl, openssl, intl, mysql,
  9. # pcre, ... and allows to install PHP in custom dir, offers options
  10. # to compile PHP with Apache (prefork or worker) or fpm support.
  11. # --------------------------------------------------------------------
  12. #
  13. # Argument testing
  14. #
  15. #if [[ $# == 0 ]]; then
  16. # echo Wrong number of args type -h for help.
  17. # exit 1
  18. #elif [[ ! -d "$1" ]]; then
  19. # echo Php source is not valid directory
  20. # exit 2
  21. #elif [[ "$@" =~ \-a.*\-?f ]]; then
  22. # echo -a and -f are exclusive options
  23. # exit 3
  24. #fi
  25. #
  26. # Global variables
  27. #
  28. main_conf=
  29. userdo=
  30. arch_id=$(getconf LONG_BIT)
  31. #
  32. # Testing if you have root access!
  33. ls /root/ &>/dev/null && is_root=true || is_root=false
  34. if [[ $is_root == false ]]; then
  35. userdo=$(which sudo)
  36. if [[ $userdo != '' ]]; then
  37. $userdo ls /root/ &>/dev/null && is_root=true
  38. fi
  39. fi
  40. if [[ $is_root == false ]]; then
  41. echo This script requires root access
  42. echo Bye!
  43. exit 1
  44. fi
  45. root_dir="$(cd "$(dirname "$0")" && pwd)"
  46. #
  47. # Load configuration
  48. source ./build.ini
  49. php_file=php-${php_version}.${compression}
  50. #
  51. # create metalinks dir if no exists
  52. [[ ! -d ./metalinks ]] && mkdir metalinks
  53. #
  54. # If metalink does no exits generate it based on metalink template
  55. metalink_file=./metalinks/${php_file}.metalink
  56. sha256=$(cat ./signatures/$php_file.sig)
  57. [[ ! -f $metalink_file ]] && source ./template.metalink > $metalink_file
  58. #
  59. # Create download dir
  60. [[ ! -d ./downloads ]] && mkdir downloads
  61. #
  62. # Move to downloads dir
  63. cd ./downloads
  64. #
  65. # Download PHP, if the file is not already downloaded, if the file was downloaded partially then delete it
  66. check_integrity=true
  67. if [[ ! -f ./$php_file ]]; then
  68. echo Downloading $php_file...
  69. if [[ `curl -V|grep -i 'features:.*\bmetalink\b'` ]]; then # Test for metalink support
  70. curl -# -L --metalink file://$root_dir/metalinks/${php_file}.metalink || exit 2
  71. [[ $? ]] && check_integrity=false
  72. else # Fallback download method
  73. while read mirror && ! curl -# -L `printf $mirror $php_file` -o $php_file; do
  74. # Sleep 1s
  75. sleep 1
  76. done < ./../mirrors.txt
  77. [[ ! $? ]] && exit
  78. fi
  79. fi
  80. #
  81. # If download check integrity
  82. if [[ $check_integrity==true ]] && [[ "`sha256sum ./$php_file|awk '{print $1}'`" != "$sha256" ]]; then
  83. echo "Corrupted file: $php_file"
  84. exit 3
  85. fi
  86. #
  87. # Uncompressing: Uncompress only if the php-${php_version} dir does not exist to uncompress again delete the dir
  88. if [[ ! -d ./php-${php_version} ]]; then
  89. if [[ 'tar.xz' == $compression ]]; then
  90. tar xvJf $php_file
  91. elif [[ 'tar.bz2' == $compression ]]; then
  92. tar xvjf $php_file
  93. elif [[ 'tar.gz' == $compression ]]; then
  94. tar xvzf $php_file
  95. else
  96. echo "Uknown file ($php_file) compression"
  97. echo Bye!
  98. exit 2
  99. fi
  100. fi
  101. #
  102. # Load OS details
  103. [[ -f /etc/os-release ]] && { source /etc/os-release; os_id=$ID; os_version=$VERSION_ID; }
  104. #
  105. # Installing dependencies
  106. # Common dependencies
  107. # Replace minor release by an x, 7.2.0 => 7.2.x
  108. php_mayor_revision=php-`echo $php_version|sed -r 's/\.[[:digit:]]+$/.x/'`
  109. #actions=`echo $databases|sed -r 's/([[:alpha:]]+)/'$os_id'-\1/g'|xargs -I{} echo {} "$os_id $os_id-$web_server $os_id-$php_mayor_revision $os_id-$sysinit"`
  110. prebuild_dir="$root_dir/pre-build"
  111. #
  112. # Common actions
  113. for action in $os_id ${os_id}-${web_server} ${os_id}-${sysinit}; do
  114. action_file="$prebuild_dir/$action"
  115. [[ -f "$action_file" ]] && source "$action_file"
  116. done
  117. #
  118. # Load php prebuild action, example centos-7-php-7.2.x overrides centos-php-7.2.x
  119. if [[ -f "$prebuild_dir/${os_id}-${os_version}-${php_mayor_revision}" ]]; then
  120. source "$prebuild_dir/${os_id}-${os_version}-${php_mayor_revision}"
  121. elif [[ -f "$prebuild_dir/${os_id}-${php_mayor_revision}" ]]; then
  122. source "$prebuild_dir/${os_id}-${php_mayor_revision}"
  123. fi
  124. #
  125. # Load databases prebuild actions
  126. for action in $databases; do
  127. action_file="$prebuild_dir/$action"
  128. [[ -f $action_file ]] && source $action_file
  129. action_file="$prebuild_dir/${os_id}-${action}"
  130. [[ -f $action_file ]] && source $action_file
  131. done
  132. #
  133. # Compilation params
  134. if [[ "$install_prefix" =~ ^/usr/local/?$ ]]; then
  135. install_prefix=/usr/local
  136. sysconfdir=$install_prefix/etc/php
  137. elif [[ "$install_prefix" =~ ^/usr/?$ ]]; then
  138. install_prefix=/usr
  139. sysconfdir=/etc/php
  140. else
  141. echo "Invalid install dir: $install_prefix"
  142. exit 4;
  143. fi
  144. EXTENSION_DIR=$install_prefix/lib/php/modules
  145. export EXTENSION_DIR
  146. #PEAR_INSTALLDIR=$install_prefix/share/pear
  147. #export PEAR_INSTALLDIR
  148. #
  149. # Create conf dirs
  150. [[ ! -d "$sysconfdir" ]] && $userdo mkdir -p $sysconfdir/conf.d && echo Created $sysconfdir, $sysconfdir/conf.d
  151. [[ ! -d "$EXTENSION_DIR" ]] && $userdo mkdir -p $EXTENSION_DIR && echo Created $EXTENSION_DIR
  152. #[[ ! -d "$PEAR_INSTALLDIR" ]] && $userdo mkdir -p $PEAR_INSTALLDIR && echo Created $PEAR_INSTALLDIR
  153. #
  154. # New icu libs for intl does not include this stdc++ lib ld flag
  155. EXTRA_LIBS=-lstdc++
  156. export EXTRA_LIBS
  157. cd "$root_dir/downloads/php-${php_version}"
  158. if [ ! -f ./configure ]; then
  159. ./buildconf --force # Build configure, not included in git versions
  160. fi
  161. extensions=
  162. ext_dir="$root_dir/extensions"
  163. for extension_name in common $sysinit $databases $php_mode $thread_model; do
  164. ext_file="$ext_dir/${extension_name}.conf"
  165. [[ -f "$ext_file" ]] && extensions="$extensions `source $ext_file`"
  166. done
  167. # Configuration for OS has priority, for example ubuntu-18.04.conf has priority over ubuntu.conf and ubuntu.conf has priorioty
  168. # over php_mayor_revision: php-7.4.x.conf
  169. if [[ -f "$ext_dir/${os_id}-${os_version}.conf" ]]; then
  170. ext_file="$ext_dir/${os_id}-${os_version}.conf"
  171. extensions="$extensions `source $ext_file`"
  172. elif [[ -f "$ext_dir/${os_id}.conf" ]]; then
  173. ext_file="$ext_dir/${os_id}.conf"
  174. extensions="$extensions `source $ext_file`"
  175. elif [[ -f "$ext_dir/${php_mayor_revision}.conf" ]]; then
  176. ext_file="$ext_dir/${php_mayor_revision}.conf"
  177. extensions="$extensions `source $ext_file`"
  178. fi
  179. export LDFLAGS="$LDFLAGS -lpthread"
  180. #
  181. # Remove leading white space
  182. extensions=`echo "$extensions"|sed -r 's/^\s+//'`
  183. #
  184. # Configure, compile and install
  185. ./configure ${extensions} && make && $userdo make install
  186. #
  187. # Execute post install action if compilation finish sucessful
  188. if [[ $? == 0 ]]; then
  189. postinstall_dir=$root_dir/post-install
  190. #
  191. # Post install actions
  192. for action in $php_env $php_mode opcache $sysinit ${os_id}-${web_server}; do
  193. action_file="$postinstall_dir/$action"
  194. [[ -f $action_file ]] && source $action_file
  195. done
  196. #
  197. # Load databases postinstall actions
  198. for action in $databases; do
  199. action_file="$postinstall_dir/$action"
  200. [[ -f $action_file ]] && source $action_file
  201. action_file="$postinstall_dir/${os_id}-${action}"
  202. [[ -f $action_file ]] && source $action_file
  203. done
  204. fi