blob: 09050aa347e548bf1ebacc68d192f7787e58a32d [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"
Jooyung Han61b66e92020-03-21 14:21:46 +000022
23 "github.com/google/blueprint"
Dan Willemsenb916b802017-03-19 13:44:32 -070024)
25
Jooyung Han61b66e92020-03-21 14:21:46 +000026var llndkImplDep = struct {
27 blueprint.DependencyTag
28}{}
29
Dan Willemsenb916b802017-03-19 13:44:32 -070030var (
31 llndkLibrarySuffix = ".llndk"
Jiyong Park2a454122017-10-19 15:59:33 +090032 llndkHeadersSuffix = ".llndk"
Dan Willemsenb916b802017-03-19 13:44:32 -070033)
34
35// Creates a stub shared library based on the provided version file.
36//
Dan Willemsenb916b802017-03-19 13:44:32 -070037// Example:
38//
39// llndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070040// name: "libfoo",
Dan Willemsenb916b802017-03-19 13:44:32 -070041// symbol_file: "libfoo.map.txt",
42// export_include_dirs: ["include_vndk"],
43// }
44//
45type llndkLibraryProperties struct {
46 // Relative path to the symbol map.
47 // An example file can be seen here: TODO(danalbert): Make an example.
Nan Zhang0007d812017-11-07 10:57:05 -080048 Symbol_file *string
Dan Willemsenb916b802017-03-19 13:44:32 -070049
50 // Whether to export any headers as -isystem instead of -I. Mainly for use by
51 // bionic/libc.
Nan Zhang0007d812017-11-07 10:57:05 -080052 Export_headers_as_system *bool
Dan Willemsenb916b802017-03-19 13:44:32 -070053
54 // Which headers to process with versioner. This really only handles
55 // bionic/libc/include right now.
56 Export_preprocessed_headers []string
57
58 // Whether the system library uses symbol versions.
Nan Zhang0007d812017-11-07 10:57:05 -080059 Unversioned *bool
Jiyong Park82e2bf32017-08-16 14:05:54 +090060
61 // whether this module can be directly depended upon by libs that are installed to /vendor.
62 // When set to false, this module can only be depended on by VNDK libraries, not vendor
63 // libraries. This effectively hides this module from vendors. Default value is true.
Nan Zhang0007d812017-11-07 10:57:05 -080064 Vendor_available *bool
Jiyong Park2a454122017-10-19 15:59:33 +090065
66 // list of llndk headers to re-export include directories from.
67 Export_llndk_headers []string `android:"arch_variant"`
Dan Willemsenb916b802017-03-19 13:44:32 -070068}
69
70type llndkStubDecorator struct {
71 *libraryDecorator
72
73 Properties llndkLibraryProperties
74
75 exportHeadersTimestamp android.OptionalPath
76 versionScriptPath android.ModuleGenPath
77}
78
Colin Crossf18e1102017-11-16 14:33:08 -080079func (stub *llndkStubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
80 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
George Burgess IVf5310e32017-07-19 11:39:53 -070081 return addStubLibraryCompilerFlags(flags)
82}
83
Dan Willemsenb916b802017-03-19 13:44:32 -070084func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Justin Yun5f7f7e82019-11-18 19:52:14 +090085 vndkVer := ctx.Module().(*Module).VndkVersion()
86 if !inList(vndkVer, ctx.Config().PlatformVersionCombinedCodenames()) || vndkVer == "" {
87 // For non-enforcing devices, vndkVer is empty. Use "current" in that case, too.
88 vndkVer = "current"
Justin Yun732aa6a2018-03-23 17:43:47 +090089 }
Jooyung Han61b66e92020-03-21 14:21:46 +000090 if stub.stubsVersion() != "" {
91 vndkVer = stub.stubsVersion()
92 }
Justin Yun5f7f7e82019-11-18 19:52:14 +090093 objs, versionScript := compileStubLibrary(ctx, flags, String(stub.Properties.Symbol_file), vndkVer, "--llndk")
Dan Willemsenb916b802017-03-19 13:44:32 -070094 stub.versionScriptPath = versionScript
95 return objs
96}
97
98func (stub *llndkStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Jiyong Park2a454122017-10-19 15:59:33 +090099 headers := addSuffix(stub.Properties.Export_llndk_headers, llndkHeadersSuffix)
100 deps.HeaderLibs = append(deps.HeaderLibs, headers...)
101 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, headers...)
102 return deps
Dan Willemsenb916b802017-03-19 13:44:32 -0700103}
104
Dan Willemsen01a90592017-04-07 15:21:13 -0700105func (stub *llndkStubDecorator) Name(name string) string {
106 return name + llndkLibrarySuffix
107}
108
Dan Willemsenb916b802017-03-19 13:44:32 -0700109func (stub *llndkStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
110 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
111 llndkLibrarySuffix)
112 return stub.libraryDecorator.linkerFlags(ctx, flags)
113}
114
115func (stub *llndkStubDecorator) processHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path {
116 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir)
Dan Willemsen540a78c2018-02-26 21:50:08 -0800117 srcFiles := ctx.GlobFiles(filepath.Join(srcDir.String(), "**/*.h"), nil)
Dan Willemsenb916b802017-03-19 13:44:32 -0700118
119 var installPaths []android.WritablePath
120 for _, header := range srcFiles {
121 headerDir := filepath.Dir(header.String())
122 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir)
123 if err != nil {
124 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s",
125 srcDir.String(), headerDir, err)
126 continue
127 }
128
129 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base()))
130 }
131
132 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths)
133}
134
135func (stub *llndkStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
136 objs Objects) android.Path {
137
Nan Zhang0007d812017-11-07 10:57:05 -0800138 if !Bool(stub.Properties.Unversioned) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700139 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800140 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
Dan Willemsen939408a2019-06-10 18:02:25 -0700141 flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
Dan Willemsenb916b802017-03-19 13:44:32 -0700142 }
143
144 if len(stub.Properties.Export_preprocessed_headers) > 0 {
145 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
146
147 var timestampFiles android.Paths
148 for _, dir := range stub.Properties.Export_preprocessed_headers {
149 timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir))
150 }
151
Nan Zhang0007d812017-11-07 10:57:05 -0800152 if Bool(stub.Properties.Export_headers_as_system) {
Jiyong Park74955042019-10-22 20:19:51 +0900153 stub.reexportSystemDirs(genHeaderOutDir)
Inseob Kim69378442019-06-03 19:10:47 +0900154 } else {
Jiyong Park74955042019-10-22 20:19:51 +0900155 stub.reexportDirs(genHeaderOutDir)
Dan Willemsenb916b802017-03-19 13:44:32 -0700156 }
157
Inseob Kim69378442019-06-03 19:10:47 +0900158 stub.reexportDeps(timestampFiles...)
Dan Willemsenb916b802017-03-19 13:44:32 -0700159 }
160
Nan Zhang0007d812017-11-07 10:57:05 -0800161 if Bool(stub.Properties.Export_headers_as_system) {
Inseob Kim69378442019-06-03 19:10:47 +0900162 stub.exportIncludesAsSystem(ctx)
Dan Willemsenb916b802017-03-19 13:44:32 -0700163 stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{}
164 }
165
Jooyung Han61b66e92020-03-21 14:21:46 +0000166 if stub.stubsVersion() != "" {
167 stub.reexportFlags("-D" + versioningMacroName(ctx.baseModuleName()) + "=" + stub.stubsVersion())
168 }
169
Dan Willemsenb916b802017-03-19 13:44:32 -0700170 return stub.libraryDecorator.link(ctx, flags, deps, objs)
171}
172
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700173func (stub *llndkStubDecorator) nativeCoverage() bool {
174 return false
175}
176
Jiyong Park64ca4b72017-11-14 20:53:00 +0900177func NewLLndkStubLibrary() *Module {
Dan Willemsenb916b802017-03-19 13:44:32 -0700178 module, library := NewLibrary(android.DeviceSupported)
179 library.BuildOnlyShared()
180 module.stl = nil
181 module.sanitize = nil
Nan Zhang0007d812017-11-07 10:57:05 -0800182 library.StripProperties.Strip.None = BoolPtr(true)
Dan Willemsenb916b802017-03-19 13:44:32 -0700183
184 stub := &llndkStubDecorator{
185 libraryDecorator: library,
186 }
Nan Zhang0007d812017-11-07 10:57:05 -0800187 stub.Properties.Vendor_available = BoolPtr(true)
Dan Willemsenb916b802017-03-19 13:44:32 -0700188 module.compiler = stub
189 module.linker = stub
190 module.installer = nil
191
Colin Cross36242852017-06-23 15:06:31 -0700192 module.AddProperties(
Jiyong Park5e676fe2019-04-17 13:12:10 +0900193 &module.Properties,
Colin Cross36242852017-06-23 15:06:31 -0700194 &stub.Properties,
195 &library.MutatedProperties,
196 &library.flagExporter.Properties)
197
198 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700199}
200
Patrice Arrudaea3fcdf2019-04-03 14:37:46 -0700201// llndk_library creates a stub llndk shared library based on the provided
202// version file. Example:
203//
204// llndk_library {
205// name: "libfoo",
206// symbol_file: "libfoo.map.txt",
207// export_include_dirs: ["include_vndk"],
208// }
Jiyong Parkda6eb592018-12-19 17:12:36 +0900209func LlndkLibraryFactory() android.Module {
Jiyong Park64ca4b72017-11-14 20:53:00 +0900210 module := NewLLndkStubLibrary()
Colin Cross36242852017-06-23 15:06:31 -0700211 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
212 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700213}
214
Jiyong Park2a454122017-10-19 15:59:33 +0900215type llndkHeadersDecorator struct {
216 *libraryDecorator
217}
218
219func (headers *llndkHeadersDecorator) Name(name string) string {
220 return name + llndkHeadersSuffix
221}
222
Patrice Arrudaea3fcdf2019-04-03 14:37:46 -0700223// llndk_headers contains a set of c/c++ llndk headers files which are imported
224// by other soongs cc modules.
Jiyong Park2a454122017-10-19 15:59:33 +0900225func llndkHeadersFactory() android.Module {
226 module, library := NewLibrary(android.DeviceSupported)
227 library.HeaderOnly()
Jiyong Park2a454122017-10-19 15:59:33 +0900228
229 decorator := &llndkHeadersDecorator{
230 libraryDecorator: library,
231 }
232
233 module.compiler = nil
234 module.linker = decorator
235 module.installer = nil
236
Jiyong Park5e676fe2019-04-17 13:12:10 +0900237 module.AddProperties(
238 &module.Properties,
239 &library.MutatedProperties,
240 &library.flagExporter.Properties)
Jiyong Park2a454122017-10-19 15:59:33 +0900241
Jiyong Park1d1119f2019-07-29 21:27:18 +0900242 module.Init()
Jiyong Park2a454122017-10-19 15:59:33 +0900243
244 return module
245}
246
Dan Willemsenb916b802017-03-19 13:44:32 -0700247func init() {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900248 android.RegisterModuleType("llndk_library", LlndkLibraryFactory)
Jiyong Park2a454122017-10-19 15:59:33 +0900249 android.RegisterModuleType("llndk_headers", llndkHeadersFactory)
Dan Willemsenb916b802017-03-19 13:44:32 -0700250}