Attempted centos build fix.
[libsub.git] / wscript
1 import subprocess
2 import os
3 from waflib import Context
4
5 APPNAME = 'libsub'
6 VERSION = '1.2.4devel'
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
55 def configure(conf):
56     conf.load('compiler_cxx')
57     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-D_FILE_OFFSET_BITS=64', '-D__STDC_FORMAT_MACROS'])
58     conf.env.append_value('CXXFLAGS', ['-DLIBSUB_VERSION="%s"' % VERSION])
59
60     conf.env.ENABLE_DEBUG = conf.options.enable_debug
61     conf.env.STATIC = conf.options.static
62     conf.env.TARGET_WINDOWS = conf.options.target_windows
63     conf.env.DISABLE_TESTS = conf.options.disable_tests
64     conf.env.API_VERSION = API_VERSION
65
66     if conf.options.enable_debug:
67         conf.env.append_value('CXXFLAGS', '-g')
68     else:
69         conf.env.append_value('CXXFLAGS', '-O3')
70
71     # Disable libxml++ deprecation warnings for now
72     conf.env.append_value('CXXFLAGS', ['-Wno-deprecated-declarations'])
73
74     conf.check_cfg(package='openssl', args='--cflags --libs', uselib_store='OPENSSL', mandatory=True)
75
76     if conf.options.static:
77         conf.check_cfg(package='libcxml', atleast_version='0.14.0', args='--cflags', uselib_store='CXML', mandatory=True)
78         conf.env.HAVE_CXML = 1
79         conf.env.LIB_CXML = ['glibmm-2.4', 'glib-2.0', 'pcre', 'sigc-2.0', 'rt', 'xml++-2.6', 'xml2', 'pthread', 'lzma', 'dl', 'z']
80         conf.env.STLIB_CXML = ['cxml']
81         conf.check_cfg(package='libdcp-1.0', atleast_version='1.4.4', args='--cflags', uselib_store='DCP', mandatory=True)
82         conf.env.HAVE_DCP = 1
83         conf.env.STLIB_DCP = ['dcp-1.0', 'asdcp-cth', 'kumu-cth', 'openjp2']
84         conf.env.LIB_DCP = ['ssl', 'crypto', 'xmlsec1-openssl', 'xmlsec1']
85     else:
86         conf.check_cfg(package='libcxml', atleast_version='0.15.2', args='--cflags --libs', uselib_store='CXML', mandatory=True)
87         conf.check_cfg(package='libdcp-1.0', atleast_version='1.4.4', args='--cflags --libs', uselib_store='DCP', mandatory=True)
88
89     conf.env.DEFINES_DCP = [f.replace('\\', '') for f in conf.env.DEFINES_DCP]
90
91     boost_lib_suffix = ''
92     if conf.env.TARGET_WINDOWS:
93         boost_lib_suffix = '-mt'
94
95     conf.check_cxx(fragment="""
96                             #include <boost/version.hpp>\n
97                             #if BOOST_VERSION < 104500\n
98                             #error boost too old\n
99                             #endif\n
100                             int main(void) { return 0; }\n
101                             """,
102                    mandatory=True,
103                    msg='Checking for boost library >= 1.45',
104                    okmsg='yes',
105                    errmsg='too old\nPlease install boost version 1.45 or higher.')
106
107     conf.check_cxx(fragment="""
108                             #include <boost/filesystem.hpp>\n
109                             int main() { boost::filesystem::copy_file ("a", "b"); }\n
110                             """,
111                    msg='Checking for boost filesystem library',
112                    libpath='/usr/local/lib',
113                    lib=['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
114                    uselib_store='BOOST_FILESYSTEM')
115
116     # Find the icu- libraries on the system as we need to link to them when we look for boost locale.
117     locale_libs = ['boost_locale%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix]
118     for pkg in subprocess.check_output(['pkg-config', '--list-all']).splitlines():
119         if pkg.startswith("icu"):
120             for lib in subprocess.check_output(['pkg-config', '--libs-only-l', pkg.split()[0]]).split():
121                 name = lib[2:]
122                 if not name in locale_libs:
123                     locale_libs.append(name)
124
125     conf.check_cxx(fragment="""
126                             #include <boost/locale.hpp>\n
127                             int main() { boost::locale::conv::to_utf<char> ("a", "cp850"); }\n
128                             """,
129                    msg='Checking for boost locale library',
130                    libpath='/usr/local/lib',
131                    lib=locale_libs,
132                    uselib_store='BOOST_LOCALE')
133
134     conf.check_cxx(fragment="""
135                             #include <boost/regex.hpp>\n
136                             int main() { boost::regex re ("foo"); }\n
137                             """,
138                    msg='Checking for boost regex library',
139                    libpath='/usr/local/lib',
140                    lib=['boost_regex%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
141                    uselib_store='BOOST_REGEX')
142
143     if not conf.env.DISABLE_TESTS:
144         conf.recurse('test')
145
146     # libxml++ 2.39.1 and later must be built with -std=c++11
147     libxmlpp_version = conf.cmd_and_log(['pkg-config', '--modversion', 'libxml++-2.6'], output=Context.STDOUT, quiet=Context.BOTH)
148     s = libxmlpp_version.split('.')
149     v = (int(s[0]) << 16) | (int(s[1]) << 8) | int(s[2])
150     if v >= 0x022701:
151         conf.env.append_value('CXXFLAGS', '-std=c++11')
152
153 def build(bld):
154     create_version_cc(bld, VERSION)
155
156     if bld.env.TARGET_WINDOWS:
157         boost_lib_suffix = '-mt'
158     else:
159         boost_lib_suffix = ''
160
161     bld(source='libsub%s.pc.in' % bld.env.API_VERSION,
162         version=VERSION,
163         includedir='%s/include/libsub%s' % (bld.env.PREFIX, bld.env.API_VERSION),
164         libs="-L${libdir} -lsub%s -lboost_system%s" % (bld.env.API_VERSION, boost_lib_suffix),
165         install_path='${LIBDIR}/pkgconfig')
166
167     bld.recurse('src')
168     if not bld.env.DISABLE_TESTS:
169         bld.recurse('test')
170     bld.recurse('tools')
171
172     bld.add_post_fun(post)
173
174 def dist(ctx):
175     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__ GPATH GRTAGS GSYMS GTAGS'
176
177 def create_version_cc(bld, version):
178     if os.path.exists('.git'):
179         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
180         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
181         o = output[0].decode('utf-8')
182         commit = o.replace ("commit ", "")[0:10]
183     else:
184         commit = "release"
185
186     try:
187         text =  '#include "version.h"\n'
188         text += 'char const * sub::git_commit = \"%s\";\n' % commit
189         text += 'char const * sub::version = \"%s\";\n' % version
190         if bld.env.ENABLE_DEBUG:
191             debug_string = 'true'
192         else:
193             debug_string = 'false'
194         text += 'bool const built_with_debug = %s;\n' % debug_string
195         print('Writing version information to src/version.cc')
196         o = open('src/version.cc', 'w')
197         o.write(text)
198         o.close()
199     except IOError:
200         print('Could not open src/version.cc for writing\n')
201         sys.exit(-1)
202
203 def post(ctx):
204     if ctx.cmd == 'install':
205         ctx.exec_command('/sbin/ldconfig')
206
207 def tags(bld):
208     os.system('etags src/*.cc src/*.h')