Fix debian unstable package.
[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)), 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', 'cb2b073d4f88230fca1d1d74e45235f5268fd825', ffmpeg_options)]
397     else:
398         # Use distro-provided FFmpeg on Arch
399         deps = []
400
401     deps.append(('libdcp', 'v1.8.7'))
402     deps.append(('libsub', 'v1.6.7'))
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', '10934f1a9cd9770ef0b38da153f9576e77e7e925'))
413
414     return deps
415
416 option_defaults = { "gui": True, "variant": None }
417
418 def configure_options(target, options):
419     opt = ' --warnings-are-errors'
420
421     if not ((target.platform == 'linux' and target.distro == 'ubuntu' and target.version == '18.04') or
422             (target.platform == 'osx') or
423             (target.platform == 'windows')):
424         # Currently we only build tests on Ubuntu 18.04, macOS and Windows
425         opt += ' --disable-tests'
426
427     if target.debug:
428         opt += ' --enable-debug'
429     if target.platform == 'windows':
430         opt += ' --target-windows'
431     elif target.platform == 'linux':
432         opt += ' --static-dcpomatic --static-wxwidgets --static-ffmpeg --static-dcp --static-sub --static-cxml'
433         if target.distro == 'centos':
434             if target.version == '6.5':
435                 opt += ' --static-boost --static-xmlpp'
436             elif target.version == '7':
437                 opt += ' --workaround-gssapi'
438
439     if not options['gui']:
440         opt += ' --disable-gui'
441
442     if options['variant'] is not None:
443         opt += ' --variant=%s' % options['variant']
444
445     # Build Windows debug versions with static linking as I think gdb works better then
446     if target.debug and target.platform == 'windows':
447         opt += ' --static-dcpomatic'
448
449     if can_build_disk(target):
450         opt += ' --enable-disk'
451
452     if target.platform == 'osx' and target.arch == 'arm64':
453         opt += ' --target-macos-arm64 --wx-config=%s/wx-config' % target.bin
454
455     return opt
456
457 def build(target, options):
458     if target.platform == 'flatpak':
459         target.checkout_dependencies()
460         prefix = 'https://dcpomatic.com/deps'
461         modules = []
462         modules.append({'name': 'libzip',
463                         'buildsystem': 'cmake',
464                         'sources': [{'type': 'archive',
465                                      'url': '%s/libzip-1.4.0.tar.xz' % prefix,
466                                      'sha256': 'e508aba025f5f94b267d5120fc33761bcd98440ebe49dbfe2ed3df3afeacc7b1'}]})
467         modules.append({'name': 'libsigc++',
468                         'sources': [{'type': 'archive',
469                                      'url': '%s/libsigc++-2.10.0.tar.xz' % prefix,
470                                      'sha256': 'f843d6346260bfcb4426259e314512b99e296e8ca241d771d21ac64f28298d81'}]})
471         modules.append({'name': 'glibmm',
472                         'sources': [{'type': 'archive',
473                                      'url': '%s/glibmm-2.48.1.tar.xz' % prefix,
474                                      'sha256': 'dc225f7d2f466479766332483ea78f82dc349d59399d30c00de50e5073157cdf'}]})
475         modules.append({'name': 'cairomm',
476                         'sources': [{'type': 'archive',
477                                      'url': '%s/cairomm-1.12.2.tar.gz' % prefix,
478                                      'sha256': '45c47fd4d0aa77464a75cdca011143fea3ef795c4753f6e860057da5fb8bd599'}]})
479         modules.append({'name': 'pangomm',
480                         'sources': [{'type': 'archive',
481                                      'url': '%s/pangomm-2.40.1.tar.xz' % prefix,
482                                      'sha256': '9762ee2a2d5781be6797448d4dd2383ce14907159b30bc12bf6b08e7227be3af'}]})
483         modules.append({'name': 'libxml++',
484                         'sources': [{'type': 'archive',
485                                      'url': '%s/libxml++-2.40.1.tar.xz' % prefix,
486                                      'sha256': '4ad4abdd3258874f61c2e2a41d08e9930677976d303653cd1670d3e9f35463e9'}]})
487         modules.append({'name': 'xmlsec1',
488                         'sources': [{'type': 'archive',
489                                      'url': '%s/xmlsec1-1.2.25.tar.gz' % prefix,
490                                      'sha256': '967ca83edf25ccb5b48a3c4a09ad3405a63365576503bf34290a42de1b92fcd2'}]})
491         modules.append({'name': 'openjpeg2',
492                         'buildsystem': 'cmake',
493                         'sources': [{'type': 'dir', 'path': os.path.abspath('../openjpeg2-cdist')}]})
494         modules.append({'name': 'boost',
495                         'buildsystem': 'simple',
496                         'build-commands': [
497                             './bootstrap.sh --prefix=/app',
498                             './b2 install'
499                         ],
500                         'sources': [{'type': 'archive',
501                                      'url': '%s/boost_1_66_0.tar.bz2' % prefix,
502                                      'sha256': '5721818253e6a0989583192f96782c4a98eb6204965316df9f5ad75819225ca9'}]})
503         modules.append({'name': 'asdcplib',
504                         'buildsystem': 'simple',
505                         'build-commands': [
506                             './waf configure --prefix=/app  --libdir=/app/lib build install'
507                         ],
508                         'sources': [{'type': 'dir', 'path': os.path.abspath('../asdcplib-carl')}]})
509         modules.append({'name': 'locked_sstream',
510                         'buildsystem': 'simple',
511                         'build-commands': [
512                             './waf configure --prefix=/app build install'
513                         ],
514                         'sources': [{'type': 'dir', 'path': os.path.abspath('../locked_sstream')}]})
515         modules.append({'name': 'libcxml',
516                         'buildsystem': 'simple',
517                         'build-commands': [
518                             './waf configure --prefix=/app  --libdir=/app/lib build install'
519                         ],
520                         'sources': [{'type': 'dir', 'path': os.path.abspath('../libcxml')}]})
521         modules.append({'name': 'libdcp',
522                         'buildsystem': 'simple',
523                         'build-commands': [
524                             './waf configure --prefix=/app --libdir=/app/lib build install'
525                         ],
526                         'sources': [{'type': 'dir', 'path': os.path.abspath('../libdcp')}]})
527         modules.append({'name': 'libsub',
528                         'buildsystem': 'simple',
529                         'build-commands': [
530                             './waf configure --prefix=/app --libdir=/app/lib build install'
531                         ],
532                         'sources': [{'type': 'dir', 'path': os.path.abspath('../libsub')}]})
533         modules.append({'name': 'rtaudio',
534                         'build-options': {
535                         'config-opts': [
536                             '--prefix=/app',
537                             '--with-pulse',
538                             '--with-alsa'
539                         ]
540                         },
541                         'sources': [{'type': 'dir', 'path': os.path.abspath('../rtaudio-cdist')}]})
542         modules.append({'name': 'wxwidgets',
543                         'sources': [{'type': 'archive',
544                                      'url': '%s/wxWidgets-3.0.3.tar.bz2' % prefix,
545                                      'sha256': '08c8033f48ec1b23520f036cde37b5ae925a6a65f137ded665633ca159b9307b'}]})
546         modules.append({'name': 'libssh',
547                         'buildsystem': 'cmake',
548                         'builddir': True,
549                         'sources': [{'type': 'archive',
550                                      'url': '%s/libssh-0.7.5.tar.xz' % prefix,
551                                      'sha256': '54e86dd5dc20e5367e58f3caab337ce37675f863f80df85b6b1614966a337095'}]})
552         modules.append({'name': 'dcpomatic',
553                         'buildsystem': 'simple',
554                         'build-commands': [
555                             './waf configure --prefix=/app build install'
556                          ],
557                         'build-options': {
558                            'build-args': ['--share=network']
559                         },
560                         'sources': [{'type': 'dir', 'path': os.path.abspath('.')}]})
561         desc = {'app-id': 'com.dcpomatic.DCP-o-matic',
562                 'runtime': 'org.gnome.Sdk',
563                 'runtime-version': '3.26',
564                 'sdk': 'org.gnome.Sdk',
565                 'command': 'dcpomatic2',
566                 'finish-args': ['--socket=x11', '--share=ipc', '--share=network', '--socket=pulseaudio', '--filesystem=host'],
567                 'modules': modules}
568         os.makedirs('build/platform')
569         with open('build/com.dcpomatic.DCP-o-matic.json', 'w') as outfile:
570             json.dump(desc, outfile)
571         target.command('%s --repo=build/platform/repo build/platform/flatpak build/com.dcpomatic.DCP-o-matic.json' % target.flatpak_builder())
572     elif target.platform != "linux" or target.detail == 'appimage':
573         # Build here if the packaging step won't do it
574         target.command('./waf configure --prefix=%s %s' % (target.directory, configure_options(target, options)))
575         target.command('./waf')
576         target.command('./waf install')
577
578 def package_windows(target):
579     identifier = ''
580     if target.version is not None:
581         identifier = '%s.' % target.version
582     identifier += '%d' % target.bits
583     shutil.copyfile('build/platform/windows/installer.%s.nsi' % identifier, 'build/platform/windows/installer2.%s.nsi' % identifier)
584     target.command('sed -i "s~%%resources%%~%s/platform/windows~g" build/platform/windows/installer2.%s.nsi' % (os.getcwd(), identifier))
585     target.command('sed -i "s~%%graphics%%~%s/graphics~g" build/platform/windows/installer2.%s.nsi' % (os.getcwd(), identifier))
586     target.command('sed -i "s~%%static_deps%%~%s~g" build/platform/windows/installer2.%s.nsi' % (target.windows_prefix, identifier))
587     target.command('sed -i "s~%%cdist_deps%%~%s~g" build/platform/windows/installer2.%s.nsi' % (target.directory, identifier))
588     target.command('sed -i "s~%%mingw%%~%s~g" build/platform/windows/installer2.%s.nsi' % (target.environment_prefix, identifier))
589     target.command('sed -i "s~%%binaries%%~%s/build~g" build/platform/windows/installer2.%s.nsi' % (os.getcwd(), identifier))
590     target.command('sed -i "s~%%bits%%~32~g" build/platform/windows/installer2.%s.nsi' % identifier)
591     target.command('makensis build/platform/windows/installer2.%s.nsi' % identifier)
592     return os.path.abspath(glob.glob('build/platform/windows/*%s*.exe' % target.bits)[0])
593
594 def package_debian(target, cpu, version, options):
595     make_control(target.version, target.bits, 'debian/control', target.debug, options['gui'])
596     if target.version != '9' and target.version != '16.04' and options['gui']:
597         with open('debian/postinst', 'w') as f:
598             print('#!/bin/sh', file=f)
599             # Get the required capability to write to disks
600             print('setcap "cap_dac_override+ep cap_sys_admin+ep" /usr/bin/dcpomatic2_disk_writer', file=f)
601     target.command('./waf dist')
602     f = open('debian/files', 'w')
603     suffix = '' if options['gui'] else '-cli'
604     print(f'dcpomatic{suffix}_{version}-1_{cpu}.deb video extra', file=f)
605     shutil.rmtree('build/deb', ignore_errors=True)
606
607     os.makedirs('build/deb')
608     os.chdir('build/deb')
609     shutil.move('../../dcpomatic-%s.tar.bz2' % version, 'dcpomatic_%s.orig.tar.bz2' % version)
610     target.command('tar xjf dcpomatic_%s.orig.tar.bz2' % version)
611     os.chdir('dcpomatic-%s' % version)
612     target.set('EMAIL', 'carl@dcpomatic.com')
613     target.command('dch -b -v %s-1 "New upstream release."' % version)
614     target.set('CDIST_LINKFLAGS', target.get('LINKFLAGS'))
615     target.set('CDIST_CXXFLAGS', target.get('CXXFLAGS'))
616     target.set('CDIST_PKG_CONFIG_PATH', target.get('PKG_CONFIG_PATH'))
617     target.set('CDIST_DIRECTORY', target.directory)
618
619     target.set('CDIST_CONFIGURE', '"' + configure_options(target, options) + '"')
620     target.set('CDIST_PACKAGE', f'dcpomatic{suffix}')
621     if not target.debug:
622         target.set('CDIST_DEBUG_PACKAGE_FLAG', '--no-ddebs')
623
624     target.command('dpkg-buildpackage -uc -us')
625
626     debs = []
627     for p in glob.glob('../*.deb'):
628         debs.append(os.path.abspath(p))
629
630     return debs
631
632 def package_rpm(target, cpu, version, options):
633     topdir = os.path.realpath('build/rpmbuild')
634     os.makedirs('%s/BUILD' % topdir)
635     os.makedirs('%s/RPMS' % topdir)
636     os.makedirs('%s/SOURCES' % topdir)
637     os.makedirs('%s/SPECS' % topdir)
638     os.makedirs('%s/SRPMS' % topdir)
639
640     target.command('./waf dist')
641     shutil.copyfile(
642         "%s/src/dcpomatic/dcpomatic-%s.tar.bz2" % (target.directory, version),
643         "%s/SOURCES/dcpomatic-%s.tar.bz2" % (topdir, version)
644         )
645
646     requires = None
647     if target.distro == 'mageia':
648         requires = "lib64xmlsec1-devel lib64canberra-gtk0 libcap-utils"
649
650     make_spec('dcpomatic2.spec', version, target, options, requires)
651     cmd = 'rpmbuild --define "_topdir %s" -bb dcpomatic2.spec' % topdir
652     target.command(cmd)
653     rpms = []
654
655     if cpu == "amd64":
656         cpu = "x86_64"
657     else:
658         cpu = "i686"
659
660     for p in glob.glob('%s/RPMS/%s/*.rpm' % (topdir, cpu)):
661         rpms.append(os.path.abspath(p))
662
663     return rpms
664
665 def make_appimage(target, nice_name, internal_name, version):
666     nice_filename = nice_name.replace(' ', '_')
667     appdir = f'build/{nice_filename}.AppDir'
668     os.makedirs(f'{appdir}/usr/bin')
669     target.command(f'cp {target.directory}/bin/{internal_name} {appdir}/usr/bin')
670     target.command(f'cp {target.directory}/src/openssl/apps/openssl {appdir}/usr/bin/dcpomatic2_openssl')
671     target.command(f'cp {target.directory}/bin/dcpverify {appdir}/usr/bin/dcpomatic2_verify')
672     target.command(f'mkdir -p {appdir}/usr/share/libdcp')
673     target.command(f'cp -r {target.directory}/share/dcpomatic2 {appdir}/usr/share/')
674     target.command(f'cp -r {target.directory}/share/libdcp/xsd {appdir}/usr/share/libdcp/')
675     target.command(f'cp -r {target.directory}/share/libdcp/tags {appdir}/usr/share/libdcp/')
676     lib = 'usr/lib/x86_64-linux-gnu'
677     target.command(f'mkdir -p build/{nice_filename}.AppDir/{lib}/gdk-pixbuf-2.0/2.10.0')
678     target.command(f'cp -a /{lib}/gdk-pixbuf-2.0 build/{nice_filename}.AppDir/usr/lib/x86_64-linux-gnu/')
679     target.command('apt update')
680     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']:
681         target.command(f'apt download {package}')
682         target.command(f'dpkg-deb -x {package}*.deb {appdir}')
683     target.command(f'glib-compile-schemas {appdir}/usr/share/glib-2.0/schemas')
684     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')
685     # Stop anything loading from outside the AppImage
686     target.command(f'sed -i -e "s|/usr|/xxx|g" {appdir}/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2')
687
688     if internal_name == 'dcpomatic2_disk':
689         target.command(f'mkdir -p {appdir}/usr/share/polkit-1/actions')
690         target.command(f'cp {target.directory}/share/polkit-1/actions/com.dcpomatic.write-drive.policy {appdir}/usr/share/polkit-1/actions')
691
692     with open(f'{appdir}/AppRun', 'w') as f:
693         print('#!/bin/bash', file=f)
694         print('export PATH=$APPDIR/usr/bin:$PATH', file=f)
695         print('export XDG_DATA_DIRS="$APPDIR/usr/share/:/usr/share/:$XDG_DATA_DIRS"', file=f)
696         print('export GDK_PIXBUF_MODULEDIR=$(readlink -f "$APPDIR"/usr/lib/x86_64-linux-gnu/gdk-pixbuf-*/*/loaders/ )', file=f)
697         print('export GDK_PIXBUF_MODULE_FILE=$(readlink -f "$APPDIR"/usr/lib/x86_64-linux-gnu/gdk-pixbuf-*/*/loaders.cache )', file=f)
698         print('export LD_LIBRARY_PATH=$GDK_PIXBUF_MODULEDIR:$APPDIR/usr/lib:$APPDIR/usr/lib/x86_64-linux-gnu', file=f)
699         print(f'"$APPDIR"/usr/bin/{internal_name} $@', file=f)
700     target.command(f'chmod a+rx {appdir}/AppRun')
701     with open(f'{appdir}/{internal_name}.desktop', 'w') as f:
702         print('[Desktop Entry]', file=f)
703         print('Type=Application', file=f)
704         print('Categories=AudioVideo;', file=f)
705         print(f'Name={nice_name}', file=f)
706         print(f'Icon={internal_name}', file=f)
707     target.command(f'cp graphics/linux/256/{internal_name}.png {appdir}')
708     target.command(f'linuxdeploy-x86_64.AppImage --appdir {appdir}')
709     target.command(f'appimagetool-x86_64.AppImage {appdir}')
710     target.command(f'mv {nice_filename}-x86_64.AppImage build/{nice_filename}-{version}-x86_64.AppImage')
711     return os.path.abspath(f'build/{nice_filename}-{version}-x86_64.AppImage')
712
713 def package(target, version, options):
714     """version: DCP-o-matic version string"""
715     if target.platform == 'windows':
716         return package_windows(target)
717     elif target.platform == 'linux':
718         if target.detail == 'appimage':
719             out = []
720             out.append(make_appimage(target, 'DCP-o-matic', 'dcpomatic2', version))
721             out.append(make_appimage(target, 'DCP-o-matic Player', 'dcpomatic2_player', version))
722             out.append(make_appimage(target, 'DCP-o-matic Playlist Editor', 'dcpomatic2_playlist', version))
723             out.append(make_appimage(target, 'DCP-o-matic KDM Creator', 'dcpomatic2_kdm', version))
724             out.append(make_appimage(target, 'DCP-o-matic Batch Converter', 'dcpomatic2_batch', version))
725             out.append(make_appimage(target, 'DCP-o-matic Encode Server', 'dcpomatic2_server', version))
726             out.append(make_appimage(target, 'DCP-o-matic Combiner', 'dcpomatic2_combiner', version))
727             return out
728         else:
729             if target.bits == 32:
730                 cpu = 'i386'
731             else:
732                 cpu = 'amd64'
733
734             if target.distro == 'debian' or target.distro == 'ubuntu':
735                 return package_debian(target, cpu, version, options)
736             elif target.distro == 'centos' or target.distro == 'fedora' or target.distro == 'mageia':
737                 return package_rpm(target, cpu, version, options)
738     elif target.platform == 'osx':
739         archs = ' '.join(f'{t.arch}/{t.deployment}' for t in target.sub_targets)
740         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))
741         packages = []
742         for x in glob.glob('build/platform/osx/DCP-o-matic*.dmg'):
743             a = os.path.abspath(x)
744             if x.find("Player") != -1:
745                 packages.append((a, "com.dcpomatic.player"))
746             elif x.find("Playlist Editor") != -1:
747                 packages.append((a, "com.dcpomatic.playlist"))
748             elif x.find("KDM Creator") != -1:
749                 packages.append((a, "com.dcpomatic.kdm"))
750             elif x.find("Batch Converter") != -1:
751                 packages.append((a, "com.dcpomatic.batch"))
752             elif x.find("Encode Server") != -1:
753                 packages.append((a, "com.dcpomatic.server"))
754             elif x.find("Disk Writer") != -1:
755                 packages.append((a, "com.dcpomatic.disk"))
756             elif x.find("Combiner") != -1:
757                 packages.append((a, "com.dcpomatic.combiner"))
758             else:
759                 packages.append((a, "com.dcpomatic"))
760         return packages
761     elif target.platform == 'docker':
762         shutil.copyfile(target.deb, 'build/platform/docker')
763         f = open('build/platform/docker/Dockerfile', 'w')
764         print('FROM debian:jessie', file=f)
765         print('MAINTAINER carl@dcpomatic.com', file=f)
766         print('ADD build/platform/docker/dcpomatic_%s-1_amd64.deb /tmp' % (version, version), file=f)
767         print('RUN apt-get -o Acquire:http::Timeout="5" update; exit 0', file=f)
768         print('RUN dpkg -i /tmp/dcpomatic_*.deb; exit 0', file=f)
769         print('RUN apt-get -y -f install', file=f)
770         print('RUN apt-get clean', file=f)
771         print('EXPOSE 6192', file=f)
772         print('CMD ["/usr/bin/dcpomatic2_server_cli", "--verbose"]', file=f)
773         f.close()
774         target.command('docker build build/platform/docker -t dcpomatic-server:%s' % version)
775         target.command('docker save dcpomatic-server:%s -o dcpomatic-server-%s-docker.tar' % (version, version))
776     elif target.platform == 'flatpak':
777         target.command('%s build-bundle build/platform/repo build/dcpomatic_%s.flatpak com.dcpomatic.DCP-o-matic' % (target.flatpak(), version))
778         return os.path.abspath('build/dcpomatic_%s.flatpak' % version)
779
780 def make_pot(target):
781     target.command('./waf pot')
782     return [os.path.abspath('build/src/lib/libdcpomatic.pot'),
783             os.path.abspath('build/src/wx/libdcpomatic-wx.pot'),
784             os.path.abspath('build/src/tools/dcpomatic.pot')]
785
786 def make_manual(target):
787     target.command('make -C doc/manual LIBDCP=../../../libdcp')
788     os.chdir('doc/manual')
789     target.command('pdflatex colour.tex')
790     return [os.path.abspath('pdf'), os.path.abspath('html'), os.path.abspath('colour.pdf')]
791
792 def test(target, options, test):
793     target.set('LC_ALL', 'C')
794     if target.platform == 'windows':
795         cmd = 'run\\tests '
796     else:
797         cmd = 'run/tests '
798     if target.debug:
799         cmd += '--backtrace '
800     if test is not None:
801         cmd += '-t %s' % test
802     target.command(cmd)