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