Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 17 | import ( |
Spandan Das | 4238c65 | 2022-09-09 01:38:47 +0000 | [diff] [blame] | 18 | "github.com/google/blueprint/proptools" |
| 19 | |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 20 | "android/soong/android" |
| 21 | "android/soong/bazel" |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 22 | "android/soong/bazel/cquery" |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 23 | ) |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 24 | |
| 25 | func init() { |
| 26 | RegisterLibraryHeadersBuildComponents(android.InitRegistrationContext) |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 27 | |
| 28 | // Register sdk member types. |
| 29 | android.RegisterSdkMemberType(headersLibrarySdkMemberType) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 30 | |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | var headersLibrarySdkMemberType = &librarySdkMemberType{ |
| 34 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 35 | PropertyName: "native_header_libs", |
| 36 | SupportsSdk: true, |
| 37 | HostOsDependent: true, |
Paul Duffin | 93b750e | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 38 | Traits: []android.SdkMemberTrait{ |
| 39 | nativeBridgeSdkTrait, |
Paul Duffin | 12a0a31 | 2021-09-15 17:25:10 +0100 | [diff] [blame] | 40 | ramdiskImageRequiredSdkTrait, |
Paul Duffin | 6369622 | 2021-09-06 10:28:34 +0100 | [diff] [blame] | 41 | recoveryImageRequiredSdkTrait, |
Paul Duffin | 93b750e | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 42 | }, |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 43 | }, |
| 44 | prebuiltModuleType: "cc_prebuilt_library_headers", |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 45 | noOutputFiles: true, |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | func RegisterLibraryHeadersBuildComponents(ctx android.RegistrationContext) { |
| 49 | ctx.RegisterModuleType("cc_library_headers", LibraryHeaderFactory) |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 50 | ctx.RegisterModuleType("cc_prebuilt_library_headers", prebuiltLibraryHeaderFactory) |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 53 | type libraryHeaderBazelHandler struct { |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 54 | module *Module |
| 55 | library *libraryDecorator |
| 56 | } |
| 57 | |
Chris Parsons | 6ce2cf9 | 2022-05-20 10:54:17 -0400 | [diff] [blame] | 58 | var _ BazelHandler = (*libraryHeaderBazelHandler)(nil) |
| 59 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 60 | func (handler *libraryHeaderBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 61 | bazelCtx := ctx.Config().BazelContext |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 62 | bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx)) |
| 63 | } |
| 64 | |
| 65 | func (h *libraryHeaderBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { |
| 66 | bazelCtx := ctx.Config().BazelContext |
| 67 | ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx)) |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 68 | if err != nil { |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 69 | ctx.ModuleErrorf(err.Error()) |
| 70 | return |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 71 | } |
| 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 Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 76 | return |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 77 | } |
| 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 Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 85 | h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo) |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 86 | |
| 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{} |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 93 | // cc_library_headers contains a set of c/c++ headers which are imported by |
| 94 | // other soong cc modules using the header_libs property. For best practices, |
| 95 | // use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for |
| 96 | // Make. |
| 97 | func LibraryHeaderFactory() android.Module { |
| 98 | module, library := NewLibrary(android.HostAndDeviceSupported) |
| 99 | library.HeaderOnly() |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 100 | module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType} |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 101 | module.bazelable = true |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 102 | module.bazelHandler = &libraryHeaderBazelHandler{module: module, library: library} |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 103 | return module.Init() |
| 104 | } |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 105 | |
| 106 | // cc_prebuilt_library_headers is a prebuilt version of cc_library_headers |
| 107 | func prebuiltLibraryHeaderFactory() android.Module { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 108 | module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "") |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 109 | library.HeaderOnly() |
Liz Kammer | 3d3b35c | 2022-01-11 16:02:50 +0000 | [diff] [blame] | 110 | module.bazelable = true |
| 111 | module.bazelHandler = &ccLibraryBazelHandler{module: module} |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 112 | return module.Init() |
| 113 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 114 | |
| 115 | type bazelCcLibraryHeadersAttributes struct { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 116 | Hdrs bazel.LabelListAttribute |
| 117 | Export_includes bazel.StringListAttribute |
| 118 | Export_absolute_includes bazel.StringListAttribute |
| 119 | Export_system_includes bazel.StringListAttribute |
| 120 | Deps bazel.LabelListAttribute |
| 121 | Implementation_deps bazel.LabelListAttribute |
| 122 | System_dynamic_deps bazel.LabelListAttribute |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 123 | sdkAttributes |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 126 | func libraryHeadersBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 127 | baseAttributes := bp2BuildParseBaseProps(ctx, module) |
Liz Kammer | 5454944 | 2022-05-11 13:55:06 -0400 | [diff] [blame] | 128 | exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, &baseAttributes.includes) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 129 | linkerAttrs := baseAttributes.linkerAttributes |
Trevor Radcliffe | 2be7f54 | 2022-06-22 20:30:55 +0000 | [diff] [blame] | 130 | (&linkerAttrs.deps).Append(linkerAttrs.dynamicDeps) |
Trevor Radcliffe | 7f897fc | 2022-08-18 15:53:00 +0000 | [diff] [blame] | 131 | (&linkerAttrs.deps).Append(linkerAttrs.wholeArchiveDeps) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 132 | |
| 133 | attrs := &bazelCcLibraryHeadersAttributes{ |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 134 | Export_includes: exportedIncludes.Includes, |
| 135 | Export_absolute_includes: exportedIncludes.AbsoluteIncludes, |
| 136 | Export_system_includes: exportedIncludes.SystemIncludes, |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 137 | Deps: linkerAttrs.deps, |
| 138 | System_dynamic_deps: linkerAttrs.systemDynamicDeps, |
| 139 | Hdrs: baseAttributes.hdrs, |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 140 | sdkAttributes: bp2BuildParseSdkAttributes(module), |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 143 | props := bazel.BazelTargetModuleProperties{ |
| 144 | Rule_class: "cc_library_headers", |
Liz Kammer | 2b376bc | 2022-01-12 12:00:49 -0500 | [diff] [blame] | 145 | Bzl_load_location: "//build/bazel/rules/cc:cc_library_headers.bzl", |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 146 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 147 | |
Sam Delmerico | eddd3c0 | 2022-12-02 17:31:58 -0500 | [diff] [blame] | 148 | tags := android.ApexAvailableTags(module) |
| 149 | |
| 150 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{ |
| 151 | Name: module.Name(), |
| 152 | Tags: tags, |
| 153 | }, attrs) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 154 | } |
Spandan Das | 4238c65 | 2022-09-09 01:38:47 +0000 | [diff] [blame] | 155 | |
| 156 | // Append .contribution suffix to input labels |
| 157 | func apiBazelTargets(ll bazel.LabelList) bazel.LabelList { |
| 158 | labels := make([]bazel.Label, 0) |
| 159 | for _, l := range ll.Includes { |
| 160 | labels = append(labels, bazel.Label{ |
| 161 | Label: android.ApiContributionTargetName(l.Label), |
| 162 | }) |
| 163 | } |
| 164 | return bazel.MakeLabelList(labels) |
| 165 | } |
| 166 | |
| 167 | func apiLibraryHeadersBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
| 168 | // cc_api_library_headers have a 1:1 mapping to arch/no-arch |
| 169 | // For API export, create a top-level arch-agnostic target and list the arch-specific targets as its deps |
| 170 | |
| 171 | // arch-agnostic includes |
| 172 | apiIncludes := getSystemApiIncludes(ctx, module) |
| 173 | // arch and os specific includes |
| 174 | archApiIncludes, androidOsIncludes := archOsSpecificApiIncludes(ctx, module) |
| 175 | for _, arch := range allArches { // sorted iteration |
| 176 | archApiInclude := archApiIncludes[arch] |
| 177 | if !archApiInclude.isEmpty() { |
| 178 | createApiHeaderTarget(ctx, archApiInclude) |
| 179 | apiIncludes.addDep(archApiInclude.name) |
| 180 | } |
| 181 | } |
| 182 | // os==android includes |
| 183 | if !androidOsIncludes.isEmpty() { |
| 184 | createApiHeaderTarget(ctx, androidOsIncludes) |
| 185 | apiIncludes.addDep(androidOsIncludes.name) |
| 186 | } |
| 187 | |
| 188 | if !apiIncludes.isEmpty() { |
| 189 | // override the name from <mod>.systemapi.headers --> <mod>.contribution |
| 190 | apiIncludes.name = android.ApiContributionTargetName(module.Name()) |
| 191 | createApiHeaderTarget(ctx, apiIncludes) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | func createApiHeaderTarget(ctx android.TopDownMutatorContext, includes apiIncludes) { |
| 196 | props := bazel.BazelTargetModuleProperties{ |
| 197 | Rule_class: "cc_api_library_headers", |
| 198 | Bzl_load_location: "//build/bazel/rules/apis:cc_api_contribution.bzl", |
| 199 | } |
| 200 | ctx.CreateBazelTargetModule( |
| 201 | props, |
| 202 | android.CommonAttributes{ |
| 203 | Name: includes.name, |
| 204 | SkipData: proptools.BoolPtr(true), |
| 205 | }, |
| 206 | &includes.attrs, |
| 207 | ) |
| 208 | } |
| 209 | |
| 210 | var ( |
| 211 | allArches = []string{"arm", "arm64", "x86", "x86_64"} |
| 212 | ) |
| 213 | |
| 214 | type archApiIncludes map[string]apiIncludes |
| 215 | |
| 216 | func archOsSpecificApiIncludes(ctx android.TopDownMutatorContext, module *Module) (archApiIncludes, apiIncludes) { |
| 217 | baseProps := bp2BuildParseBaseProps(ctx, module) |
| 218 | i := bp2BuildParseExportedIncludes(ctx, module, &baseProps.includes) |
| 219 | archRet := archApiIncludes{} |
| 220 | for _, arch := range allArches { |
| 221 | includes := i.Includes.SelectValue( |
| 222 | bazel.ArchConfigurationAxis, |
| 223 | arch) |
| 224 | systemIncludes := i.SystemIncludes.SelectValue( |
| 225 | bazel.ArchConfigurationAxis, |
| 226 | arch) |
| 227 | deps := baseProps.deps.SelectValue( |
| 228 | bazel.ArchConfigurationAxis, |
| 229 | arch) |
| 230 | attrs := bazelCcLibraryHeadersAttributes{ |
| 231 | Export_includes: bazel.MakeStringListAttribute(includes), |
| 232 | Export_system_includes: bazel.MakeStringListAttribute(systemIncludes), |
| 233 | } |
| 234 | apiDeps := apiBazelTargets(deps) |
| 235 | if !apiDeps.IsEmpty() { |
| 236 | attrs.Deps = bazel.MakeLabelListAttribute(apiDeps) |
| 237 | } |
| 238 | apiIncludes := apiIncludes{ |
| 239 | name: android.ApiContributionTargetName(module.Name()) + "." + arch, |
| 240 | attrs: bazelCcApiLibraryHeadersAttributes{ |
| 241 | bazelCcLibraryHeadersAttributes: attrs, |
| 242 | Arch: proptools.StringPtr(arch), |
| 243 | }, |
| 244 | } |
| 245 | archRet[arch] = apiIncludes |
| 246 | } |
| 247 | |
| 248 | // apiIncludes for os == Android |
| 249 | androidOsDeps := baseProps.deps.SelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid) |
| 250 | androidOsAttrs := bazelCcLibraryHeadersAttributes{ |
| 251 | Export_includes: bazel.MakeStringListAttribute( |
| 252 | i.Includes.SelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid), |
| 253 | ), |
| 254 | Export_system_includes: bazel.MakeStringListAttribute( |
| 255 | i.SystemIncludes.SelectValue(bazel.OsConfigurationAxis, bazel.OsAndroid), |
| 256 | ), |
| 257 | } |
| 258 | androidOsApiDeps := apiBazelTargets(androidOsDeps) |
| 259 | if !androidOsApiDeps.IsEmpty() { |
| 260 | androidOsAttrs.Deps = bazel.MakeLabelListAttribute(androidOsApiDeps) |
| 261 | } |
| 262 | osRet := apiIncludes{ |
| 263 | name: android.ApiContributionTargetName(module.Name()) + ".androidos", |
| 264 | attrs: bazelCcApiLibraryHeadersAttributes{ |
| 265 | bazelCcLibraryHeadersAttributes: androidOsAttrs, |
| 266 | }, |
| 267 | } |
| 268 | return archRet, osRet |
| 269 | } |