blob: aea3fe67685f1e97020a9bea01fa28e3f3617140 [file] [log] [blame]
Paul Duffin1c6c1c72020-02-21 10:28:43 +00001// Copyright 2020 Google Inc. 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
15package cc
16
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000017import (
Spandan Das4238c652022-09-09 01:38:47 +000018 "github.com/google/blueprint/proptools"
19
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000020 "android/soong/android"
21 "android/soong/bazel"
Chris Parsonsf874e462022-05-10 13:50:12 -040022 "android/soong/bazel/cquery"
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000023)
Paul Duffin1c6c1c72020-02-21 10:28:43 +000024
25func init() {
26 RegisterLibraryHeadersBuildComponents(android.InitRegistrationContext)
Paul Duffin91756d22020-02-21 16:29:57 +000027
28 // Register sdk member types.
29 android.RegisterSdkMemberType(headersLibrarySdkMemberType)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000030
Paul Duffin91756d22020-02-21 16:29:57 +000031}
32
33var headersLibrarySdkMemberType = &librarySdkMemberType{
34 SdkMemberTypeBase: android.SdkMemberTypeBase{
Martin Stjernholmcaa47d72020-07-11 04:52:24 +010035 PropertyName: "native_header_libs",
36 SupportsSdk: true,
37 HostOsDependent: true,
Paul Duffin93b750e2019-11-19 19:44:10 +000038 Traits: []android.SdkMemberTrait{
39 nativeBridgeSdkTrait,
Paul Duffin12a0a312021-09-15 17:25:10 +010040 ramdiskImageRequiredSdkTrait,
Paul Duffin63696222021-09-06 10:28:34 +010041 recoveryImageRequiredSdkTrait,
Paul Duffin93b750e2019-11-19 19:44:10 +000042 },
Paul Duffin91756d22020-02-21 16:29:57 +000043 },
44 prebuiltModuleType: "cc_prebuilt_library_headers",
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000045 noOutputFiles: true,
Paul Duffin1c6c1c72020-02-21 10:28:43 +000046}
47
48func RegisterLibraryHeadersBuildComponents(ctx android.RegistrationContext) {
49 ctx.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Paul Duffinf5ea9e12020-02-21 10:57:00 +000050 ctx.RegisterModuleType("cc_prebuilt_library_headers", prebuiltLibraryHeaderFactory)
Paul Duffin1c6c1c72020-02-21 10:28:43 +000051}
52
Chris Parsonsf874e462022-05-10 13:50:12 -040053type libraryHeaderBazelHandler struct {
Liz Kammerb6a55bf2021-04-12 15:42:51 -040054 module *Module
55 library *libraryDecorator
56}
57
Chris Parsons6ce2cf92022-05-20 10:54:17 -040058var _ BazelHandler = (*libraryHeaderBazelHandler)(nil)
59
Chris Parsonsf874e462022-05-10 13:50:12 -040060func (handler *libraryHeaderBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
Liz Kammerb6a55bf2021-04-12 15:42:51 -040061 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -040062 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
63}
64
65func (h *libraryHeaderBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
66 bazelCtx := ctx.Config().BazelContext
67 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerb6a55bf2021-04-12 15:42:51 -040068 if err != nil {
Chris Parsonsf874e462022-05-10 13:50:12 -040069 ctx.ModuleErrorf(err.Error())
70 return
Liz Kammerb6a55bf2021-04-12 15:42:51 -040071 }
72
73 outputPaths := ccInfo.OutputFiles
74 if len(outputPaths) != 1 {
75 ctx.ModuleErrorf("expected exactly one output file for %q, but got %q", label, outputPaths)
Chris Parsonsf874e462022-05-10 13:50:12 -040076 return
Liz Kammerb6a55bf2021-04-12 15:42:51 -040077 }
78
79 outputPath := android.PathForBazelOut(ctx, outputPaths[0])
80 h.module.outputFile = android.OptionalPathForPath(outputPath)
81
82 // HeaderLibraryInfo is an empty struct to indicate to dependencies that this is a header library
83 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
84
Chris Parsons94a0bba2021-06-04 15:03:47 -040085 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
Liz Kammerb6a55bf2021-04-12 15:42:51 -040086
87 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
88 // validation will fail. For now, set this to an empty list.
89 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
90 h.library.collectedSnapshotHeaders = android.Paths{}
Sam Delmerico5fb794a2023-01-27 16:01:37 -050091
92 h.module.setAndroidMkVariablesFromCquery(ccInfo.CcAndroidMkInfo)
Liz Kammerb6a55bf2021-04-12 15:42:51 -040093}
94
Paul Duffin1c6c1c72020-02-21 10:28:43 +000095// cc_library_headers contains a set of c/c++ headers which are imported by
96// other soong cc modules using the header_libs property. For best practices,
97// use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for
98// Make.
99func LibraryHeaderFactory() android.Module {
100 module, library := NewLibrary(android.HostAndDeviceSupported)
101 library.HeaderOnly()
Paul Duffin91756d22020-02-21 16:29:57 +0000102 module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType}
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400103 module.bazelable = true
Chris Parsonsf874e462022-05-10 13:50:12 -0400104 module.bazelHandler = &libraryHeaderBazelHandler{module: module, library: library}
Paul Duffin1c6c1c72020-02-21 10:28:43 +0000105 return module.Init()
106}
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000107
108// cc_prebuilt_library_headers is a prebuilt version of cc_library_headers
109func prebuiltLibraryHeaderFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000110 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "")
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000111 library.HeaderOnly()
Liz Kammer3d3b35c2022-01-11 16:02:50 +0000112 module.bazelable = true
113 module.bazelHandler = &ccLibraryBazelHandler{module: module}
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000114 return module.Init()
115}
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000116
117type bazelCcLibraryHeadersAttributes struct {
Liz Kammer1263d9b2021-12-10 14:28:20 -0500118 Hdrs bazel.LabelListAttribute
119 Export_includes bazel.StringListAttribute
120 Export_absolute_includes bazel.StringListAttribute
121 Export_system_includes bazel.StringListAttribute
122 Deps bazel.LabelListAttribute
123 Implementation_deps bazel.LabelListAttribute
124 System_dynamic_deps bazel.LabelListAttribute
Yu Liufc603162022-03-01 15:44:08 -0800125 sdkAttributes
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000126}
127
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400128func libraryHeadersBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Liz Kammere6583482021-10-19 13:56:10 -0400129 baseAttributes := bp2BuildParseBaseProps(ctx, module)
Liz Kammer54549442022-05-11 13:55:06 -0400130 exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, &baseAttributes.includes)
Liz Kammere6583482021-10-19 13:56:10 -0400131 linkerAttrs := baseAttributes.linkerAttributes
Trevor Radcliffe2be7f542022-06-22 20:30:55 +0000132 (&linkerAttrs.deps).Append(linkerAttrs.dynamicDeps)
Trevor Radcliffe7f897fc2022-08-18 15:53:00 +0000133 (&linkerAttrs.deps).Append(linkerAttrs.wholeArchiveDeps)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000134
135 attrs := &bazelCcLibraryHeadersAttributes{
Liz Kammer1263d9b2021-12-10 14:28:20 -0500136 Export_includes: exportedIncludes.Includes,
137 Export_absolute_includes: exportedIncludes.AbsoluteIncludes,
138 Export_system_includes: exportedIncludes.SystemIncludes,
Liz Kammer1263d9b2021-12-10 14:28:20 -0500139 Deps: linkerAttrs.deps,
140 System_dynamic_deps: linkerAttrs.systemDynamicDeps,
141 Hdrs: baseAttributes.hdrs,
Yu Liufc603162022-03-01 15:44:08 -0800142 sdkAttributes: bp2BuildParseSdkAttributes(module),
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000143 }
144
Liz Kammerfc46bc12021-02-19 11:06:17 -0500145 props := bazel.BazelTargetModuleProperties{
146 Rule_class: "cc_library_headers",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500147 Bzl_load_location: "//build/bazel/rules/cc:cc_library_headers.bzl",
Liz Kammerfc46bc12021-02-19 11:06:17 -0500148 }
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000149
Sam Delmericoeddd3c02022-12-02 17:31:58 -0500150 tags := android.ApexAvailableTags(module)
151
152 ctx.CreateBazelTargetModule(props, android.CommonAttributes{
153 Name: module.Name(),
154 Tags: tags,
155 }, attrs)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000156}
Spandan Das4238c652022-09-09 01:38:47 +0000157
158// Append .contribution suffix to input labels
159func apiBazelTargets(ll bazel.LabelList) bazel.LabelList {
160 labels := make([]bazel.Label, 0)
161 for _, l := range ll.Includes {
162 labels = append(labels, bazel.Label{
163 Label: android.ApiContributionTargetName(l.Label),
164 })
165 }
166 return bazel.MakeLabelList(labels)
167}
168
169func apiLibraryHeadersBp2Build(ctx android.TopDownMutatorContext, module *Module) {
170 // cc_api_library_headers have a 1:1 mapping to arch/no-arch
171 // For API export, create a top-level arch-agnostic target and list the arch-specific targets as its deps
172
173 // arch-agnostic includes
174 apiIncludes := getSystemApiIncludes(ctx, module)
175 // arch and os specific includes
176 archApiIncludes, androidOsIncludes := archOsSpecificApiIncludes(ctx, module)
177 for _, arch := range allArches { // sorted iteration
178 archApiInclude := archApiIncludes[arch]
179 if !archApiInclude.isEmpty() {
180 createApiHeaderTarget(ctx, archApiInclude)
181 apiIncludes.addDep(archApiInclude.name)
182 }
183 }
184 // os==android includes
185 if !androidOsIncludes.isEmpty() {
186 createApiHeaderTarget(ctx, androidOsIncludes)
187 apiIncludes.addDep(androidOsIncludes.name)
188 }
189
190 if !apiIncludes.isEmpty() {
191 // override the name from <mod>.systemapi.headers --> <mod>.contribution
192 apiIncludes.name = android.ApiContributionTargetName(module.Name())
193 createApiHeaderTarget(ctx, apiIncludes)
194 }
195}
196
197func createApiHeaderTarget(ctx android.TopDownMutatorContext, includes apiIncludes) {
198 props := bazel.BazelTargetModuleProperties{
199 Rule_class: "cc_api_library_headers",
200 Bzl_load_location: "//build/bazel/rules/apis:cc_api_contribution.bzl",
201 }
202 ctx.CreateBazelTargetModule(
203 props,
204 android.CommonAttributes{
205 Name: includes.name,
206 SkipData: proptools.BoolPtr(true),
207 },
208 &includes.attrs,
209 )
210}
211
212var (
213 allArches = []string{"arm", "arm64", "x86", "x86_64"}
214)
215
216type archApiIncludes map[string]apiIncludes
217
218func archOsSpecificApiIncludes(ctx android.TopDownMutatorContext, module *Module) (archApiIncludes, apiIncludes) {
219 baseProps := bp2BuildParseBaseProps(ctx, module)
220 i := bp2BuildParseExportedIncludes(ctx, module, &baseProps.includes)
221 archRet := archApiIncludes{}
222 for _, arch := range allArches {
223 includes := i.Includes.SelectValue(
224 bazel.ArchConfigurationAxis,
225 arch)
226 systemIncludes := i.SystemIncludes.SelectValue(
227 bazel.ArchConfigurationAxis,
228 arch)
229 deps := baseProps.deps.SelectValue(
230 bazel.ArchConfigurationAxis,
231 arch)
232 attrs := bazelCcLibraryHeadersAttributes{
233 Export_includes: bazel.MakeStringListAttribute(includes),
234 Export_system_includes: bazel.MakeStringListAttribute(systemIncludes),
235 }
236 apiDeps := apiBazelTargets(deps)
237 if !apiDeps.IsEmpty() {
238 attrs.Deps = bazel.MakeLabelListAttribute(apiDeps)
239 }
240 apiIncludes := apiIncludes{
241 name: android.ApiContributionTargetName(module.Name()) + "." + arch,
242 attrs: bazelCcApiLibraryHeadersAttributes{
243 bazelCcLibraryHeadersAttributes: attrs,
244 Arch: proptools.StringPtr(arch),
245 },
246 }
247 archRet[arch] = apiIncludes
248 }
249
250 // apiIncludes for os == Android
251 androidOsDeps := baseProps.deps.SelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid)
252 androidOsAttrs := bazelCcLibraryHeadersAttributes{
253 Export_includes: bazel.MakeStringListAttribute(
254 i.Includes.SelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid),
255 ),
256 Export_system_includes: bazel.MakeStringListAttribute(
257 i.SystemIncludes.SelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid),
258 ),
259 }
260 androidOsApiDeps := apiBazelTargets(androidOsDeps)
261 if !androidOsApiDeps.IsEmpty() {
262 androidOsAttrs.Deps = bazel.MakeLabelListAttribute(androidOsApiDeps)
263 }
264 osRet := apiIncludes{
265 name: android.ApiContributionTargetName(module.Name()) + ".androidos",
266 attrs: bazelCcApiLibraryHeadersAttributes{
267 bazelCcLibraryHeadersAttributes: androidOsAttrs,
268 },
269 }
270 return archRet, osRet
271}