build.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2014 wkhtmltopdf authors
  4. #
  5. # This file is part of wkhtmltopdf.
  6. #
  7. # wkhtmltopdf is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # wkhtmltopdf is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License
  18. # along with wkhtmltopdf. If not, see <http:#www.gnu.org/licenses/>.
  19. # --------------------------------------------------------------- CONFIGURATION
  20. OPENSSL = {
  21. 'repository': 'https://github.com/openssl/openssl.git',
  22. 'branch' : 'OpenSSL_1_0_1-stable',
  23. 'tag' : 'OpenSSL_1_0_1g',
  24. 'build' : {
  25. 'msvc*-win32*': {
  26. 'configure' : 'VC-WIN32 no-asm',
  27. 'debug' : 'debug-VC-WIN32 no-asm',
  28. 'build' : ['ms\\do_ms.bat', 'nmake /f ms\\nt.mak install'],
  29. 'libs' : ['ssleay32.lib', 'libeay32.lib'],
  30. 'os_libs' : '-lUser32 -lAdvapi32 -lGdi32 -lCrypt32'
  31. },
  32. 'msvc*-win64*': {
  33. 'configure' : 'VC-WIN64A',
  34. 'debug' : 'debug-VC-WIN64A',
  35. 'build' : ['ms\\do_win64a.bat', 'nmake /f ms\\nt.mak install'],
  36. 'libs' : ['ssleay32.lib', 'libeay32.lib'],
  37. 'os_libs' : '-lUser32 -lAdvapi32 -lGdi32 -lCrypt32'
  38. },
  39. 'mingw-w64-cross-win32': {
  40. 'configure' : '--cross-compile-prefix=i686-w64-mingw32- no-shared no-asm mingw64',
  41. 'build' : ['make', 'make install_sw'],
  42. 'libs' : ['libssl.a', 'libcrypto.a'],
  43. 'os_libs' : '-lws2_32 -lgdi32 -lcrypt32'
  44. },
  45. 'mingw-w64-cross-win64': {
  46. 'configure' : '--cross-compile-prefix=x86_64-w64-mingw32- no-shared no-asm mingw64',
  47. 'build' : ['make', 'make install_sw'],
  48. 'libs' : ['libssl.a', 'libcrypto.a'],
  49. 'os_libs' : '-lws2_32 -lgdi32 -lcrypt32'
  50. }
  51. }
  52. }
  53. QT_CONFIG = {
  54. 'common' : [
  55. '-opensource',
  56. '-confirm-license',
  57. '-fast',
  58. '-release',
  59. '-static',
  60. '-graphicssystem raster',
  61. '-webkit',
  62. '-exceptions', # required by XmlPatterns
  63. '-xmlpatterns', # required for TOC support
  64. '-qt-zlib', # use bundled versions of libraries
  65. '-qt-libpng',
  66. '-qt-libjpeg',
  67. '-no-libmng',
  68. '-no-libtiff',
  69. '-no-accessibility',
  70. '-no-stl',
  71. '-no-qt3support',
  72. '-no-phonon',
  73. '-no-phonon-backend',
  74. '-no-opengl',
  75. '-no-declarative',
  76. '-no-scripttools',
  77. '-no-sql-ibase',
  78. '-no-sql-mysql',
  79. '-no-sql-odbc',
  80. '-no-sql-psql',
  81. '-no-sql-sqlite',
  82. '-no-sql-sqlite2',
  83. '-no-mmx',
  84. '-no-3dnow',
  85. '-no-sse',
  86. '-no-sse2',
  87. '-no-multimedia',
  88. '-nomake demos',
  89. '-nomake docs',
  90. '-nomake examples',
  91. '-nomake tools',
  92. '-nomake tests',
  93. '-nomake translations'
  94. ],
  95. 'msvc': [
  96. '-mp',
  97. '-no-script',
  98. '-qt-style-windows',
  99. '-qt-style-cleanlooks',
  100. '-no-style-windowsxp',
  101. '-no-style-windowsvista',
  102. '-no-style-plastique',
  103. '-no-style-motif',
  104. '-no-style-cde',
  105. '-openssl-linked' # static linkage for OpenSSL
  106. ],
  107. 'posix': [
  108. '-silent', # perform a silent build
  109. '-script', # "make install" does not copy QtScript/qscriptengine.h
  110. '-xrender', # xrender support is required
  111. '-largefile',
  112. '-rpath',
  113. '-openssl', # load OpenSSL binaries at runtime
  114. '-no-dbus',
  115. '-no-nis',
  116. '-no-cups',
  117. '-no-iconv',
  118. '-no-pch',
  119. '-no-gtkstyle',
  120. '-no-nas-sound',
  121. '-no-sm',
  122. '-no-xshape',
  123. '-no-xinerama',
  124. '-no-xcursor',
  125. '-no-xfixes',
  126. '-no-xrandr',
  127. '-no-mitshm',
  128. '-no-xinput',
  129. '-no-xkb',
  130. '-no-glib',
  131. '-no-gstreamer',
  132. '-D ENABLE_VIDEO=0', # required as otherwise gstreamer gets linked in
  133. '-no-openvg',
  134. '-no-xsync',
  135. '-no-audio-backend',
  136. '-no-sse3',
  137. '-no-ssse3',
  138. '-no-sse4.1',
  139. '-no-sse4.2',
  140. '-no-avx',
  141. '-no-neon'
  142. ],
  143. 'mingw-w64-cross' : [
  144. '-silent', # perform a silent build
  145. '-script', # "make install" does not copy QtScript/qscriptengine.h
  146. '-openssl-linked', # static linkage for OpenSSL
  147. '-no-reduce-exports',
  148. '-no-rpath',
  149. '-xplatform win32-g++-4.6'
  150. ]
  151. }
  152. BUILDERS = {
  153. 'msvc2008-win32': 'msvc',
  154. 'msvc2008-win64': 'msvc',
  155. 'msvc2010-win32': 'msvc',
  156. 'msvc2010-win64': 'msvc',
  157. 'msvc2012-win32': 'msvc',
  158. 'msvc2012-win64': 'msvc',
  159. 'msvc2013-win32': 'msvc',
  160. 'msvc2013-win64': 'msvc',
  161. 'msvc-winsdk71-win32': 'msvc_winsdk71',
  162. 'msvc-winsdk71-win64': 'msvc_winsdk71',
  163. 'centos5-i386': 'linux_schroot',
  164. 'centos5-amd64': 'linux_schroot',
  165. 'wheezy-i386': 'linux_schroot',
  166. 'wheezy-amd64': 'linux_schroot',
  167. 'mingw-w64-cross-win32': 'mingw64_cross',
  168. 'mingw-w64-cross-win64': 'mingw64_cross'
  169. }
  170. # --------------------------------------------------------------- HELPERS
  171. import os, sys, subprocess, shutil, fnmatch, multiprocessing
  172. from os.path import exists
  173. CPU_COUNT = max(2, multiprocessing.cpu_count()-1) # leave one CPU free
  174. def rchop(s, e):
  175. if s.endswith(e):
  176. return s[:-len(e)]
  177. return s
  178. def error(msg):
  179. print msg
  180. sys.exit(1)
  181. def shell(cmd):
  182. ret = os.system(cmd)
  183. if ret != 0:
  184. error("command failed: exit code %d" % ret)
  185. def rmdir(path):
  186. if exists(path):
  187. shutil.rmtree(path)
  188. def mkdir_p(path):
  189. if not exists(path):
  190. os.makedirs(path)
  191. def get_version(basedir):
  192. text = open(os.path.join(basedir, '..', 'VERSION'), 'r').read()
  193. if '-' not in text:
  194. return (text, text)
  195. version = text[:text.index('-')]
  196. os.chdir(os.path.join(basedir, '..'))
  197. try:
  198. hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], stderr=subprocess.STDOUT).strip()
  199. except subprocess.CalledProcessError:
  200. return (text, version)
  201. return ('%s-%s' % (version, hash), version)
  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 = rchop(config, '-dbg').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):
  255. msvc, arch = rchop(config, '-dbg').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_common(config, basedir)
  272. # --------------------------------------------------------------- MSVC via Windows SDK 7.1
  273. def check_msvc_winsdk71(config):
  274. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  275. if pfile in os.environ and exists(os.path.join(os.environ[pfile], 'Microsoft SDKs', 'Windows', 'v7.1', 'Bin', 'SetEnv.cmd')):
  276. return
  277. error("Unable to detect the location of Windows SDK 7.1")
  278. def build_msvc_winsdk71(config, basedir):
  279. arch = config[config.rindex('-'):]
  280. setenv = None
  281. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  282. if not pfile in os.environ:
  283. continue
  284. setenv = os.path.join(os.environ[pfile], 'Microsoft SDKs', 'Windows', 'v7.1', 'Bin', 'SetEnv.cmd')
  285. mode = debug and '/Debug' or '/Release'
  286. if arch == 'win64':
  287. args = '/2008 /x64 %s' % mode
  288. else:
  289. args = '/2008 /x86 %s' % mode
  290. python = sys.executable
  291. process = subprocess.Popen('("%s" %s>nul)&&"%s" -c "import os; print repr(os.environ)"' % (
  292. setenv, args, python), stdout=subprocess.PIPE, shell=True)
  293. stdout, _ = process.communicate()
  294. exitcode = process.wait()
  295. if exitcode != 0:
  296. error("unable to initialize the environment for Windows SDK 7.1")
  297. os.environ.update(eval(stdout.strip()))
  298. build_msvc_common(config, basedir)
  299. def build_msvc_common(config, basedir):
  300. version, simple_version = get_version(basedir)
  301. ssl_libs = build_openssl(config, basedir)
  302. ssldir = os.path.join(basedir, config, 'openssl')
  303. qtdir = os.path.join(basedir, config, 'qt')
  304. mkdir_p(qtdir)
  305. args = []
  306. args.extend(QT_CONFIG['common'])
  307. args.extend(QT_CONFIG['msvc'])
  308. args.append('-I %s\\include' % ssldir)
  309. args.append('-L %s\\lib' % ssldir)
  310. args.append('OPENSSL_LIBS="-L%s -lssleay32 -llibeay32 %s"' % \
  311. (ssldir.replace('\\', '\\\\'), ssl_libs))
  312. os.chdir(qtdir)
  313. if not exists('is_configured'):
  314. shell('%s\\..\\qt\\configure.exe %s' % (basedir, ' '.join(args)))
  315. open('is_configured', 'w').write('')
  316. shell('nmake')
  317. appdir = os.path.join(basedir, config, 'app')
  318. mkdir_p(appdir)
  319. os.chdir(appdir)
  320. rmdir('bin')
  321. mkdir_p('bin')
  322. os.environ['WKHTMLTOX_VERSION'] = version
  323. shell('%s\\bin\\qmake %s\\..\\wkhtmltopdf.pro' % (qtdir, basedir))
  324. shell('nmake')
  325. found = False
  326. for pfile in ['ProgramFiles(x86)', 'ProgramFiles']:
  327. if not pfile in os.environ or not exists(os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')):
  328. continue
  329. found = True
  330. makensis = os.path.join(os.environ[pfile], 'NSIS', 'makensis.exe')
  331. os.chdir(os.path.join(basedir, '..'))
  332. shell('"%s" /DVERSION=%s /DSIMPLE_VERSION=%s /DTARGET=%s wkhtmltox.nsi' % \
  333. (makensis, version, simple_version, config))
  334. if not found:
  335. print "\n\nCould not build installer as NSIS was not found."
  336. # ------------------------------------------------ MinGW-W64 Cross Environment
  337. MINGW_W64_PREFIX = {
  338. 'mingw-w64-cross-win32' : 'i686-w64-mingw32',
  339. 'mingw-w64-cross-win64' : 'x86_64-w64-mingw32',
  340. }
  341. def check_mingw64_cross(config):
  342. shell('%s-gcc --version' % MINGW_W64_PREFIX[rchop(config, '-dbg')])
  343. def build_mingw64_cross(config, basedir):
  344. version, simple_version = get_version(basedir)
  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[rchop(config, '-dbg')])
  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. os.environ['WKHTMLTOX_VERSION'] = version
  371. shell('%s/bin/qmake -set CROSS_COMPILE %s-' % (qtdir, MINGW_W64_PREFIX[rchop(config, '-dbg')]))
  372. shell('%s/bin/qmake -spec win32-g++-4.6 %s/../wkhtmltopdf.pro' % (qtdir, basedir))
  373. shell('make')
  374. shutil.copy('bin/libwkhtmltox0.a', 'bin/wkhtmltox.lib')
  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' % rchop(config, '-dbg'))
  381. def build_linux_schroot(config, basedir):
  382. version, simple_version = get_version(basedir)
  383. dir = os.path.join(basedir, config)
  384. script = os.path.join(dir, 'build.sh')
  385. dist = os.path.join(dir, 'wkhtmltox-%s' % version)
  386. mkdir_p(os.path.join(dir, 'qt_build'))
  387. mkdir_p(os.path.join(dir, 'app'))
  388. rmdir(dist)
  389. mkdir_p(os.path.join(dist, 'bin'))
  390. mkdir_p(os.path.join(dist, 'include', 'wkhtmltox'))
  391. mkdir_p(os.path.join(dist, 'lib'))
  392. args = []
  393. args.extend(QT_CONFIG['common'])
  394. args.extend(QT_CONFIG['posix'])
  395. args.append('--prefix=../qt')
  396. lines = ['#!/bin/bash']
  397. lines.append('# start of autogenerated build script')
  398. lines.append('cd qt_build')
  399. if config == 'centos5-i386':
  400. lines.append('export CFLAGS=-march=i486')
  401. lines.append('export CXXFLAGS=-march=i486')
  402. lines.append('if [ ! -f is_configured ]; then')
  403. lines.append(' ../../../qt/configure %s || exit 1' % ' '.join(args))
  404. lines.append(' touch is_configured')
  405. lines.append('fi')
  406. lines.append('if ! make -j%d -q; then\n make -j%d || exit 1\nfi' % (CPU_COUNT, CPU_COUNT))
  407. lines.append('make install || exit 1')
  408. lines.append('cd ../app')
  409. lines.append('rm -f bin/*')
  410. lines.append('export WKHTMLTOX_VERSION=%s' % version)
  411. lines.append('../qt/bin/qmake ../../../wkhtmltopdf.pro')
  412. lines.append('make -j%d || exit 1' % CPU_COUNT)
  413. lines.append('strip bin/wkhtmltopdf bin/wkhtmltoimage')
  414. lines.append('cp bin/wkhtmlto* ../wkhtmltox-%s/bin' % version)
  415. lines.append('cp -P bin/libwkhtmltox*.so.* ../wkhtmltox-%s/lib' % version)
  416. lines.append('cp ../../../include/wkhtmltox/*.h ../wkhtmltox-%s/include/wkhtmltox' % version)
  417. lines.append('cp ../../../include/wkhtmltox/dll*.inc ../wkhtmltox-%s/include/wkhtmltox' % version)
  418. lines.append('cd ..')
  419. lines.append('tar -c -v -f ../wkhtmltox-%s_linux-%s.tar wkhtmltox-%s/' % (version, config, version))
  420. lines.append('xz --compress -9 ../wkhtmltox-%s_linux-%s.tar' % (version, config))
  421. lines.append('# end of build script')
  422. open(script, 'w').write('\n'.join(lines))
  423. os.chdir(dir)
  424. shell('chmod +x build.sh')
  425. shell('schroot -c wkhtmltopdf-%s -- ./build.sh' % rchop(config, '-dbg'))
  426. # --------------------------------------------------------------- command line
  427. def usage(exit_code=2):
  428. print "Usage: scripts/build.py <target> [-clean] [-debug]\n\nThe supported targets are:\n",
  429. opts = list(BUILDERS.keys())
  430. opts.sort()
  431. for opt in opts:
  432. print '* %s' % opt
  433. sys.exit(exit_code)
  434. def main():
  435. basedir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'static-build')
  436. mkdir_p(basedir)
  437. if len(sys.argv) == 1:
  438. usage(0)
  439. config = sys.argv[1]
  440. if config not in BUILDERS:
  441. usage()
  442. for arg in sys.argv[2:]:
  443. if not arg in ['-clean', '-debug']:
  444. usage()
  445. final_config = config
  446. if '-debug' in sys.argv[2:]:
  447. # use the debug OpenSSL configuration if possible
  448. ssl = OPENSSL['build']
  449. for key in ssl:
  450. if fnmatch.fnmatch(config, key) and 'debug' in ssl[key]:
  451. ssl[key]['configure'] = ssl[key]['debug']
  452. # use a debug build of QT and WebKit
  453. cfg = QT_CONFIG['common']
  454. cfg[cfg.index('-release')] = '-debug'
  455. cfg[cfg.index('-webkit')] = '-webkit-debug'
  456. final_config += '-dbg'
  457. if '-clean' in sys.argv[2:]:
  458. rmdir(os.path.join(basedir, config))
  459. globals()['check_%s' % BUILDERS[config]](final_config)
  460. globals()['build_%s' % BUILDERS[config]](final_config, os.path.realpath(basedir))
  461. if __name__ == '__main__':
  462. main()