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