Validate XML with xerces.
[libdcp.git] / wscript
1 #
2 #    Copyright (C) 2012-2019 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]
44 last_version = subprocess.Popen(shlex.split('git describe --tags --abbrev=0'), stdout=subprocess.PIPE).communicate()[0]
45
46 if this_version == '':
47     VERSION = '%sdevel' % last_version[1:].strip()
48 else:
49     VERSION = this_version[1:].strip()
50
51 API_VERSION = '-1.0'
52
53 def options(opt):
54     opt.load('compiler_cxx')
55     opt.add_option('--target-windows', action='store_true', default=False, help='set up to do a cross-compile to Windows')
56     opt.add_option('--enable-debug', action='store_true', default=False, help='build with debugging information and without optimisation')
57     opt.add_option('--static', action='store_true', default=False, help='build libdcp statically, and link statically to openjpeg, cxml, asdcplib-carl')
58     opt.add_option('--disable-tests', action='store_true', default=False, help='disable building of tests')
59     opt.add_option('--disable-gcov', action='store_true', default=False, help='don''t use gcov in tests')
60     opt.add_option('--disable-examples', action='store_true', default=False, help='disable building of examples')
61     opt.add_option('--enable-openmp', action='store_true', default=False, help='enable use of OpenMP')
62     opt.add_option('--openmp', default='gomp', help='specify OpenMP Library to use: omp, gomp (default), iomp')
63     opt.add_option('--jpeg', default='oj2', help='specify JPEG library to build with: oj1 or oj2 for OpenJPEG 1.5.x or OpenJPEG 2.1.x respectively')
64     opt.add_option('--force-cpp11', action='store_true', default=False, help='force use of C++11')
65
66 def configure(conf):
67     conf.load('compiler_cxx')
68     conf.load('clang_compilation_database', tooldir=['waf-tools'])
69     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-D_FILE_OFFSET_BITS=64', '-D__STDC_FORMAT_MACROS'])
70     if conf.options.force_cpp11:
71         conf.env.append_value('CXXFLAGS', ['-std=c++11', '-DBOOST_NO_CXX11_SCOPED_ENUMS'])
72     gcc = conf.env['CC_VERSION']
73     if int(gcc[0]) >= 4 and int(gcc[1]) > 1:
74         conf.env.append_value('CXXFLAGS', ['-Wno-maybe-uninitialized'])
75     conf.env.append_value('CXXFLAGS', ['-DLIBDCP_VERSION="%s"' % VERSION])
76
77     conf.env.TARGET_WINDOWS = conf.options.target_windows
78     conf.env.TARGET_OSX = sys.platform == 'darwin'
79     conf.env.ENABLE_DEBUG = conf.options.enable_debug
80     conf.env.DISABLE_TESTS = conf.options.disable_tests
81     conf.env.DISABLE_EXAMPLES = conf.options.disable_examples
82     conf.env.STATIC = conf.options.static
83     conf.env.API_VERSION = API_VERSION
84
85     if conf.options.target_windows:
86         conf.env.append_value('CXXFLAGS', '-DLIBDCP_WINDOWS')
87     else:
88         conf.env.append_value('CXXFLAGS', '-DLIBDCP_POSIX')
89
90     if conf.env.TARGET_OSX:
91         conf.env.append_value('CXXFLAGS', ['-Wno-unused-result', '-Wno-unused-parameter', '-Wno-unused-local-typedef'])
92
93     # Disable libxml++ deprecation warnings for now
94     conf.env.append_value('CXXFLAGS', ['-Wno-deprecated-declarations'])
95
96     if conf.options.enable_openmp:
97         conf.env.append_value('CXXFLAGS', ['-fopenmp', '-DLIBDCP_OPENMP'])
98         conf.env.LIB_OPENMP = [conf.options.openmp]
99         conf.env.append_value('LDFLAGS', ['-l%s' % conf.options.openmp])
100         conf.check_cxx(cxxflags='-fopenmp', msg='Checking that compiler supports -fopenmp')
101
102     if not conf.env.TARGET_WINDOWS:
103         conf.env.append_value('LINKFLAGS', '-pthread')
104
105     if conf.options.jpeg == 'oj1':
106         conf.env.append_value('CXXFLAGS', ['-DLIBDCP_OPENJPEG1'])
107     elif conf.options.jpeg == 'oj2':
108         conf.env.append_value('CXXFLAGS', ['-DLIBDCP_OPENJPEG2'])
109     else:
110         Logs.error('Invalid --jpeg value %s' % conf.options.jpeg)
111
112     conf.check_cfg(package='openssl', args='--cflags --libs', uselib_store='OPENSSL', mandatory=True)
113     conf.check_cfg(package='libxml++-2.6', args='--cflags --libs', uselib_store='LIBXML++', mandatory=True)
114     conf.check_cfg(package='xmlsec1', args='--cflags --libs', uselib_store='XMLSEC1', mandatory=True)
115     # Remove erroneous escaping of quotes from xmlsec1 defines
116     conf.env.DEFINES_XMLSEC1 = [f.replace('\\', '') for f in conf.env.DEFINES_XMLSEC1]
117
118     # ImageMagick / GraphicsMagick
119     if distutils.spawn.find_executable('Magick++-config'):
120         conf.check_cfg(package='', path='Magick++-config', args='--cppflags --cxxflags --libs', uselib_store='MAGICK', mandatory=True, msg='Checking for ImageMagick/GraphicsMagick')
121     else:
122         image = conf.check_cfg(package='ImageMagick++', args='--cflags --libs', uselib_store='MAGICK', mandatory=False)
123         graphics = conf.check_cfg(package='GraphicsMagick++', args='--cflags --libs', uselib_store='MAGICK', mandatory=False)
124         if image is None and graphics is None:
125             Logs.error('Neither ImageMagick++ nor GraphicsMagick++ found: one or the other is required')
126
127     conf.check_cfg(package='sndfile', args='--cflags --libs', uselib_store='SNDFILE', mandatory=False)
128     conf.check_cfg(package='xerces-c', args='--cflags --libs', uselib_store='XERCES', mandatory=False)
129
130     if conf.options.static:
131         if conf.options.jpeg == 'oj2':
132             conf.check_cfg(package='libopenjp2', args='--cflags', atleast_version='2.1.0', uselib_store='OPENJPEG', mandatory=True)
133             conf.env.STLIB_OPENJPEG = ['openjp2']
134         elif conf.options.jpeg == 'oj1':
135             conf.check_cfg(package='libopenjpeg1', args='--cflags', atleast_version='1.5.0', uselib_store='OPENJPEG', mandatory=True)
136             conf.env.STLIB_OPENJPEG = ['openjpeg']
137         conf.check_cfg(package='libasdcp-carl', atleast_version='0.1.3', args='--cflags', uselib_store='ASDCPLIB_CTH', mandatory=True)
138         conf.env.HAVE_ASDCPLIB_CTH = 1
139         conf.env.STLIB_ASDCPLIB_CTH = ['asdcp-carl', 'kumu-carl']
140         conf.env.HAVE_CXML = 1
141         conf.env.LIB_CXML = ['xml++-2.6', 'glibmm-2.4']
142         conf.env.STLIB_CXML = ['cxml']
143     else:
144         if conf.options.jpeg == 'oj2':
145             conf.check_cfg(package='libopenjp2', args='--cflags --libs', atleast_version='2.1.0', uselib_store='OPENJPEG', mandatory=True)
146         elif conf.options.jpeg == 'oj1':
147             conf.check_cfg(package='libopenjpeg1', args='--cflags --libs', atleast_version='1.5.0', uselib_store='OPENJPEG', mandatory=True)
148         conf.check_cfg(package='libasdcp-carl', atleast_version='0.1.3', args='--cflags --libs', uselib_store='ASDCPLIB_CTH', mandatory=True)
149         conf.check_cfg(package='libcxml', atleast_version='0.16.0', args='--cflags --libs', uselib_store='CXML', mandatory=True)
150
151     if conf.options.target_windows:
152         boost_lib_suffix = '-mt'
153     else:
154         boost_lib_suffix = ''
155
156     if conf.options.enable_debug:
157         conf.env.append_value('CXXFLAGS', '-g')
158     else:
159         # Somewhat experimental use of -O2 rather than -O3 to see if
160         # Windows builds are any more reliable
161         conf.env.append_value('CXXFLAGS', '-O2')
162
163     conf.check_cxx(fragment="""
164                             #include <boost/version.hpp>\n
165                             #if BOOST_VERSION < 104500\n
166                             #error boost too old\n
167                             #endif\n
168                             int main(void) { return 0; }\n
169                             """,
170                    mandatory=True,
171                    msg='Checking for boost library >= 1.45',
172                    okmsg='yes',
173                    errmsg='too old\nPlease install boost version 1.45 or higher.')
174
175     conf.check_cxx(fragment="""
176                             #include <boost/filesystem.hpp>\n
177                             int main() { boost::filesystem::copy_file ("a", "b"); }\n
178                             """,
179                    msg='Checking for boost filesystem library',
180                    libpath='/usr/local/lib',
181                    lib=['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
182                    uselib_store='BOOST_FILESYSTEM')
183
184     conf.check_cxx(fragment="""
185                             #include <boost/signals2.hpp>\n
186                             int main() { boost::signals2::signal<void (int)> x; }\n
187                             """,
188                    msg='Checking for boost signals2 library',
189                    uselib_store='BOOST_SIGNALS2')
190
191     conf.check_cxx(fragment="""
192                             #include <boost/date_time.hpp>\n
193                             int main() { boost::gregorian::day_clock::local_day(); }\n
194                             """,
195                    msg='Checking for boost datetime library',
196                    libpath='/usr/local/lib',
197                    lib=['boost_date_time%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
198                    uselib_store='BOOST_DATETIME')
199
200     conf.check_cxx(fragment="""
201                             #include <boost/regex.hpp>\n
202                             int main() { boost::regex ex("a"); }\n
203                             """,
204                    msg='Checking for boost regex library',
205                    libpath='/usr/local/lib',
206                    lib=['boost_regex%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
207                    uselib_store='BOOST_REGEX')
208
209     if not conf.env.DISABLE_TESTS:
210         conf.recurse('test')
211         if not conf.options.disable_gcov:
212             conf.check(lib='gcov', define_name='HAVE_GCOV', mandatory=False)
213             conf.env.append_value('LINKFLAGS', '-fprofile-arcs')
214
215 def build(bld):
216     create_version_cc(bld, VERSION)
217
218     if bld.env.TARGET_WINDOWS:
219         boost_lib_suffix = '-mt'
220     else:
221         boost_lib_suffix = ''
222
223     bld(source='libdcp%s.pc.in' % bld.env.API_VERSION,
224         version=VERSION,
225         includedir='%s/include/libdcp%s' % (bld.env.PREFIX, bld.env.API_VERSION),
226         libs="-L${libdir} -ldcp%s -lcxml -lboost_system%s" % (bld.env.API_VERSION, boost_lib_suffix),
227         install_path='${LIBDIR}/pkgconfig')
228
229     bld.recurse('src')
230     bld.recurse('tools')
231     if not bld.env.DISABLE_TESTS:
232         bld.recurse('test')
233     if not bld.env.DISABLE_EXAMPLES:
234         bld.recurse('examples')
235
236     bld.add_post_fun(post)
237
238 def dist(ctx):
239     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__ GPATH GRTAGS GSYMS GTAGS'
240
241 def create_version_cc(bld, version):
242     if os.path.exists('.git'):
243         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
244         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
245         o = output[0].decode('utf-8')
246         commit = o.replace ("commit ", "")[0:10]
247     else:
248         commit = "release"
249
250     try:
251         text =  '#include "version.h"\n'
252         text += 'char const * dcp::git_commit = \"%s\";\n' % commit
253         text += 'char const * dcp::version = \"%s\";\n' % version
254         if bld.env.ENABLE_DEBUG:
255             debug_string = 'true'
256         else:
257             debug_string = 'false'
258         text += 'bool const dcp::built_with_debug = %s;\n' % debug_string
259         print('Writing version information to src/version.cc')
260         o = open('src/version.cc', 'w')
261         o.write(text)
262         o.close()
263     except IOError:
264         print('Could not open src/version.cc for writing\n')
265         sys.exit(-1)
266
267 def post(ctx):
268     if ctx.cmd == 'install':
269         ctx.exec_command('/sbin/ldconfig')
270
271 def tags(bld):
272     os.system('etags src/*.cc src/*.h')