blob: d612e9e72b0d7df9748ec6a44c91c6f2202c697a [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 (
mrziwange2346b82024-06-10 15:09:45 -070018 "strings"
19
Kiyoung Kim37693d02024-04-04 09:56:15 +090020 "android/soong/android"
Kiyoung Kim973cb6f2024-04-29 14:14:53 +090021 "android/soong/etc"
Kiyoung Kim37693d02024-04-04 09:56:15 +090022)
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{}
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900100
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.
108func llndkLibrariesTxtFactory() android.SingletonModule {
109 m := &llndkLibrariesTxtModule{}
110 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
111 return m
112}
113
114func (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)
mrziwange2346b82024-06-10 15:09:45 -0700121
122 ctx.SetOutputFiles(android.Paths{txt.outputFile}, "")
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900123}
124
125func (txt *llndkLibrariesTxtModule) GenerateSingletonBuildActions(ctx android.SingletonContext) {
Kiyoung Kim0d4a9ca2024-05-07 16:11:41 +0900126 if txt.outputFile.String() == "" {
127 // Skip if target file path is empty
128 return
129 }
130
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900131 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
150func (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
162func (txt *llndkLibrariesTxtModule) MakeVars(ctx android.MakeVarsContext) {
163 ctx.Strict("LLNDK_LIBRARIES", strings.Join(txt.moduleNames, " "))
164}
165
166// PrebuiltEtcModule interface
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900167func (txt *llndkLibrariesTxtModule) BaseDir() string {
168 return "etc"
169}
170
171// PrebuiltEtcModule interface
172func (txt *llndkLibrariesTxtModule) SubDir() string {
173 return ""
174}
175
176func (txt *llndkLibrariesTxtModule) OutputFiles(tag string) (android.Paths, error) {
177 return android.Paths{txt.outputFile}, nil
178}
179
180func llndkMutator(mctx android.BottomUpMutatorContext) {
181 m, ok := mctx.Module().(*Module)
182 if !ok {
183 return
184 }
185
Cole Fausta963b942024-04-11 17:43:00 -0700186 if shouldSkipLlndkMutator(mctx, m) {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900187 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 Fausta963b942024-04-11 17:43:00 -0700206func shouldSkipLlndkMutator(mctx android.BottomUpMutatorContext, m *Module) bool {
207 if !m.Enabled(mctx) {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +0900208 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}