build.py 18 KB

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