|
| 1 | +# $Copyright(c) 2013 Progress Software Corporation (PSC). All rights reserved.$ |
| 2 | +# $Copyright (c) 2013-2017 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.$ |
| 3 | +# Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG |
| 4 | +# |
| 5 | +# $Id: doxygen.py 392236 2021-06-17 15:25:31Z sgol $ |
| 6 | + |
| 7 | +import subprocess, re |
| 8 | + |
| 9 | +from xpybuild.basetarget import BaseTarget |
| 10 | + |
| 11 | +from xpybuild.buildcommon import * |
| 12 | +from xpybuild.utils.buildexceptions import * |
| 13 | +from xpybuild.utils.fileutils import openForWrite, mkdir |
| 14 | +from xpybuild.pathsets import PathSet |
| 15 | +from xpybuild.propertysupport import defineOption |
| 16 | + |
| 17 | +defineOption('doxygen.exepath', None) |
| 18 | +defineOption('doxygen.warnignores', []) |
| 19 | + |
| 20 | +class Doxygen(BaseTarget): |
| 21 | + def __init__(self, target, source, config, internal=False, dependencies=None, failOnWarnings=False): |
| 22 | + """ |
| 23 | + Uses the doxygen tool to generate a directory of API documentation for languages such as C/C++. |
| 24 | + |
| 25 | + @param target: The destination directory. |
| 26 | + |
| 27 | + @param source: pathset listing the top-level source files to be documented. |
| 28 | + To avoid classes appearing twice under different namespaces, use "dependencies" not "source" |
| 29 | + for files that are #included by source/documented files where the namespace is provided |
| 30 | + around the #include statement but not in the file itself. |
| 31 | + |
| 32 | + @param config: the doxygen configuration file. |
| 33 | + |
| 34 | + @param internal: True if items marked as internal/private should be included. |
| 35 | + |
| 36 | + @param dependencies: optional pathset for files that are implicitly depended upon |
| 37 | + by the source files to be documented (e.g. due to #include statements) but should not be |
| 38 | + handled as source files in their own right (e.g. if the namespace is provided by the |
| 39 | + file that #includes them). |
| 40 | + |
| 41 | + @param failOnWarnings: makes this target fail if any warnings are logged to stderr |
| 42 | + (the option doxygen.warnignores can be used to ignore expected warnings) |
| 43 | + """ |
| 44 | + |
| 45 | + BaseTarget.__init__(self, target, dependencies=[source, config, dependencies]) |
| 46 | + self.source = PathSet(source) |
| 47 | + self.config = PathSet(config) |
| 48 | + self.internal = internal |
| 49 | + self.failOnWarnings = failOnWarnings |
| 50 | + |
| 51 | + # todo: ought to be setting hashable implicit inputs |
| 52 | + |
| 53 | + def run(self, context): |
| 54 | + configtmpl = self.config.resolve(context) |
| 55 | + assert len(configtmpl)==1, configtmpl |
| 56 | + configtmpl = configtmpl[0] |
| 57 | + |
| 58 | + sources = " ".join(self.source.resolve(context)) |
| 59 | + config = os.path.join(self.workDir, 'doxygen.conf') |
| 60 | + |
| 61 | + replace = { |
| 62 | + 'INPUT' : sources, |
| 63 | + 'OUTPUT_DIR' : self.path, |
| 64 | + 'INTERNAL':'YES' if self.internal else 'NO', |
| 65 | + 'HIDE_INTERNAL':'NO' if self.internal else 'YES', |
| 66 | + } |
| 67 | + |
| 68 | + mkdir(self.workDir) |
| 69 | + with openForWrite(config, 'wb') as outf: |
| 70 | + with open(configtmpl, 'rb') as inf: |
| 71 | + for l in inf: |
| 72 | + for k in replace: |
| 73 | + l = l.replace('@%s@' % k, replace[k]) |
| 74 | + outf.write(l) |
| 75 | + |
| 76 | + exe = context.mergeOptions(self)['doxygen.exepath'] |
| 77 | + if not exe: raise BuildException('doxygen.exepath option must be set') |
| 78 | + args = [ exe, config ] |
| 79 | + |
| 80 | + args = [ context.expandPropertyValues(x) for x in args ] |
| 81 | + #self.log.info('%s doxygen output file is: %s', self, os.path.join(self.workDir, 'doxygen.err')) |
| 82 | + with open(os.path.join(self.workDir, 'doxygen.out'), 'wb') as stdout: |
| 83 | + with open(os.path.join(self.workDir, 'doxygen.err'), 'wb') as stderr: |
| 84 | + subprocess.check_call(args, stdout=stdout, stderr=stderr) |
| 85 | + |
| 86 | + contents = [] |
| 87 | + with open(os.path.join(self.workDir, 'doxygen.out'), 'r') as stdout: |
| 88 | + for l in stdout: |
| 89 | + # Generating docs for compound com::softwareag::connectivity::StatusReporter... |
| 90 | + if l.startswith('Generating docs for '): |
| 91 | + # normalize " nested compound " to " compound " since the sorted list is easier to read that way |
| 92 | + l = l.replace(' nested ', ' ').replace('Generating docs for ','').strip().strip('.') |
| 93 | + contents.append(l) |
| 94 | + if contents: |
| 95 | + with open(os.path.join(self.path, 'html/contents.txt'), 'w') as f: |
| 96 | + for l in sorted(contents): |
| 97 | + print >>f, l |
| 98 | + with open(os.path.join(self.workDir, 'doxygen.err'), 'r') as stderr: |
| 99 | + errs = [] |
| 100 | + for l in stderr: |
| 101 | + if not l.strip(): continue |
| 102 | + if l.startswith(' ') and errs: |
| 103 | + errs[-1] += ' - '+l.strip() |
| 104 | + else: |
| 105 | + errs.append(l.strip()) |
| 106 | + ignores = context.mergeOptions(self)['doxygen.warnignores'] |
| 107 | + errs = [e for e in errs if not any([re.match(i, e) for i in ignores])] |
| 108 | + if errs: |
| 109 | + if self.failOnWarnings: |
| 110 | + self.log.error('Doxygen reported %d lines of stderr warnings for %s; see %s: \n%s', len(errs), self, os.path.join(self.workDir, 'doxygen.err'), '\n '.join(errs)) |
| 111 | + raise BuildException('Doxygen reported %d lines of stderr warnings (see %s), first is: %s'%(len(errs), os.path.join(self.workDir, 'doxygen.err'), errs[0].strip())) |
| 112 | + else: |
| 113 | + self.log.info('Doxygen reported %d lines of stderr warnings for %s; see %s', len(errs), self, os.path.join(self.workDir, 'doxygen.err')) |
| 114 | + |
| 115 | + |
0 commit comments