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