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