blob: cde1bc79d6b8733e514a3fa8e56de7c944a1c224 [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
21 "github.com/google/blueprint"
22
23 "android/soong/android"
24)
25
26var (
27 llndkLibrarySuffix = ".llndk"
28)
29
30// Creates a stub shared library based on the provided version file.
31//
Dan Willemsenb916b802017-03-19 13:44:32 -070032// Example:
33//
34// llndk_library {
Dan Willemsen01a90592017-04-07 15:21:13 -070035// name: "libfoo",
Dan Willemsenb916b802017-03-19 13:44:32 -070036// symbol_file: "libfoo.map.txt",
37// export_include_dirs: ["include_vndk"],
38// }
39//
40type llndkLibraryProperties struct {
41 // Relative path to the symbol map.
42 // An example file can be seen here: TODO(danalbert): Make an example.
43 Symbol_file string
44
45 // Whether to export any headers as -isystem instead of -I. Mainly for use by
46 // bionic/libc.
47 Export_headers_as_system bool
48
49 // Which headers to process with versioner. This really only handles
50 // bionic/libc/include right now.
51 Export_preprocessed_headers []string
52
53 // Whether the system library uses symbol versions.
54 Unversioned bool
55}
56
57type llndkStubDecorator struct {
58 *libraryDecorator
59
60 Properties llndkLibraryProperties
61
62 exportHeadersTimestamp android.OptionalPath
63 versionScriptPath android.ModuleGenPath
64}
65
66func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Dan Willemsenb916b802017-03-19 13:44:32 -070067 objs, versionScript := compileStubLibrary(ctx, flags, stub.Properties.Symbol_file, "current", "--vndk")
68 stub.versionScriptPath = versionScript
69 return objs
70}
71
72func (stub *llndkStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
73 return Deps{}
74}
75
Dan Willemsen01a90592017-04-07 15:21:13 -070076func (stub *llndkStubDecorator) Name(name string) string {
77 return name + llndkLibrarySuffix
78}
79
Dan Willemsenb916b802017-03-19 13:44:32 -070080func (stub *llndkStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
81 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
82 llndkLibrarySuffix)
83 return stub.libraryDecorator.linkerFlags(ctx, flags)
84}
85
86func (stub *llndkStubDecorator) processHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path {
87 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir)
88 srcFiles := ctx.Glob(filepath.Join(srcDir.String(), "**/*.h"), nil)
89
90 var installPaths []android.WritablePath
91 for _, header := range srcFiles {
92 headerDir := filepath.Dir(header.String())
93 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir)
94 if err != nil {
95 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s",
96 srcDir.String(), headerDir, err)
97 continue
98 }
99
100 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base()))
101 }
102
103 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths)
104}
105
106func (stub *llndkStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
107 objs Objects) android.Path {
108
109 if !stub.Properties.Unversioned {
110 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
111 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
112 }
113
114 if len(stub.Properties.Export_preprocessed_headers) > 0 {
115 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
116
117 var timestampFiles android.Paths
118 for _, dir := range stub.Properties.Export_preprocessed_headers {
119 timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir))
120 }
121
122 includePrefix := "-I "
123 if stub.Properties.Export_headers_as_system {
124 includePrefix = "-isystem "
125 }
126
127 stub.reexportFlags([]string{includePrefix + " " + genHeaderOutDir.String()})
128 stub.reexportDeps(timestampFiles)
129 }
130
131 if stub.Properties.Export_headers_as_system {
132 stub.exportIncludes(ctx, "-isystem")
133 stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{}
134 }
135
136 return stub.libraryDecorator.link(ctx, flags, deps, objs)
137}
138
139func newLLndkStubLibrary() (*Module, []interface{}) {
140 module, library := NewLibrary(android.DeviceSupported)
141 library.BuildOnlyShared()
142 module.stl = nil
143 module.sanitize = nil
144 library.StripProperties.Strip.None = true
145
146 stub := &llndkStubDecorator{
147 libraryDecorator: library,
148 }
149 module.compiler = stub
150 module.linker = stub
151 module.installer = nil
152
153 return module, []interface{}{&stub.Properties, &library.MutatedProperties, &library.flagExporter.Properties}
154}
155
156func llndkLibraryFactory() (blueprint.Module, []interface{}) {
157 module, properties := newLLndkStubLibrary()
158 return android.InitAndroidArchModule(module, android.DeviceSupported,
159 android.MultilibBoth, properties...)
160}
161
162func init() {
163 android.RegisterModuleType("llndk_library", llndkLibraryFactory)
164}