Check for required executables before starting tests.
[libdcp.git] / wscript
1 import subprocess
2 import os
3 import sys
4
5 APPNAME = 'libdcp'
6 VERSION = '1.00.0devel'
7 API_VERSION = '-1.0'
8
9 def options(opt):
10     opt.load('compiler_cxx')
11     opt.add_option('--target-windows', action='store_true', default=False, help='set up to do a cross-compile to Windows')
12     opt.add_option('--enable-debug', action='store_true', default=False, help='build with debugging information and without optimisation')
13     opt.add_option('--static', action='store_true', default=False, help='build libdcp and in-tree dependencies statically, and link statically to openjpeg and cxml')
14     opt.add_option('--disable-tests', action='store_true', default=False, help='disable building of tests')
15     opt.add_option('--disable-examples', action='store_true', default=False, help='disable building of examples')
16
17 def configure(conf):
18     conf.load('compiler_cxx')
19     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-D_FILE_OFFSET_BITS=64', '-D__STDC_FORMAT_MACROS'])
20     conf.env.append_value('CXXFLAGS', ['-DLIBDCP_VERSION="%s"' % VERSION])
21
22     conf.env.TARGET_WINDOWS = conf.options.target_windows
23     conf.env.TARGET_OSX = sys.platform == 'darwin'
24     conf.env.ENABLE_DEBUG = conf.options.enable_debug
25     conf.env.DISABLE_TESTS = conf.options.disable_tests
26     conf.env.DISABLE_EXAMPLES = conf.options.disable_examples
27     conf.env.STATIC = conf.options.static
28     conf.env.API_VERSION = API_VERSION
29
30     if conf.options.target_windows:
31         conf.env.append_value('CXXFLAGS', '-DLIBDCP_WINDOWS')
32     else:
33         conf.env.append_value('CXXFLAGS', '-DLIBDCP_POSIX')
34
35     if not conf.env.TARGET_OSX:
36         conf.env.append_value('CXXFLAGS', ['-Wno-unused-result', '-Wno-unused-parameter'])
37
38     conf.check_cfg(package='openssl', args='--cflags --libs', uselib_store='OPENSSL', mandatory=True)
39     conf.check_cfg(package='libxml++-2.6', args='--cflags --libs', uselib_store='LIBXML++', mandatory=True)
40     conf.check_cfg(package='xmlsec1', args='--cflags --libs', uselib_store='XMLSEC1', mandatory=True)
41     # Remove erroneous escaping of quotes from xmlsec1 defines
42     conf.env.DEFINES_XMLSEC1 = [f.replace('\\', '') for f in conf.env.DEFINES_XMLSEC1]
43     conf.check_cfg(package='', path='Magick++-config', args='--cppflags --cxxflags --libs', uselib_store='MAGICK', mandatory=False)
44     conf.check_cfg(package='sndfile', args='--cflags --libs', uselib_store='SNDFILE', mandatory=False)
45
46     if conf.options.static:
47         conf.check_cc(fragment="""
48                      #include <stdio.h>\n
49                      #include <openjpeg.h>\n
50                      int main () {\n
51                      void* p = (void *) opj_image_create;\n
52                      return 0;\n
53                      }
54                      """,
55                        msg='Checking for library openjpeg', stlib='openjpeg', uselib_store='OPENJPEG', mandatory=True)
56         
57         conf.env.HAVE_CXML = 1
58         conf.env.STLIB_CXML = ['cxml']
59     else:
60         conf.check_cfg(package='libopenjpeg', args='--cflags --libs', uselib_store='OPENJPEG', mandatory=True)
61         conf.check_cfg(package='libcxml', atleast_version='0.08', args='--cflags --libs', uselib_store='CXML', mandatory=True)
62
63     if conf.options.target_windows:
64         boost_lib_suffix = '-mt'
65     else:
66         boost_lib_suffix = ''
67
68     if conf.options.enable_debug:
69         conf.env.append_value('CXXFLAGS', '-g')
70     else:
71         # Somewhat experimental use of -O2 rather than -O3 to see if
72         # Windows builds are any more reliable
73         conf.env.append_value('CXXFLAGS', '-O2')
74
75     conf.check_cxx(fragment="""
76                             #include <boost/version.hpp>\n
77                             #if BOOST_VERSION < 104500\n
78                             #error boost too old\n
79                             #endif\n
80                             int main(void) { return 0; }\n
81                             """,
82                    mandatory=True,
83                    msg='Checking for boost library >= 1.45',
84                    okmsg='yes',
85                    errmsg='too old\nPlease install boost version 1.45 or higher.')
86
87     conf.check_cxx(fragment="""
88                             #include <boost/filesystem.hpp>\n
89                             int main() { boost::filesystem::copy_file ("a", "b"); }\n
90                             """,
91                    msg='Checking for boost filesystem library',
92                    libpath='/usr/local/lib',
93                    lib=['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
94                    uselib_store='BOOST_FILESYSTEM')
95
96     conf.check_cxx(fragment="""
97                             #include <boost/signals2.hpp>\n
98                             int main() { boost::signals2::signal<void (int)> x; }\n
99                             """,
100                    msg='Checking for boost signals2 library',
101                    uselib_store='BOOST_SIGNALS2')
102
103     conf.check_cxx(fragment="""
104                             #include <boost/date_time.hpp>\n
105                             int main() { boost::gregorian::day_clock::local_day(); }\n
106                             """,
107                    msg='Checking for boost datetime library',
108                    libpath='/usr/local/lib',
109                    lib=['boost_date_time%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
110                    uselib_store='BOOST_DATETIME')
111
112     if not conf.env.DISABLE_TESTS:
113         conf.recurse('test')
114         conf.check(lib='gcov', define_name='HAVE_GCOV', mandatory=False)
115     conf.recurse('asdcplib')
116
117 def build(bld):
118     create_version_cc(bld, VERSION)
119
120     if bld.env.TARGET_WINDOWS:
121         boost_lib_suffix = '-mt'
122     else:
123         boost_lib_suffix = ''
124
125     bld(source='libdcp%s.pc.in' % bld.env.API_VERSION,
126         version=VERSION,
127         includedir='%s/include/libdcp%s' % (bld.env.PREFIX, bld.env.API_VERSION),
128         libs="-L${libdir} -ldcp%s -lasdcp-libdcp%s -lkumu-libdcp%s -lcxml -lboost_system%s" % (API_VERSION, bld.env.API_VERSION, bld.env.API_VERSION, boost_lib_suffix),
129         install_path='${LIBDIR}/pkgconfig')
130
131     bld.recurse('src')
132     bld.recurse('tools')
133     if not bld.env.DISABLE_TESTS:
134         bld.recurse('test')
135     bld.recurse('asdcplib')
136     if not bld.env.DISABLE_EXAMPLES:
137         bld.recurse('examples')
138
139     bld.add_post_fun(post)
140
141 def dist(ctx):
142     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__ GPATH GRTAGS GSYMS GTAGS'
143
144 def create_version_cc(bld, version):
145     if os.path.exists('.git'):
146         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
147         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
148         o = output[0].decode('utf-8')
149         commit = o.replace ("commit ", "")[0:10]
150     else:
151         commit = "release"
152
153     try:
154         text =  '#include "version.h"\n'
155         text += 'char const * dcp::git_commit = \"%s\";\n' % commit
156         text += 'char const * dcp::version = \"%s\";\n' % version
157         if bld.env.ENABLE_DEBUG:
158             debug_string = 'true'
159         else:
160             debug_string = 'false'
161         text += 'bool const built_with_debug = %s;\n' % debug_string
162         print('Writing version information to src/version.cc')
163         o = open('src/version.cc', 'w')
164         o.write(text)
165         o.close()
166     except IOError:
167         print('Could not open src/version.cc for writing\n')
168         sys.exit(-1)
169
170 def post(ctx):
171     if ctx.cmd == 'install':
172         ctx.exec_command('/sbin/ldconfig')