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