Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 | |
Kiyoung Kim | 37693d0 | 2024-04-04 09:56:15 +0900 | [diff] [blame] | 17 | import ( |
Kiyoung Kim | 9f26fcf | 2024-05-27 17:25:52 +0900 | [diff] [blame] | 18 | "fmt" |
Kiyoung Kim | 37693d0 | 2024-04-04 09:56:15 +0900 | [diff] [blame] | 19 | "strings" |
mrziwang | e2346b8 | 2024-06-10 15:09:45 -0700 | [diff] [blame] | 20 | |
Kiyoung Kim | 37693d0 | 2024-04-04 09:56:15 +0900 | [diff] [blame] | 21 | "android/soong/android" |
| 22 | "android/soong/etc" |
Kiyoung Kim | 37693d0 | 2024-04-04 09:56:15 +0900 | [diff] [blame] | 23 | ) |
| 24 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 25 | var ( |
| 26 | llndkLibrarySuffix = ".llndk" |
| 27 | ) |
| 28 | |
Colin Cross | 127bb8b | 2020-12-16 16:46:01 -0800 | [diff] [blame] | 29 | // Holds properties to describe a stub shared library based on the provided version file. |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 30 | type llndkLibraryProperties struct { |
| 31 | // Relative path to the symbol map. |
| 32 | // An example file can be seen here: TODO(danalbert): Make an example. |
Spandan Das | 02be101 | 2024-07-24 01:21:53 +0000 | [diff] [blame^] | 33 | Symbol_file *string `android:"path,arch_variant"` |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 34 | |
| 35 | // Whether to export any headers as -isystem instead of -I. Mainly for use by |
| 36 | // bionic/libc. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 37 | Export_headers_as_system *bool |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 38 | |
| 39 | // Which headers to process with versioner. This really only handles |
| 40 | // bionic/libc/include right now. |
| 41 | Export_preprocessed_headers []string |
| 42 | |
| 43 | // Whether the system library uses symbol versions. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 44 | Unversioned *bool |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 45 | |
Jiyong Park | 2a45412 | 2017-10-19 15:59:33 +0900 | [diff] [blame] | 46 | // list of llndk headers to re-export include directories from. |
Colin Cross | 0fb7fcd | 2021-03-02 11:00:07 -0800 | [diff] [blame] | 47 | Export_llndk_headers []string |
| 48 | |
| 49 | // list of directories relative to the Blueprints file that willbe added to the include path |
| 50 | // (using -I) for any module that links against the LLNDK variant of this module, replacing |
| 51 | // any that were listed outside the llndk clause. |
| 52 | Override_export_include_dirs []string |
Colin Cross | 127bb8b | 2020-12-16 16:46:01 -0800 | [diff] [blame] | 53 | |
| 54 | // whether this module can be directly depended upon by libs that are installed |
| 55 | // to /vendor and /product. |
| 56 | // When set to true, this module can only be depended on by VNDK libraries, not |
| 57 | // vendor nor product libraries. This effectively hides this module from |
| 58 | // non-system modules. Default value is false. |
| 59 | Private *bool |
Colin Cross | 1f3f130 | 2021-04-26 18:37:44 -0700 | [diff] [blame] | 60 | |
| 61 | // if true, make this module available to provide headers to other modules that set |
| 62 | // llndk.symbol_file. |
| 63 | Llndk_headers *bool |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 64 | } |
Kiyoung Kim | 37693d0 | 2024-04-04 09:56:15 +0900 | [diff] [blame] | 65 | |
| 66 | func makeLlndkVars(ctx android.MakeVarsContext) { |
| 67 | // Make uses LLNDK_MOVED_TO_APEX_LIBRARIES to avoid installing libraries on /system if |
| 68 | // they been moved to an apex. |
| 69 | movedToApexLlndkLibraries := make(map[string]bool) |
| 70 | ctx.VisitAllModules(func(module android.Module) { |
| 71 | if library := moduleLibraryInterface(module); library != nil && library.hasLLNDKStubs() { |
| 72 | // Skip bionic libs, they are handled in different manner |
| 73 | name := library.implementationModuleName(module.(*Module).BaseModuleName()) |
| 74 | if module.(android.ApexModule).DirectlyInAnyApex() && !isBionic(name) { |
| 75 | movedToApexLlndkLibraries[name] = true |
| 76 | } |
| 77 | } |
| 78 | }) |
| 79 | |
| 80 | ctx.Strict("LLNDK_MOVED_TO_APEX_LIBRARIES", |
| 81 | strings.Join(android.SortedKeys(movedToApexLlndkLibraries), " ")) |
| 82 | } |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 83 | |
| 84 | func init() { |
| 85 | RegisterLlndkLibraryTxtType(android.InitRegistrationContext) |
| 86 | } |
| 87 | |
| 88 | func RegisterLlndkLibraryTxtType(ctx android.RegistrationContext) { |
| 89 | ctx.RegisterParallelSingletonModuleType("llndk_libraries_txt", llndkLibrariesTxtFactory) |
| 90 | } |
| 91 | |
| 92 | type llndkLibrariesTxtModule struct { |
| 93 | android.SingletonModuleBase |
| 94 | |
| 95 | outputFile android.OutputPath |
| 96 | moduleNames []string |
| 97 | fileNames []string |
| 98 | } |
| 99 | |
| 100 | var _ etc.PrebuiltEtcModule = &llndkLibrariesTxtModule{} |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 101 | |
| 102 | // llndk_libraries_txt is a singleton module whose content is a list of LLNDK libraries |
| 103 | // generated by Soong but can be referenced by other modules. |
| 104 | // For example, apex_vndk can depend on these files as prebuilt. |
| 105 | // Make uses LLNDK_LIBRARIES to determine which libraries to install. |
| 106 | // HWASAN is only part of the LL-NDK in builds in which libc depends on HWASAN. |
| 107 | // Therefore, by removing the library here, we cause it to only be installed if libc |
| 108 | // depends on it. |
| 109 | func llndkLibrariesTxtFactory() android.SingletonModule { |
| 110 | m := &llndkLibrariesTxtModule{} |
| 111 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 112 | return m |
| 113 | } |
| 114 | |
| 115 | func (txt *llndkLibrariesTxtModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 116 | filename := txt.Name() |
| 117 | |
| 118 | txt.outputFile = android.PathForModuleOut(ctx, filename).OutputPath |
| 119 | |
| 120 | installPath := android.PathForModuleInstall(ctx, "etc") |
| 121 | ctx.InstallFile(installPath, filename, txt.outputFile) |
mrziwang | e2346b8 | 2024-06-10 15:09:45 -0700 | [diff] [blame] | 122 | |
| 123 | ctx.SetOutputFiles(android.Paths{txt.outputFile}, "") |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 124 | } |
| 125 | |
Kiyoung Kim | 9f26fcf | 2024-05-27 17:25:52 +0900 | [diff] [blame] | 126 | func getVndkFileName(m *Module) (string, error) { |
| 127 | if library, ok := m.linker.(*libraryDecorator); ok { |
| 128 | return library.getLibNameHelper(m.BaseModuleName(), true, false) + ".so", nil |
| 129 | } |
| 130 | if prebuilt, ok := m.linker.(*prebuiltLibraryLinker); ok { |
| 131 | return prebuilt.libraryDecorator.getLibNameHelper(m.BaseModuleName(), true, false) + ".so", nil |
| 132 | } |
| 133 | return "", fmt.Errorf("VNDK library should have libraryDecorator or prebuiltLibraryLinker as linker: %T", m.linker) |
| 134 | } |
| 135 | |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 136 | func (txt *llndkLibrariesTxtModule) GenerateSingletonBuildActions(ctx android.SingletonContext) { |
Kiyoung Kim | 0d4a9ca | 2024-05-07 16:11:41 +0900 | [diff] [blame] | 137 | if txt.outputFile.String() == "" { |
| 138 | // Skip if target file path is empty |
| 139 | return |
| 140 | } |
| 141 | |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 142 | ctx.VisitAllModules(func(m android.Module) { |
| 143 | if c, ok := m.(*Module); ok && c.VendorProperties.IsLLNDK && !c.Header() && !c.IsVndkPrebuiltLibrary() { |
| 144 | filename, err := getVndkFileName(c) |
| 145 | if err != nil { |
| 146 | ctx.ModuleErrorf(m, "%s", err) |
| 147 | } |
| 148 | |
| 149 | if !strings.HasPrefix(ctx.ModuleName(m), "libclang_rt.hwasan") { |
| 150 | txt.moduleNames = append(txt.moduleNames, ctx.ModuleName(m)) |
| 151 | } |
| 152 | txt.fileNames = append(txt.fileNames, filename) |
| 153 | } |
| 154 | }) |
| 155 | txt.moduleNames = android.SortedUniqueStrings(txt.moduleNames) |
| 156 | txt.fileNames = android.SortedUniqueStrings(txt.fileNames) |
| 157 | |
| 158 | android.WriteFileRule(ctx, txt.outputFile, strings.Join(txt.fileNames, "\n")) |
| 159 | } |
| 160 | |
| 161 | func (txt *llndkLibrariesTxtModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 162 | return []android.AndroidMkEntries{{ |
| 163 | Class: "ETC", |
| 164 | OutputFile: android.OptionalPathForPath(txt.outputFile), |
| 165 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 166 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 167 | entries.SetString("LOCAL_MODULE_STEM", txt.outputFile.Base()) |
| 168 | }, |
| 169 | }, |
| 170 | }} |
| 171 | } |
| 172 | |
| 173 | func (txt *llndkLibrariesTxtModule) MakeVars(ctx android.MakeVarsContext) { |
| 174 | ctx.Strict("LLNDK_LIBRARIES", strings.Join(txt.moduleNames, " ")) |
| 175 | } |
| 176 | |
| 177 | // PrebuiltEtcModule interface |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 178 | func (txt *llndkLibrariesTxtModule) BaseDir() string { |
| 179 | return "etc" |
| 180 | } |
| 181 | |
| 182 | // PrebuiltEtcModule interface |
| 183 | func (txt *llndkLibrariesTxtModule) SubDir() string { |
| 184 | return "" |
| 185 | } |
| 186 | |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 187 | func llndkMutator(mctx android.BottomUpMutatorContext) { |
| 188 | m, ok := mctx.Module().(*Module) |
| 189 | if !ok { |
| 190 | return |
| 191 | } |
| 192 | |
Cole Faust | 021bf3d | 2024-05-01 16:59:00 -0700 | [diff] [blame] | 193 | if shouldSkipLlndkMutator(mctx, m) { |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 194 | return |
| 195 | } |
| 196 | |
| 197 | lib, isLib := m.linker.(*libraryDecorator) |
| 198 | prebuiltLib, isPrebuiltLib := m.linker.(*prebuiltLibraryLinker) |
| 199 | |
| 200 | if m.InVendorOrProduct() && isLib && lib.hasLLNDKStubs() { |
| 201 | m.VendorProperties.IsLLNDK = true |
| 202 | } |
| 203 | if m.InVendorOrProduct() && isPrebuiltLib && prebuiltLib.hasLLNDKStubs() { |
| 204 | m.VendorProperties.IsLLNDK = true |
| 205 | } |
| 206 | |
Kiyoung Kim | 9f26fcf | 2024-05-27 17:25:52 +0900 | [diff] [blame] | 207 | if vndkprebuilt, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok { |
| 208 | if !Bool(vndkprebuilt.properties.Vndk.Enabled) { |
| 209 | m.VendorProperties.IsLLNDK = true |
| 210 | } |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | // Check for modules that mustn't be LLNDK |
Cole Faust | 021bf3d | 2024-05-01 16:59:00 -0700 | [diff] [blame] | 215 | func shouldSkipLlndkMutator(mctx android.BottomUpMutatorContext, m *Module) bool { |
| 216 | if !m.Enabled(mctx) { |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 217 | return true |
| 218 | } |
| 219 | if !m.Device() { |
| 220 | return true |
| 221 | } |
| 222 | if m.Target().NativeBridge == android.NativeBridgeEnabled { |
| 223 | return true |
| 224 | } |
| 225 | return false |
| 226 | } |