| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | import glob | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import re | 
					
						
							|  |  |  | import shutil | 
					
						
							|  |  |  | import stat | 
					
						
							|  |  |  | import subprocess | 
					
						
							|  |  |  | from types import TracebackType | 
					
						
							|  |  |  | from typing import Any, Callable, Tuple, Type | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | git_tag = "v4.3.1" | 
					
						
							| 
									
										
										
										
											2022-11-24 11:45:59 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | include_dirs = [ | 
					
						
							|  |  |  |     "common/tasking", | 
					
						
							|  |  |  |     "kernels/bvh", | 
					
						
							|  |  |  |     "kernels/builders", | 
					
						
							|  |  |  |     "common/sys", | 
					
						
							|  |  |  |     "kernels", | 
					
						
							|  |  |  |     "kernels/common", | 
					
						
							|  |  |  |     "common/math", | 
					
						
							|  |  |  |     "common/algorithms", | 
					
						
							|  |  |  |     "common/lexers", | 
					
						
							|  |  |  |     "common/simd", | 
					
						
							| 
									
										
										
										
											2021-05-20 12:49:33 +02:00
										 |  |  |     "common/simd/arm", | 
					
						
							| 
									
										
										
										
											2022-11-24 11:45:59 -03:00
										 |  |  |     "common/simd/wasm", | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |     "include/embree4", | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  |     "kernels/subdiv", | 
					
						
							|  |  |  |     "kernels/geometry", | 
					
						
							|  |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | cpp_files = [ | 
					
						
							|  |  |  |     "common/sys/sysinfo.cpp", | 
					
						
							|  |  |  |     "common/sys/alloc.cpp", | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |     "common/sys/estring.cpp", | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  |     "common/sys/filename.cpp", | 
					
						
							|  |  |  |     "common/sys/library.cpp", | 
					
						
							|  |  |  |     "common/sys/thread.cpp", | 
					
						
							|  |  |  |     "common/sys/regression.cpp", | 
					
						
							|  |  |  |     "common/sys/mutex.cpp", | 
					
						
							|  |  |  |     "common/sys/condition.cpp", | 
					
						
							|  |  |  |     "common/sys/barrier.cpp", | 
					
						
							|  |  |  |     "common/math/constants.cpp", | 
					
						
							|  |  |  |     "common/simd/sse.cpp", | 
					
						
							|  |  |  |     "common/lexers/stringstream.cpp", | 
					
						
							|  |  |  |     "common/lexers/tokenstream.cpp", | 
					
						
							|  |  |  |     "common/tasking/taskschedulerinternal.cpp", | 
					
						
							|  |  |  |     "kernels/common/device.cpp", | 
					
						
							|  |  |  |     "kernels/common/stat.cpp", | 
					
						
							|  |  |  |     "kernels/common/acceln.cpp", | 
					
						
							|  |  |  |     "kernels/common/accelset.cpp", | 
					
						
							|  |  |  |     "kernels/common/state.cpp", | 
					
						
							|  |  |  |     "kernels/common/rtcore.cpp", | 
					
						
							|  |  |  |     "kernels/common/rtcore_builder.cpp", | 
					
						
							|  |  |  |     "kernels/common/scene.cpp", | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |     "kernels/common/scene_verify.cpp", | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  |     "kernels/common/alloc.cpp", | 
					
						
							|  |  |  |     "kernels/common/geometry.cpp", | 
					
						
							|  |  |  |     "kernels/common/scene_triangle_mesh.cpp", | 
					
						
							|  |  |  |     "kernels/geometry/primitive4.cpp", | 
					
						
							|  |  |  |     "kernels/builders/primrefgen.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_statistics.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh4_factory.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh8_factory.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_collider.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_rotate.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_refit.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_builder.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_builder_morton.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_builder_sah.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_builder_sah_spatial.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_builder_sah_mb.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_builder_twolevel.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_intersector1.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_intersector1_bvh4.cpp", | 
					
						
							| 
									
										
										
										
											2021-09-13 15:05:21 +02:00
										 |  |  |     "kernels/bvh/bvh_intersector_hybrid4_bvh4.cpp", | 
					
						
							|  |  |  |     "kernels/bvh/bvh_intersector_hybrid.cpp", | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | config_files = [ | 
					
						
							|  |  |  |     "kernels/config.h.in", | 
					
						
							|  |  |  |     "kernels/rtcore_config.h.in", | 
					
						
							|  |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | license_file = "LICENSE.txt" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | os.chdir(f"{os.path.dirname(__file__)}/../../thirdparty") | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-20 12:49:33 +02:00
										 |  |  | dir_name = "embree" | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | if os.path.exists(dir_name): | 
					
						
							|  |  |  |     shutil.rmtree(dir_name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | # In case something went wrong and embree-tmp stayed on the system. | 
					
						
							|  |  |  | if os.path.exists("embree-tmp"): | 
					
						
							|  |  |  |     shutil.rmtree("embree-tmp") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-20 12:49:33 +02:00
										 |  |  | subprocess.run(["git", "clone", "https://github.com/embree/embree.git", "embree-tmp"]) | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | os.chdir("embree-tmp") | 
					
						
							| 
									
										
										
										
											2022-11-24 11:45:59 -03:00
										 |  |  | subprocess.run(["git", "checkout", git_tag]) | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | commit_hash = str(subprocess.check_output(["git", "rev-parse", "HEAD"], universal_newlines=True)).strip() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | def on_rm_error( | 
					
						
							|  |  |  |     function: Callable[..., Any], path: str, excinfo: Tuple[Type[Exception], Exception, TracebackType] | 
					
						
							|  |  |  | ) -> None: | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Error handler for `shutil.rmtree()`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If the error is due to read-only files, | 
					
						
							|  |  |  |     it will change the file permissions and retry. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     os.chmod(path, stat.S_IWRITE) | 
					
						
							|  |  |  |     os.unlink(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # 3.12 Python and beyond should replace `onerror` with `onexc`. | 
					
						
							|  |  |  | # We remove the .git directory because it contains | 
					
						
							|  |  |  | # a lot of read-only files that are problematic on Windows. | 
					
						
							|  |  |  | shutil.rmtree(".git", onerror=on_rm_error) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | all_files = set(cpp_files) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | for config_file in config_files: | 
					
						
							|  |  |  |     all_files.add(config_file) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | all_files.add(license_file) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | dest_dir = os.path.join("..", dir_name) | 
					
						
							|  |  |  | for include_dir in include_dirs: | 
					
						
							|  |  |  |     headers = glob.iglob(os.path.join(include_dir, "*.h")) | 
					
						
							|  |  |  |     all_files.update(headers) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | for f in all_files: | 
					
						
							|  |  |  |     d = os.path.join(dest_dir, os.path.dirname(f)) | 
					
						
							|  |  |  |     if not os.path.exists(d): | 
					
						
							|  |  |  |         os.makedirs(d) | 
					
						
							|  |  |  |     shutil.copy2(f, d) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-09 14:29:24 -06:00
										 |  |  | with open(os.path.join(dest_dir, "kernels/hash.h"), "w", encoding="utf-8", newline="\n") as hash_file: | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  |     hash_file.write( | 
					
						
							| 
									
										
										
										
											2022-11-24 11:45:59 -03:00
										 |  |  |         f"""// Copyright 2009-2021 Intel Corporation
 | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | // SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define RTC_HASH "{commit_hash}" | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | for config_file in config_files: | 
					
						
							|  |  |  |     os.rename(os.path.join(dest_dir, config_file), os.path.join(dest_dir, config_file[:-3])) | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-24 18:02:56 +01:00
										 |  |  | with open("CMakeLists.txt", "r", encoding="utf-8") as cmake_file: | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  |     cmake_content = cmake_file.read() | 
					
						
							|  |  |  |     major_version = int(re.compile(r"EMBREE_VERSION_MAJOR\s(\d+)").findall(cmake_content)[0]) | 
					
						
							|  |  |  |     minor_version = int(re.compile(r"EMBREE_VERSION_MINOR\s(\d+)").findall(cmake_content)[0]) | 
					
						
							|  |  |  |     patch_version = int(re.compile(r"EMBREE_VERSION_PATCH\s(\d+)").findall(cmake_content)[0]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | shutil.move(os.path.join(dest_dir, "kernels/rtcore_config.h"), os.path.join(dest_dir, ("include/embree4/"))) | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  | with open( | 
					
						
							|  |  |  |     os.path.join(dest_dir, "include/embree4/rtcore_config.h"), "r+", encoding="utf-8", newline="\n" | 
					
						
							|  |  |  | ) as rtcore_config: | 
					
						
							|  |  |  |     lines = rtcore_config.readlines() | 
					
						
							|  |  |  |     rtcore_config.seek(0) | 
					
						
							|  |  |  |     for i, line in enumerate(lines): | 
					
						
							|  |  |  |         if line.startswith("#define RTC_VERSION_MAJOR"): | 
					
						
							|  |  |  |             lines[i : i + 5] = [ | 
					
						
							|  |  |  |                 f"#define RTC_VERSION_MAJOR {major_version}\n", | 
					
						
							|  |  |  |                 f"#define RTC_VERSION_MINOR {minor_version}\n", | 
					
						
							|  |  |  |                 f"#define RTC_VERSION_PATCH {patch_version}\n", | 
					
						
							|  |  |  |                 f"#define RTC_VERSION {major_version}{minor_version:02d}{patch_version:02d}\n", | 
					
						
							|  |  |  |                 f'#define RTC_VERSION_STRING "{major_version}.{minor_version}.{patch_version}"\n', | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |     rtcore_config.writelines(lines) | 
					
						
							|  |  |  |     rtcore_config.truncate() | 
					
						
							| 
									
										
										
										
											2021-04-20 18:40:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | os.chdir("..") | 
					
						
							|  |  |  | shutil.rmtree("embree-tmp") | 
					
						
							| 
									
										
										
										
											2021-09-13 15:05:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | subprocess.run(["git", "restore", "embree/patches"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | for patch in os.listdir("embree/patches"): | 
					
						
							| 
									
										
										
										
											2024-02-24 12:40:55 +01:00
										 |  |  |     subprocess.run(["git", "apply", f"embree/patches/{patch}"]) |