Use boost::random for make_random_file to make it repeatable across platforms.
[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                              'libicu70',
133                              'libnettle8',
134                              'libssh-4',
135                              'libx264-163',
136                              'libcurl4',
137                              'libpulse0',
138                              'libxerces-c3.2',
139                              'libnanomsg5',
140                              'libdav1d5'])
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_editor', file=f)
329     print('%{_bindir}/dcpomatic2_kdm', file=f)
330     print('%{_bindir}/dcpomatic2_server', file=f)
331     print('%{_bindir}/dcpomatic2_server_cli', file=f)
332     print('%{_bindir}/dcpomatic2_kdm_cli', file=f)
333     print('%{_bindir}/dcpomatic2_player', file=f)
334     print('%{_bindir}/dcpomatic2_playlist', file=f)
335     print('%{_bindir}/dcpomatic2_openssl', file=f)
336     print('%{_bindir}/dcpomatic2_combiner', file=f)
337     print('%{_bindir}/dcpomatic2_verify', file=f)
338     if can_build_disk(target):
339         print('%{_bindir}/dcpomatic2_disk', file=f)
340         print('%caps(cap_dac_override=ep) %{_bindir}/dcpomatic2_disk_writer', file=f)
341     print('%{_datadir}/applications/dcpomatic2.desktop', file=f)
342     print('%{_datadir}/applications/dcpomatic2_batch.desktop', file=f)
343     print('%{_datadir}/applications/dcpomatic2_editor.desktop', file=f)
344     print('%{_datadir}/applications/dcpomatic2_server.desktop', file=f)
345     print('%{_datadir}/applications/dcpomatic2_kdm.desktop', file=f)
346     print('%{_datadir}/applications/dcpomatic2_player.desktop', file=f)
347     print('%{_datadir}/applications/dcpomatic2_playlist.desktop', file=f)
348     print('%{_datadir}/applications/dcpomatic2_combiner.desktop', file=f)
349     if can_build_disk(target):
350         print('%{_datadir}/applications/dcpomatic2_disk.desktop', file=f)
351     print('%{_datadir}/dcpomatic2/dcpomatic2_server_small.png', file=f)
352     print('%{_datadir}/dcpomatic2/select.png', file=f)
353     print('%{_datadir}/dcpomatic2/sequence.png', file=f)
354     print('%{_datadir}/dcpomatic2/snap.png', file=f)
355     print('%{_datadir}/dcpomatic2/zoom.png', file=f)
356     print('%{_datadir}/dcpomatic2/zoom_all.png', file=f)
357     print('%{_datadir}/dcpomatic2/tick.png', file=f)
358     print('%{_datadir}/dcpomatic2/no_tick.png', file=f)
359     print('%{_datadir}/dcpomatic2/link.png', file=f)
360     print('%{_datadir}/dcpomatic2/me.jpg', file=f)
361     print('%{_datadir}/dcpomatic2/LiberationSans-Regular.ttf', file=f)
362     print('%{_datadir}/dcpomatic2/LiberationSans-Italic.ttf', file=f)
363     print('%{_datadir}/dcpomatic2/LiberationSans-Bold.ttf', file=f)
364     print('%{_datadir}/dcpomatic2/splash.png', file=f)
365     for r in ['128x128', '16x16', '22x22', '256x256', '32x32', '48x48', '512x512', '64x64']:
366         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2.png' % r, file=f)
367         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_batch.png' % r, file=f)
368         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_editor.png' % r, file=f)
369         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_kdm.png' % r, file=f)
370         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_server.png' % r, file=f)
371         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_player.png' % r, file=f)
372         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_playlist.png' % r, file=f)
373         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_disk.png' % r, file=f)
374         print('%%{_datadir}/icons/hicolor/%s/apps/dcpomatic2_combiner.png' % r, file=f)
375     for l in ['de_DE', 'es_ES', 'fr_FR', 'it_IT', 'sv_SE', 'nl_NL', 'ru_RU', 'pl_PL', 'da_DK',
376               'pt_PT', 'pt_BR', 'sk_SK', 'cs_CZ', 'uk_UA', 'zh_CN', 'tr_TR']:
377         print('%%{_datadir}/locale/%s/LC_MESSAGES/dcpomatic2.mo' % l, file=f)
378         print('%%{_datadir}/locale/%s/LC_MESSAGES/libdcpomatic2-wx.mo' % l, file=f)
379         print('%%{_datadir}/locale/%s/LC_MESSAGES/libdcpomatic2.mo' % l, file=f)
380     print('%{_datadir}/libdcp/tags/*', file=f)
381     print('%{_datadir}/libdcp/xsd/*', file=f)
382     print('%{_datadir}/libdcp/ratings', file=f)
383     print('%{_datadir}/polkit-1/actions/com.dcpomatic.write-drive.policy', file=f)
384     print('', file=f)
385     print('%prep', file=f)
386     print('rm -rf $RPM_BUILD_DIR/dcpomatic-%s' % version, file=f)
387     print('tar xjf $RPM_SOURCE_DIR/dcpomatic-%s.tar.bz2' % version, file=f)
388     print('%build', file=f)
389     print('cd dcpomatic-%s' % version, file=f)
390     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)
391     print('CXXFLAGS="-I%s/include" LDFLAGS="-L%s/lib" ./waf configure --prefix=%%{buildroot}/usr --destdir=/usr %s' %
392           (target.directory, target.directory, configure_options(target, options, for_package=True)), file=f)
393     print('./waf', file=f)
394     print('%install', file=f)
395     print('cd dcpomatic-%s' % version, file=f)
396     print('./waf install', file=f)
397     print('/bin/cp %s/src/openssl/apps/openssl %%{buildroot}/usr/bin/dcpomatic2_openssl' % target.directory, file=f)
398     print('/bin/mkdir -p %{buildroot}/usr/share/libdcp', file=f)
399     print('/bin/cp -r %s/src/libdcp/tags %%{buildroot}/usr/share/libdcp' % target.directory, file=f)
400     print('/bin/cp -r %s/src/libdcp/xsd %%{buildroot}/usr/share/libdcp' % target.directory, file=f)
401     print('/bin/cp %s/src/libdcp/ratings %%{buildroot}/usr/share/libdcp' % target.directory, file=f)
402     print('/bin/mv %s/bin/dcpverify %%{buildroot}/usr/bin/dcpomatic2_verify' % target.directory, file=f)
403     print('', file=f)
404     print('%post', file=f)
405     print('/bin/touch --no-create %{_datadir}/icons/hicolor &>/dev/null || :', file=f)
406     print('setcap "cap_dac_override+ep cap_sys_admin+ep" /usr/bin/dcpomatic2_disk_writer', file=f)
407     print('', file=f)
408     print('%postun', file=f)
409     print('if [ $1 -eq 0 ] ; then', file=f)
410     print('    /bin/touch --no-create %{_datadir}/icons/hicolor &>/dev/null', file=f)
411     print('    /usr/bin/gtk-update-icon-cache %{_datadir}/icons/hicolor &>/dev/null || :', file=f)
412     print('fi', file=f)
413     print('', file=f)
414     print('%posttrans', file=f)
415     print('/usr/bin/gtk-update-icon-cache %{_datadir}/icons/hicolor &>/dev/null || :', file=f)
416
417 def dependencies(target, options):
418
419     if target.platform == 'linux':
420         ffmpeg_options = { 'shared': False }
421     else:
422         ffmpeg_options = {}
423
424     if target.platform != 'linux' or target.distro != 'arch':
425         deps = [('ffmpeg-cdist', '4721b55de017702b0d1c8ce1163331378905c637', ffmpeg_options)]
426     else:
427         # Use distro-provided FFmpeg on Arch
428         deps = []
429
430     deps.append(('libdcp', 'v1.8.22'))
431     deps.append(('libsub', 'v1.6.23'))
432     deps.append(('leqm-nrt', '93ae9e6'))
433     deps.append(('rtaudio', 'f619b76'))
434     # We get our OpenSSL libraries from the environment, but we
435     # also need a patched openssl binary to make certificates.
436     # This dependency is to get that binary, which is added into
437     # the appropriate place later
438     deps.append(('openssl', '7f29dd5'))
439     if can_build_disk(target):
440         deps.append(('lwext4', 'cce3730'))
441     deps.append(('ffcmp', 'da96af56f3ddf074f2044a0cd6e50c95184fd169'))
442
443     return deps
444
445 option_defaults = { "gui": True, "variant": None }
446
447 def configure_options(target, options, for_package=False):
448     opt = ' --warnings-are-errors'
449
450     if for_package or not (
451             (target.platform == 'linux' and target.distro == 'ubuntu' and target.version == '18.04') or
452             (target.platform == 'osx') or
453             (target.platform == 'windows')
454         ):
455         # Currently we only build tests on Ubuntu 18.04, macOS and Windows
456         opt += ' --disable-tests'
457
458     if target.debug:
459         opt += ' --enable-debug'
460     if target.platform == 'windows':
461         opt += f' --target-windows-{target.bits}'
462     elif target.platform == 'linux':
463         opt += ' --static-dcpomatic --static-wxwidgets --static-ffmpeg --static-dcp --static-sub --static-cxml'
464         if target.distro == 'centos':
465             if target.version == '6.5':
466                 opt += ' --static-boost --static-xmlpp'
467             elif target.version == '7':
468                 opt += ' --workaround-gssapi'
469
470     if not options['gui']:
471         opt += ' --disable-gui'
472
473     if options['variant'] is not None:
474         opt += ' --variant=%s' % options['variant']
475
476     # Build Windows debug versions with static linking as I think gdb works better then
477     if target.debug and target.platform == 'windows':
478         opt += ' --static-dcpomatic'
479
480     if can_build_disk(target):
481         opt += ' --enable-disk'
482
483     if target.platform == 'osx' and target.arch == 'arm64':
484         opt += ' --target-macos-arm64 --wx-config=%s/wx-config' % target.bin
485
486     return opt
487
488 def build(target, options, for_package):
489     if target.platform == 'flatpak':
490         target.checkout_dependencies()
491         prefix = 'https://dcpomatic.com/deps'
492         modules = []
493         modules.append({'name': 'libzip',
494                         'buildsystem': 'cmake',
495                         'sources': [{'type': 'archive',
496                                      'url': '%s/libzip-1.4.0.tar.xz' % prefix,
497                                      'sha256': 'e508aba025f5f94b267d5120fc33761bcd98440ebe49dbfe2ed3df3afeacc7b1'}]})
498         modules.append({'name': 'libsigc++',
499                         'sources': [{'type': 'archive',
500                                      'url': '%s/libsigc++-2.10.0.tar.xz' % prefix,
501                                      'sha256': 'f843d6346260bfcb4426259e314512b99e296e8ca241d771d21ac64f28298d81'}]})
502         modules.append({'name': 'glibmm',
503                         'sources': [{'type': 'archive',
504                                      'url': '%s/glibmm-2.48.1.tar.xz' % prefix,
505                                      'sha256': 'dc225f7d2f466479766332483ea78f82dc349d59399d30c00de50e5073157cdf'}]})
506         modules.append({'name': 'cairomm',
507                         'sources': [{'type': 'archive',
508                                      'url': '%s/cairomm-1.12.2.tar.gz' % prefix,
509                                      'sha256': '45c47fd4d0aa77464a75cdca011143fea3ef795c4753f6e860057da5fb8bd599'}]})
510         modules.append({'name': 'pangomm',
511                         'sources': [{'type': 'archive',
512                                      'url': '%s/pangomm-2.40.1.tar.xz' % prefix,
513                                      'sha256': '9762ee2a2d5781be6797448d4dd2383ce14907159b30bc12bf6b08e7227be3af'}]})
514         modules.append({'name': 'libxml++',
515                         'sources': [{'type': 'archive',
516                                      'url': '%s/libxml++-2.40.1.tar.xz' % prefix,
517                                      'sha256': '4ad4abdd3258874f61c2e2a41d08e9930677976d303653cd1670d3e9f35463e9'}]})
518         modules.append({'name': 'xmlsec1',
519                         'sources': [{'type': 'archive',
520                                      'url': '%s/xmlsec1-1.2.25.tar.gz' % prefix,
521                                      'sha256': '967ca83edf25ccb5b48a3c4a09ad3405a63365576503bf34290a42de1b92fcd2'}]})
522         modules.append({'name': 'openjpeg2',
523                         'buildsystem': 'cmake',
524                         'sources': [{'type': 'dir', 'path': os.path.abspath('../openjpeg2-cdist')}]})
525         modules.append({'name': 'boost',
526                         'buildsystem': 'simple',
527                         'build-commands': [
528                             './bootstrap.sh --prefix=/app',
529                             './b2 install'
530                         ],
531                         'sources': [{'type': 'archive',
532                                      'url': '%s/boost_1_66_0.tar.bz2' % prefix,
533                                      'sha256': '5721818253e6a0989583192f96782c4a98eb6204965316df9f5ad75819225ca9'}]})
534         modules.append({'name': 'asdcplib',
535                         'buildsystem': 'simple',
536                         'build-commands': [
537                             './waf configure --prefix=/app  --libdir=/app/lib build install'
538                         ],
539                         'sources': [{'type': 'dir', 'path': os.path.abspath('../asdcplib-carl')}]})
540         modules.append({'name': 'locked_sstream',
541                         'buildsystem': 'simple',
542                         'build-commands': [
543                             './waf configure --prefix=/app build install'
544                         ],
545                         'sources': [{'type': 'dir', 'path': os.path.abspath('../locked_sstream')}]})
546         modules.append({'name': 'libcxml',
547                         'buildsystem': 'simple',
548                         'build-commands': [
549                             './waf configure --prefix=/app  --libdir=/app/lib build install'
550                         ],
551                         'sources': [{'type': 'dir', 'path': os.path.abspath('../libcxml')}]})
552         modules.append({'name': 'libdcp',
553                         'buildsystem': 'simple',
554                         'build-commands': [
555                             './waf configure --prefix=/app --libdir=/app/lib build install'
556                         ],
557                         'sources': [{'type': 'dir', 'path': os.path.abspath('../libdcp')}]})
558         modules.append({'name': 'libsub',
559                         'buildsystem': 'simple',
560                         'build-commands': [
561                             './waf configure --prefix=/app --libdir=/app/lib build install'
562                         ],
563                         'sources': [{'type': 'dir', 'path': os.path.abspath('../libsub')}]})
564         modules.append({'name': 'rtaudio',
565                         'build-options': {
566                         'config-opts': [
567                             '--prefix=/app',
568                             '--with-pulse',
569                             '--with-alsa'
570                         ]
571                         },
572                         'sources': [{'type': 'dir', 'path': os.path.abspath('../rtaudio-cdist')}]})
573         modules.append({'name': 'wxwidgets',
574                         'sources': [{'type': 'archive',
575                                      'url': '%s/wxWidgets-3.0.3.tar.bz2' % prefix,
576                                      'sha256': '08c8033f48ec1b23520f036cde37b5ae925a6a65f137ded665633ca159b9307b'}]})
577         modules.append({'name': 'libssh',
578                         'buildsystem': 'cmake',
579                         'builddir': True,
580                         'sources': [{'type': 'archive',
581                                      'url': '%s/libssh-0.7.5.tar.xz' % prefix,
582                                      'sha256': '54e86dd5dc20e5367e58f3caab337ce37675f863f80df85b6b1614966a337095'}]})
583         modules.append({'name': 'dcpomatic',
584                         'buildsystem': 'simple',
585                         'build-commands': [
586                             './waf configure --prefix=/app build install'
587                          ],
588                         'build-options': {
589                            'build-args': ['--share=network']
590                         },
591                         'sources': [{'type': 'dir', 'path': os.path.abspath('.')}]})
592         desc = {'app-id': 'com.dcpomatic.DCP-o-matic',
593                 'runtime': 'org.gnome.Sdk',
594                 'runtime-version': '3.26',
595                 'sdk': 'org.gnome.Sdk',
596                 'command': 'dcpomatic2',
597                 'finish-args': ['--socket=x11', '--share=ipc', '--share=network', '--socket=pulseaudio', '--filesystem=host'],
598                 'modules': modules}
599         os.makedirs('build/platform')
600         with open('build/com.dcpomatic.DCP-o-matic.json', 'w') as outfile:
601             json.dump(desc, outfile)
602         target.command('%s --repo=build/platform/repo build/platform/flatpak build/com.dcpomatic.DCP-o-matic.json' % target.flatpak_builder())
603     elif target.platform != 'linux' or target.detail == 'appimage' or not for_package:
604         target.command('./waf configure --prefix=%s %s' % (target.directory, configure_options(target, options, for_package)))
605         target.command('./waf')
606         target.command('./waf install')
607
608 def package_windows(target):
609     identifier = ''
610     if target.version is not None:
611         identifier = '%s.' % target.version
612     identifier += '%d' % target.bits
613     shutil.copyfile('build/platform/windows/installer.%s.nsi' % identifier, 'build/platform/windows/installer2.%s.nsi' % identifier)
614     target.command('sed -i "s~%%resources%%~%s/platform/windows~g" build/platform/windows/installer2.%s.nsi' % (os.getcwd(), identifier))
615     target.command('sed -i "s~%%graphics%%~%s/graphics~g" build/platform/windows/installer2.%s.nsi' % (os.getcwd(), identifier))
616     target.command('sed -i "s~%%static_deps%%~%s~g" build/platform/windows/installer2.%s.nsi' % (target.windows_prefix, identifier))
617     target.command('sed -i "s~%%cdist_deps%%~%s~g" build/platform/windows/installer2.%s.nsi' % (target.directory, identifier))
618     target.command('sed -i "s~%%mingw%%~%s~g" build/platform/windows/installer2.%s.nsi' % (target.environment_prefix, identifier))
619     target.command('sed -i "s~%%binaries%%~%s/build~g" build/platform/windows/installer2.%s.nsi' % (os.getcwd(), identifier))
620     target.command('sed -i "s~%%bits%%~32~g" build/platform/windows/installer2.%s.nsi' % identifier)
621     target.command('makensis build/platform/windows/installer2.%s.nsi' % identifier)
622     return os.path.abspath(glob.glob('build/platform/windows/*%s*.exe' % target.bits)[0])
623
624 def package_debian(target, cpu, version, options):
625     make_control(target.version, target.bits, 'debian/control', target.debug, options['gui'])
626     if target.version != '9' and target.version != '16.04' and options['gui']:
627         with open('debian/postinst', 'w') as f:
628             print('#!/bin/sh', file=f)
629             # Get the required capability to write to disks
630             print('setcap "cap_dac_override+ep cap_sys_admin+ep" /usr/bin/dcpomatic2_disk_writer', file=f)
631     target.command('./waf dist')
632     f = open('debian/files', 'w')
633     suffix = '' if options['gui'] else '-cli'
634     print(f'dcpomatic{suffix}_{version}-1_{cpu}.deb video extra', file=f)
635     shutil.rmtree('build/deb', ignore_errors=True)
636
637     os.makedirs('build/deb')
638     os.chdir('build/deb')
639     shutil.move('../../dcpomatic-%s.tar.bz2' % version, 'dcpomatic_%s.orig.tar.bz2' % version)
640     target.command('tar xjf dcpomatic_%s.orig.tar.bz2' % version)
641     os.chdir('dcpomatic-%s' % version)
642     target.set('EMAIL', 'carl@dcpomatic.com')
643     target.command('dch -b -v %s-1 "New upstream release."' % version)
644     target.set('CDIST_LINKFLAGS', target.get('LINKFLAGS'))
645     target.set('CDIST_CXXFLAGS', target.get('CXXFLAGS'))
646     target.set('CDIST_PKG_CONFIG_PATH', target.get('PKG_CONFIG_PATH'))
647     target.set('CDIST_DIRECTORY', target.directory)
648
649     target.set('CDIST_CONFIGURE', '"' + configure_options(target, options, for_package=True) + '"')
650     target.set('CDIST_PACKAGE', f'dcpomatic{suffix}')
651     if not target.debug:
652         target.set('CDIST_DEBUG_PACKAGE_FLAG', '--no-ddebs')
653
654     target.command('dpkg-buildpackage -uc -us')
655
656     debs = []
657     for p in glob.glob('../*.deb'):
658         debs.append(os.path.abspath(p))
659
660     return debs
661
662 def package_rpm(target, cpu, version, options):
663     topdir = os.path.realpath('build/rpmbuild')
664     os.makedirs('%s/BUILD' % topdir)
665     os.makedirs('%s/RPMS' % topdir)
666     os.makedirs('%s/SOURCES' % topdir)
667     os.makedirs('%s/SPECS' % topdir)
668     os.makedirs('%s/SRPMS' % topdir)
669
670     target.command('./waf dist')
671     shutil.copyfile(
672         "%s/src/dcpomatic/dcpomatic-%s.tar.bz2" % (target.directory, version),
673         "%s/SOURCES/dcpomatic-%s.tar.bz2" % (topdir, version)
674         )
675
676     requires = None
677     if target.distro == 'mageia':
678         requires = "lib64xmlsec1-devel lib64canberra-gtk0 libcap-utils"
679
680     make_spec('dcpomatic2.spec', version, target, options, requires)
681     cmd = 'rpmbuild --define "_topdir %s" -bb dcpomatic2.spec' % topdir
682     target.command(cmd)
683     rpms = []
684
685     if cpu == "amd64":
686         cpu = "x86_64"
687     else:
688         cpu = "i686"
689
690     for p in glob.glob('%s/RPMS/%s/*.rpm' % (topdir, cpu)):
691         rpms.append(os.path.abspath(p))
692
693     return rpms
694
695 def make_appimage(target, nice_name, internal_name, version):
696     nice_filename = nice_name.replace(' ', '_')
697     appdir = f'build/{nice_filename}.AppDir'
698     os.makedirs(f'{appdir}/usr/bin')
699     target.command(f'cp {target.directory}/bin/{internal_name} {appdir}/usr/bin')
700     target.command(f'cp {target.directory}/src/openssl/apps/openssl {appdir}/usr/bin/dcpomatic2_openssl')
701     target.command(f'cp {target.directory}/bin/dcpverify {appdir}/usr/bin/dcpomatic2_verify')
702     target.command(f'mkdir -p {appdir}/usr/share/libdcp')
703     target.command(f'cp -r {target.directory}/share/dcpomatic2 {appdir}/usr/share/')
704     target.command(f'cp -r {target.directory}/share/libdcp/xsd {appdir}/usr/share/libdcp/')
705     target.command(f'cp -r {target.directory}/share/libdcp/tags {appdir}/usr/share/libdcp/')
706     target.command(f'cp {target.directory}/share/libdcp/ratings {appdir}/usr/share/libdcp/')
707     lib = 'usr/lib/x86_64-linux-gnu'
708     target.command(f'mkdir -p build/{nice_filename}.AppDir/{lib}/gdk-pixbuf-2.0/2.10.0')
709     target.command(f'cp -a /{lib}/gdk-pixbuf-2.0 build/{nice_filename}.AppDir/usr/lib/x86_64-linux-gnu/')
710     target.command('apt update')
711     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']:
712         target.command(f'apt download {package}')
713         target.command(f'dpkg-deb -x {package}*.deb {appdir}')
714     target.command(f'glib-compile-schemas {appdir}/usr/share/glib-2.0/schemas')
715     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')
716     # Stop anything loading from outside the AppImage
717     target.command(f'sed -i -e "s|/usr|/xxx|g" {appdir}/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2')
718
719     if internal_name == 'dcpomatic2_disk':
720         target.command(f'mkdir -p {appdir}/usr/share/polkit-1/actions')
721         target.command(f'cp {target.directory}/share/polkit-1/actions/com.dcpomatic.write-drive.policy {appdir}/usr/share/polkit-1/actions')
722
723     with open(f'{appdir}/AppRun', 'w') as f:
724         print('#!/bin/bash', file=f)
725         print('export PATH=$APPDIR/usr/bin:$PATH', file=f)
726         print('export XDG_DATA_DIRS="$APPDIR/usr/share/:/usr/share/:$XDG_DATA_DIRS"', file=f)
727         print('export GDK_PIXBUF_MODULEDIR=$(readlink -f "$APPDIR"/usr/lib/x86_64-linux-gnu/gdk-pixbuf-*/*/loaders/ )', file=f)
728         print('export GDK_PIXBUF_MODULE_FILE=$(readlink -f "$APPDIR"/usr/lib/x86_64-linux-gnu/gdk-pixbuf-*/*/loaders.cache )', file=f)
729         print('export LD_LIBRARY_PATH=$GDK_PIXBUF_MODULEDIR:$APPDIR/usr/lib:$APPDIR/usr/lib/x86_64-linux-gnu', file=f)
730         print(f'"$APPDIR"/usr/bin/{internal_name} $@', file=f)
731     target.command(f'chmod a+rx {appdir}/AppRun')
732     with open(f'{appdir}/{internal_name}.desktop', 'w') as f:
733         print('[Desktop Entry]', file=f)
734         print('Type=Application', file=f)
735         print('Categories=AudioVideo;', file=f)
736         print(f'Name={nice_name}', file=f)
737         print(f'Icon={internal_name}', file=f)
738     target.command(f'cp graphics/linux/256/{internal_name}.png {appdir}')
739     target.command(f'linuxdeploy-x86_64.AppImage --appdir {appdir}')
740     target.command(f'appimagetool-x86_64.AppImage {appdir}')
741     target.command(f'mv {nice_filename}-x86_64.AppImage build/{nice_filename}-{version}-x86_64.AppImage')
742     return os.path.abspath(f'build/{nice_filename}-{version}-x86_64.AppImage')
743
744 def package(target, version, options):
745     """version: DCP-o-matic version string"""
746     if target.platform == 'windows':
747         return package_windows(target)
748     elif target.platform == 'linux':
749         if target.detail == 'appimage':
750             out = []
751             out.append(make_appimage(target, 'DCP-o-matic', 'dcpomatic2', version))
752             out.append(make_appimage(target, 'DCP-o-matic Player', 'dcpomatic2_player', version))
753             out.append(make_appimage(target, 'DCP-o-matic Playlist Editor', 'dcpomatic2_playlist', version))
754             out.append(make_appimage(target, 'DCP-o-matic KDM Creator', 'dcpomatic2_kdm', version))
755             out.append(make_appimage(target, 'DCP-o-matic Batch Converter', 'dcpomatic2_batch', version))
756             out.append(make_appimage(target, 'DCP-o-matic Encode Server', 'dcpomatic2_server', version))
757             out.append(make_appimage(target, 'DCP-o-matic Combiner', 'dcpomatic2_combiner', version))
758             out.append(make_appimage(target, 'DCP-o-matic Editor', 'dcpomatic2_editor', version))
759             return out
760         else:
761             if target.bits == 32:
762                 cpu = 'i386'
763             else:
764                 cpu = 'amd64'
765
766             if target.distro == 'debian' or target.distro == 'ubuntu':
767                 return package_debian(target, cpu, version, options)
768             elif target.distro == 'centos' or target.distro == 'fedora' or target.distro == 'mageia':
769                 return package_rpm(target, cpu, version, options)
770     elif target.platform == 'osx':
771         archs = ' '.join(f'{t.arch}/{t.deployment}' for t in target.sub_targets)
772         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))
773         packages = []
774         for x in glob.glob('build/platform/osx/DCP-o-matic*.dmg'):
775             a = os.path.abspath(x)
776             if x.find("Player") != -1:
777                 packages.append((a, "com.dcpomatic.player"))
778             elif x.find("Playlist Editor") != -1:
779                 packages.append((a, "com.dcpomatic.playlist"))
780             elif x.find("KDM Creator") != -1:
781                 packages.append((a, "com.dcpomatic.kdm"))
782             elif x.find("Batch Converter") != -1:
783                 packages.append((a, "com.dcpomatic.batch"))
784             elif x.find("Encode Server") != -1:
785                 packages.append((a, "com.dcpomatic.server"))
786             elif x.find("Disk Writer") != -1:
787                 packages.append((a, "com.dcpomatic.disk"))
788             elif x.find("Combiner") != -1:
789                 packages.append((a, "com.dcpomatic.combiner"))
790             elif x.find("Editor") != -1:
791                 packages.append((a, "com.dcpomatic.editor"))
792             else:
793                 packages.append((a, "com.dcpomatic"))
794         return packages
795     elif target.platform == 'docker':
796         shutil.copyfile(target.deb, 'build/platform/docker')
797         f = open('build/platform/docker/Dockerfile', 'w')
798         print('FROM debian:jessie', file=f)
799         print('MAINTAINER carl@dcpomatic.com', file=f)
800         print('ADD build/platform/docker/dcpomatic_%s-1_amd64.deb /tmp' % (version, version), file=f)
801         print('RUN apt-get -o Acquire:http::Timeout="5" update; exit 0', file=f)
802         print('RUN dpkg -i /tmp/dcpomatic_*.deb; exit 0', file=f)
803         print('RUN apt-get -y -f install', file=f)
804         print('RUN apt-get clean', file=f)
805         print('EXPOSE 6192', file=f)
806         print('CMD ["/usr/bin/dcpomatic2_server_cli", "--verbose"]', file=f)
807         f.close()
808         target.command('docker build build/platform/docker -t dcpomatic-server:%s' % version)
809         target.command('docker save dcpomatic-server:%s -o dcpomatic-server-%s-docker.tar' % (version, version))
810     elif target.platform == 'flatpak':
811         target.command('%s build-bundle build/platform/repo build/dcpomatic_%s.flatpak com.dcpomatic.DCP-o-matic' % (target.flatpak(), version))
812         return os.path.abspath('build/dcpomatic_%s.flatpak' % version)
813
814 def make_pot(target):
815     target.command('./waf pot')
816     return [os.path.abspath('build/src/lib/libdcpomatic.pot'),
817             os.path.abspath('build/src/wx/libdcpomatic-wx.pot'),
818             os.path.abspath('build/src/tools/dcpomatic.pot')]
819
820 def make_manual(target):
821     target.command('make -C doc/manual LIBDCP=../../../libdcp')
822     os.chdir('doc/manual')
823     target.command('pdflatex colour.tex')
824     return [os.path.abspath('pdf'), os.path.abspath('html'), os.path.abspath('colour.pdf')]
825
826 def test(target, options, test):
827     target.set('LC_ALL', 'C')
828     if target.platform == 'windows':
829         cmd = 'run\\tests '
830     else:
831         cmd = 'run/tests '
832     if target.debug:
833         cmd += '--backtrace '
834     if test is not None:
835         cmd += '-t %s' % test
836     target.command(cmd)