| Inseob Kim | 5eb7ee9 | 2022-04-27 10:30:34 +0900 | [diff] [blame] | 1 | // Copyright 2021 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 |  | 
|  | 17 | import ( | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 18 | "regexp" | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 19 | "strings" | 
|  | 20 |  | 
| Inseob Kim | 5eb7ee9 | 2022-04-27 10:30:34 +0900 | [diff] [blame] | 21 | "android/soong/android" | 
|  | 22 | "android/soong/multitree" | 
| Aleks Todorov | c9becde | 2024-06-10 12:51:53 +0100 | [diff] [blame] | 23 |  | 
|  | 24 | "github.com/google/blueprint/proptools" | 
| Inseob Kim | 5eb7ee9 | 2022-04-27 10:30:34 +0900 | [diff] [blame] | 25 | ) | 
|  | 26 |  | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 27 | var ( | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 28 | ndkVariantRegex  = regexp.MustCompile("ndk\\.([a-zA-Z0-9]+)") | 
|  | 29 | stubVariantRegex = regexp.MustCompile("apex\\.([a-zA-Z0-9]+)") | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 30 | ) | 
|  | 31 |  | 
| Inseob Kim | 5eb7ee9 | 2022-04-27 10:30:34 +0900 | [diff] [blame] | 32 | func init() { | 
|  | 33 | RegisterLibraryStubBuildComponents(android.InitRegistrationContext) | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | func RegisterLibraryStubBuildComponents(ctx android.RegistrationContext) { | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 37 | ctx.RegisterModuleType("cc_api_library", CcApiLibraryFactory) | 
| Kiyoung Kim | 51279d3 | 2022-08-24 14:10:46 +0900 | [diff] [blame] | 38 | ctx.RegisterModuleType("cc_api_headers", CcApiHeadersFactory) | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 39 | ctx.RegisterModuleType("cc_api_variant", CcApiVariantFactory) | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | func updateImportedLibraryDependency(ctx android.BottomUpMutatorContext) { | 
|  | 43 | m, ok := ctx.Module().(*Module) | 
|  | 44 | if !ok { | 
|  | 45 | return | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | apiLibrary, ok := m.linker.(*apiLibraryDecorator) | 
|  | 49 | if !ok { | 
|  | 50 | return | 
|  | 51 | } | 
|  | 52 |  | 
| Kiyoung Kim | aa39480 | 2024-01-08 12:55:45 +0900 | [diff] [blame] | 53 | if m.InVendorOrProduct() && apiLibrary.hasLLNDKStubs() { | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 54 | // Add LLNDK variant dependency | 
|  | 55 | if inList("llndk", apiLibrary.properties.Variants) { | 
|  | 56 | variantName := BuildApiVariantName(m.BaseModuleName(), "llndk", "") | 
|  | 57 | ctx.AddDependency(m, nil, variantName) | 
|  | 58 | } | 
|  | 59 | } else if m.IsSdkVariant() { | 
|  | 60 | // Add NDK variant dependencies | 
|  | 61 | targetVariant := "ndk." + m.StubsVersion() | 
|  | 62 | if inList(targetVariant, apiLibrary.properties.Variants) { | 
|  | 63 | variantName := BuildApiVariantName(m.BaseModuleName(), targetVariant, "") | 
|  | 64 | ctx.AddDependency(m, nil, variantName) | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 65 | } | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 66 | } else if m.IsStubs() { | 
|  | 67 | targetVariant := "apex." + m.StubsVersion() | 
|  | 68 | if inList(targetVariant, apiLibrary.properties.Variants) { | 
|  | 69 | variantName := BuildApiVariantName(m.BaseModuleName(), targetVariant, "") | 
|  | 70 | ctx.AddDependency(m, nil, variantName) | 
|  | 71 | } | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 72 | } | 
| Inseob Kim | 5eb7ee9 | 2022-04-27 10:30:34 +0900 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 75 | // 'cc_api_library' is a module type which is from the exported API surface | 
|  | 76 | // with C shared library type. The module will replace original module, and | 
|  | 77 | // offer a link to the module that generates shared library object from the | 
|  | 78 | // map file. | 
|  | 79 | type apiLibraryProperties struct { | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 80 | Src      *string `android:"arch_variant"` | 
|  | 81 | Variants []string | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
|  | 84 | type apiLibraryDecorator struct { | 
|  | 85 | *libraryDecorator | 
|  | 86 | properties apiLibraryProperties | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | func CcApiLibraryFactory() android.Module { | 
|  | 90 | module, decorator := NewLibrary(android.DeviceSupported) | 
|  | 91 | apiLibraryDecorator := &apiLibraryDecorator{ | 
|  | 92 | libraryDecorator: decorator, | 
|  | 93 | } | 
|  | 94 | apiLibraryDecorator.BuildOnlyShared() | 
|  | 95 |  | 
|  | 96 | module.stl = nil | 
|  | 97 | module.sanitize = nil | 
|  | 98 | decorator.disableStripping() | 
|  | 99 |  | 
|  | 100 | module.compiler = nil | 
|  | 101 | module.linker = apiLibraryDecorator | 
|  | 102 | module.installer = nil | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 103 | module.library = apiLibraryDecorator | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 104 | module.AddProperties(&module.Properties, &apiLibraryDecorator.properties) | 
|  | 105 |  | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 106 | // Prevent default system libs (libc, libm, and libdl) from being linked | 
|  | 107 | if apiLibraryDecorator.baseLinker.Properties.System_shared_libs == nil { | 
|  | 108 | apiLibraryDecorator.baseLinker.Properties.System_shared_libs = []string{} | 
|  | 109 | } | 
|  | 110 |  | 
| Kiyoung Kim | 835c589 | 2022-08-17 16:40:16 +0900 | [diff] [blame] | 111 | apiLibraryDecorator.baseLinker.Properties.No_libcrt = BoolPtr(true) | 
|  | 112 | apiLibraryDecorator.baseLinker.Properties.Nocrt = BoolPtr(true) | 
|  | 113 |  | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 114 | module.Init() | 
|  | 115 |  | 
|  | 116 | return module | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | func (d *apiLibraryDecorator) Name(basename string) string { | 
|  | 120 | return basename + multitree.GetApiImportSuffix() | 
|  | 121 | } | 
|  | 122 |  | 
| Spandan Das | f0beebc | 2022-10-18 18:23:28 +0000 | [diff] [blame] | 123 | // Export include dirs without checking for existence. | 
|  | 124 | // The directories are not guaranteed to exist during Soong analysis. | 
|  | 125 | func (d *apiLibraryDecorator) exportIncludes(ctx ModuleContext) { | 
|  | 126 | exporterProps := d.flagExporter.Properties | 
| Aleks Todorov | c9becde | 2024-06-10 12:51:53 +0100 | [diff] [blame] | 127 | for _, dir := range exporterProps.Export_include_dirs.GetOrDefault(ctx, nil) { | 
| Spandan Das | c6c10fa | 2022-10-21 21:52:13 +0000 | [diff] [blame] | 128 | d.dirs = append(d.dirs, android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), dir)) | 
| Spandan Das | f0beebc | 2022-10-18 18:23:28 +0000 | [diff] [blame] | 129 | } | 
|  | 130 | // system headers | 
|  | 131 | for _, dir := range exporterProps.Export_system_include_dirs { | 
| Spandan Das | c6c10fa | 2022-10-21 21:52:13 +0000 | [diff] [blame] | 132 | d.systemDirs = append(d.systemDirs, android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), dir)) | 
| Spandan Das | f0beebc | 2022-10-18 18:23:28 +0000 | [diff] [blame] | 133 | } | 
|  | 134 | } | 
|  | 135 |  | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 136 | func (d *apiLibraryDecorator) linkerInit(ctx BaseModuleContext) { | 
|  | 137 | d.baseLinker.linkerInit(ctx) | 
|  | 138 |  | 
|  | 139 | if d.hasNDKStubs() { | 
|  | 140 | // Set SDK version of module as current | 
|  | 141 | ctx.Module().(*Module).Properties.Sdk_version = StringPtr("current") | 
|  | 142 |  | 
|  | 143 | // Add NDK stub as NDK known libs | 
|  | 144 | name := ctx.ModuleName() | 
|  | 145 |  | 
|  | 146 | ndkKnownLibsLock.Lock() | 
|  | 147 | ndkKnownLibs := getNDKKnownLibs(ctx.Config()) | 
|  | 148 | if !inList(name, *ndkKnownLibs) { | 
|  | 149 | *ndkKnownLibs = append(*ndkKnownLibs, name) | 
|  | 150 | } | 
|  | 151 | ndkKnownLibsLock.Unlock() | 
|  | 152 | } | 
|  | 153 | } | 
|  | 154 |  | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 155 | func (d *apiLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objects Objects) android.Path { | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 156 | m, _ := ctx.Module().(*Module) | 
|  | 157 |  | 
|  | 158 | var in android.Path | 
|  | 159 |  | 
| Spandan Das | 6830f6b | 2022-12-02 23:26:38 +0000 | [diff] [blame] | 160 | // src might not exist during the beginning of soong analysis in Multi-tree | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 161 | if src := String(d.properties.Src); src != "" { | 
| Spandan Das | 6830f6b | 2022-12-02 23:26:38 +0000 | [diff] [blame] | 162 | in = android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), src) | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 165 | libName := m.BaseModuleName() + multitree.GetApiImportSuffix() | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 166 |  | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 167 | load_cc_variant := func(apiVariantModule string) { | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 168 | var mod android.Module | 
|  | 169 |  | 
|  | 170 | ctx.VisitDirectDeps(func(depMod android.Module) { | 
|  | 171 | if depMod.Name() == apiVariantModule { | 
|  | 172 | mod = depMod | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 173 | libName = apiVariantModule | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 174 | } | 
|  | 175 | }) | 
|  | 176 |  | 
|  | 177 | if mod != nil { | 
|  | 178 | variantMod, ok := mod.(*CcApiVariant) | 
|  | 179 | if ok { | 
|  | 180 | in = variantMod.Src() | 
|  | 181 |  | 
|  | 182 | // Copy LLDNK properties to cc_api_library module | 
| Aleks Todorov | c9becde | 2024-06-10 12:51:53 +0100 | [diff] [blame] | 183 | exportIncludeDirs := append(d.libraryDecorator.flagExporter.Properties.Export_include_dirs.GetOrDefault(ctx, nil), | 
| Kiyoung Kim | 62ed3dd | 2022-12-02 10:20:55 +0900 | [diff] [blame] | 184 | variantMod.exportProperties.Export_include_dirs...) | 
| Aleks Todorov | c9becde | 2024-06-10 12:51:53 +0100 | [diff] [blame] | 185 | d.libraryDecorator.flagExporter.Properties.Export_include_dirs = proptools.NewConfigurable[[]string]( | 
|  | 186 | nil, | 
|  | 187 | []proptools.ConfigurableCase[[]string]{ | 
|  | 188 | proptools.NewConfigurableCase[[]string](nil, &exportIncludeDirs), | 
|  | 189 | }, | 
|  | 190 | ) | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 191 |  | 
|  | 192 | // Export headers as system include dirs if specified. Mostly for libc | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 193 | if Bool(variantMod.exportProperties.Export_headers_as_system) { | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 194 | d.libraryDecorator.flagExporter.Properties.Export_system_include_dirs = append( | 
|  | 195 | d.libraryDecorator.flagExporter.Properties.Export_system_include_dirs, | 
| Aleks Todorov | c9becde | 2024-06-10 12:51:53 +0100 | [diff] [blame] | 196 | d.libraryDecorator.flagExporter.Properties.Export_include_dirs.GetOrDefault(ctx, nil)...) | 
|  | 197 | d.libraryDecorator.flagExporter.Properties.Export_include_dirs = proptools.NewConfigurable[[]string](nil, nil) | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 198 | } | 
|  | 199 | } | 
|  | 200 | } | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 201 | } | 
|  | 202 |  | 
| Kiyoung Kim | aa39480 | 2024-01-08 12:55:45 +0900 | [diff] [blame] | 203 | if m.InVendorOrProduct() && d.hasLLNDKStubs() { | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 204 | // LLNDK variant | 
|  | 205 | load_cc_variant(BuildApiVariantName(m.BaseModuleName(), "llndk", "")) | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 206 | } else if m.IsSdkVariant() { | 
|  | 207 | // NDK Variant | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 208 | load_cc_variant(BuildApiVariantName(m.BaseModuleName(), "ndk", m.StubsVersion())) | 
|  | 209 | } else if m.IsStubs() { | 
|  | 210 | // APEX Variant | 
|  | 211 | load_cc_variant(BuildApiVariantName(m.BaseModuleName(), "apex", m.StubsVersion())) | 
| Kiyoung Kim | 51279d3 | 2022-08-24 14:10:46 +0900 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 214 | // Flags reexported from dependencies. (e.g. vndk_prebuilt_shared) | 
| Spandan Das | f0beebc | 2022-10-18 18:23:28 +0000 | [diff] [blame] | 215 | d.exportIncludes(ctx) | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 216 | d.libraryDecorator.reexportDirs(deps.ReexportedDirs...) | 
|  | 217 | d.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...) | 
|  | 218 | d.libraryDecorator.reexportFlags(deps.ReexportedFlags...) | 
|  | 219 | d.libraryDecorator.reexportDeps(deps.ReexportedDeps...) | 
|  | 220 | d.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...) | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 221 |  | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 222 | if in == nil { | 
|  | 223 | ctx.PropertyErrorf("src", "Unable to locate source property") | 
|  | 224 | return nil | 
| Spandan Das | f0beebc | 2022-10-18 18:23:28 +0000 | [diff] [blame] | 225 | } | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 226 |  | 
| Spandan Das | a3c8a17 | 2022-10-26 20:54:32 +0000 | [diff] [blame] | 227 | // Make the _compilation_ of rdeps have an order-only dep on cc_api_library.src (an .so file) | 
|  | 228 | // The .so file itself has an order-only dependency on the headers contributed by this library. | 
|  | 229 | // Creating this dependency ensures that the headers are assembled before compilation of rdeps begins. | 
|  | 230 | d.libraryDecorator.reexportDeps(in) | 
|  | 231 | d.libraryDecorator.flagExporter.setProvider(ctx) | 
|  | 232 |  | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 233 | d.unstrippedOutputFile = in | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 234 | libName += flags.Toolchain.ShlibSuffix() | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 235 |  | 
|  | 236 | tocFile := android.PathForModuleOut(ctx, libName+".toc") | 
|  | 237 | d.tocFile = android.OptionalPathForPath(tocFile) | 
|  | 238 | TransformSharedObjectToToc(ctx, in, tocFile) | 
|  | 239 |  | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 240 | outputFile := android.PathForModuleOut(ctx, libName) | 
|  | 241 |  | 
|  | 242 | // TODO(b/270485584) This copies with a new name, just to avoid conflict with prebuilts. | 
|  | 243 | // We can just use original input if there is any way to avoid name conflict without copy. | 
|  | 244 | ctx.Build(pctx, android.BuildParams{ | 
|  | 245 | Rule:        android.Cp, | 
|  | 246 | Description: "API surface imported library", | 
|  | 247 | Input:       in, | 
|  | 248 | Output:      outputFile, | 
|  | 249 | Args: map[string]string{ | 
|  | 250 | "cpFlags": "-L", | 
|  | 251 | }, | 
|  | 252 | }) | 
|  | 253 |  | 
| Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 254 | android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{ | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 255 | SharedLibrary: outputFile, | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 256 | Target:        ctx.Target(), | 
|  | 257 |  | 
|  | 258 | TableOfContents: d.tocFile, | 
|  | 259 | }) | 
|  | 260 |  | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 261 | d.shareStubs(ctx) | 
|  | 262 |  | 
|  | 263 | return outputFile | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | // Share additional information about stub libraries with provider | 
|  | 267 | func (d *apiLibraryDecorator) shareStubs(ctx ModuleContext) { | 
|  | 268 | stubs := ctx.GetDirectDepsWithTag(stubImplDepTag) | 
|  | 269 | if len(stubs) > 0 { | 
|  | 270 | var stubsInfo []SharedStubLibrary | 
|  | 271 | for _, stub := range stubs { | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 272 | stubInfo, _ := android.OtherModuleProvider(ctx, stub, SharedLibraryInfoProvider) | 
|  | 273 | flagInfo, _ := android.OtherModuleProvider(ctx, stub, FlagExporterInfoProvider) | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 274 | stubsInfo = append(stubsInfo, SharedStubLibrary{ | 
|  | 275 | Version:           moduleLibraryInterface(stub).stubsVersion(), | 
|  | 276 | SharedLibraryInfo: stubInfo, | 
|  | 277 | FlagExporterInfo:  flagInfo, | 
|  | 278 | }) | 
|  | 279 | } | 
| Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 280 | android.SetProvider(ctx, SharedLibraryStubsProvider, SharedLibraryStubsInfo{ | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 281 | SharedStubLibraries: stubsInfo, | 
|  | 282 |  | 
|  | 283 | IsLLNDK: ctx.IsLlndk(), | 
|  | 284 | }) | 
|  | 285 | } | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 286 | } | 
|  | 287 |  | 
|  | 288 | func (d *apiLibraryDecorator) availableFor(what string) bool { | 
|  | 289 | // Stub from API surface should be available for any APEX. | 
|  | 290 | return true | 
|  | 291 | } | 
|  | 292 |  | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 293 | func (d *apiLibraryDecorator) hasApexStubs() bool { | 
|  | 294 | for _, variant := range d.properties.Variants { | 
|  | 295 | if strings.HasPrefix(variant, "apex") { | 
|  | 296 | return true | 
|  | 297 | } | 
|  | 298 | } | 
|  | 299 | return false | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | func (d *apiLibraryDecorator) hasStubsVariants() bool { | 
|  | 303 | return d.hasApexStubs() | 
|  | 304 | } | 
|  | 305 |  | 
| Colin Cross | add04a8 | 2024-05-22 09:57:59 -0700 | [diff] [blame] | 306 | func (d *apiLibraryDecorator) stubsVersions(ctx android.BaseModuleContext) []string { | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 307 | m, ok := ctx.Module().(*Module) | 
|  | 308 |  | 
|  | 309 | if !ok { | 
|  | 310 | return nil | 
|  | 311 | } | 
|  | 312 |  | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 313 | // TODO(b/244244438) Create more version information for NDK and APEX variations | 
|  | 314 | // NDK variants | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 315 | if m.IsSdkVariant() { | 
|  | 316 | // TODO(b/249193999) Do not check if module has NDK stubs once all NDK cc_api_library contains ndk variant of cc_api_variant. | 
|  | 317 | if d.hasNDKStubs() { | 
|  | 318 | return d.getNdkVersions() | 
|  | 319 | } | 
|  | 320 | } | 
|  | 321 |  | 
| Kiyoung Kim | aa39480 | 2024-01-08 12:55:45 +0900 | [diff] [blame] | 322 | if d.hasLLNDKStubs() && m.InVendorOrProduct() { | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 323 | // LLNDK libraries only need a single stubs variant. | 
|  | 324 | return []string{android.FutureApiLevel.String()} | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | stubsVersions := d.getStubVersions() | 
|  | 328 |  | 
|  | 329 | if len(stubsVersions) != 0 { | 
|  | 330 | return stubsVersions | 
|  | 331 | } | 
|  | 332 |  | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 333 | if m.MinSdkVersion() == "" { | 
|  | 334 | return nil | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | firstVersion, err := nativeApiLevelFromUser(ctx, | 
|  | 338 | m.MinSdkVersion()) | 
|  | 339 |  | 
|  | 340 | if err != nil { | 
|  | 341 | return nil | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | return ndkLibraryVersions(ctx, firstVersion) | 
|  | 345 | } | 
|  | 346 |  | 
|  | 347 | func (d *apiLibraryDecorator) hasLLNDKStubs() bool { | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 348 | return inList("llndk", d.properties.Variants) | 
|  | 349 | } | 
|  | 350 |  | 
|  | 351 | func (d *apiLibraryDecorator) hasNDKStubs() bool { | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 352 | for _, variant := range d.properties.Variants { | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 353 | if ndkVariantRegex.MatchString(variant) { | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 354 | return true | 
|  | 355 | } | 
|  | 356 | } | 
|  | 357 | return false | 
|  | 358 | } | 
|  | 359 |  | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 360 | func (d *apiLibraryDecorator) getNdkVersions() []string { | 
|  | 361 | ndkVersions := []string{} | 
|  | 362 |  | 
|  | 363 | for _, variant := range d.properties.Variants { | 
|  | 364 | if match := ndkVariantRegex.FindStringSubmatch(variant); len(match) == 2 { | 
|  | 365 | ndkVersions = append(ndkVersions, match[1]) | 
|  | 366 | } | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | return ndkVersions | 
|  | 370 | } | 
|  | 371 |  | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 372 | func (d *apiLibraryDecorator) getStubVersions() []string { | 
|  | 373 | stubVersions := []string{} | 
|  | 374 |  | 
|  | 375 | for _, variant := range d.properties.Variants { | 
|  | 376 | if match := stubVariantRegex.FindStringSubmatch(variant); len(match) == 2 { | 
|  | 377 | stubVersions = append(stubVersions, match[1]) | 
|  | 378 | } | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | return stubVersions | 
|  | 382 | } | 
|  | 383 |  | 
| Kiyoung Kim | 51279d3 | 2022-08-24 14:10:46 +0900 | [diff] [blame] | 384 | // 'cc_api_headers' is similar with 'cc_api_library', but which replaces | 
|  | 385 | // header libraries. The module will replace any dependencies to existing | 
|  | 386 | // original header libraries. | 
|  | 387 | type apiHeadersDecorator struct { | 
|  | 388 | *libraryDecorator | 
|  | 389 | } | 
|  | 390 |  | 
|  | 391 | func CcApiHeadersFactory() android.Module { | 
|  | 392 | module, decorator := NewLibrary(android.DeviceSupported) | 
|  | 393 | apiHeadersDecorator := &apiHeadersDecorator{ | 
|  | 394 | libraryDecorator: decorator, | 
|  | 395 | } | 
|  | 396 | apiHeadersDecorator.HeaderOnly() | 
|  | 397 |  | 
|  | 398 | module.stl = nil | 
|  | 399 | module.sanitize = nil | 
|  | 400 | decorator.disableStripping() | 
|  | 401 |  | 
|  | 402 | module.compiler = nil | 
|  | 403 | module.linker = apiHeadersDecorator | 
|  | 404 | module.installer = nil | 
|  | 405 |  | 
| Kiyoung Kim | 51279d3 | 2022-08-24 14:10:46 +0900 | [diff] [blame] | 406 | // Prevent default system libs (libc, libm, and libdl) from being linked | 
|  | 407 | if apiHeadersDecorator.baseLinker.Properties.System_shared_libs == nil { | 
|  | 408 | apiHeadersDecorator.baseLinker.Properties.System_shared_libs = []string{} | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | apiHeadersDecorator.baseLinker.Properties.No_libcrt = BoolPtr(true) | 
|  | 412 | apiHeadersDecorator.baseLinker.Properties.Nocrt = BoolPtr(true) | 
|  | 413 |  | 
|  | 414 | module.Init() | 
|  | 415 |  | 
|  | 416 | return module | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | func (d *apiHeadersDecorator) Name(basename string) string { | 
|  | 420 | return basename + multitree.GetApiImportSuffix() | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | func (d *apiHeadersDecorator) availableFor(what string) bool { | 
|  | 424 | // Stub from API surface should be available for any APEX. | 
|  | 425 | return true | 
| Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 426 | } | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 427 |  | 
|  | 428 | type ccApiexportProperties struct { | 
|  | 429 | Src     *string `android:"arch_variant"` | 
|  | 430 | Variant *string | 
|  | 431 | Version *string | 
|  | 432 | } | 
|  | 433 |  | 
|  | 434 | type variantExporterProperties struct { | 
| Kiyoung Kim | 3d776f4 | 2022-11-11 12:35:01 +0900 | [diff] [blame] | 435 | // Header directory to export | 
| Kiyoung Kim | 62ed3dd | 2022-12-02 10:20:55 +0900 | [diff] [blame] | 436 | Export_include_dirs []string `android:"arch_variant"` | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 437 |  | 
|  | 438 | // Export all headers as system include | 
|  | 439 | Export_headers_as_system *bool | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | type CcApiVariant struct { | 
|  | 443 | android.ModuleBase | 
|  | 444 |  | 
|  | 445 | properties       ccApiexportProperties | 
|  | 446 | exportProperties variantExporterProperties | 
|  | 447 |  | 
|  | 448 | src android.Path | 
|  | 449 | } | 
|  | 450 |  | 
|  | 451 | var _ android.Module = (*CcApiVariant)(nil) | 
|  | 452 | var _ android.ImageInterface = (*CcApiVariant)(nil) | 
|  | 453 |  | 
|  | 454 | func CcApiVariantFactory() android.Module { | 
|  | 455 | module := &CcApiVariant{} | 
|  | 456 |  | 
|  | 457 | module.AddProperties(&module.properties) | 
|  | 458 | module.AddProperties(&module.exportProperties) | 
|  | 459 |  | 
|  | 460 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth) | 
|  | 461 | return module | 
|  | 462 | } | 
|  | 463 |  | 
|  | 464 | func (v *CcApiVariant) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 465 | // No need to build | 
|  | 466 |  | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 467 | if String(v.properties.Src) == "" { | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 468 | ctx.PropertyErrorf("src", "src is a required property") | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | // Skip the existence check of the stub prebuilt file. | 
|  | 472 | // The file is not guaranteed to exist during Soong analysis. | 
|  | 473 | // Build orchestrator will be responsible for creating a connected ninja graph. | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 474 | v.src = android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), String(v.properties.Src)) | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 475 | } | 
|  | 476 |  | 
|  | 477 | func (v *CcApiVariant) Name() string { | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 478 | version := String(v.properties.Version) | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 479 | return BuildApiVariantName(v.BaseModuleName(), *v.properties.Variant, version) | 
|  | 480 | } | 
|  | 481 |  | 
|  | 482 | func (v *CcApiVariant) Src() android.Path { | 
|  | 483 | return v.src | 
|  | 484 | } | 
|  | 485 |  | 
|  | 486 | func BuildApiVariantName(baseName string, variant string, version string) string { | 
|  | 487 | names := []string{baseName, variant} | 
|  | 488 | if version != "" { | 
|  | 489 | names = append(names, version) | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | return strings.Join(names[:], ".") + multitree.GetApiImportSuffix() | 
|  | 493 | } | 
|  | 494 |  | 
|  | 495 | // Implement ImageInterface to generate image variants | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 496 | func (v *CcApiVariant) ImageMutatorBegin(ctx android.BaseModuleContext) {} | 
| Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 497 | func (v *CcApiVariant) VendorVariantNeeded(ctx android.BaseModuleContext) bool { | 
|  | 498 | return String(v.properties.Variant) == "llndk" | 
|  | 499 | } | 
|  | 500 | func (v *CcApiVariant) ProductVariantNeeded(ctx android.BaseModuleContext) bool { | 
|  | 501 | return String(v.properties.Variant) == "llndk" | 
|  | 502 | } | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 503 | func (v *CcApiVariant) CoreVariantNeeded(ctx android.BaseModuleContext) bool { | 
| Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 504 | return inList(String(v.properties.Variant), []string{"ndk", "apex"}) | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 505 | } | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 506 | func (v *CcApiVariant) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool       { return false } | 
|  | 507 | func (v *CcApiVariant) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { return false } | 
|  | 508 | func (v *CcApiVariant) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool  { return false } | 
|  | 509 | func (v *CcApiVariant) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool      { return false } | 
| Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 510 | func (v *CcApiVariant) ExtraImageVariations(ctx android.BaseModuleContext) []string   { return nil } | 
| Jihoon Kang | 7583e83 | 2024-06-13 21:25:45 +0000 | [diff] [blame] | 511 | func (v *CcApiVariant) SetImageVariation(ctx android.BaseModuleContext, variation string) { | 
| Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 512 | } |