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