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