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 | 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 | |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 51 | type libraryHeaderBazelHander struct { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 52 | android.BazelHandler |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 53 | |
| 54 | module *Module |
| 55 | library *libraryDecorator |
| 56 | } |
| 57 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 58 | func (h *libraryHeaderBazelHander) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool { |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 59 | bazelCtx := ctx.Config().BazelContext |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 60 | ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx)) |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 61 | if err != nil { |
| 62 | ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err) |
| 63 | return false |
| 64 | } |
| 65 | if !ok { |
| 66 | return false |
| 67 | } |
| 68 | |
| 69 | outputPaths := ccInfo.OutputFiles |
| 70 | if len(outputPaths) != 1 { |
| 71 | ctx.ModuleErrorf("expected exactly one output file for %q, but got %q", label, outputPaths) |
| 72 | return false |
| 73 | } |
| 74 | |
| 75 | outputPath := android.PathForBazelOut(ctx, outputPaths[0]) |
| 76 | h.module.outputFile = android.OptionalPathForPath(outputPath) |
| 77 | |
| 78 | // HeaderLibraryInfo is an empty struct to indicate to dependencies that this is a header library |
| 79 | ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{}) |
| 80 | |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 81 | h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo) |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 82 | |
| 83 | // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise |
| 84 | // validation will fail. For now, set this to an empty list. |
| 85 | // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation. |
| 86 | h.library.collectedSnapshotHeaders = android.Paths{} |
| 87 | |
| 88 | return true |
| 89 | } |
| 90 | |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 91 | // cc_library_headers contains a set of c/c++ headers which are imported by |
| 92 | // other soong cc modules using the header_libs property. For best practices, |
| 93 | // use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for |
| 94 | // Make. |
| 95 | func LibraryHeaderFactory() android.Module { |
| 96 | module, library := NewLibrary(android.HostAndDeviceSupported) |
| 97 | library.HeaderOnly() |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 98 | module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType} |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 99 | module.bazelHandler = &libraryHeaderBazelHander{module: module, library: library} |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 100 | return module.Init() |
| 101 | } |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 102 | |
| 103 | // cc_prebuilt_library_headers is a prebuilt version of cc_library_headers |
| 104 | func prebuiltLibraryHeaderFactory() android.Module { |
| 105 | module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported) |
| 106 | library.HeaderOnly() |
| 107 | return module.Init() |
| 108 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 109 | |
| 110 | type bazelCcLibraryHeadersAttributes struct { |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 111 | Hdrs bazel.LabelListAttribute |
| 112 | Export_includes bazel.StringListAttribute |
| 113 | Export_system_includes bazel.StringListAttribute |
| 114 | Deps bazel.LabelListAttribute |
| 115 | Implementation_deps bazel.LabelListAttribute |
| 116 | System_dynamic_deps bazel.LabelListAttribute |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 119 | func CcLibraryHeadersBp2Build(ctx android.TopDownMutatorContext) { |
| 120 | module, ok := ctx.Module().(*Module) |
| 121 | if !ok { |
| 122 | // Not a cc module |
| 123 | return |
| 124 | } |
| 125 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 126 | if !module.ConvertWithBp2build(ctx) { |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 127 | return |
| 128 | } |
| 129 | |
Jingwen Chen | c7846f3 | 2021-03-18 05:16:10 -0400 | [diff] [blame] | 130 | if ctx.ModuleType() != "cc_library_headers" { |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 131 | return |
| 132 | } |
Jingwen Chen | c7846f3 | 2021-03-18 05:16:10 -0400 | [diff] [blame] | 133 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 134 | exportedIncludes := bp2BuildParseExportedIncludes(ctx, module) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame^] | 135 | baseAttributes := bp2BuildParseBaseProps(ctx, module) |
| 136 | linkerAttrs := baseAttributes.linkerAttributes |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 137 | |
| 138 | attrs := &bazelCcLibraryHeadersAttributes{ |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 139 | Export_includes: exportedIncludes.Includes, |
| 140 | Export_system_includes: exportedIncludes.SystemIncludes, |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 141 | Implementation_deps: linkerAttrs.implementationDeps, |
| 142 | Deps: linkerAttrs.deps, |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 143 | System_dynamic_deps: linkerAttrs.systemDynamicDeps, |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame^] | 144 | Hdrs: baseAttributes.hdrs, |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 147 | props := bazel.BazelTargetModuleProperties{ |
| 148 | Rule_class: "cc_library_headers", |
| 149 | Bzl_load_location: "//build/bazel/rules:cc_library_headers.bzl", |
| 150 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 151 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 152 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 153 | } |