Make wscript work on python 2 and 3.
[libdcp.git] / wscript
1 import subprocess
2 import os
3 import lut
4
5 APPNAME = 'libdcp'
6 VERSION = '0.35pre'
7
8 def options(opt):
9     opt.load('compiler_cxx')
10     opt.add_option('--target-windows', action='store_true', default = False, help = 'set up to do a cross-compile to Windows')
11     opt.add_option('--enable-debug', action='store_true', default = False, help = 'build with debugging information and without optimisation')
12     opt.add_option('--static-openjpeg', action='store_true', default = False, help = 'link statically to openjpeg')
13     opt.add_option('--static-libdcp', action='store_true', default = False, help = 'build libdcp and in-tree dependencies statically')
14
15 def configure(conf):
16     conf.load('compiler_cxx')
17     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-Wno-unused-result', '-O2', '-D_FILE_OFFSET_BITS=64'])
18     conf.env.append_value('CXXFLAGS', ['-DLIBDCP_VERSION="%s"' % VERSION])
19
20     conf.env.TARGET_WINDOWS = conf.options.target_windows
21     conf.env.STATIC_OPENJPEG = conf.options.static_openjpeg
22     conf.env.STATIC_LIBDCP = conf.options.static_libdcp
23
24     if conf.options.target_windows:
25         conf.env.append_value('CXXFLAGS', '-DLIBDCP_WINDOWS')
26     else:
27         conf.env.append_value('CXXFLAGS', '-DLIBDCP_POSIX')
28
29     conf.check_cfg(package = 'openssl', args = '--cflags --libs', uselib_store = 'OPENSSL', mandatory = True)
30     conf.check_cfg(package = 'libxml++-2.6', args = '--cflags --libs', uselib_store = 'LIBXML++', mandatory = True)
31
32     openjpeg_fragment = """
33                         #include <stdio.h>\n
34                         #include <openjpeg.h>\n
35                         int main () {\n
36                         void* p = (void *) opj_image_create;\n
37                         return 0;\n
38                         }
39                         """
40     if conf.options.static_openjpeg:
41         conf.check_cc(fragment = openjpeg_fragment, msg = 'Checking for library openjpeg', stlib = 'openjpeg', uselib_store = 'OPENJPEG')
42     else:
43         conf.check_cc(fragment = openjpeg_fragment, msg = 'Checking for library openjpeg', lib = 'openjpeg', uselib_store = 'OPENJPEG')
44
45     if conf.options.target_windows:
46         boost_lib_suffix = '-mt'
47     else:
48         boost_lib_suffix = ''
49
50     if conf.options.enable_debug:
51         conf.env.append_value('CXXFLAGS', '-g')
52     else:
53         # Somewhat experimental use of -O2 rather than -O3 to see if
54         # Windows builds are any more reliable
55         conf.env.append_value('CXXFLAGS', '-O2')
56
57     conf.check_cxx(fragment = """
58                               #include <boost/filesystem.hpp>\n
59                               int main() { boost::filesystem::copy_file ("a", "b"); }\n
60                               """,
61                    msg = 'Checking for boost filesystem library',
62                    libpath = '/usr/local/lib',
63                    lib = ['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
64                    uselib_store = 'BOOST_FILESYSTEM')
65
66     conf.check_cxx(fragment = """
67                               #include <boost/signals2.hpp>\n
68                               int main() { boost::signals2::signal<void (int)> x; }\n
69                               """,
70                    msg = 'Checking for boost signals2 library',
71                    uselib_store = 'BOOST_SIGNALS2')
72
73     lut.make_luts()
74
75     conf.recurse('test')
76     conf.recurse('asdcplib')
77
78 def build(bld):
79     create_version_cc(VERSION)
80
81     if bld.env.TARGET_WINDOWS:
82         boost_lib_suffix = '-mt'
83     else:
84         boost_lib_suffix = ''
85
86     bld(source = 'libdcp.pc.in',
87         version = VERSION,
88         includedir = '%s/include' % bld.env.PREFIX,
89         libs = "-L${libdir} -ldcp -lasdcp-libdcp -lkumu-libdcp -lboost_system%s" % boost_lib_suffix,
90         install_path = '${LIBDIR}/pkgconfig')
91
92     bld.recurse('src')
93     bld.recurse('tools')
94     bld.recurse('test')
95     bld.recurse('asdcplib')
96     bld.recurse('examples')
97
98     bld.add_post_fun(post)
99
100 def dist(ctx):
101     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~'
102
103 def create_version_cc(version):
104     if os.path.exists('.git'):
105         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
106         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
107         o = output[0].decode('utf-8')
108         commit = o.replace ("commit ", "")[0:10]
109     else:
110         commit = "release"
111
112     try:
113         text =  '#include "version.h"\n'
114         text += 'char const * libdcp::git_commit = \"%s\";\n' % commit
115         text += 'char const * libdcp::version = \"%s\";\n' % version
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')