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