blob: dc6c43a7cca04018d56a227bed55e1007fd69862 [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 Cross33b2fb72019-05-14 14:07:01 -070056type prebuiltLibraryInterface interface {
57 libraryInterface
58 prebuiltLinkerInterface
59 disablePrebuilt()
60}
61
Colin Crossde89fb82017-03-17 13:28:06 -070062type prebuiltLibraryLinker struct {
63 *libraryDecorator
64 prebuiltLinker
65}
66
67var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070068var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070069
Yi Kong00981662018-08-13 16:02:49 -070070func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
71
72func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080073 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070074}
75
76func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070077 return flags
Yi Kong00981662018-08-13 16:02:49 -070078}
79
80func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
81 return p.libraryDecorator.linkerProps()
82}
83
Colin Crossce75d2c2016-10-06 16:12:58 -070084func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070085 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossce75d2c2016-10-06 16:12:58 -070086 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -070087 if len(p.properties.Srcs) > 0 {
Inseob Kim69378442019-06-03 19:10:47 +090088 p.libraryDecorator.exportIncludes(ctx)
89 p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
90 p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
91 p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
92 p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
Colin Cross88f6fef2018-09-05 14:20:03 -070093
94 builderFlags := flagsToBuilderFlags(flags)
95
96 in := p.Prebuilt.SingleSourcePath(ctx)
97
98 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -070099 p.unstrippedOutputFile = in
Colin Cross88f6fef2018-09-05 14:20:03 -0700100 libName := ctx.baseModuleName() + flags.Toolchain.ShlibSuffix()
101 if p.needsStrip(ctx) {
102 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700103 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700104 in = stripped
105 }
106
Colin Crossb60190a2018-09-04 16:28:17 -0700107 // Optimize out relinking against shared libraries whose interface hasn't changed by
108 // depending on a table of contents file instead of the library itself.
109 tocFile := android.PathForModuleOut(ctx, libName+".toc")
110 p.tocFile = android.OptionalPathForPath(tocFile)
111 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700112 }
113
114 return in
Colin Crossce75d2c2016-10-06 16:12:58 -0700115 }
116
117 return nil
118}
119
Jiyong Park379de2f2018-12-19 02:47:14 +0900120func (p *prebuiltLibraryLinker) shared() bool {
121 return p.libraryDecorator.shared()
122}
123
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700124func (p *prebuiltLibraryLinker) nativeCoverage() bool {
125 return false
126}
127
Colin Cross33b2fb72019-05-14 14:07:01 -0700128func (p *prebuiltLibraryLinker) disablePrebuilt() {
129 p.properties.Srcs = nil
130}
131
Patrice Arruda3554a982019-03-27 19:09:10 -0700132// cc_prebuilt_library_shared installs a precompiled shared library that are
133// listed in the srcs property in the device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700134func prebuiltSharedLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700135 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
136 return module.Init()
137}
138
139func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
140 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700141 library.BuildOnlyShared()
Colin Crossce75d2c2016-10-06 16:12:58 -0700142 module.compiler = nil
143
144 prebuilt := &prebuiltLibraryLinker{
145 libraryDecorator: library,
146 }
147 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700148
Colin Cross74d73e22017-08-02 11:05:49 -0700149 module.AddProperties(&prebuilt.properties)
150
151 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Jiyong Park379de2f2018-12-19 02:47:14 +0900152
153 // Prebuilt libraries can be included in APEXes
154 android.InitApexModule(module)
155
Leo Li74f7b972017-05-17 11:30:45 -0700156 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700157}
158
Patrice Arruda3554a982019-03-27 19:09:10 -0700159// cc_prebuilt_library_static installs a precompiled static library that are
160// listed in the srcs property in the device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700161func prebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700162 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
163 return module.Init()
164}
165
166func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
167 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700168 library.BuildOnlyStatic()
169 module.compiler = nil
170
171 prebuilt := &prebuiltLibraryLinker{
172 libraryDecorator: library,
173 }
174 module.linker = prebuilt
175
Colin Cross74d73e22017-08-02 11:05:49 -0700176 module.AddProperties(&prebuilt.properties)
177
178 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700179 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700180}
181
182type prebuiltBinaryLinker struct {
183 *binaryDecorator
184 prebuiltLinker
185}
186
187var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
188
Colin Crossde89fb82017-03-17 13:28:06 -0700189func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
190 flags Flags, deps PathDeps, objs Objects) android.Path {
191 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700192 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700193 builderFlags := flagsToBuilderFlags(flags)
194
195 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
196 in := p.Prebuilt.SingleSourcePath(ctx)
197
Colin Crossb60190a2018-09-04 16:28:17 -0700198 p.unstrippedOutputFile = in
199
Colin Cross88f6fef2018-09-05 14:20:03 -0700200 if p.needsStrip(ctx) {
201 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700202 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700203 in = stripped
204 }
Colin Cross94921e72017-08-08 16:20:15 -0700205
206 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700207 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700208 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700209 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700210 Description: "prebuilt",
211 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700212 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700213 })
214
215 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700216 }
217
218 return nil
219}
220
Patrice Arruda3554a982019-03-27 19:09:10 -0700221// cc_prebuilt_binary installs a precompiled executable in srcs property in the
222// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700223func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700224 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
225 return module.Init()
226}
227
228func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
229 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700230 module.compiler = nil
231
232 prebuilt := &prebuiltBinaryLinker{
233 binaryDecorator: binary,
234 }
235 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700236
Colin Cross74d73e22017-08-02 11:05:49 -0700237 module.AddProperties(&prebuilt.properties)
238
239 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700240 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700241}