build.py 17 KB

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