blob: c7950f9ff71fd1a949f553b5015e15727c966580 [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
Dan Willemsenb916b802017-03-19 13:44:32 -070060}
Kiyoung Kim37693d02024-04-04 09:56:15 +090061
62func makeLlndkVars(ctx android.MakeVarsContext) {
63 // Make uses LLNDK_MOVED_TO_APEX_LIBRARIES to avoid installing libraries on /system if
64 // they been moved to an apex.
65 movedToApexLlndkLibraries := make(map[string]bool)
66 ctx.VisitAllModules(func(module android.Module) {
67 if library := moduleLibraryInterface(module); library != nil && library.hasLLNDKStubs() {
68 // Skip bionic libs, they are handled in different manner
69 name := library.implementationModuleName(module.(*Module).BaseModuleName())
70 if module.(android.ApexModule).DirectlyInAnyApex() && !isBionic(name) {
71 movedToApexLlndkLibraries[name] = true
72 }
73 }
74 })
75
76 ctx.Strict("LLNDK_MOVED_TO_APEX_LIBRARIES",
77 strings.Join(android.SortedKeys(movedToApexLlndkLibraries), " "))
78}
Kiyoung Kim973cb6f2024-04-29 14:14:53 +090079
80func init() {
81 RegisterLlndkLibraryTxtType(android.InitRegistrationContext)
82}
83
84func RegisterLlndkLibraryTxtType(ctx android.RegistrationContext) {
85 ctx.RegisterParallelSingletonModuleType("llndk_libraries_txt", llndkLibrariesTxtFactory)
86}
87
88type llndkLibrariesTxtModule struct {
89 android.SingletonModuleBase
90
91 outputFile android.OutputPath
92 moduleNames []string
93 fileNames []string
94}
95
96var _ etc.PrebuiltEtcModule = &llndkLibrariesTxtModule{}
Kiyoung Kim973cb6f2024-04-29 14:14:53 +090097
98// llndk_libraries_txt is a singleton module whose content is a list of LLNDK libraries
99// generated by Soong but can be referenced by other modules.
100// For example, apex_vndk can depend on these files as prebuilt.
101// Make uses LLNDK_LIBRARIES to determine which libraries to install.
102// HWASAN is only part of the LL-NDK in builds in which libc depends on HWASAN.
103// Therefore, by removing the library here, we cause it to only be installed if libc
104// depends on it.
105func llndkLibrariesTxtFactory() android.SingletonModule {
106 m := &llndkLibrariesTxtModule{}
107 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
108 return m
109}
110
111func (txt *llndkLibrariesTxtModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
112 filename := txt.Name()
113
114 txt.outputFile = android.PathForModuleOut(ctx, filename).OutputPath
115
116 installPath := android.PathForModuleInstall(ctx, "etc")
117 ctx.InstallFile(installPath, filename, txt.outputFile)
mrziwange2346b82024-06-10 15:09:45 -0700118
119 ctx.SetOutputFiles(android.Paths{txt.outputFile}, "")
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900120}
121
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +0900122func getVndkFileName(m *Module) (string, error) {
123 if library, ok := m.linker.(*libraryDecorator); ok {
124 return library.getLibNameHelper(m.BaseModuleName(), true, false) + ".so", nil
125 }
126 if prebuilt, ok := m.linker.(*prebuiltLibraryLinker); ok {
127 return prebuilt.libraryDecorator.getLibNameHelper(m.BaseModuleName(), true, false) + ".so", nil
128 }
129 return "", fmt.Errorf("VNDK library should have libraryDecorator or prebuiltLibraryLinker as linker: %T", m.linker)
130}
131
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900132func (txt *llndkLibrariesTxtModule) GenerateSingletonBuildActions(ctx android.SingletonContext) {
Kiyoung Kim0d4a9ca2024-05-07 16:11:41 +0900133 if txt.outputFile.String() == "" {
134 // Skip if target file path is empty
135 return
136 }
137
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900138 ctx.VisitAllModules(func(m android.Module) {
139 if c, ok := m.(*Module); ok && c.VendorProperties.IsLLNDK && !c.Header() && !c.IsVndkPrebuiltLibrary() {
140 filename, err := getVndkFileName(c)
141 if err != nil {
142 ctx.ModuleErrorf(m, "%s", err)
143 }
144
145 if !strings.HasPrefix(ctx.ModuleName(m), "libclang_rt.hwasan") {
146 txt.moduleNames = append(txt.moduleNames, ctx.ModuleName(m))
147 }
148 txt.fileNames = append(txt.fileNames, filename)
149 }
150 })
151 txt.moduleNames = android.SortedUniqueStrings(txt.moduleNames)
152 txt.fileNames = android.SortedUniqueStrings(txt.fileNames)
153
154 android.WriteFileRule(ctx, txt.outputFile, strings.Join(txt.fileNames, "\n"))
155}
156
157func (txt *llndkLibrariesTxtModule) AndroidMkEntries() []android.AndroidMkEntries {
158 return []android.AndroidMkEntries{{
159 Class: "ETC",
160 OutputFile: android.OptionalPathForPath(txt.outputFile),
161 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
162 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
163 entries.SetString("LOCAL_MODULE_STEM", txt.outputFile.Base())
164 },
165 },
166 }}
167}
168
169func (txt *llndkLibrariesTxtModule) MakeVars(ctx android.MakeVarsContext) {
170 ctx.Strict("LLNDK_LIBRARIES", strings.Join(txt.moduleNames, " "))
171}
172
173// PrebuiltEtcModule interface
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900174func (txt *llndkLibrariesTxtModule) BaseDir() string {
175 return "etc"
176}
177
178// PrebuiltEtcModule interface
179func (txt *llndkLibrariesTxtModule) SubDir() string {
180 return ""
181}
182
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900183func llndkMutator(mctx android.BottomUpMutatorContext) {
184 m, ok := mctx.Module().(*Module)
185 if !ok {
186 return
187 }
188
Cole Faust021bf3d2024-05-01 16:59:00 -0700189 if shouldSkipLlndkMutator(mctx, m) {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900190 return
191 }
192
193 lib, isLib := m.linker.(*libraryDecorator)
194 prebuiltLib, isPrebuiltLib := m.linker.(*prebuiltLibraryLinker)
195
196 if m.InVendorOrProduct() && isLib && lib.hasLLNDKStubs() {
197 m.VendorProperties.IsLLNDK = true
198 }
199 if m.InVendorOrProduct() && isPrebuiltLib && prebuiltLib.hasLLNDKStubs() {
200 m.VendorProperties.IsLLNDK = true
201 }
202
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +0900203 if vndkprebuilt, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok {
204 if !Bool(vndkprebuilt.properties.Vndk.Enabled) {
205 m.VendorProperties.IsLLNDK = true
206 }
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900207 }
208}
209
210// Check for modules that mustn't be LLNDK
Cole Faust021bf3d2024-05-01 16:59:00 -0700211func shouldSkipLlndkMutator(mctx android.BottomUpMutatorContext, m *Module) bool {
212 if !m.Enabled(mctx) {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900213 return true
214 }
215 if !m.Device() {
216 return true
217 }
218 if m.Target().NativeBridge == android.NativeBridgeEnabled {
219 return true
220 }
221 return false
222}