Debian Codegraph @

bazel/skia_rules.bzl

raw
1 """
2 THIS IS THE EXTERNAL-ONLY VERSION OF THIS FILE. G3 HAS ITS OWN.
3
4 This file contains macros which require no third-party dependencies.
5 Using these where possible makes it easier for clients to use Skia
6 without needing to download a bunch of unnecessary dependencies
7 in their WORKSPACE.bazel file.
8 """
9
10 load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
11 load("@rules_cc//cc:cc_library.bzl", "cc_library")
12 load("@rules_cc//cc:cc_test.bzl", "cc_test")
13 load("@rules_cc//cc:objc_library.bzl", "objc_library")
14 load("@skia_user_config//:copts.bzl", "DEFAULT_COPTS", "DEFAULT_OBJC_COPTS")
15 load("@skia_user_config//:linkopts.bzl", "DEFAULT_LINKOPTS")
16 load(
17 "//bazel:generate_cpp_files_for_headers.bzl",
18 _generate_cpp_files_for_headers = "generate_cpp_files_for_headers",
19 )
20
21 generate_cpp_files_for_headers = _generate_cpp_files_for_headers
22
23 def select_multi(values_map, default_cases = None):
24 """select() but allowing multiple matches of the keys.
25
26 select_multi works around a restriction in native select() that prevents multiple
27 keys from being matched unless one is a strict subset of another. For some features,
28 we allow multiple of that component to be active. For example, with codecs, we let
29 the clients mix and match anywhere from 0 built in codecs to all of them.
30
31 select_multi takes a given map and turns it into several distinct select statements
32 that have the effect of using any values associated with any active keys.
33 For example, if the following parameter is passed in:
34 values_map = {
35 ":alpha": ["apple", "apricot"],
36 ":beta": ["banana"],
37 ":gamma": ["grapefruit"],
38 }
39 it will be unrolled into the following select statements
40 [] + select({
41 ":apple": ["apple", "apricot"],
42 "//conditions:default": [],
43 }) + select({
44 ":beta": ["banana"],
45 "//conditions:default": [],
46 }) + select({
47 ":gamma": ["grapefruit"],
48 "//conditions:default": [],
49 })
50
51 Args:
52 values_map: dictionary of labels to a list of labels, just like select()
53 default_cases: dictionary of labels to a list of labels to be used in the default case.
54 If not provided or a key is not mentioned, an empty list will be used.
55
56 Returns:
57 A list of values that is filled in by the generated select statements.
58 """
59 if len(values_map) == 0:
60 return []
61 rv = []
62 if not default_cases:
63 default_cases = {}
64 for key, value in values_map.items():
65 rv += select({
66 key: value,
67 "//conditions:default": default_cases.get(key, []),
68 })
69 return rv
70
71 def supports_platforms(*platforms):
72 """Convenience macro to set the "target_compatible_with" argument of binary and test targets.
73
74 The example below shows a binary that is compatible with all desktop OSes:
75
76 skia_cc_binary(
77 name = "my_binary",
78 target_compatible_with = supports_platforms(
79 "@platforms//os:linux",
80 "@platforms//os:windows",
81 "@platforms//os:macos",
82 ),
83 ...
84 )
85
86 Args:
87 *platforms: One or more supported platforms, e.g. "@platforms//os:linux".
88 Returns:
89 A select() statement with an empty list for each provided platform, and
90 ["@platforms//:incompatible"] as the default condition.
91 """
92 if len(platforms) == 0:
93 fail("Please provide at least one platform.")
94
95 platforms_map = {
96 "//conditions:default": ["@platforms//:incompatible"],
97 }
98 for platform in platforms:
99 platforms_map[platform] = []
100 return select(platforms_map)
101
102 def skia_cc_binary(name, copts = DEFAULT_COPTS, linkopts = DEFAULT_LINKOPTS, **kwargs):
103 """A wrapper around cc_library for Skia C++ executables (e.g. tools).
104
105 This lets us provide compiler flags (copts) and global linker flags (linkopts) consistently
106 to Skia built executables. These executables are almost always things like dev tools.
107
108 Args:
109 name: the name of the underlying executable.
110 copts: Flags which should be passed to the C++ compiler. By default, we use DEFAULT_COPTS
111 from @skia_user_config//:copts.bzl.
112 linkopts: Global flags which should be passed to the C++ linker. By default, we use
113 DEFAULT_LINKOPTS from @skia_user_config//:linkopts.bzl. Other linker flags will be
114 passed in via deps (see deps_and_linkopts below).
115 **kwargs: All the normal arguments that cc_binary takes.
116 """
117 cc_binary(name = name, copts = copts, linkopts = linkopts, **kwargs)
118
119 def skia_cc_test(name, copts = DEFAULT_COPTS, linkopts = DEFAULT_LINKOPTS, **kwargs):
120 """A wrapper around cc_test for Skia C++ executables (e.g. tests).
121
122 This lets us provide compiler flags (copts) and global linker flags (linkopts) consistently
123 to Skia built executables, that is, tests.
124
125 Args:
126 name: the name of the underlying executable.
127 copts: Flags which should be passed to the C++ compiler. By default, we use DEFAULT_COPTS
128 from @skia_user_config//:copts.bzl.
129 linkopts: Global flags which should be passed to the C++ linker. By default, we use
130 DEFAULT_LINKOPTS from @skia_user_config//:linkopts.bzl. Other linker flags will be
131 passed in via deps (see deps_and_linkopts below).
132 **kwargs: All the normal arguments that cc_binary takes.
133 """
134 cc_test(name = name, copts = copts, linkopts = linkopts, **kwargs)
135
136 def skia_cc_library(name, copts = DEFAULT_COPTS, local_defines = [], **kwargs):
137 """A wrapper around cc_library for Skia C++ libraries.
138
139 This lets us provide compiler flags (copts) consistently to the Skia build. By default,
140 copts do not flow up the dependency stack. Additionally, in G3, this allows us to set
141 some options universally.
142
143 It also lets us easily tweak these settings when being built in G3.
144
145 Third party libraries should *not* use this directly, as there are likely some flags used
146 by Skia (e.g. warnings) that we do not want to have to fix for third party code.
147
148 Args:
149 name: the name of the underlying library.
150 copts: Flags which should be passed to the C++ compiler. By default, we use DEFAULT_COPTS
151 from @skia_user_config//:copts.bzl.
152 local_defines: Defines set when compiling this library, but not dependents. We
153 add a define to all our libraries to correctly export/import symbols.
154 **kwargs: All the normal arguments that cc_library takes.
155 """
156
157 # This allows us to mark APIs as exported when building this
158 # as a library, but the APIs will be marked as an import
159 # (the default) when clients try to use our headers. See SkAPI.h for more.
160 # We have to create a new (mutable) list since if the client passes in a list
161 # it will be immutable ("frozen").
162 ld = []
163 ld.extend(local_defines)
164 ld.append("SKIA_IMPLEMENTATION=1")
165 cc_library(name = name, copts = copts, local_defines = ld, **kwargs)
166
167 def skia_filegroup(**kwargs):
168 """A wrapper around filegroup allowing us to customize visibility in G3."""
169 native.filegroup(**kwargs)
170
171 _COMPILE_HEADERS = (".h", ".hh", ".hpp", "hxx")
172
173 def _headers(files):
174 return [f for f in files if f.endswith(_COMPILE_HEADERS)]
175
176 def skia_objc_library(
177 name,
178 copts = DEFAULT_OBJC_COPTS,
179 hdrs = [],
180 deps = [],
181 srcs = [],
182 non_arc_srcs = [],
183 ios_frameworks = [],
184 mac_frameworks = [],
185 sdk_frameworks = [],
186 **kwargs):
187 """A wrapper around objc_library for Skia Objective C libraries.
188
189 This lets us provide compiler flags (copts) consistently to the Skia build (e.g. //:core)
190 and builds which depend on those targets (e.g. things in //tools or //modules).
191
192 It also lets us easily tweak these settings when being built in G3.
193 Args:
194 name: the name of the underlying target.
195 copts: Flags which should be passed to the C++ compiler. By default, we use
196 DEFAULT_OBJC_COPTS from @skia_user_config//:copts.bzl.
197 deps: https://bazel.build/reference/be/objective-c#objc_library.deps
198 ios_frameworks: A list (not select) of iOS-specific Frameworks.
199 mac_frameworks: A list (not select) of Mac-specific Frameworks.
200 sdk_frameworks: https://bazel.build/reference/be/objective-c#objc_library.sdk_frameworks
201 except this should only be a list, not a select.
202 **kwargs: Normal arguments to objc_library
203
204 We re-implement the non_arc_srcs attribute because at least on a Mac with Bazel 8.2.1 it
205 crashes if we pass non_arc_srcs straight through to the objc_library (explicitly or in kwargs).
206 """
207 if len(ios_frameworks) > 0 or len(mac_frameworks) > 0:
208 sdk_frameworks += select({
209 "@platforms//os:ios": ios_frameworks,
210 "@platforms//os:macos": mac_frameworks,
211 "//conditions:default": [],
212 })
213 if len(non_arc_srcs) > 0:
214 objc_library(
215 name = name + "_non_arc",
216 copts = copts + ["-fno-objc-arc"],
217 deps = deps,
218 # Make sure header files in hdrs and srcs that are required by non_arc_srcs are visible.
219 srcs = non_arc_srcs + _headers(srcs) + hdrs,
220 sdk_frameworks = sdk_frameworks,
221 **kwargs
222 )
223 deps = deps + [name + "_non_arc"]
224 objc_library(
225 name = name,
226 copts = copts,
227 deps = deps,
228 hdrs = hdrs,
229 # Make sure any headers in non_arc_srcs are visible here
230 srcs = srcs + _headers(non_arc_srcs),
231 sdk_frameworks = sdk_frameworks,
232 **kwargs
233 )
234
235 def split_srcs_and_hdrs(name, files, visibility = None):
236 """Take a list of files and creates filegroups for C++ sources and headers.
237
238 The "_srcs" and "_hdrs" filegroups will only be created if there are a non-zero amount
239 of files of both types. Otherwise, it will fail because we do not need the macro.
240
241 Args:
242 name: The prefix of the generated filegroups. One will have the suffix "_srcs" and
243 the other "_hdrs".
244 files: List of file names, e.g. ["SkAAClip.cpp", "SkAAClip.h"]
245 visibility: Optional list of visibility rules
246 """
247 srcs = []
248 hdrs = []
249 for f in files:
250 if f.endswith(".cpp"):
251 srcs.append(f)
252 elif f.endswith(".mm"):
253 srcs.append(f)
254 elif f.endswith(".h"):
255 hdrs.append(f)
256 else:
257 fail("Neither .cpp, .mm, nor .h file " + f)
258
259 if len(srcs) == 0 or len(hdrs) == 0:
260 fail("The list consist of either only source or header files. No need to use this macro.")
261
262 skia_filegroup(
263 name = name + "_srcs",
264 srcs = srcs,
265 visibility = visibility,
266 )
267 skia_filegroup(
268 name = name + "_hdrs",
269 srcs = hdrs,
270 visibility = visibility,
271 )