Debian Codegraph @

third_party/dawn/build_dawn.py

raw
1 #!/usr/bin/env python3
2 #
3 # Copyright 2025 Google LLC
4 #
5 # Use of this source code is governed by a BSD-style license that can be
6 # found in the LICENSE file.
7 #
8 """Builds Dawn using CMake.
9
10 This script configures and builds Dawn using CMake and Ninja. It then
11 discovers all object files for the specified libraries and combines them
12 into static archives (.a files) using `ar`. It also copies Dawn's generated
13 headers to the location GN expects.
14 """
15
16 import argparse
17 import os
18 import shutil
19 import subprocess
20 import sys
21
22 from cmake_utils import (add_common_cmake_args, combine_into_library,
23 discover_dependencies, get_cmake_os_cpu,
24 get_windows_settings, quote_if_needed, write_depfile,
25 get_third_party_locations)
26
27
28 def gn_bool_to_cmake(s):
29 if s.lower() == "true":
30 return "ON"
31 return "OFF"
32
33
34 def main():
35 parser = argparse.ArgumentParser(description="Build Dawn using CMake.")
36 add_common_cmake_args(parser)
37 parser.add_argument(
38 "--gen_dir",
39 required=True,
40 help="Directory for generated files (e.g., depfile and headers).")
41 parser.add_argument(
42 "--android_ndk_path", default="", help="Path to the Android NDK.")
43 parser.add_argument(
44 "--android_platform",
45 default="",
46 help="Android platform (e.g., android-29).")
47 parser.add_argument(
48 "--dawn_enable_d3d11", default="false", help="Enable D3D11 backend.")
49 parser.add_argument(
50 "--dawn_enable_d3d12", default="false", help="Enable D3D12 backend.")
51 parser.add_argument(
52 "--dawn_enable_opengles", default="false", help="Enable GLES backend.")
53 parser.add_argument(
54 "--dawn_enable_metal", default="false", help="Enable Metal backend.")
55 parser.add_argument(
56 "--dawn_enable_vulkan", default="false", help="Enable Vulkan backend.")
57 args = parser.parse_args()
58
59 cmake_exe = shutil.which("cmake")
60 if not cmake_exe:
61 print("Error: cmake not found in PATH.")
62 sys.exit(1)
63
64 ninja_exe = shutil.which("ninja")
65 if not ninja_exe:
66 print("Error: ninja not found in PATH.")
67 sys.exit(1)
68
69 target_os, target_cpu = get_cmake_os_cpu(args.target_os, args.target_cpu)
70
71 output_path = args.output_path
72 gen_dir = args.gen_dir
73 # The headers are a dependency for all libraries.
74 # We want to build the other listed dawn components into one big library.
75 build_targets = ["webgpu_headers_gen", "dawn_proc", "dawn_native"]
76 depfile_path = args.depfile_path
77
78 script_dir = os.path.dirname(os.path.realpath(__file__))
79
80 dawn_dir = os.path.join(script_dir, "..", "externals", "dawn")
81 build_dir = args.build_dir
82
83 configure_cmd = [
84 cmake_exe,
85 "-S",
86 dawn_dir,
87 "-B",
88 build_dir,
89 "-G",
90 "Ninja",
91 f"-DCMAKE_MAKE_PROGRAM={ninja_exe}",
92 f"--install-prefix={os.path.abspath(gen_dir)}",
93 f"-DCMAKE_SYSTEM_NAME={target_os}",
94 f"-DCMAKE_SYSTEM_PROCESSOR={target_cpu}",
95 "-DDAWN_BUILD_MONOLITHIC_LIBRARY=OFF",
96 f"-DCMAKE_BUILD_TYPE={args.build_type}",
97 # Explicitly set the C++ standard to avoid issues with CMake's feature
98 # detection for Clang on Windows.
99 "-DCMAKE_CXX_STANDARD=20",
100 "-DCMAKE_CXX_STANDARD_REQUIRED=ON",
101 "-DCMAKE_CXX_EXTENSIONS=OFF",
102 "-DDAWN_FORCE_SYSTEM_COMPONENT_LOAD=ON", # https://g-issues.chromium.org/issues/399358291
103 "-DDAWN_ENABLE_INSTALL=OFF",
104 "-DTINT_ENABLE_INSTALL=OFF",
105 f"-DDAWN_ENABLE_D3D11={gn_bool_to_cmake(args.dawn_enable_d3d11)}",
106 f"-DDAWN_ENABLE_D3D12={gn_bool_to_cmake(args.dawn_enable_d3d12)}",
107 f"-DDAWN_ENABLE_OPENGLES={gn_bool_to_cmake(args.dawn_enable_opengles)}",
108 f"-DDAWN_ENABLE_METAL={gn_bool_to_cmake(args.dawn_enable_metal)}",
109 f"-DDAWN_ENABLE_VULKAN={gn_bool_to_cmake(args.dawn_enable_vulkan)}",
110 ]
111 configure_cmd += get_third_party_locations()
112
113 if args.enable_rtti:
114 configure_cmd.append("-DDAWN_ENABLE_RTTI=ON")
115
116 cxx_flags = args.cxx_flags or []
117 ld_flags = args.ld_flags or []
118
119 if target_os == "Windows":
120 win_cfgs, win_cxx, win_ld = get_windows_settings(args)
121 configure_cmd += win_cfgs
122 cxx_flags += win_cxx
123 ld_flags += win_ld
124
125 # The D3D backend requires the HLSL writer.
126 configure_cmd.append("-DTINT_BUILD_HLSL_WRITER=ON")
127 else:
128 configure_cmd.append("-DTINT_BUILD_HLSL_WRITER=OFF")
129 cxx_flags.append("-w") # Silence warnings
130
131 if cxx_flags:
132 c_cxx_flags_str = " ".join(cxx_flags)
133 # -fno-rtti is not a valid C flag.
134 c_flags = [f for f in cxx_flags if f != "-fno-rtti"]
135 c_flags_str = " ".join(c_flags)
136 configure_cmd.append(f"-DCMAKE_C_FLAGS={c_flags_str}")
137 configure_cmd.append(f"-DCMAKE_CXX_FLAGS={c_cxx_flags_str}")
138
139 if ld_flags:
140 ld_flags_str = " ".join(ld_flags)
141 configure_cmd.append(f"-DCMAKE_EXE_LINKER_FLAGS={ld_flags_str}")
142 configure_cmd.append(f"-DCMAKE_SHARED_LINKER_FLAGS={ld_flags_str}")
143 configure_cmd.append(f"-DCMAKE_MODULE_LINKER_FLAGS={ld_flags_str}")
144
145 if target_os == "Android":
146 configure_cmd.append(f"-DCMAKE_TOOLCHAIN_FILE={args.android_ndk_path}/build/cmake/android.toolchain.cmake")
147 configure_cmd.append(f"-DANDROID_ABI={target_cpu}")
148 configure_cmd.append(f"-DANDROID_PLATFORM={args.android_platform}")
149 else:
150 configure_cmd.append(f"-DCMAKE_C_COMPILER={args.cc.replace(os.sep, '/')}")
151 configure_cmd.append(f"-DCMAKE_CXX_COMPILER={args.cxx.replace(os.sep, '/')}")
152
153 if target_os == "Darwin" or target_os == "iOS":
154 configure_cmd.append(f"-DCMAKE_OSX_ARCHITECTURES={target_cpu}")
155
156 env = os.environ.copy()
157 # Don't write .pyc files, which can cause race conditions when building
158 # tint and dawn in parallel.
159 env["PYTHONDONTWRITEBYTECODE"] = "1"
160
161 if target_os == "Windows":
162 # Set the WINDOWSSDKDIR environment variable to ensure that Dawn's
163 # DetectWindowsSDK function uses the correct, hermetic SDK path.
164 env["WINDOWSSDKDIR"] = args.win_sdk
165
166 print(" ".join(configure_cmd))
167 print("with environment", env)
168 subprocess.run(configure_cmd, check=True, env=env)
169
170 build_cmd = [ninja_exe, "-C", build_dir, "-dkeepdepfile"] + build_targets
171 subprocess.run(build_cmd, check=True, env=env)
172
173 # The install target is broken, so we manually copy the headers.
174 # The headers are located in the build directory in a gen/include folder.
175 # The Skia build expects to find headers in subdirectories of the include path.
176 generated_headers_src = os.path.join(build_dir, "gen", "include")
177 generated_headers_dest = os.path.join(gen_dir, "include")
178
179 if os.path.exists(generated_headers_dest):
180 shutil.rmtree(generated_headers_dest)
181
182 # Copy the contents of the 'dawn' and 'webgpu' directories into the destination.
183 shutil.copytree(
184 os.path.join(generated_headers_src, "dawn"),
185 os.path.join(generated_headers_dest, "dawn"),
186 dirs_exist_ok=True)
187 shutil.copytree(
188 os.path.join(generated_headers_src, "webgpu"),
189 os.path.join(generated_headers_dest, "webgpu"),
190 dirs_exist_ok=True)
191
192 dependencies, object_files = discover_dependencies(build_dir, build_targets)
193 write_depfile(output_path, depfile_path, dependencies)
194
195 combine_into_library(args, output_path, build_dir, target_os, object_files)
196
197 if __name__ == "__main__":
198 main()