blob: 3b8b47dabe6da37e3864577e2bc5a573a5a2f1e8 [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 (
18 "path/filepath"
19 "strings"
20
Dan Willemsenb916b802017-03-19 13:44:32 -070021 "android/soong/android"
22)
23
Colin Crossc88c2722020-09-28 17:32:47 -070024var llndkImplDep = dependencyTag{name: "llndk impl"}
Jooyung Han61b66e92020-03-21 14:21:46 +000025
Dan Willemsenb916b802017-03-19 13:44:32 -070026var (
27 llndkLibrarySuffix = ".llndk"
Jiyong Park2a454122017-10-19 15:59:33 +090028 llndkHeadersSuffix = ".llndk"
Dan Willemsenb916b802017-03-19 13:44:32 -070029)
30
31// Creates a stub shared library based on the provided version file.
32//
Dan Willemsenb916b802017-03-19 13:44:32 -070033// Example:
34//
35// llndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070036// name: "libfoo",
Dan Willemsenb916b802017-03-19 13:44:32 -070037// symbol_file: "libfoo.map.txt",
38// export_include_dirs: ["include_vndk"],
39// }
40//
41type llndkLibraryProperties struct {
42 // Relative path to the symbol map.
43 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080044 Symbol_file *string
Dan Willemsenb916b802017-03-19 13:44:32 -070045
46 // Whether to export any headers as -isystem instead of -I. Mainly for use by
47 // bionic/libc.
Nan Zhang0007d812017-11-07 10:57:05 -080048 Export_headers_as_system *bool
Dan Willemsenb916b802017-03-19 13:44:32 -070049
50 // Which headers to process with versioner. This really only handles
51 // bionic/libc/include right now.
52 Export_preprocessed_headers []string
53
54 // Whether the system library uses symbol versions.
Nan Zhang0007d812017-11-07 10:57:05 -080055 Unversioned *bool
Jiyong Park82e2bf32017-08-16 14:05:54 +090056
57 // whether this module can be directly depended upon by libs that are installed to /vendor.
58 // When set to false, this module can only be depended on by VNDK libraries, not vendor
59 // libraries. This effectively hides this module from vendors. Default value is true.
Nan Zhang0007d812017-11-07 10:57:05 -080060 Vendor_available *bool
Jiyong Park2a454122017-10-19 15:59:33 +090061
62 // list of llndk headers to re-export include directories from.
63 Export_llndk_headers []string `android:"arch_variant"`
Dan Willemsenb916b802017-03-19 13:44:32 -070064}
65
66type llndkStubDecorator struct {
67 *libraryDecorator
68
69 Properties llndkLibraryProperties
70
71 exportHeadersTimestamp android.OptionalPath
72 versionScriptPath android.ModuleGenPath
Colin Cross56a83212020-09-15 18:30:11 -070073
74 movedToApex bool
Dan Willemsenb916b802017-03-19 13:44:32 -070075}
76
Colin Crossf18e1102017-11-16 14:33:08 -080077func (stub *llndkStubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
78 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -070079 return addStubLibraryCompilerFlags(flags)
80}
81
Dan Willemsenb916b802017-03-19 13:44:32 -070082func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Justin Yun5f7f7e82019-11-18 19:52:14 +090083 vndkVer := ctx.Module().(*Module).VndkVersion()
Jooyung Han03302ee2020-04-08 09:22:26 +090084 if !inList(vndkVer, ctx.Config().PlatformVersionActiveCodenames()) || vndkVer == "" {
Justin Yun5f7f7e82019-11-18 19:52:14 +090085 // For non-enforcing devices, vndkVer is empty. Use "current" in that case, too.
86 vndkVer = "current"
Justin Yun732aa6a2018-03-23 17:43:47 +090087 }
Jooyung Han61b66e92020-03-21 14:21:46 +000088 if stub.stubsVersion() != "" {
89 vndkVer = stub.stubsVersion()
90 }
Justin Yun5f7f7e82019-11-18 19:52:14 +090091 objs, versionScript := compileStubLibrary(ctx, flags, String(stub.Properties.Symbol_file), vndkVer, "--llndk")
Dan Willemsenb916b802017-03-19 13:44:32 -070092 stub.versionScriptPath = versionScript
93 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
Nan Zhang0007d812017-11-07 10:57:05 -0800141 if !Bool(stub.Properties.Unversioned) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700142 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800143 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700144 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Willemsenb916b802017-03-19 13:44:32 -0700145 }
146
147 if len(stub.Properties.Export_preprocessed_headers) > 0 {
148 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
149
150 var timestampFiles android.Paths
151 for _, dir := range stub.Properties.Export_preprocessed_headers {
152 timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir))
153 }
154
Nan Zhang0007d812017-11-07 10:57:05 -0800155 if Bool(stub.Properties.Export_headers_as_system) {
Jiyong Park74955042019-10-22 20:19:51 +0900156 stub.reexportSystemDirs(genHeaderOutDir)
Inseob Kim69378442019-06-03 19:10:47 +0900157 } else {
Jiyong Park74955042019-10-22 20:19:51 +0900158 stub.reexportDirs(genHeaderOutDir)
Dan Willemsenb916b802017-03-19 13:44:32 -0700159 }
160
Inseob Kim69378442019-06-03 19:10:47 +0900161 stub.reexportDeps(timestampFiles...)
Dan Willemsenb916b802017-03-19 13:44:32 -0700162 }
163
Nan Zhang0007d812017-11-07 10:57:05 -0800164 if Bool(stub.Properties.Export_headers_as_system) {
Inseob Kim69378442019-06-03 19:10:47 +0900165 stub.exportIncludesAsSystem(ctx)
Dan Willemsenb916b802017-03-19 13:44:32 -0700166 stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{}
167 }
168
Jooyung Han61b66e92020-03-21 14:21:46 +0000169 if stub.stubsVersion() != "" {
170 stub.reexportFlags("-D" + versioningMacroName(ctx.baseModuleName()) + "=" + stub.stubsVersion())
171 }
172
Dan Willemsenb916b802017-03-19 13:44:32 -0700173 return stub.libraryDecorator.link(ctx, flags, deps, objs)
174}
175
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700176func (stub *llndkStubDecorator) nativeCoverage() bool {
177 return false
178}
179
Colin Crossc88c2722020-09-28 17:32:47 -0700180func (stub *llndkStubDecorator) buildStubs() bool {
181 return true
182}
183
184func (stub *llndkStubDecorator) setBuildStubs() {}
185
Jiyong Park64ca4b72017-11-14 20:53:00 +0900186func NewLLndkStubLibrary() *Module {
Dan Willemsenb916b802017-03-19 13:44:32 -0700187 module, library := NewLibrary(android.DeviceSupported)
188 library.BuildOnlyShared()
189 module.stl = nil
190 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200191 library.disableStripping()
Dan Willemsenb916b802017-03-19 13:44:32 -0700192
193 stub := &llndkStubDecorator{
194 libraryDecorator: library,
195 }
Nan Zhang0007d812017-11-07 10:57:05 -0800196 stub.Properties.Vendor_available = BoolPtr(true)
Dan Willemsenb916b802017-03-19 13:44:32 -0700197 module.compiler = stub
198 module.linker = stub
199 module.installer = nil
200
Colin Cross36242852017-06-23 15:06:31 -0700201 module.AddProperties(
Jiyong Park5e676fe2019-04-17 13:12:10 +0900202 &module.Properties,
Colin Cross36242852017-06-23 15:06:31 -0700203 &stub.Properties,
204 &library.MutatedProperties,
205 &library.flagExporter.Properties)
206
207 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700208}
209
Patrice Arrudaea3fcdf2019-04-03 14:37:46 -0700210// llndk_library creates a stub llndk shared library based on the provided
211// version file. Example:
212//
213// llndk_library {
214// name: "libfoo",
215// symbol_file: "libfoo.map.txt",
216// export_include_dirs: ["include_vndk"],
217// }
Jiyong Parkda6eb592018-12-19 17:12:36 +0900218func LlndkLibraryFactory() android.Module {
Jiyong Park64ca4b72017-11-14 20:53:00 +0900219 module := NewLLndkStubLibrary()
Colin Cross36242852017-06-23 15:06:31 -0700220 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
221 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700222}
223
Jiyong Park2a454122017-10-19 15:59:33 +0900224type llndkHeadersDecorator struct {
225 *libraryDecorator
226}
227
228func (headers *llndkHeadersDecorator) Name(name string) string {
229 return name + llndkHeadersSuffix
230}
231
Patrice Arrudaea3fcdf2019-04-03 14:37:46 -0700232// llndk_headers contains a set of c/c++ llndk headers files which are imported
233// by other soongs cc modules.
Jiyong Park2a454122017-10-19 15:59:33 +0900234func llndkHeadersFactory() android.Module {
235 module, library := NewLibrary(android.DeviceSupported)
236 library.HeaderOnly()
Inseob Kimc7c69102020-07-08 07:56:02 +0900237 module.stl = nil
238 module.sanitize = nil
Jiyong Park2a454122017-10-19 15:59:33 +0900239
240 decorator := &llndkHeadersDecorator{
241 libraryDecorator: library,
242 }
243
244 module.compiler = nil
245 module.linker = decorator
246 module.installer = nil
247
Jiyong Park5e676fe2019-04-17 13:12:10 +0900248 module.AddProperties(
249 &module.Properties,
250 &library.MutatedProperties,
251 &library.flagExporter.Properties)
Jiyong Park2a454122017-10-19 15:59:33 +0900252
Jiyong Park1d1119f2019-07-29 21:27:18 +0900253 module.Init()
Jiyong Park2a454122017-10-19 15:59:33 +0900254
255 return module
256}
257
Dan Willemsenb916b802017-03-19 13:44:32 -0700258func init() {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900259 android.RegisterModuleType("llndk_library", LlndkLibraryFactory)
Jiyong Park2a454122017-10-19 15:59:33 +0900260 android.RegisterModuleType("llndk_headers", llndkHeadersFactory)
Dan Willemsenb916b802017-03-19 13:44:32 -0700261}