-
Notifications
You must be signed in to change notification settings - Fork 2
/
objc.bzl
136 lines (129 loc) · 3.94 KB
/
objc.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
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@co_znly_rules_gomobile//:common.bzl", "slug")
load("@co_znly_rules_gomobile//:gobind_library.bzl", "gobind_library")
load("@co_znly_rules_gomobile//:providers.bzl", "GoBindInfo")
load("@co_znly_rules_gomobile//platform:transitions.bzl", "go_platform_transition")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_path")
_MODULE_MAP_TPL = """\
module {name} {{
export *
{headers}
}}
"""
def _normalize_module_name(label):
parts = [
s
for s in [
label.workspace_name,
label.package,
label.name,
]
if s
]
return "_".join(parts).replace("/", "_").replace(".", "_")
def _create_module_map(ctx, gobind_info):
module_name = _normalize_module_name(ctx.label)
headers = "\n".join([
"header \"./%s\"" % paths.relativize(hdr.short_path, ctx.label.package)
for hdr in gobind_info.objc
])
f = ctx.actions.declare_file("module.modulemap")
ctx.actions.write(f, _MODULE_MAP_TPL.format(
name = module_name,
headers = headers,
))
return f
def _gobind_ios_artifacts_impl(ctx):
gobind_info = ctx.attr.gobind[GoBindInfo]
return [
OutputGroupInfo(
hdrs = depset(gobind_info.objc),
),
apple_common.new_objc_provider(
header = depset(gobind_info.objc),
imported_library = depset(ctx.files.binary),
force_load_library = depset(ctx.files.binary),
include = depset(["."]),
module_map = depset([_create_module_map(ctx, gobind_info)]),
),
]
gobind_ios_artifacts = rule(
_gobind_ios_artifacts_impl,
attrs = {
"binary": attr.label(
allow_single_file = True,
mandatory = True,
),
"gobind": attr.label(
mandatory = True,
providers = [GoBindInfo],
),
"_whitelist_function_transition": attr.label(
default = "@bazel_tools//tools/whitelists/function_transition_whitelist",
),
},
cfg = go_platform_transition,
)
def gobind_objc(name, deps, objc_prefix, tags, **kwargs):
gopath_name = slug(name, "objc", "gopath")
gobind_name = slug(name, "objc", "gobind")
binary_name = slug(name, "objc", "binary")
artifacts_name = slug(name, "objc", "artifacts")
objc_library_name = slug(name, "objc")
objc_library_hdrs_name = slug(objc_library_name, "hdrs")
go_path(
name = gopath_name,
mode = "link",
include_pkg = True,
include_transitive = False,
linkmode = "c-archive",
deps = deps + [
"@org_golang_x_mobile//bind:go_default_library",
"@org_golang_x_mobile//bind/objc:go_default_library",
],
)
gobind_library(
name = gobind_name,
go_path = gopath_name,
lang = ["go", "objc"],
objc_prefix = objc_prefix,
copts = [
"-xobjective-c",
"-fmodules",
"-fobjc-arc",
"-D__GOBIND_DARWIN__",
],
go_tags = tags + ["ios"],
deps = deps,
)
copts = kwargs.pop("copts", []) + [
"-xobjective-c",
"-fmodules",
"-fobjc-arc",
"-D__GOBIND_DARWIN__",
]
go_binary(
name = binary_name,
embed = [gobind_name],
out = binary_name + ".a",
deps = deps + [
"@org_golang_x_mobile//bind/java:go_default_library",
"@org_golang_x_mobile//bind/seq:go_default_library",
],
copts = copts,
pure = "off",
linkmode = "c-archive",
**kwargs
)
gobind_ios_artifacts(
name = objc_library_name,
gobind = gobind_name,
binary = binary_name,
visibility = ["//visibility:public"],
)
native.filegroup(
name = objc_library_hdrs_name,
srcs = [objc_library_name],
output_group = "hdrs",
visibility = ["//visibility:public"],
)