Path: blob/master/modules/raycast/godot_update_embree.py
10277 views
import glob1import os2import re3import shutil4import stat5import subprocess6import sys7from typing import Any, Callable89git_tag = "v4.4.0"1011include_dirs = [12"common/tasking",13"kernels/bvh",14"kernels/builders",15"common/sys",16"kernels",17"kernels/common",18"common/math",19"common/algorithms",20"common/lexers",21"common/simd",22"common/simd/arm",23"common/simd/wasm",24"include/embree4",25"kernels/subdiv",26"kernels/geometry",27]2829cpp_files = [30"common/sys/sysinfo.cpp",31"common/sys/alloc.cpp",32"common/sys/estring.cpp",33"common/sys/filename.cpp",34"common/sys/library.cpp",35"common/sys/thread.cpp",36"common/sys/regression.cpp",37"common/sys/mutex.cpp",38"common/sys/condition.cpp",39"common/sys/barrier.cpp",40"common/math/constants.cpp",41"common/simd/sse.cpp",42"common/lexers/stringstream.cpp",43"common/lexers/tokenstream.cpp",44"common/tasking/taskschedulerinternal.cpp",45"kernels/common/device.cpp",46"kernels/common/stat.cpp",47"kernels/common/acceln.cpp",48"kernels/common/accelset.cpp",49"kernels/common/state.cpp",50"kernels/common/rtcore.cpp",51"kernels/common/rtcore_builder.cpp",52"kernels/common/scene.cpp",53"kernels/common/scene_verify.cpp",54"kernels/common/alloc.cpp",55"kernels/common/geometry.cpp",56"kernels/common/scene_triangle_mesh.cpp",57"kernels/geometry/primitive4.cpp",58"kernels/builders/primrefgen.cpp",59"kernels/bvh/bvh.cpp",60"kernels/bvh/bvh_statistics.cpp",61"kernels/bvh/bvh4_factory.cpp",62"kernels/bvh/bvh8_factory.cpp",63"kernels/bvh/bvh_collider.cpp",64"kernels/bvh/bvh_rotate.cpp",65"kernels/bvh/bvh_refit.cpp",66"kernels/bvh/bvh_builder.cpp",67"kernels/bvh/bvh_builder_morton.cpp",68"kernels/bvh/bvh_builder_sah.cpp",69"kernels/bvh/bvh_builder_sah_spatial.cpp",70"kernels/bvh/bvh_builder_sah_mb.cpp",71"kernels/bvh/bvh_builder_twolevel.cpp",72"kernels/bvh/bvh_intersector1.cpp",73"kernels/bvh/bvh_intersector1_bvh4.cpp",74"kernels/bvh/bvh_intersector_hybrid4_bvh4.cpp",75"kernels/bvh/bvh_intersector_hybrid.cpp",76]7778config_files = [79"kernels/config.h.in",80"kernels/rtcore_config.h.in",81]8283license_file = "LICENSE.txt"8485os.chdir(f"{os.path.dirname(__file__)}/../../thirdparty")8687dir_name = "embree"88if os.path.exists(dir_name):89shutil.rmtree(dir_name)9091# In case something went wrong and embree-tmp stayed on the system.92if os.path.exists("embree-tmp"):93shutil.rmtree("embree-tmp")9495subprocess.run(["git", "clone", "https://github.com/embree/embree.git", "embree-tmp"])96os.chdir("embree-tmp")97subprocess.run(["git", "checkout", git_tag])9899commit_hash = str(subprocess.check_output(["git", "rev-parse", "HEAD"], universal_newlines=True)).strip()100101102def on_rm_error(function: Callable[..., Any], path: str, excinfo: Exception) -> None:103"""104Error handler for `shutil.rmtree()`.105106If the error is due to read-only files,107it will change the file permissions and retry.108"""109os.chmod(path, stat.S_IWRITE)110os.unlink(path)111112113# We remove the .git directory because it contains114# a lot of read-only files that are problematic on Windows.115if sys.version_info >= (3, 12):116shutil.rmtree(".git", onexc=on_rm_error)117else:118shutil.rmtree(".git", onerror=on_rm_error) # type: ignore119120all_files = set(cpp_files)121122for config_file in config_files:123all_files.add(config_file)124125all_files.add(license_file)126127dest_dir = os.path.join("..", dir_name)128for include_dir in include_dirs:129headers = glob.iglob(os.path.join(include_dir, "*.h"))130all_files.update(headers)131132for f in all_files:133d = os.path.join(dest_dir, os.path.dirname(f))134if not os.path.exists(d):135os.makedirs(d)136shutil.copy2(f, d)137138with open(os.path.join(dest_dir, "kernels/hash.h"), "w", encoding="utf-8", newline="\n") as hash_file:139hash_file.write(140f"""// Copyright 2009-2021 Intel Corporation141// SPDX-License-Identifier: Apache-2.0142143#define RTC_HASH "{commit_hash}"144"""145)146147for config_file in config_files:148os.rename(os.path.join(dest_dir, config_file), os.path.join(dest_dir, config_file[:-3]))149150with open("CMakeLists.txt", "r", encoding="utf-8") as cmake_file:151cmake_content = cmake_file.read()152major_version = int(re.compile(r"EMBREE_VERSION_MAJOR\s(\d+)").findall(cmake_content)[0])153minor_version = int(re.compile(r"EMBREE_VERSION_MINOR\s(\d+)").findall(cmake_content)[0])154patch_version = int(re.compile(r"EMBREE_VERSION_PATCH\s(\d+)").findall(cmake_content)[0])155156shutil.move(os.path.join(dest_dir, "kernels/rtcore_config.h"), os.path.join(dest_dir, ("include/embree4/")))157158with open(159os.path.join(dest_dir, "include/embree4/rtcore_config.h"), "r+", encoding="utf-8", newline="\n"160) as rtcore_config:161lines = rtcore_config.readlines()162rtcore_config.seek(0)163for i, line in enumerate(lines):164if line.startswith("#define RTC_VERSION_MAJOR"):165lines[i : i + 5] = [166f"#define RTC_VERSION_MAJOR {major_version}\n",167f"#define RTC_VERSION_MINOR {minor_version}\n",168f"#define RTC_VERSION_PATCH {patch_version}\n",169f"#define RTC_VERSION {major_version}{minor_version:02d}{patch_version:02d}\n",170f'#define RTC_VERSION_STRING "{major_version}.{minor_version}.{patch_version}"\n',171]172break173rtcore_config.writelines(lines)174rtcore_config.truncate()175176os.chdir("..")177shutil.rmtree("embree-tmp")178179subprocess.run(["git", "restore", "embree/patches"])180181for patch in os.listdir("embree/patches"):182subprocess.run(["git", "apply", f"embree/patches/{patch}"])183184185