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