build.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. OPENSSL = {
  21. 'repository': 'https://github.com/openssl/openssl.git',
  22. 'branch' : 'OpenSSL_1_0_1-stable',
  23. 'tag' : 'OpenSSL_1_0_1g',
  24. 'build' : {
  25. 'msvc*-win32*': {
  26. 'configure' : 'VC-WIN32 no-asm',
  27. 'debug' : 'debug-VC-WIN32 no-asm',
  28. 'build' : ['ms\\do_ms.bat', 'nmake /f ms\\nt.mak install'],
  29. 'libs' : ['ssleay32.lib', 'libeay32.lib'],
  30. 'os_libs' : '-lUser32 -lAdvapi32 -lGdi32 -lCrypt32'
  31. },
  32. 'msvc*-win64*': {
  33. 'configure' : 'VC-WIN64A',
  34. 'debug' : 'debug-VC-WIN64A',
  35. 'build' : ['ms\\do_win64a.bat', 'nmake /f ms\\nt.mak install'],
  36. 'libs' : ['ssleay32.lib', 'libeay32.lib'],
  37. 'os_libs' : '-lUser32 -lAdvapi32 -lGdi32 -lCrypt32'
  38. },
  39. 'mingw-w64-cross-win32': {
  40. 'configure' : '--cross-compile-prefix=i686-w64-mingw32- no-shared no-asm mingw64',
  41. 'build' : ['make', 'make install_sw'],
  42. 'libs' : ['libssl.a', 'libcrypto.a'],
  43. 'os_libs' : '-lws2_32 -lgdi32 -lcrypt32'
  44. },
  45. 'mingw-w64-cross-win64': {
  46. 'configure' : '--cross-compile-prefix=x86_64-w64-mingw32- no-shared no-asm mingw64',
  47. 'build' : ['make', 'make install_sw'],
  48. 'libs' : ['libssl.a', 'libcrypto.a'],
  49. 'os_libs' : '-lws2_32 -lgdi32 -lcrypt32'
  50. }
  51. }
  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. '-qt-zlib', # use bundled versions of libraries
  65. '-qt-libpng',
  66. '-qt-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-scripttools',
  77. '-no-sql-ibase',
  78. '-no-sql-mysql',
  79. '-no-sql-odbc',
  80. '-no-sql-psql',
  81. '-no-sql-sqlite',
  82. '-no-sql-sqlite2',
  83. '-no-mmx',
  84. '-no-3dnow',
  85. '-no-sse',
  86. '-no-sse2',
  87. '-no-multimedia',
  88. '-nomake demos',
  89. '-nomake docs',
  90. '-nomake examples',
  91. '-nomake tools',
  92. '-nomake tests',
  93. '-nomake translations'
  94. ],
  95. 'msvc': [
  96. '-mp',
  97. '-no-script',
  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. '-script', # "make install" does not copy QtScript/qscriptengine.h
  110. '-xrender', # xrender support is required
  111. '-largefile',
  112. '-rpath',
  113. '-openssl', # load OpenSSL binaries at runtime
  114. '-no-dbus',
  115. '-no-nis',
  116. '-no-cups',
  117. '-no-iconv',
  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. '-D ENABLE_VIDEO=0', # required as otherwise gstreamer gets linked in
  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. '-script', # "make install" does not copy QtScript/qscriptengine.h
  146. '-openssl-linked', # static linkage for OpenSSL
  147. '-no-reduce-exports',
  148. '-no-rpath',
  149. '-xplatform win32-g++-4.6'
  150. ]
  151. }
  152. BUILDERS = {
  153. 'msvc2008-win32': 'msvc',
  154. 'msvc2008-win64': 'msvc',
  155. 'msvc2010-win32': 'msvc',
  156. 'msvc2010-win64': 'msvc',
  157. 'msvc2012-win32': 'msvc',
  158. 'msvc2012-win64': 'msvc',
  159. 'msvc2013-win32': 'msvc',
  160. 'msvc2013-win64': 'msvc',
  161. 'msvc-winsdk71-win32': 'msvc_winsdk71',
  162. 'msvc-winsdk71-win64': 'msvc_winsdk71',
  163. 'setup-schroot-centos5': 'setup_schroot',
  164. 'setup-schroot-wheezy': 'setup_schroot',
  165. 'update-all-schroots': 'update_schroot',
  166. 'centos5-i386': 'linux_schroot',
  167. 'centos5-amd64': 'linux_schroot',
  168. 'wheezy-i386': 'linux_schroot',
  169. 'wheezy-amd64': 'linux_schroot',
  170. 'mingw-w64-cross-win32': 'mingw64_cross',
  171. 'mingw-w64-cross-win64': 'mingw64_cross'
  172. }
  173. HOST_PACKAGES = ['git-core', 'xz-utils', 'build-essential', 'mingw-w64', 'nsis', 'debootstrap', 'schroot', 'rinse']
  174. CHROOT_SETUP = {
  175. 'wheezy': [
  176. ('debootstrap', 'wheezy', 'http://ftp.us.debian.org/debian/'),
  177. ('write_file', 'etc/apt/sources.list', """
  178. deb http://ftp.debian.org/debian/ wheezy main contrib non-free
  179. deb http://ftp.debian.org/debian/ wheezy-updates main contrib non-free
  180. deb http://security.debian.org/ wheezy/updates main contrib non-free
  181. deb-src http://ftp.debian.org/debian/ wheezy main contrib non-free
  182. deb-src http://ftp.debian.org/debian/ wheezy-updates main contrib non-free
  183. deb-src http://security.debian.org/ wheezy/updates main contrib non-free"""),
  184. ('shell', 'apt-get update'),
  185. ('shell', 'apt-get dist-upgrade --assume-yes'),
  186. ('shell', 'apt-get install --assume-yes xz-utils'),
  187. ('shell', 'apt-get build-dep --assume-yes libqt4-core'),
  188. ('write_file', 'update.sh', 'apt-get update\napt-get dist-upgrade --assume-yes\n'),
  189. ('schroot_conf', 'Debian Wheezy')
  190. ],
  191. 'centos5': [
  192. ('rinse', 'centos-5'),
  193. ('shell', 'yum update -y'),
  194. ('append_file:amd64', 'etc/yum.conf', 'exclude = *.i?86\n'),
  195. ('shell', 'yum install -y gcc gcc-c++ make qt4-devel openssl-devel diffutils perl xz'),
  196. ('write_file', 'update.sh', 'yum update -y\n'),
  197. ('schroot_conf', 'CentOS 5')
  198. ]
  199. }
  200. # --------------------------------------------------------------- HELPERS
  201. import os, sys, subprocess, shutil, fnmatch, multiprocessing
  202. from os.path import exists
  203. CPU_COUNT = max(2, multiprocessing.cpu_count()-1) # leave one CPU free
  204. def rchop(s, e):
  205. if s.endswith(e):
  206. return s[:-len(e)]
  207. return s
  208. def error(msg):
  209. print msg
  210. sys.exit(1)
  211. def shell(cmd):
  212. ret = os.system(cmd)
  213. if ret != 0:
  214. error("command failed: exit code %d" % ret)
  215. def rmdir(path):
  216. if exists(path):
  217. shutil.rmtree(path)
  218. def mkdir_p(path):
  219. if not exists(path):
  220. os.makedirs(path)
  221. def get_version(basedir):
  222. text = open(os.path.join(basedir, '..', 'VERSION'), 'r').read()
  223. if '-' not in text:
  224. return (text, text)
  225. version = text[:text.index('-')]
  226. os.chdir(os.path.join(basedir, '..'))
  227. try:
  228. hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], stderr=subprocess.STDOUT).strip()
  229. except subprocess.CalledProcessError:
  230. return (text, version)
  231. return ('%s-%s' % (version, hash), version)
  232. def build_openssl(config, basedir):
  233. cfg = None
  234. for key in OPENSSL['build']:
  235. if fnmatch.fnmatch(config, key):
  236. cfg = key
  237. if not cfg:
  238. return
  239. srcdir = os.path.join(basedir, 'openssl')
  240. dstdir = os.path.join(basedir, config, 'openssl')
  241. def is_compiled():
  242. compiled = exists(os.path.join(dstdir, 'include', 'openssl', 'ssl.h'))
  243. for lib in OPENSSL['build'][cfg]['libs']:
  244. compiled = compiled and exists(os.path.join(dstdir, 'lib', lib))
  245. return compiled
  246. if not exists(os.path.join(srcdir, '.git')):
  247. rmdir(srcdir)
  248. rmdir(dstdir)
  249. os.chdir(basedir)
  250. shell('git clone --branch %s --single-branch %s openssl' % (OPENSSL['branch'], OPENSSL['repository']))
  251. os.chdir(srcdir)
  252. shell('git clean -fdx')
  253. shell('git reset --hard HEAD')
  254. shell('git checkout %s' % (OPENSSL['tag']))
  255. if not is_compiled():
  256. opts = OPENSSL['build'][cfg]
  257. shell('perl Configure --openssldir=%s %s' % (dstdir, opts['configure']))
  258. for cmd in opts['build']:
  259. shell(cmd)
  260. shell('git clean -fdx')
  261. if not is_compiled():
  262. error("Unable to compile OpenSSL for your system, aborting.")
  263. return OPENSSL['build'][cfg]['os_libs']
  264. # --------------------------------------------------------------- Linux chroot
  265. def check_setup_schroot(config):
  266. import platform
  267. if not sys.platform.startswith('linux') or \
  268. not platform.architecture()[0].startswith('64') or \
  269. platform.linux_distribution()[0] not in ['Ubuntu', 'Debian']:
  270. error('This can only be run on a 64-bit Debian/Ubuntu distribution, aborting.')
  271. if os.geteuid() != 0:
  272. error('This script must be run as root.')
  273. try:
  274. login = subprocess.check_output(['logname'], stderr=subprocess.STDOUT).strip()
  275. if login == 'root':
  276. error('Please run via sudo to determine login for which schroot access is to be given.')
  277. except subprocess.CalledProcessError:
  278. error('Unable to determine the login for which schroot access is to be given.')
  279. def build_setup_schroot(config, basedir):
  280. shell('apt-get update')
  281. shell('apt-get install --assume-yes %s' % (' '.join(HOST_PACKAGES)))
  282. login = subprocess.check_output(['logname'], stderr=subprocess.STDOUT).strip()
  283. chroot = config[1+config.rindex('-'):]
  284. for arch in ['amd64', 'i386']:
  285. print '******************* %s-%s' % (chroot, arch)
  286. root_dir = '/opt/wkhtmltopdf-build/%s-%s' % (chroot, arch)
  287. rmdir(root_dir)
  288. mkdir_p(root_dir)
  289. for command in CHROOT_SETUP[chroot]:
  290. # handle architecture-specific commands
  291. name = command[0]
  292. if ':' in name:
  293. if name[1+name.rindex(':'):] != arch:
  294. continue
  295. else:
  296. name = name[:name.rindex(':')]
  297. # handle commands
  298. if name == 'debootstrap':
  299. shell('debootstrap --arch=%(arch)s --variant=buildd %(distro)s %(dir)s %(url)s' % {
  300. 'arch': arch, 'dir': root_dir, 'distro': command[1], 'url': command[2] })
  301. elif name == 'rinse':
  302. cmd = (arch == 'i386' and 'linux32 rinse' or 'rinse')
  303. shell('%s --arch %s --distribution %s --directory %s' % (cmd, arch, command[1], root_dir))
  304. elif name == 'shell':
  305. cmd = (arch == 'i386' and 'linux32 chroot' or 'chroot')
  306. shell('%s %s %s' % (cmd, root_dir, command[1]))
  307. elif name == 'write_file':
  308. open(os.path.join(root_dir, command[1]), 'w').write(command[2].strip())
  309. elif name == 'append_file':
  310. open(os.path.join(root_dir, command[1]), 'a').write(command[2].strip())
  311. elif name == 'schroot_conf':
  312. cfg = open('/etc/schroot/chroot.d/wkhtmltopdf-%s-%s' % (chroot, arch), 'w')
  313. cfg.write('[wkhtmltopdf-%s-%s]\n' % (chroot, arch))
  314. cfg.write('type=directory\ndirectory=%s/\n' % root_dir)
  315. cfg.write('description=%s %s for wkhtmltopdf\n' % (command[1], arch))
  316. cfg.write('users=%s\nroot-users=root\n' % login)
  317. if arch == 'i386':
  318. cfg.write('personality=linux32\n')
  319. cfg.close()
  320. def check_update_schroot(config):
  321. import platform
  322. if not sys.platform.startswith('linux') or \
  323. not platform.architecture()[0].startswith('64') or \
  324. platform.linux_distribution()[0] not in ['Ubuntu', 'Debian']:
  325. error('This can only be run on a 64-bit Debian/Ubuntu distribution, aborting.')
  326. if os.geteuid() != 0:
  327. error('This script must be run as root.')
  328. try:
  329. subprocess.check_output(['schroot', '--list'], stderr=subprocess.STDOUT)
  330. except subprocess.CalledProcessError:
  331. error('Unable to determine the list of available schroots.')
  332. def build_update_schroot(config, basedir):
  333. names = subprocess.check_output(['schroot', '--list'], stderr=subprocess.STDOUT).strip()
  334. for name in names.split('\n'):
  335. print '******************* %s' % name[name.index('wkhtmltopdf-'):]
  336. shell('schroot -c %s -- /bin/bash /update.sh' % name[name.index('wkhtmltopdf-'):])
  337. # --------------------------------------------------------------- MSVC (2008-2013)
  338. MSVC_LOCATION = {
  339. 'msvc2008': 'VS90COMNTOOLS',
  340. 'msvc2010': 'VS100COMNTOOLS',
  341. 'msvc2012': 'VS110COMNTOOLS',
  342. 'msvc2013': 'VS120COMNTOOLS'
  343. }
  344. def check_msvc(config):
  345. version, arch = rchop(config, '-dbg').split('-')
  346. env_var = MSVC_LOCATION[version]
  347. if not env_var in os.environ:
  348. error("%s does not seem to be installed." % version)
  349. vcdir = os.path.join(os.environ[env_var], '..', '..', 'VC')
  350. if not exists(os.path.join(vcdir, 'vcvarsall.bat')):
  351. error("%s: unable to find vcvarsall.bat" % version)
  352. if arch == 'win32' and not exists(os.path.join(vcdir, 'bin', 'cl.exe')):
  353. error("%s: unable to find the x86 compiler" % version)
  354. if arch == 'win64' and not exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')) \
  355. and not exists(os.path.join(vcdir, 'bin', 'x86_amd64', 'cl.exe')):
  356. error("%s: unable to find the amd64 compiler" % version)
  357. def build_msvc(config, basedir):
  358. msvc, arch = rchop(config, '-dbg').split('-')
  359. vcdir = os.path.join(os.environ[MSVC_LOCATION[msvc]], '..', '..', 'VC')
  360. vcarg = 'x86'
  361. if arch == 'win64':
  362. if exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')):
  363. vcarg = 'amd64'
  364. else:
  365. vcarg = 'x86_amd64'
  366. python = sys.executable
  367. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os; print repr(os.environ)"' % (
  368. os.path.join(vcdir, 'vcvarsall.bat'), vcarg, python), stdout=subprocess.PIPE, shell=True)
  369. stdout, _ = process.communicate()
  370. exitcode = process.wait()
  371. if exitcode != 0:
  372. error("%s: unable to initialize the environment" % msvc)
  373. os.environ.update(eval(stdout.strip()))
  374. build_msvc_common(config, basedir)
  375. # --------------------------------------------------------------- MSVC via Windows SDK 7.1
  376. def check_msvc_winsdk71(config):
  377. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  378. if pfile in os.environ and exists(os.path.join(os.environ[pfile], 'Microsoft SDKs', 'Windows', 'v7.1', 'Bin', 'SetEnv.cmd')):
  379. return
  380. error("Unable to detect the location of Windows SDK 7.1")
  381. def build_msvc_winsdk71(config, basedir):
  382. arch = config[config.rindex('-'):]
  383. setenv = None
  384. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  385. if not pfile in os.environ:
  386. continue
  387. setenv = os.path.join(os.environ[pfile], 'Microsoft SDKs', 'Windows', 'v7.1', 'Bin', 'SetEnv.cmd')
  388. mode = debug and '/Debug' or '/Release'
  389. if arch == 'win64':
  390. args = '/2008 /x64 %s' % mode
  391. else:
  392. args = '/2008 /x86 %s' % mode
  393. python = sys.executable
  394. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os; print repr(os.environ)"' % (
  395. setenv, args, python), stdout=subprocess.PIPE, shell=True)
  396. stdout, _ = process.communicate()
  397. exitcode = process.wait()
  398. if exitcode != 0:
  399. error("unable to initialize the environment for Windows SDK 7.1")
  400. os.environ.update(eval(stdout.strip()))
  401. build_msvc_common(config, basedir)
  402. def build_msvc_common(config, basedir):
  403. version, simple_version = get_version(basedir)
  404. ssl_libs = build_openssl(config, basedir)
  405. ssldir = os.path.join(basedir, config, 'openssl')
  406. qtdir = os.path.join(basedir, config, 'qt')
  407. mkdir_p(qtdir)
  408. args = []
  409. args.extend(QT_CONFIG['common'])
  410. args.extend(QT_CONFIG['msvc'])
  411. args.append('-I %s\\include' % ssldir)
  412. args.append('-L %s\\lib' % ssldir)
  413. args.append('OPENSSL_LIBS="-L%s -lssleay32 -llibeay32 %s"' % \
  414. (ssldir.replace('\\', '\\\\'), ssl_libs))
  415. os.chdir(qtdir)
  416. if not exists('is_configured'):
  417. shell('%s\\..\\qt\\configure.exe %s' % (basedir, ' '.join(args)))
  418. open('is_configured', 'w').write('')
  419. shell('nmake')
  420. appdir = os.path.join(basedir, config, 'app')
  421. mkdir_p(appdir)
  422. os.chdir(appdir)
  423. rmdir('bin')
  424. mkdir_p('bin')
  425. os.environ['WKHTMLTOX_VERSION'] = version
  426. shell('%s\\bin\\qmake %s\\..\\wkhtmltopdf.pro' % (qtdir, basedir))
  427. shell('nmake')
  428. found = False
  429. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  430. if not pfile in os.environ or not exists(os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')):
  431. continue
  432. found = True
  433. makensis = os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')
  434. os.chdir(os.path.join(basedir, '..'))
  435. shell('"%s" /DVERSION=%s /DSIMPLE_VERSION=%s /DTARGET=%s wkhtmltox.nsi' % \
  436. (makensis, version, simple_version, config))
  437. if not found:
  438. print "\n\nCould not build installer as NSIS was not found."
  439. # ------------------------------------------------ MinGW-W64 Cross Environment
  440. MINGW_W64_PREFIX = {
  441. 'mingw-w64-cross-win32' : 'i686-w64-mingw32',
  442. 'mingw-w64-cross-win64' : 'x86_64-w64-mingw32',
  443. }
  444. def check_mingw64_cross(config):
  445. shell('%s-gcc --version' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  446. def build_mingw64_cross(config, basedir):
  447. version, simple_version = get_version(basedir)
  448. ssl_libs = build_openssl(config, basedir)
  449. ssldir = os.path.join(basedir, config, 'openssl')
  450. build = os.path.join(basedir, config, 'qt_build')
  451. qtdir = os.path.join(basedir, config, 'qt')
  452. mkdir_p(build)
  453. args = []
  454. args.extend(QT_CONFIG['common'])
  455. args.extend(QT_CONFIG['mingw-w64-cross'])
  456. args.append('--prefix=%s' % qtdir)
  457. args.append('-I %s/include' % ssldir)
  458. args.append('-L %s/lib' % ssldir)
  459. args.append('-device-option CROSS_COMPILE=%s-' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  460. os.environ['OPENSSL_LIBS'] = '-lssl -lcrypto -L %s/lib %s' % (ssldir, ssl_libs)
  461. os.chdir(build)
  462. if not exists('is_configured'):
  463. shell('%s/../qt/configure %s' % (basedir, ' '.join(args)))
  464. shell('touch is_configured')
  465. shell('make -j%d' % CPU_COUNT)
  466. shell('make install')
  467. appdir = os.path.join(basedir, config, 'app')
  468. mkdir_p(appdir)
  469. os.chdir(appdir)
  470. shell('rm -f bin/*')
  471. # set up cross compiling prefix correctly (isn't set by make install)
  472. os.environ['QTDIR'] = qtdir
  473. os.environ['WKHTMLTOX_VERSION'] = version
  474. shell('%s/bin/qmake -set CROSS_COMPILE %s-' % (qtdir, MINGW_W64_PREFIX[rchop(config, '-dbg')]))
  475. shell('%s/bin/qmake -spec win32-g++-4.6 %s/../wkhtmltopdf.pro' % (qtdir, basedir))
  476. shell('make')
  477. shutil.copy('bin/libwkhtmltox0.a', 'bin/wkhtmltox.lib')
  478. os.chdir(os.path.join(basedir, '..'))
  479. shell('makensis -DVERSION=%s -DSIMPLE_VERSION=%s -DTARGET=%s wkhtmltox.nsi' % \
  480. (version, simple_version, config))
  481. # -------------------------------------------------- Linux schroot environment
  482. def check_linux_schroot(config):
  483. shell('schroot -c wkhtmltopdf-%s -- gcc --version' % rchop(config, '-dbg'))
  484. def build_linux_schroot(config, basedir):
  485. version, simple_version = get_version(basedir)
  486. dir = os.path.join(basedir, config)
  487. script = os.path.join(dir, 'build.sh')
  488. dist = os.path.join(dir, 'wkhtmltox-%s' % version)
  489. mkdir_p(os.path.join(dir, 'qt_build'))
  490. mkdir_p(os.path.join(dir, 'app'))
  491. rmdir(dist)
  492. mkdir_p(os.path.join(dist, 'bin'))
  493. mkdir_p(os.path.join(dist, 'include', 'wkhtmltox'))
  494. mkdir_p(os.path.join(dist, 'lib'))
  495. args = []
  496. args.extend(QT_CONFIG['common'])
  497. args.extend(QT_CONFIG['posix'])
  498. args.append('--prefix=../qt')
  499. lines = ['#!/bin/bash']
  500. lines.append('# start of autogenerated build script')
  501. lines.append('cd qt_build')
  502. if config == 'centos5-i386':
  503. lines.append('export CFLAGS=-march=i486')
  504. lines.append('export CXXFLAGS=-march=i486')
  505. lines.append('if [ ! -f is_configured ]; then')
  506. lines.append(' ../../../qt/configure %s || exit 1' % ' '.join(args))
  507. lines.append(' touch is_configured')
  508. lines.append('fi')
  509. lines.append('if ! make -j%d -q; then\n make -j%d || exit 1\nfi' % (CPU_COUNT, CPU_COUNT))
  510. lines.append('make install || exit 1')
  511. lines.append('cd ../app')
  512. lines.append('rm -f bin/*')
  513. lines.append('export WKHTMLTOX_VERSION=%s' % version)
  514. lines.append('../qt/bin/qmake ../../../wkhtmltopdf.pro')
  515. lines.append('make -j%d || exit 1' % CPU_COUNT)
  516. lines.append('strip bin/wkhtmltopdf bin/wkhtmltoimage')
  517. lines.append('cp bin/wkhtmlto* ../wkhtmltox-%s/bin' % version)
  518. lines.append('cp -P bin/libwkhtmltox*.so.* ../wkhtmltox-%s/lib' % version)
  519. lines.append('cp ../../../include/wkhtmltox/*.h ../wkhtmltox-%s/include/wkhtmltox' % version)
  520. lines.append('cp ../../../include/wkhtmltox/dll*.inc ../wkhtmltox-%s/include/wkhtmltox' % version)
  521. lines.append('cd ..')
  522. lines.append('tar -c -v -f ../wkhtmltox-%s_linux-%s.tar wkhtmltox-%s/' % (version, config, version))
  523. lines.append('xz --compress -9 ../wkhtmltox-%s_linux-%s.tar' % (version, config))
  524. lines.append('# end of build script')
  525. open(script, 'w').write('\n'.join(lines))
  526. os.chdir(dir)
  527. shell('chmod +x build.sh')
  528. shell('schroot -c wkhtmltopdf-%s -- ./build.sh' % rchop(config, '-dbg'))
  529. # --------------------------------------------------------------- command line
  530. def usage(exit_code=2):
  531. print "Usage: scripts/build.py <target> [-clean] [-debug]\n\nThe supported targets are:\n",
  532. opts = list(BUILDERS.keys())
  533. opts.sort()
  534. for opt in opts:
  535. print '* %s' % opt
  536. sys.exit(exit_code)
  537. def main():
  538. basedir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'static-build')
  539. mkdir_p(basedir)
  540. if len(sys.argv) == 1:
  541. usage(0)
  542. config = sys.argv[1]
  543. if config not in BUILDERS:
  544. usage()
  545. for arg in sys.argv[2:]:
  546. if not arg in ['-clean', '-debug']:
  547. usage()
  548. final_config = config
  549. if '-debug' in sys.argv[2:]:
  550. # use the debug OpenSSL configuration if possible
  551. ssl = OPENSSL['build']
  552. for key in ssl:
  553. if fnmatch.fnmatch(config, key) and 'debug' in ssl[key]:
  554. ssl[key]['configure'] = ssl[key]['debug']
  555. # use a debug build of QT and WebKit
  556. cfg = QT_CONFIG['common']
  557. cfg[cfg.index('-release')] = '-debug'
  558. cfg[cfg.index('-webkit')] = '-webkit-debug'
  559. final_config += '-dbg'
  560. if '-clean' in sys.argv[2:]:
  561. rmdir(os.path.join(basedir, config))
  562. globals()['check_%s' % BUILDERS[config]](final_config)
  563. globals()['build_%s' % BUILDERS[config]](final_config, os.path.realpath(basedir))
  564. if __name__ == '__main__':
  565. main()