build.py 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2014 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. # --------------------------------------------------------------- CONFIGURATION
  20. PROJECT_SETUP = {
  21. 'name': 'wkhtmltox',
  22. 'description': 'convert HTML to PDF and various image formats using QtWebkit',
  23. 'license': 'LGPLv3',
  24. 'maintainer': 'Ashish Kulkarni <kulkarni.ashish@gmail.com>',
  25. 'url': 'http://wkhtmltopdf.org/'
  26. }
  27. BUILDERS = {
  28. 'source-tarball': 'source_tarball',
  29. 'msvc2013-win32': 'msvc',
  30. 'msvc2013-win64': 'msvc',
  31. 'setup-mingw-w64': 'setup_mingw_w64',
  32. 'setup-schroot-centos7': 'setup_schroot',
  33. 'setup-schroot-wheezy': 'setup_schroot',
  34. 'setup-schroot-jessie': 'setup_schroot',
  35. 'setup-schroot-trusty': 'setup_schroot',
  36. 'setup-schroot-precise': 'setup_schroot',
  37. 'update-all-schroots': 'update_schroot',
  38. 'centos7-amd64': 'linux_schroot',
  39. 'wheezy-i386': 'linux_schroot',
  40. 'wheezy-amd64': 'linux_schroot',
  41. 'jessie-i386': 'linux_schroot',
  42. 'jessie-amd64': 'linux_schroot',
  43. 'trusty-i386': 'linux_schroot',
  44. 'trusty-amd64': 'linux_schroot',
  45. 'precise-i386': 'linux_schroot',
  46. 'precise-amd64': 'linux_schroot',
  47. 'mingw-w64-cross-win32': 'mingw64_cross',
  48. 'mingw-w64-cross-win64': 'mingw64_cross',
  49. 'posix-local': 'posix_local',
  50. 'osx-cocoa-x86-64': 'osx',
  51. 'osx-carbon-i386': 'osx'
  52. }
  53. QT_CONFIG = {
  54. 'common' : [
  55. '-opensource',
  56. '-confirm-license',
  57. '-fast',
  58. '-release',
  59. '-static',
  60. '-graphicssystem raster',
  61. '-webkit',
  62. '-exceptions', # required by XmlPatterns
  63. '-xmlpatterns', # required for TOC support
  64. '-system-zlib',
  65. '-system-libpng',
  66. '-system-libjpeg',
  67. '-no-libmng',
  68. '-no-libtiff',
  69. '-no-accessibility',
  70. '-no-stl',
  71. '-no-qt3support',
  72. '-no-phonon',
  73. '-no-phonon-backend',
  74. '-no-opengl',
  75. '-no-declarative',
  76. '-no-script',
  77. '-no-scripttools',
  78. '-no-sql-ibase',
  79. '-no-sql-mysql',
  80. '-no-sql-odbc',
  81. '-no-sql-psql',
  82. '-no-sql-sqlite',
  83. '-no-sql-sqlite2',
  84. '-no-mmx',
  85. '-no-3dnow',
  86. '-no-sse',
  87. '-no-sse2',
  88. '-no-multimedia',
  89. '-nomake demos',
  90. '-nomake docs',
  91. '-nomake examples',
  92. '-nomake tools',
  93. '-nomake tests',
  94. '-nomake translations'
  95. ],
  96. 'msvc': [
  97. '-mp',
  98. '-qt-style-windows',
  99. '-qt-style-cleanlooks',
  100. '-no-style-windowsxp',
  101. '-no-style-windowsvista',
  102. '-no-style-plastique',
  103. '-no-style-motif',
  104. '-no-style-cde',
  105. '-openssl-linked' # static linkage for OpenSSL
  106. ],
  107. 'posix': [
  108. '-silent', # perform a silent build
  109. '-xrender', # xrender support is required
  110. '-largefile',
  111. '-iconv', # iconv support is required for text codecs
  112. '-openssl', # load OpenSSL binaries at runtime
  113. '-no-javascript-jit', # can cause crashes/excess memory usage
  114. '-no-rpath',
  115. '-no-dbus',
  116. '-no-nis',
  117. '-no-cups',
  118. '-no-pch',
  119. '-no-gtkstyle',
  120. '-no-nas-sound',
  121. '-no-sm',
  122. '-no-xshape',
  123. '-no-xinerama',
  124. '-no-xcursor',
  125. '-no-xfixes',
  126. '-no-xrandr',
  127. '-no-mitshm',
  128. '-no-xinput',
  129. '-no-xkb',
  130. '-no-glib',
  131. '-no-gstreamer',
  132. '-no-icu',
  133. '-no-openvg',
  134. '-no-xsync',
  135. '-no-audio-backend',
  136. '-no-sse3',
  137. '-no-ssse3',
  138. '-no-sse4.1',
  139. '-no-sse4.2',
  140. '-no-avx',
  141. '-no-neon'
  142. ],
  143. 'mingw-w64-cross' : [
  144. '-silent', # perform a silent build
  145. '-openssl-linked', # static linkage for OpenSSL
  146. '-no-reduce-exports',
  147. '-no-rpath',
  148. '-xplatform win32-g++-4.6'
  149. ],
  150. 'osx': [
  151. '-silent', # perform a silent build
  152. '-no-framework',
  153. '-no-dwarf2',
  154. '-xrender', # xrender support is required
  155. '-openssl', # load OpenSSL binaries at runtime
  156. '-largefile',
  157. '-no-rpath'
  158. ]
  159. }
  160. LINUX_SCHROOT_SETUP = {
  161. 'wheezy': {
  162. 'title' : 'Debian Wheezy',
  163. 'packaging_tool' : 'apt',
  164. 'build_arch' : ['amd64', 'i386'],
  165. 'compression' : 'xz',
  166. 'runtime_packages' : 'libc6 libstdc++6 zlib1g libpng12-0 libjpeg62 '\
  167. 'libssl1.0.0 libfreetype6 libicu48 fontconfig '\
  168. 'libx11-6 libxext6 libxrender1 libxcb1 xfonts-base xfonts-75dpi',
  169. 'build_packages' : 'xz-utils ruby python perl gperf bison flex git '\
  170. 'zlib1g-dev libpng12-dev libjpeg62-dev libssl-dev libfreetype6-dev libicu-dev libfontconfig1-dev '\
  171. 'libx11-dev libxext-dev libxrender-dev libxcb1-dev',
  172. 'debootstrap' : ('wheezy', 'http://ftp.us.debian.org/debian/', """
  173. deb http://ftp.debian.org/debian/ wheezy main contrib non-free
  174. deb http://ftp.debian.org/debian/ wheezy-updates main contrib non-free
  175. deb http://security.debian.org/ wheezy/updates main contrib non-free""")
  176. },
  177. 'jessie': {
  178. 'title' : 'Debian Jessie',
  179. 'packaging_tool' : 'apt',
  180. 'build_arch' : ['amd64', 'i386'],
  181. 'compression' : 'xz',
  182. 'runtime_packages' : 'libc6 libstdc++6 zlib1g libpng12-0 libjpeg62-turbo '\
  183. 'libssl1.0.0 libfreetype6 libicu52 fontconfig '\
  184. 'libx11-6 libxext6 libxrender1 libxcb1 xfonts-base xfonts-75dpi',
  185. 'build_packages' : 'xz-utils ruby python perl gperf bison flex git '\
  186. 'zlib1g-dev libpng12-dev libjpeg62-turbo-dev libssl-dev libfreetype6-dev libicu-dev libfontconfig1-dev '\
  187. 'libx11-dev libxext-dev libxrender-dev libxcb1-dev',
  188. 'debootstrap' : ('jessie', 'http://ftp.us.debian.org/debian/', """
  189. deb http://ftp.debian.org/debian/ jessie main contrib non-free
  190. deb http://ftp.debian.org/debian/ jessie-updates main contrib non-free
  191. deb http://security.debian.org/ jessie/updates main contrib non-free""")
  192. },
  193. 'trusty': {
  194. 'title' : 'Ubuntu Trusty',
  195. 'packaging_tool' : 'apt',
  196. 'build_arch' : ['amd64', 'i386'],
  197. 'compression' : 'xz',
  198. 'runtime_packages' : 'libc6 libstdc++6 zlib1g libpng12-0 libjpeg-turbo8 '\
  199. 'libssl1.0.0 libfreetype6 libicu52 fontconfig '\
  200. 'libx11-6 libxext6 libxrender1 libxcb1 xfonts-base xfonts-75dpi',
  201. 'build_packages' : 'xz-utils ruby python perl gperf bison flex git '\
  202. 'zlib1g-dev libpng12-dev libjpeg-turbo8-dev libssl-dev libfreetype6-dev libicu-dev libfontconfig1-dev '\
  203. 'libx11-dev libxext-dev libxrender-dev libxcb1-dev',
  204. 'debootstrap' : ('trusty', 'http://archive.ubuntu.com/ubuntu/', """
  205. deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
  206. deb http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
  207. deb http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse""")
  208. },
  209. 'precise': {
  210. 'title' : 'Ubuntu Precise',
  211. 'packaging_tool' : 'apt',
  212. 'build_arch' : ['amd64', 'i386'],
  213. 'compression' : 'xz',
  214. 'runtime_packages' : 'libc6 libstdc++6 zlib1g libpng12-0 libjpeg-turbo8 '\
  215. 'libssl1.0.0 libfreetype6 libicu48 fontconfig '\
  216. 'libx11-6 libxext6 libxrender1 libxcb1 xfonts-base xfonts-75dpi',
  217. 'build_packages' : 'xz-utils ruby python perl gperf bison flex git '\
  218. 'zlib1g-dev libpng12-dev libjpeg-turbo8-dev libssl-dev libfreetype6-dev libicu-dev libfontconfig1-dev '\
  219. 'libx11-dev libxext-dev libxrender-dev libxcb1-dev',
  220. 'debootstrap' : ('precise', 'http://archive.ubuntu.com/ubuntu/', """
  221. deb http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse
  222. deb http://archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse
  223. deb http://archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse""")
  224. },
  225. 'centos7': {
  226. 'title' : 'CentOS 7',
  227. 'packaging_tool' : 'yum',
  228. 'build_arch' : ['amd64'],
  229. 'compression' : 'xz',
  230. 'runtime_packages' : 'glibc libstdc++ zlib libpng libjpeg openssl freetype icu fontconfig '\
  231. 'libX11 libXext libXrender xorg-x11-fonts-Type1 xorg-x11-fonts-75dpi',
  232. 'build_packages' : 'gcc gcc-c++ binutils python ruby perl git make diffutils gperf bison flex '\
  233. 'zlib-devel libpng-devel libjpeg-turbo-devel openssl-devel freetype-devel libicu-devel fontconfig-devel '\
  234. 'libX11-devel libXrender-devel libXext-devel',
  235. 'rinse' : 'centos-7'
  236. }
  237. }
  238. DEPENDENT_LIBS = {
  239. 'openssl': {
  240. 'order' : 1,
  241. 'url' : 'https://openssl.org/source/openssl-1.0.2c.tar.gz',
  242. 'sha1' : '6e4a5e91159eb32383296c7c83ac0e59b83a0a44',
  243. 'build' : {
  244. 'msvc*-win32*': {
  245. 'result': ['include/openssl/ssl.h', 'lib/ssleay32.lib', 'lib/libeay32.lib'],
  246. 'replace': [('util/pl/VC-32.pl', ' /MT', ' %(cflags)s')],
  247. 'commands': [
  248. 'perl Configure --openssldir=%(destdir)s VC-WIN32 no-asm',
  249. 'ms\\do_ms.bat',
  250. 'nmake /f ms\\nt.mak install'],
  251. },
  252. 'msvc*-win64*': {
  253. 'result': ['include/openssl/ssl.h', 'lib/ssleay32.lib', 'lib/libeay32.lib'],
  254. 'replace': [('util/pl/VC-32.pl', ' /MT', ' %(cflags)s')],
  255. 'commands': [
  256. 'perl Configure --openssldir=%(destdir)s VC-WIN64A',
  257. 'ms\\do_win64a.bat',
  258. 'nmake /f ms\\nt.mak install']
  259. },
  260. 'mingw-w64-cross-win*': {
  261. 'result': ['include/openssl/ssl.h', 'lib/libssl.a', 'lib/libcrypto.a'],
  262. 'commands': [
  263. 'perl Configure --openssldir=%(destdir)s --cross-compile-prefix=%(mingw_w64)s- no-shared no-asm mingw64',
  264. 'make',
  265. 'make install_sw']
  266. }
  267. }
  268. },
  269. 'zlib': {
  270. 'order' : 2,
  271. 'url' : 'http://downloads.sourceforge.net/libpng/zlib-1.2.8.tar.gz',
  272. 'sha1' : 'a4d316c404ff54ca545ea71a27af7dbc29817088',
  273. 'build' : {
  274. 'msvc*': {
  275. 'result': {
  276. 'include/zlib.h' : 'zlib.h',
  277. 'include/zconf.h': 'zconf.h',
  278. 'lib/zdll.lib' : 'zlib.lib'
  279. },
  280. 'replace': [('win32/Makefile.msc', '-MD', '%(cflags)s')],
  281. 'commands': ['nmake /f win32/Makefile.msc zlib.lib']
  282. },
  283. 'mingw-w64-cross-win*': {
  284. 'result': {
  285. 'include/zlib.h' : 'zlib.h',
  286. 'include/zconf.h': 'zconf.h',
  287. 'lib/libz.a' : 'libz.a'
  288. },
  289. 'replace': [('win32/Makefile.gcc', 'PREFIX =', 'PREFIX = %(mingw_w64)s-')],
  290. 'commands': ['make -f win32/Makefile.gcc']
  291. }
  292. }
  293. },
  294. 'libpng': {
  295. 'order' : 3,
  296. 'url' : 'http://downloads.sourceforge.net/libpng/libpng-1.2.53.tar.gz',
  297. 'sha1': '22f3cc22d26727af05d7c9a970a7d050b6761bd7',
  298. 'build' : {
  299. 'msvc*': {
  300. 'result': {
  301. 'include/png.h' : 'png.h',
  302. 'include/pngconf.h' : 'pngconf.h',
  303. 'lib/libpng.lib' : 'libpng.lib'
  304. },
  305. 'replace': [
  306. ('scripts/makefile.vcwin32', '-I..\\zlib', '-I..\\deplibs\\include'),
  307. ('scripts/makefile.vcwin32', '..\\zlib\\zlib.lib', '..\\deplibs\\lib\\zdll.lib'),
  308. ('scripts/makefile.vcwin32', '-MD', '%(cflags)s')],
  309. 'commands': ['nmake /f scripts/makefile.vcwin32 libpng.lib']
  310. },
  311. 'mingw-w64-cross-win*': {
  312. 'result': {
  313. 'include/png.h' : 'png.h',
  314. 'include/pngconf.h' : 'pngconf.h',
  315. 'lib/libpng.a' : 'libpng.a'
  316. },
  317. 'replace': [
  318. ('scripts/makefile.gcc', 'ZLIBINC = ../zlib', 'ZLIBINC = %(destdir)s/include'),
  319. ('scripts/makefile.gcc', 'ZLIBLIB = ../zlib', 'ZLIBLIB = %(destdir)s/lib'),
  320. ('scripts/makefile.gcc', 'CC = gcc', 'CC = %(mingw_w64)s-gcc'),
  321. ('scripts/makefile.gcc', 'AR_RC = ar', 'AR_RC = %(mingw_w64)s-ar'),
  322. ('scripts/makefile.gcc', 'RANLIB = ranlib', 'RANLIB = %(mingw_w64)s-ranlib')],
  323. 'commands': ['make -f scripts/makefile.gcc libpng.a']
  324. },
  325. 'osx-carbon-i386': {
  326. 'result': ['include/png.h', 'include/pngconf.h', 'lib/libpng.a'],
  327. 'commands': [
  328. 'CFLAGS="-arch i386" ./configure --disable-shared --prefix=%(destdir)s',
  329. 'make install']
  330. },
  331. 'osx-cocoa-x86-64': {
  332. 'result': ['include/png.h', 'include/pngconf.h', 'lib/libpng.a'],
  333. 'commands': [
  334. 'CFLAGS="-arch x86_64" ./configure --disable-shared --prefix=%(destdir)s',
  335. 'make install']
  336. }
  337. }
  338. },
  339. 'libjpeg': {
  340. 'order' : 4,
  341. 'url' : 'http://ijg.org/files/jpegsrc.v9a.tar.gz',
  342. 'sha1': 'd65ed6f88d318f7380a3a5f75d578744e732daca',
  343. 'build' : {
  344. 'msvc*': {
  345. 'result': {
  346. 'include/jpeglib.h' : 'jpeglib.h',
  347. 'include/jmorecfg.h': 'jmorecfg.h',
  348. 'include/jerror.h' : 'jerror.h',
  349. 'include/jconfig.h' : 'jconfig.h',
  350. 'lib/libjpeg.lib' : 'libjpeg.lib'
  351. },
  352. 'replace': [('makefile.vc', '!include <win32.mak>', ''),
  353. ('makefile.vc', '$(cc)', 'cl'),
  354. ('makefile.vc', '$(cflags) $(cdebug) $(cvars)', '-c -nologo -D_CRT_SECURE_NO_DEPRECATE %(cflags)s -O2 -W3')],
  355. 'commands': [
  356. 'copy /y jconfig.vc jconfig.h',
  357. 'nmake /f makefile.vc libjpeg.lib']
  358. },
  359. 'mingw-w64-cross-win*': {
  360. 'result': ['include/jpeglib.h', 'include/jmorecfg.h', 'include/jerror.h', 'include/jconfig.h', 'lib/libjpeg.a'],
  361. 'commands': [
  362. './configure --host=%(mingw_w64)s --disable-shared --prefix=%(destdir)s',
  363. 'make install']
  364. },
  365. 'osx-carbon-i386': {
  366. 'result': ['include/jpeglib.h', 'include/jmorecfg.h', 'include/jerror.h', 'include/jconfig.h', 'lib/libjpeg.a'],
  367. 'commands': [
  368. 'CFLAGS="-arch i386" ./configure --disable-shared --prefix=%(destdir)s',
  369. 'make install']
  370. },
  371. 'osx-cocoa-x86-64': {
  372. 'result': ['include/jpeglib.h', 'include/jmorecfg.h', 'include/jerror.h', 'include/jconfig.h', 'lib/libjpeg.a'],
  373. 'commands': [
  374. 'CFLAGS="-arch x86_64" ./configure --disable-shared --prefix=%(destdir)s',
  375. 'make install']
  376. }
  377. }
  378. },
  379. 'xz': {
  380. 'order' : 5,
  381. 'url' : 'http://tukaani.org/xz/xz-5.2.1.tar.gz',
  382. 'sha1': '6022493efb777ff4e872b63a60be1f1e146f3c0b',
  383. 'build' : {
  384. 'osx*': {
  385. 'result': ['bin/xz'],
  386. 'commands': [
  387. 'CFLAGS="-arch i386 -mmacosx-version-min=10.6" ./configure --disable-nls --enable-small --disable-shared --disable-threads --prefix=%(destdir)s',
  388. 'make -C src/liblzma', 'make -C src/xz', 'make install-strip']
  389. }
  390. }
  391. }
  392. }
  393. EXCLUDE_SRC_TARBALL = [
  394. 'qt/config.profiles*',
  395. 'qt/demos*',
  396. 'qt/dist*',
  397. 'qt/doc*',
  398. 'qt/examples*',
  399. 'qt/imports*',
  400. 'qt/templates*',
  401. 'qt/tests*',
  402. 'qt/translations*',
  403. 'qt/util*',
  404. 'qt/lib/fonts*',
  405. 'qt/src/3rdparty/*ChangeLog*',
  406. 'qt/src/3rdparty/ce-compat*',
  407. 'qt/src/3rdparty/clucene*',
  408. 'qt/src/3rdparty/fonts*',
  409. 'qt/src/3rdparty/freetype*',
  410. 'qt/src/3rdparty/javascriptcore*',
  411. 'qt/src/3rdparty/libgq*',
  412. 'qt/src/3rdparty/libmng*',
  413. 'qt/src/3rdparty/libtiff*',
  414. 'qt/src/3rdparty/patches*',
  415. 'qt/src/3rdparty/phonon*',
  416. 'qt/src/3rdparty/pixman*',
  417. 'qt/src/3rdparty/powervr*',
  418. 'qt/src/3rdparty/ptmalloc*',
  419. 'qt/src/3rdparty/s60*',
  420. 'qt/src/3rdparty/wayland*'
  421. ]
  422. # --------------------------------------------------------------- HELPERS
  423. import os, sys, platform, subprocess, shutil, re, fnmatch, multiprocessing, urllib, hashlib, tarfile
  424. from os.path import exists
  425. if platform.system() == 'Windows':
  426. try:
  427. import winreg
  428. except ImportError:
  429. import _winreg as winreg
  430. CPU_COUNT = max(2, multiprocessing.cpu_count()-1) # leave one CPU free
  431. else:
  432. CPU_COUNT = max(2, multiprocessing.cpu_count())
  433. def rchop(s, e):
  434. if s.endswith(e):
  435. return s[:-len(e)]
  436. return s
  437. def message(msg):
  438. sys.stdout.write(msg)
  439. sys.stdout.flush()
  440. def error(msg):
  441. message(msg+'\n')
  442. sys.exit(1)
  443. def shell(cmd):
  444. ret = os.system(cmd)
  445. if ret != 0:
  446. error("%s\ncommand failed: exit code %d" % (cmd, ret))
  447. def chroot_shell(name, cmd):
  448. distro = get_chroot_list().get(name)
  449. wrapper = LINUX_SCHROOT_SETUP.get(distro, {}).get('wrapper_command', '')
  450. ret = os.system('schroot -c wkhtmltox-%s -- %s%s ' % (name, wrapper, cmd))
  451. if ret != 0:
  452. error("command inside chroot failed: exit code %d" % ret)
  453. def get_output(*cmd):
  454. try:
  455. return subprocess.check_output(cmd, stderr=subprocess.STDOUT).strip()
  456. except:
  457. return None
  458. def rmdir(path):
  459. if exists(path):
  460. if platform.system() == 'Windows':
  461. shell('attrib -R %s\* /S' % path)
  462. shutil.rmtree(path)
  463. def mkdir_p(path):
  464. if not exists(path):
  465. os.makedirs(path)
  466. def get_registry_value(key, value=None):
  467. for mask in [0, winreg.KEY_WOW64_64KEY, winreg.KEY_WOW64_32KEY]:
  468. try:
  469. reg_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key, 0, winreg.KEY_READ | mask)
  470. return winreg.QueryValueEx(reg_key, value)[0]
  471. except WindowsError:
  472. pass
  473. return None
  474. def get_version(basedir):
  475. mkdir_p(basedir)
  476. text = open(os.path.join(basedir, '..', 'VERSION'), 'r').read()
  477. if '-' not in text:
  478. return (text, text)
  479. version = text[:text.index('-')]
  480. os.chdir(os.path.join(basedir, '..'))
  481. hash = get_output('git', 'rev-parse', '--short', 'HEAD')
  482. if not hash:
  483. return (text, version)
  484. return ('%s-%s' % (text, hash), version)
  485. def nsis_version(ver):
  486. while ver.count('.') < 3:
  487. ver += '.0'
  488. return ver
  489. def qt_config(key, *opts):
  490. input, output = [], []
  491. input.extend(QT_CONFIG['common'])
  492. input.extend(QT_CONFIG[key])
  493. input.extend(opts)
  494. cfg = os.environ.get('WKHTMLTOX_QT_CONFIG')
  495. if cfg:
  496. input.extend(cfg.split())
  497. for arg in input:
  498. if not arg.startswith('remove:-'):
  499. output.append(arg)
  500. elif arg[1+arg.index(':'):] in output:
  501. output.remove(arg[1+arg.index(':'):])
  502. return ' '.join(output)
  503. def fpm_params(cfg, ver):
  504. setup = LINUX_SCHROOT_SETUP[cfg[:cfg.index('-')]]
  505. output = '--force --prefix /usr/local --category utils -s dir -C dist'
  506. for key in PROJECT_SETUP:
  507. output += ' --%s "%s"' % (key, PROJECT_SETUP[key])
  508. output += ' --version "%s"' % ver
  509. output += ' --package ../%s-%s_linux-%s' % (PROJECT_SETUP['name'], ver, cfg)
  510. if setup['packaging_tool'] == 'apt':
  511. output += '.deb -t deb --deb-compression %s' % setup['compression']
  512. output += ' --provides wkhtmltopdf --conflicts wkhtmltopdf --replaces wkhtmltopdf'
  513. elif setup['packaging_tool'] == 'yum':
  514. output += '.rpm -t rpm --rpm-compression %s --epoch 1' % setup['compression']
  515. for depend in setup['runtime_packages'].split():
  516. if setup['packaging_tool'] == 'apt':
  517. version = get_output('dpkg-query', '-W', '-f=${Version}', depend)
  518. if version:
  519. output += ' --depends "%s >= %s"' % (depend, version)
  520. else:
  521. output += ' --depends %s' % depend
  522. elif setup['packaging_tool'] == 'yum':
  523. output += ' --depends %s' % depend
  524. output += ' .'
  525. return output
  526. def download_file(url, sha1, dir):
  527. name = url.split('/')[-1]
  528. loc = os.path.join(dir, name)
  529. if os.path.exists(loc):
  530. hash = hashlib.sha1(open(loc, 'rb').read()).hexdigest()
  531. if hash == sha1:
  532. return loc
  533. os.remove(loc)
  534. message('Checksum mismatch for %s, re-downloading.\n' % name)
  535. def hook(cnt, bs, total):
  536. pct = int(cnt*bs*100/total)
  537. message("\rDownloading: %s [%d%%]" % (name, pct))
  538. urllib.urlretrieve(url, loc, reporthook=hook)
  539. message("\r")
  540. hash = hashlib.sha1(open(loc, 'rb').read()).hexdigest()
  541. if hash != sha1:
  542. os.remove(loc)
  543. error('Checksum mismatch for %s, aborting.' % name)
  544. message("\rDownloaded: %s [checksum OK]\n" % name)
  545. return loc
  546. def download_tarball(url, sha1, dir, name):
  547. loc = download_file(url, sha1, dir)
  548. tar = tarfile.open(loc)
  549. sub = tar.getnames()[0]
  550. if '/' in sub:
  551. sub = sub[:sub.index('/')]
  552. src = os.path.join(dir, sub)
  553. tgt = os.path.join(dir, name)
  554. rmdir(src)
  555. tar.extractall(dir)
  556. rmdir(tgt)
  557. os.rename(src, tgt)
  558. return tgt
  559. def _is_compiled(dst, loc):
  560. present = True
  561. for name in loc['result']:
  562. if isinstance(name, tuple):
  563. present = present and bool([n for n in name if exists(os.path.join(dst, n))])
  564. else:
  565. present = present and exists(os.path.join(dst, name))
  566. return present
  567. def build_deplibs(config, basedir, **kwargs):
  568. mkdir_p(os.path.join(basedir, config))
  569. dstdir = os.path.join(basedir, config, 'deplibs')
  570. vars = {'destdir': dstdir }
  571. vars.update(kwargs)
  572. for lib in sorted(DEPENDENT_LIBS, key=lambda x: DEPENDENT_LIBS[x]['order']):
  573. cfg = None
  574. for key in DEPENDENT_LIBS[lib]['build']:
  575. if fnmatch.fnmatch(config, key):
  576. cfg = key
  577. if not cfg or _is_compiled(dstdir, DEPENDENT_LIBS[lib]['build'][cfg]):
  578. continue
  579. build_cfg = DEPENDENT_LIBS[lib]['build'][cfg]
  580. message('========== building: %s\n' % lib)
  581. srcdir = download_tarball(DEPENDENT_LIBS[lib]['url'], DEPENDENT_LIBS[lib]['sha1'],
  582. basedir, os.path.join(config, lib))
  583. for location, source, target in build_cfg.get('replace', []):
  584. data = open(os.path.join(srcdir, location), 'r').read()
  585. open(os.path.join(srcdir, location), 'w').write(data.replace(source, target % vars))
  586. os.chdir(srcdir)
  587. for command in build_cfg['commands']:
  588. shell(command % vars)
  589. if not type(build_cfg['result']) is list:
  590. for target in build_cfg['result']:
  591. mkdir_p(os.path.dirname(os.path.join(dstdir, target)))
  592. shutil.copy(build_cfg['result'][target], os.path.join(dstdir, target))
  593. os.chdir(dstdir)
  594. if not _is_compiled(dstdir, build_cfg):
  595. error("Unable to compile %s for your system, aborting." % lib)
  596. rmdir(srcdir)
  597. def build_qt(qtdir, make_cmd, configure_cmd):
  598. configured = ''
  599. if exists(os.path.join(qtdir, 'configured')):
  600. configured = open(os.path.join(qtdir, 'configured'), 'r').read()
  601. if not 'qt' in configured or not exists(qtdir):
  602. mkdir_p(qtdir)
  603. os.chdir(qtdir)
  604. shell(configure_cmd)
  605. open(os.path.join(qtdir, 'configured'), 'a').write('qt\n')
  606. os.chdir(qtdir)
  607. shell(make_cmd)
  608. def check_running_on_debian():
  609. if not sys.platform.startswith('linux') or not exists('/etc/apt/sources.list') or platform.architecture()[0] != '64bit':
  610. error('This can only be run on a 64-bit Debian/Ubuntu distribution, aborting.')
  611. if os.geteuid() != 0:
  612. error('This script must be run as root.')
  613. def install_packages(*names):
  614. inst = get_output('dpkg-query', '--show', '--showformat', '${Package}\n').split('\n')
  615. miss = [name for name in names if name not in inst]
  616. if miss:
  617. shell('apt-get update')
  618. shell('apt-get install --assume-yes %s' % (' '.join(miss)))
  619. def get_chroot_list():
  620. prefix = 'chroot:wkhtmltox-'
  621. result = {}
  622. for line in get_output('schroot', '--list').split('\n'):
  623. if not line.startswith(prefix):
  624. continue
  625. name = line[len(prefix):]
  626. for distro in LINUX_SCHROOT_SETUP:
  627. if 'chroot_alias' in LINUX_SCHROOT_SETUP[distro]:
  628. if name == LINUX_SCHROOT_SETUP[distro]['chroot_alias']:
  629. result[name] = distro
  630. continue
  631. for arch in LINUX_SCHROOT_SETUP[distro]['build_arch']:
  632. if name == '%s-%s' % (distro, arch):
  633. result[name] = distro
  634. return result
  635. # --------------------------------------------------------------- Linux chroot
  636. def check_setup_schroot(config):
  637. check_running_on_debian()
  638. login = os.environ.get('SUDO_USER') or get_output('logname')
  639. if not login or login == 'root':
  640. error('Unable to determine the login for which schroot access is to be given.')
  641. def build_setup_schroot(config, basedir):
  642. install_packages('git', 'debootstrap', 'schroot', 'rinse', 'debian-archive-keyring',
  643. 'ruby', 'ruby-dev', 'libffi-dev')
  644. if not get_output('which', 'fpm'):
  645. shell('gem install -V fpm -N')
  646. login = os.environ.get('SUDO_USER') or get_output('logname')
  647. target = config.split('-', 2)[2]
  648. distro = LINUX_SCHROOT_SETUP.get(target)
  649. allenv = get_chroot_list()
  650. for arch in distro['build_arch']:
  651. alias = distro.get('chroot_alias', '%s-%s' % (target, arch))
  652. base_dir = os.environ.get('WKHTMLTOX_CHROOT') or '/var/chroot'
  653. root_dir = os.path.join(base_dir, 'wkhtmltox-%s' % alias)
  654. pkg_list = '%s %s' % (distro['build_packages'], distro.get('runtime_packages', ''))
  655. chroot = (arch == 'i386' and 'linux32 chroot' or 'chroot')
  656. if alias in allenv:
  657. message('******************* %s (skipped)\n' % alias)
  658. continue
  659. message('******************* %s\n' % alias)
  660. os.system('umount %s/proc 2>/dev/null' % root_dir)
  661. os.system('umount %s/sys 2>/dev/null' % root_dir)
  662. rmdir(root_dir)
  663. mkdir_p(root_dir)
  664. def do_setup(*cmds):
  665. shell('mount -t proc proc %s/proc' % root_dir)
  666. shell('mount -t sysfs sysfs %s/sys' % root_dir)
  667. for cmd in cmds:
  668. shell('%s %s %s' % (chroot, root_dir, cmd))
  669. wrapper = distro.get('wrapper_command', '')
  670. shell('umount %s/proc' % root_dir)
  671. shell('umount %s/sys' % root_dir)
  672. if distro['packaging_tool'] == 'apt':
  673. cfg = distro['debootstrap']
  674. shell('debootstrap --arch=%s --variant=buildd %s %s %s' % (arch, cfg[0], root_dir, cfg[1]))
  675. open(os.path.join(root_dir, 'etc/apt/sources.list'), 'w').write(cfg[2])
  676. open(os.path.join(root_dir, 'usr/sbin/policy-rc.d'), 'w').write("#!/bin/bash\nexit 101\n")
  677. do_setup('chmod a+x /usr/sbin/policy-rc.d', # hack for Ubuntu Precise
  678. 'apt-get update',
  679. 'apt-get dist-upgrade --assume-yes',
  680. 'apt-get install --assume-yes %s' % pkg_list)
  681. elif distro['packaging_tool'] == 'yum':
  682. rinse = (arch == 'i386' and 'linux32 rinse' or 'rinse')
  683. shell('%s --arch %s --distribution %s --directory %s' % (rinse, arch, distro['rinse'], root_dir))
  684. if arch == 'amd64':
  685. open(os.path.join(root_dir, 'etc/yum.conf'), 'a').write('exclude = *.i?86\n')
  686. for repo_url in distro.get('extra_repos', []):
  687. location = os.path.join(root_dir, 'etc/yum.repos.d', repo_url.split('/')[-1])
  688. urllib.urlretrieve(repo_url, location)
  689. do_setup('yum clean all', 'yum update -y', 'yum install -y %s' % pkg_list)
  690. open('/etc/schroot/chroot.d/wkhtmltox', 'a').write("""
  691. [wkhtmltox-%(alias)s]
  692. type=directory
  693. directory=%(dir)s
  694. description=%(title)s for wkhtmltox
  695. users=%(login)s
  696. root-users=root
  697. %(personality)s""" % { 'alias': alias, 'dir': root_dir, 'title': distro['title'], 'login': login,
  698. 'personality': (arch == 'i386' and 'personality=linux32\n' or '\n') })
  699. def check_update_schroot(config):
  700. check_running_on_debian()
  701. if not get_chroot_list():
  702. error('Unable to determine the list of available schroots.')
  703. def build_update_schroot(config, basedir):
  704. for name, distro in get_chroot_list().iteritems():
  705. message('******************* %s\n' % name)
  706. tool = LINUX_SCHROOT_SETUP[distro]['packaging_tool']
  707. if tool == 'apt':
  708. chroot_shell(name, 'apt-get update')
  709. chroot_shell(name, 'apt-get dist-upgrade --assume-yes')
  710. elif tool == 'yum':
  711. chroot_shell(name, 'yum update -y')
  712. def check_setup_mingw_w64(config):
  713. check_running_on_debian()
  714. def build_setup_mingw_w64(config, basedir):
  715. install_packages('build-essential', 'mingw-w64', 'nsis', 'perl', 'git', 'ruby', 'gperf', 'bison', 'flex')
  716. def check_source_tarball(config):
  717. if not get_output('git', 'rev-parse', '--short', 'HEAD'):
  718. error("This can only be run inside a git checkout.")
  719. if not exists(os.path.join(os.getcwd(), 'qt', '.git')):
  720. error("Please initialize and download the Qt submodule before running this.")
  721. def _filter_tar(info):
  722. name = info.name[1+info.name.index('/'):]
  723. if name.endswith('.git') or [p for p in EXCLUDE_SRC_TARBALL if fnmatch.fnmatch(name, p)]:
  724. return None
  725. info.uid = info.gid = 1000
  726. info.uname = info.gname = 'wkhtmltopdf'
  727. return info
  728. def build_source_tarball(config, basedir):
  729. version, simple_version = get_version(basedir)
  730. root_dir = os.path.realpath(os.path.join(basedir, '..'))
  731. os.chdir(os.path.join(root_dir, 'qt'))
  732. shell('git clean -fdx')
  733. shell('git reset --hard HEAD')
  734. os.chdir(root_dir)
  735. shell('git clean -fdx')
  736. shell('git reset --hard HEAD')
  737. shell('git submodule update')
  738. open('VERSION', 'w').write(version)
  739. with tarfile.open('wkhtmltox-%s.tar.bz2' % version, 'w:bz2') as tar:
  740. tar.add('.', 'wkhtmltox-%s/' % version, filter=_filter_tar)
  741. shell('git reset --hard HEAD')
  742. # --------------------------------------------------------------- MSVC (2013 only)
  743. MSVC_LOCATION = {
  744. 'msvc2013': 'VS120COMNTOOLS'
  745. }
  746. MSVC_RUNTIME = {
  747. 'msvc2013-win32': ('df7f0a73bfa077e483e51bfb97f5e2eceedfb6a3', 'http://download.microsoft.com/download/2/E/6/2E61CFA4-993B-4DD4-91DA-3737CD5CD6E3/vcredist_x86.exe'),
  748. 'msvc2013-win64': ('8bf41ba9eef02d30635a10433817dbb6886da5a2', 'http://download.microsoft.com/download/2/E/6/2E61CFA4-993B-4DD4-91DA-3737CD5CD6E3/vcredist_x64.exe')
  749. }
  750. def check_msvc(config):
  751. version, arch = rchop(config, '-dbg').split('-')
  752. env_var = MSVC_LOCATION[version]
  753. if not env_var in os.environ:
  754. error("%s does not seem to be installed." % version)
  755. vcdir = os.path.join(os.environ[env_var], '..', '..', 'VC')
  756. if not exists(os.path.join(vcdir, 'vcvarsall.bat')):
  757. error("%s: unable to find vcvarsall.bat" % version)
  758. if arch == 'win32' and not exists(os.path.join(vcdir, 'bin', 'cl.exe')):
  759. error("%s: unable to find the x86 compiler" % version)
  760. if arch == 'win64' and not exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')) \
  761. and not exists(os.path.join(vcdir, 'bin', 'x86_amd64', 'cl.exe')):
  762. error("%s: unable to find the amd64 compiler" % version)
  763. perl = get_output('perl', '-V')
  764. if not perl or 'perl5' not in perl:
  765. error("perl does not seem to be installed.")
  766. nsis = get_registry_value(r'SOFTWARE\NSIS')
  767. if not nsis or not exists(os.path.join(nsis, 'makensis.exe')):
  768. error("NSIS does not seem to be installed.")
  769. def build_msvc(config, basedir):
  770. msvc, arch = rchop(config, '-dbg').split('-')
  771. vcdir = os.path.join(os.environ[MSVC_LOCATION[msvc]], '..', '..', 'VC')
  772. vcarg = 'x86'
  773. if arch == 'win64':
  774. if exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')):
  775. vcarg = 'amd64'
  776. else:
  777. vcarg = 'x86_amd64'
  778. python = sys.executable
  779. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os, sys; sys.stdout.write(repr(dict(os.environ)))"' % (
  780. os.path.join(vcdir, 'vcvarsall.bat'), vcarg, python), stdout=subprocess.PIPE, shell=True)
  781. stdout, _ = process.communicate()
  782. exitcode = process.wait()
  783. if exitcode != 0:
  784. error("%s: unable to initialize the environment" % msvc)
  785. os.environ.update(eval(stdout.strip()))
  786. version, simple_version = get_version(basedir)
  787. cflags = config.endswith('-dbg') and '/MDd /Zi' or '/MD'
  788. build_deplibs(config, basedir, cflags=cflags)
  789. sha1, url = MSVC_RUNTIME[rchop(config, '-dbg')]
  790. shutil.copy(download_file(url, sha1, basedir), os.path.join(basedir, config, 'vcredist.exe'))
  791. libdir = os.path.join(basedir, config, 'deplibs')
  792. qtdir = os.path.join(basedir, config, 'qt')
  793. mkdir_p(qtdir)
  794. configure_args = qt_config('msvc',
  795. '-I %s\\include' % libdir,
  796. '-L %s\\lib' % libdir,
  797. 'OPENSSL_LIBS="-L%s\\\\lib -lssleay32 -llibeay32 -lUser32 -lAdvapi32 -lGdi32 -lCrypt32"' % libdir.replace('\\', '\\\\'))
  798. build_qt(qtdir, 'nmake', '%s\\..\\qt\\configure.exe %s' % (basedir, configure_args))
  799. appdir = os.path.join(basedir, config, 'app')
  800. mkdir_p(appdir)
  801. os.chdir(appdir)
  802. rmdir('bin')
  803. mkdir_p('bin')
  804. os.environ['WKHTMLTOX_VERSION'] = version
  805. shell('%s\\bin\\qmake %s\\..\\wkhtmltopdf.pro' % (qtdir, basedir))
  806. shell('nmake')
  807. makensis = os.path.join(get_registry_value(r'SOFTWARE\NSIS'), 'makensis.exe')
  808. os.chdir(os.path.join(basedir, '..'))
  809. shell('"%s" /DVERSION=%s /DSIMPLE_VERSION=%s /DTARGET=%s /DMSVC /DARCH=%s wkhtmltox.nsi' % \
  810. (makensis, version, nsis_version(simple_version), config, arch))
  811. # ------------------------------------------------ MinGW-W64 Cross Environment
  812. MINGW_W64_PREFIX = {
  813. 'mingw-w64-cross-win32' : 'i686-w64-mingw32',
  814. 'mingw-w64-cross-win64' : 'x86_64-w64-mingw32',
  815. }
  816. def check_mingw64_cross(config):
  817. shell('%s-gcc --version' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  818. def build_mingw64_cross(config, basedir):
  819. version, simple_version = get_version(basedir)
  820. build_deplibs(config, basedir, mingw_w64=MINGW_W64_PREFIX.get(rchop(config, '-dbg')))
  821. libdir = os.path.join(basedir, config, 'deplibs')
  822. qtdir = os.path.join(basedir, config, 'qt')
  823. configure_args = qt_config('mingw-w64-cross',
  824. '--prefix=%s' % qtdir,
  825. '-I%s/include' % libdir,
  826. '-L%s/lib' % libdir,
  827. '-device-option CROSS_COMPILE=%s-' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  828. os.environ['OPENSSL_LIBS'] = '-lssl -lcrypto -L%s/lib -lws2_32 -lgdi32 -lcrypt32' % libdir
  829. mkdir_p(qtdir)
  830. os.chdir(qtdir)
  831. build_qt(qtdir, 'make -j%d' % CPU_COUNT,
  832. '%s/../qt/configure %s' % (basedir, configure_args))
  833. appdir = os.path.join(basedir, config, 'app')
  834. mkdir_p(appdir)
  835. os.chdir(appdir)
  836. shell('rm -f bin/*')
  837. # set up cross compiling prefix correctly
  838. os.environ['WKHTMLTOX_VERSION'] = version
  839. shell('%s/bin/qmake -set CROSS_COMPILE %s-' % (qtdir, MINGW_W64_PREFIX[rchop(config, '-dbg')]))
  840. shell('%s/bin/qmake -spec win32-g++-4.6 %s/../wkhtmltopdf.pro' % (qtdir, basedir))
  841. shell('make')
  842. shutil.copy('bin/libwkhtmltox0.a', 'bin/wkhtmltox.lib')
  843. shell('rm -f bin/lib*.dll')
  844. for dll in ['libgcc_s_sjlj-1.dll', 'libgcc_s_seh-1.dll', 'libstdc++-6.dll']:
  845. dll_path = get_output('dpkg', '-S', dll)
  846. if dll_path:
  847. for line in dll_path.split('\n'):
  848. loc = line[1+line.index(':'):].strip()
  849. if exists(loc) and MINGW_W64_PREFIX[rchop(config, '-dbg')] in loc and '-posix' not in loc:
  850. shell('cp %s bin/' % loc)
  851. os.chdir(os.path.join(basedir, '..'))
  852. shell('makensis -DVERSION=%s -DSIMPLE_VERSION=%s -DTARGET=%s -DMINGW -DARCH=%s wkhtmltox.nsi' % \
  853. (version, nsis_version(simple_version), config, rchop(config, '-dbg').split('-')[-1]))
  854. # -------------------------------------------------- Linux schroot environment
  855. def check_linux_schroot(config):
  856. chroot_shell(rchop(config, '-dbg'), 'gcc --version')
  857. def build_linux_schroot(config, basedir):
  858. os.chdir(os.path.realpath(os.path.join(basedir, '..')))
  859. chroot_shell(rchop(config, '-dbg'), 'python scripts/build.py %s -chroot-build' % ' '.join(sys.argv[1:]))
  860. version, simple_version = get_version(basedir)
  861. os.chdir(os.path.join(basedir, config))
  862. shell('fpm %s' % fpm_params(config, version))
  863. def chroot_build_linux_schroot(config, basedir):
  864. version, simple_version = get_version(basedir)
  865. qtdir = os.path.join(basedir, config, 'qt')
  866. mkdir_p(qtdir)
  867. build_qt(qtdir, 'make -j%d' % CPU_COUNT,
  868. '%s/../qt/configure %s' % (basedir, qt_config('posix', '--prefix=%s' % qtdir)))
  869. app = os.path.join(basedir, config, 'app')
  870. dist = os.path.join(basedir, config, 'dist')
  871. mkdir_p(app)
  872. mkdir_p(dist)
  873. os.chdir(app)
  874. shell('rm -f bin/*')
  875. os.environ['WKHTMLTOX_VERSION'] = version
  876. os.environ['XZ_OPT'] = '-9'
  877. shell('%s/bin/qmake %s/../wkhtmltopdf.pro' % (qtdir, basedir))
  878. shell('make install INSTALL_ROOT=%s' % dist)
  879. # -------------------------------------------------- POSIX local environment
  880. def check_posix_local(config):
  881. pass
  882. def build_posix_local(config, basedir):
  883. version, simple_version = get_version(basedir)
  884. app = os.path.join(basedir, config, 'app')
  885. qt = os.path.join(basedir, config, 'qt')
  886. dist = os.path.join(basedir, config, 'wkhtmltox-%s' % version)
  887. make = get_output('which gmake') and 'gmake' or 'make'
  888. mkdir_p(qt)
  889. mkdir_p(app)
  890. rmdir(dist)
  891. mkdir_p(os.path.join(dist, 'bin'))
  892. mkdir_p(os.path.join(dist, 'include', 'wkhtmltox'))
  893. mkdir_p(os.path.join(dist, 'lib'))
  894. build_qt(qtdir, 'make -j%d' % CPU_COUNT,
  895. '%s/../qt/configure %s' % (basedir, qt_config('posix', '--prefix=%s' % qt)))
  896. os.chdir(app)
  897. shell('rm -f bin/*')
  898. os.environ['WKHTMLTOX_VERSION'] = version
  899. shell('../qt/bin/qmake ../../../wkhtmltopdf.pro')
  900. shell('%s -j%d' % (make, CPU_COUNT))
  901. shell('cp bin/wkhtmlto* ../wkhtmltox-%s/bin' % version)
  902. shell('cp -P bin/libwkhtmltox*.so.* ../wkhtmltox-%s/lib' % version)
  903. shell('cp ../../../include/wkhtmltox/*.h ../wkhtmltox-%s/include/wkhtmltox' % version)
  904. shell('cp ../../../include/wkhtmltox/dll*.inc ../wkhtmltox-%s/include/wkhtmltox' % version)
  905. os.chdir(os.path.join(basedir, config))
  906. shell('tar -c -v -f ../wkhtmltox-%s_local-%s.tar wkhtmltox-%s/' % (version, platform.node(), version))
  907. shell('xz --compress --force --verbose -9 ../wkhtmltox-%s_local-%s.tar' % (version, platform.node()))
  908. # --------------------------------------------------------------- OS X
  909. OSXPKG_IDENTIFIER = 'org.wkhtmltopdf'
  910. OSXPKG_PREFIX = '/usr/local/share/wkhtmltox-installer'
  911. def check_osx(config):
  912. if not platform.system() == 'Darwin' or not platform.mac_ver()[0]:
  913. error('This can only be run on a OS X system!')
  914. if not get_output('xcode-select', '--print-path'):
  915. error('Xcode is not installed, aborting.')
  916. if not get_output('which', 'fpm'):
  917. error('Please install fpm by running "sudo gem install fpm --no-ri --no-rdoc"')
  918. def build_osx(config, basedir):
  919. version, simple_version = get_version(basedir)
  920. build_deplibs(config, basedir)
  921. osxver = platform.mac_ver()[0][:platform.mac_ver()[0].rindex('.')]
  922. framework = config.split('-')[1]
  923. if osxver == '10.6':
  924. osxcfg = '-%s -platform macx-g++42' % framework
  925. else:
  926. osxcfg = '-%s -platform unsupported/macx-clang' % framework
  927. flags = ''
  928. if framework == 'carbon' and osxver != '10.6':
  929. for item in ['CFLAGS', 'CXXFLAGS']:
  930. flags += '"QMAKE_%s += %s" ' % (item, '-fvisibility=hidden -fvisibility-inlines-hidden')
  931. def get_dir(name):
  932. return os.path.join(basedir, config, name)
  933. configure_args = qt_config('osx', osxcfg,
  934. '--prefix=%s' % get_dir('qt'),
  935. '-I %s/include' % get_dir('deplibs'),
  936. '-L %s/lib' % get_dir('deplibs'))
  937. rmdir(get_dir('dist'))
  938. rmdir(get_dir('pkg'))
  939. mkdir_p(get_dir('qt'))
  940. mkdir_p(get_dir('app'))
  941. mkdir_p(get_dir('pkg'))
  942. build_qt(get_dir('qt'), 'make -j%d' % CPU_COUNT, '../../../qt/configure %s' % configure_args)
  943. os.chdir(get_dir('app'))
  944. shell('rm -f bin/*')
  945. os.environ['WKHTMLTOX_VERSION'] = version
  946. shell('../qt/bin/qmake %s ../../../wkhtmltopdf.pro' % flags)
  947. shell('make -j%d' % CPU_COUNT)
  948. if osxver not in ['10.6', '10.7']:
  949. for item in ['wkhtmltoimage', 'wkhtmltopdf', 'libwkhtmltox.%s.dylib' % simple_version]:
  950. shell(' '.join([
  951. 'install_name_tool', '-change',
  952. '/System/Library/Frameworks/CoreText.framework/Versions/A/CoreText',
  953. '/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreText.framework/CoreText',
  954. 'bin/'+item]))
  955. shell('make install INSTALL_ROOT=%s' % get_dir('dist'))
  956. def _osx_tar(info):
  957. info.uid = info.gid = 0
  958. info.uname = 'root'
  959. info.gname = 'wheel'
  960. return info
  961. # create tarball for application and copy xz
  962. os.chdir(get_dir('dist'))
  963. with tarfile.open('../pkg/app.tar', 'w') as tar:
  964. tar.add('.', './', filter=_osx_tar)
  965. xz = os.path.join(get_dir('deplibs'), 'bin', 'xz')
  966. shell('%s --compress --force --verbose -9 ../pkg/app.tar' % xz)
  967. shutil.copy(xz, '../pkg/')
  968. with open('../pkg/uninstall-wkhtmltox', 'w') as f:
  969. os.chmod('../pkg/uninstall-wkhtmltox', 0o755)
  970. f.write("""#!/bin/bash
  971. if [ "$(id -u)" != "0" ]; then
  972. echo "This script must be run as sudo uninstall-wkhtmltox" 1>&2
  973. exit 1
  974. fi
  975. cd /usr/local
  976. if which pkgutil >/dev/null; then
  977. pkgutil --forget %s.%s
  978. fi
  979. """ % (OSXPKG_IDENTIFIER, PROJECT_SETUP['name']))
  980. for root, dirs, files in os.walk(get_dir('dist')):
  981. for file in files:
  982. f.write('echo REMOVE /usr/local/%(name)s && rm -f %(name)s\n' % { 'name': os.path.relpath(os.path.join(root, file)) })
  983. f.write('echo REMOVE /usr/local/include/wkhtmltox && rm -df /usr/local/include/wkhtmltox\n')
  984. f.write('echo REMOVE /usr/local/bin/uninstall-wkhtmltox && rm -f /usr/local/bin/uninstall-wkhtmltox')
  985. open('../extract.sh', 'w').write("""#!/bin/bash
  986. TGTDIR=/usr/local
  987. BASEDIR=%s
  988. cd $BASEDIR
  989. ./xz --decompress app.tar.xz
  990. cd $TGTDIR
  991. tar oxf $BASEDIR/app.tar
  992. mv $BASEDIR/uninstall-wkhtmltox $TGTDIR/bin
  993. rm -fr $BASEDIR
  994. """ % OSXPKG_PREFIX)
  995. os.chdir(os.path.join(basedir, config))
  996. fpm_args = '--force --prefix %s --category utils -s dir -C pkg' % OSXPKG_PREFIX
  997. for key in PROJECT_SETUP:
  998. fpm_args += ' --%s "%s"' % (key, PROJECT_SETUP[key])
  999. fpm_args += ' --osxpkg-identifier-prefix "%s" --version "%s"' % (OSXPKG_IDENTIFIER, version)
  1000. fpm_args += ' -t osxpkg --package ../%s-%s_%s.pkg' % (PROJECT_SETUP['name'], version, config)
  1001. fpm_args += ' --after-install extract.sh .'
  1002. shell('fpm %s' % fpm_args)
  1003. # --------------------------------------------------------------- command line
  1004. def usage(exit_code=2):
  1005. message("Usage: scripts/build.py <target> [-clean] [-debug]\n\nThe supported targets are:\n")
  1006. opts = list(BUILDERS.keys())
  1007. opts.sort()
  1008. for opt in opts:
  1009. message('* %s\n' % opt)
  1010. sys.exit(exit_code)
  1011. def main():
  1012. rootdir = os.path.realpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
  1013. basedir = os.path.join(rootdir, 'static-build')
  1014. if len(sys.argv) == 1:
  1015. usage(0)
  1016. config = sys.argv[1]
  1017. if config not in BUILDERS:
  1018. usage()
  1019. for arg in sys.argv[2:]:
  1020. if not arg in ['-clean', '-debug', '-chroot-build']:
  1021. usage()
  1022. final_config = config
  1023. if '-debug' in sys.argv[2:]:
  1024. final_config += '-dbg'
  1025. QT_CONFIG['common'].extend(['remove:-release', 'remove:-webkit', '-debug', '-webkit-debug'])
  1026. if '-clean' in sys.argv[2:]:
  1027. rmdir(os.path.join(basedir, final_config))
  1028. os.chdir(rootdir)
  1029. if '-chroot-build' in sys.argv[2:]:
  1030. globals()['chroot_build_%s' % BUILDERS[config]](final_config, basedir)
  1031. return
  1032. globals()['check_%s' % BUILDERS[config]](final_config)
  1033. globals()['build_%s' % BUILDERS[config]](final_config, basedir)
  1034. if __name__ == '__main__':
  1035. main()