blob: 4425a1040393f0626be85d4fda1e0b85d633837d [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
17import (
Colin Cross3572cf72020-10-01 15:58:11 -070018 "fmt"
Dan Willemsenb916b802017-03-19 13:44:32 -070019 "path/filepath"
20 "strings"
21
Dan Willemsenb916b802017-03-19 13:44:32 -070022 "android/soong/android"
23)
24
Colin Crossc88c2722020-09-28 17:32:47 -070025var llndkImplDep = dependencyTag{name: "llndk impl"}
Jooyung Han61b66e92020-03-21 14:21:46 +000026
Dan Willemsenb916b802017-03-19 13:44:32 -070027var (
28 llndkLibrarySuffix = ".llndk"
Jiyong Park2a454122017-10-19 15:59:33 +090029 llndkHeadersSuffix = ".llndk"
Dan Willemsenb916b802017-03-19 13:44:32 -070030)
31
32// Creates a stub shared library based on the provided version file.
33//
Dan Willemsenb916b802017-03-19 13:44:32 -070034// Example:
35//
36// llndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070037// name: "libfoo",
Dan Willemsenb916b802017-03-19 13:44:32 -070038// symbol_file: "libfoo.map.txt",
39// export_include_dirs: ["include_vndk"],
40// }
41//
42type llndkLibraryProperties struct {
43 // Relative path to the symbol map.
44 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080045 Symbol_file *string
Dan Willemsenb916b802017-03-19 13:44:32 -070046
47 // Whether to export any headers as -isystem instead of -I. Mainly for use by
48 // bionic/libc.
Nan Zhang0007d812017-11-07 10:57:05 -080049 Export_headers_as_system *bool
Dan Willemsenb916b802017-03-19 13:44:32 -070050
51 // Which headers to process with versioner. This really only handles
52 // bionic/libc/include right now.
53 Export_preprocessed_headers []string
54
55 // Whether the system library uses symbol versions.
Nan Zhang0007d812017-11-07 10:57:05 -080056 Unversioned *bool
Jiyong Park82e2bf32017-08-16 14:05:54 +090057
58 // whether this module can be directly depended upon by libs that are installed to /vendor.
59 // When set to false, this module can only be depended on by VNDK libraries, not vendor
60 // libraries. This effectively hides this module from vendors. Default value is true.
Nan Zhang0007d812017-11-07 10:57:05 -080061 Vendor_available *bool
Jiyong Park2a454122017-10-19 15:59:33 +090062
63 // list of llndk headers to re-export include directories from.
64 Export_llndk_headers []string `android:"arch_variant"`
Dan Willemsenb916b802017-03-19 13:44:32 -070065}
66
67type llndkStubDecorator struct {
68 *libraryDecorator
69
70 Properties llndkLibraryProperties
71
Colin Cross56a83212020-09-15 18:30:11 -070072 movedToApex bool
Dan Willemsenb916b802017-03-19 13:44:32 -070073}
74
Colin Crossf18e1102017-11-16 14:33:08 -080075func (stub *llndkStubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
76 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -070077 return addStubLibraryCompilerFlags(flags)
78}
79
Dan Willemsenb916b802017-03-19 13:44:32 -070080func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Justin Yun5f7f7e82019-11-18 19:52:14 +090081 vndkVer := ctx.Module().(*Module).VndkVersion()
Jooyung Han03302ee2020-04-08 09:22:26 +090082 if !inList(vndkVer, ctx.Config().PlatformVersionActiveCodenames()) || vndkVer == "" {
Justin Yun5f7f7e82019-11-18 19:52:14 +090083 // For non-enforcing devices, vndkVer is empty. Use "current" in that case, too.
84 vndkVer = "current"
Justin Yun732aa6a2018-03-23 17:43:47 +090085 }
Jooyung Han61b66e92020-03-21 14:21:46 +000086 if stub.stubsVersion() != "" {
87 vndkVer = stub.stubsVersion()
88 }
Justin Yun5f7f7e82019-11-18 19:52:14 +090089 objs, versionScript := compileStubLibrary(ctx, flags, String(stub.Properties.Symbol_file), vndkVer, "--llndk")
Colin Cross8e21aa52020-09-28 18:28:02 -070090 if !Bool(stub.Properties.Unversioned) {
91 stub.versionScriptPath = android.OptionalPathForPath(versionScript)
92 }
Dan Willemsenb916b802017-03-19 13:44:32 -070093 return objs
94}
95
96func (stub *llndkStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Jiyong Park2a454122017-10-19 15:59:33 +090097 headers := addSuffix(stub.Properties.Export_llndk_headers, llndkHeadersSuffix)
98 deps.HeaderLibs = append(deps.HeaderLibs, headers...)
99 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, headers...)
100 return deps
Dan Willemsenb916b802017-03-19 13:44:32 -0700101}
102
Dan Willemsen01a90592017-04-07 15:21:13 -0700103func (stub *llndkStubDecorator) Name(name string) string {
104 return name + llndkLibrarySuffix
105}
106
Dan Willemsenb916b802017-03-19 13:44:32 -0700107func (stub *llndkStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
108 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
109 llndkLibrarySuffix)
110 return stub.libraryDecorator.linkerFlags(ctx, flags)
111}
112
113func (stub *llndkStubDecorator) processHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path {
114 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir)
Dan Willemsen540a78c2018-02-26 21:50:08 -0800115 srcFiles := ctx.GlobFiles(filepath.Join(srcDir.String(), "**/*.h"), nil)
Dan Willemsenb916b802017-03-19 13:44:32 -0700116
117 var installPaths []android.WritablePath
118 for _, header := range srcFiles {
119 headerDir := filepath.Dir(header.String())
120 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir)
121 if err != nil {
122 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s",
123 srcDir.String(), headerDir, err)
124 continue
125 }
126
127 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base()))
128 }
129
130 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths)
131}
132
133func (stub *llndkStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
134 objs Objects) android.Path {
135
Colin Cross56a83212020-09-15 18:30:11 -0700136 impl := ctx.GetDirectDepWithTag(ctx.baseModuleName(), llndkImplDep)
137 if implApexModule, ok := impl.(android.ApexModule); ok {
138 stub.movedToApex = implApexModule.DirectlyInAnyApex()
139 }
140
Dan Willemsenb916b802017-03-19 13:44:32 -0700141 if len(stub.Properties.Export_preprocessed_headers) > 0 {
142 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
143
144 var timestampFiles android.Paths
145 for _, dir := range stub.Properties.Export_preprocessed_headers {
146 timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir))
147 }
148
Nan Zhang0007d812017-11-07 10:57:05 -0800149 if Bool(stub.Properties.Export_headers_as_system) {
Jiyong Park74955042019-10-22 20:19:51 +0900150 stub.reexportSystemDirs(genHeaderOutDir)
Inseob Kim69378442019-06-03 19:10:47 +0900151 } else {
Jiyong Park74955042019-10-22 20:19:51 +0900152 stub.reexportDirs(genHeaderOutDir)
Dan Willemsenb916b802017-03-19 13:44:32 -0700153 }
154
Inseob Kim69378442019-06-03 19:10:47 +0900155 stub.reexportDeps(timestampFiles...)
Dan Willemsenb916b802017-03-19 13:44:32 -0700156 }
157
Nan Zhang0007d812017-11-07 10:57:05 -0800158 if Bool(stub.Properties.Export_headers_as_system) {
Inseob Kim69378442019-06-03 19:10:47 +0900159 stub.exportIncludesAsSystem(ctx)
Dan Willemsenb916b802017-03-19 13:44:32 -0700160 stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{}
161 }
162
163 return stub.libraryDecorator.link(ctx, flags, deps, objs)
164}
165
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700166func (stub *llndkStubDecorator) nativeCoverage() bool {
167 return false
168}
169
Colin Crossc88c2722020-09-28 17:32:47 -0700170func (stub *llndkStubDecorator) buildStubs() bool {
171 return true
172}
173
Colin Cross3572cf72020-10-01 15:58:11 -0700174func (stub *llndkStubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
175 // Get the versions from the implementation module.
176 impls := ctx.GetDirectDepsWithTag(llndkImplDep)
177 if len(impls) > 1 {
178 panic(fmt.Errorf("Expected single implmenetation library, got %d", len(impls)))
179 } else if len(impls) == 1 {
180 return impls[0].(*Module).AllStubsVersions()
181 }
182 return nil
183}
184
Jiyong Park64ca4b72017-11-14 20:53:00 +0900185func NewLLndkStubLibrary() *Module {
Dan Willemsenb916b802017-03-19 13:44:32 -0700186 module, library := NewLibrary(android.DeviceSupported)
187 library.BuildOnlyShared()
188 module.stl = nil
189 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200190 library.disableStripping()
Dan Willemsenb916b802017-03-19 13:44:32 -0700191
192 stub := &llndkStubDecorator{
193 libraryDecorator: library,
194 }
Nan Zhang0007d812017-11-07 10:57:05 -0800195 stub.Properties.Vendor_available = BoolPtr(true)
Dan Willemsenb916b802017-03-19 13:44:32 -0700196 module.compiler = stub
197 module.linker = stub
198 module.installer = nil
199
Colin Cross36242852017-06-23 15:06:31 -0700200 module.AddProperties(
Jiyong Park5e676fe2019-04-17 13:12:10 +0900201 &module.Properties,
Colin Cross36242852017-06-23 15:06:31 -0700202 &stub.Properties,
203 &library.MutatedProperties,
204 &library.flagExporter.Properties)
205
206 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700207}
208
Patrice Arrudaea3fcdf2019-04-03 14:37:46 -0700209// llndk_library creates a stub llndk shared library based on the provided
210// version file. Example:
211//
212// llndk_library {
213// name: "libfoo",
214// symbol_file: "libfoo.map.txt",
215// export_include_dirs: ["include_vndk"],
216// }
Jiyong Parkda6eb592018-12-19 17:12:36 +0900217func LlndkLibraryFactory() android.Module {
Jiyong Park64ca4b72017-11-14 20:53:00 +0900218 module := NewLLndkStubLibrary()
Colin Cross36242852017-06-23 15:06:31 -0700219 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
220 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700221}
222
Jiyong Park2a454122017-10-19 15:59:33 +0900223type llndkHeadersDecorator struct {
224 *libraryDecorator
225}
226
227func (headers *llndkHeadersDecorator) Name(name string) string {
228 return name + llndkHeadersSuffix
229}
230
Patrice Arrudaea3fcdf2019-04-03 14:37:46 -0700231// llndk_headers contains a set of c/c++ llndk headers files which are imported
232// by other soongs cc modules.
Jiyong Park2a454122017-10-19 15:59:33 +0900233func llndkHeadersFactory() android.Module {
234 module, library := NewLibrary(android.DeviceSupported)
235 library.HeaderOnly()
Inseob Kimc7c69102020-07-08 07:56:02 +0900236 module.stl = nil
237 module.sanitize = nil
Jiyong Park2a454122017-10-19 15:59:33 +0900238
239 decorator := &llndkHeadersDecorator{
240 libraryDecorator: library,
241 }
242
243 module.compiler = nil
244 module.linker = decorator
245 module.installer = nil
246
Jiyong Park5e676fe2019-04-17 13:12:10 +0900247 module.AddProperties(
248 &module.Properties,
249 &library.MutatedProperties,
250 &library.flagExporter.Properties)
Jiyong Park2a454122017-10-19 15:59:33 +0900251
Jiyong Park1d1119f2019-07-29 21:27:18 +0900252 module.Init()
Jiyong Park2a454122017-10-19 15:59:33 +0900253
254 return module
255}
256
Dan Willemsenb916b802017-03-19 13:44:32 -0700257func init() {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900258 android.RegisterModuleType("llndk_library", LlndkLibraryFactory)
Jiyong Park2a454122017-10-19 15:59:33 +0900259 android.RegisterModuleType("llndk_headers", llndkHeadersFactory)
Dan Willemsenb916b802017-03-19 13:44:32 -0700260}