blob: 60efc57c6c24639c297fff4de7b4a8c524fac8b7 [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//
32// The name of the generated file will be based on the module name by stripping
33// the ".llndk" suffix from the module name. Module names must end with ".llndk"
34// (as a convention to allow soong to guess the LL-NDK name of a dependency when
35// needed). "libfoo.llndk" will generate "libfoo.so".
36//
37// Example:
38//
39// llndk_library {
40// name: "libfoo.llndk",
41// 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.
48 Symbol_file string
49
50 // Whether to export any headers as -isystem instead of -I. Mainly for use by
51 // bionic/libc.
52 Export_headers_as_system bool
53
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.
59 Unversioned bool
60}
61
62type llndkStubDecorator struct {
63 *libraryDecorator
64
65 Properties llndkLibraryProperties
66
67 exportHeadersTimestamp android.OptionalPath
68 versionScriptPath android.ModuleGenPath
69}
70
71func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
72 if !strings.HasSuffix(ctx.ModuleName(), llndkLibrarySuffix) {
73 ctx.ModuleErrorf("llndk_library modules names must be suffixed with %q\n",
74 llndkLibrarySuffix)
75 }
76 objs, versionScript := compileStubLibrary(ctx, flags, stub.Properties.Symbol_file, "current", "--vndk")
77 stub.versionScriptPath = versionScript
78 return objs
79}
80
81func (stub *llndkStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
82 return Deps{}
83}
84
85func (stub *llndkStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
86 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
87 llndkLibrarySuffix)
88 return stub.libraryDecorator.linkerFlags(ctx, flags)
89}
90
91func (stub *llndkStubDecorator) processHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path {
92 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir)
93 srcFiles := ctx.Glob(filepath.Join(srcDir.String(), "**/*.h"), nil)
94
95 var installPaths []android.WritablePath
96 for _, header := range srcFiles {
97 headerDir := filepath.Dir(header.String())
98 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir)
99 if err != nil {
100 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s",
101 srcDir.String(), headerDir, err)
102 continue
103 }
104
105 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base()))
106 }
107
108 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths)
109}
110
111func (stub *llndkStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
112 objs Objects) android.Path {
113
114 if !stub.Properties.Unversioned {
115 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
116 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
117 }
118
119 if len(stub.Properties.Export_preprocessed_headers) > 0 {
120 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
121
122 var timestampFiles android.Paths
123 for _, dir := range stub.Properties.Export_preprocessed_headers {
124 timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir))
125 }
126
127 includePrefix := "-I "
128 if stub.Properties.Export_headers_as_system {
129 includePrefix = "-isystem "
130 }
131
132 stub.reexportFlags([]string{includePrefix + " " + genHeaderOutDir.String()})
133 stub.reexportDeps(timestampFiles)
134 }
135
136 if stub.Properties.Export_headers_as_system {
137 stub.exportIncludes(ctx, "-isystem")
138 stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{}
139 }
140
141 return stub.libraryDecorator.link(ctx, flags, deps, objs)
142}
143
144func newLLndkStubLibrary() (*Module, []interface{}) {
145 module, library := NewLibrary(android.DeviceSupported)
146 library.BuildOnlyShared()
147 module.stl = nil
148 module.sanitize = nil
149 library.StripProperties.Strip.None = true
150
151 stub := &llndkStubDecorator{
152 libraryDecorator: library,
153 }
154 module.compiler = stub
155 module.linker = stub
156 module.installer = nil
157
158 return module, []interface{}{&stub.Properties, &library.MutatedProperties, &library.flagExporter.Properties}
159}
160
161func llndkLibraryFactory() (blueprint.Module, []interface{}) {
162 module, properties := newLLndkStubLibrary()
163 return android.InitAndroidArchModule(module, android.DeviceSupported,
164 android.MultilibBoth, properties...)
165}
166
167func init() {
168 android.RegisterModuleType("llndk_library", llndkLibraryFactory)
169}