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