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 | |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | var headersLibrarySdkMemberType = &librarySdkMemberType{ |
| 31 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 32 | PropertyName: "native_header_libs", |
| 33 | SupportsSdk: true, |
| 34 | HostOsDependent: true, |
Paul Duffin | 93b750e | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 35 | Traits: []android.SdkMemberTrait{ |
| 36 | nativeBridgeSdkTrait, |
Paul Duffin | 12a0a31 | 2021-09-15 17:25:10 +0100 | [diff] [blame] | 37 | ramdiskImageRequiredSdkTrait, |
Paul Duffin | 6369622 | 2021-09-06 10:28:34 +0100 | [diff] [blame] | 38 | recoveryImageRequiredSdkTrait, |
Paul Duffin | 93b750e | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 39 | }, |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 40 | }, |
| 41 | prebuiltModuleType: "cc_prebuilt_library_headers", |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 42 | noOutputFiles: true, |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | func RegisterLibraryHeadersBuildComponents(ctx android.RegistrationContext) { |
| 46 | ctx.RegisterModuleType("cc_library_headers", LibraryHeaderFactory) |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 47 | ctx.RegisterModuleType("cc_prebuilt_library_headers", prebuiltLibraryHeaderFactory) |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 50 | 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] | 51 | android.BazelHandler |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 52 | |
| 53 | module *Module |
| 54 | library *libraryDecorator |
| 55 | } |
| 56 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 57 | func (h *libraryHeaderBazelHander) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool { |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 58 | bazelCtx := ctx.Config().BazelContext |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 59 | ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx)) |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 60 | if err != nil { |
| 61 | ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err) |
| 62 | return false |
| 63 | } |
| 64 | if !ok { |
| 65 | return false |
| 66 | } |
| 67 | |
| 68 | outputPaths := ccInfo.OutputFiles |
| 69 | if len(outputPaths) != 1 { |
| 70 | ctx.ModuleErrorf("expected exactly one output file for %q, but got %q", label, outputPaths) |
| 71 | return false |
| 72 | } |
| 73 | |
| 74 | outputPath := android.PathForBazelOut(ctx, outputPaths[0]) |
| 75 | h.module.outputFile = android.OptionalPathForPath(outputPath) |
| 76 | |
| 77 | // HeaderLibraryInfo is an empty struct to indicate to dependencies that this is a header library |
| 78 | ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{}) |
| 79 | |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 80 | h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo) |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 81 | |
| 82 | // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise |
| 83 | // validation will fail. For now, set this to an empty list. |
| 84 | // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation. |
| 85 | h.library.collectedSnapshotHeaders = android.Paths{} |
| 86 | |
| 87 | return true |
| 88 | } |
| 89 | |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 90 | // cc_library_headers contains a set of c/c++ headers which are imported by |
| 91 | // other soong cc modules using the header_libs property. For best practices, |
| 92 | // use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for |
| 93 | // Make. |
| 94 | func LibraryHeaderFactory() android.Module { |
| 95 | module, library := NewLibrary(android.HostAndDeviceSupported) |
| 96 | library.HeaderOnly() |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 97 | module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType} |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 98 | module.bazelable = true |
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 { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 105 | module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "") |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 106 | library.HeaderOnly() |
Liz Kammer | 3d3b35c | 2022-01-11 16:02:50 +0000 | [diff] [blame] | 107 | module.bazelable = true |
| 108 | module.bazelHandler = &ccLibraryBazelHandler{module: module} |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 109 | return module.Init() |
| 110 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 111 | |
| 112 | type bazelCcLibraryHeadersAttributes struct { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 113 | Hdrs bazel.LabelListAttribute |
| 114 | Export_includes bazel.StringListAttribute |
| 115 | Export_absolute_includes bazel.StringListAttribute |
| 116 | Export_system_includes bazel.StringListAttribute |
| 117 | Deps bazel.LabelListAttribute |
| 118 | Implementation_deps bazel.LabelListAttribute |
| 119 | System_dynamic_deps bazel.LabelListAttribute |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 122 | func libraryHeadersBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 123 | baseAttributes := bp2BuildParseBaseProps(ctx, module) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 124 | exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, baseAttributes.includes) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 125 | linkerAttrs := baseAttributes.linkerAttributes |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 126 | |
| 127 | attrs := &bazelCcLibraryHeadersAttributes{ |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 128 | Export_includes: exportedIncludes.Includes, |
| 129 | Export_absolute_includes: exportedIncludes.AbsoluteIncludes, |
| 130 | Export_system_includes: exportedIncludes.SystemIncludes, |
| 131 | Implementation_deps: linkerAttrs.implementationDeps, |
| 132 | Deps: linkerAttrs.deps, |
| 133 | System_dynamic_deps: linkerAttrs.systemDynamicDeps, |
| 134 | Hdrs: baseAttributes.hdrs, |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 137 | props := bazel.BazelTargetModuleProperties{ |
| 138 | Rule_class: "cc_library_headers", |
Liz Kammer | 2b376bc | 2022-01-12 12:00:49 -0500 | [diff] [blame] | 139 | Bzl_load_location: "//build/bazel/rules/cc:cc_library_headers.bzl", |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 140 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 141 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 142 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 143 | } |