static-build.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #!/bin/bash
  2. #
  3. # Copyright 2010, 2011 wkhtmltopdf authors
  4. #
  5. # This file is part of wkhtmltopdf.
  6. #
  7. # wkhtmltopdf is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # wkhtmltopdf is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License
  18. # along with wkhtmltopdf. If not, see <http:#www.gnu.org/licenses/>.
  19. # This script will compile static versions of wkhtmltopdf for linux and for windows (it must run in linux)
  20. # It requires build-essential and wine to run. Please note that it will take quite a while
  21. #Configuration for the static build
  22. MIRROR=kent
  23. MINGWFILES="binutils-2.19.1-mingw32-bin.tar.gz \
  24. mingw32-make-3.81-20090910.tar.gz \
  25. gcc-full-4.4.0-mingw32-bin-2.tar.lzma \
  26. w32api-3.13-mingw32-dev.tar.gz \
  27. mingwrt-3.16-mingw32-dev.tar.gz \
  28. mingwrt-3.16-mingw32-dll.tar.gz"
  29. GNUWIN32FILES="openssl-0.9.8h-1-lib.zip \
  30. openssl-0.9.8h-1-bin.zip "
  31. #freetype-2.3.5-1-bin.zip \
  32. #freetype-2.3.5-1-lib.zip "
  33. QTBRANCH=
  34. J="$((1 + $(cat /proc/cpuinfo | grep -c processor)))"
  35. function usage() {
  36. echo "Usage: $0 [OPTIONS] target"
  37. echo ""
  38. echo "Options:"
  39. echo "-h Display this help message"
  40. echo "-q branch Use this qt branch"
  41. echo "-v version Compile this version of wkhtmltopdf"
  42. echo ""
  43. echo "Target:"
  44. echo "linux-local Compile under local linux distro"
  45. echo "linux-amd64 Compile under debian 64bit chroot"
  46. echo "linux-i368 Compile under debian 32bit chroot"
  47. echo "windows Compile windows binary using wine"
  48. }
  49. VERSION=""
  50. QTBRANCH="staging"
  51. while getopts hq:v: O; do
  52. case "$O" in
  53. [h?])
  54. usage
  55. exit 1
  56. ;;
  57. v)
  58. shift 2
  59. VERSION=$OPTARG
  60. ;;
  61. q)
  62. shift 2
  63. QTBRANCH=$OPTARG
  64. ;;
  65. esac
  66. done
  67. if file /bin/true | grep -q 64-bit; then
  68. UPX=upx-3.03-amd64_linux
  69. else
  70. UPX=upx-3.03-i386_linux
  71. fi
  72. #Helper functions
  73. function get() {
  74. [ -f $2.download ] || (rm -rf $2; wget $1 -O $2 && touch $2.download)
  75. }
  76. function unpack() {
  77. [ -f $1.unpack ] || (echo "unpacking $1"; (tar -xf $1 || unzip -o $1) && touch $1.unpack)
  78. }
  79. BASE=${PWD}
  80. BUILD=${BASE}/static-build
  81. VERSION=$2
  82. function git_fetch_and_update() {
  83. if [ ! -d "$1" ]; then
  84. git clone "$2" "$1" || (rm -rf "$1" && return 1)
  85. fi
  86. cd "$1"
  87. git fetch origin
  88. if ! (git checkout "$3" -f 2>/dev/null && git pull origin "$3" 2>/dev/null); then
  89. git branch -a
  90. git checkout origin/"$3" -f 2>/dev/null || return 1
  91. git branch -D "$3" 2>/dev/null
  92. git checkout origin/"$3" -b "$3" || return 1
  93. git pull origin "$3" -f || return 1
  94. fi
  95. cd ..
  96. }
  97. function checkout() {
  98. #Create static build directory
  99. mkdir -p $BUILD
  100. cd ${BUILD}
  101. #Fetch and unpack upx
  102. get http://upx.sourceforge.net/download/${UPX}.tar.bz2 ${UPX}.tar.bz2 || exit 1
  103. unpack ${UPX}.tar.bz2 || exit 1
  104. #Fetch most recent version of qt
  105. echo "Updating qt from remote"
  106. git_fetch_and_update qt git://gitorious.org/+wkhtml2pdf/qt/wkhtmltopdf-qt.git "$QTBRANCH" || exit 1
  107. cd qt
  108. }
  109. function setup_build() {
  110. echo "Updating QT"
  111. git_fetch_and_update qts ${BUILD}/qt "$QTBRANCH" || exit 1
  112. cd qts
  113. if ! [ -z "$VERSION" ] ; then
  114. git checkout wkhtmltopdf-$VERSION || exit 1
  115. fi
  116. touch conf
  117. cat ${BASE}/static_qt_conf_base ${BASE}/static_qt_conf_${1} | sed -re 's/#.*//' | sed -re '/^[ \t]*$/d' | sort -u > conf_new
  118. cd ..
  119. echo "$(cd ${BASE} && git branch --no-color | sed -nre 's/\* //p')"
  120. git_fetch_and_update wkhtmltopdf "${BASE}" "$(cd ${BASE} && git branch --no-color | sed -nre 's/\* //p')" || exit 1
  121. cd wkhtmltopdf
  122. if ! [ -z "$VERSION" ] ; then
  123. git checkout "$VERSION" || exit 1
  124. fi
  125. cd ..
  126. [ "$1" == "win" ] && return
  127. cat > build.sh <<EOF
  128. unset LANG
  129. unset LC_ALL
  130. unset LANGUAGE
  131. HERE="\${PWD}"
  132. mkdir -p qt/lib
  133. cd qts
  134. function do_configure() {
  135. echo "Configuring QT"
  136. (yes yes | ./configure \`cat conf_new\` -prefix "\${HERE}/qt" && cp conf_new conf) || exit 1
  137. }
  138. if ! cmp conf conf_new; then
  139. QTDIR=. bin/syncqt || exit 1
  140. do_configure
  141. fi
  142. if ! make -j${J} -q; then
  143. echo "Building QT"
  144. (make -j${J} && make install) || (make distclean; do_configure && make -j${J} && make install) || exit 1
  145. fi
  146. cd ../wkhtmltopdf
  147. echo "Building wkhtmltopdfe"
  148. (make distclean; ../qt/bin/qmake && make -j${J}) || exit 1
  149. strip ./bin/wkhtmltopdf || exit 1
  150. strip ./bin/wkhtmltoimage || exit 1
  151. EOF
  152. chmod +x build.sh
  153. }
  154. function packandcopylinux() {
  155. WK=${BUILD}/linux-$1/build/wkhtmltopdf
  156. rm -rf ${BASE}/bin/wkhtmltopdf-$1 ${BASE}/bin/wkhtmltoimage-$1 ${BASE}/bin/libwkhtmltopdf-$1.tar.lzma
  157. ${BUILD}/${UPX}/upx --best ${WK}/bin/wkhtmltopdf -o ${BASE}/bin/wkhtmltopdf-$1 || exit 1
  158. ${BUILD}/${UPX}/upx --best ${WK}/bin/wkhtmltoimage -o ${BASE}/bin/wkhtmltoimage-$1 || exit 1
  159. rm -rf ${WK}/lib
  160. mkdir -p ${WK}/lib
  161. cp ${WK}/bin/libwkhtmltox*.so ${WK}/lib || exit 1
  162. cd ${WK} && tar -c --bzip2 -f ${BASE}/bin/libwkhtmltox-$1.tar.bz2 lib include examples/Makefile examples/pdf_c_api.c examples/image_c_api.c
  163. }
  164. function build_linux_local() {
  165. cd ${BUILD}
  166. mkdir -p linux-local
  167. cd linux-local
  168. setup_build linux
  169. ./build.sh || exit 1
  170. cd ..
  171. packandcopylinux local
  172. }
  173. function setup_chroot() {
  174. if [ ! -f linux-$2/strapped ]; then
  175. sudo rm -rf linux-$2
  176. (sudo debootstrap --arch=$2 --variant=buildd $1 ./linux-$2 http://ftp.us.debian.org/debian/ && sudo touch linux-$2/strapped) || exit 1
  177. fi
  178. if [ ! -d linux-$2/build ]; then
  179. sudo mkdir -p linux-$2/build || exit 1
  180. sudo chown --reference=. linux-$2/build -Rv || exit 1
  181. fi
  182. if [ ! -f linux-$2/installed ]; then
  183. echo -e "deb http://ftp.us.debian.org/debian/ $1 main non-free contrib\ndeb-src http://ftp.us.debian.org/debian/ $1 main non-free contrib" | sudo tee linux-$2/etc/apt/sources.list || exit 1
  184. sudo chroot linux-$2 apt-get -y update || exit 1
  185. sudo chroot linux-$2 apt-get -y build-dep libqt4-core && sudo touch linux-$2/installed || exit 1
  186. fi
  187. echo "cd /build && ./build.sh" > linux-$2/build/buildw.sh
  188. chmod +x linux-$2/build/buildw.sh || exit 1
  189. }
  190. function build_linux_chroot() {
  191. cd ${BUILD}
  192. setup_chroot lenny $1
  193. cd linux-$1/build
  194. setup_build linux
  195. if [ "$1" == 'i386' ]; then
  196. sudo linux32 chroot ${BUILD}/linux-$1/ /build/buildw.sh || exit 1
  197. else
  198. sudo chroot ${BUILD}/linux-$1/ /build/buildw.sh || exit 1
  199. fi
  200. packandcopylinux $1
  201. }
  202. function build_windows() {
  203. cd ${BUILD}
  204. export WINEPREFIX=${BUILD}/windows
  205. if [ ! -f ${BUILD}/windows/create ]; then
  206. cat > tmp <<EOF
  207. REGEDIT4
  208. [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment]
  209. "PATH"="C:\\\\windows;C:\\\\windows\\\\system32;C:\\\\mingw\\\\bin"
  210. "INCLUDE"="C:\\\\mingw\\\\include;C:\\\\mingw\\\\include\\\\c++\\\\3.4.5"
  211. "LIB"="C:\\\\mingw\\\\lib"
  212. EOF
  213. wine regedit tmp || exit 1
  214. touch ${BUILD}/windows/create
  215. fi
  216. #http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/fontconfig-2.4.2-tml-20071015.zip
  217. mkdir -p windows/drive_c/mingw
  218. cd windows/drive_c/mingw
  219. for file in ${MINGWFILES}; do
  220. get http://${MIRROR}.dl.sourceforge.net/sourceforge/mingw/${file} ${file} || exit 1
  221. unpack ${file} || exit 1
  222. done
  223. for file in ${GNUWIN32FILES}; do
  224. get http://downloads.sourceforge.net/gnuwin32/${file} ${file} || exit 1
  225. unpack ${file} || exit 1
  226. done
  227. cd ..
  228. setup_build win
  229. unset CPLUS_INCLUDE_PATH
  230. unset C_INCLUDE_PATH
  231. export CPLUS_INCLUDE_PATH=
  232. export C_INCLUDE_PATH=C:\qts\include
  233. mkdir -p qt
  234. cp -r qts/mkspecs qt
  235. cd qts
  236. if ! [ -f Makefile ] || ! cmp conf conf_new; then
  237. QTDIR=. bin/syncqt || exit 1
  238. (yes | wine configure.exe -I "C:\qts\include" -I "C:\mingw32\include\freetype2" `cat conf_new` -prefix "C:\qt" && cp conf_new conf) || exit 1
  239. fi
  240. if ! wine mingw32-make -j${J} -q; then
  241. wine mingw32-make -j${J} || exit 1
  242. wine mingw32-make install || exit 1
  243. fi
  244. cd ../wkhtmltopdf
  245. wine mingw32-make dist-clean
  246. wine ../qt/bin/qmake.exe wkhtmltopdf.pro -o Makefile -spec win32-g++ || exit 1
  247. wine mingw32-make clean || exit 1
  248. wine mingw32-make -j${J} || exit 1
  249. wine strip.exe bin/wkhtmltopdf.exe || exit 1
  250. wine strip.exe bin/wkhtmltoimage.exe || exit 1
  251. rm -rf ${BASE}/bin/wkhtmltopdf.exe ${BASE}/bin/wkhtmltoimage.exe ${BASE}/bin/libwkhtmltox.zip
  252. ${BUILD}/${UPX}/upx --best bin/wkhtmltopdf.exe -o ${BASE}/bin/wkhtmltopdf.exe || exit 1
  253. ${BUILD}/${UPX}/upx --best bin/wkhtmltoimage.exe -o ${BASE}/bin/wkhtmltoimage.exe || exit 1
  254. rm -rf lib
  255. mkdir -p lib
  256. cp bin/wkhtmltox*.dll bin/libwkhtmltox*.a lib || exit 1
  257. zip -r9 ${BASE}/bin/libwkhtmltox.zip lib include examples/Makefile examples/pdf_c_api.c
  258. }
  259. case "$1" in
  260. 'linux-local')
  261. checkout
  262. build_linux_local
  263. ;;
  264. 'linux-i386')
  265. checkout
  266. build_linux_chroot i386
  267. ;;
  268. 'linux-amd64')
  269. checkout
  270. build_linux_chroot amd64
  271. ;;
  272. 'windows')
  273. checkout
  274. build_windows
  275. ;;
  276. 'release')
  277. checkout
  278. build_linux_chroot i386
  279. build_linux_chroot amd64
  280. build_windows
  281. ;;
  282. *)
  283. usage
  284. ;;
  285. esac