2015-07-21 23:01:52 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Syntax: mkdoc.py [-I<path> ..] [.. a list of header files ..]
|
|
|
|
#
|
|
|
|
# Extract documentation from C++ header files to use it in Python bindings
|
|
|
|
#
|
|
|
|
|
2016-04-26 22:35:03 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import platform
|
|
|
|
import re
|
|
|
|
import textwrap
|
|
|
|
|
2015-07-21 23:01:52 +00:00
|
|
|
from clang import cindex
|
|
|
|
from clang.cindex import CursorKind
|
|
|
|
from collections import OrderedDict
|
2015-07-23 12:43:34 +00:00
|
|
|
from threading import Thread, Semaphore
|
|
|
|
from multiprocessing import cpu_count
|
2015-07-21 23:01:52 +00:00
|
|
|
|
|
|
|
RECURSE_LIST = [
|
|
|
|
CursorKind.TRANSLATION_UNIT,
|
|
|
|
CursorKind.NAMESPACE,
|
|
|
|
CursorKind.CLASS_DECL,
|
|
|
|
CursorKind.STRUCT_DECL,
|
2016-05-20 14:37:13 +00:00
|
|
|
CursorKind.ENUM_DECL,
|
2015-07-21 23:01:52 +00:00
|
|
|
CursorKind.CLASS_TEMPLATE
|
|
|
|
]
|
|
|
|
|
|
|
|
PRINT_LIST = [
|
|
|
|
CursorKind.CLASS_DECL,
|
|
|
|
CursorKind.STRUCT_DECL,
|
2016-05-01 00:32:38 +00:00
|
|
|
CursorKind.ENUM_DECL,
|
2016-05-20 14:37:13 +00:00
|
|
|
CursorKind.ENUM_CONSTANT_DECL,
|
2015-07-21 23:01:52 +00:00
|
|
|
CursorKind.CLASS_TEMPLATE,
|
|
|
|
CursorKind.FUNCTION_DECL,
|
|
|
|
CursorKind.FUNCTION_TEMPLATE,
|
2016-05-01 20:36:09 +00:00
|
|
|
CursorKind.CONVERSION_FUNCTION,
|
2015-07-21 23:01:52 +00:00
|
|
|
CursorKind.CXX_METHOD,
|
|
|
|
CursorKind.CONSTRUCTOR,
|
|
|
|
CursorKind.FIELD_DECL
|
|
|
|
]
|
|
|
|
|
|
|
|
CPP_OPERATORS = {
|
2016-04-26 22:35:03 +00:00
|
|
|
'<=': 'le', '>=': 'ge', '==': 'eq', '!=': 'ne', '[]': 'array',
|
|
|
|
'+=': 'iadd', '-=': 'isub', '*=': 'imul', '/=': 'idiv', '%=':
|
|
|
|
'imod', '&=': 'iand', '|=': 'ior', '^=': 'ixor', '<<=': 'ilshift',
|
|
|
|
'>>=': 'irshift', '++': 'inc', '--': 'dec', '<<': 'lshift', '>>':
|
|
|
|
'rshift', '&&': 'land', '||': 'lor', '!': 'lnot', '~': 'bnot',
|
|
|
|
'&': 'band', '|': 'bor', '+': 'add', '-': 'sub', '*': 'mul', '/':
|
2016-05-20 14:37:13 +00:00
|
|
|
'div', '%': 'mod', '<': 'lt', '>': 'gt', '=': 'assign', '()': 'call'
|
2015-07-21 23:01:52 +00:00
|
|
|
}
|
2016-04-26 22:35:03 +00:00
|
|
|
|
|
|
|
CPP_OPERATORS = OrderedDict(
|
|
|
|
sorted(CPP_OPERATORS.items(), key=lambda t: -len(t[0])))
|
2015-07-21 23:01:52 +00:00
|
|
|
|
2015-07-23 12:43:34 +00:00
|
|
|
job_count = cpu_count()
|
|
|
|
job_semaphore = Semaphore(job_count)
|
|
|
|
|
2015-07-21 23:01:52 +00:00
|
|
|
registered_names = dict()
|
|
|
|
|
2016-04-26 22:35:03 +00:00
|
|
|
|
2015-07-21 23:01:52 +00:00
|
|
|
def d(s):
|
|
|
|
return s.decode('utf8')
|
|
|
|
|
2016-04-26 22:35:03 +00:00
|
|
|
|
2015-07-21 23:01:52 +00:00
|
|
|
def sanitize_name(name):
|
|
|
|
global registered_names
|
2016-05-01 20:36:09 +00:00
|
|
|
name = re.sub(r'type-parameter-0-([0-9]+)', r'T\1', name)
|
2015-07-21 23:01:52 +00:00
|
|
|
for k, v in CPP_OPERATORS.items():
|
|
|
|
name = name.replace('operator%s' % k, 'operator_%s' % v)
|
2016-04-26 22:35:03 +00:00
|
|
|
name = re.sub('<.*>', '', name)
|
|
|
|
name = ''.join([ch if ch.isalnum() else '_' for ch in name])
|
|
|
|
name = re.sub('_$', '', re.sub('_+', '_', name))
|
2015-07-21 23:01:52 +00:00
|
|
|
if name in registered_names:
|
|
|
|
registered_names[name] += 1
|
|
|
|
name += '_' + str(registered_names[name])
|
|
|
|
else:
|
|
|
|
registered_names[name] = 1
|
|
|
|
return '__doc_' + name
|
|
|
|
|
2016-04-26 22:35:03 +00:00
|
|
|
|
2015-07-21 23:01:52 +00:00
|
|
|
def process_comment(comment):
|
|
|
|
result = ''
|
|
|
|
|
|
|
|
# Remove C++ comment syntax
|
2016-05-20 14:37:13 +00:00
|
|
|
leading_spaces = float('inf')
|
|
|
|
for s in comment.expandtabs(tabsize=4).splitlines():
|
2015-07-21 23:01:52 +00:00
|
|
|
s = s.strip()
|
|
|
|
if s.startswith('/*'):
|
2016-05-20 14:37:13 +00:00
|
|
|
s = s[2:].lstrip('*')
|
2015-07-21 23:01:52 +00:00
|
|
|
elif s.endswith('*/'):
|
2016-05-20 14:37:13 +00:00
|
|
|
s = s[:-2].rstrip('*')
|
2015-07-21 23:01:52 +00:00
|
|
|
elif s.startswith('///'):
|
|
|
|
s = s[3:]
|
|
|
|
if s.startswith('*'):
|
|
|
|
s = s[1:]
|
2016-05-20 14:37:13 +00:00
|
|
|
if len(s) > 0:
|
|
|
|
leading_spaces = min(leading_spaces, len(s) - len(s.lstrip()))
|
|
|
|
result += s + '\n'
|
|
|
|
|
|
|
|
if leading_spaces != float('inf'):
|
|
|
|
result2 = ""
|
|
|
|
for s in result.splitlines():
|
|
|
|
result2 += s[leading_spaces:] + '\n'
|
|
|
|
result = result2
|
2015-07-21 23:01:52 +00:00
|
|
|
|
|
|
|
# Doxygen tags
|
|
|
|
cpp_group = '([\w:]+)'
|
|
|
|
param_group = '([\[\w:\]]+)'
|
|
|
|
|
|
|
|
s = result
|
|
|
|
s = re.sub(r'\\c\s+%s' % cpp_group, r'``\1``', s)
|
|
|
|
s = re.sub(r'\\a\s+%s' % cpp_group, r'*\1*', s)
|
|
|
|
s = re.sub(r'\\e\s+%s' % cpp_group, r'*\1*', s)
|
|
|
|
s = re.sub(r'\\em\s+%s' % cpp_group, r'*\1*', s)
|
|
|
|
s = re.sub(r'\\b\s+%s' % cpp_group, r'**\1**', s)
|
2016-04-30 21:36:57 +00:00
|
|
|
s = re.sub(r'\\ingroup\s+%s' % cpp_group, r'', s)
|
2016-04-26 22:35:03 +00:00
|
|
|
s = re.sub(r'\\param%s?\s+%s' % (param_group, cpp_group),
|
|
|
|
r'\n\n$Parameter ``\2``:\n\n', s)
|
2016-05-20 14:37:13 +00:00
|
|
|
s = re.sub(r'\\tparam%s?\s+%s' % (param_group, cpp_group),
|
|
|
|
r'\n\n$Template parameter ``\2``:\n\n', s)
|
2015-07-21 23:01:52 +00:00
|
|
|
|
|
|
|
for in_, out_ in {
|
2016-04-26 22:35:03 +00:00
|
|
|
'return': 'Returns',
|
|
|
|
'author': 'Author',
|
|
|
|
'authors': 'Authors',
|
|
|
|
'copyright': 'Copyright',
|
|
|
|
'date': 'Date',
|
|
|
|
'remark': 'Remark',
|
|
|
|
'sa': 'See also',
|
|
|
|
'see': 'See also',
|
|
|
|
'extends': 'Extends',
|
|
|
|
'throw': 'Throws',
|
|
|
|
'throws': 'Throws'
|
|
|
|
}.items():
|
2015-07-21 23:01:52 +00:00
|
|
|
s = re.sub(r'\\%s\s*' % in_, r'\n\n$%s:\n\n' % out_, s)
|
|
|
|
|
|
|
|
s = re.sub(r'\\details\s*', r'\n\n', s)
|
|
|
|
s = re.sub(r'\\brief\s*', r'', s)
|
|
|
|
s = re.sub(r'\\short\s*', r'', s)
|
|
|
|
s = re.sub(r'\\ref\s*', r'', s)
|
|
|
|
|
2016-05-20 14:37:13 +00:00
|
|
|
s = re.sub(r'\\code\s?(.*?)\s?\\endcode',
|
|
|
|
r"```\n\1\n```\n", s, flags=re.DOTALL)
|
|
|
|
|
2015-07-23 12:43:34 +00:00
|
|
|
# HTML/TeX tags
|
2016-05-20 14:37:13 +00:00
|
|
|
s = re.sub(r'<tt>(.*?)</tt>', r'``\1``', s, flags=re.DOTALL)
|
|
|
|
s = re.sub(r'<pre>(.*?)</pre>', r"```\n\1\n```\n", s, flags=re.DOTALL)
|
|
|
|
s = re.sub(r'<em>(.*?)</em>', r'*\1*', s, flags=re.DOTALL)
|
|
|
|
s = re.sub(r'<b>(.*?)</b>', r'**\1**', s, flags=re.DOTALL)
|
|
|
|
s = re.sub(r'\\f\$(.*?)\\f\$', r'$\1$', s, flags=re.DOTALL)
|
|
|
|
s = re.sub(r'<li>', r'\n\n* ', s)
|
|
|
|
s = re.sub(r'</?ul>', r'', s)
|
|
|
|
s = re.sub(r'</li>', r'\n\n', s)
|
2015-07-21 23:01:52 +00:00
|
|
|
|
|
|
|
s = s.replace('``true``', '``True``')
|
|
|
|
s = s.replace('``false``', '``False``')
|
|
|
|
|
|
|
|
# Re-flow text
|
|
|
|
wrapper = textwrap.TextWrapper()
|
|
|
|
wrapper.expand_tabs = True
|
|
|
|
wrapper.replace_whitespace = True
|
2016-05-20 14:37:13 +00:00
|
|
|
wrapper.drop_whitespace = True
|
|
|
|
wrapper.width = 70
|
2015-07-21 23:01:52 +00:00
|
|
|
wrapper.initial_indent = wrapper.subsequent_indent = ''
|
|
|
|
|
|
|
|
result = ''
|
2016-05-20 14:37:13 +00:00
|
|
|
in_code_segment = False
|
|
|
|
for x in re.split(r'(```)', s):
|
|
|
|
if x == '```':
|
|
|
|
if not in_code_segment:
|
|
|
|
result += '```\n'
|
|
|
|
else:
|
|
|
|
result += '\n```\n\n'
|
|
|
|
in_code_segment = not in_code_segment
|
|
|
|
elif in_code_segment:
|
|
|
|
result += x.strip()
|
2015-07-21 23:01:52 +00:00
|
|
|
else:
|
2016-05-20 14:37:13 +00:00
|
|
|
for y in re.split(r'(?: *\n *){2,}', x):
|
|
|
|
wrapped = wrapper.fill(re.sub(r'\s+', ' ', y).strip())
|
|
|
|
if len(wrapped) > 0 and wrapped[0] == '$':
|
|
|
|
result += wrapped[1:] + '\n'
|
|
|
|
wrapper.initial_indent = \
|
|
|
|
wrapper.subsequent_indent = ' ' * 4
|
|
|
|
else:
|
|
|
|
if len(wrapped) > 0:
|
|
|
|
result += wrapped + '\n\n'
|
|
|
|
wrapper.initial_indent = wrapper.subsequent_indent = ''
|
2016-05-01 20:36:09 +00:00
|
|
|
return result.rstrip().lstrip('\n')
|
2015-07-21 23:01:52 +00:00
|
|
|
|
|
|
|
|
2015-07-23 12:43:34 +00:00
|
|
|
def extract(filename, node, prefix, output):
|
2015-07-21 23:01:52 +00:00
|
|
|
num_extracted = 0
|
2016-04-26 22:35:03 +00:00
|
|
|
if not (node.location.file is None or
|
|
|
|
os.path.samefile(d(node.location.file.name), filename)):
|
2015-07-21 23:01:52 +00:00
|
|
|
return 0
|
|
|
|
if node.kind in RECURSE_LIST:
|
|
|
|
sub_prefix = prefix
|
|
|
|
if node.kind != CursorKind.TRANSLATION_UNIT:
|
|
|
|
if len(sub_prefix) > 0:
|
|
|
|
sub_prefix += '_'
|
|
|
|
sub_prefix += d(node.spelling)
|
|
|
|
for i in node.get_children():
|
2015-07-23 12:43:34 +00:00
|
|
|
num_extracted += extract(filename, i, sub_prefix, output)
|
2015-07-21 23:01:52 +00:00
|
|
|
if num_extracted == 0:
|
|
|
|
return 0
|
|
|
|
if node.kind in PRINT_LIST:
|
|
|
|
comment = d(node.raw_comment) if node.raw_comment is not None else ''
|
|
|
|
comment = process_comment(comment)
|
2016-04-25 22:12:22 +00:00
|
|
|
sub_prefix = prefix
|
|
|
|
if len(sub_prefix) > 0:
|
|
|
|
sub_prefix += '_'
|
2016-05-20 14:37:13 +00:00
|
|
|
if len(node.spelling) > 0:
|
|
|
|
name = sanitize_name(sub_prefix + d(node.spelling))
|
|
|
|
output.append('\nstatic const char *%s =%sR"doc(%s)doc";' %
|
|
|
|
(name, '\n' if '\n' in comment else ' ', comment))
|
|
|
|
num_extracted += 1
|
2015-07-21 23:01:52 +00:00
|
|
|
return num_extracted
|
|
|
|
|
2016-04-26 22:35:03 +00:00
|
|
|
|
2015-07-23 12:43:34 +00:00
|
|
|
class ExtractionThread(Thread):
|
2016-04-26 22:35:03 +00:00
|
|
|
def __init__(self, filename, parameters, output):
|
2015-07-23 12:43:34 +00:00
|
|
|
Thread.__init__(self)
|
|
|
|
self.filename = filename
|
|
|
|
self.parameters = parameters
|
|
|
|
self.output = output
|
|
|
|
job_semaphore.acquire()
|
|
|
|
|
|
|
|
def run(self):
|
2016-04-26 22:35:03 +00:00
|
|
|
print('Processing "%s" ..' % self.filename, file=sys.stderr)
|
2015-07-23 12:43:34 +00:00
|
|
|
try:
|
2016-04-26 22:35:03 +00:00
|
|
|
index = cindex.Index(
|
|
|
|
cindex.conf.lib.clang_createIndex(False, True))
|
2015-07-23 12:43:34 +00:00
|
|
|
tu = index.parse(self.filename, self.parameters)
|
|
|
|
extract(self.filename, tu.cursor, '', self.output)
|
|
|
|
finally:
|
|
|
|
job_semaphore.release()
|
|
|
|
|
2015-07-21 23:01:52 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parameters = ['-x', 'c++', '-std=c++11']
|
|
|
|
filenames = []
|
|
|
|
|
2016-04-19 11:33:21 +00:00
|
|
|
if platform.system() == 'Darwin':
|
2016-04-26 22:35:03 +00:00
|
|
|
dev_path = '/Applications/Xcode.app/Contents/Developer/'
|
|
|
|
lib_dir = dev_path + 'Toolchains/XcodeDefault.xctoolchain/usr/lib/'
|
|
|
|
sdk_dir = dev_path + 'Platforms/MacOSX.platform/Developer/SDKs'
|
|
|
|
libclang = lib_dir + 'libclang.dylib'
|
|
|
|
|
2016-04-19 11:33:21 +00:00
|
|
|
if os.path.exists(libclang):
|
|
|
|
cindex.Config.set_library_path(os.path.dirname(libclang))
|
|
|
|
|
2016-04-26 22:35:03 +00:00
|
|
|
if os.path.exists(sdk_dir):
|
|
|
|
sysroot_dir = os.path.join(sdk_dir, next(os.walk(sdk_dir))[1][0])
|
2016-04-19 11:33:21 +00:00
|
|
|
parameters.append('-isysroot')
|
2016-04-26 22:35:03 +00:00
|
|
|
parameters.append(sysroot_dir)
|
2016-04-19 11:33:21 +00:00
|
|
|
|
2015-07-21 23:01:52 +00:00
|
|
|
for item in sys.argv[1:]:
|
|
|
|
if item.startswith('-'):
|
|
|
|
parameters.append(item)
|
|
|
|
else:
|
|
|
|
filenames.append(item)
|
|
|
|
|
|
|
|
if len(filenames) == 0:
|
|
|
|
print('Syntax: %s [.. a list of header files ..]' % sys.argv[0])
|
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
print('''/*
|
|
|
|
This file contains docstrings for the Python bindings.
|
|
|
|
Do not edit! These were automatically extracted by mkdoc.py
|
|
|
|
*/
|
|
|
|
|
2016-04-30 21:55:10 +00:00
|
|
|
#define __EXPAND(x) x
|
|
|
|
#define __COUNT(_1, _2, _3, _4, _5, _6, _7, COUNT, ...) COUNT
|
|
|
|
#define __VA_SIZE(...) __EXPAND(__COUNT(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1))
|
|
|
|
#define __CAT1(a, b) a ## b
|
|
|
|
#define __CAT2(a, b) __CAT1(a, b)
|
|
|
|
#define __DOC1(n1) __doc_##n1
|
|
|
|
#define __DOC2(n1, n2) __doc_##n1##_##n2
|
|
|
|
#define __DOC3(n1, n2, n3) __doc_##n1##_##n2##_##n3
|
|
|
|
#define __DOC4(n1, n2, n3, n4) __doc_##n1##_##n2##_##n3##_##n4
|
|
|
|
#define __DOC5(n1, n2, n3, n4, n5) __doc_##n1##_##n2##_##n3##_##n4##_##n5
|
|
|
|
#define __DOC6(n1, n2, n3, n4, n5, n6) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6
|
|
|
|
#define __DOC7(n1, n2, n3, n4, n5, n6, n7) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6##_##n7
|
|
|
|
#define DOC(...) __EXPAND(__EXPAND(__CAT2(__DOC, __VA_SIZE(__VA_ARGS__)))(__VA_ARGS__))
|
2015-07-30 13:29:00 +00:00
|
|
|
|
|
|
|
#if defined(__GNUG__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-variable"
|
|
|
|
#endif
|
|
|
|
''')
|
2015-07-23 12:43:34 +00:00
|
|
|
|
|
|
|
output = []
|
2015-07-21 23:01:52 +00:00
|
|
|
for filename in filenames:
|
2015-07-23 12:43:34 +00:00
|
|
|
thr = ExtractionThread(filename, parameters, output)
|
|
|
|
thr.start()
|
|
|
|
|
2016-04-26 22:35:03 +00:00
|
|
|
print('Waiting for jobs to finish ..', file=sys.stderr)
|
2015-07-23 12:43:34 +00:00
|
|
|
for i in range(job_count):
|
|
|
|
job_semaphore.acquire()
|
|
|
|
|
|
|
|
output.sort()
|
|
|
|
for l in output:
|
|
|
|
print(l)
|
2015-07-30 13:29:00 +00:00
|
|
|
|
|
|
|
print('''
|
|
|
|
#if defined(__GNUG__)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
''')
|