Try to tidy up C++11 hacks.
[libdcp.git] / wscript
1 #
2 #    Copyright (C) 2012-2017 Carl Hetherington <cth@carlh.net>
3 #
4 #    This file is part of libdcp.
5 #
6 #    libdcp is free software; you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation; either version 2 of the License, or
9 #    (at your option) any later version.
10 #
11 #    libdcp is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #    In addition, as a special exception, the copyright holders give
20 #    permission to link the code of portions of this program with the
21 #    OpenSSL library under certain conditions as described in each
22 #    individual source file, and distribute linked combinations
23 #    including the two.
24 #
25 #    You must obey the GNU General Public License in all respects
26 #    for all of the code used other than OpenSSL.  If you modify
27 #    file(s) with this exception, you may extend this exception to your
28 #    version of the file(s), but you are not obligated to do so.  If you
29 #    do not wish to do so, delete this exception statement from your
30 #    version.  If you delete this exception statement from all source
31 #    files in the program, then also delete it here.
32 #
33
34 import subprocess
35 import os
36 import sys
37 import distutils.spawn
38 from waflib import Logs, Context
39
40 APPNAME = 'libdcp'
41 VERSION = '1.4.4devel'
42 API_VERSION = '-1.0'
43
44 def options(opt):
45     opt.load('compiler_cxx')
46     opt.add_option('--target-windows', action='store_true', default=False, help='set up to do a cross-compile to Windows')
47     opt.add_option('--enable-debug', action='store_true', default=False, help='build with debugging information and without optimisation')
48     opt.add_option('--static', action='store_true', default=False, help='build libdcp statically, and link statically to openjpeg, cxml, asdcplib-cth')
49     opt.add_option('--disable-tests', action='store_true', default=False, help='disable building of tests')
50     opt.add_option('--disable-gcov', action='store_true', default=False, help='don''t use gcov in tests')
51     opt.add_option('--disable-examples', action='store_true', default=False, help='disable building of examples')
52     opt.add_option('--enable-openmp', action='store_true', default=False, help='enable use of OpenMP')
53     opt.add_option('--jpeg', default='oj2', help='specify JPEG library to build with: oj1 or oj2 for OpenJPEG 1.5.x or OpenJPEG 2.1.x respectively')
54
55 def configure(conf):
56     conf.load('compiler_cxx')
57     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-D_FILE_OFFSET_BITS=64', '-D__STDC_FORMAT_MACROS'])
58     gcc = conf.env['CC_VERSION']
59     if int(gcc[0]) >= 4 and int(gcc[1]) > 1:
60         conf.env.append_value('CXXFLAGS', ['-Wno-maybe-uninitialized'])
61     conf.env.append_value('CXXFLAGS', ['-DLIBDCP_VERSION="%s"' % VERSION])
62
63     conf.env.TARGET_WINDOWS = conf.options.target_windows
64     conf.env.TARGET_OSX = sys.platform == 'darwin'
65     conf.env.ENABLE_DEBUG = conf.options.enable_debug
66     conf.env.DISABLE_TESTS = conf.options.disable_tests
67     conf.env.DISABLE_EXAMPLES = conf.options.disable_examples
68     conf.env.STATIC = conf.options.static
69     conf.env.API_VERSION = API_VERSION
70
71     if conf.options.target_windows:
72         conf.env.append_value('CXXFLAGS', '-DLIBDCP_WINDOWS')
73     else:
74         conf.env.append_value('CXXFLAGS', '-DLIBDCP_POSIX')
75
76     if conf.env.TARGET_OSX:
77         conf.env.append_value('CXXFLAGS', ['-Wno-unused-result', '-Wno-unused-parameter', '-Wno-unused-local-typedef'])
78
79     # Disable libxml++ deprecation warnings for now
80     conf.env.append_value('CXXFLAGS', ['-Wno-deprecated-declarations'])
81
82     if conf.options.enable_openmp:
83         conf.env.append_value('CXXFLAGS', ['-fopenmp', '-DLIBDCP_OPENMP'])
84         conf.env.LIB_OPENMP = ['gomp']
85
86     if not conf.env.TARGET_WINDOWS:
87         conf.env.append_value('LINKFLAGS', '-pthread')
88
89     if conf.options.jpeg == 'oj1':
90         conf.env.append_value('CXXFLAGS', ['-DLIBDCP_OPENJPEG1'])
91     elif conf.options.jpeg == 'oj2':
92         conf.env.append_value('CXXFLAGS', ['-DLIBDCP_OPENJPEG2'])
93     else:
94         Logs.error('Invalid --jpeg value %s' % conf.options.jpeg)
95
96     conf.check_cfg(package='openssl', args='--cflags --libs', uselib_store='OPENSSL', mandatory=True)
97     conf.check_cfg(package='libxml++-2.6', args='--cflags --libs', uselib_store='LIBXML++', mandatory=True)
98     conf.check_cfg(package='xmlsec1', args='--cflags --libs', uselib_store='XMLSEC1', mandatory=True)
99     # Remove erroneous escaping of quotes from xmlsec1 defines
100     conf.env.DEFINES_XMLSEC1 = [f.replace('\\', '') for f in conf.env.DEFINES_XMLSEC1]
101
102     # ImageMagick / GraphicsMagick
103     if distutils.spawn.find_executable('Magick++-config'):
104         conf.check_cfg(package='', path='Magick++-config', args='--cppflags --cxxflags --libs', uselib_store='MAGICK', mandatory=True)
105     else:
106         image = conf.check_cfg(package='ImageMagick++', args='--cflags --libs', uselib_store='MAGICK', mandatory=False)
107         graphics = conf.check_cfg(package='GraphicsMagick++', args='--cflags --libs', uselib_store='MAGICK', mandatory=False)
108         if image is None and graphics is None:
109             Logs.error('Neither ImageMagick++ nor GraphicsMagick++ found: one or the other is required')
110
111     conf.check_cfg(package='sndfile', args='--cflags --libs', uselib_store='SNDFILE', mandatory=False)
112
113     if conf.options.static:
114         if conf.options.jpeg == 'oj2':
115             conf.check_cfg(package='libopenjp2', args='--cflags', atleast_version='2.1.0', uselib_store='OPENJPEG', mandatory=True)
116             conf.env.STLIB_OPENJPEG = ['openjp2']
117         elif conf.options.jpeg == 'oj1':
118             conf.check_cfg(package='libopenjpeg1', args='--cflags', atleast_version='1.5.0', uselib_store='OPENJPEG', mandatory=True)
119             conf.env.STLIB_OPENJPEG = ['openjpeg']
120         conf.check_cfg(package='libasdcp-cth', atleast_version='0.1.3', args='--cflags', uselib_store='ASDCPLIB_CTH', mandatory=True)
121         conf.env.HAVE_ASDCPLIB_CTH = 1
122         conf.env.STLIB_ASDCPLIB_CTH = ['asdcp-cth', 'kumu-cth']
123         conf.env.HAVE_CXML = 1
124         conf.env.LIB_CXML = ['xml++-2.6', 'glibmm-2.4']
125         conf.env.STLIB_CXML = ['cxml']
126     else:
127         if conf.options.jpeg == 'oj2':
128             conf.check_cfg(package='libopenjp2', args='--cflags --libs', atleast_version='2.1.0', uselib_store='OPENJPEG', mandatory=True)
129         elif conf.options.jpeg == 'oj1':
130             conf.check_cfg(package='libopenjpeg1', args='--cflags --libs', atleast_version='1.5.0', uselib_store='OPENJPEG', mandatory=True)
131         conf.check_cfg(package='libasdcp-cth', atleast_version='0.1.3', args='--cflags --libs', uselib_store='ASDCPLIB_CTH', mandatory=True)
132         conf.check_cfg(package='libcxml', atleast_version='0.15.2', args='--cflags --libs', uselib_store='CXML', mandatory=True)
133
134     if conf.options.target_windows:
135         boost_lib_suffix = '-mt'
136     else:
137         boost_lib_suffix = ''
138
139     if conf.options.enable_debug:
140         conf.env.append_value('CXXFLAGS', '-g')
141     else:
142         # Somewhat experimental use of -O2 rather than -O3 to see if
143         # Windows builds are any more reliable
144         conf.env.append_value('CXXFLAGS', '-O2')
145
146     conf.check_cxx(fragment="""
147                             #include <boost/version.hpp>\n
148                             #if BOOST_VERSION < 104500\n
149                             #error boost too old\n
150                             #endif\n
151                             int main(void) { return 0; }\n
152                             """,
153                    mandatory=True,
154                    msg='Checking for boost library >= 1.45',
155                    okmsg='yes',
156                    errmsg='too old\nPlease install boost version 1.45 or higher.')
157
158     conf.check_cxx(fragment="""
159                             #include <boost/filesystem.hpp>\n
160                             int main() { boost::filesystem::copy_file ("a", "b"); }\n
161                             """,
162                    msg='Checking for boost filesystem library',
163                    libpath='/usr/local/lib',
164                    lib=['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
165                    uselib_store='BOOST_FILESYSTEM')
166
167     conf.check_cxx(fragment="""
168                             #include <boost/signals2.hpp>\n
169                             int main() { boost::signals2::signal<void (int)> x; }\n
170                             """,
171                    msg='Checking for boost signals2 library',
172                    uselib_store='BOOST_SIGNALS2')
173
174     conf.check_cxx(fragment="""
175                             #include <boost/date_time.hpp>\n
176                             int main() { boost::gregorian::day_clock::local_day(); }\n
177                             """,
178                    msg='Checking for boost datetime library',
179                    libpath='/usr/local/lib',
180                    lib=['boost_date_time%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
181                    uselib_store='BOOST_DATETIME')
182
183     if not conf.env.DISABLE_TESTS:
184         conf.recurse('test')
185         if not conf.options.disable_gcov:
186             conf.check(lib='gcov', define_name='HAVE_GCOV', mandatory=False)
187
188 def build(bld):
189     create_version_cc(bld, VERSION)
190
191     if bld.env.TARGET_WINDOWS:
192         boost_lib_suffix = '-mt'
193     else:
194         boost_lib_suffix = ''
195
196     bld(source='libdcp%s.pc.in' % bld.env.API_VERSION,
197         version=VERSION,
198         includedir='%s/include/libdcp%s' % (bld.env.PREFIX, bld.env.API_VERSION),
199         libs="-L${libdir} -ldcp%s -lcxml -lboost_system%s" % (bld.env.API_VERSION, boost_lib_suffix),
200         install_path='${LIBDIR}/pkgconfig')
201
202     bld.recurse('src')
203     bld.recurse('tools')
204     if not bld.env.DISABLE_TESTS:
205         bld.recurse('test')
206     if not bld.env.DISABLE_EXAMPLES:
207         bld.recurse('examples')
208
209     bld.add_post_fun(post)
210
211 def dist(ctx):
212     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__ GPATH GRTAGS GSYMS GTAGS'
213
214 def create_version_cc(bld, version):
215     if os.path.exists('.git'):
216         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
217         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
218         o = output[0].decode('utf-8')
219         commit = o.replace ("commit ", "")[0:10]
220     else:
221         commit = "release"
222
223     try:
224         text =  '#include "version.h"\n'
225         text += 'char const * dcp::git_commit = \"%s\";\n' % commit
226         text += 'char const * dcp::version = \"%s\";\n' % version
227         if bld.env.ENABLE_DEBUG:
228             debug_string = 'true'
229         else:
230             debug_string = 'false'
231         text += 'bool const dcp::built_with_debug = %s;\n' % debug_string
232         print('Writing version information to src/version.cc')
233         o = open('src/version.cc', 'w')
234         o.write(text)
235         o.close()
236     except IOError:
237         print('Could not open src/version.cc for writing\n')
238         sys.exit(-1)
239
240 def post(ctx):
241     if ctx.cmd == 'install':
242         ctx.exec_command('/sbin/ldconfig')
243
244 def tags(bld):
245     os.system('etags src/*.cc src/*.h')