blob: 30c4d4ca66b8fbe753627312b4a21d894409585b [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"
26)
27
28// Creates a stub shared library based on the provided version file.
29//
Dan Willemsenb916b802017-03-19 13:44:32 -070030// Example:
31//
32// llndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070033// name: "libfoo",
Dan Willemsenb916b802017-03-19 13:44:32 -070034// symbol_file: "libfoo.map.txt",
35// export_include_dirs: ["include_vndk"],
36// }
37//
38type llndkLibraryProperties struct {
39 // Relative path to the symbol map.
40 // An example file can be seen here: TODO(danalbert): Make an example.
41 Symbol_file string
42
43 // Whether to export any headers as -isystem instead of -I. Mainly for use by
44 // bionic/libc.
45 Export_headers_as_system bool
46
47 // Which headers to process with versioner. This really only handles
48 // bionic/libc/include right now.
49 Export_preprocessed_headers []string
50
51 // Whether the system library uses symbol versions.
52 Unversioned bool
Jiyong Park82e2bf32017-08-16 14:05:54 +090053
54 // whether this module can be directly depended upon by libs that are installed to /vendor.
55 // When set to false, this module can only be depended on by VNDK libraries, not vendor
56 // libraries. This effectively hides this module from vendors. Default value is true.
57 Vendor_available bool
Dan Willemsenb916b802017-03-19 13:44:32 -070058}
59
60type llndkStubDecorator struct {
61 *libraryDecorator
62
63 Properties llndkLibraryProperties
64
65 exportHeadersTimestamp android.OptionalPath
66 versionScriptPath android.ModuleGenPath
67}
68
George Burgess IVf5310e32017-07-19 11:39:53 -070069func (stub *llndkStubDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
70 flags = stub.baseCompiler.compilerFlags(ctx, flags)
71 return addStubLibraryCompilerFlags(flags)
72}
73
Dan Willemsenb916b802017-03-19 13:44:32 -070074func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Dan Willemsenb916b802017-03-19 13:44:32 -070075 objs, versionScript := compileStubLibrary(ctx, flags, stub.Properties.Symbol_file, "current", "--vndk")
76 stub.versionScriptPath = versionScript
77 return objs
78}
79
80func (stub *llndkStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
81 return Deps{}
82}
83
Dan Willemsen01a90592017-04-07 15:21:13 -070084func (stub *llndkStubDecorator) Name(name string) string {
85 return name + llndkLibrarySuffix
86}
87
Dan Willemsenb916b802017-03-19 13:44:32 -070088func (stub *llndkStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
89 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
90 llndkLibrarySuffix)
91 return stub.libraryDecorator.linkerFlags(ctx, flags)
92}
93
94func (stub *llndkStubDecorator) processHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path {
95 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir)
96 srcFiles := ctx.Glob(filepath.Join(srcDir.String(), "**/*.h"), nil)
97
98 var installPaths []android.WritablePath
99 for _, header := range srcFiles {
100 headerDir := filepath.Dir(header.String())
101 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir)
102 if err != nil {
103 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s",
104 srcDir.String(), headerDir, err)
105 continue
106 }
107
108 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base()))
109 }
110
111 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths)
112}
113
114func (stub *llndkStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
115 objs Objects) android.Path {
116
117 if !stub.Properties.Unversioned {
118 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
119 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
120 }
121
122 if len(stub.Properties.Export_preprocessed_headers) > 0 {
123 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
124
125 var timestampFiles android.Paths
126 for _, dir := range stub.Properties.Export_preprocessed_headers {
127 timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir))
128 }
129
130 includePrefix := "-I "
131 if stub.Properties.Export_headers_as_system {
132 includePrefix = "-isystem "
133 }
134
135 stub.reexportFlags([]string{includePrefix + " " + genHeaderOutDir.String()})
136 stub.reexportDeps(timestampFiles)
137 }
138
139 if stub.Properties.Export_headers_as_system {
140 stub.exportIncludes(ctx, "-isystem")
141 stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{}
142 }
143
144 return stub.libraryDecorator.link(ctx, flags, deps, objs)
145}
146
Colin Cross36242852017-06-23 15:06:31 -0700147func newLLndkStubLibrary() *Module {
Dan Willemsenb916b802017-03-19 13:44:32 -0700148 module, library := NewLibrary(android.DeviceSupported)
149 library.BuildOnlyShared()
150 module.stl = nil
151 module.sanitize = nil
152 library.StripProperties.Strip.None = true
153
154 stub := &llndkStubDecorator{
155 libraryDecorator: library,
156 }
Jiyong Park82e2bf32017-08-16 14:05:54 +0900157 stub.Properties.Vendor_available = true
Dan Willemsenb916b802017-03-19 13:44:32 -0700158 module.compiler = stub
159 module.linker = stub
160 module.installer = nil
161
Colin Cross36242852017-06-23 15:06:31 -0700162 module.AddProperties(
163 &stub.Properties,
164 &library.MutatedProperties,
165 &library.flagExporter.Properties)
166
167 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700168}
169
Colin Cross36242852017-06-23 15:06:31 -0700170func llndkLibraryFactory() android.Module {
171 module := newLLndkStubLibrary()
172 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
173 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700174}
175
176func init() {
177 android.RegisterModuleType("llndk_library", llndkLibraryFactory)
178}