XCache is a fast, stable PHP opcode cacher that has been proven and is now running on production servers under high load. https://xcache.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

418 lines
8.4 KiB

  1. #!/bin/bash
  2. # this script is for developers only
  3. set -e
  4. MAKE=/usr/bin/make
  5. true() { # {{{1
  6. return 0
  7. }
  8. hi() { # {{{1
  9. local colors=(4 2 3 5 6 7 8 9)
  10. local re=()
  11. local i=0
  12. for r in "$@"; do
  13. ((color=$i % ${#colors[@]})) || true
  14. color=${colors[$color]}
  15. r="${r/\#/\\#}"
  16. r="s#$r#[4${color}m\0#g"
  17. re[$i]="-e$r"
  18. ((i=i+1))
  19. done
  20. sed -ur "${re[@]}"
  21. }
  22. hiecho() { # {{{1
  23. echo """$@"""
  24. }
  25. # }}}
  26. stopfpm() { # {{{1
  27. if [[ -f $1 ]]; then
  28. local pid=`cat $1 2>/dev/null || true`
  29. if [[ $pid -gt 0 ]]; then
  30. hiecho Stopping fpm $pid @ $1
  31. kill $pid || true
  32. fi
  33. fi
  34. }
  35. xtest() { # {{{1
  36. $MAKE ${MAKEOPTS} -f devel/test.mak clean all
  37. }
  38. prep() { # {{{1
  39. $MAKE ${MAKEOPTS} -f devel/prepare.mak "$@"
  40. }
  41. mergepo() { # {{{1
  42. prep
  43. local i
  44. find htdocs -iname '*.po' | while read -r i; do
  45. if [[ -f $i-merged ]]; then
  46. mv $i-merged $i
  47. fi
  48. done
  49. }
  50. po2php() { # {{{1
  51. mergepo
  52. local phpfile
  53. find htdocs -iname '*.po' | while read -r i; do
  54. phpfile=${i/.po/.php}
  55. devel/po2php.awk < $i > $phpfile.tmp
  56. mv $phpfile.tmp $phpfile
  57. done
  58. }
  59. updatedeps() { # {{{1
  60. local pwd=$(readlink -f "$(pwd)")
  61. local builddirname=${xcachesrcdir}-${phpbasename}
  62. local builddir=$(readlink -f $builddirname)
  63. if [[ -z $builddir ]]; then
  64. hiecho required $builddirname not found
  65. return 1
  66. fi
  67. local sourceFile
  68. local sourceFilename
  69. find . -iname \*.c | LANG=C sort | while read -r sourceFile; do
  70. sourceFilename=${sourceFile#./}
  71. {
  72. case "$sourceFilename" in
  73. includes.c)
  74. echo '$(XCACHE_INCLUDES_I)'
  75. ;;
  76. esac
  77. echo ${sourceFilename%.c}.lo '$(builddir)/'${sourceFilename%.c}.lo:
  78. hiecho "Checking $sourceFilename ..." >&2
  79. for dependency in $(gcc -DHAVE_XCACHE_TEST -DHAVE_XCACHE_DPRINT -DXCACHE_DEBUG -MM $sourceFile -I$PHPSDIR/$phpbasename/include/php/{,main,Zend,TSRM} -I. -I${builddir} -MG | sed 's#.*:##g' | sed 's#\\##g'); do
  80. dependency=$(readlink -f "$dependency")
  81. case "$dependency" in
  82. $pwd/*)
  83. dependency=${dependency#$pwd/}
  84. ;;
  85. $builddir/*)
  86. dependency="\$(builddir)/"${dependency#$builddir/}
  87. ;;
  88. esac
  89. case "$dependency" in
  90. *xc_const_string_opcodes_php*)
  91. for dependency in xcache/xc_const_string_*.h; do
  92. echo '$(srcdir)/'$dependency
  93. done
  94. ;;
  95. /*) ;;
  96. $sourceFilename) ;;
  97. xc_processor.h|\$\(builddir\)/xc_processor.h)
  98. echo '$(XCACHE_PROC_H)'
  99. ;;
  100. xc_processor.c.h|\$\(builddir\)/xc_processor.c.h)
  101. echo '$(XCACHE_PROC_C)'
  102. ;;
  103. *)
  104. if [[ -r $dependency ]]; then
  105. echo '$(srcdir)/'$dependency
  106. else
  107. hiecho "$dependency not found" >&2
  108. fi
  109. esac
  110. done | LANG=C sort | uniq
  111. } | xargs
  112. done > Makefile.frag.deps
  113. }
  114. # }}}1
  115. hiecho "Loading config devel/run.cfg"
  116. . devel/run.cfg
  117. PHPSDIR=${PHPSDIR:-$HOME/test}
  118. if [[ $# -eq 0 ]]; then
  119. set -- "${args[@]}"
  120. fi
  121. basename=$(basename $(pwd))
  122. case "$basename" in
  123. *-*)
  124. # in build dir, starts from src dir
  125. dirs="${basename#*-}"
  126. xcachesrcdir=../${basename%%-*}
  127. cd $xcachesrcdir
  128. ;;
  129. *)
  130. # in src dir
  131. dirs=${dirs:-php5-debug-zts}
  132. xcachesrcdir=../$basename
  133. ;;
  134. esac
  135. touch devel.pid
  136. # ==========================
  137. do_phpize() { # {{{1
  138. if [[ ! -x $PHPSDIR/$phpbasename/bin/phpize ]]; then
  139. hiecho $PHPSDIR/$phpbasename/bin/phpize not found
  140. exit
  141. fi
  142. export PATH=$PHPSDIR/$phpbasename/bin:$PATH
  143. local pedantic=
  144. case $phpbasename in
  145. php5|php5.4) pedantic=-pedantic-errors;;
  146. *) pedantic=-pedantic;;
  147. esac
  148. phpize --clean \
  149. && phpize \
  150. && CFLAGS="$CFLAGS -Dinline= -g -O0 $pedantic -Wno-variadic-macros -Wno-long-long -Wall -Wno-unused-parameter -Wno-unused-function -W -Wshadow -Werror=implicit-function-declaration -std=c89 -D_GNU_SOURCE -D_POSIX_SOURCE -Dinline=" ./configure \
  151. --enable-xcache-cacher \
  152. --enable-xcache-optimizer \
  153. --enable-xcache-encoder \
  154. --enable-xcache-decoder \
  155. --enable-xcache-disassembler \
  156. --enable-xcache-assembler \
  157. --enable-xcache-coverager \
  158. --enable-xcache-test \
  159. --enable-xcache-dprint \
  160. --enable-xcache-constant
  161. }
  162. do_make() { # {{{1
  163. if [[ ! -f Makefile ]]; then
  164. do_phpize
  165. fi
  166. LANG=C $MAKE $MAKEOPTS "$@" 2>&1 \
  167. | sed -ur \
  168. -e 's#Werror=implicit-function-declaration#We/rror=i/mplicit-function-declaration#' \
  169. -e 's#-pedantic-errors#-pedantic-e/rrors#' \
  170. -e 's#\./xc_processor\.h#'$PWD'/xc_processor.h#' \
  171. -e 's#\./xc_processor\.c\.h#'$PWD'/xc_processor.c.h#' \
  172. | hi error implicit warn FAIL
  173. ret=${PIPESTATUS[0]}
  174. if [[ $ret -ne 0 ]]; then
  175. exit $ret
  176. fi
  177. }
  178. cleanfpm() { # {{{1
  179. echo
  180. stopfpm $pidfile
  181. }
  182. # }}}
  183. run() {
  184. pidfile=$xcachesrcdir/devel.pid
  185. # prepare {{{1
  186. case "$1" in
  187. phpize)
  188. if [[ -r Makefile ]]; then
  189. $MAKE $MAKEOPTS xcachesvnclean || true
  190. fi
  191. ;;
  192. esac
  193. rm -f php-src
  194. find -L . -type d -print0 | xargs -0 rmdir -p 2>/dev/null || true
  195. find -L . -type l -print0 | xargs -0 rm -fv
  196. lndir "$xcachesrcdir" >/dev/null || true
  197. find . -iname .\*.swp -print0 | xargs -0 rm -f
  198. ln -snf ~/src/php/${phpbasename%%-*} php-src
  199. for i in ~/src/php/$phpbasename/sapi/cgi/php{,-cgi}; do
  200. if [[ -r $i ]]; then
  201. ln -snf "$i" php-cgi
  202. fi
  203. done
  204. ln -snf ~/src/php/$phpbasename/sapi/cli/php php-cli
  205. f=~/src/php/$phpbasename/sapi/cli/php; [ -f "$f" ] && ln -snf "$f" php-cli
  206. f=~/src/php/$phpbasename/sapi/fpm/php-fpm; [ -f "$f" ] && ln -snf "$f" php-fpm
  207. case "$1" in
  208. phpized) CFLAGS=-DXCACHE_DEBUG do_phpize; return $?;;
  209. phpize) do_phpize; return $?;;
  210. make) shift; do_make "$@"; return $?;;
  211. *) do_make;;
  212. esac
  213. # }}}1
  214. if [[ -z $1 ]]; then
  215. set -- devel/sample.cpp.php
  216. fi
  217. cmd=()
  218. phpApp=()
  219. tracer=()
  220. # run utils {{{1
  221. case "$1" in
  222. retest)
  223. shift
  224. lastResult=$(ls php_test_results_*.txt | LANG=C sort | tail -n1)
  225. $MAKE $MAKEOPTS xcachetest "$@" TESTS="$(grep '^/.*\.phpt$' $lastResult | uniq | xargs)"
  226. return
  227. ;;
  228. test)
  229. shift
  230. case "$1" in
  231. *.phpt)
  232. $MAKE $MAKEOPTS xcachetest TEST_ARGS=-v TESTS="$*"
  233. return
  234. ;;
  235. */)
  236. $MAKE $MAKEOPTS xcachetest TESTS="$*"
  237. return
  238. ;;
  239. *)
  240. $MAKE $MAKEOPTS xcachetest
  241. return
  242. ;;
  243. esac
  244. ;;
  245. esac
  246. # }}}
  247. # pick tracer {{{1
  248. case "$1" in
  249. ltr*)
  250. shift
  251. export USE_ZEND_ALLOC=0
  252. tracer=(ltrace -s1024 -e malloc,realloc,free,write)
  253. ;;
  254. str*)
  255. shift
  256. tracer=(strace -s1024 -T)
  257. ;;
  258. gdb)
  259. shift
  260. #USE_ZEND_ALLOC=0
  261. tracer=(gdb --args)
  262. ;;
  263. val*)
  264. shift
  265. export USE_ZEND_ALLOC=0
  266. export ZEND_DONT_UNLOAD_MODULES=1
  267. tracer=(valgrind -v --gen-suppressions=all)
  268. ;;
  269. esac
  270. # pick sapi {{{1
  271. case "$phpbasename" in
  272. *-apache1*)
  273. cmd=($HOME/apache1/bin/httpd -X)
  274. ;;
  275. *-apache*)
  276. echo "Don't know how to run apache"
  277. exit 1
  278. ;;
  279. *)
  280. case "$1" in
  281. dc)
  282. shift
  283. cmd=(./php-cli -c devel.ini)
  284. phpApp=(./bin/phpdc.phpr)
  285. ;;
  286. dboth)
  287. shift
  288. cmd=(./php-cli -c devel.ini)
  289. phpApp=(./bin/phpdc.phpr)
  290. set -- "$@" -c -d
  291. ;;
  292. dop)
  293. shift
  294. cmd=(./php-cli -c devel.ini)
  295. phpApp=(./bin/phpdc.phpr)
  296. set -- "$@" -d
  297. ;;
  298. fcgi)
  299. shift
  300. cmd=(./php-cgi -q -c devel.ini)
  301. set -- -b 1026
  302. ;;
  303. fpm)
  304. shift
  305. cmd=(./php-fpm -c devel.ini -y devel.fpm -g $(readlink -f $pidfile))
  306. set --
  307. ;;
  308. *)
  309. cmd=(./php-cgi -q -c devel.ini)
  310. ;;
  311. esac
  312. "${cmd[@]}" -v || true
  313. esac
  314. # hack sample.cpp.php {{{1
  315. case "$1" in
  316. *.cpp.php)
  317. < "$1" \
  318. sed -r -e 's#__#____#g' \
  319. | cpp -C -P -traditional-cpp -undef -ffreestanding -DPHP_VERSION=$phpVersion \
  320. | sed -r -e 's#^ +##g' -e 's#\t +#\t#g' -e 's#____#__#g' > devel.php.tmp || exit $?
  321. if [[ ! -e devel.php ]] || ! cmp devel.php.tmp devel.php >/dev/null; then
  322. cat devel.php.tmp > devel.php
  323. else
  324. rm -f devel.php.tmp
  325. fi
  326. shift
  327. set -- devel.php "$@"
  328. ;;
  329. esac
  330. # run {{{1
  331. set -- "${tracer[@]}" "${cmd[@]}" "${phpApp[@]}" "$@"
  332. case "${cmd[0]}" in
  333. *php-fpm*)
  334. stopfpm
  335. hiecho Starting fpm "$@" ...
  336. "$@" | tee output.php
  337. echo -n "Ctrl-C to stop"
  338. trap cleanfpm SIGINT SIGTERM exit
  339. cat > /dev/null || true
  340. stopfpm
  341. return
  342. ;;
  343. *)
  344. hiecho "$@"
  345. case "${tracer[@]}" in
  346. gdb*|val*)
  347. "$@"
  348. ;;
  349. *)
  350. "$@" | tee output.php
  351. ret=${PIPESTATUS[0]}
  352. echo exit = $ret >&2
  353. return $ret
  354. ;;
  355. esac
  356. ;;
  357. esac
  358. # }}}
  359. }
  360. for phpbasename in "${dirs[@]}"; do
  361. phpVersion=${phpbasename%%-*}
  362. phpVersion=${phpVersion##php}
  363. phpVersion=${phpVersion/./}
  364. case "$phpVersion" in
  365. ??) phpVersion="${phpVersion}0";;
  366. ?) phpVersion="${phpVersion}00";;
  367. esac
  368. # devel actions
  369. case "$1" in
  370. prep*) shift; prep "$@"; exit;;
  371. tags) shift; rm -f tags; prep tags "$@"; exit;;
  372. po2php) po2php; exit;;
  373. mergepo) mergepo; exit;;
  374. dep*) updatedeps; exit;;
  375. xtest) xtest; exit;;
  376. stopfpm) stopfpm devel.pid; exit;;
  377. esac
  378. mkdir -p ${xcachesrcdir}-${phpbasename}
  379. cd ${xcachesrcdir}-${phpbasename} || exit
  380. lndir ${xcachesrcdir} >/dev/null || true
  381. pwd
  382. run "$@"
  383. done