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