ddc333fab45a8043a572c921891242b6a9790195
[libdcp.git] / wscript
1 import subprocess
2 import os
3 import lut
4
5 APPNAME = 'libdcp'
6 VERSION = '0.41pre'
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     conf.env.ENABLE_DEBUG = conf.options.enable_debug
24
25     if conf.options.target_windows:
26         conf.env.append_value('CXXFLAGS', '-DLIBDCP_WINDOWS')
27     else:
28         conf.env.append_value('CXXFLAGS', '-DLIBDCP_POSIX')
29
30     conf.check_cfg(package = 'openssl', args = '--cflags --libs', uselib_store = 'OPENSSL', mandatory = True)
31     conf.check_cfg(package = 'libxml++-2.6', args = '--cflags --libs', uselib_store = 'LIBXML++', mandatory = True)
32     if conf.options.static_openjpeg:
33
34         conf.check_cc(fragment = """
35                        #include <stdio.h>\n
36                        #include <openjpeg.h>\n
37                        int main () {\n
38                        void* p = (void *) opj_image_create;\n
39                        return 0;\n
40                        }
41                        """,
42                        msg = 'Checking for library openjpeg', stlib = 'openjpeg', uselib_store = 'OPENJPEG', mandatory = True)
43     else:
44         conf.check_cfg(package = 'libopenjpeg', args = '--cflags --libs', uselib_store = 'OPENJPEG', mandatory = True)
45
46     if conf.options.target_windows:
47         boost_lib_suffix = '-mt'
48     else:
49         boost_lib_suffix = ''
50
51     if conf.options.enable_debug:
52         conf.env.append_value('CXXFLAGS', '-g')
53     else:
54         # Somewhat experimental use of -O2 rather than -O3 to see if
55         # Windows builds are any more reliable
56         conf.env.append_value('CXXFLAGS', '-O2')
57
58     conf.check_cxx(fragment = """
59                               #include <boost/version.hpp>\n
60                               #if BOOST_VERSION < 104500\n
61                               #error boost too old\n
62                               #endif\n
63                               int main(void) { return 0; }\n
64                               """,
65                    mandatory = True,
66                    msg = 'Checking for boost library >= 1.45',
67                    okmsg = 'yes',
68                    errmsg = 'too old\nPlease install boost version 1.45 or higher.')
69
70     conf.check_cxx(fragment = """
71                               #include <boost/filesystem.hpp>\n
72                               int main() { boost::filesystem::copy_file ("a", "b"); }\n
73                               """,
74                    msg = 'Checking for boost filesystem library',
75                    libpath = '/usr/local/lib',
76                    lib = ['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
77                    uselib_store = 'BOOST_FILESYSTEM')
78
79     conf.check_cxx(fragment = """
80                               #include <boost/signals2.hpp>\n
81                               int main() { boost::signals2::signal<void (int)> x; }\n
82                               """,
83                    msg = 'Checking for boost signals2 library',
84                    uselib_store = 'BOOST_SIGNALS2')
85
86     lut.make_luts()
87
88     conf.recurse('test')
89     conf.recurse('asdcplib')
90
91 def build(bld):
92     create_version_cc(bld, VERSION)
93
94     if bld.env.TARGET_WINDOWS:
95         boost_lib_suffix = '-mt'
96     else:
97         boost_lib_suffix = ''
98
99     bld(source = 'libdcp.pc.in',
100         version = VERSION,
101         includedir = '%s/include' % bld.env.PREFIX,
102         libs = "-L${libdir} -ldcp -lasdcp-libdcp -lkumu-libdcp -lboost_system%s" % boost_lib_suffix,
103         install_path = '${LIBDIR}/pkgconfig')
104
105     bld.recurse('src')
106     bld.recurse('tools')
107     bld.recurse('test')
108     bld.recurse('asdcplib')
109     bld.recurse('examples')
110
111     bld.add_post_fun(post)
112
113 def dist(ctx):
114     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__'
115
116 def create_version_cc(bld, version):
117     if os.path.exists('.git'):
118         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
119         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
120         o = output[0].decode('utf-8')
121         commit = o.replace ("commit ", "")[0:10]
122     else:
123         commit = "release"
124
125     try:
126         text =  '#include "version.h"\n'
127         text += 'char const * libdcp::git_commit = \"%s\";\n' % commit
128         text += 'char const * libdcp::version = \"%s\";\n' % version
129         if bld.env.ENABLE_DEBUG:
130             debug_string = 'true'
131         else:
132             debug_string = 'false'
133         text += 'bool const built_with_debug = %s;\n' % debug_string
134         print('Writing version information to src/version.cc')
135         o = open('src/version.cc', 'w')
136         o.write(text)
137         o.close()
138     except IOError:
139         print('Could not open src/version.cc for writing\n')
140         sys.exit(-1)
141
142 def post(ctx):
143     if ctx.cmd == 'install':
144         ctx.exec_command('/sbin/ldconfig')