build.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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-mingw-w64': 'setup_mingw64',
  164. 'setup-schroot-centos5': 'setup_schroot',
  165. 'setup-schroot-centos6': 'setup_schroot',
  166. 'setup-schroot-wheezy': 'setup_schroot',
  167. 'setup-schroot-trusty': 'setup_schroot',
  168. 'setup-schroot-precise': 'setup_schroot',
  169. 'update-all-schroots': 'update_schroot',
  170. 'centos5-i386': 'linux_schroot',
  171. 'centos5-amd64': 'linux_schroot',
  172. 'centos6-i386': 'linux_schroot',
  173. 'centos6-amd64': 'linux_schroot',
  174. 'wheezy-i386': 'linux_schroot',
  175. 'wheezy-amd64': 'linux_schroot',
  176. 'trusty-i386': 'linux_schroot',
  177. 'trusty-amd64': 'linux_schroot',
  178. 'precise-i386': 'linux_schroot',
  179. 'precise-amd64': 'linux_schroot',
  180. 'mingw-w64-cross-win32': 'mingw64_cross',
  181. 'mingw-w64-cross-win64': 'mingw64_cross'
  182. }
  183. CHROOT_SETUP = {
  184. 'wheezy': [
  185. ('debootstrap', 'wheezy', 'http://ftp.us.debian.org/debian/'),
  186. ('write_file', 'etc/apt/sources.list', """
  187. deb http://ftp.debian.org/debian/ wheezy main contrib non-free
  188. deb http://ftp.debian.org/debian/ wheezy-updates main contrib non-free
  189. deb http://security.debian.org/ wheezy/updates main contrib non-free
  190. deb-src http://ftp.debian.org/debian/ wheezy main contrib non-free
  191. deb-src http://ftp.debian.org/debian/ wheezy-updates main contrib non-free
  192. deb-src http://security.debian.org/ wheezy/updates main contrib non-free"""),
  193. ('shell', 'apt-get update'),
  194. ('shell', 'apt-get dist-upgrade --assume-yes'),
  195. ('shell', 'apt-get install --assume-yes xz-utils'),
  196. ('shell', 'apt-get build-dep --assume-yes libqt4-core'),
  197. ('write_file', 'update.sh', 'apt-get update\napt-get dist-upgrade --assume-yes\n'),
  198. ('schroot_conf', 'Debian Wheezy')
  199. ],
  200. 'trusty': [
  201. ('debootstrap', 'trusty', 'http://archive.ubuntu.com/ubuntu/'),
  202. ('write_file', 'etc/apt/sources.list', """
  203. deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
  204. deb http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
  205. deb http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
  206. deb-src http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
  207. deb-src http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
  208. deb-src http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse"""),
  209. ('shell', 'apt-get update'),
  210. ('shell', 'apt-get dist-upgrade --assume-yes'),
  211. ('shell', 'apt-get install --assume-yes xz-utils'),
  212. ('shell', 'apt-get build-dep --assume-yes libqt4-core'),
  213. ('write_file', 'update.sh', 'apt-get update\napt-get dist-upgrade --assume-yes\n'),
  214. ('schroot_conf', 'Ubuntu Trusty')
  215. ],
  216. 'precise': [
  217. ('debootstrap', 'precise', 'http://archive.ubuntu.com/ubuntu/'),
  218. ('write_file', 'etc/apt/sources.list', """
  219. deb http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse
  220. deb http://archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse
  221. deb http://archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse
  222. deb-src http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse
  223. deb-src http://archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse
  224. deb-src http://archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse"""),
  225. ('shell', 'apt-get update'),
  226. ('shell', 'apt-get dist-upgrade --assume-yes'),
  227. ('shell', 'apt-get install --assume-yes xz-utils'),
  228. ('shell', 'apt-get build-dep --assume-yes libqt4-core'),
  229. ('write_file', 'update.sh', 'apt-get update\napt-get dist-upgrade --assume-yes\n'),
  230. ('schroot_conf', 'Ubuntu Precise')
  231. ],
  232. 'centos5': [
  233. ('rinse', 'centos-5'),
  234. ('shell', 'yum update -y'),
  235. ('append_file:amd64', 'etc/yum.conf', 'exclude = *.i?86\n'),
  236. ('shell', 'yum install -y gcc gcc-c++ make qt4-devel openssl-devel diffutils perl xz'),
  237. ('write_file', 'update.sh', 'yum update -y\n'),
  238. ('schroot_conf', 'CentOS 5')
  239. ],
  240. 'centos6': [
  241. ('rinse', 'centos-6'),
  242. ('shell', 'yum update -y'),
  243. ('append_file:amd64', 'etc/yum.conf', 'exclude = *.i?86\n'),
  244. ('shell', 'yum install -y gcc gcc-c++ make qt4-devel openssl-devel diffutils perl tar xz'),
  245. ('write_file', 'update.sh', 'yum update -y\n'),
  246. ('schroot_conf', 'CentOS 6')
  247. ]
  248. }
  249. # --------------------------------------------------------------- HELPERS
  250. import os, sys, platform, subprocess, shutil, re, fnmatch, multiprocessing
  251. from os.path import exists
  252. CPU_COUNT = max(2, multiprocessing.cpu_count()-1) # leave one CPU free
  253. def rchop(s, e):
  254. if s.endswith(e):
  255. return s[:-len(e)]
  256. return s
  257. def error(msg):
  258. print msg
  259. sys.exit(1)
  260. def shell(cmd):
  261. ret = os.system(cmd)
  262. if ret != 0:
  263. error("command failed: exit code %d" % ret)
  264. def get_output(*cmd):
  265. try:
  266. return subprocess.check_output(cmd, stderr=subprocess.STDOUT).strip()
  267. except subprocess.CalledProcessError:
  268. return None
  269. def rmdir(path):
  270. if exists(path):
  271. shutil.rmtree(path)
  272. def mkdir_p(path):
  273. if not exists(path):
  274. os.makedirs(path)
  275. def get_version(basedir):
  276. text = open(os.path.join(basedir, '..', 'VERSION'), 'r').read()
  277. if '-' not in text:
  278. return (text, text)
  279. version = text[:text.index('-')]
  280. os.chdir(os.path.join(basedir, '..'))
  281. hash = get_output('git', 'rev-parse', '--short', 'HEAD')
  282. if not hash:
  283. return (text, version)
  284. return ('%s-%s' % (version, hash), version)
  285. def build_openssl(config, basedir):
  286. cfg = None
  287. for key in OPENSSL['build']:
  288. if fnmatch.fnmatch(config, key):
  289. cfg = key
  290. if not cfg:
  291. return
  292. srcdir = os.path.join(basedir, 'openssl')
  293. dstdir = os.path.join(basedir, config, 'openssl')
  294. def is_compiled():
  295. compiled = exists(os.path.join(dstdir, 'include', 'openssl', 'ssl.h'))
  296. for lib in OPENSSL['build'][cfg]['libs']:
  297. compiled = compiled and exists(os.path.join(dstdir, 'lib', lib))
  298. return compiled
  299. if not exists(os.path.join(srcdir, '.git')):
  300. rmdir(srcdir)
  301. rmdir(dstdir)
  302. os.chdir(basedir)
  303. shell('git clone --branch %s --single-branch %s openssl' % (OPENSSL['branch'], OPENSSL['repository']))
  304. os.chdir(srcdir)
  305. shell('git clean -fdx')
  306. shell('git reset --hard HEAD')
  307. shell('git checkout %s' % (OPENSSL['tag']))
  308. if not is_compiled():
  309. opts = OPENSSL['build'][cfg]
  310. shell('perl Configure --openssldir=%s %s' % (dstdir, opts['configure']))
  311. for cmd in opts['build']:
  312. shell(cmd)
  313. shell('git clean -fdx')
  314. if not is_compiled():
  315. error("Unable to compile OpenSSL for your system, aborting.")
  316. return OPENSSL['build'][cfg]['os_libs']
  317. def check_running_on_debian():
  318. if not sys.platform.startswith('linux') or not exists('/etc/apt/sources.list'):
  319. error('This can only be run on a Debian/Ubuntu distribution, aborting.')
  320. if os.geteuid() != 0:
  321. error('This script must be run as root.')
  322. if platform.architecture()[0] == '64bit' and 'amd64' not in ARCH:
  323. ARCH.insert(0, 'amd64')
  324. PACKAGE_NAME = re.compile(r'ii\s+(.+?)\s+.*')
  325. def install_packages(*names):
  326. lines = get_output('dpkg-query', '--list', *names).split('\n')
  327. avail = [PACKAGE_NAME.match(line).group(1) for line in lines if PACKAGE_NAME.match(line)]
  328. if len(avail) != len(names):
  329. shell('apt-get update')
  330. shell('apt-get install --assume-yes %s' % (' '.join(names)))
  331. # --------------------------------------------------------------- Linux chroot
  332. ARCH = ['i386']
  333. def check_setup_schroot(config):
  334. check_running_on_debian()
  335. login = get_output('logname')
  336. if not login:
  337. error('Unable to determine the login for which schroot access is to be given.')
  338. if login == 'root':
  339. error('Please run via sudo to determine login for which schroot access is to be given.')
  340. def build_setup_schroot(config, basedir):
  341. install_packages('git', 'debootstrap', 'schroot', 'rinse')
  342. login = get_output('logname')
  343. chroot = config[1+config.rindex('-'):]
  344. for arch in ARCH:
  345. print '******************* %s-%s' % (chroot, arch)
  346. root_dir = '/opt/wkhtmltopdf-build/%s-%s' % (chroot, arch)
  347. rmdir(root_dir)
  348. mkdir_p(root_dir)
  349. for command in CHROOT_SETUP[chroot]:
  350. # handle architecture-specific commands
  351. name = command[0]
  352. if ':' in name:
  353. if name[1+name.rindex(':'):] != arch:
  354. continue
  355. else:
  356. name = name[:name.rindex(':')]
  357. # handle commands
  358. if name == 'debootstrap':
  359. shell('debootstrap --arch=%(arch)s --variant=buildd %(distro)s %(dir)s %(url)s' % {
  360. 'arch': arch, 'dir': root_dir, 'distro': command[1], 'url': command[2] })
  361. elif name == 'rinse':
  362. cmd = (arch == 'i386' and 'linux32 rinse' or 'rinse')
  363. shell('%s --arch %s --distribution %s --directory %s' % (cmd, arch, command[1], root_dir))
  364. elif name == 'shell':
  365. cmd = (arch == 'i386' and 'linux32 chroot' or 'chroot')
  366. shell('%s %s %s' % (cmd, root_dir, command[1]))
  367. elif name == 'write_file':
  368. open(os.path.join(root_dir, command[1]), 'w').write(command[2].strip())
  369. elif name == 'append_file':
  370. open(os.path.join(root_dir, command[1]), 'a').write(command[2].strip())
  371. elif name == 'schroot_conf':
  372. cfg = open('/etc/schroot/chroot.d/wkhtmltopdf-%s-%s' % (chroot, arch), 'w')
  373. cfg.write('[wkhtmltopdf-%s-%s]\n' % (chroot, arch))
  374. cfg.write('type=directory\ndirectory=%s/\n' % root_dir)
  375. cfg.write('description=%s %s for wkhtmltopdf\n' % (command[1], arch))
  376. cfg.write('users=%s\nroot-users=root\n' % login)
  377. if arch == 'i386' and 'amd64' in ARCH:
  378. cfg.write('personality=linux32\n')
  379. cfg.close()
  380. def check_update_schroot(config):
  381. check_running_on_debian()
  382. if not get_output('schroot', '--list'):
  383. error('Unable to determine the list of available schroots.')
  384. def build_update_schroot(config, basedir):
  385. for name in get_output('schroot', '--list').split('\n'):
  386. print '******************* %s' % name[name.index('wkhtmltopdf-'):]
  387. shell('schroot -c %s -- /bin/bash /update.sh' % name[name.index('wkhtmltopdf-'):])
  388. def check_setup_mingw64(config):
  389. check_running_on_debian()
  390. def build_setup_mingw64(config, basedir):
  391. install_packages('build-essential', 'mingw-w64', 'nsis')
  392. # --------------------------------------------------------------- MSVC (2008-2013)
  393. MSVC_LOCATION = {
  394. 'msvc2008': 'VS90COMNTOOLS',
  395. 'msvc2010': 'VS100COMNTOOLS',
  396. 'msvc2012': 'VS110COMNTOOLS',
  397. 'msvc2013': 'VS120COMNTOOLS'
  398. }
  399. def check_msvc(config):
  400. version, arch = rchop(config, '-dbg').split('-')
  401. env_var = MSVC_LOCATION[version]
  402. if not env_var in os.environ:
  403. error("%s does not seem to be installed." % version)
  404. vcdir = os.path.join(os.environ[env_var], '..', '..', 'VC')
  405. if not exists(os.path.join(vcdir, 'vcvarsall.bat')):
  406. error("%s: unable to find vcvarsall.bat" % version)
  407. if arch == 'win32' and not exists(os.path.join(vcdir, 'bin', 'cl.exe')):
  408. error("%s: unable to find the x86 compiler" % version)
  409. if arch == 'win64' and not exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')) \
  410. and not exists(os.path.join(vcdir, 'bin', 'x86_amd64', 'cl.exe')):
  411. error("%s: unable to find the amd64 compiler" % version)
  412. def build_msvc(config, basedir):
  413. msvc, arch = rchop(config, '-dbg').split('-')
  414. vcdir = os.path.join(os.environ[MSVC_LOCATION[msvc]], '..', '..', 'VC')
  415. vcarg = 'x86'
  416. if arch == 'win64':
  417. if exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')):
  418. vcarg = 'amd64'
  419. else:
  420. vcarg = 'x86_amd64'
  421. python = sys.executable
  422. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os; print repr(os.environ)"' % (
  423. os.path.join(vcdir, 'vcvarsall.bat'), vcarg, python), stdout=subprocess.PIPE, shell=True)
  424. stdout, _ = process.communicate()
  425. exitcode = process.wait()
  426. if exitcode != 0:
  427. error("%s: unable to initialize the environment" % msvc)
  428. os.environ.update(eval(stdout.strip()))
  429. build_msvc_common(config, basedir)
  430. # --------------------------------------------------------------- MSVC via Windows SDK 7.1
  431. def check_msvc_winsdk71(config):
  432. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  433. if pfile in os.environ and exists(os.path.join(os.environ[pfile], 'Microsoft SDKs', 'Windows', 'v7.1', 'Bin', 'SetEnv.cmd')):
  434. return
  435. error("Unable to detect the location of Windows SDK 7.1")
  436. def build_msvc_winsdk71(config, basedir):
  437. arch = config[config.rindex('-'):]
  438. setenv = None
  439. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  440. if not pfile in os.environ:
  441. continue
  442. setenv = os.path.join(os.environ[pfile], 'Microsoft SDKs', 'Windows', 'v7.1', 'Bin', 'SetEnv.cmd')
  443. mode = debug and '/Debug' or '/Release'
  444. if arch == 'win64':
  445. args = '/2008 /x64 %s' % mode
  446. else:
  447. args = '/2008 /x86 %s' % mode
  448. python = sys.executable
  449. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os; print repr(os.environ)"' % (
  450. setenv, args, python), stdout=subprocess.PIPE, shell=True)
  451. stdout, _ = process.communicate()
  452. exitcode = process.wait()
  453. if exitcode != 0:
  454. error("unable to initialize the environment for Windows SDK 7.1")
  455. os.environ.update(eval(stdout.strip()))
  456. build_msvc_common(config, basedir)
  457. def build_msvc_common(config, basedir):
  458. version, simple_version = get_version(basedir)
  459. ssl_libs = build_openssl(config, basedir)
  460. ssldir = os.path.join(basedir, config, 'openssl')
  461. qtdir = os.path.join(basedir, config, 'qt')
  462. mkdir_p(qtdir)
  463. args = []
  464. args.extend(QT_CONFIG['common'])
  465. args.extend(QT_CONFIG['msvc'])
  466. args.append('-I %s\\include' % ssldir)
  467. args.append('-L %s\\lib' % ssldir)
  468. args.append('OPENSSL_LIBS="-L%s -lssleay32 -llibeay32 %s"' % \
  469. (ssldir.replace('\\', '\\\\'), ssl_libs))
  470. os.chdir(qtdir)
  471. if not exists('is_configured'):
  472. shell('%s\\..\\qt\\configure.exe %s' % (basedir, ' '.join(args)))
  473. open('is_configured', 'w').write('')
  474. shell('nmake')
  475. appdir = os.path.join(basedir, config, 'app')
  476. mkdir_p(appdir)
  477. os.chdir(appdir)
  478. rmdir('bin')
  479. mkdir_p('bin')
  480. os.environ['WKHTMLTOX_VERSION'] = version
  481. shell('%s\\bin\\qmake %s\\..\\wkhtmltopdf.pro' % (qtdir, basedir))
  482. shell('nmake')
  483. found = False
  484. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  485. if not pfile in os.environ or not exists(os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')):
  486. continue
  487. found = True
  488. makensis = os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')
  489. os.chdir(os.path.join(basedir, '..'))
  490. shell('"%s" /DVERSION=%s /DSIMPLE_VERSION=%s /DTARGET=%s wkhtmltox.nsi' % \
  491. (makensis, version, simple_version, config))
  492. if not found:
  493. print "\n\nCould not build installer as NSIS was not found."
  494. # ------------------------------------------------ MinGW-W64 Cross Environment
  495. MINGW_W64_PREFIX = {
  496. 'mingw-w64-cross-win32' : 'i686-w64-mingw32',
  497. 'mingw-w64-cross-win64' : 'x86_64-w64-mingw32',
  498. }
  499. def check_mingw64_cross(config):
  500. shell('%s-gcc --version' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  501. def build_mingw64_cross(config, basedir):
  502. version, simple_version = get_version(basedir)
  503. ssl_libs = build_openssl(config, basedir)
  504. ssldir = os.path.join(basedir, config, 'openssl')
  505. build = os.path.join(basedir, config, 'qt_build')
  506. qtdir = os.path.join(basedir, config, 'qt')
  507. mkdir_p(build)
  508. args = []
  509. args.extend(QT_CONFIG['common'])
  510. args.extend(QT_CONFIG['mingw-w64-cross'])
  511. args.append('--prefix=%s' % qtdir)
  512. args.append('-I %s/include' % ssldir)
  513. args.append('-L %s/lib' % ssldir)
  514. args.append('-device-option CROSS_COMPILE=%s-' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  515. os.environ['OPENSSL_LIBS'] = '-lssl -lcrypto -L %s/lib %s' % (ssldir, ssl_libs)
  516. os.chdir(build)
  517. if not exists('is_configured'):
  518. shell('%s/../qt/configure %s' % (basedir, ' '.join(args)))
  519. shell('touch is_configured')
  520. shell('make -j%d' % CPU_COUNT)
  521. shell('make install')
  522. appdir = os.path.join(basedir, config, 'app')
  523. mkdir_p(appdir)
  524. os.chdir(appdir)
  525. shell('rm -f bin/*')
  526. # set up cross compiling prefix correctly (isn't set by make install)
  527. os.environ['QTDIR'] = qtdir
  528. os.environ['WKHTMLTOX_VERSION'] = version
  529. shell('%s/bin/qmake -set CROSS_COMPILE %s-' % (qtdir, MINGW_W64_PREFIX[rchop(config, '-dbg')]))
  530. shell('%s/bin/qmake -spec win32-g++-4.6 %s/../wkhtmltopdf.pro' % (qtdir, basedir))
  531. shell('make')
  532. shutil.copy('bin/libwkhtmltox0.a', 'bin/wkhtmltox.lib')
  533. os.chdir(os.path.join(basedir, '..'))
  534. shell('makensis -DVERSION=%s -DSIMPLE_VERSION=%s -DTARGET=%s wkhtmltox.nsi' % \
  535. (version, simple_version, config))
  536. # -------------------------------------------------- Linux schroot environment
  537. def check_linux_schroot(config):
  538. shell('schroot -c wkhtmltopdf-%s -- gcc --version' % rchop(config, '-dbg'))
  539. def build_linux_schroot(config, basedir):
  540. version, simple_version = get_version(basedir)
  541. dir = os.path.join(basedir, config)
  542. script = os.path.join(dir, 'build.sh')
  543. dist = os.path.join(dir, 'wkhtmltox-%s' % version)
  544. mkdir_p(os.path.join(dir, 'qt_build'))
  545. mkdir_p(os.path.join(dir, 'app'))
  546. rmdir(dist)
  547. mkdir_p(os.path.join(dist, 'bin'))
  548. mkdir_p(os.path.join(dist, 'include', 'wkhtmltox'))
  549. mkdir_p(os.path.join(dist, 'lib'))
  550. args = []
  551. args.extend(QT_CONFIG['common'])
  552. args.extend(QT_CONFIG['posix'])
  553. args.append('--prefix=../qt')
  554. lines = ['#!/bin/bash']
  555. lines.append('# start of autogenerated build script')
  556. lines.append('cd qt_build')
  557. if config == 'centos5-i386':
  558. lines.append('export CFLAGS=-march=i486')
  559. lines.append('export CXXFLAGS=-march=i486')
  560. lines.append('if [ ! -f is_configured ]; then')
  561. lines.append(' ../../../qt/configure %s || exit 1' % ' '.join(args))
  562. lines.append(' touch is_configured')
  563. lines.append('fi')
  564. lines.append('if ! make -j%d -q; then\n make -j%d || exit 1\nfi' % (CPU_COUNT, CPU_COUNT))
  565. lines.append('make install || exit 1')
  566. lines.append('cd ../app')
  567. lines.append('rm -f bin/*')
  568. lines.append('export WKHTMLTOX_VERSION=%s' % version)
  569. lines.append('../qt/bin/qmake ../../../wkhtmltopdf.pro')
  570. lines.append('make -j%d || exit 1' % CPU_COUNT)
  571. lines.append('strip bin/wkhtmltopdf bin/wkhtmltoimage')
  572. lines.append('cp bin/wkhtmlto* ../wkhtmltox-%s/bin' % version)
  573. lines.append('cp -P bin/libwkhtmltox*.so.* ../wkhtmltox-%s/lib' % version)
  574. lines.append('cp ../../../include/wkhtmltox/*.h ../wkhtmltox-%s/include/wkhtmltox' % version)
  575. lines.append('cp ../../../include/wkhtmltox/dll*.inc ../wkhtmltox-%s/include/wkhtmltox' % version)
  576. lines.append('cd ..')
  577. lines.append('tar -c -v -f ../wkhtmltox-%s_linux-%s.tar wkhtmltox-%s/' % (version, config, version))
  578. lines.append('xz --compress -9 ../wkhtmltox-%s_linux-%s.tar' % (version, config))
  579. lines.append('# end of build script')
  580. open(script, 'w').write('\n'.join(lines))
  581. os.chdir(dir)
  582. shell('chmod +x build.sh')
  583. shell('schroot -c wkhtmltopdf-%s -- ./build.sh' % rchop(config, '-dbg'))
  584. # --------------------------------------------------------------- command line
  585. def usage(exit_code=2):
  586. print "Usage: scripts/build.py <target> [-clean] [-debug]\n\nThe supported targets are:\n",
  587. opts = list(BUILDERS.keys())
  588. opts.sort()
  589. for opt in opts:
  590. print '* %s' % opt
  591. sys.exit(exit_code)
  592. def main():
  593. basedir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'static-build')
  594. mkdir_p(basedir)
  595. if len(sys.argv) == 1:
  596. usage(0)
  597. config = sys.argv[1]
  598. if config not in BUILDERS:
  599. usage()
  600. for arg in sys.argv[2:]:
  601. if not arg in ['-clean', '-debug']:
  602. usage()
  603. final_config = config
  604. if '-debug' in sys.argv[2:]:
  605. # use the debug OpenSSL configuration if possible
  606. ssl = OPENSSL['build']
  607. for key in ssl:
  608. if fnmatch.fnmatch(config, key) and 'debug' in ssl[key]:
  609. ssl[key]['configure'] = ssl[key]['debug']
  610. # use a debug build of QT and WebKit
  611. cfg = QT_CONFIG['common']
  612. cfg[cfg.index('-release')] = '-debug'
  613. cfg[cfg.index('-webkit')] = '-webkit-debug'
  614. final_config += '-dbg'
  615. if '-clean' in sys.argv[2:]:
  616. rmdir(os.path.join(basedir, config))
  617. globals()['check_%s' % BUILDERS[config]](final_config)
  618. globals()['build_%s' % BUILDERS[config]](final_config, os.path.realpath(basedir))
  619. if __name__ == '__main__':
  620. main()