blob: 3dee6920a5b6e997c8bfe529c1267ed8b2709f97 [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
24var (
25 llndkLibrarySuffix = ".llndk"
Jiyong Park2a454122017-10-19 15:59:33 +090026 llndkHeadersSuffix = ".llndk"
Dan Willemsenb916b802017-03-19 13:44:32 -070027)
28
29// Creates a stub shared library based on the provided version file.
30//
Dan Willemsenb916b802017-03-19 13:44:32 -070031// Example:
32//
33// llndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070034// name: "libfoo",
Dan Willemsenb916b802017-03-19 13:44:32 -070035// symbol_file: "libfoo.map.txt",
36// export_include_dirs: ["include_vndk"],
37// }
38//
39type llndkLibraryProperties struct {
40 // Relative path to the symbol map.
41 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080042 Symbol_file *string
Dan Willemsenb916b802017-03-19 13:44:32 -070043
44 // Whether to export any headers as -isystem instead of -I. Mainly for use by
45 // bionic/libc.
Nan Zhang0007d812017-11-07 10:57:05 -080046 Export_headers_as_system *bool
Dan Willemsenb916b802017-03-19 13:44:32 -070047
48 // Which headers to process with versioner. This really only handles
49 // bionic/libc/include right now.
50 Export_preprocessed_headers []string
51
52 // Whether the system library uses symbol versions.
Nan Zhang0007d812017-11-07 10:57:05 -080053 Unversioned *bool
Jiyong Park82e2bf32017-08-16 14:05:54 +090054
55 // whether this module can be directly depended upon by libs that are installed to /vendor.
56 // When set to false, this module can only be depended on by VNDK libraries, not vendor
57 // libraries. This effectively hides this module from vendors. Default value is true.
Nan Zhang0007d812017-11-07 10:57:05 -080058 Vendor_available *bool
Jiyong Park2a454122017-10-19 15:59:33 +090059
60 // list of llndk headers to re-export include directories from.
61 Export_llndk_headers []string `android:"arch_variant"`
Dan Willemsenb916b802017-03-19 13:44:32 -070062}
63
64type llndkStubDecorator struct {
65 *libraryDecorator
66
67 Properties llndkLibraryProperties
68
69 exportHeadersTimestamp android.OptionalPath
70 versionScriptPath android.ModuleGenPath
71}
72
Colin Crossf18e1102017-11-16 14:33:08 -080073func (stub *llndkStubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
74 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -070075 return addStubLibraryCompilerFlags(flags)
76}
77
Dan Willemsenb916b802017-03-19 13:44:32 -070078func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Justin Yun5f7f7e82019-11-18 19:52:14 +090079 vndkVer := ctx.Module().(*Module).VndkVersion()
80 if !inList(vndkVer, ctx.Config().PlatformVersionCombinedCodenames()) || vndkVer == "" {
81 // For non-enforcing devices, vndkVer is empty. Use "current" in that case, too.
82 vndkVer = "current"
Justin Yun732aa6a2018-03-23 17:43:47 +090083 }
Justin Yun5f7f7e82019-11-18 19:52:14 +090084 objs, versionScript := compileStubLibrary(ctx, flags, String(stub.Properties.Symbol_file), vndkVer, "--llndk")
Dan Willemsenb916b802017-03-19 13:44:32 -070085 stub.versionScriptPath = versionScript
86 return objs
87}
88
89func (stub *llndkStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Jiyong Park2a454122017-10-19 15:59:33 +090090 headers := addSuffix(stub.Properties.Export_llndk_headers, llndkHeadersSuffix)
91 deps.HeaderLibs = append(deps.HeaderLibs, headers...)
92 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, headers...)
93 return deps
Dan Willemsenb916b802017-03-19 13:44:32 -070094}
95
Dan Willemsen01a90592017-04-07 15:21:13 -070096func (stub *llndkStubDecorator) Name(name string) string {
97 return name + llndkLibrarySuffix
98}
99
Dan Willemsenb916b802017-03-19 13:44:32 -0700100func (stub *llndkStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
101 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
102 llndkLibrarySuffix)
103 return stub.libraryDecorator.linkerFlags(ctx, flags)
104}
105
106func (stub *llndkStubDecorator) processHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path {
107 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir)
Dan Willemsen540a78c2018-02-26 21:50:08 -0800108 srcFiles := ctx.GlobFiles(filepath.Join(srcDir.String(), "**/*.h"), nil)
Dan Willemsenb916b802017-03-19 13:44:32 -0700109
110 var installPaths []android.WritablePath
111 for _, header := range srcFiles {
112 headerDir := filepath.Dir(header.String())
113 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir)
114 if err != nil {
115 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s",
116 srcDir.String(), headerDir, err)
117 continue
118 }
119
120 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base()))
121 }
122
123 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths)
124}
125
126func (stub *llndkStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
127 objs Objects) android.Path {
128
Nan Zhang0007d812017-11-07 10:57:05 -0800129 if !Bool(stub.Properties.Unversioned) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700130 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800131 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700132 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Willemsenb916b802017-03-19 13:44:32 -0700133 }
134
135 if len(stub.Properties.Export_preprocessed_headers) > 0 {
136 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
137
138 var timestampFiles android.Paths
139 for _, dir := range stub.Properties.Export_preprocessed_headers {
140 timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir))
141 }
142
Nan Zhang0007d812017-11-07 10:57:05 -0800143 if Bool(stub.Properties.Export_headers_as_system) {
Jiyong Park74955042019-10-22 20:19:51 +0900144 stub.reexportSystemDirs(genHeaderOutDir)
Inseob Kim69378442019-06-03 19:10:47 +0900145 } else {
Jiyong Park74955042019-10-22 20:19:51 +0900146 stub.reexportDirs(genHeaderOutDir)
Dan Willemsenb916b802017-03-19 13:44:32 -0700147 }
148
Inseob Kim69378442019-06-03 19:10:47 +0900149 stub.reexportDeps(timestampFiles...)
Dan Willemsenb916b802017-03-19 13:44:32 -0700150 }
151
Nan Zhang0007d812017-11-07 10:57:05 -0800152 if Bool(stub.Properties.Export_headers_as_system) {
Inseob Kim69378442019-06-03 19:10:47 +0900153 stub.exportIncludesAsSystem(ctx)
Dan Willemsenb916b802017-03-19 13:44:32 -0700154 stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{}
155 }
156
157 return stub.libraryDecorator.link(ctx, flags, deps, objs)
158}
159
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700160func (stub *llndkStubDecorator) nativeCoverage() bool {
161 return false
162}
163
Jiyong Park64ca4b72017-11-14 20:53:00 +0900164func NewLLndkStubLibrary() *Module {
Dan Willemsenb916b802017-03-19 13:44:32 -0700165 module, library := NewLibrary(android.DeviceSupported)
166 library.BuildOnlyShared()
167 module.stl = nil
168 module.sanitize = nil
Nan Zhang0007d812017-11-07 10:57:05 -0800169 library.StripProperties.Strip.None = BoolPtr(true)
Dan Willemsenb916b802017-03-19 13:44:32 -0700170
171 stub := &llndkStubDecorator{
172 libraryDecorator: library,
173 }
Nan Zhang0007d812017-11-07 10:57:05 -0800174 stub.Properties.Vendor_available = BoolPtr(true)
Dan Willemsenb916b802017-03-19 13:44:32 -0700175 module.compiler = stub
176 module.linker = stub
177 module.installer = nil
178
Colin Cross36242852017-06-23 15:06:31 -0700179 module.AddProperties(
Jiyong Park5e676fe2019-04-17 13:12:10 +0900180 &module.Properties,
Colin Cross36242852017-06-23 15:06:31 -0700181 &stub.Properties,
182 &library.MutatedProperties,
183 &library.flagExporter.Properties)
184
185 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700186}
187
Patrice Arrudaea3fcdf2019-04-03 14:37:46 -0700188// llndk_library creates a stub llndk shared library based on the provided
189// version file. Example:
190//
191// llndk_library {
192// name: "libfoo",
193// symbol_file: "libfoo.map.txt",
194// export_include_dirs: ["include_vndk"],
195// }
Jiyong Parkda6eb592018-12-19 17:12:36 +0900196func LlndkLibraryFactory() android.Module {
Jiyong Park64ca4b72017-11-14 20:53:00 +0900197 module := NewLLndkStubLibrary()
Colin Cross36242852017-06-23 15:06:31 -0700198 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
199 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700200}
201
Jiyong Park2a454122017-10-19 15:59:33 +0900202type llndkHeadersDecorator struct {
203 *libraryDecorator
204}
205
206func (headers *llndkHeadersDecorator) Name(name string) string {
207 return name + llndkHeadersSuffix
208}
209
Patrice Arrudaea3fcdf2019-04-03 14:37:46 -0700210// llndk_headers contains a set of c/c++ llndk headers files which are imported
211// by other soongs cc modules.
Jiyong Park2a454122017-10-19 15:59:33 +0900212func llndkHeadersFactory() android.Module {
213 module, library := NewLibrary(android.DeviceSupported)
214 library.HeaderOnly()
Jiyong Park2a454122017-10-19 15:59:33 +0900215
216 decorator := &llndkHeadersDecorator{
217 libraryDecorator: library,
218 }
219
220 module.compiler = nil
221 module.linker = decorator
222 module.installer = nil
223
Jiyong Park5e676fe2019-04-17 13:12:10 +0900224 module.AddProperties(
225 &module.Properties,
226 &library.MutatedProperties,
227 &library.flagExporter.Properties)
Jiyong Park2a454122017-10-19 15:59:33 +0900228
Jiyong Park1d1119f2019-07-29 21:27:18 +0900229 module.Init()
Jiyong Park2a454122017-10-19 15:59:33 +0900230
231 return module
232}
233
Dan Willemsenb916b802017-03-19 13:44:32 -0700234func init() {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900235 android.RegisterModuleType("llndk_library", LlndkLibraryFactory)
Jiyong Park2a454122017-10-19 15:59:33 +0900236 android.RegisterModuleType("llndk_headers", llndkHeadersFactory)
Dan Willemsenb916b802017-03-19 13:44:32 -0700237}