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 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 39 | // Whether the system library uses symbol versions. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 40 | Unversioned *bool |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 41 | |
Jiyong Park | 2a45412 | 2017-10-19 15:59:33 +0900 | [diff] [blame] | 42 | // list of llndk headers to re-export include directories from. |
Colin Cross | 0fb7fcd | 2021-03-02 11:00:07 -0800 | [diff] [blame] | 43 | Export_llndk_headers []string |
| 44 | |
| 45 | // list of directories relative to the Blueprints file that willbe added to the include path |
| 46 | // (using -I) for any module that links against the LLNDK variant of this module, replacing |
| 47 | // any that were listed outside the llndk clause. |
| 48 | Override_export_include_dirs []string |
Colin Cross | 127bb8b | 2020-12-16 16:46:01 -0800 | [diff] [blame] | 49 | |
| 50 | // whether this module can be directly depended upon by libs that are installed |
| 51 | // to /vendor and /product. |
| 52 | // When set to true, this module can only be depended on by VNDK libraries, not |
| 53 | // vendor nor product libraries. This effectively hides this module from |
| 54 | // non-system modules. Default value is false. |
| 55 | Private *bool |
Colin Cross | 1f3f130 | 2021-04-26 18:37:44 -0700 | [diff] [blame] | 56 | |
| 57 | // if true, make this module available to provide headers to other modules that set |
| 58 | // llndk.symbol_file. |
| 59 | Llndk_headers *bool |
Colin Cross | 0510f54 | 2024-11-13 16:07:08 -0800 | [diff] [blame] | 60 | |
| 61 | // moved_to_apex marks this module has having been distributed through an apex module. |
| 62 | Moved_to_apex *bool |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 63 | } |
Kiyoung Kim | 37693d0 | 2024-04-04 09:56:15 +0900 | [diff] [blame] | 64 | |
| 65 | func makeLlndkVars(ctx android.MakeVarsContext) { |
Kiyoung Kim | 37693d0 | 2024-04-04 09:56:15 +0900 | [diff] [blame] | 66 | |
Kiyoung Kim | 37693d0 | 2024-04-04 09:56:15 +0900 | [diff] [blame] | 67 | } |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 68 | |
| 69 | func init() { |
| 70 | RegisterLlndkLibraryTxtType(android.InitRegistrationContext) |
Cole Faust | b43793e | 2024-12-13 16:53:36 -0800 | [diff] [blame] | 71 | android.RegisterParallelSingletonType("movedToApexLlndkLibraries", movedToApexLlndkLibrariesFactory) |
| 72 | } |
| 73 | |
| 74 | func movedToApexLlndkLibrariesFactory() android.Singleton { |
| 75 | return &movedToApexLlndkLibraries{} |
| 76 | } |
| 77 | |
| 78 | type movedToApexLlndkLibraries struct { |
| 79 | movedToApexLlndkLibraries []string |
| 80 | } |
| 81 | |
| 82 | func (s *movedToApexLlndkLibraries) GenerateBuildActions(ctx android.SingletonContext) { |
| 83 | // Make uses LLNDK_MOVED_TO_APEX_LIBRARIES to generate the linker config. |
| 84 | movedToApexLlndkLibrariesMap := make(map[string]bool) |
Yu Liu | 8840812 | 2025-03-04 18:46:24 +0000 | [diff] [blame] | 85 | ctx.VisitAllModuleProxies(func(module android.ModuleProxy) { |
| 86 | if library, ok := android.OtherModuleProvider(ctx, module, LinkableInfoProvider); ok { |
| 87 | if library.HasLLNDKStubs && library.IsLLNDKMovedToApex { |
| 88 | movedToApexLlndkLibrariesMap[library.ImplementationModuleName] = true |
Cole Faust | b43793e | 2024-12-13 16:53:36 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | }) |
| 92 | s.movedToApexLlndkLibraries = android.SortedKeys(movedToApexLlndkLibrariesMap) |
| 93 | |
| 94 | var sb strings.Builder |
| 95 | for i, l := range s.movedToApexLlndkLibraries { |
| 96 | if i > 0 { |
| 97 | sb.WriteRune(' ') |
| 98 | } |
| 99 | sb.WriteString(l) |
| 100 | sb.WriteString(".so") |
| 101 | } |
| 102 | android.WriteFileRule(ctx, MovedToApexLlndkLibrariesFile(ctx), sb.String()) |
| 103 | } |
| 104 | |
| 105 | func MovedToApexLlndkLibrariesFile(ctx android.PathContext) android.WritablePath { |
| 106 | return android.PathForIntermediates(ctx, "moved_to_apex_llndk_libraries.txt") |
| 107 | } |
| 108 | |
| 109 | func (s *movedToApexLlndkLibraries) MakeVars(ctx android.MakeVarsContext) { |
| 110 | ctx.Strict("LLNDK_MOVED_TO_APEX_LIBRARIES", strings.Join(s.movedToApexLlndkLibraries, " ")) |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | func RegisterLlndkLibraryTxtType(ctx android.RegistrationContext) { |
| 114 | ctx.RegisterParallelSingletonModuleType("llndk_libraries_txt", llndkLibrariesTxtFactory) |
| 115 | } |
| 116 | |
| 117 | type llndkLibrariesTxtModule struct { |
| 118 | android.SingletonModuleBase |
| 119 | |
| 120 | outputFile android.OutputPath |
| 121 | moduleNames []string |
| 122 | fileNames []string |
| 123 | } |
| 124 | |
| 125 | var _ etc.PrebuiltEtcModule = &llndkLibrariesTxtModule{} |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 126 | |
| 127 | // llndk_libraries_txt is a singleton module whose content is a list of LLNDK libraries |
| 128 | // generated by Soong but can be referenced by other modules. |
| 129 | // For example, apex_vndk can depend on these files as prebuilt. |
| 130 | // Make uses LLNDK_LIBRARIES to determine which libraries to install. |
| 131 | // HWASAN is only part of the LL-NDK in builds in which libc depends on HWASAN. |
| 132 | // Therefore, by removing the library here, we cause it to only be installed if libc |
| 133 | // depends on it. |
| 134 | func llndkLibrariesTxtFactory() android.SingletonModule { |
| 135 | m := &llndkLibrariesTxtModule{} |
| 136 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 137 | return m |
| 138 | } |
| 139 | |
| 140 | func (txt *llndkLibrariesTxtModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 141 | filename := txt.Name() |
| 142 | |
| 143 | txt.outputFile = android.PathForModuleOut(ctx, filename).OutputPath |
| 144 | |
| 145 | installPath := android.PathForModuleInstall(ctx, "etc") |
| 146 | ctx.InstallFile(installPath, filename, txt.outputFile) |
mrziwang | e2346b8 | 2024-06-10 15:09:45 -0700 | [diff] [blame] | 147 | |
| 148 | ctx.SetOutputFiles(android.Paths{txt.outputFile}, "") |
Yu Liu | 0a37d42 | 2025-02-13 02:05:00 +0000 | [diff] [blame] | 149 | |
| 150 | etc.SetCommonPrebuiltEtcInfo(ctx, txt) |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 151 | } |
| 152 | |
Yu Liu | 8840812 | 2025-03-04 18:46:24 +0000 | [diff] [blame] | 153 | func getVndkFileName(info *LinkerInfo) (string, error) { |
| 154 | if info != nil { |
| 155 | if info.LibraryDecoratorInfo != nil { |
| 156 | return info.LibraryDecoratorInfo.VndkFileName, nil |
| 157 | } |
| 158 | if info.PrebuiltLibraryLinkerInfo != nil { |
| 159 | return info.PrebuiltLibraryLinkerInfo.VndkFileName, nil |
| 160 | } |
Kiyoung Kim | 9f26fcf | 2024-05-27 17:25:52 +0900 | [diff] [blame] | 161 | } |
Yu Liu | 8840812 | 2025-03-04 18:46:24 +0000 | [diff] [blame] | 162 | return "", fmt.Errorf("VNDK library should have libraryDecorator or prebuiltLibraryLinker as linker: %T", info) |
Kiyoung Kim | 9f26fcf | 2024-05-27 17:25:52 +0900 | [diff] [blame] | 163 | } |
| 164 | |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 165 | func (txt *llndkLibrariesTxtModule) GenerateSingletonBuildActions(ctx android.SingletonContext) { |
Kiyoung Kim | 0d4a9ca | 2024-05-07 16:11:41 +0900 | [diff] [blame] | 166 | if txt.outputFile.String() == "" { |
| 167 | // Skip if target file path is empty |
| 168 | return |
| 169 | } |
| 170 | |
Yu Liu | 8840812 | 2025-03-04 18:46:24 +0000 | [diff] [blame] | 171 | ctx.VisitAllModuleProxies(func(m android.ModuleProxy) { |
| 172 | ccInfo, ok := android.OtherModuleProvider(ctx, m, CcInfoProvider) |
| 173 | if !ok { |
| 174 | return |
| 175 | } |
| 176 | linkableInfo, ok := android.OtherModuleProvider(ctx, m, LinkableInfoProvider) |
| 177 | if !ok { |
| 178 | return |
| 179 | } |
| 180 | if linkableInfo.IsLlndk && !linkableInfo.Header && !linkableInfo.IsVndkPrebuiltLibrary { |
| 181 | filename, err := getVndkFileName(ccInfo.LinkerInfo) |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 182 | if err != nil { |
| 183 | ctx.ModuleErrorf(m, "%s", err) |
| 184 | } |
| 185 | |
| 186 | if !strings.HasPrefix(ctx.ModuleName(m), "libclang_rt.hwasan") { |
| 187 | txt.moduleNames = append(txt.moduleNames, ctx.ModuleName(m)) |
| 188 | } |
| 189 | txt.fileNames = append(txt.fileNames, filename) |
| 190 | } |
| 191 | }) |
| 192 | txt.moduleNames = android.SortedUniqueStrings(txt.moduleNames) |
| 193 | txt.fileNames = android.SortedUniqueStrings(txt.fileNames) |
| 194 | |
| 195 | android.WriteFileRule(ctx, txt.outputFile, strings.Join(txt.fileNames, "\n")) |
| 196 | } |
| 197 | |
| 198 | func (txt *llndkLibrariesTxtModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 199 | return []android.AndroidMkEntries{{ |
| 200 | Class: "ETC", |
| 201 | OutputFile: android.OptionalPathForPath(txt.outputFile), |
| 202 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 203 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 204 | entries.SetString("LOCAL_MODULE_STEM", txt.outputFile.Base()) |
| 205 | }, |
| 206 | }, |
| 207 | }} |
| 208 | } |
| 209 | |
| 210 | func (txt *llndkLibrariesTxtModule) MakeVars(ctx android.MakeVarsContext) { |
| 211 | ctx.Strict("LLNDK_LIBRARIES", strings.Join(txt.moduleNames, " ")) |
| 212 | } |
| 213 | |
| 214 | // PrebuiltEtcModule interface |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 215 | func (txt *llndkLibrariesTxtModule) BaseDir() string { |
| 216 | return "etc" |
| 217 | } |
| 218 | |
| 219 | // PrebuiltEtcModule interface |
| 220 | func (txt *llndkLibrariesTxtModule) SubDir() string { |
| 221 | return "" |
| 222 | } |
| 223 | |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 224 | func llndkMutator(mctx android.BottomUpMutatorContext) { |
| 225 | m, ok := mctx.Module().(*Module) |
| 226 | if !ok { |
| 227 | return |
| 228 | } |
| 229 | |
Cole Faust | 021bf3d | 2024-05-01 16:59:00 -0700 | [diff] [blame] | 230 | if shouldSkipLlndkMutator(mctx, m) { |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 231 | return |
| 232 | } |
| 233 | |
| 234 | lib, isLib := m.linker.(*libraryDecorator) |
| 235 | prebuiltLib, isPrebuiltLib := m.linker.(*prebuiltLibraryLinker) |
| 236 | |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 237 | if m.InVendorOrProduct() && isLib && lib.HasLLNDKStubs() { |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 238 | m.VendorProperties.IsLLNDK = true |
| 239 | } |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 240 | if m.InVendorOrProduct() && isPrebuiltLib && prebuiltLib.HasLLNDKStubs() { |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 241 | m.VendorProperties.IsLLNDK = true |
| 242 | } |
| 243 | |
Kiyoung Kim | 9f26fcf | 2024-05-27 17:25:52 +0900 | [diff] [blame] | 244 | if vndkprebuilt, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok { |
| 245 | if !Bool(vndkprebuilt.properties.Vndk.Enabled) { |
| 246 | m.VendorProperties.IsLLNDK = true |
| 247 | } |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | // Check for modules that mustn't be LLNDK |
Cole Faust | 021bf3d | 2024-05-01 16:59:00 -0700 | [diff] [blame] | 252 | func shouldSkipLlndkMutator(mctx android.BottomUpMutatorContext, m *Module) bool { |
| 253 | if !m.Enabled(mctx) { |
Kiyoung Kim | 973cb6f | 2024-04-29 14:14:53 +0900 | [diff] [blame] | 254 | return true |
| 255 | } |
| 256 | if !m.Device() { |
| 257 | return true |
| 258 | } |
| 259 | if m.Target().NativeBridge == android.NativeBridgeEnabled { |
| 260 | return true |
| 261 | } |
| 262 | return false |
| 263 | } |