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