build.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. 'url' : 'http://www.openssl.org/source/openssl-1.0.1g.tar.gz',
  22. 'sha1' : 'b28b3bcb1dc3ee7b55024c9f795be60eb3183e3c',
  23. 'build' : {
  24. 'msvc*-win32*': {
  25. 'configure' : 'VC-WIN32 no-asm',
  26. 'debug' : 'debug-VC-WIN32 no-asm',
  27. 'build' : ['ms\\do_ms.bat', 'nmake /f ms\\nt.mak install'],
  28. 'libs' : ['ssleay32.lib', 'libeay32.lib'],
  29. 'os_libs' : '-lUser32 -lAdvapi32 -lGdi32 -lCrypt32'
  30. },
  31. 'msvc*-win64*': {
  32. 'configure' : 'VC-WIN64A',
  33. 'debug' : 'debug-VC-WIN64A',
  34. 'build' : ['ms\\do_win64a.bat', 'nmake /f ms\\nt.mak install'],
  35. 'libs' : ['ssleay32.lib', 'libeay32.lib'],
  36. 'os_libs' : '-lUser32 -lAdvapi32 -lGdi32 -lCrypt32'
  37. },
  38. 'mingw-w64-cross-win32': {
  39. 'configure' : '--cross-compile-prefix=i686-w64-mingw32- no-shared no-asm mingw64',
  40. 'build' : ['make', 'make install_sw'],
  41. 'libs' : ['libssl.a', 'libcrypto.a'],
  42. 'os_libs' : '-lws2_32 -lgdi32 -lcrypt32'
  43. },
  44. 'mingw-w64-cross-win64': {
  45. 'configure' : '--cross-compile-prefix=x86_64-w64-mingw32- no-shared no-asm mingw64',
  46. 'build' : ['make', 'make install_sw'],
  47. 'libs' : ['libssl.a', 'libcrypto.a'],
  48. 'os_libs' : '-lws2_32 -lgdi32 -lcrypt32'
  49. }
  50. }
  51. }
  52. QT_CONFIG = {
  53. 'common' : [
  54. '-opensource',
  55. '-confirm-license',
  56. '-fast',
  57. '-release',
  58. '-static',
  59. '-graphicssystem raster',
  60. '-webkit',
  61. '-exceptions', # required by XmlPatterns
  62. '-xmlpatterns', # required for TOC support
  63. '-qt-zlib', # use bundled versions of libraries
  64. '-qt-libpng',
  65. '-qt-libjpeg',
  66. '-no-libmng',
  67. '-no-libtiff',
  68. '-no-accessibility',
  69. '-no-stl',
  70. '-no-qt3support',
  71. '-no-phonon',
  72. '-no-phonon-backend',
  73. '-no-opengl',
  74. '-no-declarative',
  75. '-no-scripttools',
  76. '-no-sql-ibase',
  77. '-no-sql-mysql',
  78. '-no-sql-odbc',
  79. '-no-sql-psql',
  80. '-no-sql-sqlite',
  81. '-no-sql-sqlite2',
  82. '-no-mmx',
  83. '-no-3dnow',
  84. '-no-sse',
  85. '-no-sse2',
  86. '-no-multimedia',
  87. '-nomake demos',
  88. '-nomake docs',
  89. '-nomake examples',
  90. '-nomake tools',
  91. '-nomake tests',
  92. '-nomake translations'
  93. ],
  94. 'msvc': [
  95. '-mp',
  96. '-no-script',
  97. '-qt-style-windows',
  98. '-qt-style-cleanlooks',
  99. '-no-style-windowsxp',
  100. '-no-style-windowsvista',
  101. '-no-style-plastique',
  102. '-no-style-motif',
  103. '-no-style-cde',
  104. '-openssl-linked' # static linkage for OpenSSL
  105. ],
  106. 'posix': [
  107. '-silent', # perform a silent build
  108. '-script', # "make install" does not copy QtScript/qscriptengine.h
  109. '-xrender', # xrender support is required
  110. '-largefile',
  111. '-rpath',
  112. '-openssl', # load OpenSSL binaries at runtime
  113. '-no-dbus',
  114. '-no-nis',
  115. '-no-cups',
  116. '-no-iconv',
  117. '-no-pch',
  118. '-no-gtkstyle',
  119. '-no-nas-sound',
  120. '-no-sm',
  121. '-no-xshape',
  122. '-no-xinerama',
  123. '-no-xcursor',
  124. '-no-xfixes',
  125. '-no-xrandr',
  126. '-no-mitshm',
  127. '-no-xinput',
  128. '-no-xkb',
  129. '-no-glib',
  130. '-no-gstreamer',
  131. '-D ENABLE_VIDEO=0', # required as otherwise gstreamer gets linked in
  132. '-no-openvg',
  133. '-no-xsync',
  134. '-no-audio-backend',
  135. '-no-sse3',
  136. '-no-ssse3',
  137. '-no-sse4.1',
  138. '-no-sse4.2',
  139. '-no-avx',
  140. '-no-neon'
  141. ],
  142. 'mingw-w64-cross' : [
  143. '-silent', # perform a silent build
  144. '-script', # "make install" does not copy QtScript/qscriptengine.h
  145. '-openssl-linked', # static linkage for OpenSSL
  146. '-no-reduce-exports',
  147. '-no-rpath',
  148. '-xplatform win32-g++-4.6'
  149. ]
  150. }
  151. BUILDERS = {
  152. 'msvc2008-win32': 'msvc',
  153. 'msvc2008-win64': 'msvc',
  154. 'msvc2010-win32': 'msvc',
  155. 'msvc2010-win64': 'msvc',
  156. 'msvc2012-win32': 'msvc',
  157. 'msvc2012-win64': 'msvc',
  158. 'msvc2013-win32': 'msvc',
  159. 'msvc2013-win64': 'msvc',
  160. 'msvc-winsdk71-win32': 'msvc_winsdk71',
  161. 'msvc-winsdk71-win64': 'msvc_winsdk71',
  162. 'setup-mingw-w64': 'setup_mingw64',
  163. 'setup-schroot-centos5': 'setup_schroot',
  164. 'setup-schroot-centos6': 'setup_schroot',
  165. 'setup-schroot-wheezy': 'setup_schroot',
  166. 'setup-schroot-trusty': 'setup_schroot',
  167. 'setup-schroot-precise': 'setup_schroot',
  168. 'update-all-schroots': 'update_schroot',
  169. 'centos5-i386': 'linux_schroot',
  170. 'centos5-amd64': 'linux_schroot',
  171. 'centos6-i386': 'linux_schroot',
  172. 'centos6-amd64': 'linux_schroot',
  173. 'wheezy-i386': 'linux_schroot',
  174. 'wheezy-amd64': 'linux_schroot',
  175. 'trusty-i386': 'linux_schroot',
  176. 'trusty-amd64': 'linux_schroot',
  177. 'precise-i386': 'linux_schroot',
  178. 'precise-amd64': 'linux_schroot',
  179. 'mingw-w64-cross-win32': 'mingw64_cross',
  180. 'mingw-w64-cross-win64': 'mingw64_cross',
  181. 'posix-local': 'posix_local'
  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, urllib, hashlib, tarfile
  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 qt_config(key, *opts):
  286. input, output = [], []
  287. input.extend(QT_CONFIG['common'])
  288. input.extend(QT_CONFIG[key])
  289. input.extend(opts)
  290. cfg = os.environ.get('WKHTMLTOX_QT_CONFIG')
  291. if cfg:
  292. input.extend(cfg.split())
  293. for arg in input:
  294. if not arg.startswith('remove:-'):
  295. output.append(arg)
  296. elif arg[1+arg.index(':'):] in output:
  297. output.remove(arg[1+arg.index(':'):])
  298. return ' '.join(output)
  299. def download_file(url, dir, sha1):
  300. name = url.split('/')[-1]
  301. loc = os.path.join(dir, name)
  302. if os.path.exists(loc):
  303. hash = hashlib.sha1(open(loc, 'rb').read()).hexdigest()
  304. if hash != sha1:
  305. error('Checksum mismatch for %s' % name)
  306. os.remove(loc)
  307. return loc
  308. def hook(cnt, bs, total):
  309. pct = int(cnt*bs*100/total)
  310. sys.stdout.write("\rDownloading: %s [%d%%]" % (name, pct))
  311. sys.stdout.flush()
  312. urllib.urlretrieve(url, loc, reporthook=hook)
  313. sys.stdout.write("\r")
  314. sys.stdout.flush()
  315. hash = hashlib.sha1(open(loc, 'rb').read()).hexdigest()
  316. if hash != sha1:
  317. error('Checksum mismatch for %s' % name)
  318. os.remove(loc)
  319. sys.stdout.write("\rDownloaded: %s [checksum OK]\n" % name)
  320. sys.stdout.flush()
  321. return loc
  322. def build_openssl(config, basedir):
  323. cfg = None
  324. for key in OPENSSL['build']:
  325. if fnmatch.fnmatch(config, key):
  326. cfg = key
  327. if not cfg:
  328. return
  329. dstdir = os.path.join(basedir, config, 'openssl')
  330. location = download_file(OPENSSL['url'], basedir, OPENSSL['sha1'])
  331. relname = os.path.basename(location)[:os.path.basename(location).index('.tar')]
  332. srcdir = os.path.join(basedir, relname)
  333. def is_compiled():
  334. compiled = exists(os.path.join(dstdir, 'include', 'openssl', 'ssl.h'))
  335. for lib in OPENSSL['build'][cfg]['libs']:
  336. compiled = compiled and exists(os.path.join(dstdir, 'lib', lib))
  337. return compiled
  338. if not is_compiled():
  339. rmdir(srcdir)
  340. tarfile.open(location).extractall(basedir)
  341. os.chdir(srcdir)
  342. opts = OPENSSL['build'][cfg]
  343. shell('perl Configure --openssldir=%s %s' % (dstdir, opts['configure']))
  344. for cmd in opts['build']:
  345. shell(cmd)
  346. if not is_compiled():
  347. error("Unable to compile OpenSSL for your system, aborting.")
  348. return OPENSSL['build'][cfg]['os_libs']
  349. def check_running_on_debian():
  350. if not sys.platform.startswith('linux') or not exists('/etc/apt/sources.list'):
  351. error('This can only be run on a Debian/Ubuntu distribution, aborting.')
  352. if os.geteuid() != 0:
  353. error('This script must be run as root.')
  354. if platform.architecture()[0] == '64bit' and 'amd64' not in ARCH:
  355. ARCH.insert(0, 'amd64')
  356. PACKAGE_NAME = re.compile(r'ii\s+(.+?)\s+.*')
  357. def install_packages(*names):
  358. lines = get_output('dpkg-query', '--list', *names).split('\n')
  359. avail = [PACKAGE_NAME.match(line).group(1) for line in lines if PACKAGE_NAME.match(line)]
  360. if len(avail) != len(names):
  361. shell('apt-get update')
  362. shell('apt-get install --assume-yes %s' % (' '.join(names)))
  363. # --------------------------------------------------------------- Linux chroot
  364. ARCH = ['i386']
  365. def check_setup_schroot(config):
  366. check_running_on_debian()
  367. login = get_output('logname') or os.environ.get('SUDO_USER')
  368. if not login:
  369. error('Unable to determine the login for which schroot access is to be given.')
  370. if login == 'root':
  371. error('Please run via sudo to determine login for which schroot access is to be given.')
  372. def build_setup_schroot(config, basedir):
  373. install_packages('git', 'debootstrap', 'schroot', 'rinse')
  374. login = get_output('logname') or os.environ.get('SUDO_USER')
  375. chroot = config[1+config.rindex('-'):]
  376. for arch in ARCH:
  377. print '******************* %s-%s' % (chroot, arch)
  378. root_dir = '/opt/wkhtmltopdf-build/%s-%s' % (chroot, arch)
  379. rmdir(root_dir)
  380. mkdir_p(root_dir)
  381. for command in CHROOT_SETUP[chroot]:
  382. # handle architecture-specific commands
  383. name = command[0]
  384. if ':' in name:
  385. if name[1+name.rindex(':'):] != arch:
  386. continue
  387. else:
  388. name = name[:name.rindex(':')]
  389. # handle commands
  390. if name == 'debootstrap':
  391. shell('debootstrap --arch=%(arch)s --variant=buildd %(distro)s %(dir)s %(url)s' % {
  392. 'arch': arch, 'dir': root_dir, 'distro': command[1], 'url': command[2] })
  393. elif name == 'rinse':
  394. cmd = (arch == 'i386' and 'linux32 rinse' or 'rinse')
  395. shell('%s --arch %s --distribution %s --directory %s' % (cmd, arch, command[1], root_dir))
  396. elif name == 'shell':
  397. cmd = (arch == 'i386' and 'linux32 chroot' or 'chroot')
  398. shell('%s %s %s' % (cmd, root_dir, command[1]))
  399. elif name == 'write_file':
  400. open(os.path.join(root_dir, command[1]), 'w').write(command[2].strip())
  401. elif name == 'append_file':
  402. open(os.path.join(root_dir, command[1]), 'a').write(command[2].strip())
  403. elif name == 'schroot_conf':
  404. cfg = open('/etc/schroot/chroot.d/wkhtmltopdf-%s-%s' % (chroot, arch), 'w')
  405. cfg.write('[wkhtmltopdf-%s-%s]\n' % (chroot, arch))
  406. cfg.write('type=directory\ndirectory=%s/\n' % root_dir)
  407. cfg.write('description=%s %s for wkhtmltopdf\n' % (command[1], arch))
  408. cfg.write('users=%s\nroot-users=root\n' % login)
  409. if arch == 'i386' and 'amd64' in ARCH:
  410. cfg.write('personality=linux32\n')
  411. cfg.close()
  412. def check_update_schroot(config):
  413. check_running_on_debian()
  414. if not get_output('schroot', '--list'):
  415. error('Unable to determine the list of available schroots.')
  416. def build_update_schroot(config, basedir):
  417. for name in get_output('schroot', '--list').split('\n'):
  418. print '******************* %s' % name[name.index('wkhtmltopdf-'):]
  419. shell('schroot -c %s -- /bin/bash /update.sh' % name[name.index('wkhtmltopdf-'):])
  420. def check_setup_mingw64(config):
  421. check_running_on_debian()
  422. def build_setup_mingw64(config, basedir):
  423. install_packages('build-essential', 'mingw-w64', 'nsis')
  424. # --------------------------------------------------------------- MSVC (2008-2013)
  425. MSVC_LOCATION = {
  426. 'msvc2008': 'VS90COMNTOOLS',
  427. 'msvc2010': 'VS100COMNTOOLS',
  428. 'msvc2012': 'VS110COMNTOOLS',
  429. 'msvc2013': 'VS120COMNTOOLS'
  430. }
  431. def check_msvc(config):
  432. version, arch = rchop(config, '-dbg').split('-')
  433. env_var = MSVC_LOCATION[version]
  434. if not env_var in os.environ:
  435. error("%s does not seem to be installed." % version)
  436. vcdir = os.path.join(os.environ[env_var], '..', '..', 'VC')
  437. if not exists(os.path.join(vcdir, 'vcvarsall.bat')):
  438. error("%s: unable to find vcvarsall.bat" % version)
  439. if arch == 'win32' and not exists(os.path.join(vcdir, 'bin', 'cl.exe')):
  440. error("%s: unable to find the x86 compiler" % version)
  441. if arch == 'win64' and not exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')) \
  442. and not exists(os.path.join(vcdir, 'bin', 'x86_amd64', 'cl.exe')):
  443. error("%s: unable to find the amd64 compiler" % version)
  444. def build_msvc(config, basedir):
  445. msvc, arch = rchop(config, '-dbg').split('-')
  446. vcdir = os.path.join(os.environ[MSVC_LOCATION[msvc]], '..', '..', 'VC')
  447. vcarg = 'x86'
  448. if arch == 'win64':
  449. if exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')):
  450. vcarg = 'amd64'
  451. else:
  452. vcarg = 'x86_amd64'
  453. python = sys.executable
  454. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os; print repr(os.environ)"' % (
  455. os.path.join(vcdir, 'vcvarsall.bat'), vcarg, python), stdout=subprocess.PIPE, shell=True)
  456. stdout, _ = process.communicate()
  457. exitcode = process.wait()
  458. if exitcode != 0:
  459. error("%s: unable to initialize the environment" % msvc)
  460. os.environ.update(eval(stdout.strip()))
  461. build_msvc_common(config, basedir)
  462. # --------------------------------------------------------------- MSVC via Windows SDK 7.1
  463. def check_msvc_winsdk71(config):
  464. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  465. if pfile in os.environ and exists(os.path.join(os.environ[pfile], 'Microsoft SDKs', 'Windows', 'v7.1', 'Bin', 'SetEnv.cmd')):
  466. return
  467. error("Unable to detect the location of Windows SDK 7.1")
  468. def build_msvc_winsdk71(config, basedir):
  469. arch = config[config.rindex('-'):]
  470. setenv = None
  471. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  472. if not pfile in os.environ:
  473. continue
  474. setenv = os.path.join(os.environ[pfile], 'Microsoft SDKs', 'Windows', 'v7.1', 'Bin', 'SetEnv.cmd')
  475. mode = debug and '/Debug' or '/Release'
  476. if arch == 'win64':
  477. args = '/2008 /x64 %s' % mode
  478. else:
  479. args = '/2008 /x86 %s' % mode
  480. python = sys.executable
  481. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os; print repr(os.environ)"' % (
  482. setenv, args, python), stdout=subprocess.PIPE, shell=True)
  483. stdout, _ = process.communicate()
  484. exitcode = process.wait()
  485. if exitcode != 0:
  486. error("unable to initialize the environment for Windows SDK 7.1")
  487. os.environ.update(eval(stdout.strip()))
  488. build_msvc_common(config, basedir)
  489. def build_msvc_common(config, basedir):
  490. version, simple_version = get_version(basedir)
  491. ssl_libs = build_openssl(config, basedir)
  492. ssldir = os.path.join(basedir, config, 'openssl')
  493. qtdir = os.path.join(basedir, config, 'qt')
  494. mkdir_p(qtdir)
  495. configure_args = qt_config('msvc',
  496. '-I %s\\include' % ssldir,
  497. '-L %s\\lib' % ssldir,
  498. 'OPENSSL_LIBS="-L%s -lssleay32 -llibeay32 %s"' % (ssldir.replace('\\', '\\\\'), ssl_libs))
  499. os.chdir(qtdir)
  500. if not exists('is_configured'):
  501. shell('%s\\..\\qt\\configure.exe %s' % (basedir, configure_args))
  502. open('is_configured', 'w').write('')
  503. shell('nmake')
  504. appdir = os.path.join(basedir, config, 'app')
  505. mkdir_p(appdir)
  506. os.chdir(appdir)
  507. rmdir('bin')
  508. mkdir_p('bin')
  509. os.environ['WKHTMLTOX_VERSION'] = version
  510. shell('%s\\bin\\qmake %s\\..\\wkhtmltopdf.pro' % (qtdir, basedir))
  511. shell('nmake')
  512. found = False
  513. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  514. if not pfile in os.environ or not exists(os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')):
  515. continue
  516. found = True
  517. makensis = os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')
  518. os.chdir(os.path.join(basedir, '..'))
  519. shell('"%s" /DVERSION=%s /DSIMPLE_VERSION=%s /DTARGET=%s wkhtmltox.nsi' % \
  520. (makensis, version, simple_version, config))
  521. if not found:
  522. print "\n\nCould not build installer as NSIS was not found."
  523. # ------------------------------------------------ MinGW-W64 Cross Environment
  524. MINGW_W64_PREFIX = {
  525. 'mingw-w64-cross-win32' : 'i686-w64-mingw32',
  526. 'mingw-w64-cross-win64' : 'x86_64-w64-mingw32',
  527. }
  528. def check_mingw64_cross(config):
  529. shell('%s-gcc --version' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  530. def build_mingw64_cross(config, basedir):
  531. version, simple_version = get_version(basedir)
  532. ssl_libs = build_openssl(config, basedir)
  533. ssldir = os.path.join(basedir, config, 'openssl')
  534. build = os.path.join(basedir, config, 'qt_build')
  535. qtdir = os.path.join(basedir, config, 'qt')
  536. mkdir_p(build)
  537. configure_args = qt_config('mingw-w64-cross',
  538. '--prefix=%s' % qtdir,
  539. '-I %s/include' % ssldir,
  540. '-L %s/lib' % ssldir,
  541. '-device-option CROSS_COMPILE=%s-' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  542. os.environ['OPENSSL_LIBS'] = '-lssl -lcrypto -L %s/lib %s' % (ssldir, ssl_libs)
  543. os.chdir(build)
  544. if not exists('is_configured'):
  545. for var in ['CFLAGS', 'CXXFLAGS']:
  546. os.environ[var] = '-w'
  547. shell('%s/../qt/configure %s' % (basedir, configure_args))
  548. shell('touch is_configured')
  549. shell('make -j%d' % CPU_COUNT)
  550. shell('make install')
  551. appdir = os.path.join(basedir, config, 'app')
  552. mkdir_p(appdir)
  553. os.chdir(appdir)
  554. shell('rm -f bin/*')
  555. # set up cross compiling prefix correctly (isn't set by make install)
  556. os.environ['QTDIR'] = qtdir
  557. os.environ['WKHTMLTOX_VERSION'] = version
  558. shell('%s/bin/qmake -set CROSS_COMPILE %s-' % (qtdir, MINGW_W64_PREFIX[rchop(config, '-dbg')]))
  559. shell('%s/bin/qmake -spec win32-g++-4.6 %s/../wkhtmltopdf.pro' % (qtdir, basedir))
  560. shell('make')
  561. shutil.copy('bin/libwkhtmltox0.a', 'bin/wkhtmltox.lib')
  562. os.chdir(os.path.join(basedir, '..'))
  563. shell('makensis -DVERSION=%s -DSIMPLE_VERSION=%s -DTARGET=%s wkhtmltox.nsi' % \
  564. (version, simple_version, config))
  565. # -------------------------------------------------- Linux schroot environment
  566. def check_linux_schroot(config):
  567. shell('schroot -c wkhtmltopdf-%s -- gcc --version' % rchop(config, '-dbg'))
  568. def build_linux_schroot(config, basedir):
  569. version, simple_version = get_version(basedir)
  570. dir = os.path.join(basedir, config)
  571. script = os.path.join(dir, 'build.sh')
  572. dist = os.path.join(dir, 'wkhtmltox-%s' % version)
  573. mkdir_p(os.path.join(dir, 'qt_build'))
  574. mkdir_p(os.path.join(dir, 'app'))
  575. rmdir(dist)
  576. mkdir_p(os.path.join(dist, 'bin'))
  577. mkdir_p(os.path.join(dist, 'include', 'wkhtmltox'))
  578. mkdir_p(os.path.join(dist, 'lib'))
  579. configure_args = qt_config('posix', '--prefix=../qt')
  580. lines = ['#!/bin/bash']
  581. lines.append('# start of autogenerated build script')
  582. lines.append('cd qt_build')
  583. if config == 'centos5-i386':
  584. lines.append('export CFLAGS="-march=i486 -w"')
  585. lines.append('export CXXFLAGS="-march=i486 -w"')
  586. else:
  587. for var in ['CFLAGS', 'CXXFLAGS']:
  588. lines.append('export %s="-w"' % var)
  589. lines.append('if [ ! -f is_configured ]; then')
  590. lines.append(' ../../../qt/configure %s || exit 1' % configure_args)
  591. lines.append(' touch is_configured')
  592. lines.append('fi')
  593. lines.append('if ! make -j%d -q; then\n make -j%d || exit 1\nfi' % (CPU_COUNT, CPU_COUNT))
  594. lines.append('make install || exit 1')
  595. lines.append('cd ../app')
  596. lines.append('rm -f bin/*')
  597. lines.append('export WKHTMLTOX_VERSION=%s' % version)
  598. lines.append('../qt/bin/qmake ../../../wkhtmltopdf.pro')
  599. lines.append('make -j%d || exit 1' % CPU_COUNT)
  600. lines.append('strip bin/wkhtmltopdf bin/wkhtmltoimage')
  601. lines.append('cp bin/wkhtmlto* ../wkhtmltox-%s/bin' % version)
  602. lines.append('cp -P bin/libwkhtmltox*.so.* ../wkhtmltox-%s/lib' % version)
  603. lines.append('cp ../../../include/wkhtmltox/*.h ../wkhtmltox-%s/include/wkhtmltox' % version)
  604. lines.append('cp ../../../include/wkhtmltox/dll*.inc ../wkhtmltox-%s/include/wkhtmltox' % version)
  605. lines.append('cd ..')
  606. lines.append('tar -c -v -f ../wkhtmltox-%s_linux-%s.tar wkhtmltox-%s/' % (version, config, version))
  607. lines.append('xz --compress --force --verbose -9 ../wkhtmltox-%s_linux-%s.tar' % (version, config))
  608. lines.append('# end of build script')
  609. open(script, 'w').write('\n'.join(lines))
  610. os.chdir(dir)
  611. shell('chmod +x build.sh')
  612. shell('schroot -c wkhtmltopdf-%s -- ./build.sh' % rchop(config, '-dbg'))
  613. # -------------------------------------------------- POSIX local environment
  614. def check_posix_local(config):
  615. pass
  616. def build_posix_local(config, basedir):
  617. version, simple_version = get_version(basedir)
  618. build = os.path.join(basedir, config, 'qt_build')
  619. app = os.path.join(basedir, config, 'app')
  620. qtdir = os.path.join(basedir, config, 'qt')
  621. dist = os.path.join(basedir, config, 'wkhtmltox-%s' % version)
  622. mkdir_p(build)
  623. mkdir_p(app)
  624. rmdir(dist)
  625. mkdir_p(os.path.join(dist, 'bin'))
  626. mkdir_p(os.path.join(dist, 'include', 'wkhtmltox'))
  627. mkdir_p(os.path.join(dist, 'lib'))
  628. os.chdir(build)
  629. if not exists('is_configured'):
  630. shell('../../../qt/configure %s' % qt_config('posix', '--prefix=../qt'))
  631. shell('touch is_configured')
  632. if subprocess.call(['make', '-j%d' % CPU_COUNT]):
  633. shell('make -j%d' % CPU_COUNT)
  634. shell('make install')
  635. os.chdir(app)
  636. shell('rm -f bin/*')
  637. os.environ['WKHTMLTOX_VERSION'] = version
  638. shell('../qt/bin/qmake ../../../wkhtmltopdf.pro')
  639. shell('make -j%d' % CPU_COUNT)
  640. shell('cp bin/wkhtmlto* ../wkhtmltox-%s/bin' % version)
  641. shell('cp -P bin/libwkhtmltox*.so.* ../wkhtmltox-%s/lib' % version)
  642. shell('cp ../../../include/wkhtmltox/*.h ../wkhtmltox-%s/include/wkhtmltox' % version)
  643. shell('cp ../../../include/wkhtmltox/dll*.inc ../wkhtmltox-%s/include/wkhtmltox' % version)
  644. os.chdir(basedir)
  645. shell('tar -c -v -f ../wkhtmltox-%s_local-%s.tar wkhtmltox-%s/' % (version, platform.node(), version))
  646. shell('xz --compress --force --verbose -9 ../wkhtmltox-%s_local-%s.tar' % (version, platform.node()))
  647. # --------------------------------------------------------------- command line
  648. def usage(exit_code=2):
  649. print "Usage: scripts/build.py <target> [-clean] [-debug]\n\nThe supported targets are:\n",
  650. opts = list(BUILDERS.keys())
  651. opts.sort()
  652. for opt in opts:
  653. print '* %s' % opt
  654. sys.exit(exit_code)
  655. def main():
  656. basedir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'static-build')
  657. mkdir_p(basedir)
  658. if len(sys.argv) == 1:
  659. usage(0)
  660. config = sys.argv[1]
  661. if config not in BUILDERS:
  662. usage()
  663. for arg in sys.argv[2:]:
  664. if not arg in ['-clean', '-debug']:
  665. usage()
  666. final_config = config
  667. if '-debug' in sys.argv[2:]:
  668. # use the debug OpenSSL configuration if possible
  669. ssl = OPENSSL['build']
  670. for key in ssl:
  671. if fnmatch.fnmatch(config, key) and 'debug' in ssl[key]:
  672. ssl[key]['configure'] = ssl[key]['debug']
  673. # use a debug build of QT and WebKit
  674. cfg = QT_CONFIG['common']
  675. cfg[cfg.index('-release')] = '-debug'
  676. cfg[cfg.index('-webkit')] = '-webkit-debug'
  677. final_config += '-dbg'
  678. if '-clean' in sys.argv[2:]:
  679. rmdir(os.path.join(basedir, config))
  680. globals()['check_%s' % BUILDERS[config]](final_config)
  681. globals()['build_%s' % BUILDERS[config]](final_config, os.path.realpath(basedir))
  682. if __name__ == '__main__':
  683. main()