generate_bindings.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #!/usr/bin/env python3
  2. #
  3. # Syntax: generate_docstrings.py <path_to_c++_header_files> <path_to_python_files>
  4. #
  5. # Extract documentation from C++ header files to use it in libiglPython bindings
  6. #
  7. import os, sys, glob
  8. import pickle
  9. import shutil
  10. from joblib import Parallel, delayed
  11. from multiprocessing import cpu_count
  12. from mako.template import Template
  13. from parser import parse
  14. # http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python
  15. def get_filepaths(directory):
  16. """
  17. This function will generate the file names in a directory
  18. tree by walking the tree either top-down or bottom-up. For each
  19. directory in the tree rooted at directory top (including top itself),
  20. it yields a 3-tuple (dirpath, dirnames, filenames).
  21. """
  22. file_paths = [] # List which will store all of the full filepaths.
  23. # Walk the tree.
  24. for root, directories, files in os.walk(directory):
  25. for filename in files:
  26. # Join the two strings in order to form the full filepath.
  27. filepath = os.path.join(root, filename)
  28. file_paths.append(filepath) # Add it to the list.
  29. return file_paths # Self-explanatory.
  30. def get_name_from_path(path, basepath, prefix, postfix):
  31. f_clean = path[len(basepath):]
  32. f_clean = f_clean.replace(basepath, "")
  33. f_clean = f_clean.replace(postfix, "")
  34. f_clean = f_clean.replace(prefix, "")
  35. f_clean = f_clean.replace("/", "_")
  36. f_clean = f_clean.replace("\\", "_")
  37. f_clean = f_clean.replace(" ", "_")
  38. f_clean = f_clean.replace(".", "_")
  39. return f_clean
  40. def map_parameter_types(name, cpp_type, parsed_types, errors, enum_types):
  41. # TODO Replace with proper regex matching and derive types from templates, comment parsing, names in cpp files
  42. # CAUTION: This is work in progress mapping code to get a grip of the problem
  43. # Types to map
  44. # const int dim -> const int& dim ?
  45. result = []
  46. if cpp_type.startswith("const"):
  47. result.append("const ")
  48. cpp_type = cpp_type[6:] # Strip const part
  49. # Handle special types
  50. skip_parsing = False
  51. if cpp_type.startswith("MatY"):
  52. result.append("Eigen::SparseMatrix<double>&")
  53. skip_parsing = True
  54. if cpp_type == "std::vector<std::vector<Scalar> > &":
  55. result.append("std::vector<std::vector<double> > &")
  56. skip_parsing = True
  57. if cpp_type == "std::vector<std::vector<Index> > &":
  58. result.append("std::vector<std::vector<int> > &")
  59. skip_parsing = True
  60. for constant in enum_types:
  61. if cpp_type.endswith(constant):
  62. result.append(cpp_type)
  63. skip_parsing = True
  64. if len(parsed_types) == 0:
  65. errors.append("Empty typechain: %s" % cpp_type)
  66. if cpp_type == "int" or cpp_type == "bool":
  67. return cpp_type, True
  68. else:
  69. return cpp_type, False
  70. # print(parsed_types, cpp_type)
  71. if not skip_parsing:
  72. for i, t in enumerate(parsed_types):
  73. if t == "Eigen":
  74. result.append("Eigen::")
  75. continue
  76. if t == "std":
  77. result.append("std::")
  78. continue
  79. if t == "PlainObjectBase" or t == "MatrixBase":
  80. if name == "F":
  81. result.append("MatrixXi&")
  82. elif name == "V":
  83. result.append("MatrixXd&")
  84. else:
  85. result.append("MatrixXd&")
  86. break
  87. if t == "MatrixXi" or t == "VectorXi":
  88. result.append("MatrixXi&")
  89. break
  90. if t == "MatrixXd" or t == "VectorXd":
  91. result.append("MatrixXd&")
  92. break
  93. if t == "SparseMatrix" and len(parsed_types) >= i + 2 and (
  94. parsed_types[i + 1] == "Scalar" or parsed_types[i + 1] == "T"):
  95. result.append("SparseMatrix<double>&")
  96. break
  97. if t == "SparseVector" and len(parsed_types) >= i + 2 and (parsed_types[i + 1] == "Scalar" or parsed_types[
  98. i + 1] == "T"):
  99. result.append("SparseMatrix<double>&")
  100. break
  101. if t == "bool" or t == "int" or t == "double" or t == "unsigned" or t == "string":
  102. if cpp_type.endswith("&"):
  103. result.append(t + " &")
  104. else:
  105. result.append(t)
  106. break
  107. else:
  108. errors.append("Unknown typechain: %s" % cpp_type)
  109. return cpp_type, False
  110. return "".join(result), True
  111. if __name__ == '__main__':
  112. if len(sys.argv) != 2:
  113. print('Syntax: %s <path_to_c++_files>' % sys.argv[0])
  114. exit(-1)
  115. errors = {"missing": [], "empty": [], "others": [], "incorrect": [], "render": [], "various": []}
  116. files = {"complete": [], "partial": [], "errors": [], "others": [], "empty": []}
  117. # List all files in the given folder and subfolders
  118. cpp_base_path = sys.argv[1]
  119. cpp_file_paths = get_filepaths(cpp_base_path)
  120. # Add all the .h filepaths to a dict
  121. print("Collecting cpp files for parsing...")
  122. mapping = {}
  123. cppmapping = {}
  124. for f in cpp_file_paths:
  125. if f.endswith(".h"):
  126. name = get_name_from_path(f, cpp_base_path, "", ".h")
  127. mapping[name] = f
  128. if f.endswith(".cpp"):
  129. name = get_name_from_path(f, cpp_base_path, "", ".cpp")
  130. cppmapping[name] = f
  131. # Add all python binding files to a list
  132. implemented_names = list(mapping.keys()) # ["point_mesh_squared_distance"]
  133. implemented_names.sort()
  134. single_postfix = ""
  135. single_prefix = ""
  136. # Create a list of all cpp header files
  137. files_to_parse = []
  138. cppfiles_to_parse = []
  139. for n in implemented_names:
  140. files_to_parse.append(mapping[n])
  141. if n not in cppmapping:
  142. errors["missing"].append("No cpp source file for function %s found." % n)
  143. else:
  144. cppfiles_to_parse.append(cppmapping[n])
  145. # Parse c++ header files
  146. print("Parsing header files...")
  147. load_headers = False
  148. if load_headers:
  149. with open("headers.dat", 'rb') as fs:
  150. dicts = pickle.load(fs)
  151. else:
  152. job_count = cpu_count()
  153. dicts = Parallel(n_jobs=job_count)(delayed(parse)(path) for path in files_to_parse)
  154. if not load_headers:
  155. print("Saving parsed header files...")
  156. with open("headers.dat", 'wb') as fs:
  157. pickle.dump(dicts, fs)
  158. # Not yet needed, as explicit template parsing does not seem to be supported in clang
  159. # Parse c++ source files
  160. # cppdicts = Parallel(n_jobs=job_count)(delayed(parse)(path) for path in cppfiles_to_parse)
  161. # Change directory to become independent of execution directory
  162. print("Generating directory tree for binding files...")
  163. path = os.path.dirname(__file__)
  164. if path != "":
  165. os.chdir(path)
  166. try:
  167. shutil.rmtree("generated")
  168. except:
  169. pass # Ignore missing generated directory
  170. os.makedirs("generated/complete")
  171. os.mkdir("generated/partial")
  172. print("Generating and writing binding files...")
  173. for idx, n in enumerate(implemented_names):
  174. d = dicts[idx]
  175. contained_elements = sum(map(lambda x: len(x), d.values()))
  176. # Skip files that don't contain functions/enums/classes
  177. if contained_elements == 0:
  178. errors["empty"].append("Function %s contains no parseable content in cpp header. Something might be wrong." % n)
  179. files["empty"].append(n)
  180. continue
  181. # Add functions with classes to others
  182. if len(d["classes"]) != 0 or len(d["structs"]) != 0:
  183. errors["others"].append("Function %s contains classes/structs in cpp header. Skipping" % n)
  184. files["others"].append(n)
  185. continue
  186. # Work on files that contain only functions/enums and namespaces
  187. if len(d["functions"]) + len(d["namespaces"]) + len(d["enums"]) == contained_elements:
  188. correct_functions = []
  189. incorrect_functions = []
  190. # Collect enums to generate binding files
  191. enums = []
  192. enum_types = []
  193. for e in d["enums"]:
  194. enums.append({"name": e.name, "namespaces": d["namespaces"], "constants": e.constants})
  195. enum_types.append(e.name)
  196. # Collect functions to generate binding files
  197. for f in d["functions"]:
  198. parameters = []
  199. correct_function = True
  200. f_errors = []
  201. for p in f.parameters:
  202. typ, correct = map_parameter_types(p[0], p[1], p[2], f_errors, enum_types)
  203. correct_function &= correct
  204. parameters.append({"name": p[0], "type": typ})
  205. if correct_function and len(parameters) > 0: #TODO add constants like EPS
  206. correct_functions.append({"parameters": parameters, "namespaces": d["namespaces"], "name": f.name})
  207. elif len(parameters) > 0:
  208. incorrect_functions.append({"parameters": parameters, "namespaces": d["namespaces"], "name": f.name})
  209. errors["incorrect"].append("Incorrect function in %s: %s, %s\n" % (n, f.name, ",".join(f_errors)))
  210. else:
  211. errors["various"].append("Function without pars in %s: %s, %s\n" % (n, f.name, ","
  212. "".join(f_errors)))
  213. # Write binding files
  214. try:
  215. tpl = Template(filename='basic_function.mako')
  216. rendered = tpl.render(functions=correct_functions, enums=enums)
  217. tpl1 = Template(filename='basic_function.mako')
  218. rendered1 = tpl.render(functions=incorrect_functions, enums=enums)
  219. path = "generated/"
  220. if len(incorrect_functions) == 0 and (len(correct_functions) != 0 or len(enums) != 0):
  221. path += "complete/"
  222. with open(path + single_prefix + "py_" + n + ".cpp", 'w') as fs:
  223. fs.write(rendered)
  224. files["complete"].append(n)
  225. else:
  226. path += "partial/"
  227. with open(path + single_prefix + "py_" + n + ".cpp", 'w') as fs:
  228. fs.write("// COMPLETE BINDINGS ========================\n")
  229. fs.write(rendered)
  230. fs.write("\n\n\n\n// INCOMPLETE BINDINGS ========================\n")
  231. fs.write(rendered1)
  232. if len(correct_functions) != 0:
  233. files["partial"].append(n)
  234. else:
  235. files["errors"].append(n)
  236. except Exception as e:
  237. files["errors"].append(n)
  238. errors["render"].append("Template rendering failed:" + n + " " + str(correct_functions) + ", incorrect "
  239. "functions are " + str(
  240. incorrect_functions) + str(e) + "\n")
  241. print("Writing error and overview files...")
  242. with open("errors.txt" + single_postfix, 'w') as fs:
  243. l = list(errors.keys())
  244. l.sort()
  245. for k in l:
  246. fs.write("%s: %i \n" %(k, len(errors[k])))
  247. fs.writelines("\n".join(errors[k]))
  248. fs.write("\n\n\n")
  249. with open("files.txt" + single_postfix, 'w') as fs:
  250. l = list(files.keys())
  251. l.sort()
  252. for k in l:
  253. fs.write("%s: %i \n" %(k, len(files[k])))
  254. fs.writelines("\n".join(files[k]))
  255. fs.write("\n\n\n")