079f38ad9adc5d3fb31547040d89528094529733
[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.LIB_CXML = ['glibmm-2.4', 'glib-2.0', 'pcre', 'sigc-2.0', 'rt', 'xml++-2.6', 'xml2', 'pthread', 'lzma', 'dl', 'z']
28         conf.env.STLIB_CXML = ['cxml']
29         conf.check_cfg(package='libcxml', atleast_version='0.08', args='--cflags', uselib_store='CXML', mandatory=True)
30     else:
31         conf.check_cfg(package='libcxml', atleast_version='0.08', args='--cflags --libs', uselib_store='CXML', mandatory=True)
32
33     conf.check_cxx(fragment="""
34                             #include <boost/version.hpp>\n
35                             #if BOOST_VERSION < 104500\n
36                             #error boost too old\n
37                             #endif\n
38                             int main(void) { return 0; }\n
39                             """,
40                    mandatory=True,
41                    msg='Checking for boost library >= 1.45',
42                    okmsg='yes',
43                    errmsg='too old\nPlease install boost version 1.45 or higher.')
44
45     conf.check_cxx(fragment = """
46                               #include <boost/filesystem.hpp>\n
47                               int main() { boost::filesystem::copy_file ("a", "b"); }\n
48                               """,
49                    msg = 'Checking for boost filesystem library',
50                    libpath = '/usr/local/lib',
51                    lib = ['boost_filesystem', 'boost_system'],
52                    uselib_store = 'BOOST_FILESYSTEM')
53
54     conf.check_cxx(fragment = """
55                               #include <boost/locale.hpp>\n
56                               int main() { boost::locale::conv::to_utf<char> ("a", "cp850"); }\n
57                               """,
58                    msg = 'Checking for boost locale library',
59                    libpath = '/usr/local/lib',
60                    lib = ['boost_locale', 'boost_system'],
61                    uselib_store = 'BOOST_LOCALE')
62
63     conf.recurse('test')
64
65 def build(bld):
66     create_version_cc(bld, VERSION)
67
68     bld(source='libsub.pc.in',
69         version=VERSION,
70         includedir='%s/include' % bld.env.PREFIX,
71         libs="-L${libdir} -lsub -lboost_system",
72         install_path='${LIBDIR}/pkgconfig')
73
74     bld.recurse('src')
75     bld.recurse('test')
76     bld.recurse('tools')
77
78     bld.add_post_fun(post)
79
80 def dist(ctx):
81     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__ GPATH GRTAGS GSYMS GTAGS'
82
83 def create_version_cc(bld, version):
84     if os.path.exists('.git'):
85         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
86         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
87         o = output[0].decode('utf-8')
88         commit = o.replace ("commit ", "")[0:10]
89     else:
90         commit = "release"
91
92     try:
93         text =  '#include "version.h"\n'
94         text += 'char const * sub::git_commit = \"%s\";\n' % commit
95         text += 'char const * sub::version = \"%s\";\n' % version
96         if bld.env.ENABLE_DEBUG:
97             debug_string = 'true'
98         else:
99             debug_string = 'false'
100         text += 'bool const built_with_debug = %s;\n' % debug_string
101         print('Writing version information to src/version.cc')
102         o = open('src/version.cc', 'w')
103         o.write(text)
104         o.close()
105     except IOError:
106         print('Could not open src/version.cc for writing\n')
107         sys.exit(-1)
108
109 def post(ctx):
110     if ctx.cmd == 'install':
111         ctx.exec_command('/sbin/ldconfig')