blob: 4446ab3eaf923d4806201c61ab86e2e2e2d3e87b [file] [log] [blame]
Colin Crossce75d2c2016-10-06 16:12:58 -07001// Copyright 2016 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 "android/soong/android"
Colin Crossce75d2c2016-10-06 16:12:58 -070019)
20
21func init() {
Colin Crossdfee1bc2017-03-17 14:03:18 -070022 android.RegisterModuleType("cc_prebuilt_library_shared", prebuiltSharedLibraryFactory)
Colin Crossde89fb82017-03-17 13:28:06 -070023 android.RegisterModuleType("cc_prebuilt_library_static", prebuiltStaticLibraryFactory)
24 android.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070025}
26
27type prebuiltLinkerInterface interface {
28 Name(string) string
29 prebuilt() *android.Prebuilt
30}
31
Colin Crossde89fb82017-03-17 13:28:06 -070032type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070033 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080034
Colin Cross74d73e22017-08-02 11:05:49 -070035 properties struct {
36 Srcs []string `android:"arch_variant"`
Logan Chien4fcea3d2018-11-20 11:59:08 +080037
38 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
39 // symbols, etc), default true.
40 Check_elf_files *bool
Colin Cross74d73e22017-08-02 11:05:49 -070041 }
Colin Crossce75d2c2016-10-06 16:12:58 -070042}
43
Colin Crossde89fb82017-03-17 13:28:06 -070044func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070045 return &p.Prebuilt
46}
47
Colin Cross74d73e22017-08-02 11:05:49 -070048func (p *prebuiltLinker) PrebuiltSrcs() []string {
49 return p.properties.Srcs
50}
51
Colin Crossde89fb82017-03-17 13:28:06 -070052type prebuiltLibraryLinker struct {
53 *libraryDecorator
54 prebuiltLinker
55}
56
57var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
58
Yi Kong00981662018-08-13 16:02:49 -070059func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
60
61func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
62 // export_header_lib_headers needs to be passed along
63 return Deps{
64 HeaderLibs: p.baseLinker.Properties.Header_libs,
65 ReexportHeaderLibHeaders: p.baseLinker.Properties.Export_header_lib_headers,
66 }
67}
68
69func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070070 return flags
Yi Kong00981662018-08-13 16:02:49 -070071}
72
73func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
74 return p.libraryDecorator.linkerProps()
75}
76
Colin Crossce75d2c2016-10-06 16:12:58 -070077func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070078 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossce75d2c2016-10-06 16:12:58 -070079 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -070080 if len(p.properties.Srcs) > 0 {
Colin Crossce75d2c2016-10-06 16:12:58 -070081 p.libraryDecorator.exportIncludes(ctx, "-I")
82 p.libraryDecorator.reexportFlags(deps.ReexportedFlags)
83 p.libraryDecorator.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross88f6fef2018-09-05 14:20:03 -070084
85 builderFlags := flagsToBuilderFlags(flags)
86
87 in := p.Prebuilt.SingleSourcePath(ctx)
88
89 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -070090 p.unstrippedOutputFile = in
Colin Cross88f6fef2018-09-05 14:20:03 -070091 libName := ctx.baseModuleName() + flags.Toolchain.ShlibSuffix()
92 if p.needsStrip(ctx) {
93 stripped := android.PathForModuleOut(ctx, "stripped", libName)
94 p.strip(ctx, in, stripped, builderFlags)
95 in = stripped
96 }
97
Colin Crossb60190a2018-09-04 16:28:17 -070098 // Optimize out relinking against shared libraries whose interface hasn't changed by
99 // depending on a table of contents file instead of the library itself.
100 tocFile := android.PathForModuleOut(ctx, libName+".toc")
101 p.tocFile = android.OptionalPathForPath(tocFile)
102 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700103 }
104
105 return in
Colin Crossce75d2c2016-10-06 16:12:58 -0700106 }
107
108 return nil
109}
110
Jiyong Park379de2f2018-12-19 02:47:14 +0900111func (p *prebuiltLibraryLinker) shared() bool {
112 return p.libraryDecorator.shared()
113}
114
Colin Cross36242852017-06-23 15:06:31 -0700115func prebuiltSharedLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700116 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
117 return module.Init()
118}
119
120func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
121 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700122 library.BuildOnlyShared()
Colin Crossce75d2c2016-10-06 16:12:58 -0700123 module.compiler = nil
124
125 prebuilt := &prebuiltLibraryLinker{
126 libraryDecorator: library,
127 }
128 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700129
Colin Cross74d73e22017-08-02 11:05:49 -0700130 module.AddProperties(&prebuilt.properties)
131
132 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Jiyong Park379de2f2018-12-19 02:47:14 +0900133
134 // Prebuilt libraries can be included in APEXes
135 android.InitApexModule(module)
136
Leo Li74f7b972017-05-17 11:30:45 -0700137 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700138}
139
Colin Cross36242852017-06-23 15:06:31 -0700140func prebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700141 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
142 return module.Init()
143}
144
145func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
146 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700147 library.BuildOnlyStatic()
148 module.compiler = nil
149
150 prebuilt := &prebuiltLibraryLinker{
151 libraryDecorator: library,
152 }
153 module.linker = prebuilt
154
Colin Cross74d73e22017-08-02 11:05:49 -0700155 module.AddProperties(&prebuilt.properties)
156
157 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700158 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700159}
160
161type prebuiltBinaryLinker struct {
162 *binaryDecorator
163 prebuiltLinker
164}
165
166var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
167
Colin Crossde89fb82017-03-17 13:28:06 -0700168func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
169 flags Flags, deps PathDeps, objs Objects) android.Path {
170 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700171 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700172 builderFlags := flagsToBuilderFlags(flags)
173
174 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
175 in := p.Prebuilt.SingleSourcePath(ctx)
176
Colin Crossb60190a2018-09-04 16:28:17 -0700177 p.unstrippedOutputFile = in
178
Colin Cross88f6fef2018-09-05 14:20:03 -0700179 if p.needsStrip(ctx) {
180 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
181 p.strip(ctx, in, stripped, builderFlags)
182 in = stripped
183 }
Colin Cross94921e72017-08-08 16:20:15 -0700184
185 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700186 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700187 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700188 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700189 Description: "prebuilt",
190 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700191 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700192 })
193
194 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700195 }
196
197 return nil
198}
199
Colin Cross36242852017-06-23 15:06:31 -0700200func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700201 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
202 return module.Init()
203}
204
205func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
206 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700207 module.compiler = nil
208
209 prebuilt := &prebuiltBinaryLinker{
210 binaryDecorator: binary,
211 }
212 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700213
Colin Cross74d73e22017-08-02 11:05:49 -0700214 module.AddProperties(&prebuilt.properties)
215
216 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700217 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700218}