Bump libdcp for ATMOS fixes.
[libsub.git] / wscript
1 #
2 #    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3 #
4 #    This file is part of libsub.
5 #
6 #    libsub 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 #    libsub 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 libsub.  If not, see <http://www.gnu.org/licenses/>.
18
19 import subprocess
20 import os
21 import shlex
22 from waflib import Context
23
24 APPNAME = 'libsub'
25
26 this_version = subprocess.Popen(shlex.split('git tag -l --points-at HEAD'), stdout=subprocess.PIPE).communicate()[0]
27 last_version = subprocess.Popen(shlex.split('git describe --tags --abbrev=0'), stdout=subprocess.PIPE).communicate()[0]
28
29 if this_version == '':
30     VERSION = '%sdevel' % last_version[1:].strip()
31 else:
32     VERSION = this_version[1:].strip()
33
34 API_VERSION = '-1.0'
35
36 try:
37     from subprocess import STDOUT, check_output, CalledProcessError
38 except ImportError:
39     # python 2.6 (in Centos 6) doesn't include check_output
40     # monkey patch it in!
41     import subprocess
42     STDOUT = subprocess.STDOUT
43
44     def check_output(*popenargs, **kwargs):
45         if 'stdout' in kwargs:  # pragma: no cover
46             raise ValueError('stdout argument not allowed, '
47                              'it will be overridden.')
48         process = subprocess.Popen(stdout=subprocess.PIPE,
49                                    *popenargs, **kwargs)
50         output, _ = process.communicate()
51         retcode = process.poll()
52         if retcode:
53             cmd = kwargs.get("args")
54             if cmd is None:
55                 cmd = popenargs[0]
56             raise subprocess.CalledProcessError(retcode, cmd,
57                                                 output=output)
58         return output
59     subprocess.check_output = check_output
60
61     # overwrite CalledProcessError due to `output`
62     # keyword not being available (in 2.6)
63     class CalledProcessError(Exception):
64
65         def __init__(self, returncode, cmd, output=None):
66             self.returncode = returncode
67             self.cmd = cmd
68             self.output = output
69
70         def __str__(self):
71             return "Command '%s' returned non-zero exit status %d" % (
72                 self.cmd, self.returncode)
73     subprocess.CalledProcessError = CalledProcessError
74
75 def options(opt):
76     opt.load('compiler_cxx')
77     opt.add_option('--enable-debug', action='store_true', default=False, help='build with debugging information and without optimisation')
78     opt.add_option('--static', action='store_true', default=False, help='build libsub statically and link statically to cxml and dcp')
79     opt.add_option('--target-windows', action='store_true', default=False, help='set up to do a cross-compile to make a Windows package')
80     opt.add_option('--disable-tests', action='store_true', default=False, help='disable building of tests')
81     opt.add_option('--force-cpp11', action='store_true', default=False, help='force use of C++11')
82
83 def configure(conf):
84     conf.load('compiler_cxx')
85     conf.load('clang_compilation_database', tooldir=['waf-tools'])
86     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-D_FILE_OFFSET_BITS=64', '-D__STDC_FORMAT_MACROS'])
87     if conf.options.force_cpp11:
88         conf.env.append_value('CXXFLAGS', ['-std=c++11', '-DBOOST_NO_CXX11_SCOPED_ENUMS'])
89     conf.env.append_value('CXXFLAGS', ['-DLIBSUB_VERSION="%s"' % VERSION])
90
91     conf.env.ENABLE_DEBUG = conf.options.enable_debug
92     conf.env.STATIC = conf.options.static
93     conf.env.TARGET_WINDOWS = conf.options.target_windows
94     conf.env.DISABLE_TESTS = conf.options.disable_tests
95     conf.env.API_VERSION = API_VERSION
96
97     if conf.options.target_windows:
98         conf.env.append_value('CXXFLAGS', '-DLIBSUB_WINDOWS')
99     else:
100         conf.env.append_value('CXXFLAGS', '-DLIBSUB_POSIX')
101
102     if conf.options.enable_debug:
103         conf.env.append_value('CXXFLAGS', '-g')
104     else:
105         conf.env.append_value('CXXFLAGS', '-O3')
106
107     # Disable libxml++ deprecation warnings for now
108     conf.env.append_value('CXXFLAGS', ['-Wno-deprecated-declarations'])
109
110     conf.check_cfg(package='openssl', args='--cflags --libs', uselib_store='OPENSSL', mandatory=True)
111
112     if conf.options.static:
113         conf.check_cfg(package='libcxml', atleast_version='0.16.0', args='--cflags', uselib_store='CXML', mandatory=True)
114         conf.env.HAVE_CXML = 1
115         conf.env.LIB_CXML = ['glibmm-2.4', 'glib-2.0', 'pcre', 'sigc-2.0', 'rt', 'xml++-2.6', 'xml2', 'pthread', 'lzma', 'dl', 'z']
116         conf.env.STLIB_CXML = ['cxml']
117         conf.check_cfg(package='libdcp-1.0', atleast_version='1.6.2', args='--cflags', uselib_store='DCP', mandatory=True)
118         conf.env.HAVE_DCP = 1
119         conf.env.STLIB_DCP = ['dcp-1.0', 'asdcp-carl', 'kumu-carl', 'openjp2']
120         conf.env.LIB_DCP = ['ssl', 'crypto', 'xmlsec1-openssl', 'xmlsec1']
121     else:
122         conf.check_cfg(package='libcxml', atleast_version='0.16.0', args='--cflags --libs', uselib_store='CXML', mandatory=True)
123         conf.check_cfg(package='libdcp-1.0', atleast_version='1.6.2', args='--cflags --libs', uselib_store='DCP', mandatory=True)
124
125     conf.env.DEFINES_DCP = [f.replace('\\', '') for f in conf.env.DEFINES_DCP]
126
127     boost_lib_suffix = ''
128     if conf.env.TARGET_WINDOWS:
129         boost_lib_suffix = '-mt'
130
131     conf.check_cxx(fragment="""
132                             #include <boost/version.hpp>\n
133                             #if BOOST_VERSION < 104500\n
134                             #error boost too old\n
135                             #endif\n
136                             int main(void) { return 0; }\n
137                             """,
138                    mandatory=True,
139                    msg='Checking for boost library >= 1.45',
140                    okmsg='yes',
141                    errmsg='too old\nPlease install boost version 1.45 or higher.')
142
143     conf.check_cxx(fragment="""
144                             #include <boost/filesystem.hpp>\n
145                             int main() { boost::filesystem::copy_file ("a", "b"); }\n
146                             """,
147                    msg='Checking for boost filesystem library',
148                    libpath='/usr/local/lib',
149                    lib=['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
150                    uselib_store='BOOST_FILESYSTEM')
151
152     # Find the icu- libraries on the system as we need to link to them when we look for boost locale.
153     locale_libs = ['boost_locale%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix]
154     for pkg in subprocess.check_output(['pkg-config', '--list-all']).splitlines():
155         pkg = pkg.decode('utf-8')
156         if pkg.startswith("icu"):
157             for lib in subprocess.check_output(['pkg-config', '--libs-only-l', pkg.split()[0]]).split():
158                 name = lib[2:]
159                 if not name in locale_libs:
160                     locale_libs.append(name.decode('utf-8'))
161
162     conf.check_cxx(fragment="""
163                             #include <boost/locale.hpp>\n
164                             int main() { boost::locale::conv::to_utf<char> ("a", "cp850"); }\n
165                             """,
166                    msg='Checking for boost locale library',
167                    libpath='/usr/local/lib',
168                    lib=locale_libs,
169                    uselib_store='BOOST_LOCALE')
170
171     conf.check_cxx(fragment="""
172                             #include <boost/regex.hpp>\n
173                             int main() { boost::regex re ("foo"); }\n
174                             """,
175                    msg='Checking for boost regex library',
176                    libpath='/usr/local/lib',
177                    lib=['boost_regex%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
178                    uselib_store='BOOST_REGEX')
179
180     if not conf.env.DISABLE_TESTS:
181         conf.recurse('test')
182
183 def build(bld):
184     create_version_cc(bld, VERSION)
185
186     if bld.env.TARGET_WINDOWS:
187         boost_lib_suffix = '-mt'
188     else:
189         boost_lib_suffix = ''
190
191     bld(source='libsub%s.pc.in' % bld.env.API_VERSION,
192         version=VERSION,
193         includedir='%s/include/libsub%s' % (bld.env.PREFIX, bld.env.API_VERSION),
194         libs="-L${libdir} -lsub%s -lboost_system%s" % (bld.env.API_VERSION, boost_lib_suffix),
195         install_path='${LIBDIR}/pkgconfig')
196
197     bld.recurse('src')
198     if not bld.env.DISABLE_TESTS:
199         bld.recurse('test')
200     bld.recurse('tools')
201
202     bld.add_post_fun(post)
203
204 def dist(ctx):
205     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__ GPATH GRTAGS GSYMS GTAGS'
206
207 def create_version_cc(bld, version):
208     if os.path.exists('.git'):
209         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
210         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
211         o = output[0].decode('utf-8')
212         commit = o.replace ("commit ", "")[0:10]
213     else:
214         commit = "release"
215
216     try:
217         text =  '#include "version.h"\n'
218         text += 'char const * sub::git_commit = \"%s\";\n' % commit
219         text += 'char const * sub::version = \"%s\";\n' % version
220         if bld.env.ENABLE_DEBUG:
221             debug_string = 'true'
222         else:
223             debug_string = 'false'
224         text += 'bool const built_with_debug = %s;\n' % debug_string
225         print('Writing version information to src/version.cc')
226         o = open('src/version.cc', 'w')
227         o.write(text)
228         o.close()
229     except IOError:
230         print('Could not open src/version.cc for writing\n')
231         sys.exit(-1)
232
233 def post(ctx):
234     if ctx.cmd == 'install':
235         ctx.exec_command('/sbin/ldconfig')
236
237 def tags(bld):
238     os.system('etags src/*.cc src/*.h')