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