scu_builders.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. """Functions used to generate scu build source files during build time
  2. """
  3. import glob, os
  4. import math
  5. from pathlib import Path
  6. from os.path import normpath, basename
  7. base_folder_path = str(Path(__file__).parent) + "/"
  8. base_folder_only = os.path.basename(os.path.normpath(base_folder_path))
  9. _verbose = False # Set manually for debug prints
  10. _scu_folders = set()
  11. _max_includes_per_scu = 1024
  12. def clear_out_existing_files(output_folder, extension):
  13. output_folder = os.path.abspath(output_folder)
  14. # print("clear_out_existing_files from folder: " + output_folder)
  15. if not os.path.isdir(output_folder):
  16. # folder does not exist or has not been created yet,
  17. # no files to clearout. (this is not an error)
  18. return
  19. for file in glob.glob(output_folder + "/*." + extension):
  20. # print("removed pre-existing file: " + file)
  21. os.remove(file)
  22. def folder_not_found(folder):
  23. abs_folder = base_folder_path + folder + "/"
  24. return not os.path.isdir(abs_folder)
  25. def find_files_in_folder(folder, sub_folder, include_list, extension, sought_exceptions, found_exceptions):
  26. abs_folder = base_folder_path + folder + "/" + sub_folder
  27. if not os.path.isdir(abs_folder):
  28. print("SCU: ERROR: %s not found." % abs_folder)
  29. return include_list, found_exceptions
  30. os.chdir(abs_folder)
  31. sub_folder_slashed = ""
  32. if sub_folder != "":
  33. sub_folder_slashed = sub_folder + "/"
  34. for file in glob.glob("*." + extension):
  35. simple_name = Path(file).stem
  36. if file.endswith(".gen.cpp"):
  37. continue
  38. li = '#include "' + folder + "/" + sub_folder_slashed + file + '"'
  39. if not simple_name in sought_exceptions:
  40. include_list.append(li)
  41. else:
  42. found_exceptions.append(li)
  43. return include_list, found_exceptions
  44. def write_output_file(file_count, include_list, start_line, end_line, output_folder, output_filename_prefix, extension):
  45. output_folder = os.path.abspath(output_folder)
  46. if not os.path.isdir(output_folder):
  47. # create
  48. os.mkdir(output_folder)
  49. if not os.path.isdir(output_folder):
  50. print("SCU: ERROR: %s could not be created." % output_folder)
  51. return
  52. if _verbose:
  53. print("SCU: Creating folder: %s" % output_folder)
  54. file_text = ""
  55. for l in range(start_line, end_line):
  56. if l < len(include_list):
  57. line = include_list[l]
  58. li = line + "\n"
  59. file_text += li
  60. num_string = ""
  61. if file_count > 0:
  62. num_string = "_" + str(file_count)
  63. short_filename = output_filename_prefix + num_string + ".gen." + extension
  64. output_filename = output_folder + "/" + short_filename
  65. if _verbose:
  66. print("SCU: Generating: %s" % short_filename)
  67. output_path = Path(output_filename)
  68. output_path.write_text(file_text, encoding="utf8")
  69. def write_exception_output_file(file_count, exception_string, output_folder, output_filename_prefix, extension):
  70. output_folder = os.path.abspath(output_folder)
  71. if not os.path.isdir(output_folder):
  72. print("SCU: ERROR: %s does not exist." % output_folder)
  73. return
  74. file_text = exception_string + "\n"
  75. num_string = ""
  76. if file_count > 0:
  77. num_string = "_" + str(file_count)
  78. short_filename = output_filename_prefix + "_exception" + num_string + ".gen." + extension
  79. output_filename = output_folder + "/" + short_filename
  80. if _verbose:
  81. print("SCU: Generating: " + short_filename)
  82. output_path = Path(output_filename)
  83. output_path.write_text(file_text, encoding="utf8")
  84. def find_section_name(sub_folder):
  85. # Construct a useful name for the section from the path for debug logging
  86. section_path = os.path.abspath(base_folder_path + sub_folder) + "/"
  87. folders = []
  88. folder = ""
  89. for i in range(8):
  90. folder = os.path.dirname(section_path)
  91. folder = os.path.basename(folder)
  92. if folder == base_folder_only:
  93. break
  94. folders.append(folder)
  95. section_path += "../"
  96. section_path = os.path.abspath(section_path) + "/"
  97. section_name = ""
  98. for n in range(len(folders)):
  99. section_name += folders[len(folders) - n - 1]
  100. if n != (len(folders) - 1):
  101. section_name += "_"
  102. return section_name
  103. # "folders" is a list of folders to add all the files from to add to the SCU
  104. # "section (like a module)". The name of the scu file will be derived from the first folder
  105. # (thus e.g. scene/3d becomes scu_scene_3d.gen.cpp)
  106. # "includes_per_scu" limits the number of includes in a single scu file.
  107. # This allows the module to be built in several translation units instead of just 1.
  108. # This will usually be slower to compile but will use less memory per compiler instance, which
  109. # is most relevant in release builds.
  110. # "sought_exceptions" are a list of files (without extension) that contain
  111. # e.g. naming conflicts, and are therefore not suitable for the scu build.
  112. # These will automatically be placed in their own separate scu file,
  113. # which is slow like a normal build, but prevents the naming conflicts.
  114. # Ideally in these situations, the source code should be changed to prevent naming conflicts.
  115. # "extension" will usually be cpp, but can also be set to c (for e.g. third party libraries that use c)
  116. def process_folder(folders, sought_exceptions=[], includes_per_scu=0, extension="cpp"):
  117. if len(folders) == 0:
  118. return
  119. # Construct the filename prefix from the FIRST folder name
  120. # e.g. "scene_3d"
  121. out_filename = find_section_name(folders[0])
  122. found_includes = []
  123. found_exceptions = []
  124. main_folder = folders[0]
  125. abs_main_folder = base_folder_path + main_folder
  126. # Keep a record of all folders that have been processed for SCU,
  127. # this enables deciding what to do when we call "add_source_files()"
  128. global _scu_folders
  129. _scu_folders.add(main_folder)
  130. # main folder (first)
  131. found_includes, found_exceptions = find_files_in_folder(
  132. main_folder, "", found_includes, extension, sought_exceptions, found_exceptions
  133. )
  134. # sub folders
  135. for d in range(1, len(folders)):
  136. found_includes, found_exceptions = find_files_in_folder(
  137. main_folder, folders[d], found_includes, extension, sought_exceptions, found_exceptions
  138. )
  139. found_includes = sorted(found_includes)
  140. # calculate how many lines to write in each file
  141. total_lines = len(found_includes)
  142. # adjust number of output files according to whether DEV or release
  143. num_output_files = 1
  144. if includes_per_scu == 0:
  145. includes_per_scu = _max_includes_per_scu
  146. else:
  147. if includes_per_scu > _max_includes_per_scu:
  148. includes_per_scu = _max_includes_per_scu
  149. num_output_files = max(math.ceil(total_lines / float(includes_per_scu)), 1)
  150. lines_per_file = math.ceil(total_lines / float(num_output_files))
  151. lines_per_file = max(lines_per_file, 1)
  152. start_line = 0
  153. file_number = 0
  154. # These do not vary throughout the loop
  155. output_folder = abs_main_folder + "/scu/"
  156. output_filename_prefix = "scu_" + out_filename
  157. # Clear out any existing files (usually we will be overwriting,
  158. # but we want to remove any that are pre-existing that will not be
  159. # overwritten, so as to not compile anything stale)
  160. clear_out_existing_files(output_folder, extension)
  161. for file_count in range(0, num_output_files):
  162. end_line = start_line + lines_per_file
  163. # special case to cover rounding error in final file
  164. if file_count == (num_output_files - 1):
  165. end_line = len(found_includes)
  166. write_output_file(
  167. file_count, found_includes, start_line, end_line, output_folder, output_filename_prefix, extension
  168. )
  169. start_line = end_line
  170. # Write the exceptions each in their own scu gen file,
  171. # so they can effectively compile in "old style / normal build".
  172. for exception_count in range(len(found_exceptions)):
  173. write_exception_output_file(
  174. exception_count, found_exceptions[exception_count], output_folder, output_filename_prefix, extension
  175. )
  176. def generate_scu_files(max_includes_per_scu):
  177. print("=============================")
  178. print("Single Compilation Unit Build")
  179. print("=============================")
  180. global _max_includes_per_scu
  181. _max_includes_per_scu = max_includes_per_scu
  182. print("SCU: Generating build files... (max includes per SCU: %d)" % _max_includes_per_scu)
  183. curr_folder = os.path.abspath("./")
  184. # check we are running from the correct folder
  185. if folder_not_found("core") or folder_not_found("platform") or folder_not_found("scene"):
  186. raise RuntimeError("scu_builders.py must be run from the godot folder.")
  187. return
  188. process_folder(["core"])
  189. process_folder(["core/crypto"])
  190. process_folder(["core/debugger"])
  191. process_folder(["core/extension"])
  192. process_folder(["core/input"])
  193. process_folder(["core/io"])
  194. process_folder(["core/math"])
  195. process_folder(["core/object"])
  196. process_folder(["core/os"])
  197. process_folder(["core/string"])
  198. process_folder(["core/variant"], ["variant_utility"])
  199. process_folder(["drivers/unix"])
  200. process_folder(["drivers/png"])
  201. process_folder(["editor"], ["file_system_dock", "editor_resource_preview"], 32)
  202. process_folder(["editor/debugger"])
  203. process_folder(["editor/debugger/debug_adapter"])
  204. process_folder(["editor/export"])
  205. process_folder(["editor/gui"])
  206. process_folder(["editor/import"])
  207. process_folder(["editor/plugins"])
  208. process_folder(["editor/plugins/gizmos"])
  209. process_folder(["editor/plugins/tiles"])
  210. process_folder(["platform/android/export"])
  211. process_folder(["platform/ios/export"])
  212. process_folder(["platform/linuxbsd/export"])
  213. process_folder(["platform/macos/export"])
  214. process_folder(["platform/web/export"])
  215. process_folder(["platform/windows/export"])
  216. process_folder(["modules/gltf"])
  217. process_folder(["modules/gltf/structures"])
  218. process_folder(["modules/gltf/editor"])
  219. process_folder(["modules/gltf/extensions"])
  220. process_folder(["modules/gltf/extensions/physics"])
  221. process_folder(["modules/navigation"])
  222. process_folder(["modules/webrtc"])
  223. process_folder(["modules/websocket"])
  224. process_folder(["modules/gridmap"])
  225. process_folder(["modules/multiplayer"])
  226. process_folder(["modules/multiplayer/editor"])
  227. process_folder(["modules/openxr"], ["register_types"])
  228. process_folder(["modules/openxr/action_map"])
  229. process_folder(["modules/openxr/editor"])
  230. process_folder(["modules/csg"])
  231. process_folder(["modules/gdscript"])
  232. process_folder(["modules/gdscript/editor"])
  233. process_folder(["modules/gdscript/language_server"])
  234. process_folder(["scene/2d"])
  235. process_folder(["scene/3d"])
  236. process_folder(["scene/animation"])
  237. process_folder(["scene/gui"])
  238. process_folder(["scene/main"])
  239. process_folder(["scene/resources"])
  240. process_folder(["servers"])
  241. process_folder(["servers/rendering"])
  242. process_folder(["servers/rendering/storage"])
  243. process_folder(["servers/rendering/renderer_rd"])
  244. process_folder(["servers/rendering/renderer_rd/effects"])
  245. process_folder(["servers/rendering/renderer_rd/environment"])
  246. process_folder(["servers/rendering/renderer_rd/storage_rd"])
  247. process_folder(["servers/physics_2d"])
  248. process_folder(["servers/physics_3d"])
  249. process_folder(["servers/physics_3d/joints"])
  250. process_folder(["servers/audio"])
  251. process_folder(["servers/audio/effects"])
  252. # Finally change back the path to the calling folder
  253. os.chdir(curr_folder)
  254. if _verbose:
  255. print("SCU: Processed folders: %s" % sorted(_scu_folders))
  256. return _scu_folders