scu_builders.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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
  10. _is_release_build = False
  11. _scu_folders = set()
  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. os.chdir(output_folder)
  20. for file in glob.glob("*." + extension):
  21. # print("removed pre-existing file: " + file)
  22. os.remove(file)
  23. def folder_not_found(folder):
  24. abs_folder = base_folder_path + folder + "/"
  25. return not os.path.isdir(abs_folder)
  26. def find_files_in_folder(folder, sub_folder, include_list, extension, sought_exceptions, found_exceptions):
  27. abs_folder = base_folder_path + folder + "/" + sub_folder
  28. if not os.path.isdir(abs_folder):
  29. print("ERROR " + abs_folder + " not found.")
  30. return include_list, found_exceptions
  31. os.chdir(abs_folder)
  32. sub_folder_slashed = ""
  33. if sub_folder != "":
  34. sub_folder_slashed = sub_folder + "/"
  35. for file in glob.glob("*." + extension):
  36. simple_name = Path(file).stem
  37. if file.endswith(".gen.cpp"):
  38. continue
  39. li = '#include "' + folder + "/" + sub_folder_slashed + file + '"'
  40. if not simple_name in sought_exceptions:
  41. include_list.append(li)
  42. else:
  43. found_exceptions.append(li)
  44. return include_list, found_exceptions
  45. def write_output_file(file_count, include_list, start_line, end_line, output_folder, output_filename_prefix, extension):
  46. output_folder = os.path.abspath(output_folder)
  47. if not os.path.isdir(output_folder):
  48. # create
  49. os.mkdir(output_folder)
  50. if not os.path.isdir(output_folder):
  51. print("ERROR " + output_folder + " could not be created.")
  52. return
  53. print("CREATING folder " + 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("generating: " + 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("ERROR " + output_folder + " does not exist.")
  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("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 _is_release_build:
  145. # always have a maximum in release
  146. includes_per_scu = 8
  147. num_output_files = max(math.ceil(total_lines / includes_per_scu), 1)
  148. else:
  149. if includes_per_scu > 0:
  150. num_output_files = max(math.ceil(total_lines / includes_per_scu), 1)
  151. # error condition
  152. if total_lines == 0:
  153. return
  154. lines_per_file = math.ceil(total_lines / num_output_files)
  155. lines_per_file = max(lines_per_file, 1)
  156. start_line = 0
  157. file_number = 0
  158. # These do not vary throughout the loop
  159. output_folder = abs_main_folder + "/.scu/"
  160. output_filename_prefix = "scu_" + out_filename
  161. # Clear out any existing files (usually we will be overwriting,
  162. # but we want to remove any that are pre-existing that will not be
  163. # overwritten, so as to not compile anything stale)
  164. clear_out_existing_files(output_folder, extension)
  165. for file_count in range(0, num_output_files):
  166. end_line = start_line + lines_per_file
  167. # special case to cover rounding error in final file
  168. if file_count == (num_output_files - 1):
  169. end_line = len(found_includes)
  170. write_output_file(
  171. file_count, found_includes, start_line, end_line, output_folder, output_filename_prefix, extension
  172. )
  173. start_line = end_line
  174. # Write the exceptions each in their own scu gen file,
  175. # so they can effectively compile in "old style / normal build".
  176. for exception_count in range(len(found_exceptions)):
  177. write_exception_output_file(
  178. exception_count, found_exceptions[exception_count], output_folder, output_filename_prefix, extension
  179. )
  180. def generate_scu_files(verbose, is_release_build):
  181. print("=============================")
  182. print("Single Compilation Unit Build")
  183. print("=============================")
  184. print("Generating SCU build files")
  185. global _verbose
  186. _verbose = verbose
  187. global _is_release_build
  188. _is_release_build = is_release_build
  189. curr_folder = os.path.abspath("./")
  190. # check we are running from the correct folder
  191. if folder_not_found("core") or folder_not_found("platform") or folder_not_found("scene"):
  192. raise RuntimeError("scu_builders.py must be run from the godot folder.")
  193. return
  194. process_folder(["core"])
  195. process_folder(["core/math"])
  196. process_folder(["core/os"])
  197. process_folder(["core/io"])
  198. process_folder(["core/crypto"])
  199. process_folder(["drivers/gles2"])
  200. process_folder(["drivers/gles3"])
  201. process_folder(["drivers/unix"])
  202. process_folder(["drivers/png"])
  203. process_folder(["editor"])
  204. process_folder(["editor/import"])
  205. process_folder(["editor/plugins"])
  206. process_folder(["main"])
  207. process_folder(["main/tests"])
  208. process_folder(
  209. [
  210. "platform",
  211. "android/export",
  212. "iphone/export",
  213. "javascript/export",
  214. "osx/export",
  215. "uwp/export",
  216. "windows/export",
  217. "x11/export",
  218. ]
  219. )
  220. process_folder(["modules/gltf"])
  221. process_folder(["modules/gltf/structures"])
  222. process_folder(["modules/gltf/extensions"])
  223. process_folder(["modules/gltf/extensions/physics"])
  224. process_folder(["modules/bullet"])
  225. process_folder(["modules/navigation"])
  226. process_folder(["modules/visual_script"])
  227. process_folder(["modules/webrtc"])
  228. process_folder(["modules/webxr"])
  229. process_folder(["modules/websocket"])
  230. process_folder(["modules/gridmap"])
  231. process_folder(["modules/csg"])
  232. process_folder(["modules/gdscript"])
  233. process_folder(["modules/gdscript/language_server"])
  234. process_folder(["modules/fbx"])
  235. process_folder(["modules/fbx/tools"])
  236. process_folder(["modules/fbx/data"])
  237. process_folder(["modules/fbx/fbx_parser"])
  238. process_folder(["modules/gdnative"])
  239. process_folder(["modules/gdnative/gdnative"])
  240. process_folder(["modules/gdnative/nativescript"])
  241. process_folder(["modules/gdnative/arvr"])
  242. process_folder(["modules/gdnative/pluginscript"])
  243. process_folder(["modules/gdnative/net"])
  244. process_folder(["modules/mbedtls"])
  245. process_folder(["scene"])
  246. process_folder(["scene/audio"])
  247. process_folder(["scene/debugger"])
  248. process_folder(["scene/2d"])
  249. process_folder(["scene/3d"])
  250. process_folder(["scene/animation"])
  251. process_folder(["scene/gui"])
  252. process_folder(["scene/main"])
  253. process_folder(["scene/resources"])
  254. process_folder(["servers"])
  255. process_folder(["servers/visual"])
  256. process_folder(["servers/visual/portals"])
  257. process_folder(["servers/physics_2d"])
  258. process_folder(["servers/physics"])
  259. process_folder(["servers/physics/joints"])
  260. process_folder(["servers/audio"])
  261. process_folder(["servers/audio/effects"])
  262. # Finally change back the path to the calling folder
  263. os.chdir(curr_folder)
  264. return _scu_folders