• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- bazel-starlark -*-
2# Copyright 2023 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Siso configuration for clang."""
6
7load("@builtin//path.star", "path")
8load("@builtin//struct.star", "module")
9load("./ar.star", "ar")
10load("./config.star", "config")
11load("./mac_sdk.star", "mac_sdk")
12load("./win_sdk.star", "win_sdk")
13
14def __filegroups(ctx):
15    fg = {
16        "third_party/libc++/src/include:headers": {
17            "type": "glob",
18            "includes": ["*"],
19            # can't use "*.h", because c++ headers have no extension.
20        },
21        "third_party/libc++abi/src/include:headers": {
22            "type": "glob",
23            "includes": ["*.h"],
24        },
25        # vendor provided headers for libc++.
26        "buildtools/third_party/libc++:headers": {
27            "type": "glob",
28            "includes": [
29                "__*",
30            ],
31        },
32
33        # toolchain root
34        # :headers for compiling
35        "third_party/llvm-build/Release+Asserts:headers": {
36            "type": "glob",
37            "includes": [
38                "*.h",
39                "*.modulemap",
40                "bin/clang",
41                "bin/clang++",
42                "bin/clang-*",  # clang-cl, clang-<ver>
43                "*_ignorelist.txt",
44                # https://crbug.com/335997052
45                "clang_rt.profile*.lib",
46            ],
47        },
48        "third_party/cronet_android_mainline_clang/linux-amd64:headers": {
49            "type": "glob",
50            "includes": [
51                "*.h",
52                "*.modulemap",
53                "bin/clang*",
54            ],
55        },
56        "third_party/cronet_android_mainline_clang/linux-amd64:link": {
57            "type": "glob",
58            "includes": [
59                "bin/clang*",
60                "bin/ld.lld",
61                "bin/lld",
62                "bin/llvm-nm",
63                "bin/llvm-objcopy",
64                "bin/llvm-readelf",
65                "bin/llvm-readobj",
66                "bin/llvm-strip",
67                "*.so",
68                "*.so.*",
69                "*.a",
70            ],
71        },
72    }
73    if win_sdk.enabled(ctx):
74        fg.update(win_sdk.filegroups(ctx))
75    if mac_sdk.enabled(ctx):
76        fg.update(mac_sdk.filegroups(ctx))
77    return fg
78
79__input_deps = {
80    # need this because we use
81    # third_party/libc++/src/include:headers,
82    # but scandeps doesn't scan `__config` file, which uses
83    # `#include <__config_site>`
84    # also need `__assertion_handler`. b/321171148
85    "third_party/libc++/src/include": [
86        "buildtools/third_party/libc++:headers",
87    ],
88    "third_party/llvm-build/Release+Asserts/bin/clang": [
89        "build/config/unsafe_buffers_paths.txt",
90    ],
91    "third_party/llvm-build/Release+Asserts/bin/clang++": [
92        "build/config/unsafe_buffers_paths.txt",
93    ],
94    "third_party/llvm-build/Release+Asserts/bin/clang-cl": [
95        "build/config/unsafe_buffers_paths.txt",
96    ],
97    "third_party/llvm-build/Release+Asserts/bin/clang-cl.exe": [
98        "build/config/unsafe_buffers_paths.txt",
99    ],
100    "third_party/llvm-build/Release+Asserts/bin/lld-link": [
101        "build/config/c++/libc++.natvis",
102        "build/win/as_invoker.manifest",
103        "build/win/common_controls.manifest",
104        "build/win/compatibility.manifest",
105        "build/win/require_administrator.manifest",
106        "build/win/segment_heap.manifest",
107        "remoting/host/win/dpi_aware.manifest",
108        "third_party/llvm-build/Release+Asserts/bin/lld",
109        "tools/win/DebugVisualizers/blink.natvis",
110        "tools/win/DebugVisualizers/chrome.natvis",
111    ],
112    "third_party/llvm-build/Release+Asserts/bin/lld-link.exe": [
113        "build/config/c++/libc++.natvis",
114        "build/win/as_invoker.manifest",
115        "build/win/common_controls.manifest",
116        "build/win/compatibility.manifest",
117        "build/win/require_administrator.manifest",
118        "build/win/segment_heap.manifest",
119        "remoting/host/win/dpi_aware.manifest",
120        "third_party/llvm-build/Release+Asserts/bin/lld.exe",
121        "tools/win/DebugVisualizers/blink.natvis",
122        "tools/win/DebugVisualizers/chrome.natvis",
123    ],
124    "build/toolchain/gcc_solink_wrapper.py": [
125        "build/toolchain/whole_archive.py",
126        "build/toolchain/wrapper_utils.py",
127    ],
128    "build/toolchain/gcc_solink_wrapper.py:link": [
129        "build/toolchain/gcc_solink_wrapper.py",
130        "build/toolchain/whole_archive.py",
131        "build/toolchain/wrapper_utils.py",
132    ],
133    "build/toolchain/gcc_link_wrapper.py": [
134        "build/toolchain/whole_archive.py",
135        "build/toolchain/wrapper_utils.py",
136    ],
137    "build/toolchain/gcc_link_wrapper.py:link": [
138        "build/toolchain/gcc_link_wrapper.py",
139        "build/toolchain/whole_archive.py",
140        "build/toolchain/wrapper_utils.py",
141    ],
142    "build/toolchain/apple/linker_driver.py:link": [
143        "build/toolchain/apple/linker_driver.py",
144        "build/toolchain/whole_archive.py",
145    ],
146    "build/toolchain/apple/solink_driver.py:link": [
147        "build/toolchain/apple/linker_driver.py",
148        "build/toolchain/apple/solink_driver.py",
149        "build/toolchain/whole_archive.py",
150    ],
151}
152
153def __lld_link(ctx, cmd):
154    # Replace thin archives with /start-lib ... /end-lib in rsp file.
155    new_lines = []
156    for line in str(cmd.rspfile_content).split("\n"):
157        new_elems = []
158        for elem in line.split(" "):
159            # Parse only .lib files.
160            if not elem.endswith(".lib"):
161                new_elems.append(elem)
162                continue
163
164            # Parse files under the out dir.
165            fname = ctx.fs.canonpath(elem)
166            if not ctx.fs.exists(fname):
167                new_elems.append(elem)
168                continue
169
170            # Check if the library is generated or not.
171            # The source libs are not under the build dir.
172            build_dir = ctx.fs.canonpath("./")
173            if path.rel(build_dir, fname).startswith("../../"):
174                new_elems.append(elem)
175                continue
176
177            ents = ar.entries(ctx, fname, build_dir)
178            if not ents:
179                new_elems.append(elem)
180                continue
181
182            new_elems.append("-start-lib")
183            new_elems.extend(ents)
184            new_elems.append("-end-lib")
185        new_lines.append(" ".join(new_elems))
186
187    ctx.actions.fix(rspfile_content = "\n".join(new_lines))
188
189def __thin_archive(ctx, cmd):
190    # TODO: This handler can be used despite remote linking?
191    if not config.get(ctx, "remote-link"):
192        return
193    if "lld-link" in cmd.args[0]:
194        if not "/llvmlibthin" in cmd.args:
195            print("not thin archive")
196            return
197    else:
198        # check command line to see "-T" and "-S".
199        # rm -f obj/third_party/angle/libangle_common.a && "../../third_party/llvm-build/Release+Asserts/bin/llvm-ar" -T -S -r -c -D obj/third_party/angle/libangle_common.a @"obj/third_party/angle/libangle_common.a.rsp"
200        if not ("-T" in cmd.args[-1] and "-S" in cmd.args[-1]):
201            print("not thin archive without symbol table")
202            return
203
204    # create thin archive without symbol table by handler.
205    rspfile_content = str(cmd.rspfile_content)
206    inputs = []
207    for line in rspfile_content.split("\n"):
208        for fname in line.split(" "):
209            inputs.append(ctx.fs.canonpath(fname))
210    data = ar.create(ctx, path.dir(cmd.outputs[0]), inputs)
211    ctx.actions.write(cmd.outputs[0], data)
212    ctx.actions.exit(exit_status = 0)
213
214__handlers = {
215    "lld_link": __lld_link,
216    "lld_thin_archive": __thin_archive,
217}
218
219clang_all = module(
220    "clang_all",
221    filegroups = __filegroups,
222    input_deps = __input_deps,
223    handlers = __handlers,
224)
225