xref: /aosp_15_r20/external/skia/toolchain/download_windows_amd64_toolchain.bzl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1"""
2This file assembles a toolchain for a Windows host using the Clang Compiler.
3
4It downloads the necessary executables and creates symlinks in the external subfolder of the Bazel
5cache (the same place third party deps are downloaded with http_archive or similar functions in
6WORKSPACE.bazel). These will be able to be used via our
7custom c++ toolchain configuration (see //toolchain/windows_toolchain_config.bzl)
8
9The destination folder for these files and symlinks are:
10  [outputRoot (aka Bazel cache)]/[outputUserRoot]/[outputBase]/external/clang_windows_amd64
11  (See https://bazel.build/docs/output_directories#layout-diagram)
12"""
13
14load("//bazel:cipd_install.bzl", "cipd_download_urls")
15load(":utils.bzl", "gcs_mirror_url")
16
17# From https://github.com/llvm/llvm-project/releases/tag/llvmorg-18.1.8
18# When updating this, don't forget to use //bazel/gcs_mirror to upload a new version.
19# go run bazel/gcs_mirror/gcs_mirror.go --url [clang_url] --sha256 [clang_sha256]
20# We're using a newer version on Windows than Linux or Mac because major version 18 is the earliest
21# version to include pre-builts for Windows.
22clang_prefix = "clang+llvm-18.1.8-x86_64-pc-windows-msvc"
23clang_sha256 = "22c5907db053026cc2a8ff96d21c0f642a90d24d66c23c6d28ee7b1d572b82e8"
24clang_url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-x86_64-pc-windows-msvc.tar.xz"
25
26win_toolchain_pkg = "skia/bots/win_toolchain"
27win_toolchain_tag = "version:15"
28win_toolchain_sha256 = "f3dfb040052661124470d959faf8350aef4c2da9b396ad191a2c922d7a01c43f"
29
30def _download_windows_amd64_toolchain_impl(ctx):
31    # Download the clang toolchain (the extraction can take a while)
32    # https://bazel.build/rules/lib/repository_ctx#download_and_extract
33    ctx.download_and_extract(
34        url = gcs_mirror_url(clang_url, clang_sha256),
35        output = "",
36        stripPrefix = clang_prefix,
37        sha256 = clang_sha256,
38    )
39
40    # Create a BUILD.bazel file that makes the files necessary for compiling,
41    # linking and creating archive files visible to Bazel.
42    # The smaller the globs are, the more performant the sandboxed builds will be.
43    # Additionally, globs that are too wide can pick up infinite symlink loops,
44    # and be difficult to quash: https://github.com/bazelbuild/bazel/issues/13950
45    # https://bazel.build/rules/lib/repository_ctx#file
46    ctx.file(
47        "BUILD.bazel",
48        content = """
49# DO NOT EDIT THIS BAZEL FILE DIRECTLY
50# Generated from ctx.file action in download_windows_amd64_toolchain.bzl
51load(":vars.bzl", "MSVC_INCLUDE", "MSVC_LIB", "WIN_SDK_INCLUDE", "WIN_SDK_LIB")
52
53filegroup(
54    name = "archive_files",
55    srcs = [
56        "bin/llvm-ar.exe",
57    ],
58    visibility = ["//visibility:public"],
59)
60
61filegroup(
62    name = "compile_files",
63    srcs = [
64        "bin/clang.exe",
65    ] + glob(
66        include = [
67            "lib/clang/18/include/**",
68            MSVC_INCLUDE + "/**",
69            WIN_SDK_INCLUDE + "/**",
70        ],
71        allow_empty = False,
72    ),
73    visibility = ["//visibility:public"],
74)
75
76filegroup(
77    name = "link_files",
78    srcs = [
79        "bin/clang.exe",
80        "bin/ld.lld.exe",
81        "bin/lld.exe",
82    ] + glob(
83        [
84            MSVC_LIB + "/**",
85            WIN_SDK_LIB + "/**",
86        ],
87        allow_empty = False,
88    ),
89    visibility = ["//visibility:public"],
90)
91""",
92        executable = False,
93    )
94
95    # Download the win_toolchain CIPD package (the extraction can take a while).
96    ctx.download_and_extract(
97        url = cipd_download_urls(win_toolchain_pkg, win_toolchain_sha256, win_toolchain_tag),
98        output = "",
99        sha256 = win_toolchain_sha256,
100        type = "zip",
101    )
102
103    # Find the correct version of MSVC and the Windows SDK.
104    if ctx.os.name.lower().find("windows") != -1:
105        msvc_version = ctx.execute(["powershell.exe", "/c", "(Get-ChildItem -Path VC/Tools/MSVC | sort Name | select -Last 1).BaseName"]).stdout.rstrip()
106        win_sdk_version = ctx.execute(["powershell.exe", "/c", "(Get-ChildItem -Path win_sdk/Include | sort Name | select -Last 1).BaseName"]).stdout.rstrip()
107    else:
108        msvc_version = ctx.execute(["bash", "-c", "ls VC/Tools/MSVC | sort | tail -1"]).stdout.rstrip()
109        win_sdk_version = ctx.execute(["bash", "-c", "ls win_sdk/Include | sort | tail -1"]).stdout.rstrip()
110
111    # Write vars.bzl.
112    ctx.file(
113        "vars.bzl",
114        content = """
115MSVC_VERSION = "%s"
116MSVC_INCLUDE = "VC/Tools/MSVC/" + MSVC_VERSION + "/include"
117MSVC_LIB = "VC/Tools/MSVC/" + MSVC_VERSION + "/lib"
118WIN_SDK_VERSION = "%s"
119WIN_SDK_INCLUDE = "win_sdk/Include/" + WIN_SDK_VERSION
120WIN_SDK_LIB = "win_sdk/Lib/" + WIN_SDK_VERSION
121""" % (msvc_version, win_sdk_version),
122        executable = False,
123    )
124
125# https://bazel.build/rules/repository_rules
126download_windows_amd64_toolchain = repository_rule(
127    implementation = _download_windows_amd64_toolchain_impl,
128    attrs = {},
129    doc = "Downloads clang to build Skia with." +
130          " Assumes you have msvc located on your device and have" +
131          " VCToolsInstallDir, WindowsSdkDir, and WindowsSDKVersion in your" +
132          " environment, or have MSVC and Windows SDK installed in a" +
133          " predictable location.",
134)
135