build.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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', 'debug'],
  170. 'msvc_winsdk71': ['clean', 'debug'],
  171. 'linux_schroot': ['clean'],
  172. 'mingw64_cross': ['clean'],
  173. }
  174. FLAG_HELP = {
  175. 'clean': 'performs a clean build (instead of an incremental build)',
  176. 'debug': 'performs a debug build'
  177. }
  178. # --------------------------------------------------------------- HELPERS
  179. import os, sys, subprocess, shutil, fnmatch, multiprocessing
  180. from os.path import exists
  181. CPU_COUNT = max(2, multiprocessing.cpu_count()-1) # leave one CPU free
  182. def error(msg):
  183. print msg
  184. sys.exit(1)
  185. def shell(cmd):
  186. ret = os.system(cmd)
  187. if ret != 0:
  188. error("command failed: exit code %d" % ret)
  189. def rmdir(path):
  190. if exists(path):
  191. shutil.rmtree(path)
  192. def mkdir_p(path):
  193. if not exists(path):
  194. os.makedirs(path)
  195. def get_version(basedir):
  196. text = open(os.path.join(basedir, '..', 'VERSION'), 'r').read()
  197. if '-' not in text:
  198. return (text, text)
  199. version = text[:text.index('-')]
  200. os.chdir(os.path.join(basedir, '..'))
  201. try:
  202. hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], stderr=subprocess.STDOUT).strip()
  203. except subprocess.CalledProcessError:
  204. return (text, version)
  205. return ('%s-%s' % (version, hash), version)
  206. def build_openssl(config, basedir):
  207. cfg = None
  208. for key in OPENSSL['build']:
  209. if fnmatch.fnmatch(config, key):
  210. cfg = key
  211. if not cfg:
  212. return
  213. srcdir = os.path.join(basedir, 'openssl')
  214. dstdir = os.path.join(basedir, config, 'openssl')
  215. def is_compiled():
  216. compiled = exists(os.path.join(dstdir, 'include', 'openssl', 'ssl.h'))
  217. for lib in OPENSSL['build'][cfg]['libs']:
  218. compiled = compiled and exists(os.path.join(dstdir, 'lib', lib))
  219. return compiled
  220. if not exists(os.path.join(srcdir, '.git')):
  221. rmdir(srcdir)
  222. rmdir(dstdir)
  223. os.chdir(basedir)
  224. shell('git clone --branch %s --single-branch %s openssl' % (OPENSSL['branch'], OPENSSL['repository']))
  225. os.chdir(srcdir)
  226. shell('git clean -fdx')
  227. shell('git reset --hard HEAD')
  228. shell('git checkout %s' % (OPENSSL['tag']))
  229. if not is_compiled():
  230. opts = OPENSSL['build'][cfg]
  231. shell('perl Configure --openssldir=%s %s' % (dstdir, opts['configure']))
  232. for cmd in opts['build']:
  233. shell(cmd)
  234. shell('git clean -fdx')
  235. if not is_compiled():
  236. error("Unable to compile OpenSSL for your system, aborting.")
  237. return OPENSSL['build'][cfg]['os_libs']
  238. # --------------------------------------------------------------- MSVC (2008-2013)
  239. MSVC_LOCATION = {
  240. 'msvc2008': 'VS90COMNTOOLS',
  241. 'msvc2010': 'VS100COMNTOOLS',
  242. 'msvc2012': 'VS110COMNTOOLS',
  243. 'msvc2013': 'VS120COMNTOOLS'
  244. }
  245. def check_msvc(config):
  246. version, arch = config.split('-')
  247. env_var = MSVC_LOCATION[version]
  248. if not env_var in os.environ:
  249. error("%s does not seem to be installed." % version)
  250. vcdir = os.path.join(os.environ[env_var], '..', '..', 'VC')
  251. if not exists(os.path.join(vcdir, 'vcvarsall.bat')):
  252. error("%s: unable to find vcvarsall.bat" % version)
  253. if arch == 'win32' and not exists(os.path.join(vcdir, 'bin', 'cl.exe')):
  254. error("%s: unable to find the x86 compiler" % version)
  255. if arch == 'win64' and not exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')) \
  256. and not exists(os.path.join(vcdir, 'bin', 'x86_amd64', 'cl.exe')):
  257. error("%s: unable to find the amd64 compiler" % version)
  258. def build_msvc(config, basedir, clean, debug):
  259. msvc, arch = config.split('-')
  260. vcdir = os.path.join(os.environ[MSVC_LOCATION[msvc]], '..', '..', 'VC')
  261. vcarg = 'x86'
  262. if arch == 'win64':
  263. if exists(os.path.join(vcdir, 'bin', 'amd64', 'cl.exe')):
  264. vcarg = 'amd64'
  265. else:
  266. vcarg = 'x86_amd64'
  267. python = sys.executable
  268. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os; print repr(os.environ)"' % (
  269. os.path.join(vcdir, 'vcvarsall.bat'), vcarg, python), stdout=subprocess.PIPE, shell=True)
  270. stdout, _ = process.communicate()
  271. exitcode = process.wait()
  272. if exitcode != 0:
  273. error("%s: unable to initialize the environment" % msvc)
  274. os.environ.update(eval(stdout.strip()))
  275. build_msvc_winsdk71(config, basedir, clean, debug)
  276. # --------------------------------------------------------------- MSVC via Windows SDK 7.1
  277. def check_msvc_winsdk71(config):
  278. for key in ['Configuration', 'TARGET_PLATFORM', 'TARGET_CPU']:
  279. if not key in os.environ:
  280. error("Please run under appropriate 'Windows SDK 7.1 Command Prompt'.")
  281. if os.environ['TARGET_PLATFORM'] not in ['XP', 'LH', 'SRV', 'LHS']:
  282. error("Please configure for 'Windows Server 2008 Release' or earlier.")
  283. if os.environ['Configuration'] != 'Release':
  284. error("Please configure for release mode.")
  285. if os.environ['TARGET_CPU'] not in ['x86', 'x64']:
  286. error("Please configure CPU for either x86 or x64.")
  287. if os.environ['TARGET_CPU'] == 'x86' and config == 'msvc2010-win64':
  288. error("Error: SDK configured for x86 but trying to build 64-bit.")
  289. if os.environ['TARGET_CPU'] == 'x64' and config == 'msvc2010-win32':
  290. error("Error: SDK configured for x64 but trying to build 32-bit.")
  291. def build_msvc_winsdk71(config, basedir, clean, debug):
  292. if debug:
  293. ssl = OPENSSL['build']
  294. cfg = QT_CONFIG['common']
  295. for key in ssl:
  296. if fnmatch.fnmatch(config, key):
  297. ssl[key]['configure'] = 'debug-'+ssl[key]['configure']
  298. cfg[cfg.index('-release')] = '-debug'
  299. cfg[cfg.index('-webkit')] = '-webkit-debug'
  300. config += '-dbg'
  301. if clean:
  302. rmdir(os.path.join(basedir, config))
  303. ssl_libs = build_openssl(config, basedir)
  304. ssldir = os.path.join(basedir, config, 'openssl')
  305. qtdir = os.path.join(basedir, config, 'qt')
  306. mkdir_p(qtdir)
  307. args = []
  308. args.extend(QT_CONFIG['common'])
  309. args.extend(QT_CONFIG['msvc'])
  310. args.append('-I %s\\include' % ssldir)
  311. args.append('-L %s\\lib' % ssldir)
  312. args.append('OPENSSL_LIBS="-L%s -lssleay32 -llibeay32 %s"' % \
  313. (ssldir.replace('\\', '\\\\'), ssl_libs))
  314. os.chdir(qtdir)
  315. if not exists('is_configured'):
  316. shell('%s\\..\\qt\\configure.exe %s' % (basedir, ' '.join(args)))
  317. open('is_configured', 'w').write('')
  318. shell('nmake')
  319. appdir = os.path.join(basedir, config, 'app')
  320. mkdir_p(appdir)
  321. os.chdir(appdir)
  322. rmdir('bin')
  323. mkdir_p('bin')
  324. shell('%s\\bin\\qmake %s\\..\\wkhtmltopdf.pro' % (qtdir, basedir))
  325. shell('nmake')
  326. found = False
  327. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  328. if not pfile in os.environ or not exists(os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')):
  329. continue
  330. found = True
  331. version, simple_version = get_version(basedir)
  332. makensis = os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')
  333. os.chdir(os.path.join(basedir, '..'))
  334. shell('"%s" /DVERSION=%s /DSIMPLE_VERSION=%s /DTARGET=%s wkhtmltox.nsi' % \
  335. (makensis, version, simple_version, config))
  336. if not found:
  337. print "\n\nCould not build installer as NSIS was not found."
  338. # ------------------------------------------------ MinGW-W64 Cross Environment
  339. MINGW_W64_PREFIX = {
  340. 'mingw-w64-cross-win32' : 'i686-w64-mingw32',
  341. 'mingw-w64-cross-win64' : 'x86_64-w64-mingw32',
  342. }
  343. def check_mingw64_cross(config):
  344. shell('%s-gcc --version' % MINGW_W64_PREFIX[config])
  345. def build_mingw64_cross(config, basedir, clean):
  346. if clean:
  347. rmdir(os.path.join(basedir, config))
  348. ssl_libs = build_openssl(config, basedir)
  349. ssldir = os.path.join(basedir, config, 'openssl')
  350. build = os.path.join(basedir, config, 'qt_build')
  351. qtdir = os.path.join(basedir, config, 'qt')
  352. mkdir_p(build)
  353. args = []
  354. args.extend(QT_CONFIG['common'])
  355. args.extend(QT_CONFIG['mingw-w64-cross'])
  356. args.append('--prefix=%s' % qtdir)
  357. args.append('-I %s/include' % ssldir)
  358. args.append('-L %s/lib' % ssldir)
  359. args.append('-device-option CROSS_COMPILE=%s-' % MINGW_W64_PREFIX[config])
  360. os.environ['OPENSSL_LIBS'] = '-lssl -lcrypto -L %s/lib %s' % (ssldir, ssl_libs)
  361. os.chdir(build)
  362. if not exists('is_configured'):
  363. shell('%s/../qt/configure %s' % (basedir, ' '.join(args)))
  364. shell('touch is_configured')
  365. shell('make -j%d' % CPU_COUNT)
  366. shell('make install')
  367. appdir = os.path.join(basedir, config, 'app')
  368. mkdir_p(appdir)
  369. os.chdir(appdir)
  370. shell('rm -f bin/*')
  371. # set up cross compiling prefix correctly (isn't set by make install)
  372. os.environ['QTDIR'] = qtdir
  373. shell('%s/bin/qmake -set CROSS_COMPILE %s-' % (qtdir, MINGW_W64_PREFIX[config]))
  374. shell('%s/bin/qmake -spec win32-g++-4.6 %s/../wkhtmltopdf.pro' % (qtdir, basedir))
  375. shell('make')
  376. shutil.copy('bin/libwkhtmltox0.a', 'bin/wkhtmltox.lib')
  377. version, simple_version = get_version(basedir)
  378. os.chdir(os.path.join(basedir, '..'))
  379. shell('makensis -DVERSION=%s -DSIMPLE_VERSION=%s -DTARGET=%s wkhtmltox.nsi' % \
  380. (version, simple_version, config))
  381. # -------------------------------------------------- Linux schroot environment
  382. def check_linux_schroot(config):
  383. shell('schroot -c wkhtmltopdf-%s -- gcc --version' % config)
  384. def build_linux_schroot(config, basedir, clean):
  385. if clean:
  386. rmdir(os.path.join(basedir, config))
  387. version, simple_version = get_version(basedir)
  388. dir = os.path.join(basedir, config)
  389. script = os.path.join(dir, 'build.sh')
  390. dist = os.path.join(dir, 'wkhtmltox-%s' % version)
  391. mkdir_p(os.path.join(dir, 'qt_build'))
  392. mkdir_p(os.path.join(dir, 'app'))
  393. rmdir(dist)
  394. mkdir_p(os.path.join(dist, 'bin'))
  395. mkdir_p(os.path.join(dist, 'include', 'wkhtmltox'))
  396. mkdir_p(os.path.join(dist, 'lib'))
  397. args = []
  398. args.extend(QT_CONFIG['common'])
  399. args.extend(QT_CONFIG['posix'])
  400. args.append('--prefix=../qt')
  401. lines = ['#!/bin/bash']
  402. lines.append('# start of autogenerated build script')
  403. lines.append('cd qt_build')
  404. if config == 'centos5-i386':
  405. lines.append('export CFLAGS=-march=i486')
  406. lines.append('export CXXFLAGS=-march=i486')
  407. lines.append('if [ ! -f is_configured ]; then')
  408. lines.append(' ../../../qt/configure %s || exit 1' % ' '.join(args))
  409. lines.append(' touch is_configured')
  410. lines.append('fi')
  411. lines.append('if ! make -j%d -q; then\n make -j%d || exit 1\nfi' % (CPU_COUNT, CPU_COUNT))
  412. lines.append('make install || exit 1')
  413. lines.append('cd ../app')
  414. lines.append('rm -f bin/*')
  415. lines.append('GIT_DIR=../../../.git ../qt/bin/qmake ../../../wkhtmltopdf.pro')
  416. lines.append('make -j%d || exit 1' % CPU_COUNT)
  417. lines.append('strip bin/wkhtmltopdf bin/wkhtmltoimage')
  418. lines.append('cp bin/wkhtmlto* ../wkhtmltox-%s/bin' % version)
  419. lines.append('cp -P bin/libwkhtmltox*.so.* ../wkhtmltox-%s/lib' % version)
  420. lines.append('cp ../../../include/wkhtmltox/*.h ../wkhtmltox-%s/include/wkhtmltox' % version)
  421. lines.append('cp ../../../include/wkhtmltox/dll*.inc ../wkhtmltox-%s/include/wkhtmltox' % version)
  422. lines.append('cd ..')
  423. lines.append('tar -c -v -f ../wkhtmltox-%s_linux-%s.tar wkhtmltox-%s/' % (version, config, version))
  424. lines.append('xz --compress -9 ../wkhtmltox-%s_linux-%s.tar' % (version, config))
  425. lines.append('# end of build script')
  426. open(script, 'w').write('\n'.join(lines))
  427. os.chdir(dir)
  428. shell('chmod +x build.sh')
  429. shell('schroot -c wkhtmltopdf-%s -- ./build.sh' % config)
  430. # --------------------------------------------------------------- command line
  431. def usage(exit_code=2):
  432. print "Usage: scripts/build.py <target> [flags]\n\nThe supported targets and associated flags are:\n",
  433. opts = list(BUILDERS.keys())
  434. size = 1 + max([len(opt) for opt in opts])
  435. opts.sort()
  436. for opt in opts:
  437. flags = BUILDER_FLAGS.get(BUILDERS[opt])
  438. if flags:
  439. print '* %s[-%s]' % (opt.ljust(size), '] [-'.join(flags))
  440. else:
  441. print '* %s' % opt.ljust(size)
  442. size = max([len(key) for key in FLAG_HELP])
  443. print "\nFlags:"
  444. for flag in FLAG_HELP:
  445. print "-%s: %s" % (flag.ljust(size), FLAG_HELP[flag])
  446. sys.exit(exit_code)
  447. def main():
  448. basedir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'static-build')
  449. mkdir_p(basedir)
  450. if len(sys.argv) == 1:
  451. usage(0)
  452. config = sys.argv[1]
  453. if config not in BUILDERS:
  454. usage()
  455. args = { 'config': config, 'basedir': os.path.realpath(basedir) }
  456. flags = BUILDER_FLAGS.get(BUILDERS[config]) or []
  457. for arg in sys.argv[2:]:
  458. if not arg.startswith('-') or arg[1:] not in flags:
  459. usage()
  460. for flag in flags:
  461. args[flag.replace('-', '_')] = '-'+flag in sys.argv[2:]
  462. globals()['check_%s' % BUILDERS[config]](config)
  463. globals()['build_%s' % BUILDERS[config]](**args)
  464. if __name__ == '__main__':
  465. main()