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 ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/multitree" |
| 20 | ) |
| 21 | |
| 22 | func init() { |
| 23 | RegisterLibraryStubBuildComponents(android.InitRegistrationContext) |
| 24 | } |
| 25 | |
| 26 | func RegisterLibraryStubBuildComponents(ctx android.RegistrationContext) { |
Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 27 | ctx.RegisterModuleType("cc_api_library", CcApiLibraryFactory) |
Kiyoung Kim | 51279d3 | 2022-08-24 14:10:46 +0900 | [diff] [blame] | 28 | ctx.RegisterModuleType("cc_api_headers", CcApiHeadersFactory) |
Inseob Kim | 5eb7ee9 | 2022-04-27 10:30:34 +0900 | [diff] [blame] | 29 | } |
| 30 | |
Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 31 | // 'cc_api_library' is a module type which is from the exported API surface |
| 32 | // with C shared library type. The module will replace original module, and |
| 33 | // offer a link to the module that generates shared library object from the |
| 34 | // map file. |
| 35 | type apiLibraryProperties struct { |
| 36 | Src *string `android:"arch_variant"` |
| 37 | } |
| 38 | |
| 39 | type apiLibraryDecorator struct { |
| 40 | *libraryDecorator |
| 41 | properties apiLibraryProperties |
| 42 | } |
| 43 | |
| 44 | func CcApiLibraryFactory() android.Module { |
| 45 | module, decorator := NewLibrary(android.DeviceSupported) |
| 46 | apiLibraryDecorator := &apiLibraryDecorator{ |
| 47 | libraryDecorator: decorator, |
| 48 | } |
| 49 | apiLibraryDecorator.BuildOnlyShared() |
| 50 | |
| 51 | module.stl = nil |
| 52 | module.sanitize = nil |
| 53 | decorator.disableStripping() |
| 54 | |
| 55 | module.compiler = nil |
| 56 | module.linker = apiLibraryDecorator |
| 57 | module.installer = nil |
| 58 | module.AddProperties(&module.Properties, &apiLibraryDecorator.properties) |
| 59 | |
| 60 | // Mark module as stub, so APEX would not include this stub in the package. |
| 61 | module.library.setBuildStubs(true) |
| 62 | |
| 63 | // Prevent default system libs (libc, libm, and libdl) from being linked |
| 64 | if apiLibraryDecorator.baseLinker.Properties.System_shared_libs == nil { |
| 65 | apiLibraryDecorator.baseLinker.Properties.System_shared_libs = []string{} |
| 66 | } |
| 67 | |
Kiyoung Kim | 835c589 | 2022-08-17 16:40:16 +0900 | [diff] [blame] | 68 | apiLibraryDecorator.baseLinker.Properties.No_libcrt = BoolPtr(true) |
| 69 | apiLibraryDecorator.baseLinker.Properties.Nocrt = BoolPtr(true) |
| 70 | |
Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 71 | module.Init() |
| 72 | |
| 73 | return module |
| 74 | } |
| 75 | |
| 76 | func (d *apiLibraryDecorator) Name(basename string) string { |
| 77 | return basename + multitree.GetApiImportSuffix() |
| 78 | } |
| 79 | |
Spandan Das | f0beebc | 2022-10-18 18:23:28 +0000 | [diff] [blame] | 80 | // Export include dirs without checking for existence. |
| 81 | // The directories are not guaranteed to exist during Soong analysis. |
| 82 | func (d *apiLibraryDecorator) exportIncludes(ctx ModuleContext) { |
| 83 | exporterProps := d.flagExporter.Properties |
| 84 | for _, dir := range exporterProps.Export_include_dirs { |
Spandan Das | c6c10fa | 2022-10-21 21:52:13 +0000 | [diff] [blame] | 85 | d.dirs = append(d.dirs, android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), dir)) |
Spandan Das | f0beebc | 2022-10-18 18:23:28 +0000 | [diff] [blame] | 86 | } |
| 87 | // system headers |
| 88 | for _, dir := range exporterProps.Export_system_include_dirs { |
Spandan Das | c6c10fa | 2022-10-21 21:52:13 +0000 | [diff] [blame] | 89 | d.systemDirs = append(d.systemDirs, android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), dir)) |
Spandan Das | f0beebc | 2022-10-18 18:23:28 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 93 | func (d *apiLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objects Objects) android.Path { |
Kiyoung Kim | 51279d3 | 2022-08-24 14:10:46 +0900 | [diff] [blame] | 94 | // Export headers as system include dirs if specified. Mostly for libc |
| 95 | if Bool(d.libraryDecorator.Properties.Llndk.Export_headers_as_system) { |
| 96 | d.libraryDecorator.flagExporter.Properties.Export_system_include_dirs = append( |
| 97 | d.libraryDecorator.flagExporter.Properties.Export_system_include_dirs, |
| 98 | d.libraryDecorator.flagExporter.Properties.Export_include_dirs...) |
| 99 | d.libraryDecorator.flagExporter.Properties.Export_include_dirs = nil |
| 100 | } |
| 101 | |
Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 102 | // Flags reexported from dependencies. (e.g. vndk_prebuilt_shared) |
Spandan Das | f0beebc | 2022-10-18 18:23:28 +0000 | [diff] [blame] | 103 | d.exportIncludes(ctx) |
Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 104 | d.libraryDecorator.reexportDirs(deps.ReexportedDirs...) |
| 105 | d.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...) |
| 106 | d.libraryDecorator.reexportFlags(deps.ReexportedFlags...) |
| 107 | d.libraryDecorator.reexportDeps(deps.ReexportedDeps...) |
| 108 | d.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...) |
Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 109 | |
Spandan Das | f0beebc | 2022-10-18 18:23:28 +0000 | [diff] [blame] | 110 | if d.properties.Src == nil { |
| 111 | ctx.PropertyErrorf("src", "src is a required property") |
| 112 | } |
| 113 | // Skip the existence check of the stub prebuilt file. |
| 114 | // The file is not guaranteed to exist during Soong analysis. |
| 115 | // Build orchestrator will be responsible for creating a connected ninja graph. |
Spandan Das | c6c10fa | 2022-10-21 21:52:13 +0000 | [diff] [blame] | 116 | in := android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), *d.properties.Src) |
Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 117 | |
Spandan Das | a3c8a17 | 2022-10-26 20:54:32 +0000 | [diff] [blame^] | 118 | // Make the _compilation_ of rdeps have an order-only dep on cc_api_library.src (an .so file) |
| 119 | // The .so file itself has an order-only dependency on the headers contributed by this library. |
| 120 | // Creating this dependency ensures that the headers are assembled before compilation of rdeps begins. |
| 121 | d.libraryDecorator.reexportDeps(in) |
| 122 | d.libraryDecorator.flagExporter.setProvider(ctx) |
| 123 | |
Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 124 | d.unstrippedOutputFile = in |
| 125 | libName := d.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix() |
| 126 | |
| 127 | tocFile := android.PathForModuleOut(ctx, libName+".toc") |
| 128 | d.tocFile = android.OptionalPathForPath(tocFile) |
| 129 | TransformSharedObjectToToc(ctx, in, tocFile) |
| 130 | |
| 131 | ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{ |
| 132 | SharedLibrary: in, |
| 133 | Target: ctx.Target(), |
| 134 | |
| 135 | TableOfContents: d.tocFile, |
| 136 | }) |
| 137 | |
| 138 | return in |
| 139 | } |
| 140 | |
| 141 | func (d *apiLibraryDecorator) availableFor(what string) bool { |
| 142 | // Stub from API surface should be available for any APEX. |
| 143 | return true |
| 144 | } |
| 145 | |
Kiyoung Kim | 51279d3 | 2022-08-24 14:10:46 +0900 | [diff] [blame] | 146 | // 'cc_api_headers' is similar with 'cc_api_library', but which replaces |
| 147 | // header libraries. The module will replace any dependencies to existing |
| 148 | // original header libraries. |
| 149 | type apiHeadersDecorator struct { |
| 150 | *libraryDecorator |
| 151 | } |
| 152 | |
| 153 | func CcApiHeadersFactory() android.Module { |
| 154 | module, decorator := NewLibrary(android.DeviceSupported) |
| 155 | apiHeadersDecorator := &apiHeadersDecorator{ |
| 156 | libraryDecorator: decorator, |
| 157 | } |
| 158 | apiHeadersDecorator.HeaderOnly() |
| 159 | |
| 160 | module.stl = nil |
| 161 | module.sanitize = nil |
| 162 | decorator.disableStripping() |
| 163 | |
| 164 | module.compiler = nil |
| 165 | module.linker = apiHeadersDecorator |
| 166 | module.installer = nil |
| 167 | |
| 168 | // Mark module as stub, so APEX would not include this stub in the package. |
| 169 | module.library.setBuildStubs(true) |
| 170 | |
| 171 | // Prevent default system libs (libc, libm, and libdl) from being linked |
| 172 | if apiHeadersDecorator.baseLinker.Properties.System_shared_libs == nil { |
| 173 | apiHeadersDecorator.baseLinker.Properties.System_shared_libs = []string{} |
| 174 | } |
| 175 | |
| 176 | apiHeadersDecorator.baseLinker.Properties.No_libcrt = BoolPtr(true) |
| 177 | apiHeadersDecorator.baseLinker.Properties.Nocrt = BoolPtr(true) |
| 178 | |
| 179 | module.Init() |
| 180 | |
| 181 | return module |
| 182 | } |
| 183 | |
| 184 | func (d *apiHeadersDecorator) Name(basename string) string { |
| 185 | return basename + multitree.GetApiImportSuffix() |
| 186 | } |
| 187 | |
| 188 | func (d *apiHeadersDecorator) availableFor(what string) bool { |
| 189 | // Stub from API surface should be available for any APEX. |
| 190 | return true |
Kiyoung Kim | 487689e | 2022-07-26 09:48:22 +0900 | [diff] [blame] | 191 | } |