• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2This file assembles a toolchain for an amd64 Linux host using the Clang Compiler and glibc.
3
4It downloads the necessary headers, executables, and pre-compiled static/shared libraries to
5the external subfolder of the Bazel cache (the same place third party deps are downloaded with
6http_archive or similar functions in WORKSPACE.bazel). These will be able to be used via our
7custom c++ toolchain configuration (see //toolchain/linux_amd64_toolchain_config.bzl)
8
9Most files are downloaded as .deb files from packages.debian.org (with us acting as the dependency
10resolver) and extracted to
11  [outputRoot (aka Bazel cache)]/[outputUserRoot]/[outputBase]/external/clang_linux_amd64
12  (See https://bazel.build/docs/output_directories#layout-diagram)
13which will act as our sysroot.
14"""
15
16load(":clang_layering_check.bzl", "generate_system_module_map")
17load(":utils.bzl", "gcs_mirror_only", "gcs_mirror_url")
18
19# The clang from CIPD has no prefix, and we download it directly from our GCS bucket
20# This is clang 15.0.1 and iwyu built from source.
21# https://chrome-infra-packages.appspot.com/p/skia/bots/clang_linux/+/5h9JgVTkZk0fFuOyLUCHZXIFqG1b1TAdYG9fHTFLEzoC
22clang_sha256 = "e61f498154e4664d1f16e3b22d4087657205a86d5bd5301d606f5f1d314b133a"
23
24debs_to_install = [
25    # These three comprise glibc. libc6 has the shared libraries, like libc itself, the math library
26    # (libm), etc. linux-libc-dev has the header files specific to linux. libc6-dev has the libc
27    # system headers (e.g. malloc.h, math.h).
28    {
29        # We use this old version of glibc because as of Nov 2022, many of our Swarming machines
30        # are still on Debian 10. While many of the Bazel tasks can be run in RBE, using a newer
31        # Debian 11 image (see //bazel/rbe/gce_linux_container/Dockerfile) some tasks need to be
32        # run on these host machines using Debian 10. As a result, we need to compile and link
33        # against a version of glibc that can be run on Debian 10 until we update those Swarming
34        # hosts.
35        # From https://packages.debian.org/buster/amd64/libc6/download
36        "sha256": "980066e3e6124b8d84cdfd4cfa96d78a97cd659f8f3ba995bbcb887dad9ac237",
37        "url": "https://security.debian.org/debian-security/pool/updates/main/g/glibc/libc6_2.28-10+deb10u2_amd64.deb",
38    },
39    {
40        # From https://packages.debian.org/buster/amd64/linux-libc-dev/download
41        "sha256": "e724656440d71d6316772fe58d7a8ac9634a0060a94af4e3b50e4f0a9e5a75e0",
42        "url": "https://security.debian.org/debian-security/pool/updates/main/l/linux/linux-libc-dev_4.19.260-1_amd64.deb",
43    },
44    {
45        # From https://packages.debian.org/buster/amd64/libc6-dev/download
46        "sha256": "6c11087f5bdc6a2a59fc6424e003dddede53fb97888ade2e35738448fa30a159",
47        "url": "https://security.debian.org/debian-security/pool/updates/main/g/glibc/libc6-dev_2.28-10+deb10u2_amd64.deb",
48    },
49    # These two put the X11 include files in ${PWD}/usr/include/X11
50    # libx11-dev puts libX11.a in ${PWD}/usr/lib/x86_64-linux-gnu
51    {
52        # From https://packages.debian.org/bullseye/amd64/libx11-dev/download
53        "sha256": "11e5f9dcded1a1226b3ee02847b86edce525240367b3989274a891a43dc49f5f",
54        "url": "https://ftp.debian.org/debian/pool/main/libx/libx11/libx11-dev_1.7.2-1_amd64.deb",
55    },
56    {
57        # From https://packages.debian.org/bullseye/amd64/libx11-6/download
58        "sha256": "086bd667fc07369472a923da015d182bb0c15a72228a5c0e6ddbcbeaab70acd2",
59        "url": "https://ftp.debian.org/debian/pool/main/libx/libx11/libx11-6_1.7.2-1_amd64.deb",
60    },
61    {
62        # From https://packages.debian.org/bullseye/all/x11proto-dev/download
63        "sha256": "d5568d587d9ad2664c34c14b0ac538ccb3c567e126ee5291085a8de704a565f5",
64        "url": "https://ftp.debian.org/debian/pool/main/x/xorgproto/x11proto-dev_2020.1-1_all.deb",
65    },
66    # xcb is a dep of X11
67    {
68        # From https://packages.debian.org/bullseye/amd64/libxcb1-dev/download
69        "sha256": "b75544f334c8963b8b7b0e8a88f8a7cde95a714dddbcda076d4beb669a961b58",
70        "url": "https://ftp.debian.org/debian/pool/main/libx/libxcb/libxcb1-dev_1.14-3_amd64.deb",
71    },
72    {
73        # From https://packages.debian.org/bullseye/amd64/libxcb1/download
74        "sha256": "d5e0f047ed766f45eb7473947b70f9e8fddbe45ef22ecfd92ab712c0671a93ac",
75        "url": "https://ftp.debian.org/debian/pool/main/libx/libxcb/libxcb1_1.14-3_amd64.deb",
76    },
77    # Xau is a dep of xcb
78    {
79        # From https://packages.debian.org/bullseye/amd64/libxau-dev/download
80        "sha256": "d1a7f5d484e0879b3b2e8d512894744505e53d078712ce65903fef2ecfd824bb",
81        "url": "https://ftp.debian.org/debian/pool/main/libx/libxau/libxau-dev_1.0.9-1_amd64.deb",
82    },
83    {
84        # From https://packages.debian.org/bullseye/amd64/libxau6/download
85        "sha256": "679db1c4579ec7c61079adeaae8528adeb2e4bf5465baa6c56233b995d714750",
86        "url": "https://ftp.debian.org/debian/pool/main/libx/libxau/libxau6_1.0.9-1_amd64.deb",
87    },
88
89    # Xdmcp is a dep of xcb. libxdmcp-dev provides the the libXdmcp.so symlink (and the
90    # .a if we want to statically include it). libxdmcp6 actually provides the .so file
91    {
92        # https://packages.debian.org/bullseye/amd64/libxdmcp-dev/download
93        "sha256": "c6733e5f6463afd261998e408be6eb37f24ce0a64b63bed50a87ddb18ebc1699",
94        "url": "https://ftp.debian.org/debian/pool/main/libx/libxdmcp/libxdmcp-dev_1.1.2-3_amd64.deb",
95    },
96    {
97        # https://packages.debian.org/bullseye/amd64/libxdmcp6/download
98        "sha256": "ecb8536f5fb34543b55bb9dc5f5b14c9dbb4150a7bddb3f2287b7cab6e9d25ef",
99        "url": "https://ftp.debian.org/debian/pool/main/libx/libxdmcp/libxdmcp6_1.1.2-3_amd64.deb",
100    },
101    # These two put GL include files in ${PWD}/usr/include/GL
102    {
103        # From https://packages.debian.org/bullseye/amd64/libgl-dev/download
104        "sha256": "a6487873f2706bbabf9346cdb190f47f23a1464f31cecf92c363bac37c342f2f",
105        "url": "https://ftp.debian.org/debian/pool/main/libg/libglvnd/libgl-dev_1.3.2-1_amd64.deb",
106    },
107    {
108        # From https://packages.debian.org/bullseye/amd64/libglx-dev/download
109        "sha256": "5a50549948bc4363eab32b1083dad2165402c3628f2ee85e9a32563228cc61c1",
110        "url": "https://ftp.debian.org/debian/pool/main/libg/libglvnd/libglx-dev_1.3.2-1_amd64.deb",
111    },
112    {
113        # From https://packages.debian.org/bullseye/amd64/libglx0/download
114        "sha256": "cb642200f7e28e6dbb4075110a0b441880eeec35c8a00a2198c59c53309e5e17",
115        "url": "https://ftp.debian.org/debian/pool/main/libg/libglvnd/libglx0_1.3.2-1_amd64.deb",
116    },
117    # This provides libGL.so for us to link against.
118    {
119        # From https://packages.debian.org/bullseye/amd64/libgl1/download
120        "sha256": "f300f9610b5f05f1ce566c4095f1bf2170e512ac5d201c40d895b8fce29dec98",
121        "url": "https://ftp.debian.org/debian/pool/main/libg/libglvnd/libgl1_1.3.2-1_amd64.deb",
122    },
123    # This is used by sk_app for Vulkan and Dawn on Unix.
124    {
125        # From https://packages.debian.org/bullseye/amd64/libx11-xcb-dev/download
126        "sha256": "80a2413ace2a0a073f2472059b9e589737cbf8a336fb6862684a5811bf640aa3",
127        "url": "https://ftp.debian.org/debian/pool/main/libx/libx11/libx11-xcb-dev_1.7.2-1_amd64.deb",
128    },
129    {
130        # From https://packages.debian.org/bullseye/amd64/libx11-xcb1/download
131        "sha256": "1f9f2dbe7744a2bb7f855d819f43167df095fe7d5291546bec12865aed045e0c",
132        "url": "https://ftp.debian.org/debian/pool/main/libx/libx11/libx11-xcb1_1.7.2-1_amd64.deb",
133    },
134    {
135        # https://packages.debian.org/bullseye/libfontconfig-dev/download
136        "sha256": "7655d4238ee7e6ced13501006d20986cbf9ff08454a4e502d5aa399f83e28876",
137        "url": "https://ftp.debian.org/debian/pool/main/f/fontconfig/libfontconfig-dev_2.13.1-4.2_amd64.deb",
138    },
139    {
140        # https://packages.debian.org/bullseye/amd64/libfontconfig1/download
141        "sha256": "b92861827627a76e74d6f447a5577d039ef2f95da18af1f29aa98fb96baea4c1",
142        "url": "https://ftp.debian.org/debian/pool/main/f/fontconfig/libfontconfig1_2.13.1-4.2_amd64.deb",
143    },
144    {
145        # https://packages.debian.org/bullseye/libglu1-mesa-dev/download
146        "sha256": "5df6abeedb1f6986cec4b17810ef1a2773a5cd3291544abacc2bf602a9520893",
147        "url": "https://ftp.debian.org/debian/pool/main/libg/libglu/libglu1-mesa-dev_9.0.1-1_amd64.deb",
148    },
149    {
150        # https://packages.debian.org/bullseye/amd64/libglu1-mesa/download
151        "sha256": "479736c235af0537c1af8df4befc32e638a4e979961fdb02f366501298c50526",
152        "url": "https://ftp.debian.org/debian/pool/main/libg/libglu/libglu1-mesa_9.0.1-1_amd64.deb",
153    },
154]
155
156def _download_and_extract_deb(ctx, deb, sha256, prefix, output = ""):
157    """Downloads a debian file and extracts the data into the provided output directory"""
158
159    # https://bazel.build/rules/lib/repository_ctx#download_and_extract
160    # A .deb file has a data.tar.xz and a control.tar.xz, but the important contents
161    # (i.e. the headers or libs) are in the data.tar.xz
162    ctx.download_and_extract(
163        url = gcs_mirror_url(deb, sha256),
164        output = "tmp",
165        sha256 = sha256,
166    )
167
168    # https://bazel.build/rules/lib/repository_ctx#extract
169    ctx.extract(
170        archive = "tmp/data.tar.xz",
171        output = output,
172        stripPrefix = prefix,
173    )
174
175    # Clean up
176    ctx.delete("tmp")
177
178def _download_linux_amd64_toolchain_impl(ctx):
179    # Download the clang toolchain (the extraction can take a while)
180    # https://bazel.build/rules/lib/repository_ctx#download_and_extract
181    ctx.download_and_extract(
182        url = gcs_mirror_only(clang_sha256, ".zip"),
183        output = "",
184        sha256 = clang_sha256,
185    )
186
187    # Extract all the debs into our sysroot. This is very similar to installing them, except their
188    # dependencies are not installed automatically.
189    for deb in debs_to_install:
190        _download_and_extract_deb(
191            ctx,
192            deb["url"],
193            deb["sha256"],
194            ".",
195        )
196
197    # This list of files lines up with _make_default_flags() in linux_amd64_toolchain_config.bzl
198    # It is all locations that our toolchain could find a system header.
199    builtin_include_directories = [
200        "include/c++/v1",
201        "include/x86_64-unknown-linux-gnu/c++/v1",
202        "lib/clang/15.0.1/include",
203        "usr/include",
204        "usr/include/x86_64-linux-gnu",
205    ]
206
207    generate_system_module_map(
208        ctx,
209        module_file = "toolchain_system_headers.modulemap",
210        folders = builtin_include_directories,
211    )
212
213    # Create a BUILD.bazel file that makes the files downloaded into the toolchain visible.
214    # We have separate groups for each task because doing less work (sandboxing fewer files
215    # or uploading less data to RBE) makes compiles go faster. We try to strike a balance
216    # between minimal specifications and not having to edit this file often with our use
217    # of globs.
218    # https://bazel.build/rules/lib/repository_ctx#file
219    ctx.file(
220        "BUILD.bazel",
221        content = """
222# DO NOT EDIT THIS BAZEL FILE DIRECTLY
223# Generated from ctx.file action in download_linux_amd64_toolchain.bzl
224filegroup(
225    name = "generated_module_map",
226    srcs = ["toolchain_system_headers.modulemap"],
227    visibility = ["//visibility:public"],
228)
229
230filegroup(
231    name = "archive_files",
232    srcs = [
233        "bin/llvm-ar",
234    ],
235    visibility = ["//visibility:public"],
236)
237
238filegroup(
239    name = "compile_files",
240    srcs = [
241        "bin/clang",
242        "bin/include-what-you-use",
243    ] + glob(
244        include = [
245            "include/c++/v1/**",
246            "include/x86_64-unknown-linux-gnu/c++/v1/**",
247            "usr/include/**",
248            "lib/clang/15.0.1/**",
249            "usr/include/x86_64-linux-gnu/**",
250        ],
251        allow_empty = False,
252    ),
253    visibility = ["//visibility:public"],
254)
255
256filegroup(
257    name = "link_files",
258    srcs = [
259        "bin/clang",
260        "bin/ld.lld",
261        "bin/lld",
262        "lib/x86_64-unknown-linux-gnu/libc++.a",
263        "lib/x86_64-unknown-linux-gnu/libc++abi.a",
264        "lib/x86_64-unknown-linux-gnu/libunwind.a",
265        "lib64/ld-linux-x86-64.so.2",
266    ] + glob(
267        include = [
268            "lib/clang/15.0.1/lib/**",
269            "lib/x86_64-linux-gnu/**",
270            "usr/lib/x86_64-linux-gnu/**",
271        ],
272        allow_empty = False,
273    ),
274    visibility = ["//visibility:public"],
275)
276""",
277        executable = False,
278    )
279
280# https://bazel.build/rules/repository_rules
281download_linux_amd64_toolchain = repository_rule(
282    implementation = _download_linux_amd64_toolchain_impl,
283    attrs = {},
284    doc = "Downloads clang, and all supporting headers, executables, " +
285          "and shared libraries required to build Skia on a Linux amd64 host",
286)
287