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 ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/bazel" |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 20 | "android/soong/bazel/cquery" |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 21 | ) |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 22 | |
| 23 | func init() { |
| 24 | RegisterLibraryHeadersBuildComponents(android.InitRegistrationContext) |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 25 | |
| 26 | // Register sdk member types. |
| 27 | android.RegisterSdkMemberType(headersLibrarySdkMemberType) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 28 | |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | var headersLibrarySdkMemberType = &librarySdkMemberType{ |
| 32 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 33 | PropertyName: "native_header_libs", |
| 34 | SupportsSdk: true, |
| 35 | HostOsDependent: true, |
Paul Duffin | 93b750e | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 36 | Traits: []android.SdkMemberTrait{ |
| 37 | nativeBridgeSdkTrait, |
Paul Duffin | 12a0a31 | 2021-09-15 17:25:10 +0100 | [diff] [blame] | 38 | ramdiskImageRequiredSdkTrait, |
Paul Duffin | 6369622 | 2021-09-06 10:28:34 +0100 | [diff] [blame] | 39 | recoveryImageRequiredSdkTrait, |
Paul Duffin | 93b750e | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 40 | }, |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 41 | }, |
| 42 | prebuiltModuleType: "cc_prebuilt_library_headers", |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 43 | noOutputFiles: true, |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func RegisterLibraryHeadersBuildComponents(ctx android.RegistrationContext) { |
| 47 | ctx.RegisterModuleType("cc_library_headers", LibraryHeaderFactory) |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 48 | ctx.RegisterModuleType("cc_prebuilt_library_headers", prebuiltLibraryHeaderFactory) |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 51 | type libraryHeaderBazelHandler struct { |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 52 | module *Module |
| 53 | library *libraryDecorator |
| 54 | } |
| 55 | |
Chris Parsons | 6ce2cf9 | 2022-05-20 10:54:17 -0400 | [diff] [blame] | 56 | var _ BazelHandler = (*libraryHeaderBazelHandler)(nil) |
| 57 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 58 | func (handler *libraryHeaderBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 59 | bazelCtx := ctx.Config().BazelContext |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 60 | bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx))) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func (h *libraryHeaderBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { |
| 64 | bazelCtx := ctx.Config().BazelContext |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 65 | ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx))) |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 66 | if err != nil { |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 67 | ctx.ModuleErrorf(err.Error()) |
| 68 | return |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | outputPaths := ccInfo.OutputFiles |
| 72 | if len(outputPaths) != 1 { |
| 73 | 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] | 74 | return |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 77 | var outputPath android.Path = android.PathForBazelOut(ctx, outputPaths[0]) |
| 78 | if len(ccInfo.TidyFiles) > 0 { |
| 79 | h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles) |
| 80 | outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles) |
| 81 | } |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 82 | h.module.outputFile = android.OptionalPathForPath(outputPath) |
| 83 | |
| 84 | // HeaderLibraryInfo is an empty struct to indicate to dependencies that this is a header library |
| 85 | ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{}) |
| 86 | |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 87 | h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo) |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 88 | |
| 89 | // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise |
| 90 | // validation will fail. For now, set this to an empty list. |
| 91 | // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation. |
| 92 | h.library.collectedSnapshotHeaders = android.Paths{} |
Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 93 | |
| 94 | h.module.setAndroidMkVariablesFromCquery(ccInfo.CcAndroidMkInfo) |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 97 | // cc_library_headers contains a set of c/c++ headers which are imported by |
| 98 | // other soong cc modules using the header_libs property. For best practices, |
| 99 | // use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for |
| 100 | // Make. |
| 101 | func LibraryHeaderFactory() android.Module { |
| 102 | module, library := NewLibrary(android.HostAndDeviceSupported) |
| 103 | library.HeaderOnly() |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 104 | module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType} |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 105 | module.bazelable = true |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 106 | module.bazelHandler = &libraryHeaderBazelHandler{module: module, library: library} |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 107 | return module.Init() |
| 108 | } |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 109 | |
| 110 | // cc_prebuilt_library_headers is a prebuilt version of cc_library_headers |
| 111 | func prebuiltLibraryHeaderFactory() android.Module { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 112 | module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "") |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 113 | library.HeaderOnly() |
Liz Kammer | 3d3b35c | 2022-01-11 16:02:50 +0000 | [diff] [blame] | 114 | module.bazelable = true |
| 115 | module.bazelHandler = &ccLibraryBazelHandler{module: module} |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 116 | return module.Init() |
| 117 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 118 | |
| 119 | type bazelCcLibraryHeadersAttributes struct { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 120 | Hdrs bazel.LabelListAttribute |
| 121 | Export_includes bazel.StringListAttribute |
| 122 | Export_absolute_includes bazel.StringListAttribute |
| 123 | Export_system_includes bazel.StringListAttribute |
| 124 | Deps bazel.LabelListAttribute |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 125 | sdkAttributes |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 128 | func libraryHeadersBp2Build(ctx android.Bp2buildMutatorContext, module *Module) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 129 | baseAttributes := bp2BuildParseBaseProps(ctx, module) |
Liz Kammer | 5454944 | 2022-05-11 13:55:06 -0400 | [diff] [blame] | 130 | exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, &baseAttributes.includes) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 131 | linkerAttrs := baseAttributes.linkerAttributes |
Trevor Radcliffe | 2be7f54 | 2022-06-22 20:30:55 +0000 | [diff] [blame] | 132 | (&linkerAttrs.deps).Append(linkerAttrs.dynamicDeps) |
Trevor Radcliffe | 7f897fc | 2022-08-18 15:53:00 +0000 | [diff] [blame] | 133 | (&linkerAttrs.deps).Append(linkerAttrs.wholeArchiveDeps) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 134 | |
| 135 | attrs := &bazelCcLibraryHeadersAttributes{ |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 136 | Export_includes: exportedIncludes.Includes, |
| 137 | Export_absolute_includes: exportedIncludes.AbsoluteIncludes, |
| 138 | Export_system_includes: exportedIncludes.SystemIncludes, |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 139 | Deps: linkerAttrs.deps, |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 140 | Hdrs: baseAttributes.hdrs, |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 141 | sdkAttributes: bp2BuildParseSdkAttributes(module), |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 144 | props := bazel.BazelTargetModuleProperties{ |
| 145 | Rule_class: "cc_library_headers", |
Liz Kammer | 2b376bc | 2022-01-12 12:00:49 -0500 | [diff] [blame] | 146 | Bzl_load_location: "//build/bazel/rules/cc:cc_library_headers.bzl", |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 147 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 148 | |
Spandan Das | 39b6cc5 | 2023-04-12 19:05:49 +0000 | [diff] [blame] | 149 | tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module) |
Sam Delmerico | eddd3c0 | 2022-12-02 17:31:58 -0500 | [diff] [blame] | 150 | |
Liz Kammer | 86a407f | 2023-09-18 09:23:38 -0400 | [diff] [blame] | 151 | name := module.Name() |
| 152 | if module.IsPrebuilt() { |
| 153 | name = android.RemoveOptionalPrebuiltPrefix(name) |
| 154 | } |
| 155 | |
Sam Delmerico | eddd3c0 | 2022-12-02 17:31:58 -0500 | [diff] [blame] | 156 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{ |
Liz Kammer | 86a407f | 2023-09-18 09:23:38 -0400 | [diff] [blame] | 157 | Name: name, |
Sam Delmerico | eddd3c0 | 2022-12-02 17:31:58 -0500 | [diff] [blame] | 158 | Tags: tags, |
| 159 | }, attrs) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 160 | } |