Fix build on Debian unstable which now has GraphicsMagick rather than ImageMagick.
[dcpomatic.git] / wscript
1 import subprocess
2 import os
3 import sys
4 import distutils
5 import distutils.spawn
6
7 APPNAME = 'dcpomatic'
8 VERSION = '1.75.1devel'
9
10 def options(opt):
11     opt.load('compiler_cxx')
12     opt.load('winres')
13
14     opt.add_option('--enable-debug',      action='store_true', default=False, help='build with debugging information and without optimisation')
15     opt.add_option('--disable-gui',       action='store_true', default=False, help='disable building of GUI tools')
16     opt.add_option('--disable-tests',     action='store_true', default=False, help='disable building of tests')
17     opt.add_option('--target-windows',    action='store_true', default=False, help='set up to do a cross-compile to make a Windows package')
18     opt.add_option('--target-debian',     action='store_true', default=False, help='set up to compile for a Debian/Ubuntu package')
19     opt.add_option('--debian-unstable',   action='store_true', default=False, help='add extra libraries to static-build correctly on Debian unstable')
20     opt.add_option('--target-centos-6',   action='store_true', default=False, help='set up to compile for a Centos 6.5 package')
21     opt.add_option('--target-centos-7',   action='store_true', default=False, help='set up to compile for a Centos 7 package')
22     opt.add_option('--magickpp-config',   action='store',      default='Magick++-config', help='path to Magick++-config')
23     opt.add_option('--wx-config',         action='store',      default='wx-config', help='path to wx-config')
24     opt.add_option('--address-sanitizer', action='store_true', default=False, help='build with address sanitizer')
25     opt.add_option('--install-prefix',                         default=None,  help='prefix of where DCP-o-matic will be installed')
26
27 def static_ffmpeg(conf):
28     conf.check_cfg(package='libavformat', args='--cflags', uselib_store='AVFORMAT', mandatory=True)
29     conf.env.STLIB_AVFORMAT = ['avformat']
30     conf.check_cfg(package='libavfilter', args='--cflags', uselib_store='AVFILTER', mandatory=True)
31     conf.env.STLIB_AVFILTER = ['avfilter', 'swresample']
32     conf.check_cfg(package='libavcodec', args='--cflags', uselib_store='AVCODEC', mandatory=True)
33     # lzma link is needed by Centos 7, at least
34     conf.env.STLIB_AVCODEC = ['avcodec']
35     conf.env.LIB_AVCODEC = ['z', 'lzma']
36     conf.check_cfg(package='libavutil', args='--cflags', uselib_store='AVUTIL', mandatory=True)
37     conf.env.STLIB_AVUTIL = ['avutil']
38     conf.check_cfg(package='libswscale', args='--cflags', uselib_store='SWSCALE', mandatory=True)
39     conf.env.STLIB_SWSCALE = ['swscale']
40     conf.check_cfg(package='libswresample', args='--cflags', uselib_store='SWRESAMPLE', mandatory=True)
41     conf.env.STLIB_SWRESAMPLE = ['swresample']
42     conf.check_cfg(package='libpostproc', args='--cflags', uselib_store='POSTPROC', mandatory=True)
43     conf.env.STLIB_POSTPROC = ['postproc']
44
45 def dynamic_ffmpeg(conf):
46     conf.check_cfg(package='libavformat', args='--cflags --libs', uselib_store='AVFORMAT', mandatory=True)
47     conf.check_cfg(package='libavfilter', args='--cflags --libs', uselib_store='AVFILTER', mandatory=True)
48     conf.check_cfg(package='libavcodec', args='--cflags --libs', uselib_store='AVCODEC', mandatory=True)
49     conf.check_cfg(package='libavutil', args='--cflags --libs', uselib_store='AVUTIL', mandatory=True)
50     conf.check_cfg(package='libswscale', args='--cflags --libs', uselib_store='SWSCALE', mandatory=True)
51     conf.check_cfg(package='libswresample', args='--cflags --libs', uselib_store='SWRESAMPLE', mandatory=True)
52     conf.check_cfg(package='libpostproc', args='--cflags --libs', uselib_store='POSTPROC', mandatory=True)
53
54 def static_openjpeg(conf):
55     conf.check_cfg(package='libopenjpeg', args='--cflags', atleast_version='1.5.0', uselib_store='OPENJPEG', mandatory=True)
56     conf.check_cfg(package='libopenjpeg', args='--cflags', max_version='1.5.2', mandatory=True)
57     conf.env.STLIB_OPENJPEG = ['openjpeg']
58
59 def dynamic_openjpeg(conf):
60     conf.check_cfg(package='libopenjpeg', args='--cflags --libs', atleast_version='1.5.0', uselib_store='OPENJPEG', mandatory=True)
61     conf.check_cfg(package='libopenjpeg', args='--cflags --libs', max_version='1.5.2', mandatory=True)
62
63 def static_dcp(conf, static_boost, static_xmlpp, static_xmlsec, static_ssh):
64     conf.check_cfg(package='libdcp', atleast_version='0.98', args='--cflags', uselib_store='DCP', mandatory=True)
65     conf.env.DEFINES_DCP = [f.replace('\\', '') for f in conf.env.DEFINES_DCP]
66     conf.env.STLIB_DCP = ['dcp', 'asdcp-libdcp', 'kumu-libdcp']
67     conf.env.LIB_DCP = ['glibmm-2.4', 'ssl', 'crypto', 'bz2', 'xslt']
68
69     if static_boost:
70         conf.env.STLIB_DCP.append('boost_system')
71
72     if static_xmlpp:
73         conf.env.STLIB_DCP.append('xml++-2.6')
74     else:
75         conf.env.LIB_DCP.append('xml++-2.6')
76
77     if static_xmlsec:
78         conf.env.STLIB_DCP.append('xmlsec1-openssl')
79         conf.env.STLIB_DCP.append('xmlsec1')
80     else:
81         conf.env.LIB_DCP.append('xmlsec1-openssl')
82         conf.env.LIB_DCP.append('xmlsec1')
83
84     if static_ssh:
85         conf.env.STLIB_DCP.append('ssh')
86     else:
87         conf.env.LIB_DCP.append('ssh')
88
89 def dynamic_dcp(conf):
90     conf.check_cfg(package='libdcp', atleast_version='0.97.0', args='--cflags --libs', uselib_store='DCP', mandatory=True)
91     conf.env.DEFINES_DCP = [f.replace('\\', '') for f in conf.env.DEFINES_DCP]
92
93 def dynamic_ssh(conf):
94     conf.check_cc(fragment="""
95                            #include <libssh/libssh.h>\n
96                            int main () {\n
97                            ssh_session s = ssh_new ();\n
98                            return 0;\n
99                            }
100                            """, msg='Checking for library libssh', mandatory=True, lib='ssh', uselib_store='SSH')
101
102 def dynamic_boost(conf, lib_suffix, thread):
103     conf.check_cxx(fragment="""
104                             #include <boost/version.hpp>\n
105                             #if BOOST_VERSION < 104500\n
106                             #error boost too old\n
107                             #endif\n
108                             int main(void) { return 0; }\n
109                             """,
110                    mandatory=True,
111                    msg='Checking for boost library >= 1.45',
112                    okmsg='yes',
113                    errmsg='too old\nPlease install boost version 1.45 or higher.')
114
115     conf.check_cxx(fragment="""
116                             #include <boost/thread.hpp>\n
117                             int main() { boost::thread t (); }\n
118                             """, msg='Checking for boost threading library',
119                             libpath='/usr/local/lib',
120                             lib=[thread, 'boost_system%s' % lib_suffix],
121                             uselib_store='BOOST_THREAD')
122
123     conf.check_cxx(fragment="""
124                             #include <boost/filesystem.hpp>\n
125                             int main() { boost::filesystem::copy_file ("a", "b"); }\n
126                             """, msg='Checking for boost filesystem library',
127                             libpath='/usr/local/lib',
128                             lib=['boost_filesystem%s' % lib_suffix, 'boost_system%s' % lib_suffix],
129                             uselib_store='BOOST_FILESYSTEM')
130
131     conf.check_cxx(fragment="""
132                             #include <boost/date_time.hpp>\n
133                             int main() { boost::gregorian::day_clock::local_day(); }\n
134                             """, msg='Checking for boost datetime library',
135                             libpath='/usr/local/lib',
136                             lib=['boost_date_time%s' % lib_suffix, 'boost_system%s' % lib_suffix],
137                             uselib_store='BOOST_DATETIME')
138
139     conf.check_cxx(fragment="""
140                             #include <boost/signals2.hpp>\n
141                             int main() { boost::signals2::signal<void (int)> x; }\n
142                             """,
143                             msg='Checking for boost signals2 library',
144                             uselib_store='BOOST_SIGNALS2')
145
146 def static_boost(conf, lib_suffix):
147     conf.env.STLIB_BOOST_THREAD = ['boost_thread']
148     conf.env.STLIB_BOOST_FILESYSTEM = ['boost_filesystem%s' % lib_suffix]
149     conf.env.STLIB_BOOST_DATETIME = ['boost_date_time%s' % lib_suffix, 'boost_system%s' % lib_suffix]
150     conf.env.STLIB_BOOST_SIGNALS2 = ['boost_signals2']
151
152 def dynamic_quickmail(conf):
153     conf.check_cxx(fragment="""
154                             #include <quickmail.h>
155                             int main(void) { quickmail_initialize (); }
156                             """,
157                    mandatory=True,
158                    msg='Checking for libquickmail',
159                    libpath='/usr/local/lib',
160                    lib=['quickmail', 'curl'],
161                    uselib_store='QUICKMAIL')
162
163 def configure(conf):
164     conf.load('compiler_cxx')
165     if conf.options.target_windows:
166         conf.load('winres')
167
168     # conf.options -> conf.env
169     conf.env.TARGET_WINDOWS = conf.options.target_windows
170     conf.env.DISABLE_GUI = conf.options.disable_gui
171     conf.env.DISABLE_TESTS = conf.options.disable_tests
172     conf.env.TARGET_DEBIAN = conf.options.target_debian
173     conf.env.DEBIAN_UNSTABLE = conf.options.debian_unstable
174     conf.env.TARGET_CENTOS_6 = conf.options.target_centos_6
175     conf.env.TARGET_CENTOS_7 = conf.options.target_centos_7
176     conf.env.VERSION = VERSION
177     conf.env.TARGET_OSX = sys.platform == 'darwin'
178     conf.env.TARGET_LINUX = not conf.env.TARGET_WINDOWS and not conf.env.TARGET_OSX
179     # true if we should build dcpomatic/libdcpomatic/libdcpomatic-wx statically
180     conf.env.BUILD_STATIC = conf.options.target_debian or conf.options.target_centos_6 or conf.options.target_centos_7
181     if conf.options.install_prefix is None:
182         conf.env.INSTALL_PREFIX = conf.env.PREFIX
183     else:
184         conf.env.INSTALL_PREFIX = conf.options.install_prefix
185
186     # Common CXXFLAGS
187     conf.env.append_value('CXXFLAGS', ['-D__STDC_CONSTANT_MACROS', '-D__STDC_LIMIT_MACROS', '-msse', '-ffast-math', '-fno-strict-aliasing',
188                                        '-Wall', '-Wno-attributes', '-Wextra', '-Wno-unused-result', '-D_FILE_OFFSET_BITS=64'])
189
190     if conf.options.enable_debug:
191         conf.env.append_value('CXXFLAGS', ['-g', '-DDCPOMATIC_DEBUG'])
192     else:
193         conf.env.append_value('CXXFLAGS', '-O2')
194
195     if conf.options.address_sanitizer:
196         conf.env.append_value('CXXFLAGS', ['-fsanitize=address', '-fno-omit-frame-pointer'])
197         conf.env.append_value('LINKFLAGS', ['-fsanitize=address'])
198
199     #
200     # Platform-specific CFLAGS hacks and other tinkering
201     #
202
203     # Windows
204     if conf.env.TARGET_WINDOWS:
205         conf.env.append_value('CXXFLAGS', '-DDCPOMATIC_WINDOWS')
206         conf.env.append_value('CXXFLAGS', '-DWIN32_LEAN_AND_MEAN')
207         conf.env.append_value('CXXFLAGS', '-DBOOST_USE_WINDOWS_H')
208         conf.env.append_value('CXXFLAGS', '-DUNICODE')
209         conf.env.append_value('CXXFLAGS', '-DBOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN')
210         conf.env.append_value('CXXFLAGS', '-mfpmath=sse')
211         wxrc = os.popen('wx-config --rescomp').read().split()[1:]
212         conf.env.append_value('WINRCFLAGS', wxrc)
213         if conf.options.enable_debug:
214             conf.env.append_value('CXXFLAGS', ['-mconsole'])
215             conf.env.append_value('LINKFLAGS', ['-mconsole'])
216         conf.check(lib='ws2_32', uselib_store='WINSOCK2', msg="Checking for library winsock2")
217         conf.check(lib='bfd', uselib_store='BFD', msg="Checking for library bfd")
218         conf.check(lib='dbghelp', uselib_store='DBGHELP', msg="Checking for library dbghelp")
219         conf.check(lib='iberty', uselib_store='IBERTY', msg="Checking for library iberty")
220         conf.check(lib='shlwapi', uselib_store='SHLWAPI', msg="Checking for library shlwapi")
221         conf.check(lib='mswsock', uselib_store='MSWSOCK', msg="Checking for library mswsock")
222         boost_lib_suffix = '-mt'
223         boost_thread = 'boost_thread_win32-mt'
224
225     # POSIX
226     if conf.env.TARGET_LINUX or conf.env.TARGET_OSX:
227         conf.env.append_value('CXXFLAGS', '-DDCPOMATIC_POSIX')
228         conf.env.append_value('CXXFLAGS', '-DPOSIX_LOCALE_PREFIX="%s/share/locale"' % conf.env['INSTALL_PREFIX'])
229         conf.env.append_value('CXXFLAGS', '-DPOSIX_ICON_PREFIX="%s/share/dcpomatic"' % conf.env['INSTALL_PREFIX'])
230         boost_lib_suffix = ''
231         boost_thread = 'boost_thread'
232         conf.env.append_value('LINKFLAGS', '-pthread')
233
234     # Linux
235     if conf.env.TARGET_LINUX:
236         conf.env.append_value('CXXFLAGS', '-mfpmath=sse')
237         conf.env.append_value('CXXFLAGS', '-DDCPOMATIC_LINUX')
238
239     if conf.env.TARGET_DEBIAN:
240         # libxml2 seems to be linked against this on Ubuntu but it doesn't mention it in its .pc file
241         conf.check_cfg(package='liblzma', args='--cflags --libs', uselib_store='LZMA', mandatory=True)
242         
243     if not conf.env.DISABLE_GUI and conf.env.TARGET_LINUX:
244         conf.check_cfg(package='gtk+-2.0', args='--cflags --libs', uselib_store='GTK', mandatory=True)
245
246     # OSX
247     if conf.env.TARGET_OSX:
248         conf.env.append_value('CXXFLAGS', ['-DDCPOMATIC_OSX', '-Wno-unused-function', '-Wno-unused-parameter'])
249         conf.env.append_value('LINKFLAGS', '-headerpad_max_install_names')
250
251     #
252     # Dependencies.
253     # There's probably a neater way of expressing these, but I've gone for brute force for now.
254     #
255
256     if conf.env.TARGET_DEBIAN:
257         conf.check_cfg(package='libcxml', atleast_version='0.11', args='--cflags', uselib_store='CXML', mandatory=True)
258         conf.env.STLIB_CXML = ['cxml']
259         conf.check_cfg(package='libxml++-2.6', args='--cflags --libs', uselib_store='XMLPP', mandatory=True)
260         conf.check_cfg(package='libcurl', args='--cflags --libs', uselib_store='CURL', mandatory=True)
261         conf.env.STLIB_QUICKMAIL = ['quickmail']
262         static_ffmpeg(conf)
263         static_openjpeg(conf)
264         static_dcp(conf, False, False, False, False)
265         dynamic_boost(conf, boost_lib_suffix, boost_thread)
266
267     if conf.env.TARGET_CENTOS_6:
268         # Centos 6.5's boost is too old, so we build a new version statically in the chroot
269         conf.check_cfg(package='libcxml', atleast_version='0.08', args='--cflags --libs-only-L', uselib_store='CXML', mandatory=True)
270         conf.env.STLIB_CXML = ['cxml', 'boost_filesystem']
271         conf.check_cfg(package='libcurl', args='--cflags --libs-only-L', uselib_store='CURL', mandatory=True)
272         conf.env.STLIB_CURL = ['curl']
273         conf.env.LIB_CURL = ['ssh2', 'idn']
274         conf.env.STLIB_QUICKMAIL = ['quickmail', 'curl']
275         conf.env.LIB_QUICKMAIL = ['ssh2', 'idn']
276         static_ffmpeg(conf)
277         static_openjpeg(conf)
278         static_dcp(conf, True, True, True, True)
279         static_boost(conf, boost_lib_suffix)
280
281     if conf.env.TARGET_CENTOS_7:
282         # Centos 7's boost is ok so we link it dynamically
283         conf.check_cfg(package='libcxml', atleast_version='0.08', args='--cflags', uselib_store='CXML', mandatory=True)
284         conf.env.STLIB_CXML = ['cxml']
285         conf.check_cfg(package='libcurl', args='--cflags --libs', uselib_store='CURL', mandatory=True)
286         conf.env.STLIB_QUICKMAIL = ['quickmail']
287         conf.env.LIB_SSH = ['gssapi_krb5']
288         conf.env.LIB_XMLPP = ['xml2']
289         conf.env.LIB_XMLSEC = ['ltdl']
290         static_ffmpeg(conf)
291         static_openjpeg(conf)
292         static_dcp(conf, False, True, True, True)
293         dynamic_boost(conf, boost_lib_suffix, boost_thread)
294
295     if conf.env.TARGET_WINDOWS:
296         conf.check_cfg(package='libxml++-2.6', args='--cflags --libs', uselib_store='XMLPP', mandatory=True)
297         conf.check_cfg(package='libcurl', args='--cflags --libs', uselib_store='CURL', mandatory=True)
298         conf.check_cxx(fragment="""
299                                 #include <boost/locale.hpp>\n
300                                 int main() { std::locale::global (boost::locale::generator().generate ("")); }\n
301                                 """, msg='Checking for boost locale library',
302                                 libpath='/usr/local/lib',
303                                 lib=['boost_locale%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
304                                 uselib_store='BOOST_LOCALE')
305         dynamic_quickmail(conf)
306         dynamic_boost(conf, boost_lib_suffix, boost_thread)
307         dynamic_ffmpeg(conf)
308         dynamic_openjpeg(conf)
309         dynamic_dcp(conf)
310         dynamic_ssh(conf)
311
312     # Not packaging; just a straight build
313     if not conf.env.TARGET_WINDOWS and not conf.env.TARGET_DEBIAN and not conf.env.TARGET_CENTOS_6 and not conf.env.TARGET_CENTOS_7:
314         conf.check_cfg(package='libcxml', atleast_version='0.08', args='--cflags --libs', uselib_store='CXML', mandatory=True)
315         conf.check_cfg(package='libxml++-2.6', args='--cflags --libs', uselib_store='XMLPP', mandatory=True)
316         conf.check_cfg(package='libcurl', args='--cflags --libs', uselib_store='CURL', mandatory=True)
317         dynamic_quickmail(conf)
318         dynamic_boost(conf, boost_lib_suffix, boost_thread)
319         dynamic_ffmpeg(conf)
320         dynamic_dcp(conf)
321         dynamic_openjpeg(conf)
322         dynamic_ssh(conf)
323
324     # Dependencies which are always dynamically linked
325     conf.check_cfg(package='sndfile', args='--cflags --libs', uselib_store='SNDFILE', mandatory=True)
326     conf.check_cfg(package='glib-2.0', args='--cflags --libs', uselib_store='GLIB', mandatory=True)
327     if distutils.spawn.find_executable(conf.options.magickpp_config):
328         conf.check_cfg(package='', path=conf.options.magickpp_config, args='--cppflags --cxxflags --libs', uselib_store='MAGICK', mandatory=True)
329         conf.env.append_value('CXXFLAGS', '-DDCPOMATIC_IMAGE_MAGICK')
330     else:
331         conf.check_cfg(package='GraphicsMagick++', args='--cppflags --cxxflags --libs', uselib_store='MAGICK', mandatory=True)
332         conf.env.append_value('CXXFLAGS', '-DDCPOMATIC_GRAPHICS_MAGICK')
333         
334     conf.check_cfg(package='libzip', args='--cflags --libs', uselib_store='ZIP', mandatory=True)
335
336     conf.check_cc(fragment="""
337                            #include <glib.h>
338                            int main() { g_format_size (1); }
339                            """, msg='Checking for g_format_size ()',
340                            uselib='GLIB',
341                            define_name='HAVE_G_FORMAT_SIZE',
342                            mandatory=False)
343
344     conf.find_program('msgfmt', var='MSGFMT')
345     
346     datadir = conf.env.DATADIR
347     if not datadir:
348         datadir = os.path.join(conf.env.PREFIX, 'share')
349     
350     conf.define('LOCALEDIR', os.path.join(datadir, 'locale'))
351     conf.define('DATADIR', datadir)
352
353     conf.recurse('src')
354     if not conf.env.DISABLE_TESTS:
355         conf.recurse('test')
356
357 def build(bld):
358     create_version_cc(VERSION, bld.env.CXXFLAGS)
359
360     bld.recurse('src')
361     if not bld.env.DISABLE_TESTS:
362         bld.recurse('test')
363     if bld.env.TARGET_WINDOWS:
364         bld.recurse('platform/windows')
365     if bld.env.TARGET_LINUX:
366         bld.recurse('platform/linux')
367     if bld.env.TARGET_OSX:
368         bld.recurse('platform/osx')
369
370     for r in ['22x22', '32x32', '48x48', '64x64', '128x128']:
371         bld.install_files('${PREFIX}/share/icons/hicolor/%s/apps' % r, 'icons/%s/dcpomatic.png' % r)
372
373     if not bld.env.TARGET_WINDOWS:
374         bld.install_files('${PREFIX}/share/dcpomatic', 'icons/taskbar_icon.png')
375
376     bld.add_post_fun(post)
377
378 def git_revision():
379     if not os.path.exists('.git'):
380         return None
381
382     cmd = "LANG= git log --abbrev HEAD^..HEAD ."
383     output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
384     o = output[0].decode('utf-8')
385     return o.replace("commit ", "")[0:10]
386
387 def dist(ctx):
388     r = git_revision()
389     if r is not None:
390         f = open('.git_revision', 'w')
391         print >>f,r
392     f.close()
393
394     ctx.excl = """
395                TODO core *~ src/wx/*~ src/lib/*~ builds/*~ doc/manual/*~ src/tools/*~ *.pyc .waf* build .git
396                deps alignment hacks sync *.tar.bz2 *.exe .lock* *build-windows doc/manual/pdf doc/manual/html
397                GRSYMS GRTAGS GSYMS GTAGS
398                """
399
400
401 def create_version_cc(version, cxx_flags):
402     commit = git_revision()
403     if commit is None and os.path.exists('.git_revision'):
404         f = open('.git_revision', 'r')
405         commit = f.readline().strip()
406     
407     if commit is None:
408         commit = 'release'
409
410     try:
411         text =  '#include "version.h"\n'
412         text += 'char const * dcpomatic_git_commit = \"%s\";\n' % commit
413         text += 'char const * dcpomatic_version = \"%s\";\n' % version
414
415         t = ''
416         for f in cxx_flags:
417             f = f.replace('"', '\\"')
418             t += f + ' '
419         text += 'char const * dcpomatic_cxx_flags = \"%s\";\n' % t[:-1]
420
421         print('Writing version information to src/lib/version.cc')
422         o = open('src/lib/version.cc', 'w')
423         o.write(text)
424         o.close()
425     except IOError:
426         print('Could not open src/lib/version.cc for writing\n')
427         sys.exit(-1)
428     
429 def post(ctx):
430     if ctx.cmd == 'install':
431         ctx.exec_command('/sbin/ldconfig')
432
433 def pot(bld):
434     bld.recurse('src')
435
436 def pot_merge(bld):
437     bld.recurse('src')
438
439 def tags(bld):
440     os.system('etags src/lib/*.cc src/lib/*.h src/wx/*.cc src/wx/*.h src/tools/*.cc src/tools/*.h')