static-build.sh 8.4 KB

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