blob: e50da9aba7a5e038dfc67a5afd374586cf27148e [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
Colin Cross56a83212020-09-15 18:30:11 -070071 movedToApex bool
Dan Willemsenb916b802017-03-19 13:44:32 -070072}
73
Colin Crossf18e1102017-11-16 14:33:08 -080074func (stub *llndkStubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
75 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -070076 return addStubLibraryCompilerFlags(flags)
77}
78
Dan Willemsenb916b802017-03-19 13:44:32 -070079func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Justin Yun5f7f7e82019-11-18 19:52:14 +090080 vndkVer := ctx.Module().(*Module).VndkVersion()
Jooyung Han03302ee2020-04-08 09:22:26 +090081 if !inList(vndkVer, ctx.Config().PlatformVersionActiveCodenames()) || vndkVer == "" {
Justin Yun5f7f7e82019-11-18 19:52:14 +090082 // For non-enforcing devices, vndkVer is empty. Use "current" in that case, too.
83 vndkVer = "current"
Justin Yun732aa6a2018-03-23 17:43:47 +090084 }
Jooyung Han61b66e92020-03-21 14:21:46 +000085 if stub.stubsVersion() != "" {
86 vndkVer = stub.stubsVersion()
87 }
Justin Yun5f7f7e82019-11-18 19:52:14 +090088 objs, versionScript := compileStubLibrary(ctx, flags, String(stub.Properties.Symbol_file), vndkVer, "--llndk")
Colin Cross8e21aa52020-09-28 18:28:02 -070089 if !Bool(stub.Properties.Unversioned) {
90 stub.versionScriptPath = android.OptionalPathForPath(versionScript)
91 }
Dan Willemsenb916b802017-03-19 13:44:32 -070092 return objs
93}
94
95func (stub *llndkStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Jiyong Park2a454122017-10-19 15:59:33 +090096 headers := addSuffix(stub.Properties.Export_llndk_headers, llndkHeadersSuffix)
97 deps.HeaderLibs = append(deps.HeaderLibs, headers...)
98 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, headers...)
99 return deps
Dan Willemsenb916b802017-03-19 13:44:32 -0700100}
101
Dan Willemsen01a90592017-04-07 15:21:13 -0700102func (stub *llndkStubDecorator) Name(name string) string {
103 return name + llndkLibrarySuffix
104}
105
Dan Willemsenb916b802017-03-19 13:44:32 -0700106func (stub *llndkStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
107 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
108 llndkLibrarySuffix)
109 return stub.libraryDecorator.linkerFlags(ctx, flags)
110}
111
112func (stub *llndkStubDecorator) processHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path {
113 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir)
Dan Willemsen540a78c2018-02-26 21:50:08 -0800114 srcFiles := ctx.GlobFiles(filepath.Join(srcDir.String(), "**/*.h"), nil)
Dan Willemsenb916b802017-03-19 13:44:32 -0700115
116 var installPaths []android.WritablePath
117 for _, header := range srcFiles {
118 headerDir := filepath.Dir(header.String())
119 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir)
120 if err != nil {
121 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s",
122 srcDir.String(), headerDir, err)
123 continue
124 }
125
126 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base()))
127 }
128
129 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths)
130}
131
132func (stub *llndkStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
133 objs Objects) android.Path {
134
Colin Cross56a83212020-09-15 18:30:11 -0700135 impl := ctx.GetDirectDepWithTag(ctx.baseModuleName(), llndkImplDep)
136 if implApexModule, ok := impl.(android.ApexModule); ok {
137 stub.movedToApex = implApexModule.DirectlyInAnyApex()
138 }
139
Dan Willemsenb916b802017-03-19 13:44:32 -0700140 if len(stub.Properties.Export_preprocessed_headers) > 0 {
141 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
142
143 var timestampFiles android.Paths
144 for _, dir := range stub.Properties.Export_preprocessed_headers {
145 timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir))
146 }
147
Nan Zhang0007d812017-11-07 10:57:05 -0800148 if Bool(stub.Properties.Export_headers_as_system) {
Jiyong Park74955042019-10-22 20:19:51 +0900149 stub.reexportSystemDirs(genHeaderOutDir)
Inseob Kim69378442019-06-03 19:10:47 +0900150 } else {
Jiyong Park74955042019-10-22 20:19:51 +0900151 stub.reexportDirs(genHeaderOutDir)
Dan Willemsenb916b802017-03-19 13:44:32 -0700152 }
153
Inseob Kim69378442019-06-03 19:10:47 +0900154 stub.reexportDeps(timestampFiles...)
Dan Willemsenb916b802017-03-19 13:44:32 -0700155 }
156
Nan Zhang0007d812017-11-07 10:57:05 -0800157 if Bool(stub.Properties.Export_headers_as_system) {
Inseob Kim69378442019-06-03 19:10:47 +0900158 stub.exportIncludesAsSystem(ctx)
Dan Willemsenb916b802017-03-19 13:44:32 -0700159 stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{}
160 }
161
162 return stub.libraryDecorator.link(ctx, flags, deps, objs)
163}
164
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700165func (stub *llndkStubDecorator) nativeCoverage() bool {
166 return false
167}
168
Colin Crossc88c2722020-09-28 17:32:47 -0700169func (stub *llndkStubDecorator) buildStubs() bool {
170 return true
171}
172
Jiyong Park64ca4b72017-11-14 20:53:00 +0900173func NewLLndkStubLibrary() *Module {
Dan Willemsenb916b802017-03-19 13:44:32 -0700174 module, library := NewLibrary(android.DeviceSupported)
175 library.BuildOnlyShared()
176 module.stl = nil
177 module.sanitize = nil
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200178 library.disableStripping()
Dan Willemsenb916b802017-03-19 13:44:32 -0700179
180 stub := &llndkStubDecorator{
181 libraryDecorator: library,
182 }
Nan Zhang0007d812017-11-07 10:57:05 -0800183 stub.Properties.Vendor_available = BoolPtr(true)
Dan Willemsenb916b802017-03-19 13:44:32 -0700184 module.compiler = stub
185 module.linker = stub
186 module.installer = nil
187
Colin Cross36242852017-06-23 15:06:31 -0700188 module.AddProperties(
Jiyong Park5e676fe2019-04-17 13:12:10 +0900189 &module.Properties,
Colin Cross36242852017-06-23 15:06:31 -0700190 &stub.Properties,
191 &library.MutatedProperties,
192 &library.flagExporter.Properties)
193
194 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700195}
196
Patrice Arrudaea3fcdf2019-04-03 14:37:46 -0700197// llndk_library creates a stub llndk shared library based on the provided
198// version file. Example:
199//
200// llndk_library {
201// name: "libfoo",
202// symbol_file: "libfoo.map.txt",
203// export_include_dirs: ["include_vndk"],
204// }
Jiyong Parkda6eb592018-12-19 17:12:36 +0900205func LlndkLibraryFactory() android.Module {
Jiyong Park64ca4b72017-11-14 20:53:00 +0900206 module := NewLLndkStubLibrary()
Colin Cross36242852017-06-23 15:06:31 -0700207 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
208 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700209}
210
Jiyong Park2a454122017-10-19 15:59:33 +0900211type llndkHeadersDecorator struct {
212 *libraryDecorator
213}
214
215func (headers *llndkHeadersDecorator) Name(name string) string {
216 return name + llndkHeadersSuffix
217}
218
Patrice Arrudaea3fcdf2019-04-03 14:37:46 -0700219// llndk_headers contains a set of c/c++ llndk headers files which are imported
220// by other soongs cc modules.
Jiyong Park2a454122017-10-19 15:59:33 +0900221func llndkHeadersFactory() android.Module {
222 module, library := NewLibrary(android.DeviceSupported)
223 library.HeaderOnly()
Inseob Kimc7c69102020-07-08 07:56:02 +0900224 module.stl = nil
225 module.sanitize = nil
Jiyong Park2a454122017-10-19 15:59:33 +0900226
227 decorator := &llndkHeadersDecorator{
228 libraryDecorator: library,
229 }
230
231 module.compiler = nil
232 module.linker = decorator
233 module.installer = nil
234
Jiyong Park5e676fe2019-04-17 13:12:10 +0900235 module.AddProperties(
236 &module.Properties,
237 &library.MutatedProperties,
238 &library.flagExporter.Properties)
Jiyong Park2a454122017-10-19 15:59:33 +0900239
Jiyong Park1d1119f2019-07-29 21:27:18 +0900240 module.Init()
Jiyong Park2a454122017-10-19 15:59:33 +0900241
242 return module
243}
244
Dan Willemsenb916b802017-03-19 13:44:32 -0700245func init() {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900246 android.RegisterModuleType("llndk_library", LlndkLibraryFactory)
Jiyong Park2a454122017-10-19 15:59:33 +0900247 android.RegisterModuleType("llndk_headers", llndkHeadersFactory)
Dan Willemsenb916b802017-03-19 13:44:32 -0700248}