64c7e71432b727894c435b93183ce5cd61465b97
[libdcp.git] / wscript
1 #
2 #    Copyright (C) 2012-2016 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
20 import subprocess
21 import os
22 import sys
23 import distutils.spawn
24 from waflib import Logs, Context
25
26 APPNAME = 'libdcp'
27 VERSION = '1.3.3devel'
28 API_VERSION = '-1.0'
29
30 def options(opt):
31     opt.load('compiler_cxx')
32     opt.add_option('--target-windows', action='store_true', default=False, help='set up to do a cross-compile to Windows')
33     opt.add_option('--enable-debug', action='store_true', default=False, help='build with debugging information and without optimisation')
34     opt.add_option('--static', action='store_true', default=False, help='build libdcp statically, and link statically to openjpeg, cxml, asdcplib-cth')
35     opt.add_option('--disable-tests', action='store_true', default=False, help='disable building of tests')
36     opt.add_option('--disable-gcov', action='store_true', default=False, help='don''t use gcov in tests')
37     opt.add_option('--disable-examples', action='store_true', default=False, help='disable building of examples')
38     opt.add_option('--enable-openmp', action='store_true', default=False, help='enable use of OpenMP')
39
40 def configure(conf):
41     conf.load('compiler_cxx')
42     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-D_FILE_OFFSET_BITS=64', '-D__STDC_FORMAT_MACROS'])
43     gcc = conf.env['CC_VERSION']
44     if int(gcc[0]) >= 4 and int(gcc[1]) > 1:
45         conf.env.append_value('CXXFLAGS', ['-Wno-maybe-uninitialized'])
46     conf.env.append_value('CXXFLAGS', ['-DLIBDCP_VERSION="%s"' % VERSION])
47
48     conf.env.TARGET_WINDOWS = conf.options.target_windows
49     conf.env.TARGET_OSX = sys.platform == 'darwin'
50     conf.env.ENABLE_DEBUG = conf.options.enable_debug
51     conf.env.DISABLE_TESTS = conf.options.disable_tests
52     conf.env.DISABLE_EXAMPLES = conf.options.disable_examples
53     conf.env.STATIC = conf.options.static
54     conf.env.API_VERSION = API_VERSION
55
56     if conf.options.target_windows:
57         conf.env.append_value('CXXFLAGS', '-DLIBDCP_WINDOWS')
58     else:
59         conf.env.append_value('CXXFLAGS', '-DLIBDCP_POSIX')
60
61     if conf.env.TARGET_OSX:
62         conf.env.append_value('CXXFLAGS', ['-Wno-unused-result', '-Wno-unused-parameter', '-Wno-unused-local-typedef'])
63
64     if conf.options.enable_openmp:
65         conf.env.append_value('CXXFLAGS', ['-fopenmp', '-DLIBDCP_OPENMP'])
66         conf.env.LIB_OPENMP = ['gomp']
67
68     conf.check_cfg(package='openssl', args='--cflags --libs', uselib_store='OPENSSL', mandatory=True)
69     conf.check_cfg(package='libxml++-2.6', args='--cflags --libs', uselib_store='LIBXML++', mandatory=True)
70     conf.check_cfg(package='xmlsec1', args='--cflags --libs', uselib_store='XMLSEC1', mandatory=True)
71     # Remove erroneous escaping of quotes from xmlsec1 defines
72     conf.env.DEFINES_XMLSEC1 = [f.replace('\\', '') for f in conf.env.DEFINES_XMLSEC1]
73
74     # ImageMagick / GraphicsMagick
75     if distutils.spawn.find_executable('Magick++-config'):
76         conf.check_cfg(package='', path='Magick++-config', args='--cppflags --cxxflags --libs', uselib_store='MAGICK', mandatory=True)
77     else:
78         image = conf.check_cfg(package='ImageMagick++', args='--cflags --libs', uselib_store='MAGICK', mandatory=False)
79         graphics = conf.check_cfg(package='GraphicsMagick++', args='--cflags --libs', uselib_store='MAGICK', mandatory=False)
80         if image is None and graphics is None:
81             Logs.pprint('RED', 'Neither ImageMagick++ nor GraphicsMagick++ found: one or the other is required')
82
83     conf.check_cfg(package='sndfile', args='--cflags --libs', uselib_store='SNDFILE', mandatory=False)
84
85     if conf.options.static:
86         conf.check_cfg(package='libopenjp2', args='--cflags', atleast_version='2.1.0', uselib_store='OPENJPEG', mandatory=True)
87         conf.env.STLIB_OPENJPEG = ['openjp2']
88         conf.env.HAVE_CXML = 1
89         conf.env.LIB_CXML = ['xml++-2.6', 'glibmm-2.4']
90         conf.env.STLIB_CXML = ['cxml']
91         conf.check_cfg(package='libasdcp-cth', atleast_version='0.0.1', args='--cflags', uselib_store='ASDCPLIB_CTH', mandatory=True)
92         conf.env.HAVE_ASDCPLIB_CTH = 1
93         conf.env.STLIB_ASDCPLIB_CTH = ['asdcp-cth', 'kumu-cth']
94     else:
95         conf.check_cfg(package='libopenjp2', args='--cflags --libs', atleast_version='2.1.0', uselib_store='OPENJPEG', mandatory=True)
96         conf.check_cfg(package='libcxml', atleast_version='0.15.1', args='--cflags --libs', uselib_store='CXML', mandatory=True)
97         conf.check_cfg(package='libasdcp-cth', atleast_version='0.1.0', args='--cflags --libs', uselib_store='ASDCPLIB_CTH', mandatory=True)
98
99     if conf.options.target_windows:
100         boost_lib_suffix = '-mt'
101     else:
102         boost_lib_suffix = ''
103
104     if conf.options.enable_debug:
105         conf.env.append_value('CXXFLAGS', '-g')
106     else:
107         # Somewhat experimental use of -O2 rather than -O3 to see if
108         # Windows builds are any more reliable
109         conf.env.append_value('CXXFLAGS', '-O2')
110
111     conf.check_cxx(fragment="""
112                             #include <boost/version.hpp>\n
113                             #if BOOST_VERSION < 104500\n
114                             #error boost too old\n
115                             #endif\n
116                             int main(void) { return 0; }\n
117                             """,
118                    mandatory=True,
119                    msg='Checking for boost library >= 1.45',
120                    okmsg='yes',
121                    errmsg='too old\nPlease install boost version 1.45 or higher.')
122
123     conf.check_cxx(fragment="""
124                             #include <boost/filesystem.hpp>\n
125                             int main() { boost::filesystem::copy_file ("a", "b"); }\n
126                             """,
127                    msg='Checking for boost filesystem library',
128                    libpath='/usr/local/lib',
129                    lib=['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
130                    uselib_store='BOOST_FILESYSTEM')
131
132     conf.check_cxx(fragment="""
133                             #include <boost/signals2.hpp>\n
134                             int main() { boost::signals2::signal<void (int)> x; }\n
135                             """,
136                    msg='Checking for boost signals2 library',
137                    uselib_store='BOOST_SIGNALS2')
138
139     conf.check_cxx(fragment="""
140                             #include <boost/date_time.hpp>\n
141                             int main() { boost::gregorian::day_clock::local_day(); }\n
142                             """,
143                    msg='Checking for boost datetime library',
144                    libpath='/usr/local/lib',
145                    lib=['boost_date_time%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
146                    uselib_store='BOOST_DATETIME')
147
148     if not conf.env.DISABLE_TESTS:
149         conf.recurse('test')
150         if not conf.options.disable_gcov:
151             conf.check(lib='gcov', define_name='HAVE_GCOV', mandatory=False)
152
153     # libxml++ 2.39.1 and later must be built with -std=c++11
154     libxmlpp_version = conf.cmd_and_log(['pkg-config', '--modversion', 'libxml++-2.6'], output=Context.STDOUT, quiet=Context.BOTH)
155     s = libxmlpp_version.split('.')
156     v = (int(s[0]) << 16) | (int(s[1]) << 8) | int(s[2])
157     if v >= 0x022701:
158         conf.env.append_value('CXXFLAGS', '-std=c++11')
159
160 def build(bld):
161     create_version_cc(bld, VERSION)
162
163     if bld.env.TARGET_WINDOWS:
164         boost_lib_suffix = '-mt'
165     else:
166         boost_lib_suffix = ''
167
168     bld(source='libdcp%s.pc.in' % bld.env.API_VERSION,
169         version=VERSION,
170         includedir='%s/include/libdcp%s' % (bld.env.PREFIX, bld.env.API_VERSION),
171         libs="-L${libdir} -ldcp%s -lcxml -lboost_system%s" % (bld.env.API_VERSION, boost_lib_suffix),
172         install_path='${LIBDIR}/pkgconfig')
173
174     bld.recurse('src')
175     bld.recurse('tools')
176     if not bld.env.DISABLE_TESTS:
177         bld.recurse('test')
178     if not bld.env.DISABLE_EXAMPLES:
179         bld.recurse('examples')
180
181     bld.add_post_fun(post)
182
183 def dist(ctx):
184     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__ GPATH GRTAGS GSYMS GTAGS'
185
186 def create_version_cc(bld, version):
187     if os.path.exists('.git'):
188         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
189         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
190         o = output[0].decode('utf-8')
191         commit = o.replace ("commit ", "")[0:10]
192     else:
193         commit = "release"
194
195     try:
196         text =  '#include "version.h"\n'
197         text += 'char const * dcp::git_commit = \"%s\";\n' % commit
198         text += 'char const * dcp::version = \"%s\";\n' % version
199         if bld.env.ENABLE_DEBUG:
200             debug_string = 'true'
201         else:
202             debug_string = 'false'
203         text += 'bool const dcp::built_with_debug = %s;\n' % debug_string
204         print('Writing version information to src/version.cc')
205         o = open('src/version.cc', 'w')
206         o.write(text)
207         o.close()
208     except IOError:
209         print('Could not open src/version.cc for writing\n')
210         sys.exit(-1)
211
212 def post(ctx):
213     if ctx.cmd == 'install':
214         ctx.exec_command('/sbin/ldconfig')
215
216 def tags(bld):
217     os.system('etags src/*.cc src/*.h')