Merge master.
[dcpomatic.git] / wscript
1 import subprocess
2 import os
3 import sys
4
5 APPNAME = 'dcpomatic'
6 VERSION = '1.64.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 Windows')
15     opt.add_option('--static', action='store_true', default=False, help='build statically, and link statically to libdcp and FFmpeg')
16     opt.add_option('--magickpp-config', action='store', default='Magick++-config', help='path to Magick++-config')
17     opt.add_option('--wx-config', action='store', default='wx-config', help='path to wx-config')
18     opt.add_option('--address-sanitizer', action='store_true', default=False, help='build with address sanitizer')
19
20 def pkg_config_args(conf):
21     if conf.env.STATIC:
22         return '--cflags'
23     else:
24         return '--cflags --libs'
25
26 def configure(conf):
27     conf.load('compiler_cxx')
28     if conf.options.target_windows:
29         conf.load('winres')
30
31     conf.env.TARGET_WINDOWS = conf.options.target_windows
32     conf.env.DISABLE_GUI = conf.options.disable_gui
33     conf.env.STATIC = conf.options.static
34     conf.env.VERSION = VERSION
35     conf.env.TARGET_OSX = sys.platform == 'darwin'
36     conf.env.TARGET_LINUX = not conf.env.TARGET_WINDOWS and not conf.env.TARGET_OSX
37
38     # Common CXXFLAGS
39     conf.env.append_value('CXXFLAGS', ['-D__STDC_CONSTANT_MACROS', '-D__STDC_LIMIT_MACROS', '-msse', '-mfpmath=sse', '-ffast-math', '-fno-strict-aliasing',
40                                        '-Wall', '-Wno-attributes', '-Wextra', '-D_FILE_OFFSET_BITS=64'])
41
42     if conf.options.enable_debug:
43         conf.env.append_value('CXXFLAGS', ['-g', '-DDCPOMATIC_DEBUG'])
44     else:
45         conf.env.append_value('CXXFLAGS', '-O2')
46
47     if conf.options.address_sanitizer:
48         conf.env.append_value('CXXFLAGS', ['-fsanitize=address', '-fno-omit-frame-pointer'])
49         conf.env.append_value('LINKFLAGS', ['-fsanitize=address'])
50
51     # Windows-specific
52     if conf.env.TARGET_WINDOWS:
53         conf.env.append_value('CXXFLAGS', ['-DDCPOMATIC_WINDOWS', '-DWIN32_LEAN_AND_MEAN', '-DBOOST_USE_WINDOWS_H', '-DUNICODE', '-DBOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN'])
54         wxrc = os.popen('wx-config --rescomp').read().split()[1:]
55         conf.env.append_value('WINRCFLAGS', wxrc)
56         if conf.options.enable_debug:
57             conf.env.append_value('CXXFLAGS', ['-mconsole'])
58             conf.env.append_value('LINKFLAGS', ['-mconsole'])
59         conf.check(lib='ws2_32', uselib_store='WINSOCK2', msg="Checking for library winsock2")
60         conf.check(lib='bfd', uselib_store='BFD', msg="Checking for library bfd")
61         conf.check(lib='dbghelp', uselib_store='DBGHELP', msg="Checking for library dbghelp")
62         conf.check(lib='iberty', uselib_store='IBERTY', msg="Checking for library iberty")
63         conf.check(lib='shlwapi', uselib_store='SHLWAPI', msg="Checking for library shlwapi")
64         conf.check(lib='mswsock', uselib_store='MSWSOCK', msg="Checking for library mswsock")
65         boost_lib_suffix = '-mt'
66         boost_thread = 'boost_thread_win32-mt'
67
68     # POSIX-specific
69     if conf.env.TARGET_LINUX or conf.env.TARGET_OSX:
70         conf.env.append_value('CXXFLAGS', '-DDCPOMATIC_POSIX')
71         conf.env.append_value('CXXFLAGS', '-DPOSIX_LOCALE_PREFIX="%s/share/locale"' % conf.env['PREFIX'])
72         conf.env.append_value('CXXFLAGS', '-DPOSIX_ICON_PREFIX="%s/share/dcpomatic"' % conf.env['PREFIX'])
73         boost_lib_suffix = ''
74         boost_thread = 'boost_thread'
75         conf.env.append_value('LINKFLAGS', '-pthread')
76
77     # Linux-specific
78     if conf.env.TARGET_LINUX:
79         conf.env.append_value('CXXFLAGS', '-DDCPOMATIC_LINUX')
80         # libxml2 seems to be linked against this on Ubuntu but it doesn't mention it in its .pc file
81         conf.check_cfg(package='liblzma', args='--cflags --libs', uselib_store='LZMA', mandatory=True)
82         if not conf.env.DISABLE_GUI:
83             if conf.env.STATIC:
84                 conf.check_cfg(package='gtk+-2.0', args='--cflags --libs', uselib_store='GTK', mandatory=True)
85             else:
86                 # On Linux we need to be able to include <gtk/gtk.h> to check GTK's version
87                 conf.check_cfg(package='gtk+-2.0', args='--cflags', uselib_store='GTK', mandatory=True)
88
89     # OSX-specific
90     if conf.env.TARGET_OSX:
91         conf.env.append_value('CXXFLAGS', '-DDCPOMATIC_OSX')
92         conf.env.append_value('LINKFLAGS', '-headerpad_max_install_names')
93
94     # Dependencies which are dynamically linked everywhere except --static
95     # Get libs only when we are dynamically linking
96     conf.check_cfg(package='libdcp',        atleast_version='0.92', args=pkg_config_args(conf), uselib_store='DCP',  mandatory=True)
97     # Remove erroneous escaping of quotes from xmlsec1 defines
98     conf.env.DEFINES_DCP = [f.replace('\\', '') for f in conf.env.DEFINES_DCP]
99     conf.check_cfg(package='libcxml',       atleast_version='0.08', args=pkg_config_args(conf), uselib_store='CXML', mandatory=True)
100     conf.check_cfg(package='libavformat',   args=pkg_config_args(conf), uselib_store='AVFORMAT',   mandatory=True)
101     conf.check_cfg(package='libavfilter',   args=pkg_config_args(conf), uselib_store='AVFILTER',   mandatory=True)
102     conf.check_cfg(package='libavcodec',    args=pkg_config_args(conf), uselib_store='AVCODEC',    mandatory=True)
103     conf.check_cfg(package='libavutil',     args=pkg_config_args(conf), uselib_store='AVUTIL',     mandatory=True)
104     conf.check_cfg(package='libswscale',    args=pkg_config_args(conf), uselib_store='SWSCALE',    mandatory=True)
105     conf.check_cfg(package='libswresample', args=pkg_config_args(conf), uselib_store='SWRESAMPLE', mandatory=True)
106     conf.check_cfg(package='libpostproc',   args=pkg_config_args(conf), uselib_store='POSTPROC',   mandatory=True)
107     conf.check_cfg(package='libopenjpeg',   args=pkg_config_args(conf), atleast_version='1.5.0', uselib_store='OPENJPEG', mandatory=True)
108     conf.check_cfg(package='libopenjpeg',   args=pkg_config_args(conf), max_version='1.5.1', mandatory=True)
109
110     if conf.env.STATIC:
111         # This is hackio grotesquio for static builds (ie for .deb packages).  We need to link some things
112         # statically and some dynamically, or things get horribly confused and the dynamic linker (I think)
113         # crashes.  These calls do what the check_cfg calls would have done, but specify the
114         # different bits as static or dynamic as required.  It'll break if you look at it funny, but
115         # I think anyone else who builds would do so dynamically.
116         conf.env.STLIB_CXML       = ['cxml']
117         conf.env.STLIB_DCP        = ['dcp', 'asdcp-libdcp', 'kumu-libdcp']
118         conf.env.LIB_DCP          = ['glibmm-2.4', 'xml++-2.6', 'ssl', 'crypto', 'bz2', 'xmlsec1', 'xmlsec1-openssl', 'xslt']
119         conf.env.STLIB_CXML       = ['cxml']
120         conf.env.STLIB_AVFORMAT   = ['avformat']
121         conf.env.STLIB_AVFILTER   = ['avfilter', 'swresample']
122         conf.env.STLIB_AVCODEC    = ['avcodec']
123         conf.env.LIB_AVCODEC      = ['z']
124         conf.env.STLIB_AVUTIL     = ['avutil']
125         conf.env.STLIB_SWSCALE    = ['swscale']
126         conf.env.STLIB_POSTPROC   = ['postproc']
127         conf.env.STLIB_SWRESAMPLE = ['swresample']
128         conf.env.STLIB_OPENJPEG   = ['openjpeg']
129         conf.env.STLIB_QUICKMAIL  = ['quickmail']
130     else:
131         conf.check_cxx(fragment="""
132                             #include <quickmail.h>
133                             int main(void) { quickmail_initialize (); }
134                             """,
135                        mandatory=True,
136                        msg='Checking for libquickmail',
137                        libpath='/usr/local/lib',
138                        lib=['quickmail', 'curl'],
139                        uselib_store='QUICKMAIL')
140
141     # Dependencies which are always dynamically linked
142     conf.check_cfg(package='sndfile', args='--cflags --libs', uselib_store='SNDFILE', mandatory=True)
143     conf.check_cfg(package='glib-2.0', args='--cflags --libs', uselib_store='GLIB', mandatory=True)
144     conf.check_cfg(package= '', path=conf.options.magickpp_config, args='--cppflags --cxxflags --libs', uselib_store='MAGICK', mandatory=True)
145     conf.check_cfg(package='libxml++-2.6', args='--cflags --libs', uselib_store='XML++', mandatory=True)
146     conf.check_cfg(package='libcurl', args='--cflags --libs', uselib_store='CURL', mandatory=True)
147     conf.check_cfg(package='libzip', args='--cflags --libs', uselib_store='ZIP', mandatory=True)
148     conf.check_cfg(package='pangomm-1.4', args='--cflags --libs', uselib_store='PANGOMM', mandatory=True)
149     conf.check_cfg(package='cairomm-1.0', args='--cflags --libs', uselib_store='CAIROMM', mandatory=True)
150
151     conf.check_cxx(fragment="""
152                             #include <boost/version.hpp>\n
153                             #if BOOST_VERSION < 104500\n
154                             #error boost too old\n
155                             #endif\n
156                             int main(void) { return 0; }\n
157                             """,
158                    mandatory=True,
159                    msg='Checking for boost library >= 1.45',
160                    okmsg='yes',
161                    errmsg='too old\nPlease install boost version 1.45 or higher.')
162
163     conf.check_cc(fragment="""
164                            #include <libssh/libssh.h>\n
165                            int main () {\n
166                            ssh_session s = ssh_new ();\n
167                            return 0;\n
168                            }
169                            """, msg='Checking for library libssh', mandatory=True, lib='ssh', uselib_store='SSH')
170
171     conf.check_cxx(fragment="""
172                             #include <boost/thread.hpp>\n
173                             int main() { boost::thread t (); }\n
174                             """, msg='Checking for boost threading library',
175                             libpath='/usr/local/lib',
176                             lib=[boost_thread, 'boost_system%s' % boost_lib_suffix],
177                             uselib_store='BOOST_THREAD')
178
179     conf.check_cxx(fragment="""
180                             #include <boost/filesystem.hpp>\n
181                             int main() { boost::filesystem::copy_file ("a", "b"); }\n
182                             """, msg='Checking for boost filesystem library',
183                             libpath='/usr/local/lib',
184                             lib=['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
185                             uselib_store='BOOST_FILESYSTEM')
186
187     conf.check_cxx(fragment="""
188                             #include <boost/date_time.hpp>\n
189                             int main() { boost::gregorian::day_clock::local_day(); }\n
190                             """, msg='Checking for boost datetime library',
191                             libpath='/usr/local/lib',
192                             lib=['boost_date_time%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
193                             uselib_store='BOOST_DATETIME')
194
195     # Only Windows versions require boost::locale, which is handy, as it was only introduced in
196     # boost 1.48 and so isn't (easily) available on old Ubuntus.
197     if conf.env.TARGET_WINDOWS:
198         conf.check_cxx(fragment="""
199                                 #include <boost/locale.hpp>\n
200                                 int main() { std::locale::global (boost::locale::generator().generate ("")); }\n
201                                 """, msg='Checking for boost locale library',
202                                 libpath='/usr/local/lib',
203                                 lib=['boost_locale%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
204                                 uselib_store='BOOST_LOCALE')
205
206     conf.check_cxx(fragment="""
207                             #include <boost/signals2.hpp>\n
208                             int main() { boost::signals2::signal<void (int)> x; }\n
209                             """,
210                             msg='Checking for boost signals2 library',
211                             uselib_store='BOOST_SIGNALS2')
212
213     conf.check_cc(fragment="""
214                            #include <glib.h>
215                            int main() { g_format_size (1); }
216                            """, msg='Checking for g_format_size ()',
217                            uselib='GLIB',
218                            define_name='HAVE_G_FORMAT_SIZE',
219                            mandatory=False)
220
221     conf.find_program('msgfmt', var='MSGFMT')
222     
223     datadir = conf.env.DATADIR
224     if not datadir:
225         datadir = os.path.join(conf.env.PREFIX, 'share')
226     
227     conf.define('LOCALEDIR', os.path.join(datadir, 'locale'))
228     conf.define('DATADIR', datadir)
229
230     conf.recurse('src')
231     conf.recurse('test')
232
233 def build(bld):
234     create_version_cc(VERSION, bld.env.CXXFLAGS)
235
236     bld.recurse('src')
237     bld.recurse('test')
238     if bld.env.TARGET_WINDOWS:
239         bld.recurse('platform/windows')
240     if bld.env.TARGET_LINUX:
241         bld.recurse('platform/linux')
242     if bld.env.TARGET_OSX:
243         bld.recurse('platform/osx')
244
245     for r in ['22x22', '32x32', '48x48', '64x64', '128x128']:
246         bld.install_files('${PREFIX}/share/icons/hicolor/%s/apps' % r, 'icons/%s/dcpomatic.png' % r)
247
248     if not bld.env.TARGET_WINDOWS:
249         bld.install_files('${PREFIX}/share/dcpomatic', 'icons/taskbar_icon.png')
250
251     bld.add_post_fun(post)
252
253 def git_revision():
254     if not os.path.exists('.git'):
255         return None
256
257     cmd = "LANG= git log --abbrev HEAD^..HEAD ."
258     output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
259     o = output[0].decode('utf-8')
260     return o.replace("commit ", "")[0:10]
261
262 def dist(ctx):
263     r = git_revision()
264     if r is not None:
265         f = open('.git_revision', 'w')
266         print >>f,r
267     f.close()
268
269     ctx.excl = """
270                TODO core *~ src/wx/*~ src/lib/*~ builds/*~ doc/manual/*~ src/tools/*~ *.pyc .waf* build .git
271                deps alignment hacks sync *.tar.bz2 *.exe .lock* *build-windows doc/manual/pdf doc/manual/html
272                GRSYMS GRTAGS GSYMS GTAGS
273                """
274
275
276 def create_version_cc(version, cxx_flags):
277     commit = git_revision()
278     if commit is None and os.path.exists('.git_revision'):
279         f = open('.git_revision', 'r')
280         commit = f.readline().strip()
281     
282     if commit is None:
283         commit = 'release'
284
285     try:
286         text =  '#include "version.h"\n'
287         text += 'char const * dcpomatic_git_commit = \"%s\";\n' % commit
288         text += 'char const * dcpomatic_version = \"%s\";\n' % version
289
290         t = ''
291         for f in cxx_flags:
292             f = f.replace('"', '\\"')
293             t += f + ' '
294         text += 'char const * dcpomatic_cxx_flags = \"%s\";\n' % t[:-1]
295
296         print('Writing version information to src/lib/version.cc')
297         o = open('src/lib/version.cc', 'w')
298         o.write(text)
299         o.close()
300     except IOError:
301         print('Could not open src/lib/version.cc for writing\n')
302         sys.exit(-1)
303     
304 def post(ctx):
305     if ctx.cmd == 'install':
306         ctx.exec_command('/sbin/ldconfig')
307
308 def pot(bld):
309     bld.recurse('src')
310
311 def pot_merge(bld):
312     bld.recurse('src')