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