More STL binary reading stuff.
[libsub.git] / wscript
1 import subprocess
2 import os
3
4 APPNAME = 'libsub'
5 VERSION = '0.01.0devel'
6
7 def options(opt):
8     opt.load('compiler_cxx')
9     opt.add_option('--enable-debug', action='store_true', default=False, help='build with debugging information and without optimisation')
10     opt.add_option('--static', action='store_true', default=False, help='build libsub statically and link statically to cxml')
11
12 def configure(conf):
13     conf.load('compiler_cxx')
14     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-D_FILE_OFFSET_BITS=64', '-D__STDC_FORMAT_MACROS'])
15     conf.env.append_value('CXXFLAGS', ['-DLIBSUB_VERSION="%s"' % VERSION])
16
17     conf.env.ENABLE_DEBUG = conf.options.enable_debug
18     conf.env.STATIC = conf.options.static
19
20     if conf.options.enable_debug:
21         conf.env.append_value('CXXFLAGS', '-g')
22     else:
23         conf.env.append_value('CXXFLAGS', '-O3')
24
25     if conf.options.static:
26         conf.env.HAVE_CXML = 1
27         conf.env.STLIB_CXML = ['cxml']
28     else:
29         conf.check_cfg(package='libcxml', atleast_version='0.08', args='--cflags --libs', uselib_store='CXML', mandatory=True)
30
31     conf.check_cxx(fragment="""
32                             #include <boost/version.hpp>\n
33                             #if BOOST_VERSION < 104500\n
34                             #error boost too old\n
35                             #endif\n
36                             int main(void) { return 0; }\n
37                             """,
38                    mandatory=True,
39                    msg='Checking for boost library >= 1.45',
40                    okmsg='yes',
41                    errmsg='too old\nPlease install boost version 1.45 or higher.')
42
43     conf.check_cxx(fragment = """
44                               #include <boost/filesystem.hpp>\n
45                               int main() { boost::filesystem::copy_file ("a", "b"); }\n
46                               """,
47                    msg = 'Checking for boost filesystem library',
48                    libpath = '/usr/local/lib',
49                    lib = ['boost_filesystem', 'boost_system'],
50                    uselib_store = 'BOOST_FILESYSTEM')
51
52     conf.check_cxx(fragment = """
53                               #include <boost/locale.hpp>\n
54                               int main() { boost::locale::conv::to_utf<char> ("a", "cp850"); }\n
55                               """,
56                    msg = 'Checking for boost locale library',
57                    libpath = '/usr/local/lib',
58                    lib = ['boost_locale', 'boost_system'],
59                    uselib_store = 'BOOST_LOCALE')
60
61     conf.recurse('test')
62
63 def build(bld):
64     create_version_cc(bld, VERSION)
65
66     bld(source='libsub.pc.in',
67         version=VERSION,
68         includedir='%s/include' % bld.env.PREFIX,
69         libs="-L${libdir} -lsub -lboost_system",
70         install_path='${LIBDIR}/pkgconfig')
71
72     bld.recurse('src')
73     bld.recurse('test')
74     bld.recurse('tools')
75
76     bld.add_post_fun(post)
77
78 def dist(ctx):
79     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__ GPATH GRTAGS GSYMS GTAGS'
80
81 def create_version_cc(bld, version):
82     if os.path.exists('.git'):
83         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
84         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
85         o = output[0].decode('utf-8')
86         commit = o.replace ("commit ", "")[0:10]
87     else:
88         commit = "release"
89
90     try:
91         text =  '#include "version.h"\n'
92         text += 'char const * sub::git_commit = \"%s\";\n' % commit
93         text += 'char const * sub::version = \"%s\";\n' % version
94         if bld.env.ENABLE_DEBUG:
95             debug_string = 'true'
96         else:
97             debug_string = 'false'
98         text += 'bool const built_with_debug = %s;\n' % debug_string
99         print('Writing version information to src/version.cc')
100         o = open('src/version.cc', 'w')
101         o.write(text)
102         o.close()
103     except IOError:
104         print('Could not open src/version.cc for writing\n')
105         sys.exit(-1)
106
107 def post(ctx):
108     if ctx.cmd == 'install':
109         ctx.exec_command('/sbin/ldconfig')