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