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