blob: c3d3462bb997218c25bdc1c7f45c8dcbd575fc9e [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
53}
54
55type llndkStubDecorator struct {
56 *libraryDecorator
57
58 Properties llndkLibraryProperties
59
60 exportHeadersTimestamp android.OptionalPath
61 versionScriptPath android.ModuleGenPath
62}
63
George Burgess IVf5310e32017-07-19 11:39:53 -070064func (stub *llndkStubDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
65 flags = stub.baseCompiler.compilerFlags(ctx, flags)
66 return addStubLibraryCompilerFlags(flags)
67}
68
Dan Willemsenb916b802017-03-19 13:44:32 -070069func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Dan Willemsenb916b802017-03-19 13:44:32 -070070 objs, versionScript := compileStubLibrary(ctx, flags, stub.Properties.Symbol_file, "current", "--vndk")
71 stub.versionScriptPath = versionScript
72 return objs
73}
74
75func (stub *llndkStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
76 return Deps{}
77}
78
Dan Willemsen01a90592017-04-07 15:21:13 -070079func (stub *llndkStubDecorator) Name(name string) string {
80 return name + llndkLibrarySuffix
81}
82
Dan Willemsenb916b802017-03-19 13:44:32 -070083func (stub *llndkStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
84 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(),
85 llndkLibrarySuffix)
86 return stub.libraryDecorator.linkerFlags(ctx, flags)
87}
88
89func (stub *llndkStubDecorator) processHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path {
90 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir)
91 srcFiles := ctx.Glob(filepath.Join(srcDir.String(), "**/*.h"), nil)
92
93 var installPaths []android.WritablePath
94 for _, header := range srcFiles {
95 headerDir := filepath.Dir(header.String())
96 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir)
97 if err != nil {
98 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s",
99 srcDir.String(), headerDir, err)
100 continue
101 }
102
103 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base()))
104 }
105
106 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths)
107}
108
109func (stub *llndkStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
110 objs Objects) android.Path {
111
112 if !stub.Properties.Unversioned {
113 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
114 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag)
115 }
116
117 if len(stub.Properties.Export_preprocessed_headers) > 0 {
118 genHeaderOutDir := android.PathForModuleGen(ctx, "include")
119
120 var timestampFiles android.Paths
121 for _, dir := range stub.Properties.Export_preprocessed_headers {
122 timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir))
123 }
124
125 includePrefix := "-I "
126 if stub.Properties.Export_headers_as_system {
127 includePrefix = "-isystem "
128 }
129
130 stub.reexportFlags([]string{includePrefix + " " + genHeaderOutDir.String()})
131 stub.reexportDeps(timestampFiles)
132 }
133
134 if stub.Properties.Export_headers_as_system {
135 stub.exportIncludes(ctx, "-isystem")
136 stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{}
137 }
138
139 return stub.libraryDecorator.link(ctx, flags, deps, objs)
140}
141
Colin Cross36242852017-06-23 15:06:31 -0700142func newLLndkStubLibrary() *Module {
Dan Willemsenb916b802017-03-19 13:44:32 -0700143 module, library := NewLibrary(android.DeviceSupported)
144 library.BuildOnlyShared()
145 module.stl = nil
146 module.sanitize = nil
147 library.StripProperties.Strip.None = true
148
149 stub := &llndkStubDecorator{
150 libraryDecorator: library,
151 }
152 module.compiler = stub
153 module.linker = stub
154 module.installer = nil
155
Colin Cross36242852017-06-23 15:06:31 -0700156 module.AddProperties(
157 &stub.Properties,
158 &library.MutatedProperties,
159 &library.flagExporter.Properties)
160
161 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700162}
163
Colin Cross36242852017-06-23 15:06:31 -0700164func llndkLibraryFactory() android.Module {
165 module := newLLndkStubLibrary()
166 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
167 return module
Dan Willemsenb916b802017-03-19 13:44:32 -0700168}
169
170func init() {
171 android.RegisterModuleType("llndk_library", llndkLibraryFactory)
172}