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