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