Bump ffmpeg to build on newer binutils.
[dcpomatic.git] / cscript
1 # -*- mode: python -*-
2 #
3 #    Copyright (C) 2012-2022 Carl Hetherington <cth@carlh.net>
4 #
5 #    This file is part of DCP-o-matic.
6 #
7 #    DCP-o-matic is free software; you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation; either version 2 of the License, or
10 #    (at your option) any later version.
11 #
12 #    DCP-o-matic 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 General Public License
18 #    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 from __future__ import print_function
22 import glob
23 import shutil
24 import os
25 import copy
26 import json
27
28 deb_build_depends = dict()
29
30 deb_build_depends_base = ['debhelper', 'g++', 'pkg-config', 'libsndfile1-dev', 'libgtk2.0-dev', 'libx264-dev']
31
32 for v in ['16.04', '18.04', '20.04', '20.10', '21.04', '21.10']:
33     deb_build_depends[v] = copy.deepcopy(deb_build_depends_base)
34     deb_build_depends[v].extend(['libssh-dev', 'python'])
35 for v in ['9', '10']:
36     deb_build_depends[v] = copy.deepcopy(deb_build_depends_base)
37     deb_build_depends[v].extend(['libssh-gcrypt-dev', 'python'])
38 for v in ['11']:
39     deb_build_depends[v] = copy.deepcopy(deb_build_depends_base)
40     deb_build_depends[v].extend(['libssh-gcrypt-dev', 'python3.9'])
41 deb_build_depends['unstable'] = copy.deepcopy(deb_build_depends_base)
42 deb_build_depends['unstable'].extend(['python3'])
43
44 deb_depends = dict()
45 deb_depends_gui = dict()
46
47 deb_depends_base = ['libc6', 'libsndfile1', 'libsamplerate0', 'libxmlsec1', 'libxmlsec1-openssl', 'libgtk2.0-0']
48
49 deb_depends['16.04'] = copy.deepcopy(deb_depends_base)
50 deb_depends['16.04'].extend(['libboost-filesystem1.58.0',
51                              'libboost-thread1.58.0',
52                              'libboost-regex1.58.0',
53                              'libxml++2.6-2v5',
54                              'libboost-date-time1.58.0',
55                              'libzip4',
56                              'libcairomm-1.0-1v5',
57                              'libpangomm-1.4-1v5',
58                              'libicu55',
59                              'libnettle6',
60                              'libssh-4',
61                              'libx264-148',
62                              'libcurl3',
63                              'libxerces-c3.1'])
64
65 deb_depends['18.04'] = copy.deepcopy(deb_depends_base)
66 deb_depends['18.04'].extend(['libboost-filesystem1.65.1',
67                              'libboost-thread1.65.1',
68                              'libboost-regex1.65.1',
69                              'libboost-date-time1.65.1',
70                              'libcairomm-1.0-1v5',
71                              'libpangomm-1.4-1v5',
72                              'libxml++2.6-2v5',
73                              'libzip4',
74                              'libicu60',
75                              'libnettle6',
76                              'libssh-4',
77                              'libx264-152',
78                              'libcurl4',
79                              'libpulse0',
80                              'libxerces-c3.2',
81                              'libnanomsg4'])
82
83 deb_depends['20.04'] = copy.deepcopy(deb_depends_base)
84 deb_depends['20.04'].extend(['libboost-filesystem1.71.0',
85                              'libboost-thread1.71.0',
86                              'libboost-regex1.71.0',
87                              'libboost-date-time1.71.0',
88                              'libcairomm-1.0-1v5',
89                              'libpangomm-1.4-1v5',
90                              'libxml++2.6-2v5',
91                              'libzip5',
92                              'libicu66',
93                              'libnettle7',
94                              'libssh-4',
95                              'libx264-155',
96                              'libcurl4',
97                              'libpulse0',
98                              'libxerces-c3.2',
99                              'libnanomsg5'])
100
101 deb_depends['21.10'] = copy.deepcopy(deb_depends_base)
102 deb_depends['21.10'].extend(['libboost-filesystem1.74.0',
103                              'libboost-thread1.74.0',
104                              'libboost-regex1.74.0',
105                              'libboost-date-time1.74.0',
106                              'libcairomm-1.0-1v5',
107                              'libpangomm-1.4-1v5',
108                              'libxml++2.6-2v5',
109                              'libzip4',
110                              'libicu67',
111                              'libnettle8',
112                              'libssh-4',
113                              'libx264-160',
114                              'libcurl4',
115                              'libpulse0',
116                              'libxerces-c3.2',
117                              'libnanomsg5',
118                              'libdav1d4'])
119
120 deb_depends['9'] = copy.deepcopy(deb_depends_base)
121 deb_depends['9'].extend(['libboost-filesystem1.62.0',
122                          'libboost-thread1.62.0',
123                          'libboost-regex1.62.0',
124                          'libboost-date-time1.62.0',
125                          'libxml++2.6-2v5',
126                          'libgtk2.0-0',
127                          'libzip4',
128                          'libcairomm-1.0-1v5',
129                          'libpangomm-1.4-1v5',
130                          'libicu57',
131                          'libssh-4',
132                          'libssh-gcrypt-4',
133                          'libnettle6',
134                          'libx264-148',
135                          'libcurl3',
136                          'libxerces-c3.1'])
137
138 deb_depends_gui['9'] = [ 'libxcb-xfixes0',
139                          'libxcb-shape0',
140                          'libasound2',
141                          'libpulse0' ]
142
143 deb_depends['10'] = copy.deepcopy(deb_depends_base)
144 deb_depends['10'].extend(['libboost-filesystem1.67.0',
145                           'libboost-thread1.67.0',
146                           'libboost-regex1.67.0',
147                           'libboost-date-time1.67.0',
148                           'libxml++2.6-2v5',
149                           'libgtk2.0-0',
150                           'libzip4',
151                           'libcairomm-1.0-1v5',
152                           'libpangomm-1.4-1v5',
153                           'libicu63',
154                           'libssh-4',
155                           'libssh-gcrypt-4',
156                           'libnettle6',
157                           'libx264-155',
158                           'libcurl4',
159                           'libxerces-c3.2',
160                           'libnanomsg5'])
161
162 deb_depends_gui['10'] = [ 'libxcb-xfixes0',
163                           'libxcb-shape0',
164                           'libasound2',
165                           'libpulse0' ]
166
167 deb_depends['11'] = copy.deepcopy(deb_depends_base)
168 deb_depends['11'].extend(['libboost-filesystem1.74.0',
169                           'libboost-thread1.74.0',
170                           'libboost-regex1.74.0',
171                           'libboost-date-time1.74.0',
172                           'libxml++2.6-2v5',
173                           'libzip4',
174                           'libcairomm-1.0-1v5',
175                           'libpangomm-1.4-1v5',
176                           'libicu67',
177                           'libssh-4',
178                           'libssh-gcrypt-4',
179                           'libnettle8',
180                           'libx264-160',
181                           'libcurl4',
182                           'libxerces-c3.2',
183                           'libnanomsg5',
184                           'libdav1d4'])
185
186 deb_depends_gui['11'] = [ 'libxcb-xfixes0',
187                           'libxcb-shape0',
188                           'libasound2',
189                           'libpulse0' ]
190
191 deb_depends['unstable'] = copy.deepcopy(deb_depends_base)
192 deb_depends['unstable'].extend(['libboost-filesystem1.67.0',
193                                 'libboost-thread1.67.0',
194                                 'libboost-regex1.67.0',
195                                 'libboost-date-time1.67.0',
196                                 'libxml++2.6-2v5',
197                                 'libgtk2.0-0',
198                                 'libzip4',
199                                 'libicu63',
200                                 'libnettle6',
201                                 'libx264-155',
202                                 'libcurl4',
203                                 'libxerces-c3.2',
204                                 'libdav1d4'])
205
206 def can_build_disk(target):
207     # We can build dcpomatic2_disk on platforms that have Boost process and can build the lwext4
208     # library.  For now, just whitelist good ones here.
209     #
210     # - Lots of Linux distros (including Ubuntu 16.04) don't have a new enough boost (1.64 or above)
211     # - On Centos 6 we can't build lwext4 because it needs a new CMake which Centos 6's g++ is not new enough to build.
212     # - On Centos 7 there is a build error in lwext4 related to __unused
213     if target.platform == 'windows':
214         return True
215     if target.platform == 'osx':
216         return True
217     if target.platform == 'linux':
218         if target.distro == 'ubuntu' and target.version != '16.04':
219             return True
220         if target.distro == 'debian' and target.version != '9':
221             return True
222         if target.detail == 'appimage':
223             return True 
224         if target.distro == 'fedora' and int(target.version) >= 31:
225             return True
226         if target.distro == 'centos' and target.version != '7':
227             return True
228         if target.distro == 'mageia':
229             return True
230     return False
231
232 def packages(name, packages, f):
233     s = '%s: ' % name
234     for p in packages:
235         s += str(p) + ', '
236     print(s[:-2], file=f)
237
238 def make_control(debian_version, bits, filename, debug, gui):
239     f = open(filename, 'w')
240     print('Source: dcpomatic', file=f)
241     print('Section: video', file=f)
242     print('Priority: extra', file=f)
243     print('Maintainer: Carl Hetherington <carl@dcpomatic.com>', file=f)
244     packages('Build-Depends', deb_build_depends[debian_version], f)
245     print('Standards-Version: 3.9.3', file=f)
246     print('Homepage: https://dcpomatic.com/', file=f)
247     print('', file=f)
248     suffix = '' if gui else '-cli'
249     print(f'Package: dcpomatic{suffix}', file=f)
250     if bits == 32:
251         print('Architecture: i386', file=f)
252     else:
253         print('Architecture: amd64', file=f)
254
255     pkg = deb_depends[debian_version]
256     if gui and debian_version in deb_depends_gui:
257         pkg.extend(deb_depends_gui[debian_version])
258
259     packages('Depends', pkg, f)
260
261     print('Description: Generator of Digital Cinema Packages (DCPs)', file=f)
262     print('  DCP-o-matic generates Digital Cinema Packages (DCPs) from videos, images,', file=f)
263     print('  sound and subtitle files.  You can use it to make content for playback on DCI-compliant', file=f)
264     print('  cinema projectors.', file=f)
265     if not gui:
266         print('  This package contains the command-line tools only.', file=f)
267
268     if debug:
269         print('', file=f)
270         print(f'Package: dcpomatic{suffix}-dbg', file=f)
271         if bits == 32:
272             print('Architecture: i386', file=f)
273         else:
274             print('Architecture: amd64', file=f)
275         print('Section: debug', file=f)
276         print('Priority: extra', file=f)
277         packages('Depends', pkg, f)
278         print('Description: debugging symbols for dcpomatic', file=f)
279         print('  This package contains the debugging symbols for dcpomatic.', file=f)
280         print('', file=f)
281
282 def make_spec(filename, version, target, options, requires=None):
283     """Make a .spec file for a RPM build"""
284     f = open(filename, 'w')
285     print('Summary:A program that generates Digital Cinema Packages (DCPs) from video and audio files', file=f)
286     print('Name:dcpomatic2', file=f)
287     print('Version:%s' % version, file=f)
288     print('Release:1%{?dist}', file=f)
289     print('License:GPL', file=f)
290     print('Group:Applications/Multimedia', file=f)
291     print('URL:https://dcpomatic.com/', file=f)
292     if requires is not None:
293         print('Requires:%s' % requires, file=f)
294     print('', file=f)
295     print('%description', file=f)
296     print('DCP-o-matic generates Digital Cinema Packages (DCPs) from video and audio ', file=f)
297     print('files for presentation on DCI-compliant digital projectors.', file=f)
298     print('', file=f)
299     print('%files', file=f)
300     print('%{_bindir}/dcpomatic2', file=f)
301     print('%{_bindir}/dcpomatic2_batch', file=f)
302     print('%{_bindir}/dcpomatic2_cli', file=f)
303     print('%{_bindir}/dcpomatic2_create', file=f)
304     print('%{_bindir}/dcpomatic2_kdm', file=f)
305     print('%{_bindir}/dcpomatic2_server', file=f)
306     print('%{_bindir}/dcpomatic2_server_cli', file=f)
307     print('%{_bindir}/dcpomatic2_kdm_cli', file=f)
308     print('%{_bindir}/dcpomatic2_player', file=f)
309     print('%{_bindir}/dcpomatic2_playlist', file=f)
310     print('%{_bindir}/dcpomatic2_openssl', file=f)
311     print('%{_bindir}/dcpomatic2_combiner', file=f)
312     print('%{_bindir}/dcpomatic2_verify', file=f)
313     if can_build_disk(target):
314         print('%{_bindir}/dcpomatic2_disk', file=f)
315         print('%caps(cap_dac_override=ep) %{_bindir}/dcpomatic2_disk_writer', file=f)
316     print('%{_datadir}/applications/dcpomatic2.desktop', file=f)
317     print('%{_datadir}/applications/dcpomatic2_batch.desktop', file=f)
318     print('%{_datadir}/applications/dcpomatic2_server.desktop', file=f)
319     print('%{_datadir}/applications/dcpomatic2_kdm.desktop', file=f)
320     print('%{_datadir}/applications/dcpomatic2_player.desktop', file=f)
321     print('%{_datadir}/applications/dcpomatic2_playlist.desktop', file=f)
322     print('%{_datadir}/applications/dcpomatic2_combiner.desktop', file=f)
323     if can_build_disk(target):
324         print('%{_datadir}/applications/dcpomatic2_disk.desktop', file=f)
325     print('%{_datadir}/dcpomatic2/dcpomatic2_server_small.png', file=f)
326     print('%{_datadir}/dcpomatic2/select.png', file=f)
327     print('%{_datadir}/dcpomatic2/sequence.png', file=f)
328     print('%{_datadir}/dcpomatic2/snap.png', file=f)
329     print('%{_datadir}/dcpomatic2/zoom.png', file=f)
330     print('%{_datadir}/dcpomatic2/zoom_all.png', file=f)
331     print('%{_datadir}/dcpomatic2/tick.png', file=f)
332     print('%{_datadir}/dcpomatic2/no_tick.png', file=f)
333     print('%{_datadir}/dcpomatic2/link.png', file=f)
334     print('%{_datadir}/dcpomatic2/me.jpg', file=f)
335     print('%{_datadir}/dcpomatic2/LiberationSans-Regular.ttf', file=f)
336     print('%{_datadir}/dcpomatic2/LiberationSans-Italic.ttf', file=f)
337     print('%{_datadir}/dcpomatic2/LiberationSans-Bold.ttf', file=f)
338     print('%{_datadir}/dcpomatic2/splash.png', file=f)
339     for r in ['128x128', '16x16', '22x22', '256x256', '32x32', '48x48', '512x512', '64x64']:
340         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2.png' % r, file=f)
341         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_batch.png' % r, file=f)
342         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_kdm.png' % r, file=f)
343         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_server.png' % r, file=f)
344         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_player.png' % r, file=f)
345         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_playlist.png' % r, file=f)
346         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_disk.png' % r, file=f)
347         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_combiner.png' % r, file=f)
348     for l in ['de_DE', 'es_ES', 'fr_FR', 'it_IT', 'sv_SE', 'nl_NL', 'ru_RU', 'pl_PL', 'da_DK',
349               'pt_PT', 'pt_BR', 'sk_SK', 'cs_CZ', 'uk_UA', 'zh_CN', 'tr_TR']:
350         print('%%{_datadir}/locale/%s/LC_MESSAGES/dcpomatic2.mo' % l, file=f)
351         print('%%{_datadir}/locale/%s/LC_MESSAGES/libdcpomatic2-wx.mo' % l, file=f)
352         print('%%{_datadir}/locale/%s/LC_MESSAGES/libdcpomatic2.mo' % l, file=f)
353     print('%{_datadir}/libdcp/tags/*', file=f)
354     print('%{_datadir}/libdcp/xsd/*', file=f)
355     print('%{_datadir}/polkit-1/actions/com.dcpomatic.write-drive.policy', file=f)
356     print('', file=f)
357     print('%prep', file=f)
358     print('rm -rf $RPM_BUILD_DIR/dcpomatic-%s' % version, file=f)
359     print('tar xjf $RPM_SOURCE_DIR/dcpomatic-%s.tar.bz2' % version, file=f)
360     print('%build', file=f)
361     print('cd dcpomatic-%s' % version, file=f)
362     print('export PKG_CONFIG_PATH=%s/lib/pkgconfig:%s/lib64/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/lib64/pkgconfig' % (target.directory, target.directory), file=f)
363     print('CXXFLAGS="-I%s/include" LDFLAGS="-L%s/lib" ./waf configure --prefix=%%{buildroot}/usr --destdir=/usr %s' %
364           (target.directory, target.directory, configure_options(target, options, for_package=True)), file=f)
365     print('./waf', file=f)
366     print('%install', file=f)
367     print('cd dcpomatic-%s' % version, file=f)
368     print('./waf install', file=f)
369     print('/bin/cp %s/src/openssl/apps/openssl %%{buildroot}/usr/bin/dcpomatic2_openssl' % target.directory, file=f)
370     print('/bin/mkdir -p %{buildroot}/usr/share/libdcp', file=f)
371     print('/bin/cp -r %s/src/libdcp/tags %%{buildroot}/usr/share/libdcp' % target.directory, file=f)
372     print('/bin/cp -r %s/src/libdcp/xsd %%{buildroot}/usr/share/libdcp' % target.directory, file=f)
373     print('/bin/mv %s/bin/dcpverify %%{buildroot}/usr/bin/dcpomatic2_verify' % target.directory, file=f)
374     print('', file=f)
375     print('%post', file=f)
376     print('/bin/touch --no-create %{_datadir}/icons/hicolor &>/dev/null || :', file=f)
377     print('setcap "cap_dac_override+ep cap_sys_admin+ep" /usr/bin/dcpomatic2_disk_writer', file=f)
378     print('', file=f)
379     print('%postun', file=f)
380     print('if [ $1 -eq 0 ] ; then', file=f)
381     print('    /bin/touch --no-create %{_datadir}/icons/hicolor &>/dev/null', file=f)
382     print('    /usr/bin/gtk-update-icon-cache %{_datadir}/icons/hicolor &>/dev/null || :', file=f)
383     print('fi', file=f)
384     print('', file=f)
385     print('%posttrans', file=f)
386     print('/usr/bin/gtk-update-icon-cache %{_datadir}/icons/hicolor &>/dev/null || :', file=f)
387
388 def dependencies(target, options):
389
390     if target.platform == 'linux':
391         ffmpeg_options = { 'shared': False }
392     else:
393         ffmpeg_options = {}
394
395     if target.platform != 'linux' or target.distro != 'arch':
396         deps = [('ffmpeg-cdist', '4721b55de017702b0d1c8ce1163331378905c637', ffmpeg_options)]
397     else:
398         # Use distro-provided FFmpeg on Arch
399         deps = []
400
401     deps.append(('libdcp', 'v1.8.8'))
402     deps.append(('libsub', 'v1.6.8'))
403     deps.append(('leqm-nrt', '93ae9e6'))
404     deps.append(('rtaudio', 'f619b76'))
405     # We get our OpenSSL libraries from the environment, but we
406     # also need a patched openssl binary to make certificates.
407     # This dependency is to get that binary, which is added into
408     # the appropriate place later
409     deps.append(('openssl', '7f29dd5'))
410     if can_build_disk(target):
411         deps.append(('lwext4', 'cce3730'))
412     deps.append(('ffcmp', 'da96af56f3ddf074f2044a0cd6e50c95184fd169'))
413
414     return deps
415
416 option_defaults = { "gui": True, "variant": None }
417
418 def configure_options(target, options, for_package=False):
419     opt = ' --warnings-are-errors'
420
421     if for_package or not (
422             (target.platform == 'linux' and target.distro == 'ubuntu' and target.version == '18.04') or
423             (target.platform == 'osx') or
424             (target.platform == 'windows')
425         ):
426         # Currently we only build tests on Ubuntu 18.04, macOS and Windows
427         opt += ' --disable-tests'
428
429     if target.debug:
430         opt += ' --enable-debug'
431     if target.platform == 'windows':
432         opt += f' --target-windows-{target.bits}'
433     elif target.platform == 'linux':
434         opt += ' --static-dcpomatic --static-wxwidgets --static-ffmpeg --static-dcp --static-sub --static-cxml'
435         if target.distro == 'centos':
436             if target.version == '6.5':
437                 opt += ' --static-boost --static-xmlpp'
438             elif target.version == '7':
439                 opt += ' --workaround-gssapi'
440
441     if not options['gui']:
442         opt += ' --disable-gui'
443
444     if options['variant'] is not None:
445         opt += ' --variant=%s' % options['variant']
446
447     # Build Windows debug versions with static linking as I think gdb works better then
448     if target.debug and target.platform == 'windows':
449         opt += ' --static-dcpomatic'
450
451     if can_build_disk(target):
452         opt += ' --enable-disk'
453
454     if target.platform == 'osx' and target.arch == 'arm64':
455         opt += ' --target-macos-arm64 --wx-config=%s/wx-config' % target.bin
456
457     return opt
458
459 def build(target, options, for_package):
460     if target.platform == 'flatpak':
461         target.checkout_dependencies()
462         prefix = 'https://dcpomatic.com/deps'
463         modules = []
464         modules.append({'name': 'libzip',
465                         'buildsystem': 'cmake',
466                         'sources': [{'type': 'archive',
467                                      'url': '%s/libzip-1.4.0.tar.xz' % prefix,
468                                      'sha256': 'e508aba025f5f94b267d5120fc33761bcd98440ebe49dbfe2ed3df3afeacc7b1'}]})
469         modules.append({'name': 'libsigc++',
470                         'sources': [{'type': 'archive',
471                                      'url': '%s/libsigc++-2.10.0.tar.xz' % prefix,
472                                      'sha256': 'f843d6346260bfcb4426259e314512b99e296e8ca241d771d21ac64f28298d81'}]})
473         modules.append({'name': 'glibmm',
474                         'sources': [{'type': 'archive',
475                                      'url': '%s/glibmm-2.48.1.tar.xz' % prefix,
476                                      'sha256': 'dc225f7d2f466479766332483ea78f82dc349d59399d30c00de50e5073157cdf'}]})
477         modules.append({'name': 'cairomm',
478                         'sources': [{'type': 'archive',
479                                      'url': '%s/cairomm-1.12.2.tar.gz' % prefix,
480                                      'sha256': '45c47fd4d0aa77464a75cdca011143fea3ef795c4753f6e860057da5fb8bd599'}]})
481         modules.append({'name': 'pangomm',
482                         'sources': [{'type': 'archive',
483                                      'url': '%s/pangomm-2.40.1.tar.xz' % prefix,
484                                      'sha256': '9762ee2a2d5781be6797448d4dd2383ce14907159b30bc12bf6b08e7227be3af'}]})
485         modules.append({'name': 'libxml++',
486                         'sources': [{'type': 'archive',
487                                      'url': '%s/libxml++-2.40.1.tar.xz' % prefix,
488                                      'sha256': '4ad4abdd3258874f61c2e2a41d08e9930677976d303653cd1670d3e9f35463e9'}]})
489         modules.append({'name': 'xmlsec1',
490                         'sources': [{'type': 'archive',
491                                      'url': '%s/xmlsec1-1.2.25.tar.gz' % prefix,
492                                      'sha256': '967ca83edf25ccb5b48a3c4a09ad3405a63365576503bf34290a42de1b92fcd2'}]})
493         modules.append({'name': 'openjpeg2',
494                         'buildsystem': 'cmake',
495                         'sources': [{'type': 'dir', 'path': os.path.abspath('../openjpeg2-cdist')}]})
496         modules.append({'name': 'boost',
497                         'buildsystem': 'simple',
498                         'build-commands': [
499                             './bootstrap.sh --prefix=/app',
500                             './b2 install'
501                         ],
502                         'sources': [{'type': 'archive',
503                                      'url': '%s/boost_1_66_0.tar.bz2' % prefix,
504                                      'sha256': '5721818253e6a0989583192f96782c4a98eb6204965316df9f5ad75819225ca9'}]})
505         modules.append({'name': 'asdcplib',
506                         'buildsystem': 'simple',
507                         'build-commands': [
508                             './waf configure --prefix=/app  --libdir=/app/lib build install'
509                         ],
510                         'sources': [{'type': 'dir', 'path': os.path.abspath('../asdcplib-carl')}]})
511         modules.append({'name': 'locked_sstream',
512                         'buildsystem': 'simple',
513                         'build-commands': [
514                             './waf configure --prefix=/app build install'
515                         ],
516                         'sources': [{'type': 'dir', 'path': os.path.abspath('../locked_sstream')}]})
517         modules.append({'name': 'libcxml',
518                         'buildsystem': 'simple',
519                         'build-commands': [
520                             './waf configure --prefix=/app  --libdir=/app/lib build install'
521                         ],
522                         'sources': [{'type': 'dir', 'path': os.path.abspath('../libcxml')}]})
523         modules.append({'name': 'libdcp',
524                         'buildsystem': 'simple',
525                         'build-commands': [
526                             './waf configure --prefix=/app --libdir=/app/lib build install'
527                         ],
528                         'sources': [{'type': 'dir', 'path': os.path.abspath('../libdcp')}]})
529         modules.append({'name': 'libsub',
530                         'buildsystem': 'simple',
531                         'build-commands': [
532                             './waf configure --prefix=/app --libdir=/app/lib build install'
533                         ],
534                         'sources': [{'type': 'dir', 'path': os.path.abspath('../libsub')}]})
535         modules.append({'name': 'rtaudio',
536                         'build-options': {
537                         'config-opts': [
538                             '--prefix=/app',
539                             '--with-pulse',
540                             '--with-alsa'
541                         ]
542                         },
543                         'sources': [{'type': 'dir', 'path': os.path.abspath('../rtaudio-cdist')}]})
544         modules.append({'name': 'wxwidgets',
545                         'sources': [{'type': 'archive',
546                                      'url': '%s/wxWidgets-3.0.3.tar.bz2' % prefix,
547                                      'sha256': '08c8033f48ec1b23520f036cde37b5ae925a6a65f137ded665633ca159b9307b'}]})
548         modules.append({'name': 'libssh',
549                         'buildsystem': 'cmake',
550                         'builddir': True,
551                         'sources': [{'type': 'archive',
552                                      'url': '%s/libssh-0.7.5.tar.xz' % prefix,
553                                      'sha256': '54e86dd5dc20e5367e58f3caab337ce37675f863f80df85b6b1614966a337095'}]})
554         modules.append({'name': 'dcpomatic',
555                         'buildsystem': 'simple',
556                         'build-commands': [
557                             './waf configure --prefix=/app build install'
558                          ],
559                         'build-options': {
560                            'build-args': ['--share=network']
561                         },
562                         'sources': [{'type': 'dir', 'path': os.path.abspath('.')}]})
563         desc = {'app-id': 'com.dcpomatic.DCP-o-matic',
564                 'runtime': 'org.gnome.Sdk',
565                 'runtime-version': '3.26',
566                 'sdk': 'org.gnome.Sdk',
567                 'command': 'dcpomatic2',
568                 'finish-args': ['--socket=x11', '--share=ipc', '--share=network', '--socket=pulseaudio', '--filesystem=host'],
569                 'modules': modules}
570         os.makedirs('build/platform')
571         with open('build/com.dcpomatic.DCP-o-matic.json', 'w') as outfile:
572             json.dump(desc, outfile)
573         target.command('%s --repo=build/platform/repo build/platform/flatpak build/com.dcpomatic.DCP-o-matic.json' % target.flatpak_builder())
574     elif target.platform != 'linux' or target.detail == 'appimage' or not for_package:
575         target.command('./waf configure --prefix=%s %s' % (target.directory, configure_options(target, options, for_package)))
576         target.command('./waf')
577         target.command('./waf install')
578
579 def package_windows(target):
580     identifier = ''
581     if target.version is not None:
582         identifier = '%s.' % target.version
583     identifier += '%d' % target.bits
584     shutil.copyfile('build/platform/windows/installer.%s.nsi' % identifier, 'build/platform/windows/installer2.%s.nsi' % identifier)
585     target.command('sed -i "s~%%resources%%~%s/platform/windows~g" build/platform/windows/installer2.%s.nsi' % (os.getcwd(), identifier))
586     target.command('sed -i "s~%%graphics%%~%s/graphics~g" build/platform/windows/installer2.%s.nsi' % (os.getcwd(), identifier))
587     target.command('sed -i "s~%%static_deps%%~%s~g" build/platform/windows/installer2.%s.nsi' % (target.windows_prefix, identifier))
588     target.command('sed -i "s~%%cdist_deps%%~%s~g" build/platform/windows/installer2.%s.nsi' % (target.directory, identifier))
589     target.command('sed -i "s~%%mingw%%~%s~g" build/platform/windows/installer2.%s.nsi' % (target.environment_prefix, identifier))
590     target.command('sed -i "s~%%binaries%%~%s/build~g" build/platform/windows/installer2.%s.nsi' % (os.getcwd(), identifier))
591     target.command('sed -i "s~%%bits%%~32~g" build/platform/windows/installer2.%s.nsi' % identifier)
592     target.command('makensis build/platform/windows/installer2.%s.nsi' % identifier)
593     return os.path.abspath(glob.glob('build/platform/windows/*%s*.exe' % target.bits)[0])
594
595 def package_debian(target, cpu, version, options):
596     make_control(target.version, target.bits, 'debian/control', target.debug, options['gui'])
597     if target.version != '9' and target.version != '16.04' and options['gui']:
598         with open('debian/postinst', 'w') as f:
599             print('#!/bin/sh', file=f)
600             # Get the required capability to write to disks
601             print('setcap "cap_dac_override+ep cap_sys_admin+ep" /usr/bin/dcpomatic2_disk_writer', file=f)
602     target.command('./waf dist')
603     f = open('debian/files', 'w')
604     suffix = '' if options['gui'] else '-cli'
605     print(f'dcpomatic{suffix}_{version}-1_{cpu}.deb video extra', file=f)
606     shutil.rmtree('build/deb', ignore_errors=True)
607
608     os.makedirs('build/deb')
609     os.chdir('build/deb')
610     shutil.move('../../dcpomatic-%s.tar.bz2' % version, 'dcpomatic_%s.orig.tar.bz2' % version)
611     target.command('tar xjf dcpomatic_%s.orig.tar.bz2' % version)
612     os.chdir('dcpomatic-%s' % version)
613     target.set('EMAIL', 'carl@dcpomatic.com')
614     target.command('dch -b -v %s-1 "New upstream release."' % version)
615     target.set('CDIST_LINKFLAGS', target.get('LINKFLAGS'))
616     target.set('CDIST_CXXFLAGS', target.get('CXXFLAGS'))
617     target.set('CDIST_PKG_CONFIG_PATH', target.get('PKG_CONFIG_PATH'))
618     target.set('CDIST_DIRECTORY', target.directory)
619
620     target.set('CDIST_CONFIGURE', '"' + configure_options(target, options, for_package=True) + '"')
621     target.set('CDIST_PACKAGE', f'dcpomatic{suffix}')
622     if not target.debug:
623         target.set('CDIST_DEBUG_PACKAGE_FLAG', '--no-ddebs')
624
625     target.command('dpkg-buildpackage -uc -us')
626
627     debs = []
628     for p in glob.glob('../*.deb'):
629         debs.append(os.path.abspath(p))
630
631     return debs
632
633 def package_rpm(target, cpu, version, options):
634     topdir = os.path.realpath('build/rpmbuild')
635     os.makedirs('%s/BUILD' % topdir)
636     os.makedirs('%s/RPMS' % topdir)
637     os.makedirs('%s/SOURCES' % topdir)
638     os.makedirs('%s/SPECS' % topdir)
639     os.makedirs('%s/SRPMS' % topdir)
640
641     target.command('./waf dist')
642     shutil.copyfile(
643         "%s/src/dcpomatic/dcpomatic-%s.tar.bz2" % (target.directory, version),
644         "%s/SOURCES/dcpomatic-%s.tar.bz2" % (topdir, version)
645         )
646
647     requires = None
648     if target.distro == 'mageia':
649         requires = "lib64xmlsec1-devel lib64canberra-gtk0 libcap-utils"
650
651     make_spec('dcpomatic2.spec', version, target, options, requires)
652     cmd = 'rpmbuild --define "_topdir %s" -bb dcpomatic2.spec' % topdir
653     target.command(cmd)
654     rpms = []
655
656     if cpu == "amd64":
657         cpu = "x86_64"
658     else:
659         cpu = "i686"
660
661     for p in glob.glob('%s/RPMS/%s/*.rpm' % (topdir, cpu)):
662         rpms.append(os.path.abspath(p))
663
664     return rpms
665
666 def make_appimage(target, nice_name, internal_name, version):
667     nice_filename = nice_name.replace(' ', '_')
668     appdir = f'build/{nice_filename}.AppDir'
669     os.makedirs(f'{appdir}/usr/bin')
670     target.command(f'cp {target.directory}/bin/{internal_name} {appdir}/usr/bin')
671     target.command(f'cp {target.directory}/src/openssl/apps/openssl {appdir}/usr/bin/dcpomatic2_openssl')
672     target.command(f'cp {target.directory}/bin/dcpverify {appdir}/usr/bin/dcpomatic2_verify')
673     target.command(f'mkdir -p {appdir}/usr/share/libdcp')
674     target.command(f'cp -r {target.directory}/share/dcpomatic2 {appdir}/usr/share/')
675     target.command(f'cp -r {target.directory}/share/libdcp/xsd {appdir}/usr/share/libdcp/')
676     target.command(f'cp -r {target.directory}/share/libdcp/tags {appdir}/usr/share/libdcp/')
677     lib = 'usr/lib/x86_64-linux-gnu'
678     target.command(f'mkdir -p build/{nice_filename}.AppDir/{lib}/gdk-pixbuf-2.0/2.10.0')
679     target.command(f'cp -a /{lib}/gdk-pixbuf-2.0 build/{nice_filename}.AppDir/usr/lib/x86_64-linux-gnu/')
680     target.command('apt update')
681     for package in ['libc6', 'libglib2.0-0', 'gnome-settings-daemon-schemas', 'librsvg2-common', 'libgdk-pixbuf2.0-0', 'libpango-1.0-0', 'libpangoft2-1.0-0', 'libpangocairo-1.0-0']:
682         target.command(f'apt download {package}')
683         target.command(f'dpkg-deb -x {package}*.deb {appdir}')
684     target.command(f'glib-compile-schemas {appdir}/usr/share/glib-2.0/schemas')
685     target.command(f'sed -i -e "s|/usr/lib/x86_64-linux-gnu/gdk-pixbuf-.*/.*/loaders/||g" {appdir}/usr/lib/x86_64-linux-gnu/gdk-pixbuf-*/*/loaders.cache')
686     # Stop anything loading from outside the AppImage
687     target.command(f'sed -i -e "s|/usr|/xxx|g" {appdir}/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2')
688
689     if internal_name == 'dcpomatic2_disk':
690         target.command(f'mkdir -p {appdir}/usr/share/polkit-1/actions')
691         target.command(f'cp {target.directory}/share/polkit-1/actions/com.dcpomatic.write-drive.policy {appdir}/usr/share/polkit-1/actions')
692
693     with open(f'{appdir}/AppRun', 'w') as f:
694         print('#!/bin/bash', file=f)
695         print('export PATH=$APPDIR/usr/bin:$PATH', file=f)
696         print('export XDG_DATA_DIRS="$APPDIR/usr/share/:/usr/share/:$XDG_DATA_DIRS"', file=f)
697         print('export GDK_PIXBUF_MODULEDIR=$(readlink -f "$APPDIR"/usr/lib/x86_64-linux-gnu/gdk-pixbuf-*/*/loaders/ )', file=f)
698         print('export GDK_PIXBUF_MODULE_FILE=$(readlink -f "$APPDIR"/usr/lib/x86_64-linux-gnu/gdk-pixbuf-*/*/loaders.cache )', file=f)
699         print('export LD_LIBRARY_PATH=$GDK_PIXBUF_MODULEDIR:$APPDIR/usr/lib:$APPDIR/usr/lib/x86_64-linux-gnu', file=f)
700         print(f'"$APPDIR"/usr/bin/{internal_name} $@', file=f)
701     target.command(f'chmod a+rx {appdir}/AppRun')
702     with open(f'{appdir}/{internal_name}.desktop', 'w') as f:
703         print('[Desktop Entry]', file=f)
704         print('Type=Application', file=f)
705         print('Categories=AudioVideo;', file=f)
706         print(f'Name={nice_name}', file=f)
707         print(f'Icon={internal_name}', file=f)
708     target.command(f'cp graphics/linux/256/{internal_name}.png {appdir}')
709     target.command(f'linuxdeploy-x86_64.AppImage --appdir {appdir}')
710     target.command(f'appimagetool-x86_64.AppImage {appdir}')
711     target.command(f'mv {nice_filename}-x86_64.AppImage build/{nice_filename}-{version}-x86_64.AppImage')
712     return os.path.abspath(f'build/{nice_filename}-{version}-x86_64.AppImage')
713
714 def package(target, version, options):
715     """version: DCP-o-matic version string"""
716     if target.platform == 'windows':
717         return package_windows(target)
718     elif target.platform == 'linux':
719         if target.detail == 'appimage':
720             out = []
721             out.append(make_appimage(target, 'DCP-o-matic', 'dcpomatic2', version))
722             out.append(make_appimage(target, 'DCP-o-matic Player', 'dcpomatic2_player', version))
723             out.append(make_appimage(target, 'DCP-o-matic Playlist Editor', 'dcpomatic2_playlist', version))
724             out.append(make_appimage(target, 'DCP-o-matic KDM Creator', 'dcpomatic2_kdm', version))
725             out.append(make_appimage(target, 'DCP-o-matic Batch Converter', 'dcpomatic2_batch', version))
726             out.append(make_appimage(target, 'DCP-o-matic Encode Server', 'dcpomatic2_server', version))
727             out.append(make_appimage(target, 'DCP-o-matic Combiner', 'dcpomatic2_combiner', version))
728             return out
729         else:
730             if target.bits == 32:
731                 cpu = 'i386'
732             else:
733                 cpu = 'amd64'
734
735             if target.distro == 'debian' or target.distro == 'ubuntu':
736                 return package_debian(target, cpu, version, options)
737             elif target.distro == 'centos' or target.distro == 'fedora' or target.distro == 'mageia':
738                 return package_rpm(target, cpu, version, options)
739     elif target.platform == 'osx':
740         archs = ' '.join(f'{t.arch}/{t.deployment}' for t in target.sub_targets)
741         target.command('bash platform/osx/make_dmg.sh %s %s %s %s %s' % (target.environment_prefix, target.directory, target.apple_id, target.apple_password, archs))
742         packages = []
743         for x in glob.glob('build/platform/osx/DCP-o-matic*.dmg'):
744             a = os.path.abspath(x)
745             if x.find("Player") != -1:
746                 packages.append((a, "com.dcpomatic.player"))
747             elif x.find("Playlist Editor") != -1:
748                 packages.append((a, "com.dcpomatic.playlist"))
749             elif x.find("KDM Creator") != -1:
750                 packages.append((a, "com.dcpomatic.kdm"))
751             elif x.find("Batch Converter") != -1:
752                 packages.append((a, "com.dcpomatic.batch"))
753             elif x.find("Encode Server") != -1:
754                 packages.append((a, "com.dcpomatic.server"))
755             elif x.find("Disk Writer") != -1:
756                 packages.append((a, "com.dcpomatic.disk"))
757             elif x.find("Combiner") != -1:
758                 packages.append((a, "com.dcpomatic.combiner"))
759             else:
760                 packages.append((a, "com.dcpomatic"))
761         return packages
762     elif target.platform == 'docker':
763         shutil.copyfile(target.deb, 'build/platform/docker')
764         f = open('build/platform/docker/Dockerfile', 'w')
765         print('FROM debian:jessie', file=f)
766         print('MAINTAINER carl@dcpomatic.com', file=f)
767         print('ADD build/platform/docker/dcpomatic_%s-1_amd64.deb /tmp' % (version, version), file=f)
768         print('RUN apt-get -o Acquire:http::Timeout="5" update; exit 0', file=f)
769         print('RUN dpkg -i /tmp/dcpomatic_*.deb; exit 0', file=f)
770         print('RUN apt-get -y -f install', file=f)
771         print('RUN apt-get clean', file=f)
772         print('EXPOSE 6192', file=f)
773         print('CMD ["/usr/bin/dcpomatic2_server_cli", "--verbose"]', file=f)
774         f.close()
775         target.command('docker build build/platform/docker -t dcpomatic-server:%s' % version)
776         target.command('docker save dcpomatic-server:%s -o dcpomatic-server-%s-docker.tar' % (version, version))
777     elif target.platform == 'flatpak':
778         target.command('%s build-bundle build/platform/repo build/dcpomatic_%s.flatpak com.dcpomatic.DCP-o-matic' % (target.flatpak(), version))
779         return os.path.abspath('build/dcpomatic_%s.flatpak' % version)
780
781 def make_pot(target):
782     target.command('./waf pot')
783     return [os.path.abspath('build/src/lib/libdcpomatic.pot'),
784             os.path.abspath('build/src/wx/libdcpomatic-wx.pot'),
785             os.path.abspath('build/src/tools/dcpomatic.pot')]
786
787 def make_manual(target):
788     target.command('make -C doc/manual LIBDCP=../../../libdcp')
789     os.chdir('doc/manual')
790     target.command('pdflatex colour.tex')
791     return [os.path.abspath('pdf'), os.path.abspath('html'), os.path.abspath('colour.pdf')]
792
793 def test(target, options, test):
794     target.set('LC_ALL', 'C')
795     if target.platform == 'windows':
796         cmd = 'run\\tests '
797     else:
798         cmd = 'run/tests '
799     if target.debug:
800         cmd += '--backtrace '
801     if test is not None:
802         cmd += '-t %s' % test
803     target.command(cmd)