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