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