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