Debian Codegraph @

third_party/dawn/build_tint.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 Tint using CMake.
9
10 This script configures and builds Tint using CMake and Ninja. It then
11 discovers all object files for the specified libraries and combines them
12 into a static archive (.a [Linux/Mac] or .lib [Windows]).
13 """
14
15 import argparse
16 import os
17 import shutil
18 import subprocess
19 import sys
20
21 from cmake_utils import (
22 add_common_cmake_args, combine_into_library, discover_dependencies,
23 get_cmake_os_cpu, get_windows_settings, quote_if_needed, write_depfile,
24 get_third_party_locations)
25
26
27 def main():
28 parser = argparse.ArgumentParser(description="Build Tint using CMake.")
29 add_common_cmake_args(parser)
30 args = parser.parse_args()
31
32 target_os, target_cpu = get_cmake_os_cpu(args.target_os, args.target_cpu)
33
34 output_path = args.output_path
35 depfile_path = args.depfile_path
36 script_dir = os.path.dirname(os.path.realpath(__file__))
37 dawn_dir = os.path.join(script_dir, "..", "externals", "dawn")
38
39 # The build can be invoked in parallel for different toolchains, so we use
40 # a short, unique build directory.
41 build_dir = args.build_dir
42
43 cmake_exe = shutil.which("cmake")
44 if not cmake_exe:
45 print("Error: cmake not found in PATH.")
46 sys.exit(1)
47
48 ninja_exe = shutil.which("ninja")
49 if not ninja_exe:
50 print("Error: ninja not found in PATH.")
51 sys.exit(1)
52
53 # Configure the project using CMake. Tint is a part of Dawn.
54 # https://github.com/google/dawn/blob/main/docs/quickstart-cmake.md
55 configure_cmd = [
56 cmake_exe,
57 "-S",
58 dawn_dir,
59 "-B",
60 build_dir,
61 f"-DCMAKE_C_COMPILER={args.cc.replace(os.sep, '/')}",
62 f"-DCMAKE_CXX_COMPILER={args.cxx.replace(os.sep, '/')}",
63 f"-DCMAKE_SYSTEM_NAME={target_os}",
64 f"-DCMAKE_SYSTEM_PROCESSOR={target_cpu}",
65 # This is handled by GN
66 "-DDAWN_FETCH_DEPENDENCIES=OFF",
67 "-DDAWN_ENABLE_INSTALL=OFF",
68 f"-DCMAKE_BUILD_TYPE={args.build_type}",
69 # skslc needs WGSL support.
70 "-DTINT_BUILD_WGSL_READER=ON",
71 "-DTINT_BUILD_WGSL_WRITER=ON",
72 "-DTINT_ENABLE_INSTALL=ON",
73 # Use Ninja for faster builds.
74 "-G",
75 "Ninja",
76 f"-DCMAKE_MAKE_PROGRAM={ninja_exe}",
77 "-DDAWN_ENABLE_D3D11=OFF",
78 "-DDAWN_ENABLE_D3D12=OFF",
79 "-DDAWN_ENABLE_METAL=OFF",
80 "-DDAWN_ENABLE_NULL=OFF",
81 "-DDAWN_ENABLE_DESKTOP_GL=OFF",
82 "-DDAWN_ENABLE_OPENGLES=OFF",
83 "-DDAWN_ENABLE_VULKAN=OFF",
84 ]
85 configure_cmd += get_third_party_locations()
86 if args.enable_rtti:
87 configure_cmd.append("-DDAWN_ENABLE_RTTI=ON")
88
89 cxx_flags = args.cxx_flags or []
90 ld_flags = args.ld_flags or []
91
92 if target_os == "Windows":
93 win_cfgs, win_cxx, win_ld = get_windows_settings(args)
94 configure_cmd += win_cfgs
95 cxx_flags += win_cxx
96 ld_flags += win_ld
97
98 if cxx_flags:
99 c_cxx_flags_str = " ".join(cxx_flags)
100 configure_cmd.append(f"-DCMAKE_CXX_FLAGS={c_cxx_flags_str}")
101
102 if ld_flags:
103 ld_flags_str = " ".join(ld_flags)
104 configure_cmd.append(f"-DCMAKE_EXE_LINKER_FLAGS={ld_flags_str}")
105 configure_cmd.append(f"-DCMAKE_SHARED_LINKER_FLAGS={ld_flags_str}")
106 configure_cmd.append(f"-DCMAKE_MODULE_LINKER_FLAGS={ld_flags_str}")
107
108 # Set PYTHONPATH to include Dawn's third_party directory. This is needed
109 # for the generator scripts to find jinja2 and markupsafe as packages.
110 third_party_dir = os.path.abspath(os.path.join(dawn_dir, "third_party"))
111 env = os.environ.copy()
112 env["PYTHONPATH"] = third_party_dir
113 # Don't write .pyc files, which can cause race conditions when building
114 # tint and dawn in parallel.
115 env["PYTHONDONTWRITEBYTECODE"] = "1"
116
117 subprocess.run(configure_cmd, check=True, env=env)
118
119 # These tint targets (and their deps) are what Skia needs to build
120 tint_targets = ["tint_api", "tint_lang_wgsl_reader", "tint_lang_wgsl_writer"]
121
122 build_cmd = [ninja_exe, "-C", build_dir, "-dkeepdepfile"] + tint_targets
123 subprocess.run(build_cmd, check=True, env=env)
124
125 dependencies, object_files = discover_dependencies(build_dir, tint_targets)
126 # Generate the depfile. This lists all source files that the Tint library
127 # depends on. This allows GN to know when to re-run this script.
128 write_depfile(output_path, depfile_path, dependencies)
129
130 # After building, Tint consists of many small object files. For easier
131 # consumption by GN, we combine them into a single archive using ar.
132 combine_into_library(args, output_path, build_dir, target_os, object_files)
133
134
135 if __name__ == "__main__":
136 main()