Make some not-so-important CPL read errors non-fatal (DoM #2797).
[libdcp.git] / wscript
1 #
2 #    Copyright (C) 2012-2022 Carl Hetherington <cth@carlh.net>
3 #
4 #    This file is part of libdcp.
5 #
6 #    libdcp is free software; you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation; either version 2 of the License, or
9 #    (at your option) any later version.
10 #
11 #    libdcp is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #    In addition, as a special exception, the copyright holders give
20 #    permission to link the code of portions of this program with the
21 #    OpenSSL library under certain conditions as described in each
22 #    individual source file, and distribute linked combinations
23 #    including the two.
24 #
25 #    You must obey the GNU General Public License in all respects
26 #    for all of the code used other than OpenSSL.  If you modify
27 #    file(s) with this exception, you may extend this exception to your
28 #    version of the file(s), but you are not obligated to do so.  If you
29 #    do not wish to do so, delete this exception statement from your
30 #    version.  If you delete this exception statement from all source
31 #    files in the program, then also delete it here.
32 #
33
34 import subprocess
35 import os
36 import sys
37 import shlex
38 import distutils.spawn
39 from waflib import Logs, Context
40
41 APPNAME = 'libdcp'
42
43 this_version = subprocess.Popen(shlex.split('git tag -l --points-at HEAD'), stdout=subprocess.PIPE).communicate()[0].decode('UTF-8')
44 last_version = subprocess.Popen(shlex.split('git describe --tags --abbrev=0'), stdout=subprocess.PIPE).communicate()[0].decode('UTF-8')
45
46 if this_version == '':
47     VERSION = '%sdevel' % last_version[1:].strip()
48 else:
49     VERSION = this_version[1:].strip()
50
51 if sys.version_info.major == 2:
52     # Handle Python 2 (for Ubuntu 16.04)
53     VERSION = VERSION.encode('UTF-8')
54
55 API_VERSION = '-1.0'
56
57 def options(opt):
58     opt.load('compiler_cxx')
59     opt.add_option('--target-windows-64', action='store_true', default=False, help='set up to do a cross-compile to Windows 64-bit')
60     opt.add_option('--target-windows-32', action='store_true', default=False, help='set up to do a cross-compile to Windows 32-bit')
61     opt.add_option('--enable-debug', action='store_true', default=False, help='build with debugging information and without optimisation')
62     opt.add_option('--static', action='store_true', default=False, help='build libdcp statically, and link statically to openjpeg, cxml, asdcplib-carl')
63     opt.add_option('--disable-tests', action='store_true', default=False, help='disable building of tests')
64     opt.add_option('--disable-benchmarks', action='store_true', default=False, help='disable building of benchmarks')
65     opt.add_option('--enable-gcov', action='store_true', default=False, help='use gcov in tests')
66     opt.add_option('--disable-examples', action='store_true', default=False, help='disable building of examples')
67     opt.add_option('--disable-dumpimage', action='store_true', default=False, help='disable building of dcpdumpimage')
68     opt.add_option('--enable-openmp', action='store_true', default=False, help='enable use of OpenMP')
69     opt.add_option('--openmp', default='gomp', help='specify OpenMP Library to use: omp, gomp (default), iomp')
70
71 def configure(conf):
72     conf.load('compiler_cxx')
73     conf.load('clang_compilation_database', tooldir=['waf-tools'])
74     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-D_FILE_OFFSET_BITS=64', '-D__STDC_FORMAT_MACROS', '-std=c++11'])
75     gcc = conf.env['CC_VERSION']
76     if int(gcc[0]) >= 4 and int(gcc[1]) > 1:
77         conf.env.append_value('CXXFLAGS', ['-Wno-maybe-uninitialized'])
78     conf.env.append_value('CXXFLAGS', ['-DLIBDCP_VERSION="%s"' % VERSION])
79
80     conf.env.TARGET_WINDOWS_64 = conf.options.target_windows_64
81     conf.env.TARGET_WINDOWS_32 = conf.options.target_windows_32
82     conf.env.TARGET_OSX = sys.platform == 'darwin'
83     conf.env.TARGET_LINUX = not conf.env.TARGET_WINDOWS and not conf.env.TARGET_OSX
84     conf.env.ENABLE_DEBUG = conf.options.enable_debug
85     conf.env.DISABLE_TESTS = conf.options.disable_tests
86     conf.env.DISABLE_BENCHMARKS = conf.options.disable_benchmarks
87     conf.env.DISABLE_EXAMPLES = conf.options.disable_examples
88     conf.env.DISABLE_DUMPIMAGE = conf.options.disable_dumpimage
89     conf.env.STATIC = conf.options.static
90     conf.env.API_VERSION = API_VERSION
91
92     if conf.env.TARGET_WINDOWS_64 or conf.env.TARGET_WINDOWS_32:
93         conf.env.append_value('CXXFLAGS', '-DLIBDCP_WINDOWS')
94     if conf.env.TARGET_OSX:
95         conf.env.append_value('CXXFLAGS', '-DLIBDCP_OSX')
96     if conf.env.TARGET_LINUX:
97         conf.env.append_value('CXXFLAGS', '-DLIBDCP_LINUX')
98
99     if conf.env.TARGET_OSX:
100         conf.env.append_value('CXXFLAGS', ['-Wno-unused-result', '-Wno-unused-parameter', '-Wno-unused-local-typedef'])
101         conf.env.append_value('LINKFLAGS', '-headerpad_max_install_names')
102     elif int(gcc[0]) > 4:
103         conf.env.append_value('CXXFLAGS', ['-Wsuggest-override'])
104
105     # Disable libxml++ deprecation warnings for now
106     conf.env.append_value('CXXFLAGS', ['-Wno-deprecated-declarations'])
107
108     if conf.options.enable_openmp:
109         conf.env.append_value('CXXFLAGS', ['-fopenmp', '-DLIBDCP_OPENMP'])
110         conf.env.LIB_OPENMP = [conf.options.openmp]
111         conf.env.append_value('LDFLAGS', ['-l%s' % conf.options.openmp])
112         conf.check_cxx(cxxflags='-fopenmp', msg='Checking that compiler supports -fopenmp')
113
114     if not conf.env.TARGET_WINDOWS_64 and not conf.env.TARGET_WINDOWS_32:
115         conf.env.append_value('LINKFLAGS', '-pthread')
116
117     if conf.env.TARGET_LINUX:
118         conf.check(lib='dl', uselib_store='DL', msg='Checking for library dl')
119
120     conf.check_cfg(package='openssl', args='--cflags --libs', uselib_store='OPENSSL', mandatory=True)
121     conf.check_cfg(package='libxml++-2.6', args='--cflags --libs', uselib_store='LIBXML++', mandatory=True)
122     conf.check_cfg(package='xmlsec1', args='--cflags --libs', uselib_store='XMLSEC1', mandatory=True)
123     # Remove erroneous escaping of quotes from xmlsec1 defines
124     conf.env.DEFINES_XMLSEC1 = [f.replace('\\', '') for f in conf.env.DEFINES_XMLSEC1]
125
126     # ImageMagick / GraphicsMagick
127     if (not conf.options.disable_examples) and (not conf.options.disable_dumpimage):
128         if distutils.spawn.find_executable('Magick++-config'):
129             conf.check_cfg(package='', path='Magick++-config', args='--cppflags --cxxflags --libs', uselib_store='MAGICK', mandatory=True, msg='Checking for ImageMagick/GraphicsMagick')
130         else:
131             image = conf.check_cfg(package='ImageMagick++', args='--cflags --libs', uselib_store='MAGICK', mandatory=False)
132             graphics = conf.check_cfg(package='GraphicsMagick++', args='--cflags --libs', uselib_store='MAGICK', mandatory=False)
133             if image is None and graphics is None:
134                 Logs.error('Neither ImageMagick++ nor GraphicsMagick++ found: one or the other is required unless you ./waf configure --disable-examples --disable-dumpimage')
135                 sys.exit(1)
136
137     conf.check_cfg(package='sndfile', args='--cflags --libs', uselib_store='SNDFILE', mandatory=False)
138
139     # Find openjpeg so that we can test to see if it's the right version
140     if conf.options.static:
141         conf.check_cfg(package='libopenjp2', args='--cflags', uselib_store='OPENJPEG', mandatory=True, msg='Checking for any version of libopenjp2')
142     else:
143         conf.check_cfg(package='libopenjp2', args='--cflags --libs', uselib_store='OPENJPEG', mandatory=True, msg='Checking for any version of libopenjp2')
144
145     patched_openjpeg = conf.check_cxx(fragment="""
146                    #include <openjpeg.h>
147                    int main() { opj_cparameters_t p; p.numgbits = 2; }\n
148                    """,
149                    msg='Checking for numgbits in opj_cparameters_t',
150                    use='OPENJPEG',
151                    mandatory=False,
152                    define_name='LIBDCP_HAVE_NUMGBITS')
153
154     if not patched_openjpeg:
155         # We don't have our patched version so we need 2.5.0 to get the GUARD_BITS option
156         conf.check_cfg(package='libopenjp2', args='libopenjp2 >= 2.5.0', uselib_store='OPENJPEG', mandatory=True, msg='Checking for libopenjp2 >= 2.5.0')
157
158     if conf.options.static:
159         conf.env.STLIB_OPENJPEG = ['openjp2']
160         conf.check_cfg(package='libasdcp-carl', args='libasdcp-carl >= 0.1.3 --cflags', uselib_store='ASDCPLIB_CTH', mandatory=True)
161         conf.env.HAVE_ASDCPLIB_CTH = 1
162         conf.env.STLIB_ASDCPLIB_CTH = ['asdcp-carl', 'kumu-carl']
163         conf.env.HAVE_CXML = 1
164         conf.env.LIB_CXML = ['xml++-2.6', 'glibmm-2.4']
165         conf.env.STLIB_CXML = ['cxml']
166         conf.check_cfg(package='xerces-c', args='--cflags', uselib_store='XERCES', mandatory=True)
167         conf.env.LIB_XERCES = ['xerces-c', 'icuuc', 'curl']
168     else:
169         conf.check_cfg(package='libasdcp-carl', args='libasdcp-carl >= 0.1.3 --cflags --libs', uselib_store='ASDCPLIB_CTH', mandatory=True)
170         conf.check_cfg(package='libcxml', args='libcxml >= 0.17.0 --cflags --libs', uselib_store='CXML', mandatory=True)
171         conf.check_cfg(package='xerces-c', args='--cflags --libs', uselib_store='XERCES', mandatory=True)
172
173     if conf.env.TARGET_WINDOWS_64 or conf.env.TARGET_WINDOWS_32:
174         # XXX: it feels like there should be a more elegant way to get these included
175         conf.env.LIB_XERCES.append('curl')
176         conf.env.LIB_XERCES.append('ws2_32')
177
178     if conf.options.target_windows_64:
179         boost_lib_suffix = '-mt-x64'
180     elif conf.options.target_windows_32:
181         boost_lib_suffix = '-mt-x32'
182     else:
183         boost_lib_suffix = ''
184
185     if conf.options.enable_debug:
186         conf.env.append_value('CXXFLAGS', '-g')
187     else:
188         # Somewhat experimental use of -O2 rather than -O3 to see if
189         # Windows builds are any more reliable
190         conf.env.append_value('CXXFLAGS', '-O2')
191
192     # We support older boosts on Linux so we can use the distribution-provided package
193     # on Centos 7, but it's good if we can use 1.61 for boost::dll::program_location()
194     boost_version = ('1.45', '104500') if conf.env.TARGET_LINUX else ('1.61', '106800')
195
196     conf.check_cxx(fragment="""
197                             #include <boost/version.hpp>\n
198                             #if BOOST_VERSION < %s\n
199                             #error boost too old\n
200                             #endif\n
201                             int main(void) { return 0; }\n
202                             """ % boost_version[1],
203                    mandatory=True,
204                    msg='Checking for boost library >= %s' % boost_version[0],
205                    okmsg='yes',
206                    errmsg='too old\nPlease install boost version %s or higher.' % boost_version[0])
207
208     conf.check_cxx(fragment="""
209                             #include <boost/filesystem.hpp>\n
210                             int main() { boost::filesystem::copy_file ("a", "b"); }\n
211                             """,
212                    msg='Checking for boost filesystem library',
213                    libpath='/usr/local/lib',
214                    lib=['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
215                    uselib_store='BOOST_FILESYSTEM')
216
217     conf.check_cxx(fragment="""
218                    #include <boost/filesystem.hpp>\n
219                    int main() { boost::filesystem::weakly_canonical("a/b/c"); }\n
220                    """,
221                    mandatory=False,
222                    msg='Checking for boost::filesystem::weakly_canonical',
223                    uselib='BOOST_FILESYSTEM',
224                    define_name='LIBDCP_HAVE_WEAKLY_CANONICAL')
225
226     conf.check_cxx(fragment="""
227                             #include <boost/signals2.hpp>\n
228                             int main() { boost::signals2::signal<void (int)> x; }\n
229                             """,
230                    msg='Checking for boost signals2 library',
231                    uselib_store='BOOST_SIGNALS2')
232
233     conf.check_cxx(fragment="""
234                             #include <boost/date_time.hpp>\n
235                             int main() { boost::gregorian::day_clock::local_day(); }\n
236                             """,
237                    msg='Checking for boost datetime library',
238                    libpath='/usr/local/lib',
239                    lib=['boost_date_time%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
240                    uselib_store='BOOST_DATETIME')
241
242     if not conf.env.DISABLE_TESTS:
243         conf.recurse('test')
244         if conf.options.enable_gcov:
245             conf.check(lib='gcov', define_name='HAVE_GCOV', mandatory=False)
246             conf.env.append_value('LINKFLAGS', '-fprofile-arcs')
247
248 def build(bld):
249     create_version_cc(bld, VERSION)
250
251     if bld.env.TARGET_WINDOWS_64:
252         boost_lib_suffix = '-mt-x64'
253     elif bld.env.TARGET_WINDOWS_32:
254         boost_lib_suffix = '-mt-x32'
255     else:
256         boost_lib_suffix = ''
257
258     libs="-L${libdir} -ldcp%s -lcxml -lboost_system%s" % (bld.env.API_VERSION, boost_lib_suffix)
259     if bld.env.TARGET_LINUX:
260         libs += " -ldl"
261
262     bld(source='libdcp%s.pc.in' % bld.env.API_VERSION,
263         version=VERSION,
264         includedir='%s/include/libdcp%s' % (bld.env.PREFIX, bld.env.API_VERSION),
265         libs=libs,
266         install_path='${LIBDIR}/pkgconfig')
267
268     bld.recurse('src')
269     bld.recurse('tools')
270     if not bld.env.DISABLE_TESTS:
271         bld.recurse('test')
272     if not bld.env.DISABLE_EXAMPLES:
273         bld.recurse('examples')
274     if not bld.env.DISABLE_BENCHMARKS:
275         bld.recurse('benchmark')
276
277     for i in os.listdir('xsd'):
278         bld.install_files('${PREFIX}/share/libdcp/xsd', os.path.join('xsd', i))
279
280     for i in ['language', 'region', 'script', 'variant', 'extlang', 'dcnc']:
281         bld.install_files('${PREFIX}/share/libdcp/tags', os.path.join('tags', i))
282
283     bld.install_files('${PREFIX}/share/libdcp', 'ratings')
284
285     bld.add_post_fun(post)
286
287 def dist(ctx):
288     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__ GPATH GRTAGS GSYMS GTAGS'
289
290 def create_version_cc(bld, version):
291     if os.path.exists('.git'):
292         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
293         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
294         o = output[0].decode('utf-8')
295         commit = o.replace ("commit ", "")[0:10]
296     else:
297         commit = "release"
298
299     try:
300         text =  '#include "version.h"\n'
301         text += 'char const * dcp::git_commit = \"%s\";\n' % commit
302         text += 'char const * dcp::version = \"%s\";\n' % version
303         if bld.env.ENABLE_DEBUG:
304             debug_string = 'true'
305         else:
306             debug_string = 'false'
307         text += 'bool const dcp::built_with_debug = %s;\n' % debug_string
308         print('Writing version information to src/version.cc')
309         o = open('src/version.cc', 'w')
310         o.write(text)
311         o.close()
312     except IOError:
313         print('Could not open src/version.cc for writing\n')
314         sys.exit(-1)
315
316 def post(ctx):
317     if ctx.cmd == 'install':
318         ctx.exec_command('/sbin/ldconfig')
319
320 def tags(bld):
321     os.system('etags src/*.cc src/*.h')