blob: 5ece78a835c73f4bc8f4f57368bf9bb16170bc09 [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 (
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +090018 "fmt"
Kiyoung Kim37693d02024-04-04 09:56:15 +090019 "strings"
mrziwange2346b82024-06-10 15:09:45 -070020
Kiyoung Kim37693d02024-04-04 09:56:15 +090021 "android/soong/android"
22 "android/soong/etc"
Kiyoung Kim37693d02024-04-04 09:56:15 +090023)
24
Dan Willemsenb916b802017-03-19 13:44:32 -070025var (
26 llndkLibrarySuffix = ".llndk"
27)
28
Colin Cross127bb8b2020-12-16 16:46:01 -080029// Holds properties to describe a stub shared library based on the provided version file.
Dan Willemsenb916b802017-03-19 13:44:32 -070030type llndkLibraryProperties struct {
31 // Relative path to the symbol map.
32 // An example file can be seen here: TODO(danalbert): Make an example.
Spandan Das02be1012024-07-24 01:21:53 +000033 Symbol_file *string `android:"path,arch_variant"`
Dan Willemsenb916b802017-03-19 13:44:32 -070034
35 // Whether to export any headers as -isystem instead of -I. Mainly for use by
36 // bionic/libc.
Nan Zhang0007d812017-11-07 10:57:05 -080037 Export_headers_as_system *bool
Dan Willemsenb916b802017-03-19 13:44:32 -070038
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 Zhang0007d812017-11-07 10:57:05 -080044 Unversioned *bool
Jiyong Park82e2bf32017-08-16 14:05:54 +090045
Jiyong Park2a454122017-10-19 15:59:33 +090046 // list of llndk headers to re-export include directories from.
Colin Cross0fb7fcd2021-03-02 11:00:07 -080047 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 Cross127bb8b2020-12-16 16:46:01 -080053
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 Cross1f3f1302021-04-26 18:37:44 -070060
61 // if true, make this module available to provide headers to other modules that set
62 // llndk.symbol_file.
63 Llndk_headers *bool
Dan Willemsenb916b802017-03-19 13:44:32 -070064}
Kiyoung Kim37693d02024-04-04 09:56:15 +090065
66func 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 Kim973cb6f2024-04-29 14:14:53 +090083
84func init() {
85 RegisterLlndkLibraryTxtType(android.InitRegistrationContext)
86}
87
88func RegisterLlndkLibraryTxtType(ctx android.RegistrationContext) {
89 ctx.RegisterParallelSingletonModuleType("llndk_libraries_txt", llndkLibrariesTxtFactory)
90}
91
92type llndkLibrariesTxtModule struct {
93 android.SingletonModuleBase
94
95 outputFile android.OutputPath
96 moduleNames []string
97 fileNames []string
98}
99
100var _ etc.PrebuiltEtcModule = &llndkLibrariesTxtModule{}
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900101
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)
mrziwange2346b82024-06-10 15:09:45 -0700122
123 ctx.SetOutputFiles(android.Paths{txt.outputFile}, "")
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900124}
125
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +0900126func 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 Kim973cb6f2024-04-29 14:14:53 +0900136func (txt *llndkLibrariesTxtModule) GenerateSingletonBuildActions(ctx android.SingletonContext) {
Kiyoung Kim0d4a9ca2024-05-07 16:11:41 +0900137 if txt.outputFile.String() == "" {
138 // Skip if target file path is empty
139 return
140 }
141
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900142 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
161func (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
173func (txt *llndkLibrariesTxtModule) MakeVars(ctx android.MakeVarsContext) {
174 ctx.Strict("LLNDK_LIBRARIES", strings.Join(txt.moduleNames, " "))
175}
176
177// PrebuiltEtcModule interface
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900178func (txt *llndkLibrariesTxtModule) BaseDir() string {
179 return "etc"
180}
181
182// PrebuiltEtcModule interface
183func (txt *llndkLibrariesTxtModule) SubDir() string {
184 return ""
185}
186
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900187func llndkMutator(mctx android.BottomUpMutatorContext) {
188 m, ok := mctx.Module().(*Module)
189 if !ok {
190 return
191 }
192
Cole Faust021bf3d2024-05-01 16:59:00 -0700193 if shouldSkipLlndkMutator(mctx, m) {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900194 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 Kim9f26fcf2024-05-27 17:25:52 +0900207 if vndkprebuilt, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok {
208 if !Bool(vndkprebuilt.properties.Vndk.Enabled) {
209 m.VendorProperties.IsLLNDK = true
210 }
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900211 }
212}
213
214// Check for modules that mustn't be LLNDK
Cole Faust021bf3d2024-05-01 16:59:00 -0700215func shouldSkipLlndkMutator(mctx android.BottomUpMutatorContext, m *Module) bool {
216 if !m.Enabled(mctx) {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900217 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}