Debian Codegraph @

wscript

raw
1 ## -*- python -*-
2 ## (C) 2007-2014 Gustavo J. A. M. Carneiro
3
4 from wutils import get_version
5
6 from waflib import Options
7 from waflib import Build
8
9 from waflib import Scripting
10 Scripting.dist_format = 'zip'
11
12 from waflib import Configure
13
14 from waflib import Logs
15
16 import os
17 import subprocess
18 import shutil
19 import sys
20 import tarfile
21 import re
22 import types
23
24
25 APPNAME='pybindgen'
26 top = '.'
27 out = 'build'
28
29
30
31 ## Add the pybindgen dir to PYTHONPATH, so that the examples and tests are properly built before pybindgen is installed.
32 if 'PYTHONPATH' in os.environ:
33 os.environ['PYTHONPATH'] = os.pathsep.join([os.getcwd(), os.environ['PYTHONPATH']])
34 else:
35 os.environ['PYTHONPATH'] = os.getcwd()
36
37
38 # http://coreygoldberg.blogspot.com/2009/07/python-zip-directories-recursively.html
39 def zipper(dir, zip_file, archive_main_folder=None):
40 import zipfile
41 zip = zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_DEFLATED)
42 root_len = len(os.path.abspath(dir))
43 for root, dirs, files in os.walk(dir):
44 archive_root = os.path.abspath(root)[root_len:]
45 for f in files:
46 fullpath = os.path.join(root, f)
47 archive_name = os.path.join(archive_root, f)
48 if archive_main_folder is not None:
49 if archive_name.startswith(os.sep):
50 n = archive_name[len(os.sep):]
51 else:
52 n = archive_name
53 archive_name = os.path.join(archive_main_folder, n)
54 zip.write(fullpath, archive_name, zipfile.ZIP_DEFLATED)
55 zip.close()
56
57
58
59 def options(opt):
60 opt.load('python_patched', tooldir="waf-tools")
61 opt.load('compiler_cc')
62 opt.load('compiler_cxx')
63 opt.load('cflags', tooldir="waf-tools")
64 opt.recurse('examples')
65
66
67 optgrp = opt.add_option_group("PyBindGen Options")
68
69 optgrp.add_option('--examples',
70 help=('Compile the example programs.'),
71 action="store_true", default=False,
72 dest='examples')
73
74 optgrp.add_option('--disable-pygccxml',
75 help=('Disable pygccxml for unit tests / examples.'),
76 action="store_true", default=False,
77 dest='disable_pygccxml')
78
79 optgrp.add_option('--valgrind',
80 help=('Run unit tests through valgrind.'),
81 action="store_true", default=False,
82 dest='valgrind')
83
84
85
86 def _check_compilation_flag(conf, flag, mode='cxx', linkflags=None):
87 """
88 Checks if the C++ compiler accepts a certain compilation flag or flags
89 flag: can be a string or a list of strings
90 """
91 l = []
92 if flag:
93 l.append(flag)
94 if isinstance(linkflags, list):
95 l.extend(linkflags)
96 else:
97 if linkflags:
98 l.append(linkflags)
99 if len(l) > 1:
100 flag_str = 'flags ' + ' '.join(l)
101 else:
102 flag_str = 'flag ' + ' '.join(l)
103 if len(flag_str) > 28:
104 flag_str = flag_str[:28] + "..."
105
106 conf.start_msg('Checking for compilation %s support' % (flag_str,))
107 env = conf.env.derive()
108
109 if mode == 'cc':
110 mode = 'c'
111
112 if mode == 'cxx':
113 fname = 'test.cc'
114 env.append_value('CXXFLAGS', flag)
115 else:
116 fname = 'test.c'
117 env.append_value('CFLAGS', flag)
118
119 if linkflags is not None:
120 env.append_value("LINKFLAGS", linkflags)
121
122 try:
123 retval = conf.run_c_code(code='#include <stdio.h>\nint main() { return 0; }\n',
124 env=env, compile_filename=fname,
125 features=[mode, mode+'program'], execute=False)
126 except conf.errors.ConfigurationError:
127 ok = False
128 else:
129 ok = (retval == 0)
130 conf.end_msg(ok)
131 return ok
132
133
134 # starting with waf 1.6, conf.check() becomes fatal by default if the
135 # test fails, this alternative method makes the test non-fatal, as it
136 # was in waf <= 1.5
137 def _check_nonfatal(conf, *args, **kwargs):
138 try:
139 return conf.check(*args, **kwargs)
140 except conf.errors.ConfigurationError:
141 return None
142
143
144 def configure(conf):
145
146 conf.check_compilation_flag = types.MethodType(_check_compilation_flag, conf)
147 conf.check_nonfatal = types.MethodType(_check_nonfatal, conf)
148
149 conf.load('command', tooldir="waf-tools")
150 conf.load('python_patched', tooldir="waf-tools")
151 conf.check_python_version((2,3))
152
153 try:
154 conf.load('compiler_cc')
155 conf.load('compiler_cxx')
156 except conf.errors.ConfigurationError:
157 Logs.warn("C/C++ compiler not detected. Unit tests and examples will not be compiled.")
158 conf.env['CXX'] = ''
159 else:
160 conf.load('cflags')
161 conf.check_python_headers()
162
163 if not Options.options.disable_pygccxml:
164 castxml = conf.find_program('castxml', mandatory=False)
165 gccxml = conf.find_program('gccxml', mandatory=False)
166 if not (gccxml or castxml):
167 conf.env['ENABLE_PYGCCXML'] = False
168 else:
169 try:
170 conf.check_python_module('pygccxml')
171 except conf.errors.ConfigurationError:
172 conf.env['ENABLE_PYGCCXML'] = False
173 else:
174 conf.env['ENABLE_PYGCCXML'] = True
175 if castxml:
176 conf.env['PYGCCXML_MODE'] = 'castxml'
177 else:
178 conf.env['PYGCCXML_MODE'] = 'gccxml'
179
180 # -fvisibility=hidden optimization
181 if (conf.env['CXX_NAME'] == 'gcc' and [int(x) for x in conf.env['CC_VERSION']] >= [4,0,0]
182 and conf.check_compilation_flag('-fvisibility=hidden')):
183 conf.env.append_value('CXXFLAGS_PYEXT', '-fvisibility=hidden')
184 conf.env.append_value('CCFLAGS_PYEXT', '-fvisibility=hidden')
185
186 # Add include path for our stdint.h replacement, if needed (pstdint.h)
187 if not conf.check_nonfatal(header_name='stdint.h'):
188 conf.env.append_value('CPPPATH', os.path.join(conf.curdir, 'include'))
189
190 if conf.check_nonfatal(header_name='boost/shared_ptr.hpp'):
191 conf.env['ENABLE_BOOST_SHARED_PTR'] = True
192
193 conf.recurse('benchmarks')
194 conf.recurse('examples')
195
196
197 def build(bld):
198 bld.recurse('pybindgen')
199
200 if bld.cmd == 'check':
201 bld.recurse('tests')
202
203 if Options.options.examples:
204 bld.recurse('examples')
205
206 if 0: # FIXME
207
208 if Options.commands.get('bench', False) or Options.commands['clean']:
209 bld.recurse('benchmarks')
210
211
212 check_context = Build.BuildContext
213 def bench(bld):
214 "run the benchmarks; requires many tools, used by maintainers only"
215 Scripting.build(bld)
216 env = g_bld.env
217
218 print("Running benchmarks...")
219 retval = subprocess.Popen([env['PYTHON'], '-O', 'benchmarks/bench.py',
220 "build/default/benchmarks/results.xml", ' '.join(env['CXXFLAGS_PYEXT'] + env['CXXFLAGS'])]).wait()
221 if retval:
222 raise SystemExit(retval)
223
224 print("Generating benchmarks report...")
225 retval = subprocess.Popen([env['PYTHON'], '-O', 'benchmarks/plotresults.py',
226 "build/default/benchmarks/results.xml",
227 "build/default/benchmarks/results"]).wait()
228 if retval:
229 raise SystemExit(retval)
230
231
232 from waflib import Context, Build, Scripting
233 class CheckContext(Context.Context):
234 """run the unit tests"""
235 cmd = 'check'
236
237 def execute(self):
238
239 # first we execute the build
240 bld = Context.create_context("build")
241 bld.options = Options.options # provided for convenience
242 bld.cmd = "check"
243 bld.execute()
244
245
246 if Options.options.verbose:
247 verbosity = ['-v']
248 else:
249 verbosity = []
250
251 if Options.options.valgrind:
252 valgrind = ['valgrind', '--leak-check=full', '--leak-resolution=low']
253 else:
254 valgrind = []
255
256 print("Running pure python unit tests...")
257 python = bld.env['PYTHON'][0]
258 retval1 = subprocess.Popen([python, 'tests/test.py'] + verbosity).wait()
259
260 env = bld.env
261
262 retvals = []
263
264 if env['CXX']:
265 print("Running manual module generation unit tests (module foo)...")
266 retvals.append(subprocess.Popen(valgrind + [python, 'tests/footest.py', '1', 'none'] + verbosity).wait())
267 else:
268 print("Skipping manual module generation unit tests (no C/C++ compiler)...")
269
270 if env['ENABLE_PYGCCXML']:
271 print("Running automatically scanned module generation unit tests (module foo2)...")
272 retvals.append(subprocess.Popen(valgrind + [python, 'tests/footest.py', '2', env['PYGCCXML_MODE']] + verbosity).wait())
273
274 print("Running module generated by automatically generated python script unit tests (module foo3)...")
275 retvals.append(subprocess.Popen(valgrind + [python, 'tests/footest.py', '3', env['PYGCCXML_MODE']] + verbosity).wait())
276
277 print("Running module generated by generated and split python script unit tests (module foo4)...")
278 retvals.append(subprocess.Popen(valgrind + [python, 'tests/footest.py', '4', env['PYGCCXML_MODE']] + verbosity).wait())
279
280 print("Running semi-automatically scanned c-hello module ('hello')...")
281 retvals.append(subprocess.Popen(valgrind + [python, 'tests/c-hello/hellotest.py'] + verbosity).wait())
282 else:
283 print("Skipping automatically scanned module generation unit tests (pygccxml missing)...")
284 print("Skipping module generated by automatically generated python script unit tests (pygccxml missing)...")
285 print("Skipping module generated by generated and split python script unit tests (pygccxml missing)...")
286 print("Skipping semi-automatically scanned c-hello module (pygccxml missing)...")
287
288 if env['ENABLE_BOOST_SHARED_PTR']:
289 print("Running boost::shared_ptr unit tests...")
290 retvals.append(subprocess.Popen(valgrind + [python, 'tests/boost/bartest.py'] + verbosity).wait())
291 else:
292 print("Skipping boost::shared_ptr unit tests (boost headers not found)...")
293
294 if any(retvals):
295 Logs.error("Unit test failures")
296 raise SystemExit(2)
297
298
299
300 class DistContext(Scripting.Dist):
301
302 cmd = 'dist'
303 def get_base_name(self):
304 srcdir = '.'
305 version = get_version(srcdir)
306 return "pybindgen-" + version
307
308
309 def execute(self):
310 blddir = './build'
311 srcdir = '.'
312 subprocess.Popen(["python", os.path.join(srcdir, "setup.py"), "clean"]).wait()
313 version = get_version(srcdir)
314 subprocess.Popen([os.path.join(srcdir, "generate-ChangeLog")], shell=True).wait()
315 try:
316 os.chmod(os.path.join(srcdir, "ChangeLog"), 0o644)
317 except OSError:
318 pass
319
320 ## Package the api docs in a separate tarball
321
322 # generate the docs first
323 r = subprocess.Popen(["make", "html"], cwd='doc').wait()
324 if r:
325 raise SystemExit(r)
326
327 apidocs = 'apidocs'
328 if not os.path.isdir('doc/_build/html'):
329 Logs.warn("Not creating docs archive: the `doc/_build/html' directory does not exist")
330 else:
331 zipper('doc/_build/html', "pybindgen-%s-docs.zip" % version)
332
333
334 try:
335 os.unlink('waf-light')
336 except OSError:
337 pass
338
339 super(DistContext, self).execute() # -----------------------------
340
341 def get_excl(self):
342 return super(DistContext, self).get_excl() + ' *.zip doc/_build .shelf *.pstat'
343
344
345
346 def docs(ctx):
347 "generate API documentation, using epydoc"
348 retval = subprocess.Popen(["make", "html"], cwd='doc').wait()
349 if retval:
350 Logs.error("make returned with code %i" % retval)
351 raise SystemExit(2)
352