build.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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-linked', # directly link to OpenSSL during compilation
  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.2k.tar.gz',
  175. 'sha1' : '5f26a624479c51847ebd2f22bb9f84b3b44dcb44',
  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.11.tar.gz',
  205. 'sha1' : 'e6d119755acdf9104d7ba236b1242696940ed6dd',
  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.28.tar.gz',
  230. 'sha1': '004556d65f21baed83755f8e094112711e39ebae',
  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://downloads.sourceforge.net/lzmautils/xz-5.2.3.tar.gz',
  329. 'sha1': '529638eec3597e429cc54c74551ac0a89169e841',
  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 silent_shell(cmd):
  395. message(' %s\n' % cmd)
  396. try:
  397. subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
  398. except subprocess.CalledProcessError as e:
  399. error("\n%s\ncommand failed: exit code %d" % (e.output.decode('utf-8'), e.returncode))
  400. def chroot_shell(name, cmd):
  401. distro = get_chroot_list().get(name)
  402. wrapper = LINUX_SCHROOT_SETUP.get(distro, {}).get('wrapper_command', '')
  403. ret = os.system('schroot -c wkhtmltox-%s -- %s%s ' % (name, wrapper, cmd))
  404. if ret != 0:
  405. error("command inside chroot failed: exit code %d" % ret)
  406. def get_output(*cmd):
  407. try:
  408. return subprocess.check_output(cmd, stderr=subprocess.STDOUT).strip()
  409. except:
  410. return None
  411. def rmdir(path):
  412. if exists(path):
  413. if platform.system() == 'Windows':
  414. shell('attrib -R %s\* /S' % path)
  415. shutil.rmtree(path)
  416. def mkdir_p(path):
  417. if not exists(path):
  418. os.makedirs(path)
  419. def get_registry_value(key, value=None):
  420. for mask in [0, winreg.KEY_WOW64_64KEY, winreg.KEY_WOW64_32KEY]:
  421. try:
  422. reg_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key, 0, winreg.KEY_READ | mask)
  423. return winreg.QueryValueEx(reg_key, value)[0]
  424. except WindowsError:
  425. pass
  426. return None
  427. def get_version(basedir):
  428. mkdir_p(basedir)
  429. text = open(os.path.join(basedir, '..', 'VERSION'), 'r').read().strip()
  430. if '-' not in text:
  431. return (text, text)
  432. version = text[:text.index('-')]
  433. os.chdir(os.path.join(basedir, '..'))
  434. hash = get_output('git', 'rev-parse', '--short', 'HEAD')
  435. if not hash:
  436. return (text, version)
  437. return ('%s-%s' % (text, hash), version)
  438. def nsis_version(ver):
  439. while ver.count('.') < 3:
  440. ver += '.0'
  441. return ver
  442. def qt_config(key, *opts):
  443. input, output = [], []
  444. input.extend(QT_CONFIG['common'])
  445. input.extend(QT_CONFIG[key])
  446. input.extend(opts)
  447. cfg = os.environ.get('WKHTMLTOX_QT_CONFIG')
  448. if cfg:
  449. input.extend(cfg.split())
  450. for arg in input:
  451. if not arg.startswith('remove:-'):
  452. output.append(arg)
  453. elif arg[1+arg.index(':'):] in output:
  454. output.remove(arg[1+arg.index(':'):])
  455. return ' '.join(output)
  456. def download_file(url, sha1, dir):
  457. name = url.split('/')[-1]
  458. loc = os.path.join(dir, name)
  459. if os.path.exists(loc):
  460. hash = hashlib.sha1(open(loc, 'rb').read()).hexdigest()
  461. if hash == sha1:
  462. return loc
  463. os.remove(loc)
  464. message('Checksum mismatch for %s, re-downloading.\n' % name)
  465. def hook(cnt, bs, total):
  466. pct = int(cnt*bs*100/total)
  467. message("\rDownloading: %s [%d%%]" % (name, pct))
  468. urllib.urlretrieve(url, loc, reporthook=hook)
  469. message("\r")
  470. hash = hashlib.sha1(open(loc, 'rb').read()).hexdigest()
  471. if hash != sha1:
  472. os.remove(loc)
  473. error('Checksum mismatch for %s, aborting.' % name)
  474. message("\rDownloaded: %s [checksum OK]\n" % name)
  475. return loc
  476. def download_tarball(url, sha1, dir, name):
  477. loc = download_file(url, sha1, dir)
  478. tar = tarfile.open(loc)
  479. sub = tar.getnames()[0]
  480. if '/' in sub:
  481. sub = sub[:sub.index('/')]
  482. src = os.path.join(dir, sub)
  483. tgt = os.path.join(dir, name)
  484. rmdir(src)
  485. tar.extractall(dir)
  486. rmdir(tgt)
  487. os.rename(src, tgt)
  488. return tgt
  489. def _is_compiled(dst, loc):
  490. present = True
  491. for name in loc['result']:
  492. if isinstance(name, tuple):
  493. present = present and bool([n for n in name if exists(os.path.join(dst, n))])
  494. else:
  495. present = present and exists(os.path.join(dst, name))
  496. return present
  497. def build_deplibs(config, basedir, **kwargs):
  498. mkdir_p(os.path.join(basedir, config))
  499. dstdir = os.path.join(basedir, config, 'deplibs')
  500. vars = {'destdir': dstdir }
  501. vars.update(kwargs)
  502. for lib in sorted(DEPENDENT_LIBS, key=lambda x: DEPENDENT_LIBS[x]['order']):
  503. cfg = None
  504. for key in DEPENDENT_LIBS[lib]['build']:
  505. if fnmatch.fnmatch(config, key):
  506. cfg = key
  507. if not cfg or _is_compiled(dstdir, DEPENDENT_LIBS[lib]['build'][cfg]):
  508. continue
  509. build_cfg = DEPENDENT_LIBS[lib]['build'][cfg]
  510. message('========== building: %s\n' % lib)
  511. srcdir = download_tarball(DEPENDENT_LIBS[lib]['url'], DEPENDENT_LIBS[lib]['sha1'],
  512. basedir, os.path.join(config, lib))
  513. for location, source, target in build_cfg.get('replace', []):
  514. data = open(os.path.join(srcdir, location), 'r').read()
  515. open(os.path.join(srcdir, location), 'w').write(data.replace(source, target % vars))
  516. os.chdir(srcdir)
  517. for command in build_cfg['commands']:
  518. silent_shell(command % vars)
  519. if not type(build_cfg['result']) is list:
  520. for target in build_cfg['result']:
  521. mkdir_p(os.path.dirname(os.path.join(dstdir, target)))
  522. shutil.copy(build_cfg['result'][target], os.path.join(dstdir, target))
  523. os.chdir(dstdir)
  524. if not _is_compiled(dstdir, build_cfg):
  525. error("Unable to compile %s for your system, aborting." % lib)
  526. rmdir(srcdir)
  527. def build_qt(qtdir, make_cmd, configure_cmd):
  528. configured = ''
  529. if exists(os.path.join(qtdir, 'configured')):
  530. configured = open(os.path.join(qtdir, 'configured'), 'r').read()
  531. if not 'qt' in configured or not exists(qtdir):
  532. mkdir_p(qtdir)
  533. os.chdir(qtdir)
  534. shell(configure_cmd)
  535. open(os.path.join(qtdir, 'configured'), 'a').write('qt\n')
  536. os.chdir(qtdir)
  537. shell(make_cmd)
  538. def check_running_on_debian():
  539. if not sys.platform.startswith('linux') or not exists('/etc/apt/sources.list') or platform.architecture()[0] != '64bit':
  540. error('This can only be run on a 64-bit Debian/Ubuntu distribution, aborting.')
  541. if os.geteuid() != 0:
  542. error('This script must be run as root.')
  543. def install_packages(*names):
  544. inst = get_output('dpkg-query', '--show', '--showformat', '${Package}\n').split('\n')
  545. miss = [name for name in names if name not in inst]
  546. if miss:
  547. shell('apt-get update')
  548. shell('apt-get install --assume-yes %s' % (' '.join(miss)))
  549. def get_chroot_list():
  550. prefix = 'chroot:wkhtmltox-'
  551. result = {}
  552. for line in get_output('schroot', '--list').split('\n'):
  553. if not line.startswith(prefix):
  554. continue
  555. name = line[len(prefix):]
  556. for distro in LINUX_SCHROOT_SETUP:
  557. if 'chroot_alias' in LINUX_SCHROOT_SETUP[distro]:
  558. if name == LINUX_SCHROOT_SETUP[distro]['chroot_alias']:
  559. result[name] = distro
  560. continue
  561. for arch in LINUX_SCHROOT_SETUP[distro]['build_arch']:
  562. if name == '%s-%s' % (distro, arch):
  563. result[name] = distro
  564. return result
  565. # --------------------------------------------------------------- Linux chroot
  566. def check_setup_schroot(config):
  567. check_running_on_debian()
  568. login = os.environ.get('SUDO_USER') or get_output('logname')
  569. if not login or login == 'root':
  570. error('Unable to determine the login for which schroot access is to be given.')
  571. def build_setup_schroot(config, basedir):
  572. install_packages('git', 'debootstrap', 'schroot', 'rinse', 'debian-archive-keyring',
  573. 'build-essential', 'tar', 'xz-utils')
  574. login = os.environ.get('SUDO_USER') or get_output('logname')
  575. target = config.split('-', 2)[2]
  576. distro = LINUX_SCHROOT_SETUP.get(target)
  577. allenv = get_chroot_list()
  578. for arch in distro['build_arch']:
  579. alias = distro.get('chroot_alias', '%s-%s' % (target, arch))
  580. base_dir = os.environ.get('WKHTMLTOX_CHROOT') or '/var/chroot'
  581. root_dir = os.path.join(base_dir, 'wkhtmltox-%s' % alias)
  582. pkg_list = '%s %s' % (distro['build_packages'], distro.get('runtime_packages', ''))
  583. chroot = (arch == 'i386' and 'linux32 chroot' or 'chroot')
  584. if alias in allenv:
  585. message('******************* %s (skipped)\n' % alias)
  586. continue
  587. message('******************* %s\n' % alias)
  588. os.system('umount %s/proc 2>/dev/null' % root_dir)
  589. os.system('umount %s/sys 2>/dev/null' % root_dir)
  590. rmdir(root_dir)
  591. mkdir_p(root_dir)
  592. def do_setup(*cmds):
  593. shell('mount -t proc proc %s/proc' % root_dir)
  594. shell('mount -t sysfs sysfs %s/sys' % root_dir)
  595. for cmd in cmds:
  596. shell('%s %s %s' % (chroot, root_dir, cmd))
  597. shell('umount %s/proc' % root_dir)
  598. shell('umount %s/sys' % root_dir)
  599. if distro['packaging_tool'] == 'yum':
  600. rinse = (arch == 'i386' and 'linux32 rinse' or 'rinse')
  601. rinse_distro, extra_repos = distro['rinse']
  602. shell('%s --arch %s --distribution %s --directory %s' % (rinse, arch, rinse_distro, root_dir))
  603. if arch == 'amd64':
  604. open(os.path.join(root_dir, 'etc/yum.conf'), 'a').write('exclude = *.i?86\n')
  605. if extra_repos:
  606. open(os.path.join(root_dir, 'etc/yum.repos.d/wkhtmltopdf.repo'), 'w').write(extra_repos)
  607. do_setup('yum clean all', 'yum update -y', 'yum install -y %s' % pkg_list)
  608. open('/etc/schroot/chroot.d/wkhtmltox', 'a').write("""
  609. [wkhtmltox-%(alias)s]
  610. type=directory
  611. directory=%(dir)s
  612. description=%(title)s for wkhtmltox
  613. users=%(login)s
  614. root-users=root
  615. %(personality)s""" % { 'alias': alias, 'dir': root_dir, 'title': distro['title'], 'login': login,
  616. 'personality': (arch == 'i386' and 'personality=linux32\n' or '\n') })
  617. def check_update_schroot(config):
  618. check_running_on_debian()
  619. if not get_chroot_list():
  620. error('Unable to determine the list of available schroots.')
  621. def build_update_schroot(config, basedir):
  622. for name, distro in get_chroot_list().iteritems():
  623. message('******************* %s\n' % name)
  624. tool = LINUX_SCHROOT_SETUP[distro]['packaging_tool']
  625. if tool == 'apt':
  626. chroot_shell(name, 'apt-get update')
  627. chroot_shell(name, 'apt-get dist-upgrade --assume-yes')
  628. elif tool == 'yum':
  629. chroot_shell(name, 'yum update -y')
  630. def check_setup_mingw_w64(config):
  631. check_running_on_debian()
  632. def build_setup_mingw_w64(config, basedir):
  633. install_packages('build-essential', 'mingw-w64', 'nsis', 'perl', 'git', 'ruby', 'gperf', 'bison', 'flex')
  634. def check_source_tarball(config):
  635. if not get_output('git', 'rev-parse', '--short', 'HEAD'):
  636. error("This can only be run inside a git checkout.")
  637. if not exists(os.path.join(os.getcwd(), 'qt', '.git')):
  638. error("Please initialize and download the Qt submodule before running this.")
  639. def _filter_tar(info):
  640. name = info.name[1+info.name.index('/'):]
  641. if name.endswith('.git') or [p for p in EXCLUDE_SRC_TARBALL if fnmatch.fnmatch(name, p)]:
  642. return None
  643. info.uid = info.gid = 1000
  644. info.uname = info.gname = 'wkhtmltopdf'
  645. return info
  646. def build_source_tarball(config, basedir):
  647. version, simple_version = get_version(basedir)
  648. root_dir = os.path.realpath(os.path.join(basedir, '..'))
  649. os.chdir(os.path.join(root_dir, 'qt'))
  650. shell('git clean -fdx')
  651. shell('git reset --hard HEAD')
  652. os.chdir(root_dir)
  653. shell('git clean -fdx')
  654. shell('git reset --hard HEAD')
  655. shell('git submodule update')
  656. open('VERSION', 'w').write(version)
  657. with tarfile.open('wkhtmltox-%s.tar.bz2' % version, 'w:bz2') as tar:
  658. tar.add('.', 'wkhtmltox-%s/' % version, filter=_filter_tar)
  659. shell('git reset --hard HEAD')
  660. # --------------------------------------------------------------- MSVC (2013 only)
  661. MSVC_LOCATION = {
  662. 'msvc2013': ('VS120COMNTOOLS', '12.0'),
  663. 'msvc2015': ('VS140COMNTOOLS', '14.0')
  664. }
  665. MSVC_RUNTIME = {
  666. 'msvc2013-win32': ('df7f0a73bfa077e483e51bfb97f5e2eceedfb6a3', 'http://download.microsoft.com/download/2/E/6/2E61CFA4-993B-4DD4-91DA-3737CD5CD6E3/vcredist_x86.exe'),
  667. 'msvc2013-win64': ('8bf41ba9eef02d30635a10433817dbb6886da5a2', 'http://download.microsoft.com/download/2/E/6/2E61CFA4-993B-4DD4-91DA-3737CD5CD6E3/vcredist_x64.exe'),
  668. 'msvc2015-win32': ('bfb74e498c44d3a103ca3aa2831763fb417134d1', 'https://download.microsoft.com/download/9/3/F/93FCF1E7-E6A4-478B-96E7-D4B285925B00/vc_redist.x86.exe'),
  669. 'msvc2015-win64': ('3155cb0f146b927fcc30647c1a904cd162548c8c', 'https://download.microsoft.com/download/9/3/F/93FCF1E7-E6A4-478B-96E7-D4B285925B00/vc_redist.x64.exe')
  670. }
  671. def check_msvc(config):
  672. version, arch = rchop(config, '-dbg').split('-')
  673. env_var, _ = MSVC_LOCATION[version]
  674. if not env_var in os.environ:
  675. error("%s does not seem to be installed." % version)
  676. vcdir = os.path.join(os.environ[env_var], '..', '..', 'VC')
  677. if not exists(os.path.join(vcdir, 'vcvarsall.bat')):
  678. error("%s: unable to find vcvarsall.bat" % version)
  679. if arch == 'win32' and not exists(os.path.join(vcdir, 'bin', 'cl.exe')):
  680. error("%s: unable to find the x86 compiler" % version)
  681. if arch == 'win64' and not exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')) \
  682. and not exists(os.path.join(vcdir, 'bin', 'x86_amd64', 'cl.exe')):
  683. error("%s: unable to find the amd64 compiler" % version)
  684. perl = get_output('perl', '-V')
  685. if not perl or 'perl5' not in perl:
  686. error("perl does not seem to be installed.")
  687. nsis = get_registry_value(r'SOFTWARE\NSIS')
  688. if not nsis or not exists(os.path.join(nsis, 'makensis.exe')):
  689. error("NSIS does not seem to be installed.")
  690. def build_msvc(config, basedir):
  691. msvc, arch = rchop(config, '-dbg').split('-')
  692. env_var, reg_ver = MSVC_LOCATION[msvc]
  693. vcdir = os.path.join(os.environ[env_var], '..', '..', 'VC')
  694. vcarg = 'x86'
  695. if arch == 'win64':
  696. if exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')):
  697. vcarg = 'amd64'
  698. else:
  699. vcarg = 'x86_amd64'
  700. python = sys.executable
  701. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os, sys; sys.stdout.write(repr(dict(os.environ)))"' % (
  702. os.path.join(vcdir, 'vcvarsall.bat'), vcarg, python), stdout=subprocess.PIPE, shell=True)
  703. stdout, _ = process.communicate()
  704. exitcode = process.wait()
  705. if exitcode != 0:
  706. error("%s: unable to initialize the environment" % msvc)
  707. os.environ.update(eval(stdout.strip()))
  708. version, simple_version = get_version(basedir)
  709. cflags = config.endswith('-dbg') and '/MDd /Zi' or '/MD'
  710. build_deplibs(config, basedir, cflags=cflags)
  711. sha1, url = MSVC_RUNTIME[rchop(config, '-dbg')]
  712. shutil.copy(download_file(url, sha1, basedir), os.path.join(basedir, config, 'vcredist.exe'))
  713. libdir = os.path.join(basedir, config, 'deplibs')
  714. qtdir = os.path.join(basedir, config, 'qt')
  715. mkdir_p(qtdir)
  716. configure_args = qt_config('msvc',
  717. '-I %s\\include' % libdir,
  718. '-L %s\\lib' % libdir,
  719. 'OPENSSL_LIBS="-L%s\\\\lib -lssleay32 -llibeay32 -lUser32 -lAdvapi32 -lGdi32 -lCrypt32"' % libdir.replace('\\', '\\\\'))
  720. build_qt(qtdir, 'nmake', '%s\\..\\qt\\configure.exe %s' % (basedir, configure_args))
  721. appdir = os.path.join(basedir, config, 'app')
  722. mkdir_p(appdir)
  723. os.chdir(appdir)
  724. rmdir('bin')
  725. mkdir_p('bin')
  726. os.environ['WKHTMLTOX_VERSION'] = version
  727. shell('%s\\bin\\qmake %s\\..\\wkhtmltopdf.pro' % (qtdir, basedir))
  728. shell('nmake')
  729. if config.endswith('-dbg'):
  730. return
  731. makensis = os.path.join(get_registry_value(r'SOFTWARE\NSIS'), 'makensis.exe')
  732. os.chdir(os.path.join(basedir, '..'))
  733. shell('"%s" /DVERSION=%s /DSIMPLE_VERSION=%s /DTARGET=%s /DMSVC=%s /DARCH=%s wkhtmltox.nsi' % \
  734. (makensis, version, nsis_version(simple_version), config, reg_ver, arch))
  735. # ------------------------------------------------ MinGW-W64 Cross Environment
  736. MINGW_W64_PREFIX = {
  737. 'mingw-w64-cross-win32' : 'i686-w64-mingw32',
  738. 'mingw-w64-cross-win64' : 'x86_64-w64-mingw32',
  739. }
  740. def check_mingw64_cross(config):
  741. shell('%s-gcc --version' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  742. def build_mingw64_cross(config, basedir):
  743. version, simple_version = get_version(basedir)
  744. build_deplibs(config, basedir, mingw_w64=MINGW_W64_PREFIX.get(rchop(config, '-dbg')))
  745. libdir = os.path.join(basedir, config, 'deplibs')
  746. qtdir = os.path.join(basedir, config, 'qt')
  747. configure_args = qt_config('mingw-w64-cross',
  748. '--prefix=%s' % qtdir,
  749. '-I%s/include' % libdir,
  750. '-L%s/lib' % libdir,
  751. '-device-option CROSS_COMPILE=%s-' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  752. os.environ['OPENSSL_LIBS'] = '-lssl -lcrypto -L%s/lib -lws2_32 -lgdi32 -lcrypt32' % libdir
  753. mkdir_p(qtdir)
  754. os.chdir(qtdir)
  755. build_qt(qtdir, 'make -j%d' % CPU_COUNT,
  756. '%s/../qt/configure %s' % (basedir, configure_args))
  757. appdir = os.path.join(basedir, config, 'app')
  758. mkdir_p(appdir)
  759. os.chdir(appdir)
  760. shell('rm -f bin/*')
  761. # set up cross compiling prefix correctly
  762. os.environ['WKHTMLTOX_VERSION'] = version
  763. shell('%s/bin/qmake -set CROSS_COMPILE %s-' % (qtdir, MINGW_W64_PREFIX[rchop(config, '-dbg')]))
  764. shell('%s/bin/qmake -spec win32-g++-4.6 %s/../wkhtmltopdf.pro' % (qtdir, basedir))
  765. shell('make')
  766. shutil.copy('bin/libwkhtmltox0.a', 'bin/wkhtmltox.lib')
  767. if config.endswith('-dbg'):
  768. return
  769. os.chdir(os.path.join(basedir, '..'))
  770. shell('makensis -DVERSION=%s -DSIMPLE_VERSION=%s -DTARGET=%s -DMINGW -DARCH=%s wkhtmltox.nsi' % \
  771. (version, nsis_version(simple_version), config, rchop(config, '-dbg').split('-')[-1]))
  772. # -------------------------------------------------- Linux schroot environment
  773. def check_linux_generic(config):
  774. chroot_env = ('amd64' in config) and 'generic-amd64' or 'generic-i386'
  775. chroot_shell(chroot_env, 'gcc --version')
  776. def build_linux_generic(config, basedir):
  777. chroot_env = ('amd64' in config) and 'generic-amd64' or 'generic-i386'
  778. os.chdir(os.path.realpath(os.path.join(basedir, '..')))
  779. chroot_shell(chroot_env, 'python scripts/build.py %s -chroot-build' % ' '.join(sys.argv[1:]))
  780. if config.endswith('-dbg'):
  781. return
  782. version, simple_version = get_version(basedir)
  783. os.chdir(os.path.join(basedir, config))
  784. shell('XZ_OPT=-9 tar Jcf ../%s-%s_%s.tar.xz wkhtmltox/' % (PROJECT_SETUP['name'], version, config))
  785. def chroot_build_linux_generic(config, basedir):
  786. version, simple_version = get_version(basedir)
  787. build_deplibs(config, basedir)
  788. libdir = os.path.join(basedir, config, 'deplibs')
  789. qtdir = os.path.join(basedir, config, 'qt')
  790. mkdir_p(qtdir)
  791. configure_args = qt_config('posix',
  792. '--prefix=%s' % qtdir,
  793. '-I%s/include' % libdir,
  794. '-L%s/lib' % libdir,
  795. '-DOPENSSL_NO_SSL2')
  796. build_qt(qtdir, 'make -j%d' % CPU_COUNT, '%s/../qt/configure %s' % (basedir, configure_args))
  797. app = os.path.join(basedir, config, 'app')
  798. dist = os.path.join(basedir, config, 'wkhtmltox')
  799. mkdir_p(app)
  800. rmdir(dist)
  801. mkdir_p(dist)
  802. os.chdir(app)
  803. shell('rm -f bin/*')
  804. os.environ['WKHTMLTOX_VERSION'] = version
  805. shell('%s/bin/qmake %s/../wkhtmltopdf.pro' % (qtdir, basedir))
  806. shell('make install INSTALL_ROOT=%s' % dist)
  807. # -------------------------------------------------- POSIX local environment
  808. def check_posix_local(config):
  809. pass
  810. def build_posix_local(config, basedir):
  811. version, simple_version = get_version(basedir)
  812. app = os.path.join(basedir, config, 'app')
  813. qt = os.path.join(basedir, config, 'qt')
  814. dist = os.path.join(basedir, config, 'wkhtmltox-%s' % version)
  815. make = get_output('which', 'gmake') and 'gmake' or 'make'
  816. mkdir_p(qt)
  817. mkdir_p(app)
  818. rmdir(dist)
  819. mkdir_p(os.path.join(dist, 'bin'))
  820. mkdir_p(os.path.join(dist, 'include', 'wkhtmltox'))
  821. mkdir_p(os.path.join(dist, 'lib'))
  822. build_qt(qt, '%s -j%d' % (make, CPU_COUNT),
  823. '%s/../qt/configure %s' % (basedir, qt_config('posix', '--prefix=%s' % qt)))
  824. os.chdir(app)
  825. shell('rm -f bin/*')
  826. os.environ['WKHTMLTOX_VERSION'] = version
  827. shell('../qt/bin/qmake ../../../wkhtmltopdf.pro')
  828. shell('%s -j%d' % (make, CPU_COUNT))
  829. shell('%s install INSTALL_ROOT=../wkhtmltox-%s' % (make, version))
  830. if config.endswith('-dbg'):
  831. return
  832. os.chdir(os.path.join(basedir, config))
  833. shell('tar -c -v -f ../wkhtmltox-%s_local-%s.tar wkhtmltox-%s/' % (version, platform.node(), version))
  834. shell('xz --compress --force --verbose -9 ../wkhtmltox-%s_local-%s.tar' % (version, platform.node()))
  835. # --------------------------------------------------------------- OS X
  836. OSXPKG_IDENTIFIER = 'org.wkhtmltopdf'
  837. OSXPKG_PREFIX = '/usr/local/share/wkhtmltox-installer'
  838. def check_osx(config):
  839. if not platform.system() == 'Darwin' or not platform.mac_ver()[0]:
  840. error('This can only be run on a OS X system!')
  841. if not get_output('xcode-select', '--print-path'):
  842. error('Xcode is not installed, aborting.')
  843. if not config.endswith('-dbg') and not get_output('which', 'fpm'):
  844. error('Please install fpm by running "sudo gem install fpm --no-ri --no-rdoc"')
  845. def build_osx(config, basedir):
  846. version, simple_version = get_version(basedir)
  847. build_deplibs(config, basedir)
  848. osxver = platform.mac_ver()[0][:platform.mac_ver()[0].rindex('.')]
  849. framework = config.split('-')[1]
  850. if osxver == '10.6':
  851. osxcfg = '-%s -platform macx-g++42' % framework
  852. else:
  853. osxcfg = '-%s -platform unsupported/macx-clang' % framework
  854. flags = ''
  855. if framework == 'carbon' and osxver != '10.6':
  856. for item in ['CFLAGS', 'CXXFLAGS']:
  857. flags += '"QMAKE_%s += %s" ' % (item, '-fvisibility=hidden -fvisibility-inlines-hidden')
  858. def get_dir(name):
  859. return os.path.join(basedir, config, name)
  860. configure_args = qt_config('osx', osxcfg,
  861. '--prefix=%s' % get_dir('qt'),
  862. '-I %s/include' % get_dir('deplibs'),
  863. '-L %s/lib' % get_dir('deplibs'))
  864. rmdir(get_dir('dist'))
  865. rmdir(get_dir('pkg'))
  866. mkdir_p(get_dir('qt'))
  867. mkdir_p(get_dir('app'))
  868. mkdir_p(get_dir('pkg'))
  869. build_qt(get_dir('qt'), 'make -j%d' % CPU_COUNT, '../../../qt/configure %s' % configure_args)
  870. os.chdir(get_dir('app'))
  871. shell('rm -f bin/*')
  872. os.environ['WKHTMLTOX_VERSION'] = version
  873. shell('../qt/bin/qmake %s ../../../wkhtmltopdf.pro' % flags)
  874. shell('make -j%d' % CPU_COUNT)
  875. if osxver not in ['10.6', '10.7']:
  876. for item in ['wkhtmltoimage', 'wkhtmltopdf', 'libwkhtmltox.%s.dylib' % simple_version]:
  877. shell(' '.join([
  878. 'install_name_tool', '-change',
  879. '/System/Library/Frameworks/CoreText.framework/Versions/A/CoreText',
  880. '/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreText.framework/CoreText',
  881. 'bin/'+item]))
  882. shell('make install INSTALL_ROOT=%s' % get_dir('dist'))
  883. if config.endswith('-dbg'):
  884. return
  885. def _osx_tar(info):
  886. info.uid = info.gid = 0
  887. info.uname = 'root'
  888. info.gname = 'wheel'
  889. return info
  890. # create tarball for application and copy xz
  891. os.chdir(get_dir('dist'))
  892. with tarfile.open('../pkg/app.tar', 'w') as tar:
  893. tar.add('.', './', filter=_osx_tar)
  894. xz = os.path.join(get_dir('deplibs'), 'bin', 'xz')
  895. shell('%s --compress --force --verbose -9 ../pkg/app.tar' % xz)
  896. shutil.copy(xz, '../pkg/')
  897. with open('../pkg/uninstall-wkhtmltox', 'w') as f:
  898. os.chmod('../pkg/uninstall-wkhtmltox', 0o755)
  899. f.write("""#!/bin/bash
  900. if [ "$(id -u)" != "0" ]; then
  901. echo "This script must be run as sudo uninstall-wkhtmltox" 1>&2
  902. exit 1
  903. fi
  904. cd /usr/local
  905. if which pkgutil >/dev/null; then
  906. pkgutil --forget %s.%s
  907. fi
  908. """ % (OSXPKG_IDENTIFIER, PROJECT_SETUP['name']))
  909. for root, dirs, files in os.walk(get_dir('dist')):
  910. for file in files:
  911. f.write('echo REMOVE /usr/local/%(name)s && rm -f %(name)s\n' % { 'name': os.path.relpath(os.path.join(root, file)) })
  912. f.write('echo REMOVE /usr/local/include/wkhtmltox && rm -df /usr/local/include/wkhtmltox\n')
  913. f.write('echo REMOVE /usr/local/bin/uninstall-wkhtmltox && rm -f /usr/local/bin/uninstall-wkhtmltox')
  914. open('../extract.sh', 'w').write("""#!/bin/bash
  915. TGTDIR=/usr/local
  916. BASEDIR=%s
  917. cd $BASEDIR
  918. ./xz --decompress app.tar.xz
  919. cd $TGTDIR
  920. tar oxf $BASEDIR/app.tar
  921. mv $BASEDIR/uninstall-wkhtmltox $TGTDIR/bin
  922. rm -fr $BASEDIR
  923. """ % OSXPKG_PREFIX)
  924. os.chdir(os.path.join(basedir, config))
  925. fpm_args = '--force --prefix %s --category utils -s dir -C pkg' % OSXPKG_PREFIX
  926. for key in PROJECT_SETUP:
  927. fpm_args += ' --%s "%s"' % (key, PROJECT_SETUP[key])
  928. fpm_args += ' --osxpkg-identifier-prefix "%s" --version "%s"' % (OSXPKG_IDENTIFIER, version)
  929. fpm_args += ' -t osxpkg --package ../%s-%s_%s.pkg' % (PROJECT_SETUP['name'], version, config)
  930. fpm_args += ' --after-install extract.sh .'
  931. shell('fpm %s' % fpm_args)
  932. # --------------------------------------------------------------- command line
  933. def usage(exit_code=2):
  934. message("Usage: scripts/build.py <target> [-clean] [-debug]\n\nThe supported targets are:\n")
  935. opts = list(BUILDERS.keys())
  936. opts.sort()
  937. for opt in opts:
  938. message('* %s\n' % opt)
  939. sys.exit(exit_code)
  940. def main():
  941. rootdir = os.path.realpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
  942. basedir = os.path.join(rootdir, 'static-build')
  943. if not exists(os.path.join(rootdir, 'qt', 'configure')):
  944. error('error: source code for Qt not available, cannot proceed.')
  945. if len(sys.argv) == 1:
  946. usage(0)
  947. config = sys.argv[1]
  948. if config not in BUILDERS:
  949. usage()
  950. for arg in sys.argv[2:]:
  951. if not arg in ['-clean', '-debug', '-chroot-build']:
  952. usage()
  953. final_config = config
  954. if '-debug' in sys.argv[2:]:
  955. final_config += '-dbg'
  956. QT_CONFIG['common'].extend(['remove:-release', 'remove:-webkit', '-debug', '-webkit-debug'])
  957. if '-clean' in sys.argv[2:]:
  958. rmdir(os.path.join(basedir, final_config))
  959. os.chdir(rootdir)
  960. if '-chroot-build' in sys.argv[2:]:
  961. globals()['chroot_build_%s' % BUILDERS[config]](final_config, basedir)
  962. return
  963. globals()['check_%s' % BUILDERS[config]](final_config)
  964. globals()['build_%s' % BUILDERS[config]](final_config, basedir)
  965. if __name__ == '__main__':
  966. main()