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