Debian Codegraph @

toolchain/download_mac_toolchain.bzl

raw
1 """
2 This file assembles a toolchain for a Mac host (either M1 or Intel) using the Clang Compiler
3 and a locally-installed XCode.
4
5 It downloads the necessary executables and creates symlinks in the external subfolder of the Bazel
6 cache (the same place third party deps are downloaded with http_archive or similar functions in
7 WORKSPACE.bazel). These will be able to be used via our
8 custom c++ toolchain configuration (see //toolchain/mac_toolchain_config.bzl)
9
10 The destination folder for these files and symlinks are:
11 [outputRoot (aka Bazel cache)]/[outputUserRoot]/[outputBase]/external/clang_mac
12 (See https://bazel.build/docs/output_directories#layout-diagram)
13 """
14
15 load(":clang_layering_check.bzl", "generate_system_module_map")
16
17 # https://chrome-infra-packages.appspot.com/p/skia/bots/clang_mac_intel/+/NL1eTB6Iud2w7mk4TkMXSFpVZmzjF3Ms4BwDE130ZkQC
18 clang_intel_url = "https://chrome-infra-packages.appspot.com/dl/skia/bots/clang_mac_intel/+/version:0"
19 clang_intel_sha256 = "34bd5e4c1e88b9ddb0ee69384e4317485a55666ce317732ce01c03135df46644"
20
21 # https://chrome-infra-packages.appspot.com/p/skia/bots/clang_mac_arm/+/lG-b1HzK2Qbd3ebEJvtaaF6iwbZgpcyu8xTerfoa2jAC
22 clang_arm_url = "https://chrome-infra-packages.appspot.com/dl/skia/bots/clang_mac_arm/+/version:0"
23 clang_arm_sha256 = "946f9bd47ccad906dddde6c426fb5a685ea2c1b660a5ccaef314deadfa1ada30"
24
25 # This should be the same across both arm and intel.
26 clang_ver = "22"
27
28 def _get_system_sdk_path(ctx):
29 res = ctx.execute(["xcrun", "--sdk", "macosx", "--show-sdk-path"])
30 if res.return_code != 0:
31 fail("Error Getting SDK path: " + res.stderr)
32 return res.stdout.rstrip()
33
34 def _delete_macos_sdk_symlinks(ctx):
35 ctx.delete("./symlinks/xcode/MacSDK/usr")
36 ctx.delete("./symlinks/xcode/MacSDK/System/Library/Frameworks")
37
38 def _create_macos_sdk_symlinks(ctx):
39 system_sdk_path = _get_system_sdk_path(ctx)
40
41 # https://bazel.build/rules/lib/actions#symlink
42 ctx.symlink(
43 # from =
44 system_sdk_path + "/usr",
45 # to =
46 "./symlinks/xcode/MacSDK/usr",
47 )
48
49 # It is very important to symlink the frameworks directory to [sysroot]/System/Library/Frameworks
50 # because some Frameworks "re-export" other frameworks. These framework paths are relative to
51 # the sysroot (which on a typical machine is /), and it is difficult to change these paths.
52 # By making the symlinks emulate the original path structure, we can keep those re-exports
53 # from breaking.
54 ctx.symlink(
55 # from =
56 system_sdk_path + "/System/Library/Frameworks",
57 # to =
58 "./symlinks/xcode/MacSDK/System/Library/Frameworks",
59 )
60
61 def _download_mac_toolchain_impl(ctx):
62 # https://bazel.build/rules/lib/repository_ctx
63
64 # Download the clang toolchain (the extraction can take a while)
65 # https://bazel.build/rules/lib/repository_ctx#download_and_extract
66 clang_url = clang_intel_url
67 clang_sha256 = clang_intel_sha256
68 if "arm" in ctx.os.arch or "aarch64" in ctx.os.arch:
69 clang_url = clang_arm_url
70 clang_sha256 = clang_arm_sha256
71 ctx.download_and_extract(
72 url = clang_url,
73 type = "zip",
74 output = "",
75 sha256 = clang_sha256,
76 )
77
78 # Some std library headers use #include_next to include system specific headers, and
79 # some skia source files require Xcode headers when compiling, (see SkTypes.h and look
80 # for TargetedConditionals.h)) All of these are located in Xcode, stopping the Mac
81 # builds from being purely hermetic.
82 # For now, we can grab the user's Xcode path by calling xcode-select and create a symlink in
83 # our toolchain directory to refer to during compilation.
84
85 _delete_macos_sdk_symlinks(ctx)
86 _create_macos_sdk_symlinks(ctx)
87
88 # This list of files lines up with _make_default_flags() in mac_toolchain_config.bzl
89 # It is all locations that our toolchain could find a system header.
90 builtin_include_directories = [
91 "lib/clang/" + clang_ver + "/include",
92 # Frameworks is a symlink, and the trailing slash is intentional
93 # (to ensure traversal in generate_system_module_map.sh's find).
94 "symlinks/xcode/MacSDK/System/Library/Frameworks/",
95 "symlinks/xcode/MacSDK/usr/include",
96 # The C++ standard library headers are here but that's accounted for above.
97 # "symlinks/xcode/MacSDK/usr/include/c++/v1",
98 ]
99
100 generate_system_module_map(
101 ctx,
102 module_file = "toolchain_system_headers.modulemap",
103 folders = builtin_include_directories,
104 )
105
106 # Create a BUILD.bazel file that makes the files necessary for compiling,
107 # linking and creating archive files visible to Bazel.
108 # The smaller the globs are, the more performant the sandboxed builds will be.
109 # Additionally, globs that are too wide can pick up infinite symlink loops,
110 # and be difficult to quash: https://github.com/bazelbuild/bazel/issues/13950
111 # https://bazel.build/rules/lib/repository_ctx#file
112 ctx.file(
113 "BUILD.bazel",
114 content = """
115 # DO NOT EDIT THIS BAZEL FILE DIRECTLY
116 # Generated from ctx.file action in download_mac_toolchain.bzl
117 filegroup(
118 name = "generated_module_map",
119 srcs = ["toolchain_system_headers.modulemap"],
120 visibility = ["//visibility:public"],
121 )
122
123 filegroup(
124 name = "archive_files",
125 srcs = [
126 "bin/llvm-ar",
127 ],
128 visibility = ["//visibility:public"],
129 )
130
131 # Any framework that Skia depends on directly or indirectly needs to be listed here.
132 FRAMEWORK_GLOB = [
133 "symlinks/xcode/MacSDK/System/Library/Frameworks/AppKit.Framework/**",
134 "symlinks/xcode/MacSDK/System/Library/Frameworks/ApplicationServices.Framework/**",
135 "symlinks/xcode/MacSDK/System/Library/Frameworks/AVFAudio.Framework/**",
136 "symlinks/xcode/MacSDK/System/Library/Frameworks/AVFoundation.Framework/**",
137 "symlinks/xcode/MacSDK/System/Library/Frameworks/Carbon.Framework/**",
138 "symlinks/xcode/MacSDK/System/Library/Frameworks/CFNetwork.Framework/**",
139 "symlinks/xcode/MacSDK/System/Library/Frameworks/CloudKit.Framework/**",
140 "symlinks/xcode/MacSDK/System/Library/Frameworks/Cocoa.Framework/**",
141 "symlinks/xcode/MacSDK/System/Library/Frameworks/ColorSync.Framework/**",
142 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreData.Framework/**",
143 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreAudio.Framework/**",
144 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreAudioTypes.Framework/**",
145 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreFoundation.Framework/**",
146 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreGraphics.Framework/**",
147 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreImage.Framework/**",
148 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreLocation.Framework/**",
149 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreMedia.Framework/**",
150 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreMIDI.Framework/**",
151 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreServices.Framework/**",
152 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreText.Framework/**",
153 "symlinks/xcode/MacSDK/System/Library/Frameworks/CoreVideo.Framework/**",
154 "symlinks/xcode/MacSDK/System/Library/Frameworks/DiskArbitration.Framework/**",
155 "symlinks/xcode/MacSDK/System/Library/Frameworks/Foundation.Framework/**",
156 "symlinks/xcode/MacSDK/System/Library/Frameworks/ImageIO.Framework/**",
157 "symlinks/xcode/MacSDK/System/Library/Frameworks/IOKit.Framework/**",
158 "symlinks/xcode/MacSDK/System/Library/Frameworks/IOSurface.Framework/**",
159 "symlinks/xcode/MacSDK/System/Library/Frameworks/Metal.Framework/**",
160 "symlinks/xcode/MacSDK/System/Library/Frameworks/MetalKit.Framework/**",
161 "symlinks/xcode/MacSDK/System/Library/Frameworks/ModelIO.Framework/**",
162 "symlinks/xcode/MacSDK/System/Library/Frameworks/OpenGL.Framework/**",
163 "symlinks/xcode/MacSDK/System/Library/Frameworks/QuartzCore.Framework/**",
164 "symlinks/xcode/MacSDK/System/Library/Frameworks/Security.Framework/**",
165 "symlinks/xcode/MacSDK/System/Library/Frameworks/Symbols.Framework/**",
166 "symlinks/xcode/MacSDK/System/Library/Frameworks/UniformTypeIdentifiers.framework/**",
167 ]
168
169 filegroup(
170 name = "compile_files",
171 srcs = [
172 "bin/clang",
173 ] + glob(
174 include = [
175 "lib/clang/*/include/**",
176 "symlinks/xcode/MacSDK/usr/include/**",
177 ],
178 allow_empty = False,
179 ) + glob(
180 # Frameworks are SDK-dependent, and can vary between releases.
181 # We attempt to capture a super-set.
182 include = FRAMEWORK_GLOB,
183 allow_empty = True,
184 ),
185 visibility = ["//visibility:public"],
186 )
187
188 filegroup(
189 name = "link_files",
190 srcs = [
191 "bin/clang",
192 "bin/ld.lld",
193 "bin/lld",
194 ] + glob(
195 include = [
196 # libc++.tbd and libSystem.tbd live here.
197 "symlinks/xcode/MacSDK/usr/lib/*",
198 ],
199 allow_empty = False,
200 ) + glob(
201 # Frameworks are SDK-dependent, and can vary between releases.
202 # We attempt to capture a super-set.
203 include = FRAMEWORK_GLOB,
204 allow_empty = True,
205 ),
206 visibility = ["//visibility:public"],
207 )
208 """,
209 executable = False,
210 )
211
212 # https://bazel.build/rules/repository_rules
213 download_mac_toolchain = repository_rule(
214 implementation = _download_mac_toolchain_impl,
215 attrs = {},
216 doc = "Downloads clang to build Skia with." +
217 "Assumes you have xcode located on your device and have" +
218 "xcode-select in your $PATH.",
219 )