blob: 5586576680ddc8e06dd22767cab686e44abb14ff [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
Dan Willemsenb916b802017-03-19 13:44:32 -070039 // Whether the system library uses symbol versions.
Nan Zhang0007d812017-11-07 10:57:05 -080040 Unversioned *bool
Jiyong Park82e2bf32017-08-16 14:05:54 +090041
Jiyong Park2a454122017-10-19 15:59:33 +090042 // list of llndk headers to re-export include directories from.
Colin Cross0fb7fcd2021-03-02 11:00:07 -080043 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 Cross127bb8b2020-12-16 16:46:01 -080049
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 Cross1f3f1302021-04-26 18:37:44 -070056
57 // if true, make this module available to provide headers to other modules that set
58 // llndk.symbol_file.
59 Llndk_headers *bool
Colin Cross0510f542024-11-13 16:07:08 -080060
61 // moved_to_apex marks this module has having been distributed through an apex module.
62 Moved_to_apex *bool
Dan Willemsenb916b802017-03-19 13:44:32 -070063}
Kiyoung Kim37693d02024-04-04 09:56:15 +090064
65func makeLlndkVars(ctx android.MakeVarsContext) {
Kiyoung Kim37693d02024-04-04 09:56:15 +090066
Kiyoung Kim37693d02024-04-04 09:56:15 +090067}
Kiyoung Kim973cb6f2024-04-29 14:14:53 +090068
69func init() {
70 RegisterLlndkLibraryTxtType(android.InitRegistrationContext)
Cole Faustb43793e2024-12-13 16:53:36 -080071 android.RegisterParallelSingletonType("movedToApexLlndkLibraries", movedToApexLlndkLibrariesFactory)
72}
73
74func movedToApexLlndkLibrariesFactory() android.Singleton {
75 return &movedToApexLlndkLibraries{}
76}
77
78type movedToApexLlndkLibraries struct {
79 movedToApexLlndkLibraries []string
80}
81
82func (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)
85 ctx.VisitAllModules(func(module android.Module) {
Ivan Lozano9eaacc82024-10-30 14:28:17 +000086 if library := moduleVersionedInterface(module); library != nil && library.HasLLNDKStubs() {
87 if library.IsLLNDKMovedToApex() {
88 name := library.ImplementationModuleName(module.(*Module).BaseModuleName())
Cole Faustb43793e2024-12-13 16:53:36 -080089 movedToApexLlndkLibrariesMap[name] = true
90 }
91 }
92 })
93 s.movedToApexLlndkLibraries = android.SortedKeys(movedToApexLlndkLibrariesMap)
94
95 var sb strings.Builder
96 for i, l := range s.movedToApexLlndkLibraries {
97 if i > 0 {
98 sb.WriteRune(' ')
99 }
100 sb.WriteString(l)
101 sb.WriteString(".so")
102 }
103 android.WriteFileRule(ctx, MovedToApexLlndkLibrariesFile(ctx), sb.String())
104}
105
106func MovedToApexLlndkLibrariesFile(ctx android.PathContext) android.WritablePath {
107 return android.PathForIntermediates(ctx, "moved_to_apex_llndk_libraries.txt")
108}
109
110func (s *movedToApexLlndkLibraries) MakeVars(ctx android.MakeVarsContext) {
111 ctx.Strict("LLNDK_MOVED_TO_APEX_LIBRARIES", strings.Join(s.movedToApexLlndkLibraries, " "))
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900112}
113
114func RegisterLlndkLibraryTxtType(ctx android.RegistrationContext) {
115 ctx.RegisterParallelSingletonModuleType("llndk_libraries_txt", llndkLibrariesTxtFactory)
116}
117
118type llndkLibrariesTxtModule struct {
119 android.SingletonModuleBase
120
121 outputFile android.OutputPath
122 moduleNames []string
123 fileNames []string
124}
125
126var _ etc.PrebuiltEtcModule = &llndkLibrariesTxtModule{}
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900127
128// llndk_libraries_txt is a singleton module whose content is a list of LLNDK libraries
129// generated by Soong but can be referenced by other modules.
130// For example, apex_vndk can depend on these files as prebuilt.
131// Make uses LLNDK_LIBRARIES to determine which libraries to install.
132// HWASAN is only part of the LL-NDK in builds in which libc depends on HWASAN.
133// Therefore, by removing the library here, we cause it to only be installed if libc
134// depends on it.
135func llndkLibrariesTxtFactory() android.SingletonModule {
136 m := &llndkLibrariesTxtModule{}
137 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
138 return m
139}
140
141func (txt *llndkLibrariesTxtModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
142 filename := txt.Name()
143
144 txt.outputFile = android.PathForModuleOut(ctx, filename).OutputPath
145
146 installPath := android.PathForModuleInstall(ctx, "etc")
147 ctx.InstallFile(installPath, filename, txt.outputFile)
mrziwange2346b82024-06-10 15:09:45 -0700148
149 ctx.SetOutputFiles(android.Paths{txt.outputFile}, "")
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900150}
151
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +0900152func getVndkFileName(m *Module) (string, error) {
153 if library, ok := m.linker.(*libraryDecorator); ok {
154 return library.getLibNameHelper(m.BaseModuleName(), true, false) + ".so", nil
155 }
156 if prebuilt, ok := m.linker.(*prebuiltLibraryLinker); ok {
157 return prebuilt.libraryDecorator.getLibNameHelper(m.BaseModuleName(), true, false) + ".so", nil
158 }
159 return "", fmt.Errorf("VNDK library should have libraryDecorator or prebuiltLibraryLinker as linker: %T", m.linker)
160}
161
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900162func (txt *llndkLibrariesTxtModule) GenerateSingletonBuildActions(ctx android.SingletonContext) {
Kiyoung Kim0d4a9ca2024-05-07 16:11:41 +0900163 if txt.outputFile.String() == "" {
164 // Skip if target file path is empty
165 return
166 }
167
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900168 ctx.VisitAllModules(func(m android.Module) {
169 if c, ok := m.(*Module); ok && c.VendorProperties.IsLLNDK && !c.Header() && !c.IsVndkPrebuiltLibrary() {
170 filename, err := getVndkFileName(c)
171 if err != nil {
172 ctx.ModuleErrorf(m, "%s", err)
173 }
174
175 if !strings.HasPrefix(ctx.ModuleName(m), "libclang_rt.hwasan") {
176 txt.moduleNames = append(txt.moduleNames, ctx.ModuleName(m))
177 }
178 txt.fileNames = append(txt.fileNames, filename)
179 }
180 })
181 txt.moduleNames = android.SortedUniqueStrings(txt.moduleNames)
182 txt.fileNames = android.SortedUniqueStrings(txt.fileNames)
183
184 android.WriteFileRule(ctx, txt.outputFile, strings.Join(txt.fileNames, "\n"))
185}
186
187func (txt *llndkLibrariesTxtModule) AndroidMkEntries() []android.AndroidMkEntries {
188 return []android.AndroidMkEntries{{
189 Class: "ETC",
190 OutputFile: android.OptionalPathForPath(txt.outputFile),
191 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
192 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
193 entries.SetString("LOCAL_MODULE_STEM", txt.outputFile.Base())
194 },
195 },
196 }}
197}
198
199func (txt *llndkLibrariesTxtModule) MakeVars(ctx android.MakeVarsContext) {
200 ctx.Strict("LLNDK_LIBRARIES", strings.Join(txt.moduleNames, " "))
201}
202
203// PrebuiltEtcModule interface
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900204func (txt *llndkLibrariesTxtModule) BaseDir() string {
205 return "etc"
206}
207
208// PrebuiltEtcModule interface
209func (txt *llndkLibrariesTxtModule) SubDir() string {
210 return ""
211}
212
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900213func llndkMutator(mctx android.BottomUpMutatorContext) {
214 m, ok := mctx.Module().(*Module)
215 if !ok {
216 return
217 }
218
Cole Faust021bf3d2024-05-01 16:59:00 -0700219 if shouldSkipLlndkMutator(mctx, m) {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900220 return
221 }
222
223 lib, isLib := m.linker.(*libraryDecorator)
224 prebuiltLib, isPrebuiltLib := m.linker.(*prebuiltLibraryLinker)
225
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000226 if m.InVendorOrProduct() && isLib && lib.HasLLNDKStubs() {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900227 m.VendorProperties.IsLLNDK = true
228 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000229 if m.InVendorOrProduct() && isPrebuiltLib && prebuiltLib.HasLLNDKStubs() {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900230 m.VendorProperties.IsLLNDK = true
231 }
232
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +0900233 if vndkprebuilt, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok {
234 if !Bool(vndkprebuilt.properties.Vndk.Enabled) {
235 m.VendorProperties.IsLLNDK = true
236 }
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900237 }
238}
239
240// Check for modules that mustn't be LLNDK
Cole Faust021bf3d2024-05-01 16:59:00 -0700241func shouldSkipLlndkMutator(mctx android.BottomUpMutatorContext, m *Module) bool {
242 if !m.Enabled(mctx) {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900243 return true
244 }
245 if !m.Device() {
246 return true
247 }
248 if m.Target().NativeBridge == android.NativeBridgeEnabled {
249 return true
250 }
251 return false
252}