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