build.py 43 KB

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