godot_update_embree.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import glob
  2. import os
  3. import re
  4. import shutil
  5. import stat
  6. import subprocess
  7. import sys
  8. from typing import Any, Callable
  9. git_tag = "v4.3.1"
  10. include_dirs = [
  11. "common/tasking",
  12. "kernels/bvh",
  13. "kernels/builders",
  14. "common/sys",
  15. "kernels",
  16. "kernels/common",
  17. "common/math",
  18. "common/algorithms",
  19. "common/lexers",
  20. "common/simd",
  21. "common/simd/arm",
  22. "common/simd/wasm",
  23. "include/embree4",
  24. "kernels/subdiv",
  25. "kernels/geometry",
  26. ]
  27. cpp_files = [
  28. "common/sys/sysinfo.cpp",
  29. "common/sys/alloc.cpp",
  30. "common/sys/estring.cpp",
  31. "common/sys/filename.cpp",
  32. "common/sys/library.cpp",
  33. "common/sys/thread.cpp",
  34. "common/sys/regression.cpp",
  35. "common/sys/mutex.cpp",
  36. "common/sys/condition.cpp",
  37. "common/sys/barrier.cpp",
  38. "common/math/constants.cpp",
  39. "common/simd/sse.cpp",
  40. "common/lexers/stringstream.cpp",
  41. "common/lexers/tokenstream.cpp",
  42. "common/tasking/taskschedulerinternal.cpp",
  43. "kernels/common/device.cpp",
  44. "kernels/common/stat.cpp",
  45. "kernels/common/acceln.cpp",
  46. "kernels/common/accelset.cpp",
  47. "kernels/common/state.cpp",
  48. "kernels/common/rtcore.cpp",
  49. "kernels/common/rtcore_builder.cpp",
  50. "kernels/common/scene.cpp",
  51. "kernels/common/scene_verify.cpp",
  52. "kernels/common/alloc.cpp",
  53. "kernels/common/geometry.cpp",
  54. "kernels/common/scene_triangle_mesh.cpp",
  55. "kernels/geometry/primitive4.cpp",
  56. "kernels/builders/primrefgen.cpp",
  57. "kernels/bvh/bvh.cpp",
  58. "kernels/bvh/bvh_statistics.cpp",
  59. "kernels/bvh/bvh4_factory.cpp",
  60. "kernels/bvh/bvh8_factory.cpp",
  61. "kernels/bvh/bvh_collider.cpp",
  62. "kernels/bvh/bvh_rotate.cpp",
  63. "kernels/bvh/bvh_refit.cpp",
  64. "kernels/bvh/bvh_builder.cpp",
  65. "kernels/bvh/bvh_builder_morton.cpp",
  66. "kernels/bvh/bvh_builder_sah.cpp",
  67. "kernels/bvh/bvh_builder_sah_spatial.cpp",
  68. "kernels/bvh/bvh_builder_sah_mb.cpp",
  69. "kernels/bvh/bvh_builder_twolevel.cpp",
  70. "kernels/bvh/bvh_intersector1.cpp",
  71. "kernels/bvh/bvh_intersector1_bvh4.cpp",
  72. "kernels/bvh/bvh_intersector_hybrid4_bvh4.cpp",
  73. "kernels/bvh/bvh_intersector_hybrid.cpp",
  74. ]
  75. config_files = [
  76. "kernels/config.h.in",
  77. "kernels/rtcore_config.h.in",
  78. ]
  79. license_file = "LICENSE.txt"
  80. os.chdir(f"{os.path.dirname(__file__)}/../../thirdparty")
  81. dir_name = "embree"
  82. if os.path.exists(dir_name):
  83. shutil.rmtree(dir_name)
  84. # In case something went wrong and embree-tmp stayed on the system.
  85. if os.path.exists("embree-tmp"):
  86. shutil.rmtree("embree-tmp")
  87. subprocess.run(["git", "clone", "https://github.com/embree/embree.git", "embree-tmp"])
  88. os.chdir("embree-tmp")
  89. subprocess.run(["git", "checkout", git_tag])
  90. commit_hash = str(subprocess.check_output(["git", "rev-parse", "HEAD"], universal_newlines=True)).strip()
  91. def on_rm_error(function: Callable[..., Any], path: str, excinfo: Exception) -> None:
  92. """
  93. Error handler for `shutil.rmtree()`.
  94. If the error is due to read-only files,
  95. it will change the file permissions and retry.
  96. """
  97. os.chmod(path, stat.S_IWRITE)
  98. os.unlink(path)
  99. # We remove the .git directory because it contains
  100. # a lot of read-only files that are problematic on Windows.
  101. if sys.version_info >= (3, 12):
  102. shutil.rmtree(".git", onexc=on_rm_error)
  103. else:
  104. shutil.rmtree(".git", onerror=on_rm_error) # type: ignore
  105. all_files = set(cpp_files)
  106. for config_file in config_files:
  107. all_files.add(config_file)
  108. all_files.add(license_file)
  109. dest_dir = os.path.join("..", dir_name)
  110. for include_dir in include_dirs:
  111. headers = glob.iglob(os.path.join(include_dir, "*.h"))
  112. all_files.update(headers)
  113. for f in all_files:
  114. d = os.path.join(dest_dir, os.path.dirname(f))
  115. if not os.path.exists(d):
  116. os.makedirs(d)
  117. shutil.copy2(f, d)
  118. with open(os.path.join(dest_dir, "kernels/hash.h"), "w", encoding="utf-8", newline="\n") as hash_file:
  119. hash_file.write(
  120. f"""// Copyright 2009-2021 Intel Corporation
  121. // SPDX-License-Identifier: Apache-2.0
  122. #define RTC_HASH "{commit_hash}"
  123. """
  124. )
  125. for config_file in config_files:
  126. os.rename(os.path.join(dest_dir, config_file), os.path.join(dest_dir, config_file[:-3]))
  127. with open("CMakeLists.txt", "r", encoding="utf-8") as cmake_file:
  128. cmake_content = cmake_file.read()
  129. major_version = int(re.compile(r"EMBREE_VERSION_MAJOR\s(\d+)").findall(cmake_content)[0])
  130. minor_version = int(re.compile(r"EMBREE_VERSION_MINOR\s(\d+)").findall(cmake_content)[0])
  131. patch_version = int(re.compile(r"EMBREE_VERSION_PATCH\s(\d+)").findall(cmake_content)[0])
  132. shutil.move(os.path.join(dest_dir, "kernels/rtcore_config.h"), os.path.join(dest_dir, ("include/embree4/")))
  133. with open(
  134. os.path.join(dest_dir, "include/embree4/rtcore_config.h"), "r+", encoding="utf-8", newline="\n"
  135. ) as rtcore_config:
  136. lines = rtcore_config.readlines()
  137. rtcore_config.seek(0)
  138. for i, line in enumerate(lines):
  139. if line.startswith("#define RTC_VERSION_MAJOR"):
  140. lines[i : i + 5] = [
  141. f"#define RTC_VERSION_MAJOR {major_version}\n",
  142. f"#define RTC_VERSION_MINOR {minor_version}\n",
  143. f"#define RTC_VERSION_PATCH {patch_version}\n",
  144. f"#define RTC_VERSION {major_version}{minor_version:02d}{patch_version:02d}\n",
  145. f'#define RTC_VERSION_STRING "{major_version}.{minor_version}.{patch_version}"\n',
  146. ]
  147. break
  148. rtcore_config.writelines(lines)
  149. rtcore_config.truncate()
  150. os.chdir("..")
  151. shutil.rmtree("embree-tmp")
  152. subprocess.run(["git", "restore", "embree/patches"])
  153. for patch in os.listdir("embree/patches"):
  154. subprocess.run(["git", "apply", f"embree/patches/{patch}"])