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