blob: 5c5933154a1faac2458245fb498673b08743e31c [file] [log] [blame]
Dan Willemsenb916b802017-03-19 13:44:32 -07001// 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
15package cc
16
Kiyoung Kim37693d02024-04-04 09:56:15 +090017import (
18 "android/soong/android"
Kiyoung Kim973cb6f2024-04-29 14:14:53 +090019 "android/soong/etc"
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +090020 "fmt"
Kiyoung Kim37693d02024-04-04 09:56:15 +090021 "strings"
22)
23
Dan Willemsenb916b802017-03-19 13:44:32 -070024var (
25 llndkLibrarySuffix = ".llndk"
26)
27
Colin Cross127bb8b2020-12-16 16:46:01 -080028// Holds properties to describe a stub shared library based on the provided version file.
Dan Willemsenb916b802017-03-19 13:44:32 -070029type llndkLibraryProperties struct {
30 // Relative path to the symbol map.
31 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080032 Symbol_file *string
Dan Willemsenb916b802017-03-19 13:44:32 -070033
34 // Whether to export any headers as -isystem instead of -I. Mainly for use by
35 // bionic/libc.
Nan Zhang0007d812017-11-07 10:57:05 -080036 Export_headers_as_system *bool
Dan Willemsenb916b802017-03-19 13:44:32 -070037
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 Zhang0007d812017-11-07 10:57:05 -080043 Unversioned *bool
Jiyong Park82e2bf32017-08-16 14:05:54 +090044
Jiyong Park2a454122017-10-19 15:59:33 +090045 // list of llndk headers to re-export include directories from.
Colin Cross0fb7fcd2021-03-02 11:00:07 -080046 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 Cross127bb8b2020-12-16 16:46:01 -080052
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 Cross1f3f1302021-04-26 18:37:44 -070059
60 // if true, make this module available to provide headers to other modules that set
61 // llndk.symbol_file.
62 Llndk_headers *bool
Dan Willemsenb916b802017-03-19 13:44:32 -070063}
Kiyoung Kim37693d02024-04-04 09:56:15 +090064
65func 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 Kim973cb6f2024-04-29 14:14:53 +090082
83func init() {
84 RegisterLlndkLibraryTxtType(android.InitRegistrationContext)
85}
86
87func RegisterLlndkLibraryTxtType(ctx android.RegistrationContext) {
88 ctx.RegisterParallelSingletonModuleType("llndk_libraries_txt", llndkLibrariesTxtFactory)
89}
90
91type llndkLibrariesTxtModule struct {
92 android.SingletonModuleBase
93
94 outputFile android.OutputPath
95 moduleNames []string
96 fileNames []string
97}
98
99var _ etc.PrebuiltEtcModule = &llndkLibrariesTxtModule{}
100var _ android.OutputFileProducer = &llndkLibrariesTxtModule{}
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.
109func llndkLibrariesTxtFactory() android.SingletonModule {
110 m := &llndkLibrariesTxtModule{}
111 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
112 return m
113}
114
115func (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)
122}
123
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +0900124func getVndkFileName(m *Module) (string, error) {
125 if library, ok := m.linker.(*libraryDecorator); ok {
126 return library.getLibNameHelper(m.BaseModuleName(), true, false) + ".so", nil
127 }
128 if prebuilt, ok := m.linker.(*prebuiltLibraryLinker); ok {
129 return prebuilt.libraryDecorator.getLibNameHelper(m.BaseModuleName(), true, false) + ".so", nil
130 }
131 return "", fmt.Errorf("VNDK library should have libraryDecorator or prebuiltLibraryLinker as linker: %T", m.linker)
132}
133
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900134func (txt *llndkLibrariesTxtModule) GenerateSingletonBuildActions(ctx android.SingletonContext) {
Kiyoung Kim0d4a9ca2024-05-07 16:11:41 +0900135 if txt.outputFile.String() == "" {
136 // Skip if target file path is empty
137 return
138 }
139
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900140 ctx.VisitAllModules(func(m android.Module) {
141 if c, ok := m.(*Module); ok && c.VendorProperties.IsLLNDK && !c.Header() && !c.IsVndkPrebuiltLibrary() {
142 filename, err := getVndkFileName(c)
143 if err != nil {
144 ctx.ModuleErrorf(m, "%s", err)
145 }
146
147 if !strings.HasPrefix(ctx.ModuleName(m), "libclang_rt.hwasan") {
148 txt.moduleNames = append(txt.moduleNames, ctx.ModuleName(m))
149 }
150 txt.fileNames = append(txt.fileNames, filename)
151 }
152 })
153 txt.moduleNames = android.SortedUniqueStrings(txt.moduleNames)
154 txt.fileNames = android.SortedUniqueStrings(txt.fileNames)
155
156 android.WriteFileRule(ctx, txt.outputFile, strings.Join(txt.fileNames, "\n"))
157}
158
159func (txt *llndkLibrariesTxtModule) AndroidMkEntries() []android.AndroidMkEntries {
160 return []android.AndroidMkEntries{{
161 Class: "ETC",
162 OutputFile: android.OptionalPathForPath(txt.outputFile),
163 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
164 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
165 entries.SetString("LOCAL_MODULE_STEM", txt.outputFile.Base())
166 },
167 },
168 }}
169}
170
171func (txt *llndkLibrariesTxtModule) MakeVars(ctx android.MakeVarsContext) {
172 ctx.Strict("LLNDK_LIBRARIES", strings.Join(txt.moduleNames, " "))
173}
174
175// PrebuiltEtcModule interface
176func (txt *llndkLibrariesTxtModule) OutputFile() android.OutputPath {
177 return txt.outputFile
178}
179
180// PrebuiltEtcModule interface
181func (txt *llndkLibrariesTxtModule) BaseDir() string {
182 return "etc"
183}
184
185// PrebuiltEtcModule interface
186func (txt *llndkLibrariesTxtModule) SubDir() string {
187 return ""
188}
189
190func (txt *llndkLibrariesTxtModule) OutputFiles(tag string) (android.Paths, error) {
191 return android.Paths{txt.outputFile}, nil
192}
193
194func llndkMutator(mctx android.BottomUpMutatorContext) {
195 m, ok := mctx.Module().(*Module)
196 if !ok {
197 return
198 }
199
Cole Faust021bf3d2024-05-01 16:59:00 -0700200 if shouldSkipLlndkMutator(mctx, m) {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900201 return
202 }
203
204 lib, isLib := m.linker.(*libraryDecorator)
205 prebuiltLib, isPrebuiltLib := m.linker.(*prebuiltLibraryLinker)
206
207 if m.InVendorOrProduct() && isLib && lib.hasLLNDKStubs() {
208 m.VendorProperties.IsLLNDK = true
209 }
210 if m.InVendorOrProduct() && isPrebuiltLib && prebuiltLib.hasLLNDKStubs() {
211 m.VendorProperties.IsLLNDK = true
212 }
213
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +0900214 if vndkprebuilt, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok {
215 if !Bool(vndkprebuilt.properties.Vndk.Enabled) {
216 m.VendorProperties.IsLLNDK = true
217 }
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900218 }
219}
220
221// Check for modules that mustn't be LLNDK
Cole Faust021bf3d2024-05-01 16:59:00 -0700222func shouldSkipLlndkMutator(mctx android.BottomUpMutatorContext, m *Module) bool {
223 if !m.Enabled(mctx) {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900224 return true
225 }
226 if !m.Device() {
227 return true
228 }
229 if m.Target().NativeBridge == android.NativeBridgeEnabled {
230 return true
231 }
232 return false
233}