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" |
| 20 | ) |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 21 | |
| 22 | func init() { |
| 23 | RegisterLibraryHeadersBuildComponents(android.InitRegistrationContext) |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 24 | |
| 25 | // Register sdk member types. |
| 26 | android.RegisterSdkMemberType(headersLibrarySdkMemberType) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 27 | |
| 28 | android.RegisterBp2BuildMutator("cc_library_headers", CcLibraryHeadersBp2Build) |
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 | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 36 | }, |
| 37 | prebuiltModuleType: "cc_prebuilt_library_headers", |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 38 | noOutputFiles: true, |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | func RegisterLibraryHeadersBuildComponents(ctx android.RegistrationContext) { |
| 42 | ctx.RegisterModuleType("cc_library_headers", LibraryHeaderFactory) |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 43 | ctx.RegisterModuleType("cc_prebuilt_library_headers", prebuiltLibraryHeaderFactory) |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 46 | type libraryHeaderBazelHander struct { |
| 47 | bazelHandler |
| 48 | |
| 49 | module *Module |
| 50 | library *libraryDecorator |
| 51 | } |
| 52 | |
| 53 | func (h *libraryHeaderBazelHander) generateBazelBuildActions(ctx android.ModuleContext, label string) bool { |
| 54 | bazelCtx := ctx.Config().BazelContext |
| 55 | ccInfo, ok, err := bazelCtx.GetCcInfo(label, ctx.Arch().ArchType) |
| 56 | if err != nil { |
| 57 | ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err) |
| 58 | return false |
| 59 | } |
| 60 | if !ok { |
| 61 | return false |
| 62 | } |
| 63 | |
| 64 | outputPaths := ccInfo.OutputFiles |
| 65 | if len(outputPaths) != 1 { |
| 66 | ctx.ModuleErrorf("expected exactly one output file for %q, but got %q", label, outputPaths) |
| 67 | return false |
| 68 | } |
| 69 | |
| 70 | outputPath := android.PathForBazelOut(ctx, outputPaths[0]) |
| 71 | h.module.outputFile = android.OptionalPathForPath(outputPath) |
| 72 | |
| 73 | // HeaderLibraryInfo is an empty struct to indicate to dependencies that this is a header library |
| 74 | ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{}) |
| 75 | |
| 76 | flagExporterInfo := flagExporterInfoFromCcInfo(ctx, ccInfo) |
| 77 | // Store flag info to be passed along to androimk |
| 78 | // TODO(b/184387147): Androidmk should be done in Bazel, not Soong. |
| 79 | h.library.flagExporterInfo = &flagExporterInfo |
| 80 | // flag exporters consolidates properties like includes, flags, dependencies that should be |
| 81 | // exported from this module to other modules |
| 82 | ctx.SetProvider(FlagExporterInfoProvider, flagExporterInfo) |
| 83 | |
| 84 | // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise |
| 85 | // validation will fail. For now, set this to an empty list. |
| 86 | // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation. |
| 87 | h.library.collectedSnapshotHeaders = android.Paths{} |
| 88 | |
| 89 | return true |
| 90 | } |
| 91 | |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 92 | // cc_library_headers contains a set of c/c++ headers which are imported by |
| 93 | // other soong cc modules using the header_libs property. For best practices, |
| 94 | // use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for |
| 95 | // Make. |
| 96 | func LibraryHeaderFactory() android.Module { |
| 97 | module, library := NewLibrary(android.HostAndDeviceSupported) |
| 98 | library.HeaderOnly() |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 99 | module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType} |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 100 | module.bazelHandler = &libraryHeaderBazelHander{module: module, library: library} |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 101 | return module.Init() |
| 102 | } |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 103 | |
| 104 | // cc_prebuilt_library_headers is a prebuilt version of cc_library_headers |
| 105 | func prebuiltLibraryHeaderFactory() android.Module { |
| 106 | module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported) |
| 107 | library.HeaderOnly() |
| 108 | return module.Init() |
| 109 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 110 | |
| 111 | type bazelCcLibraryHeadersAttributes struct { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 112 | Copts bazel.StringListAttribute |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 113 | Hdrs bazel.LabelListAttribute |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 114 | Includes bazel.StringListAttribute |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 115 | Deps bazel.LabelListAttribute |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | type bazelCcLibraryHeaders struct { |
| 119 | android.BazelTargetModuleBase |
| 120 | bazelCcLibraryHeadersAttributes |
| 121 | } |
| 122 | |
| 123 | func BazelCcLibraryHeadersFactory() android.Module { |
| 124 | module := &bazelCcLibraryHeaders{} |
| 125 | module.AddProperties(&module.bazelCcLibraryHeadersAttributes) |
| 126 | android.InitBazelTargetModule(module) |
| 127 | return module |
| 128 | } |
| 129 | |
| 130 | func CcLibraryHeadersBp2Build(ctx android.TopDownMutatorContext) { |
| 131 | module, ok := ctx.Module().(*Module) |
| 132 | if !ok { |
| 133 | // Not a cc module |
| 134 | return |
| 135 | } |
| 136 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 137 | if !module.ConvertWithBp2build(ctx) { |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 138 | return |
| 139 | } |
| 140 | |
Jingwen Chen | c7846f3 | 2021-03-18 05:16:10 -0400 | [diff] [blame] | 141 | if ctx.ModuleType() != "cc_library_headers" { |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 142 | return |
| 143 | } |
Jingwen Chen | c7846f3 | 2021-03-18 05:16:10 -0400 | [diff] [blame] | 144 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame^] | 145 | exportedIncludes := bp2BuildParseExportedIncludes(ctx, module) |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 146 | compilerAttrs := bp2BuildParseCompilerProps(ctx, module) |
| 147 | linkerAttrs := bp2BuildParseLinkerProps(ctx, module) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 148 | |
| 149 | attrs := &bazelCcLibraryHeadersAttributes{ |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 150 | Copts: compilerAttrs.copts, |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 151 | Includes: exportedIncludes, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 152 | Deps: linkerAttrs.deps, |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 155 | props := bazel.BazelTargetModuleProperties{ |
| 156 | Rule_class: "cc_library_headers", |
| 157 | Bzl_load_location: "//build/bazel/rules:cc_library_headers.bzl", |
| 158 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 159 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 160 | ctx.CreateBazelTargetModule(BazelCcLibraryHeadersFactory, module.Name(), props, attrs) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | func (m *bazelCcLibraryHeaders) Name() string { |
| 164 | return m.BaseModuleName() |
| 165 | } |
| 166 | |
| 167 | func (m *bazelCcLibraryHeaders) GenerateAndroidBuildActions(ctx android.ModuleContext) {} |