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