| 1 | # -*- coding: utf-8 -*- |
---|
| 2 | # |
---|
| 3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
| 4 | # Copyright (C) 2007-2010 Edgewall Software |
---|
| 5 | # All rights reserved. |
---|
| 6 | # |
---|
| 7 | # This software is licensed as described in the file COPYING, which |
---|
| 8 | # you should have received as part of this distribution. The terms |
---|
| 9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
| 10 | |
---|
1 | 11 | """Recipe commands for build tasks commonly used for C/C++ projects.""" |
---|
| 12 | |
---|
1 | 13 | import logging |
---|
1 | 14 | import re |
---|
1 | 15 | import os |
---|
1 | 16 | import posixpath |
---|
1 | 17 | import shlex |
---|
| 18 | |
---|
1 | 19 | from bitten.build import CommandLine, FileSet |
---|
1 | 20 | from bitten.util import xmlio |
---|
| 21 | |
---|
1 | 22 | log = logging.getLogger('bitten.build.ctools') |
---|
| 23 | |
---|
1 | 24 | __docformat__ = 'restructuredtext en' |
---|
| 25 | |
---|
1 | 26 | def configure(ctxt, file_='configure', enable=None, disable=None, with_=None, |
---|
1 | 27 | without=None, cflags=None, cxxflags=None, prefix=None, **kw): |
---|
| 28 | """Run a ``configure`` script. |
---|
| 29 | |
---|
| 30 | :param ctxt: the build context |
---|
| 31 | :type ctxt: `Context` |
---|
| 32 | :param file\_: name of the configure script |
---|
| 33 | :param enable: names of the features to enable, seperated by spaces |
---|
| 34 | :param disable: names of the features to disable, separated by spaces |
---|
| 35 | :param with\_: names of external packages to include |
---|
| 36 | :param without: names of external packages to exclude |
---|
| 37 | :param cflags: ``CFLAGS`` to pass to the configure script |
---|
| 38 | :param cxxflags: ``CXXFLAGS`` to pass to the configure script |
---|
| 39 | :param prefix: install prefix to pass to the configure script, will be postfixed by the machine name from the build |
---|
| 40 | """ |
---|
0 | 41 | args = [] |
---|
0 | 42 | if enable: |
---|
0 | 43 | args += ['--enable-%s' % feature for feature in enable.split()] |
---|
0 | 44 | if disable: |
---|
0 | 45 | args += ['--disable-%s' % feature for feature in disable.split()] |
---|
| 46 | # since 'with' is a reserved word in python, we need to handle the argument carefully |
---|
0 | 47 | with_ = kw.pop('with', with_) |
---|
0 | 48 | for key in kw: |
---|
0 | 49 | raise TypeError("configure() got an unexpected keyword argument '%s'" % key) |
---|
0 | 50 | if with_: |
---|
0 | 51 | for pkg in with_.split(): |
---|
0 | 52 | pkg_path = pkg + '.path' |
---|
0 | 53 | if pkg_path in ctxt.config: |
---|
0 | 54 | args.append('--with-%s=%s' % (pkg, ctxt.config[pkg_path])) |
---|
0 | 55 | else: |
---|
0 | 56 | args.append('--with-%s' % pkg) |
---|
0 | 57 | if without: |
---|
0 | 58 | args += ['--without-%s' % pkg for pkg in without.split()] |
---|
0 | 59 | if cflags: |
---|
0 | 60 | args.append('CFLAGS=%s' % cflags) |
---|
0 | 61 | if cxxflags: |
---|
0 | 62 | args.append('CXXFLAGS=%s' % cxxflags) |
---|
0 | 63 | if prefix: |
---|
0 | 64 | args.append('--prefix=%s' % prefix) |
---|
| 65 | |
---|
0 | 66 | from bitten.build import shtools |
---|
0 | 67 | returncode = shtools.execute(ctxt, file_=file_, args=args) |
---|
0 | 68 | if returncode != 0: |
---|
0 | 69 | ctxt.error('configure failed (%s)' % returncode) |
---|
| 70 | |
---|
1 | 71 | def autoreconf(ctxt, file_='configure', force=None, install=None, symlink=None, |
---|
1 | 72 | warnings=None, prepend_include=None, include =None): |
---|
| 73 | """Run the autotoll ``autoreconf``. |
---|
| 74 | |
---|
| 75 | :param ctxt: the build context |
---|
| 76 | :type ctxt: `Context` |
---|
| 77 | :param force: consider all files obsolete |
---|
| 78 | :param install: copy missing auxiliary files |
---|
| 79 | :param symlink: install symbolic links instead of copies |
---|
| 80 | :param warnings: report the warnings falling in CATEGORY |
---|
| 81 | :param prepend_include: prepend directories to search path |
---|
| 82 | :param include: append directories to search path |
---|
| 83 | |
---|
| 84 | """ |
---|
0 | 85 | args = [] |
---|
0 | 86 | if install: |
---|
0 | 87 | args.append('--install') |
---|
0 | 88 | if symlink: |
---|
0 | 89 | args.append('--symlink') |
---|
0 | 90 | if force: |
---|
0 | 91 | args.append('--force') |
---|
0 | 92 | if warnings: |
---|
0 | 93 | args.append('--warnings=%s' % warnings) |
---|
| 94 | |
---|
0 | 95 | if include: |
---|
0 | 96 | args += ['--include=%s' % inc for inc in include.split()] |
---|
0 | 97 | if prepend_include: |
---|
0 | 98 | args += ['--prepend-include=%s' % pinc for pinc in prepend_include.split()] |
---|
| 99 | |
---|
0 | 100 | from bitten.build import shtools |
---|
0 | 101 | returncode = shtools.execute(ctxt, 'autoreconf', args=args) |
---|
0 | 102 | if returncode != 0: |
---|
0 | 103 | ctxt.error('autoreconf failed (%s)' % returncode) |
---|
| 104 | |
---|
1 | 105 | def make(ctxt, target=None, file_=None, keep_going=False, directory=None, jobs=None, args=None): |
---|
| 106 | """Execute a Makefile target. |
---|
| 107 | |
---|
| 108 | :param ctxt: the build context |
---|
| 109 | :type ctxt: `Context` |
---|
| 110 | :param file\_: name of the Makefile |
---|
| 111 | :param keep_going: whether make should keep going when errors are |
---|
| 112 | encountered |
---|
| 113 | :param directory: directory in which to build; defaults to project source directory |
---|
| 114 | :param jobs: number of concurrent jobs to run |
---|
| 115 | :param args: command-line arguments to pass to the script |
---|
| 116 | """ |
---|
0 | 117 | executable = ctxt.config.get_filepath('make.path') or 'make' |
---|
| 118 | |
---|
0 | 119 | if directory is None: |
---|
0 | 120 | directory = ctxt.basedir |
---|
| 121 | |
---|
0 | 122 | margs = ['--directory', directory] |
---|
| 123 | |
---|
0 | 124 | if file_: |
---|
0 | 125 | margs += ['--file', ctxt.resolve(file_)] |
---|
0 | 126 | if keep_going: |
---|
0 | 127 | margs.append('--keep-going') |
---|
0 | 128 | if target: |
---|
0 | 129 | margs.append(target) |
---|
0 | 130 | if jobs: |
---|
0 | 131 | margs += ['--jobs', jobs] |
---|
| 132 | |
---|
0 | 133 | if args: |
---|
0 | 134 | if isinstance(args, basestring): |
---|
0 | 135 | margs += shlex.split(args) |
---|
| 136 | |
---|
0 | 137 | from bitten.build import shtools |
---|
0 | 138 | returncode = shtools.execute(ctxt, executable=executable, args=margs) |
---|
0 | 139 | if returncode != 0: |
---|
0 | 140 | ctxt.error('make failed (%s)' % returncode) |
---|
| 141 | |
---|
1 | 142 | def cppunit(ctxt, file_=None, srcdir=None): |
---|
| 143 | """Collect CppUnit XML data. |
---|
| 144 | |
---|
| 145 | :param ctxt: the build context |
---|
| 146 | :type ctxt: `Context` |
---|
| 147 | :param file\_: path of the file containing the CppUnit results; may contain |
---|
| 148 | globbing wildcards to match multiple files |
---|
| 149 | :param srcdir: name of the directory containing the source files, used to |
---|
| 150 | link the test results to the corresponding files |
---|
| 151 | """ |
---|
2 | 152 | assert file_, 'Missing required attribute "file"' |
---|
| 153 | |
---|
1 | 154 | try: |
---|
1 | 155 | fileobj = file(ctxt.resolve(file_), 'r') |
---|
1 | 156 | try: |
---|
1 | 157 | total, failed = 0, 0 |
---|
1 | 158 | results = xmlio.Fragment() |
---|
4 | 159 | for group in xmlio.parse(fileobj): |
---|
3 | 160 | if group.name not in ('FailedTests', 'SuccessfulTests'): |
---|
1 | 161 | continue |
---|
5 | 162 | for child in group.children(): |
---|
3 | 163 | test = xmlio.Element('test') |
---|
3 | 164 | name = child.children('Name').next().gettext() |
---|
3 | 165 | if '::' in name: |
---|
3 | 166 | parts = name.split('::') |
---|
3 | 167 | test.attr['fixture'] = '::'.join(parts[:-1]) |
---|
3 | 168 | name = parts[-1] |
---|
3 | 169 | test.attr['name'] = name |
---|
| 170 | |
---|
3 | 171 | for location in child.children('Location'): |
---|
1 | 172 | for file_elem in location.children('File'): |
---|
1 | 173 | filepath = file_elem.gettext() |
---|
1 | 174 | if srcdir is not None: |
---|
0 | 175 | filepath = posixpath.join(srcdir, filepath) |
---|
1 | 176 | test.attr['file'] = filepath |
---|
1 | 177 | break |
---|
1 | 178 | for line_elem in location.children('Line'): |
---|
1 | 179 | test.attr['line'] = line_elem.gettext() |
---|
1 | 180 | break |
---|
1 | 181 | break |
---|
| 182 | |
---|
3 | 183 | if child.name == 'FailedTest': |
---|
2 | 184 | for message in child.children('Message'): |
---|
1 | 185 | test.append(xmlio.Element('traceback')[ |
---|
1 | 186 | message.gettext() |
---|
1 | 187 | ]) |
---|
1 | 188 | test.attr['status'] = 'failure' |
---|
1 | 189 | failed += 1 |
---|
1 | 190 | else: |
---|
2 | 191 | test.attr['status'] = 'success' |
---|
| 192 | |
---|
3 | 193 | results.append(test) |
---|
3 | 194 | total += 1 |
---|
| 195 | |
---|
1 | 196 | if failed: |
---|
1 | 197 | ctxt.error('%d of %d test%s failed' % (failed, total, |
---|
1 | 198 | total != 1 and 's' or '')) |
---|
| 199 | |
---|
1 | 200 | ctxt.report('test', results) |
---|
| 201 | |
---|
0 | 202 | finally: |
---|
1 | 203 | fileobj.close() |
---|
| 204 | |
---|
0 | 205 | except IOError, e: |
---|
0 | 206 | log.warning('Error opening CppUnit results file (%s)', e) |
---|
0 | 207 | except xmlio.ParseError, e: |
---|
0 | 208 | print e |
---|
0 | 209 | log.warning('Error parsing CppUnit results file (%s)', e) |
---|
| 210 | |
---|
1 | 211 | def cunit (ctxt, file_=None, srcdir=None): |
---|
| 212 | """Collect CUnit XML data. |
---|
| 213 | |
---|
| 214 | :param ctxt: the build context |
---|
| 215 | :type ctxt: `Context` |
---|
| 216 | :param file\_: path of the file containing the CUnit results; may contain |
---|
| 217 | globbing wildcards to match multiple files |
---|
| 218 | :param srcdir: name of the directory containing the source files, used to |
---|
| 219 | link the test results to the corresponding files |
---|
| 220 | """ |
---|
0 | 221 | assert file_, 'Missing required attribute "file"' |
---|
| 222 | |
---|
0 | 223 | try: |
---|
0 | 224 | fileobj = file(ctxt.resolve(file_), 'r') |
---|
0 | 225 | try: |
---|
0 | 226 | total, failed = 0, 0 |
---|
0 | 227 | results = xmlio.Fragment() |
---|
0 | 228 | log_elem = xmlio.Fragment() |
---|
0 | 229 | def info (msg): |
---|
0 | 230 | log.info (msg) |
---|
0 | 231 | log_elem.append (xmlio.Element ('message', level='info')[msg]) |
---|
0 | 232 | def warning (msg): |
---|
0 | 233 | log.warning (msg) |
---|
0 | 234 | log_elem.append (xmlio.Element ('message', level='warning')[msg]) |
---|
0 | 235 | def error (msg): |
---|
0 | 236 | log.error (msg) |
---|
0 | 237 | log_elem.append (xmlio.Element ('message', level='error')[msg]) |
---|
0 | 238 | for node in xmlio.parse(fileobj): |
---|
0 | 239 | if node.name != 'CUNIT_RESULT_LISTING': |
---|
0 | 240 | continue |
---|
0 | 241 | for suiteRun in node.children ('CUNIT_RUN_SUITE'): |
---|
0 | 242 | for suite in suiteRun.children(): |
---|
0 | 243 | if suite.name not in ('CUNIT_RUN_SUITE_SUCCESS', 'CUNIT_RUN_SUITE_FAILURE'): |
---|
0 | 244 | warning ("Unknown node: %s" % suite.name) |
---|
0 | 245 | continue |
---|
0 | 246 | suiteName = suite.children ('SUITE_NAME').next().gettext() |
---|
0 | 247 | info ("%s [%s]" % ("*" * (57 - len (suiteName)), suiteName)) |
---|
0 | 248 | for record in suite.children ('CUNIT_RUN_TEST_RECORD'): |
---|
0 | 249 | for result in record.children(): |
---|
0 | 250 | if result.name not in ('CUNIT_RUN_TEST_SUCCESS', 'CUNIT_RUN_TEST_FAILURE'): |
---|
0 | 251 | continue |
---|
0 | 252 | testName = result.children ('TEST_NAME').next().gettext() |
---|
0 | 253 | info ("Running %s..." % testName); |
---|
0 | 254 | test = xmlio.Element('test') |
---|
0 | 255 | test.attr['fixture'] = suiteName |
---|
0 | 256 | test.attr['name'] = testName |
---|
0 | 257 | if result.name == 'CUNIT_RUN_TEST_FAILURE': |
---|
0 | 258 | error ("%s(%d): %s" |
---|
0 | 259 | % (result.children ('FILE_NAME').next().gettext(), |
---|
0 | 260 | int (result.children ('LINE_NUMBER').next().gettext()), |
---|
0 | 261 | result.children ('CONDITION').next().gettext())) |
---|
0 | 262 | test.attr['status'] = 'failure' |
---|
0 | 263 | failed += 1 |
---|
0 | 264 | else: |
---|
0 | 265 | test.attr['status'] = 'success' |
---|
| 266 | |
---|
0 | 267 | results.append(test) |
---|
0 | 268 | total += 1 |
---|
| 269 | |
---|
0 | 270 | if failed: |
---|
0 | 271 | ctxt.error('%d of %d test%s failed' % (failed, total, |
---|
0 | 272 | total != 1 and 's' or '')) |
---|
| 273 | |
---|
0 | 274 | ctxt.report('test', results) |
---|
0 | 275 | ctxt.log (log_elem) |
---|
| 276 | |
---|
0 | 277 | finally: |
---|
0 | 278 | fileobj.close() |
---|
| 279 | |
---|
0 | 280 | except IOError, e: |
---|
0 | 281 | log.warning('Error opening CUnit results file (%s)', e) |
---|
0 | 282 | except xmlio.ParseError, e: |
---|
0 | 283 | print e |
---|
0 | 284 | log.warning('Error parsing CUnit results file (%s)', e) |
---|
| 285 | |
---|
1 | 286 | def gcov(ctxt, include=None, exclude=None, prefix=None, root=""): |
---|
| 287 | """Run ``gcov`` to extract coverage data where available. |
---|
| 288 | |
---|
| 289 | :param ctxt: the build context |
---|
| 290 | :type ctxt: `Context` |
---|
| 291 | :param include: patterns of files and directories to include |
---|
| 292 | :param exclude: patterns of files and directories that should be excluded |
---|
| 293 | :param prefix: optional prefix name that is added to object files by the |
---|
| 294 | build system |
---|
| 295 | :param root: optional root path in which the build system puts the object |
---|
| 296 | files |
---|
| 297 | """ |
---|
2 | 298 | file_re = re.compile(r'^File (?:\'|\`)(?P<file>[^\']+)\'\s*$') |
---|
2 | 299 | lines_re = re.compile(r'^Lines executed:(?P<cov>\d+\.\d+)\% of (?P<num>\d+)\s*$') |
---|
| 300 | |
---|
2 | 301 | files = [] |
---|
6 | 302 | for filename in FileSet(ctxt.basedir, include, exclude): |
---|
4 | 303 | if os.path.splitext(filename)[1] in ('.c', '.cpp', '.cc', '.cxx'): |
---|
1 | 304 | files.append(filename) |
---|
| 305 | |
---|
2 | 306 | coverage = xmlio.Fragment() |
---|
2 | 307 | log_elem = xmlio.Fragment() |
---|
2 | 308 | def info (msg): |
---|
1 | 309 | log.info (msg) |
---|
1 | 310 | log_elem.append (xmlio.Element ('message', level='info')[msg]) |
---|
2 | 311 | def warning (msg): |
---|
0 | 312 | log.warning (msg) |
---|
0 | 313 | log_elem.append (xmlio.Element ('message', level='warning')[msg]) |
---|
2 | 314 | def error (msg): |
---|
0 | 315 | log.error (msg) |
---|
0 | 316 | log_elem.append (xmlio.Element ('message', level='error')[msg]) |
---|
| 317 | |
---|
3 | 318 | for srcfile in files: |
---|
| 319 | # Determine the coverage for each source file by looking for a .gcno |
---|
| 320 | # and .gcda pair |
---|
1 | 321 | info ("Getting coverage info for %s" % srcfile) |
---|
1 | 322 | filepath, filename = os.path.split(srcfile) |
---|
1 | 323 | stem = os.path.splitext(filename)[0] |
---|
1 | 324 | if prefix is not None: |
---|
0 | 325 | stem = prefix + '-' + stem |
---|
| 326 | |
---|
1 | 327 | objfile = os.path.join (root, filepath, stem + '.o') |
---|
1 | 328 | if not os.path.isfile(ctxt.resolve(objfile)): |
---|
0 | 329 | warning ('No object file found for %s at %s' % (srcfile, objfile)) |
---|
0 | 330 | continue |
---|
1 | 331 | if not os.path.isfile (ctxt.resolve (os.path.join (root, filepath, stem + '.gcno'))): |
---|
0 | 332 | warning ('No .gcno file found for %s at %s' % (srcfile, os.path.join (root, filepath, stem + '.gcno'))) |
---|
0 | 333 | continue |
---|
1 | 334 | if not os.path.isfile (ctxt.resolve (os.path.join (root, filepath, stem + '.gcda'))): |
---|
0 | 335 | warning ('No .gcda file found for %s at %s' % (srcfile, os.path.join (root, filepath, stem + '.gcda'))) |
---|
0 | 336 | continue |
---|
| 337 | |
---|
1 | 338 | num_lines, num_covered = 0, 0 |
---|
1 | 339 | skip_block = False |
---|
1 | 340 | cmd = CommandLine('gcov', ['-b', '-n', '-o', objfile, srcfile], |
---|
1 | 341 | cwd=ctxt.basedir) |
---|
12 | 342 | for out, err in cmd.execute(): |
---|
11 | 343 | if out == '': # catch blank lines, reset the block state... |
---|
0 | 344 | skip_block = False |
---|
11 | 345 | elif out and not skip_block: |
---|
| 346 | # Check for a file name |
---|
11 | 347 | match = file_re.match(out) |
---|
11 | 348 | if match: |
---|
2 | 349 | if os.path.isabs(match.group('file')): |
---|
0 | 350 | skip_block = True |
---|
0 | 351 | continue |
---|
0 | 352 | else: |
---|
| 353 | # check for a "Lines executed" message |
---|
9 | 354 | match = lines_re.match(out) |
---|
9 | 355 | if match: |
---|
2 | 356 | lines = float(match.group('num')) |
---|
2 | 357 | cov = float(match.group('cov')) |
---|
2 | 358 | num_covered += int(lines * cov / 100) |
---|
2 | 359 | num_lines += int(lines) |
---|
1 | 360 | if cmd.returncode != 0: |
---|
0 | 361 | continue |
---|
| 362 | |
---|
1 | 363 | module = xmlio.Element('coverage', name=os.path.basename(srcfile), |
---|
1 | 364 | file=srcfile.replace(os.sep, '/'), |
---|
1 | 365 | lines=num_lines, percentage=0) |
---|
1 | 366 | if num_lines: |
---|
1 | 367 | percent = int(round(num_covered * 100 / num_lines)) |
---|
1 | 368 | module.attr['percentage'] = percent |
---|
1 | 369 | coverage.append(module) |
---|
| 370 | |
---|
2 | 371 | ctxt.report('coverage', coverage) |
---|
2 | 372 | ctxt.log (log_elem) |
---|