blob: 5ffeb3288ec094865327c9290e443a37faa0a969 [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
Patrice Arruda3554a982019-03-27 19:09:10 -070032type prebuiltLinkerProperties struct {
33
34 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
35 Srcs []string `android:"path,arch_variant"`
36
37 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
38 // symbols, etc), default true.
39 Check_elf_files *bool
40}
41
Colin Crossde89fb82017-03-17 13:28:06 -070042type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070043 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080044
Patrice Arruda3554a982019-03-27 19:09:10 -070045 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070046}
47
Colin Crossde89fb82017-03-17 13:28:06 -070048func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070049 return &p.Prebuilt
50}
51
Colin Cross74d73e22017-08-02 11:05:49 -070052func (p *prebuiltLinker) PrebuiltSrcs() []string {
53 return p.properties.Srcs
54}
55
Colin Crossde89fb82017-03-17 13:28:06 -070056type prebuiltLibraryLinker struct {
57 *libraryDecorator
58 prebuiltLinker
59}
60
61var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
62
Yi Kong00981662018-08-13 16:02:49 -070063func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
64
65func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080066 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070067}
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
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700115func (p *prebuiltLibraryLinker) nativeCoverage() bool {
116 return false
117}
118
Patrice Arruda3554a982019-03-27 19:09:10 -0700119// cc_prebuilt_library_shared installs a precompiled shared library that are
120// listed in the srcs property in the device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700121func prebuiltSharedLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700122 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
123 return module.Init()
124}
125
126func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
127 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700128 library.BuildOnlyShared()
Colin Crossce75d2c2016-10-06 16:12:58 -0700129 module.compiler = nil
130
131 prebuilt := &prebuiltLibraryLinker{
132 libraryDecorator: library,
133 }
134 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700135
Colin Cross74d73e22017-08-02 11:05:49 -0700136 module.AddProperties(&prebuilt.properties)
137
138 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Jiyong Park379de2f2018-12-19 02:47:14 +0900139
140 // Prebuilt libraries can be included in APEXes
141 android.InitApexModule(module)
142
Leo Li74f7b972017-05-17 11:30:45 -0700143 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700144}
145
Patrice Arruda3554a982019-03-27 19:09:10 -0700146// cc_prebuilt_library_static installs a precompiled static library that are
147// listed in the srcs property in the device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700148func prebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700149 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
150 return module.Init()
151}
152
153func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
154 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700155 library.BuildOnlyStatic()
156 module.compiler = nil
157
158 prebuilt := &prebuiltLibraryLinker{
159 libraryDecorator: library,
160 }
161 module.linker = prebuilt
162
Colin Cross74d73e22017-08-02 11:05:49 -0700163 module.AddProperties(&prebuilt.properties)
164
165 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700166 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700167}
168
169type prebuiltBinaryLinker struct {
170 *binaryDecorator
171 prebuiltLinker
172}
173
174var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
175
Colin Crossde89fb82017-03-17 13:28:06 -0700176func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
177 flags Flags, deps PathDeps, objs Objects) android.Path {
178 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700179 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700180 builderFlags := flagsToBuilderFlags(flags)
181
182 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
183 in := p.Prebuilt.SingleSourcePath(ctx)
184
Colin Crossb60190a2018-09-04 16:28:17 -0700185 p.unstrippedOutputFile = in
186
Colin Cross88f6fef2018-09-05 14:20:03 -0700187 if p.needsStrip(ctx) {
188 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
189 p.strip(ctx, in, stripped, builderFlags)
190 in = stripped
191 }
Colin Cross94921e72017-08-08 16:20:15 -0700192
193 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700194 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700195 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700196 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700197 Description: "prebuilt",
198 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700199 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700200 })
201
202 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700203 }
204
205 return nil
206}
207
Patrice Arruda3554a982019-03-27 19:09:10 -0700208// cc_prebuilt_binary installs a precompiled executable in srcs property in the
209// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700210func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700211 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
212 return module.Init()
213}
214
215func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
216 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700217 module.compiler = nil
218
219 prebuilt := &prebuiltBinaryLinker{
220 binaryDecorator: binary,
221 }
222 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700223
Colin Cross74d73e22017-08-02 11:05:49 -0700224 module.AddProperties(&prebuilt.properties)
225
226 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700227 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700228}