Skip to content

Commit 995ff72

Browse files
hvadehracopybara-github
authored andcommitted
Fork objc_common.bzl
PiperOrigin-RevId: 791215594 Change-Id: I591ff202781aeea388cc1613f8419a114bd3a206
1 parent 42f3ee4 commit 995ff72

File tree

2 files changed

+314
-0
lines changed

2 files changed

+314
-0
lines changed
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
# Copyright 2020 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Common functionality for Objc rules."""
16+
17+
load("//cc/common:cc_common.bzl", "cc_common")
18+
load("//cc/common:cc_info.bzl", "CcInfo")
19+
load(":objc_compilation_context_info.bzl", "ObjcCompilationContextInfo")
20+
21+
_ObjcInfo = apple_common.Objc
22+
_apple_toolchain = apple_common.apple_toolchain()
23+
24+
CPP_SOURCES = [".cc", ".cpp", ".mm", ".cxx", ".C"]
25+
NON_CPP_SOURCES = [".m", ".c"]
26+
ASSEMBLY_SOURCES = [".s", ".S", ".asm"]
27+
OBJECT_FILE_SOURCES = [".o"]
28+
HEADERS = [".h", ".inc", ".hpp", ".hh"]
29+
30+
COMPILABLE_SRCS = CPP_SOURCES + NON_CPP_SOURCES + ASSEMBLY_SOURCES
31+
SRCS = COMPILABLE_SRCS + OBJECT_FILE_SOURCES + HEADERS
32+
NON_ARC_SRCS = [".m", ".mm"]
33+
34+
ios_cpus = struct(
35+
IOS_SIMULATOR_TARGET_CPUS = ["ios_x86_64", "ios_i386", "ios_sim_arm64"],
36+
IOS_DEVICE_TARGET_CPUS = ["ios_armv6", "ios_arm64", "ios_armv7", "ios_armv7s", "ios_arm64e"],
37+
VISIONOS_SIMULATOR_TARGET_CPUS = ["visionos_sim_arm64"],
38+
VISIONOS_DEVICE_TARGET_CPUS = ["visionos_arm64"],
39+
WATCHOS_SIMULATOR_TARGET_CPUS = ["watchos_i386", "watchos_x86_64", "watchos_arm64"],
40+
WATCHOS_DEVICE_TARGET_CPUS = ["watchos_armv7k", "watchos_arm64_32", "watchos_device_arm64", "watchos_device_arm64e"],
41+
TVOS_SIMULATOR_TARGET_CPUS = ["tvos_x86_64", "tvos_sim_arm64"],
42+
TVOS_DEVICE_TARGET_CPUS = ["tvos_arm64"],
43+
CATALYST_TARGET_CPUS = ["catalyst_x86_64"],
44+
MACOS_TARGET_CPUS = ["darwin_x86_64", "darwin_arm64", "darwin_arm64e"],
45+
)
46+
47+
extensions = struct(
48+
CPP_SOURCES = CPP_SOURCES,
49+
NON_CPP_SOURCES = NON_CPP_SOURCES,
50+
ASSEMBLY_SOURCES = ASSEMBLY_SOURCES,
51+
HEADERS = HEADERS,
52+
SRCS = SRCS,
53+
NON_ARC_SRCS = NON_ARC_SRCS,
54+
)
55+
56+
def _create_context_and_provider(
57+
ctx,
58+
compilation_attributes,
59+
compilation_artifacts,
60+
intermediate_artifacts,
61+
has_module_map,
62+
deps,
63+
implementation_deps,
64+
attr_linkopts,
65+
direct_cc_compilation_contexts = [],
66+
includes = []):
67+
objc_providers = []
68+
cc_compilation_contexts = []
69+
cc_linking_contexts = []
70+
71+
for dep in deps:
72+
if _ObjcInfo in dep:
73+
objc_providers.append(dep[_ObjcInfo])
74+
75+
if CcInfo in dep:
76+
cc_compilation_contexts.append(dep[CcInfo].compilation_context)
77+
cc_linking_contexts.append(dep[CcInfo].linking_context)
78+
79+
implementation_cc_compilation_contexts = []
80+
for impl_dep in implementation_deps:
81+
implementation_cc_compilation_contexts.append(impl_dep[CcInfo].compilation_context)
82+
cc_linking_contexts.append(impl_dep[CcInfo].linking_context)
83+
84+
sdk_linking_info = {
85+
"sdk_dylib": [],
86+
"sdk_framework": [],
87+
"weak_sdk_framework": [],
88+
}
89+
90+
# buildifier: disable=unsorted-dict-items
91+
objc_provider_kwargs = {
92+
"providers": objc_providers,
93+
"umbrella_header": [],
94+
"module_map": [],
95+
"source": [],
96+
}
97+
98+
# buildifier: disable=unsorted-dict-items
99+
objc_compilation_context_kwargs = {
100+
"providers": objc_providers,
101+
"cc_compilation_contexts": cc_compilation_contexts,
102+
"implementation_cc_compilation_contexts": implementation_cc_compilation_contexts,
103+
"public_hdrs": [],
104+
"private_hdrs": [],
105+
"public_textual_hdrs": [],
106+
"defines": [],
107+
"includes": list(includes),
108+
"direct_cc_compilation_contexts": direct_cc_compilation_contexts,
109+
}
110+
111+
all_non_sdk_linkopts = []
112+
non_sdk_linkopts = _add_linkopts(
113+
sdk_linking_info,
114+
cc_common.objc_expand_and_tokenize(ctx = ctx, attr = "linkopts", flags = attr_linkopts),
115+
)
116+
all_non_sdk_linkopts.extend(non_sdk_linkopts)
117+
118+
if compilation_attributes != None:
119+
sdk_dir = _apple_toolchain.sdk_dir()
120+
usr_include_dir = sdk_dir + "/usr/include/"
121+
sdk_includes = []
122+
123+
for sdk_include in compilation_attributes.sdk_includes.to_list():
124+
sdk_includes.append(usr_include_dir + sdk_include)
125+
126+
sdk_linking_info["sdk_framework"].extend(
127+
compilation_attributes.sdk_frameworks.to_list(),
128+
)
129+
sdk_linking_info["weak_sdk_framework"].extend(
130+
compilation_attributes.weak_sdk_frameworks.to_list(),
131+
)
132+
sdk_linking_info["sdk_dylib"].extend(compilation_attributes.sdk_dylibs.to_list())
133+
134+
objc_compilation_context_kwargs["public_hdrs"].extend(compilation_attributes.hdrs.to_list())
135+
objc_compilation_context_kwargs["public_textual_hdrs"].extend(
136+
compilation_attributes.textual_hdrs.to_list(),
137+
)
138+
objc_compilation_context_kwargs["defines"].extend(compilation_attributes.defines)
139+
objc_compilation_context_kwargs["includes"].extend(sdk_includes)
140+
141+
if compilation_artifacts != None:
142+
all_sources = _filter_out_by_extension(compilation_artifacts.srcs, OBJECT_FILE_SOURCES) + \
143+
compilation_artifacts.non_arc_srcs
144+
145+
objc_provider_kwargs["source"].extend(all_sources)
146+
147+
objc_compilation_context_kwargs["public_hdrs"].extend(
148+
compilation_artifacts.additional_hdrs,
149+
)
150+
objc_compilation_context_kwargs["private_hdrs"].extend(
151+
_filter_by_extension(compilation_artifacts.srcs, HEADERS),
152+
)
153+
154+
if has_module_map:
155+
module_map = intermediate_artifacts.swift_module_map()
156+
objc_provider_kwargs["module_map"].append(module_map.file())
157+
158+
objc_provider_kwargs_built = {}
159+
for k, v in objc_provider_kwargs.items():
160+
if k == "providers":
161+
objc_provider_kwargs_built[k] = v
162+
else:
163+
objc_provider_kwargs_built[k] = depset(v)
164+
165+
objc_compilation_context = ObjcCompilationContextInfo(
166+
**objc_compilation_context_kwargs
167+
)
168+
169+
all_linkopts = all_non_sdk_linkopts
170+
for sdk_framework in depset(sdk_linking_info["sdk_framework"]).to_list():
171+
all_linkopts.append("-framework")
172+
all_linkopts.append(sdk_framework)
173+
174+
for weak_sdk_framework in depset(sdk_linking_info["weak_sdk_framework"]).to_list():
175+
all_linkopts.append("-weak_framework")
176+
all_linkopts.append(weak_sdk_framework)
177+
178+
for sdk_dylib in depset(sdk_linking_info["sdk_dylib"]).to_list():
179+
if sdk_dylib.startswith("lib"):
180+
sdk_dylib = sdk_dylib[3:]
181+
all_linkopts.append("-l%s" % sdk_dylib)
182+
183+
objc_linking_context = struct(
184+
cc_linking_contexts = cc_linking_contexts,
185+
linkopts = all_linkopts,
186+
)
187+
188+
return (
189+
_ObjcInfo(**objc_provider_kwargs_built),
190+
objc_compilation_context,
191+
objc_linking_context,
192+
)
193+
194+
def _filter_by_extension(file_list, extensions):
195+
return [file for file in file_list if "." + file.extension in extensions]
196+
197+
def _filter_out_by_extension(file_list, extensions):
198+
return [file for file in file_list if "." + file.extension not in extensions]
199+
200+
def _add_linkopts(sdk_linking_info, linkopts):
201+
non_sdk_linkopts = []
202+
i = 0
203+
skip_next = False
204+
for arg in linkopts:
205+
if skip_next:
206+
skip_next = False
207+
i += 1
208+
continue
209+
if arg == "-framework" and i < len(linkopts) - 1:
210+
sdk_linking_info["sdk_framework"].append(linkopts[i + 1])
211+
skip_next = True
212+
elif arg == "-weak_framework" and i < len(linkopts) - 1:
213+
sdk_linking_info["weak_sdk_framework"].append(linkopts[i + 1])
214+
skip_next = True
215+
elif arg.startswith("-Wl,-framework,"):
216+
sdk_linking_info["sdk_framework"].append(arg[len("-Wl,-framework,"):])
217+
elif arg.startswith("-Wl,-weak_framework,"):
218+
sdk_linking_info["weak_sdk_framework"].append(arg[len("-Wl,-weak_framework,"):])
219+
elif arg.startswith("-l"):
220+
sdk_linking_info["sdk_dylib"].append(arg[2:])
221+
else:
222+
non_sdk_linkopts.append(arg)
223+
i += 1
224+
225+
return non_sdk_linkopts
226+
227+
def _is_apple_platform(cpu):
228+
return cpu in ios_cpus.IOS_SIMULATOR_TARGET_CPUS or \
229+
cpu in ios_cpus.IOS_DEVICE_TARGET_CPUS or \
230+
cpu in ios_cpus.VISIONOS_SIMULATOR_TARGET_CPUS or \
231+
cpu in ios_cpus.VISIONOS_DEVICE_TARGET_CPUS or \
232+
cpu in ios_cpus.WATCHOS_SIMULATOR_TARGET_CPUS or \
233+
cpu in ios_cpus.WATCHOS_DEVICE_TARGET_CPUS or \
234+
cpu in ios_cpus.TVOS_SIMULATOR_TARGET_CPUS or \
235+
cpu in ios_cpus.TVOS_DEVICE_TARGET_CPUS or \
236+
cpu in ios_cpus.CATALYST_TARGET_CPUS or \
237+
cpu in ios_cpus.MACOS_TARGET_CPUS
238+
239+
objc_common = struct(
240+
create_context_and_provider = _create_context_and_provider,
241+
is_apple_platform = _is_apple_platform,
242+
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""A collection of compilation information gathered for a particular rule.
16+
17+
This is used to generate the compilation command line and to the supply information that goes
18+
into the compilation info provider.
19+
"""
20+
21+
def _objc_compilation_context_info_init(
22+
defines = [],
23+
public_hdrs = [],
24+
public_textual_hdrs = [],
25+
private_hdrs = [],
26+
includes = [],
27+
system_includes = [],
28+
quote_includes = [],
29+
direct_cc_compilation_contexts = [],
30+
cc_compilation_contexts = [],
31+
implementation_cc_compilation_contexts = [],
32+
providers = []):
33+
strict_dependency_includes = [
34+
path
35+
for objc_provider in providers
36+
for path in objc_provider.strict_include.to_list()
37+
]
38+
39+
# buildifier: disable=unsorted-dict-items
40+
return {
41+
"defines": defines,
42+
"public_hdrs": public_hdrs,
43+
"public_textual_hdrs": public_textual_hdrs,
44+
"private_hdrs": private_hdrs,
45+
"includes": includes,
46+
"system_includes": system_includes,
47+
"quote_includes": quote_includes,
48+
"strict_dependency_includes": strict_dependency_includes,
49+
"direct_cc_compilation_contexts": direct_cc_compilation_contexts,
50+
"cc_compilation_contexts": cc_compilation_contexts,
51+
"implementation_cc_compilation_contexts": implementation_cc_compilation_contexts,
52+
}
53+
54+
ObjcCompilationContextInfo, _new_objccompilationcontextinfo = provider(
55+
"Provider about ObjC compilation information gathered for a particular rule.",
56+
# buildifier: disable=unsorted-dict-items
57+
fields = {
58+
"defines": "",
59+
"public_hdrs": """The list of public headers. We expect this to contain both the headers
60+
from the src attribute, as well as any "additional" headers required for compilation.""",
61+
"public_textual_hdrs": "",
62+
"private_hdrs": "",
63+
"includes": "",
64+
"system_includes": "",
65+
"quote_includes": "",
66+
"strict_dependency_includes": "",
67+
"direct_cc_compilation_contexts": "",
68+
"cc_compilation_contexts": "",
69+
"implementation_cc_compilation_contexts": "",
70+
},
71+
init = _objc_compilation_context_info_init,
72+
)

0 commit comments

Comments
 (0)