-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathexperimental_mixed_language_library.bzl
357 lines (310 loc) · 10.8 KB
/
experimental_mixed_language_library.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""experimental_mixed_language_library macro implementation."""
load("@bazel_skylib//lib:paths.bzl", "paths")
load(
"@build_bazel_rules_swift//swift:swift.bzl",
"SwiftInfo",
"swift_library",
)
_CPP_FILE_TYPES = [".cc", ".cpp", ".mm", ".cxx", ".C"]
_NON_CPP_FILE_TYPES = [".m", ".c"]
_ASSEMBLY_FILE_TYPES = [".s", ".S", ".asm"]
_OBJECT_FILE_FILE_TYPES = [".o"]
_HEADERS_FILE_TYPES = [
".h",
".hh",
".hpp",
".ipp",
".hxx",
".h++",
".inc",
".inl",
".tlh",
".tli",
".H",
".hmap",
]
_OBJC_FILE_TYPES = _CPP_FILE_TYPES + \
_NON_CPP_FILE_TYPES + \
_ASSEMBLY_FILE_TYPES + \
_OBJECT_FILE_FILE_TYPES + \
_HEADERS_FILE_TYPES
_SWIFT_FILE_TYPES = [".swift"]
def _module_map_content(
module_name,
hdrs,
textual_hdrs,
swift_generated_header,
module_map_path):
# Up to the execution root
# bazel-out/<platform-config>/bin/<path/to/package>/<target-name>.modulemaps/<module-name>
slashes_count = module_map_path.count("/")
relative_path = "".join(["../"] * slashes_count)
content = "module " + module_name + " {\n"
for hdr in hdrs:
if hdr.extension == "h":
content += " header \"%s%s\"\n" % (relative_path, hdr.path)
for hdr in textual_hdrs:
if hdr.extension == "h":
content += " textual header \"%s%s\"\n" % (relative_path, hdr.path)
content += "\n"
content += " export *\n"
content += "}\n"
# Add a Swift submodule if a Swift generated header exists
if swift_generated_header:
content += "\n"
content += "module " + module_name + ".Swift {\n"
content += " header \"../%s\"\n" % swift_generated_header.basename
content += " requires objc\n"
content += "}\n"
return content
def _umbrella_header_content(hdrs):
# If the platform is iOS, add an import call to `UIKit/UIKit.h` to the top
# of the umbrella header. This allows implicit import of UIKit from Swift.
content = """\
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#if __has_include(<UIKit/UIKit.h>)
#import <UIKit/UIKit.h>
#endif
#endif
"""
for hdr in hdrs:
if hdr.extension == "h":
content += "#import \"%s\"\n" % (hdr.path)
return content
def _module_map_impl(ctx):
outputs = []
hdrs = ctx.files.hdrs
textual_hdrs = ctx.files.textual_hdrs
outputs.extend(hdrs)
outputs.extend(textual_hdrs)
# Find Swift generated header
swift_generated_header = None
for dep in ctx.attr.deps:
if CcInfo in dep:
objc_headers = dep[CcInfo].compilation_context.headers.to_list()
else:
objc_headers = []
for hdr in objc_headers:
if hdr.owner == dep.label:
swift_generated_header = hdr
outputs.append(swift_generated_header)
break
# Write the module map content
if swift_generated_header:
umbrella_header_path = ctx.attr.module_name + ".h"
umbrella_header = ctx.actions.declare_file(umbrella_header_path)
ctx.actions.write(
content = _umbrella_header_content(hdrs),
output = umbrella_header,
)
outputs.append(umbrella_header)
module_map_path = "%s/%s" % (ctx.attr.name, "module.modulemap")
else:
module_map_path = ctx.attr.name + ".modulemap"
module_map = ctx.actions.declare_file(module_map_path)
outputs.append(module_map)
ctx.actions.write(
content = _module_map_content(
module_name = ctx.attr.module_name,
hdrs = hdrs,
textual_hdrs = textual_hdrs,
swift_generated_header = swift_generated_header,
module_map_path = module_map.path,
),
output = module_map,
)
objc_provider = apple_common.new_objc_provider()
compilation_context = cc_common.create_compilation_context(
headers = depset(outputs),
)
cc_info = CcInfo(
compilation_context = compilation_context,
)
return [
DefaultInfo(
files = depset([module_map]),
),
objc_provider,
cc_info,
]
_module_map = rule(
attrs = {
"module_name": attr.string(
mandatory = True,
doc = "The name of the module.",
),
"hdrs": attr.label_list(
allow_files = _HEADERS_FILE_TYPES,
doc = """\
The list of C, C++, Objective-C, and Objective-C++ header files used to
construct the module map.
""",
),
"textual_hdrs": attr.label_list(
allow_files = _HEADERS_FILE_TYPES,
doc = """\
The list of C, C++, Objective-C, and Objective-C++ header files used to
construct the module map. Unlike hdrs, these will be declared as 'textual
header' in the module map.
""",
),
"deps": attr.label_list(
providers = [SwiftInfo],
doc = """\
The list of swift_library targets. A `${module_name}.Swift` submodule will be
generated if non-empty.
""",
),
},
doc = "Generates a module map given a list of header files.",
implementation = _module_map_impl,
provides = [
DefaultInfo,
],
)
def experimental_mixed_language_library(
*,
name,
srcs,
deps = [],
enable_modules = False,
module_name = None,
objc_copts = [],
swift_copts = [],
swiftc_inputs = [],
testonly = False,
**kwargs):
"""Compiles and links Objective-C and Swift code into a static library.
This is an experimental macro that supports compiling mixed Objective-C and
Swift source files into a static library.
Due to the build performance reason, in general it's not recommended to
have mixed Objective-C and Swift modules, but it isn't uncommon to see
mixed language modules in some old codebases. This macro is meant to make
it easier to migrate codebases with mixed language modules to Bazel without
having to demix them first.
This macro only supports a simple use case of mixed language
modules, it does not support header maps.
Args:
name: A unique name for this target.
deps: A list of targets that are dependencies of the target being
built, which will be linked into that target.
enable_modules: Enables clang module support for the Objective-C target.
module_name: The name of the mixed language module being built.
If left unspecified, the module name will be the name of the
target.
objc_copts: Additional compiler options that should be passed to
`clang`.
srcs: The list of Objective-C and Swift source files to compile.
swift_copts: Additional compiler options that should be passed to
`swiftc`. These strings are subject to `$(location ...)` and "Make"
variable expansion.
swiftc_inputs: Additional files that are referenced using
`$(location...)` in `swift_copts`.
testonly: If True, only testonly targets (such as tests) can depend on
this target. Default False.
**kwargs: Other arguments to pass through to the underlying
`objc_library`.
"""
hdrs = kwargs.pop("hdrs", [])
if not srcs:
fail("'srcs' must be non-empty")
swift_srcs = []
objc_srcs = []
private_hdrs = []
for x in srcs:
_, extension = paths.split_extension(x)
if extension in _SWIFT_FILE_TYPES:
swift_srcs.append(x)
elif extension in _OBJC_FILE_TYPES:
objc_srcs.append(x)
if extension in _HEADERS_FILE_TYPES:
private_hdrs.append(x)
if not objc_srcs:
fail("""\
'srcs' must contain Objective-C source files. Use 'swift_library' if this
target only contains Swift files.""")
if not swift_srcs:
fail("""\
'srcs' must contain Swift source files. Use 'objc_library' if this
target only contains Objective-C files.""")
if not module_name:
module_name = name
swift_library_name = name + ".internal.swift"
objc_deps = []
objc_copts = [] + objc_copts
swift_deps = [] + deps
swift_copts = swift_copts + [
"-Xfrontend",
"-enable-objc-interop",
"-import-underlying-module",
]
# Modules is not enabled if a custom module map is present even with
# `enable_modules` set to `True` in `objc_library`. This enables it via copts.
# See: https://github.com/bazelbuild/bazel/issues/20703
if enable_modules:
objc_copts.append("-fmodules")
objc_deps = [":" + swift_library_name]
# Add Obj-C includes to Swift header search paths
repository_name = native.repository_name()
includes = kwargs.get("includes", [])
for x in includes:
include = x if repository_name == "@" else "external/" + repository_name.lstrip("@") + "/" + x
swift_copts += [
"-Xcc",
"-I{}".format(include),
]
# Generate module map for the underlying Obj-C module
objc_module_map_name = name + ".internal.objc"
textual_hdrs = kwargs.get("textual_hdrs", [])
_module_map(
name = objc_module_map_name,
hdrs = hdrs,
module_name = module_name,
textual_hdrs = textual_hdrs,
testonly = testonly,
)
swiftc_inputs = swiftc_inputs + hdrs + textual_hdrs + private_hdrs + [":" + objc_module_map_name]
swift_copts += [
"-Xcc",
"-fmodule-map-file=$(execpath :{})".format(objc_module_map_name),
]
features = kwargs.pop("features", [])
swift_features = features + ["swift.no_generated_module_map"]
swift_library(
name = swift_library_name,
copts = swift_copts,
deps = swift_deps,
features = swift_features,
generated_header_name = module_name + "-Swift.h",
generates_header = True,
module_name = module_name,
srcs = swift_srcs,
swiftc_inputs = swiftc_inputs,
testonly = testonly,
)
umbrella_module_map = name + ".internal.umbrella"
umbrella_module_map_label = ":" + umbrella_module_map
_module_map(
name = umbrella_module_map,
deps = [":" + swift_library_name],
hdrs = hdrs,
module_name = module_name,
testonly = testonly,
)
objc_deps.append(umbrella_module_map_label)
native.objc_library(
name = name,
copts = objc_copts,
deps = objc_deps,
hdrs = hdrs + [
# These aren't headers but here is the only place to declare these
# files as the inputs because objc_library doesn't have an
# attribute to declare custom inputs.
umbrella_module_map_label,
],
module_map = umbrella_module_map_label,
srcs = objc_srcs,
testonly = testonly,
**kwargs
)