Debian Codegraph @

toolchain/ios_toolchain_config.bzl

raw
1 """
2 This file specifies a clang toolchain that can run on a Mac host to build for iOS.
3
4 Hermetic toolchains still need access to Xcode for sys headers included in Skia's codebase.
5
6 See download_ios_toolchain.bzl for more details on the creation of the toolchain.
7
8 It uses the usr subfolder of the built toolchain as a sysroot
9
10 It follows the example of:
11 - linux_amd64_toolchain_config.bzl
12 """
13
14 # https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/action_names.bzl
15 load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
16
17 # https://github.com/bazelbuild/bazel/blob/master/tools/cpp/cc_toolchain_config_lib.bzl
18 load(
19 "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
20 "action_config",
21 "feature",
22 "flag_group",
23 "flag_set",
24 "tool",
25 "variable_with_value",
26 )
27 load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
28 load(":clang_layering_check.bzl", "make_layering_check_features")
29
30 # The location of the created clang toolchain.
31 EXTERNAL_TOOLCHAIN = "external/+_repo_rules+clang_ios"
32
33 # Root of our symlinks. These symlinks are created in download_ios_toolchain.bzl
34 XCODE_IOSSDK_SYMLINK = EXTERNAL_TOOLCHAIN + "/symlinks/xcode/iOSSDK"
35
36 _platform_constraints_to_import = {
37 "@platforms//cpu:arm64": "_arm64_cpu",
38 }
39
40 def _ios_toolchain_info(ctx):
41 action_configs = _make_action_configs()
42 features = []
43 features += _make_default_flags()
44 features += make_layering_check_features()
45 features += _make_diagnostic_flags()
46 features += _make_target_specific_flags(ctx)
47
48 # https://bazel.build/rules/lib/cc_common#create_cc_toolchain_config_info
49 # Note, this rule is defined in Java code, not Starlark
50 # https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcModuleApi.java
51 return cc_common.create_cc_toolchain_config_info(
52 ctx = ctx,
53 features = features,
54 action_configs = action_configs,
55 builtin_sysroot = EXTERNAL_TOOLCHAIN,
56 cxx_builtin_include_directories = [
57 # https://stackoverflow.com/a/61419490
58 # "If the compiler has --sysroot support, then these paths should use %sysroot%
59 # rather than the include path"
60 # https://bazel.build/rules/lib/cc_common#create_cc_toolchain_config_info.cxx_builtin_include_directories
61 "%sysroot%/symlinks/xcode/iOSSDK/System/Library/Frameworks/",
62 ],
63 # If `ctx.attr.cpu` is blank (which is declared as optional below), this config will target
64 # the host CPU. Specifying a target_cpu allows this config to be used for cross compilation.
65 target_cpu = ctx.attr.cpu,
66 # These are required, but do nothing
67 compiler = "",
68 target_libc = "",
69 target_system_name = "",
70 toolchain_identifier = "",
71 )
72
73 def _import_platform_constraints():
74 # In order to "import" constraint values so they can be passed in as parameters to
75 # ctx.target_platform_has_constraint(), we need to list them as a default value on a
76 # private attributes. It doesn't really matter what we call these private attributes,
77 # but to make it easier to read elsewhere, we create a mapping between the "official"
78 # name of the constraints and the private name. Then, we can refer to the official name
79 # without having to remember the secondary name.
80 # https://bazel.build/rules/rules#private_attributes_and_implicit_dependencies
81 # https://github.com/bazelbuild/proposals/blob/91579f36031f768bcf68b18a86b8df8b43cc590b/designs/2019-11-11-target-platform-constraints.md
82 rule_attributes = {}
83 for constraint in _platform_constraints_to_import:
84 private_attr = _platform_constraints_to_import[constraint]
85 rule_attributes[private_attr] = attr.label(default = constraint)
86
87 # Define an optional attribute to allow the target architecture to be explicitly specified (e.g.
88 # when selecting a cross-compilation toolchain).
89 rule_attributes["cpu"] = attr.string(
90 mandatory = False,
91 values = ["arm64"],
92 )
93 return rule_attributes
94
95 def _has_platform_constraint(ctx, official_constraint_name):
96 # ctx is of type https://bazel.build/rules/lib/ctx
97 # This pattern is from
98 # https://github.com/bazelbuild/proposals/blob/91579f36031f768bcf68b18a86b8df8b43cc590b/designs/2019-11-11-target-platform-constraints.md
99 private_attr = _platform_constraints_to_import[official_constraint_name]
100 constraint = getattr(ctx.attr, private_attr)[platform_common.ConstraintValueInfo]
101 return ctx.target_platform_has_constraint(constraint)
102
103 provide_ios_toolchain_config = rule(
104 attrs = _import_platform_constraints(),
105 provides = [CcToolchainConfigInfo],
106 implementation = _ios_toolchain_info,
107 )
108
109 def _make_action_configs():
110 """
111 This function sets up the tools needed to perform the various compile/link actions.
112
113 Bazel normally restricts us to referring to (and therefore running) executables/scripts
114 that are in this directory (That is EXEC_ROOT/toolchain). However, the executables we want
115 to run are brought in via WORKSPACE.bazel and are located in EXEC_ROOT/external/clang....
116 Therefore, we make use of "trampoline scripts" that will call the binaries from the
117 toolchain directory.
118
119 These action_configs also let us dynamically specify arguments from the Bazel
120 environment if necessary (see cpp_link_static_library_action).
121 """
122
123 # https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/cc_toolchain_config_lib.bzl;l=435;drc=3b9e6f201a9a3465720aad8712ab7bcdeaf2e5da
124 clang_tool = tool(path = "ios_trampolines/clang_trampoline_ios.sh")
125 ar_tool = tool(path = "ios_trampolines/ar_trampoline_ios.sh")
126
127 # https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/cc_toolchain_config_lib.bzl;l=488;drc=3b9e6f201a9a3465720aad8712ab7bcdeaf2e5da
128 assemble_action = action_config(
129 action_name = ACTION_NAMES.assemble,
130 tools = [clang_tool],
131 )
132 c_compile_action = action_config(
133 action_name = ACTION_NAMES.c_compile,
134 tools = [clang_tool],
135 )
136 cpp_compile_action = action_config(
137 action_name = ACTION_NAMES.cpp_compile,
138 tools = [clang_tool],
139 )
140 objc_compile_action = action_config(
141 action_name = ACTION_NAMES.objc_compile,
142 tools = [clang_tool],
143 )
144 objcpp_compile_action = action_config(
145 action_name = ACTION_NAMES.objcpp_compile,
146 tools = [clang_tool],
147 )
148 objcpp_exec_action = action_config(
149 action_name = "objc++-executable",
150 tools = [clang_tool],
151 )
152 linkstamp_compile_action = action_config(
153 action_name = ACTION_NAMES.linkstamp_compile,
154 tools = [clang_tool],
155 )
156 preprocess_assemble_action = action_config(
157 action_name = ACTION_NAMES.preprocess_assemble,
158 tools = [clang_tool],
159 )
160
161 cpp_link_dynamic_library_action = action_config(
162 action_name = ACTION_NAMES.cpp_link_dynamic_library,
163 tools = [clang_tool],
164 )
165 cpp_link_executable_action = action_config(
166 action_name = ACTION_NAMES.cpp_link_executable,
167 # Bazel assumes it is talking to clang when building an executable. There are
168 # "-Wl" flags on the command: https://releases.llvm.org/6.0.1/tools/clang/docs/ClangCommandLineReference.html#cmdoption-clang-Wl
169 tools = [clang_tool],
170 )
171 cpp_link_nodeps_dynamic_library_action = action_config(
172 action_name = ACTION_NAMES.cpp_link_nodeps_dynamic_library,
173 tools = [clang_tool],
174 )
175
176 # objc archiver and cpp archiver actions use the same base flags
177 common_archive_flags = [
178 flag_set(
179 flag_groups = [
180 flag_group(
181 # https://llvm.org/docs/CommandGuide/llvm-ar.html
182 # [r]eplace existing files or insert them if they already exist,
183 # [c]reate the file if it doesn't already exist
184 # [s]ymbol table should be added
185 # [D]eterministic timestamps should be used
186 flags = ["rcsD", "%{output_execpath}"],
187 # Despite the name, output_execpath just refers to linker output,
188 # e.g. libFoo.a
189 expand_if_available = "output_execpath",
190 ),
191 ],
192 ),
193 flag_set(
194 flag_groups = [
195 flag_group(
196 iterate_over = "libraries_to_link",
197 flag_groups = [
198 flag_group(
199 flags = ["%{libraries_to_link.name}"],
200 expand_if_equal = variable_with_value(
201 name = "libraries_to_link.type",
202 value = "object_file",
203 ),
204 ),
205 flag_group(
206 flags = ["%{libraries_to_link.object_files}"],
207 iterate_over = "libraries_to_link.object_files",
208 expand_if_equal = variable_with_value(
209 name = "libraries_to_link.type",
210 value = "object_file_group",
211 ),
212 ),
213 ],
214 expand_if_available = "libraries_to_link",
215 ),
216 ],
217 ),
218 flag_set(
219 flag_groups = [
220 flag_group(
221 flags = ["@%{linker_param_file}"],
222 expand_if_available = "linker_param_file",
223 ),
224 ],
225 ),
226 ]
227
228 # This is the same rule as
229 # https://github.com/emscripten-core/emsdk/blob/7f39d100d8cd207094decea907121df72065517e/bazel/emscripten_toolchain/crosstool.bzl#L143
230 # By default, there are no flags or libraries passed to the llvm-ar tool, so
231 # we need to specify them. The variables mentioned by expand_if_available are defined
232 # https://bazel.build/docs/cc-toolchain-config-reference#cctoolchainconfiginfo-build-variables
233 cpp_link_static_library_action = action_config(
234 action_name = ACTION_NAMES.cpp_link_static_library,
235 flag_sets = common_archive_flags,
236 tools = [ar_tool],
237 )
238
239 objc_archive_action = action_config(
240 action_name = ACTION_NAMES.objc_archive,
241 flag_sets = common_archive_flags,
242 tools = [ar_tool],
243 )
244
245 action_configs = [
246 assemble_action,
247 c_compile_action,
248 cpp_compile_action,
249 cpp_link_dynamic_library_action,
250 cpp_link_executable_action,
251 cpp_link_nodeps_dynamic_library_action,
252 cpp_link_static_library_action,
253 linkstamp_compile_action,
254 objc_archive_action,
255 objc_compile_action,
256 objcpp_compile_action,
257 objcpp_exec_action,
258 preprocess_assemble_action,
259 ]
260 return action_configs
261
262 # In addition to pointing the c and cpp compile actions to our toolchain, we also need to set objc
263 # and objcpp action flags as well. We build .m and .mm files with the objc_library rule, which
264 # will use the default toolchain if not specified here.
265 # https://docs.bazel.build/versions/3.3.0/be/objective-c.html#objc_library
266 #
267 # Note: These values must be kept in sync with those defined in cmake_exporter.go.
268 def _make_default_flags():
269 """Here we define the flags for certain actions that are always applied.
270
271 For any flag that might be conditionally applied, it should be defined in //bazel/copts.bzl.
272
273 Flags that are set here will be unconditionally applied to everything we compile with
274 this toolchain, even third_party deps.
275
276 """
277 cxx_compile_includes = flag_set(
278 actions = [
279 ACTION_NAMES.c_compile,
280 ACTION_NAMES.cpp_compile,
281 ACTION_NAMES.objc_compile,
282 ACTION_NAMES.objcpp_compile,
283 ],
284 flag_groups = [
285 flag_group(
286 flags = [
287 # THIS ORDER MATTERS GREATLY. If these are in the wrong order, the
288 # #include_next directives will fail to find the files, causing a compilation
289 # error (or, without -no-canonical-prefixes, a mysterious case where files
290 # are included with an absolute path and fail the build).
291 "-isysroot",
292 XCODE_IOSSDK_SYMLINK,
293 "-isystem",
294 EXTERNAL_TOOLCHAIN + "/include/c++/v1",
295 "-isystem",
296 XCODE_IOSSDK_SYMLINK + "/usr/include",
297 "-isystem",
298 EXTERNAL_TOOLCHAIN + "/lib/clang/15.0.1/include",
299 # Set the framework path to the iOS SDK framework directory. This has
300 # subfolders like OpenGL.framework
301 # We want -iframework so Clang hides diagnostic warnings from those header
302 # files we include. -F does not hide those.
303 "-iframework",
304 XCODE_IOSSDK_SYMLINK + "/System/Library/Frameworks",
305 # We do not want clang to search in absolute paths for files. This makes
306 # Bazel think we are using an outside resource and fail the compile.
307 "-no-canonical-prefixes",
308 ],
309 ),
310 ],
311 )
312
313 cpp_compile_flags = flag_set(
314 actions = [
315 ACTION_NAMES.cpp_compile,
316 ACTION_NAMES.objc_compile,
317 ACTION_NAMES.objcpp_compile,
318 ],
319 flag_groups = [
320 flag_group(
321 flags = [
322 "-std=c++20",
323 "-fno-aligned-allocation",
324 ],
325 ),
326 ],
327 )
328
329 # copts and defines appear to not automatically be set
330 # https://bazel.build/docs/cc-toolchain-config-reference#cctoolchainconfiginfo-build-variables
331 # https://github.com/bazelbuild/bazel/blob/5ad4a6126be2bdc53ee7e2457e076c90efe86d56/tools/cpp/cc_toolchain_config_lib.bzl#L200-L209
332 objc_compile_flags = flag_set(
333 actions = [
334 ACTION_NAMES.objc_compile,
335 ACTION_NAMES.objcpp_compile,
336 ],
337 flag_groups = [
338 flag_group(
339 iterate_over = "user_compile_flags",
340 flags = ["%{user_compile_flags}"],
341 ),
342 flag_group(
343 iterate_over = "preprocessor_defines",
344 flags = ["-D%{preprocessor_defines}"],
345 ),
346 ],
347 )
348
349 link_exe_flags = flag_set(
350 actions = [
351 ACTION_NAMES.cpp_link_executable,
352 ACTION_NAMES.cpp_link_dynamic_library,
353 ACTION_NAMES.cpp_link_nodeps_dynamic_library,
354 ],
355 flag_groups = [
356 flag_group(
357 flags = [
358 # lld goes through dynamic library dependencies for dylib and tbh files through
359 # absolute paths (/System/Library/Frameworks). However, the dependencies live in
360 # [Xcode dir]/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks
361 # -Wl tells clang to forward the next flag to the linker.
362 # -syslibroot appends to the beginning of the dylib dependency path.
363 # https://github.com/llvm/llvm-project/blob/d61341768cf0cff7ceeaddecc2f769b5c1b901c4/lld/MachO/InputFiles.cpp#L1418-L1420
364 "-Wl,-syslibroot",
365 XCODE_IOSSDK_SYMLINK,
366 # This path is relative to the syslibroot above, and we want lld to look in the
367 # Frameworks symlink that was created in download_ios_toolchain.bzl.
368 "-F/System/Library/Frameworks",
369 "-fuse-ld=lld",
370 "-std=c++20",
371 EXTERNAL_TOOLCHAIN + "/lib/libc++.a",
372 EXTERNAL_TOOLCHAIN + "/lib/libc++abi.a",
373 EXTERNAL_TOOLCHAIN + "/lib/libunwind.a",
374 ],
375 ),
376 ],
377 )
378
379 return [feature(
380 "default_flags",
381 enabled = True,
382 flag_sets = [
383 cpp_compile_flags,
384 cxx_compile_includes,
385 link_exe_flags,
386 objc_compile_flags,
387 ],
388 )]
389
390 def _make_diagnostic_flags():
391 """Here we define the flags that can be turned on via features to yield debug info."""
392 cxx_diagnostic = flag_set(
393 actions = [
394 ACTION_NAMES.c_compile,
395 ACTION_NAMES.cpp_compile,
396 ],
397 flag_groups = [
398 flag_group(
399 flags = [
400 "--trace-includes",
401 "-v",
402 ],
403 ),
404 ],
405 )
406
407 link_diagnostic = flag_set(
408 actions = [ACTION_NAMES.cpp_link_executable],
409 flag_groups = [
410 flag_group(
411 flags = [
412 "-Wl,--verbose",
413 "-v",
414 ],
415 ),
416 ],
417 )
418
419 link_search_dirs = flag_set(
420 actions = [ACTION_NAMES.cpp_link_executable],
421 flag_groups = [
422 flag_group(
423 flags = [
424 "--print-search-dirs",
425 ],
426 ),
427 ],
428 )
429 return [
430 # Running a Bazel command with --features diagnostic will cause the compilation and
431 # link steps to be more verbose.
432 feature(
433 "diagnostic",
434 enabled = False,
435 flag_sets = [
436 cxx_diagnostic,
437 link_diagnostic,
438 ],
439 ),
440 feature(
441 "link_diagnostic",
442 enabled = False,
443 flag_sets = [
444 link_diagnostic,
445 ],
446 ),
447 # Running a Bazel command with --features print_search_dirs will cause the link to fail
448 # but directories searched for libraries, etc will be displayed.
449 feature(
450 "print_search_dirs",
451 enabled = False,
452 flag_sets = [
453 link_search_dirs,
454 ],
455 ),
456 ]
457
458 # The parameter is of type https://bazel.build/rules/lib/ctx
459 def _make_target_specific_flags(ctx):
460 ios_sim_target = flag_set(
461 actions = [
462 ACTION_NAMES.assemble,
463 ACTION_NAMES.preprocess_assemble,
464 ACTION_NAMES.c_compile,
465 ACTION_NAMES.cpp_compile,
466 ACTION_NAMES.objc_compile,
467 ACTION_NAMES.objcpp_compile,
468 ACTION_NAMES.cpp_link_executable,
469 ACTION_NAMES.cpp_link_dynamic_library,
470 ],
471 flag_groups = [
472 flag_group(
473 flags = [
474 "--target=arm64-apple-ios14.5-simulator",
475 ],
476 ),
477 ],
478 )
479
480 target_specific_features = []
481 if _has_platform_constraint(ctx, "@platforms//cpu:arm64"):
482 target_specific_features.append(
483 feature(
484 name = "_ios_sim_target",
485 enabled = True,
486 flag_sets = [ios_sim_target],
487 ),
488 )
489
490 return target_specific_features