build.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. '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. 'build' : ['ms\\do_win64a.bat', 'nmake /f ms\\nt.mak install'],
  34. 'libs' : ['ssleay32.lib', 'libeay32.lib'],
  35. 'os_libs' : '-lUser32 -lAdvapi32 -lGdi32 -lCrypt32'
  36. },
  37. 'mingw-w64-cross-win32': {
  38. 'configure' : '--cross-compile-prefix=i686-w64-mingw32- no-shared no-asm mingw64',
  39. 'build' : ['make', 'make install'],
  40. 'libs' : ['libssl.a', 'libcrypto.a'],
  41. 'os_libs' : '-lws2_32 -lgdi32 -lcrypt32'
  42. },
  43. 'mingw-w64-cross-win64': {
  44. 'configure' : '--cross-compile-prefix=x86_64-w64-mingw32- no-shared no-asm mingw64',
  45. 'build' : ['make', 'make install'],
  46. 'libs' : ['libssl.a', 'libcrypto.a'],
  47. 'os_libs' : '-lws2_32 -lgdi32 -lcrypt32'
  48. }
  49. }
  50. }
  51. QT_CONFIG = {
  52. 'common' : [
  53. '-opensource',
  54. '-confirm-license',
  55. '-fast',
  56. '-release',
  57. '-static',
  58. '-graphicssystem raster',
  59. '-webkit',
  60. '-exceptions', # required by XmlPatterns
  61. '-xmlpatterns', # required for TOC support
  62. '-qt-zlib', # use bundled versions of libraries
  63. '-qt-libpng',
  64. '-qt-libjpeg',
  65. '-no-libmng',
  66. '-no-libtiff',
  67. '-no-accessibility',
  68. '-no-stl',
  69. '-no-qt3support',
  70. '-no-phonon',
  71. '-no-phonon-backend',
  72. '-no-opengl',
  73. '-no-declarative',
  74. '-no-scripttools',
  75. '-no-sql-ibase',
  76. '-no-sql-mysql',
  77. '-no-sql-odbc',
  78. '-no-sql-psql',
  79. '-no-sql-sqlite',
  80. '-no-sql-sqlite2',
  81. '-no-mmx',
  82. '-no-3dnow',
  83. '-no-sse',
  84. '-no-sse2',
  85. '-no-multimedia',
  86. '-nomake demos',
  87. '-nomake docs',
  88. '-nomake examples',
  89. '-nomake tools',
  90. '-nomake tests',
  91. '-nomake translations'
  92. ],
  93. 'msvc': [
  94. '-mp',
  95. '-no-script',
  96. '-qt-style-windows',
  97. '-qt-style-cleanlooks',
  98. '-no-style-windowsxp',
  99. '-no-style-windowsvista',
  100. '-no-style-plastique',
  101. '-no-style-motif',
  102. '-no-style-cde',
  103. '-openssl-linked' # static linkage for OpenSSL
  104. ],
  105. 'posix': [
  106. '-silent', # perform a silent build
  107. '-script', # "make install" does not copy QtScript/qscriptengine.h
  108. '-xrender', # xrender support is required
  109. '-largefile',
  110. '-rpath',
  111. '-openssl', # load OpenSSL binaries at runtime
  112. '-no-dbus',
  113. '-no-nis',
  114. '-no-cups',
  115. '-no-iconv',
  116. '-no-pch',
  117. '-no-gtkstyle',
  118. '-no-nas-sound',
  119. '-no-sm',
  120. '-no-xshape',
  121. '-no-xinerama',
  122. '-no-xcursor',
  123. '-no-xfixes',
  124. '-no-xrandr',
  125. '-no-mitshm',
  126. '-no-xinput',
  127. '-no-xkb',
  128. '-no-glib',
  129. '-no-gstreamer',
  130. '-D ENABLE_VIDEO=0', # required as otherwise gstreamer gets linked in
  131. '-no-openvg',
  132. '-no-xsync',
  133. '-no-audio-backend',
  134. '-no-sse3',
  135. '-no-ssse3',
  136. '-no-sse4.1',
  137. '-no-sse4.2',
  138. '-no-avx',
  139. '-no-neon'
  140. ],
  141. 'mingw-w64-cross' : [
  142. '-silent', # perform a silent build
  143. '-script', # "make install" does not copy QtScript/qscriptengine.h
  144. '-openssl-linked', # static linkage for OpenSSL
  145. '-no-reduce-exports',
  146. '-no-rpath',
  147. '-xplatform win32-g++-4.6'
  148. ]
  149. }
  150. BUILDERS = {
  151. 'msvc2008-win32': 'msvc',
  152. 'msvc2008-win64': 'msvc',
  153. 'msvc2010-win32': 'msvc',
  154. 'msvc2010-win64': 'msvc',
  155. 'msvc2012-win32': 'msvc',
  156. 'msvc2012-win64': 'msvc',
  157. 'msvc2013-win32': 'msvc',
  158. 'msvc2013-win64': 'msvc',
  159. 'msvc-winsdk71-win32': 'msvc_winsdk71',
  160. 'msvc-winsdk71-win64': 'msvc_winsdk71',
  161. 'centos5-i386': 'linux_schroot',
  162. 'centos5-amd64': 'linux_schroot',
  163. 'wheezy-i386': 'linux_schroot',
  164. 'wheezy-amd64': 'linux_schroot',
  165. 'mingw-w64-cross-win32': 'mingw64_cross',
  166. 'mingw-w64-cross-win64': 'mingw64_cross'
  167. }
  168. BUILDER_FLAGS = {
  169. 'msvc': ['clean'],
  170. 'msvc_winsdk71': ['clean'],
  171. 'linux_schroot': ['clean'],
  172. 'mingw64_cross': ['clean'],
  173. }
  174. FLAG_HELP = {
  175. 'clean': 'performs a clean build (instead of an incremental build)'
  176. }
  177. # --------------------------------------------------------------- HELPERS
  178. import os, sys, subprocess, shutil, fnmatch, multiprocessing
  179. from os.path import exists
  180. CPU_COUNT = max(2, multiprocessing.cpu_count()-1) # leave one CPU free
  181. def error(msg):
  182. print msg
  183. sys.exit(1)
  184. def shell(cmd):
  185. ret = os.system(cmd)
  186. if ret != 0:
  187. error("command failed: exit code %d" % ret)
  188. def rmdir(path):
  189. if exists(path):
  190. shutil.rmtree(path)
  191. def mkdir_p(path):
  192. if not exists(path):
  193. os.makedirs(path)
  194. def get_version(basedir):
  195. text = open(os.path.join(basedir, '..', 'VERSION'), 'r').read()
  196. if '-' not in text:
  197. return (text, text)
  198. text = text[:text.index('-')]
  199. os.chdir(os.path.join(basedir, '..'))
  200. hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip()
  201. return ('%s-%s' % (text, hash), text)
  202. def build_openssl(config, basedir):
  203. cfg = None
  204. for key in OPENSSL['build']:
  205. if fnmatch.fnmatch(config, key):
  206. cfg = key
  207. if not cfg:
  208. return
  209. srcdir = os.path.join(basedir, 'openssl')
  210. dstdir = os.path.join(basedir, config, 'openssl')
  211. def is_compiled():
  212. compiled = exists(os.path.join(dstdir, 'include', 'openssl', 'ssl.h'))
  213. for lib in OPENSSL['build'][cfg]['libs']:
  214. compiled = compiled and exists(os.path.join(dstdir, 'lib', lib))
  215. return compiled
  216. if not exists(os.path.join(srcdir, '.git')):
  217. rmdir(srcdir)
  218. rmdir(dstdir)
  219. os.chdir(basedir)
  220. shell('git clone --branch %s --single-branch %s openssl' % (OPENSSL['branch'], OPENSSL['repository']))
  221. os.chdir(srcdir)
  222. shell('git clean -fdx')
  223. shell('git reset --hard HEAD')
  224. shell('git checkout %s' % (OPENSSL['tag']))
  225. if not is_compiled():
  226. opts = OPENSSL['build'][cfg]
  227. shell('perl Configure --openssldir=%s %s' % (dstdir, opts['configure']))
  228. for cmd in opts['build']:
  229. shell(cmd)
  230. shell('git clean -fdx')
  231. if not is_compiled():
  232. error("Unable to compile OpenSSL for your system, aborting.")
  233. return OPENSSL['build'][cfg]['os_libs']
  234. # --------------------------------------------------------------- MSVC (2008-2013)
  235. MSVC_LOCATION = {
  236. 'msvc2008': 'VS90COMNTOOLS',
  237. 'msvc2010': 'VS100COMNTOOLS',
  238. 'msvc2012': 'VS110COMNTOOLS',
  239. 'msvc2013': 'VS120COMNTOOLS'
  240. }
  241. def check_msvc(config):
  242. version, arch = config.split('-')
  243. env_var = MSVC_LOCATION[version]
  244. if not env_var in os.environ:
  245. error("%s does not seem to be installed." % version)
  246. vcdir = os.path.join(os.environ[env_var], '..', '..', 'VC')
  247. if not exists(os.path.join(vcdir, 'vcvarsall.bat')):
  248. error("%s: unable to find vcvarsall.bat" % version)
  249. if arch == 'win32' and not exists(os.path.join(vcdir, 'bin', 'cl.exe')):
  250. error("%s: unable to find the x86 compiler" % version)
  251. if arch == 'win64' and not exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')) \
  252. and not exists(os.path.join(vcdir, 'bin', 'x86_amd64', 'cl.exe')):
  253. error("%s: unable to find the amd64 compiler" % version)
  254. def build_msvc(config, basedir, clean):
  255. msvc, arch = config.split('-')
  256. vcdir = os.path.join(os.environ[MSVC_LOCATION[msvc]], '..', '..', 'VC')
  257. vcarg = 'x86'
  258. if arch == 'win64':
  259. if exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')):
  260. vcarg = 'amd64'
  261. else:
  262. vcarg = 'x86_amd64'
  263. python = sys.executable
  264. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os; print repr(os.environ)"' % (
  265. os.path.join(vcdir, 'vcvarsall.bat'), vcarg, python), stdout=subprocess.PIPE, shell=True)
  266. stdout, _ = process.communicate()
  267. exitcode = process.wait()
  268. if exitcode != 0:
  269. error("%s: unable to initialize the environment" % msvc)
  270. os.environ.update(eval(stdout.strip()))
  271. build_msvc_winsdk71(config, basedir, clean)
  272. # --------------------------------------------------------------- MSVC via Windows SDK 7.1
  273. def check_msvc_winsdk71(config):
  274. for key in ['Configuration', 'TARGET_PLATFORM', 'TARGET_CPU']:
  275. if not key in os.environ:
  276. error("Please run under appropriate 'Windows SDK 7.1 Command Prompt'.")
  277. if os.environ['TARGET_PLATFORM'] not in ['XP', 'LH', 'SRV', 'LHS']:
  278. error("Please configure for 'Windows Server 2008 Release' or earlier.")
  279. if os.environ['Configuration'] != 'Release':
  280. error("Please configure for release mode.")
  281. if os.environ['TARGET_CPU'] not in ['x86', 'x64']:
  282. error("Please configure CPU for either x86 or x64.")
  283. if os.environ['TARGET_CPU'] == 'x86' and config == 'msvc2010-win64':
  284. error("Error: SDK configured for x86 but trying to build 64-bit.")
  285. if os.environ['TARGET_CPU'] == 'x64' and config == 'msvc2010-win32':
  286. error("Error: SDK configured for x64 but trying to build 32-bit.")
  287. def build_msvc_winsdk71(config, basedir, clean):
  288. if clean:
  289. rmdir(os.path.join(basedir, config))
  290. ssl_libs = build_openssl(config, basedir)
  291. ssldir = os.path.join(basedir, config, 'openssl')
  292. qtdir = os.path.join(basedir, config, 'qt')
  293. mkdir_p(qtdir)
  294. args = []
  295. args.extend(QT_CONFIG['common'])
  296. args.extend(QT_CONFIG['msvc'])
  297. args.append('-I %s\\include' % ssldir)
  298. args.append('-L %s\\lib' % ssldir)
  299. args.append('OPENSSL_LIBS="-L%s -lssleay32 -llibeay32 %s"' % \
  300. (ssldir.replace('\\', '\\\\'), ssl_libs))
  301. os.chdir(qtdir)
  302. if not exists('is_configured'):
  303. shell('%s\\..\\qt\\configure.exe %s' % (basedir, ' '.join(args)))
  304. open('is_configured', 'w').write('')
  305. shell('nmake')
  306. appdir = os.path.join(basedir, config, 'app')
  307. mkdir_p(appdir)
  308. os.chdir(appdir)
  309. rmdir('bin')
  310. mkdir_p('bin')
  311. shell('%s\\bin\\qmake %s\\..\\wkhtmltopdf.pro' % (qtdir, basedir))
  312. shell('nmake')
  313. found = False
  314. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  315. if not pfile in os.environ or not exists(os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')):
  316. continue
  317. found = True
  318. version, simple_version = get_version(basedir)
  319. makensis = os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')
  320. os.chdir(os.path.join(basedir, '..'))
  321. shell('"%s" /DVERSION=%s /DSIMPLE_VERSION=%s /DTARGET=%s wkhtmltox.nsi' % \
  322. (makensis, version, simple_version, config))
  323. if not found:
  324. print "\n\nCould not build installer as NSIS was not found."
  325. # ------------------------------------------------ MinGW-W64 Cross Environment
  326. MINGW_W64_PREFIX = {
  327. 'mingw-w64-cross-win32' : 'i686-w64-mingw32',
  328. 'mingw-w64-cross-win64' : 'x86_64-w64-mingw32',
  329. }
  330. def check_mingw64_cross(config):
  331. shell('%s-gcc --version' % MINGW_W64_PREFIX[config])
  332. def build_mingw64_cross(config, basedir, clean):
  333. if clean:
  334. rmdir(os.path.join(basedir, config))
  335. ssl_libs = build_openssl(config, basedir)
  336. ssldir = os.path.join(basedir, config, 'openssl')
  337. build = os.path.join(basedir, config, 'qt_build')
  338. qtdir = os.path.join(basedir, config, 'qt')
  339. mkdir_p(build)
  340. args = []
  341. args.extend(QT_CONFIG['common'])
  342. args.extend(QT_CONFIG['mingw-w64-cross'])
  343. args.append('--prefix=%s' % qtdir)
  344. args.append('-I %s/include' % ssldir)
  345. args.append('-L %s/lib' % ssldir)
  346. args.append('-device-option CROSS_COMPILE=%s-' % MINGW_W64_PREFIX[config])
  347. os.environ['OPENSSL_LIBS'] = '-lssl -lcrypto -L %s/lib %s' % (ssldir, ssl_libs)
  348. os.chdir(build)
  349. if not exists('is_configured'):
  350. shell('%s/../qt/configure %s' % (basedir, ' '.join(args)))
  351. shell('touch is_configured')
  352. shell('make -j%d' % CPU_COUNT)
  353. shell('make install')
  354. appdir = os.path.join(basedir, config, 'app')
  355. mkdir_p(appdir)
  356. os.chdir(appdir)
  357. shell('rm -f bin/*')
  358. # set up cross compiling prefix correctly (isn't set by make install)
  359. os.environ['QTDIR'] = qtdir
  360. shell('%s/bin/qmake -set CROSS_COMPILE %s-' % (qtdir, MINGW_W64_PREFIX[config]))
  361. shell('%s/bin/qmake -spec win32-g++-4.6 %s/../wkhtmltopdf.pro' % (qtdir, basedir))
  362. shell('make')
  363. shutil.copy('bin/libwkhtmltox0.a', 'bin/wkhtmltox.lib')
  364. version, simple_version = get_version(basedir)
  365. os.chdir(os.path.join(basedir, '..'))
  366. shell('makensis -DVERSION=%s -DSIMPLE_VERSION=%s -DTARGET=%s wkhtmltox.nsi' % \
  367. (version, simple_version, config))
  368. # -------------------------------------------------- Linux schroot environment
  369. def check_linux_schroot(config):
  370. shell('schroot -c wkhtmltopdf-%s -- gcc --version' % config)
  371. def build_linux_schroot(config, basedir, clean):
  372. if clean:
  373. rmdir(os.path.join(basedir, config))
  374. version, simple_version = get_version(basedir)
  375. dir = os.path.join(basedir, config)
  376. script = os.path.join(dir, 'build.sh')
  377. dist = os.path.join(dir, 'wkhtmltox-%s' % version)
  378. mkdir_p(os.path.join(dir, 'qt_build'))
  379. mkdir_p(os.path.join(dir, 'app'))
  380. rmdir(dist)
  381. mkdir_p(os.path.join(dist, 'bin'))
  382. mkdir_p(os.path.join(dist, 'include', 'wkhtmltox'))
  383. mkdir_p(os.path.join(dist, 'lib'))
  384. args = []
  385. args.extend(QT_CONFIG['common'])
  386. args.extend(QT_CONFIG['posix'])
  387. args.append('--prefix=../qt')
  388. lines = ['#!/bin/bash']
  389. lines.append('# start of autogenerated build script')
  390. lines.append('cd qt_build')
  391. if config == 'centos5-i386':
  392. lines.append('export CFLAGS=-march=i486')
  393. lines.append('export CXXFLAGS=-march=i486')
  394. lines.append('if [ ! -f is_configured ]; then')
  395. lines.append(' ../../../qt/configure %s || exit 1' % ' '.join(args))
  396. lines.append(' touch is_configured')
  397. lines.append('fi')
  398. lines.append('if ! make -j%d -q; then\n make -j%d || exit 1\nfi' % (CPU_COUNT, CPU_COUNT))
  399. lines.append('make install || exit 1')
  400. lines.append('cd ../app')
  401. lines.append('rm -f bin/*')
  402. lines.append('GIT_DIR=../../../.git ../qt/bin/qmake ../../../wkhtmltopdf.pro')
  403. lines.append('make -j%d || exit 1' % CPU_COUNT)
  404. lines.append('strip bin/wkhtmltopdf bin/wkhtmltoimage')
  405. lines.append('cp bin/wkhtmlto* ../wkhtmltox-%s/bin' % version)
  406. lines.append('cp -P bin/libwkhtmltox*.so.* ../wkhtmltox-%s/lib' % version)
  407. lines.append('cp ../../../include/wkhtmltox/*.h ../wkhtmltox-%s/include/wkhtmltox' % version)
  408. lines.append('cp ../../../include/wkhtmltox/dll*.inc ../wkhtmltox-%s/include/wkhtmltox' % version)
  409. lines.append('cd ..')
  410. lines.append('tar -c -v --use-compress-program=xz -f ../wkhtmltox-%s_linux-%s.tar.xz wkhtmltox-%s/' % (version, config, version))
  411. lines.append('# end of build script')
  412. open(script, 'w').write('\n'.join(lines))
  413. os.chdir(dir)
  414. shell('chmod +x build.sh')
  415. shell('schroot -c wkhtmltopdf-%s -- ./build.sh' % config)
  416. # --------------------------------------------------------------- command line
  417. def usage(exit_code=2):
  418. print "Usage: scripts/build.py <target> [flags]\n\nThe supported targets and associated flags are:\n",
  419. opts = list(BUILDERS.keys())
  420. size = 1 + max([len(opt) for opt in opts])
  421. opts.sort()
  422. for opt in opts:
  423. flags = BUILDER_FLAGS.get(BUILDERS[opt])
  424. if flags:
  425. print '* %s[-%s]' % (opt.ljust(size), '] [-'.join(flags))
  426. else:
  427. print '* %s' % opt.ljust(size)
  428. size = max([len(key) for key in FLAG_HELP])
  429. print "\nFlags:"
  430. for flag in FLAG_HELP:
  431. print "-%s: %s" % (flag.ljust(size), FLAG_HELP[flag])
  432. sys.exit(exit_code)
  433. def main():
  434. basedir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'static-build')
  435. mkdir_p(basedir)
  436. if len(sys.argv) == 1:
  437. usage(0)
  438. config = sys.argv[1]
  439. if config not in BUILDERS:
  440. usage()
  441. args = { 'config': config, 'basedir': os.path.realpath(basedir) }
  442. flags = BUILDER_FLAGS.get(BUILDERS[config]) or []
  443. for arg in sys.argv[2:]:
  444. if not arg.startswith('-') or arg[1:] not in flags:
  445. usage()
  446. for flag in flags:
  447. args[flag.replace('-', '_')] = '-'+flag in sys.argv[2:]
  448. globals()['check_%s' % BUILDERS[config]](config)
  449. globals()['build_%s' % BUILDERS[config]](**args)
  450. if __name__ == '__main__':
  451. main()