generate_bindings.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.startswith("Eigen::Matrix<unsigned char, Eigen::Dynamic, Eigen::Dynamic>"):
  55. result.append("Eigen::Matrix<unsigned char, Eigen::Dynamic, Eigen::Dynamic>")
  56. skip_parsing = True
  57. if cpp_type == "std::vector<std::vector<Scalar> > &":
  58. result.append("std::vector<std::vector<double> > &")
  59. skip_parsing = True
  60. if cpp_type == "std::vector<std::vector<Index> > &":
  61. result.append("std::vector<std::vector<int> > &")
  62. skip_parsing = True
  63. for constant in enum_types:
  64. if cpp_type.endswith(constant):
  65. result.append(cpp_type)
  66. skip_parsing = True
  67. if len(parsed_types) == 0:
  68. errors.append("Empty typechain: %s" % cpp_type)
  69. if cpp_type == "int" or cpp_type == "bool" or cpp_type == "unsigned int":
  70. return cpp_type, True
  71. else:
  72. return cpp_type, False
  73. # print(parsed_types, cpp_type)
  74. if not skip_parsing:
  75. for i, t in enumerate(parsed_types):
  76. if t == "Eigen":
  77. result.append("Eigen::")
  78. continue
  79. if t == "std":
  80. result.append("std::")
  81. continue
  82. if t == "PlainObjectBase" or t == "MatrixBase":
  83. if name == "F":
  84. result.append("MatrixXi&")
  85. elif name == "V":
  86. result.append("MatrixXd&")
  87. else:
  88. result.append("MatrixXd&")
  89. break
  90. if t == "MatrixXi" or t == "VectorXi":
  91. result.append("MatrixXi&")
  92. break
  93. if t == "MatrixXd" or t == "VectorXd":
  94. result.append("MatrixXd&")
  95. break
  96. if t == "SparseMatrix" and len(parsed_types) >= i + 2 and (
  97. parsed_types[i + 1] == "Scalar" or parsed_types[i + 1] == "T"):
  98. result.append("SparseMatrix<double>&")
  99. break
  100. if t == "SparseVector" and len(parsed_types) >= i + 2 and (parsed_types[i + 1] == "Scalar" or parsed_types[
  101. i + 1] == "T"):
  102. result.append("SparseMatrix<double>&")
  103. break
  104. if t == "bool" or t == "int" or t == "double" or t == "unsigned" or t == "string":
  105. if cpp_type.endswith("&"):
  106. result.append(t + " &")
  107. else:
  108. result.append(t)
  109. break
  110. else:
  111. errors.append("Unknown typechain: %s" % cpp_type)
  112. return cpp_type, False
  113. return "".join(result), True
  114. if __name__ == '__main__':
  115. if len(sys.argv) != 2:
  116. print('Syntax: %s <path_to_c++_files>' % sys.argv[0])
  117. exit(-1)
  118. errors = {"missing": [], "empty": [], "others": [], "incorrect": [], "render": [], "various": []}
  119. files = {"complete": [], "partial": [], "errors": [], "others": [], "empty": []}
  120. # List all files in the given folder and subfolders
  121. cpp_base_path = sys.argv[1]
  122. cpp_file_paths = get_filepaths(cpp_base_path)
  123. # Add all the .h filepaths to a dict
  124. print("Collecting cpp files for parsing...")
  125. mapping = {}
  126. cppmapping = {}
  127. for f in cpp_file_paths:
  128. if f.endswith(".h"):
  129. name = get_name_from_path(f, cpp_base_path, "", ".h")
  130. mapping[name] = f
  131. if f.endswith(".cpp"):
  132. name = get_name_from_path(f, cpp_base_path, "", ".cpp")
  133. cppmapping[name] = f
  134. # Add all python binding files to a list
  135. implemented_names = list(mapping.keys()) # ["point_mesh_squared_distance"]
  136. implemented_names.sort()
  137. single_postfix = ""
  138. single_prefix = ""
  139. # Create a list of all cpp header files
  140. files_to_parse = []
  141. cppfiles_to_parse = []
  142. for n in implemented_names:
  143. files_to_parse.append(mapping[n])
  144. if n not in cppmapping:
  145. errors["missing"].append("No cpp source file for function %s found." % n)
  146. else:
  147. cppfiles_to_parse.append(cppmapping[n])
  148. # Parse c++ header files
  149. print("Parsing header files...")
  150. load_headers = False
  151. if load_headers:
  152. with open("headers.dat", 'rb') as fs:
  153. dicts = pickle.load(fs)
  154. else:
  155. job_count = cpu_count()
  156. dicts = Parallel(n_jobs=job_count)(delayed(parse)(path) for path in files_to_parse)
  157. if not load_headers:
  158. print("Saving parsed header files...")
  159. with open("headers.dat", 'wb') as fs:
  160. pickle.dump(dicts, fs)
  161. # Not yet needed, as explicit template parsing does not seem to be supported in clang
  162. # Parse c++ source files
  163. # cppdicts = Parallel(n_jobs=job_count)(delayed(parse)(path) for path in cppfiles_to_parse)
  164. # Change directory to become independent of execution directory
  165. print("Generating directory tree for binding files...")
  166. path = os.path.dirname(__file__)
  167. if path != "":
  168. os.chdir(path)
  169. try:
  170. shutil.rmtree("generated")
  171. except:
  172. pass # Ignore missing generated directory
  173. os.makedirs("generated/complete")
  174. os.mkdir("generated/partial")
  175. print("Generating and writing binding files...")
  176. for idx, n in enumerate(implemented_names):
  177. d = dicts[idx]
  178. contained_elements = sum(map(lambda x: len(x), d.values()))
  179. # Skip files that don't contain functions/enums/classes
  180. if contained_elements == 0:
  181. errors["empty"].append("Function %s contains no parseable content in cpp header. Something might be wrong." % n)
  182. files["empty"].append(n)
  183. continue
  184. # Add functions with classes to others
  185. if len(d["classes"]) != 0 or len(d["structs"]) != 0:
  186. errors["others"].append("Function %s contains classes/structs in cpp header. Skipping" % n)
  187. files["others"].append(n)
  188. continue
  189. # Work on files that contain only functions/enums and namespaces
  190. if len(d["functions"]) + len(d["namespaces"]) + len(d["enums"]) == contained_elements:
  191. correct_functions = []
  192. incorrect_functions = []
  193. # Collect enums to generate binding files
  194. enums = []
  195. enum_types = []
  196. for e in d["enums"]:
  197. enums.append({"name": e.name, "namespaces": d["namespaces"], "constants": e.constants})
  198. enum_types.append(e.name)
  199. # Collect functions to generate binding files
  200. for f in d["functions"]:
  201. parameters = []
  202. correct_function = True
  203. f_errors = []
  204. for p in f.parameters:
  205. typ, correct = map_parameter_types(p[0], p[1], p[2], f_errors, enum_types)
  206. correct_function &= correct
  207. parameters.append({"name": p[0], "type": typ})
  208. if correct_function and len(parameters) > 0: #TODO add constants like EPS
  209. correct_functions.append({"parameters": parameters, "namespaces": d["namespaces"], "name": f.name})
  210. elif len(parameters) > 0:
  211. incorrect_functions.append({"parameters": parameters, "namespaces": d["namespaces"], "name": f.name})
  212. errors["incorrect"].append("Incorrect function in %s: %s, %s\n" % (n, f.name, ",".join(f_errors)))
  213. else:
  214. errors["various"].append("Function without pars in %s: %s, %s\n" % (n, f.name, ","
  215. "".join(f_errors)))
  216. # Write binding files
  217. try:
  218. tpl = Template(filename='basic_function.mako')
  219. rendered = tpl.render(functions=correct_functions, enums=enums)
  220. tpl1 = Template(filename='basic_function.mako')
  221. rendered1 = tpl.render(functions=incorrect_functions, enums=enums)
  222. path = "generated/"
  223. if len(incorrect_functions) == 0 and (len(correct_functions) != 0 or len(enums) != 0):
  224. path += "complete/"
  225. with open(path + single_prefix + "py_" + n + ".cpp", 'w') as fs:
  226. fs.write(rendered)
  227. files["complete"].append(n)
  228. else:
  229. path += "partial/"
  230. with open(path + single_prefix + "py_" + n + ".cpp", 'w') as fs:
  231. fs.write("// COMPLETE BINDINGS ========================\n")
  232. fs.write(rendered)
  233. fs.write("\n\n\n\n// INCOMPLETE BINDINGS ========================\n")
  234. fs.write(rendered1)
  235. if len(correct_functions) != 0:
  236. files["partial"].append(n)
  237. else:
  238. files["errors"].append(n)
  239. except Exception as e:
  240. files["errors"].append(n)
  241. errors["render"].append("Template rendering failed:" + n + " " + str(correct_functions) + ", incorrect "
  242. "functions are " + str(
  243. incorrect_functions) + str(e) + "\n")
  244. print("Writing error and overview files...")
  245. with open("errors.txt" + single_postfix, 'w') as fs:
  246. l = list(errors.keys())
  247. l.sort()
  248. for k in l:
  249. fs.write("%s: %i \n" %(k, len(errors[k])))
  250. fs.writelines("\n".join(errors[k]))
  251. fs.write("\n\n\n")
  252. with open("files.txt" + single_postfix, 'w') as fs:
  253. l = list(files.keys())
  254. l.sort()
  255. for k in l:
  256. fs.write("%s: %i \n" %(k, len(files[k])))
  257. fs.writelines("\n".join(files[k]))
  258. fs.write("\n\n\n")