blob: 2ef31950ee83a367cc7fa5f7cfb1db6036819ec9 [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() {
Paul Duffin59986b22019-12-19 14:38:36 +000022 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
23}
24
25func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000026 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000027 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
28 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000029 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000030 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070031}
32
33type prebuiltLinkerInterface interface {
34 Name(string) string
35 prebuilt() *android.Prebuilt
36}
37
Patrice Arruda3554a982019-03-27 19:09:10 -070038type prebuiltLinkerProperties struct {
39
40 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
41 Srcs []string `android:"path,arch_variant"`
42
43 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
44 // symbols, etc), default true.
45 Check_elf_files *bool
46}
47
Colin Crossde89fb82017-03-17 13:28:06 -070048type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070049 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080050
Patrice Arruda3554a982019-03-27 19:09:10 -070051 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070052}
53
Colin Crossde89fb82017-03-17 13:28:06 -070054func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070055 return &p.Prebuilt
56}
57
Colin Cross74d73e22017-08-02 11:05:49 -070058func (p *prebuiltLinker) PrebuiltSrcs() []string {
59 return p.properties.Srcs
60}
61
Colin Cross33b2fb72019-05-14 14:07:01 -070062type prebuiltLibraryInterface interface {
63 libraryInterface
64 prebuiltLinkerInterface
65 disablePrebuilt()
66}
67
Colin Crossde89fb82017-03-17 13:28:06 -070068type prebuiltLibraryLinker struct {
69 *libraryDecorator
70 prebuiltLinker
71}
72
73var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070074var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070075
Yi Kong00981662018-08-13 16:02:49 -070076func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
77
78func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080079 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070080}
81
82func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070083 return flags
Yi Kong00981662018-08-13 16:02:49 -070084}
85
86func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
87 return p.libraryDecorator.linkerProps()
88}
89
Colin Crossce75d2c2016-10-06 16:12:58 -070090func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070091 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +000092
93 p.libraryDecorator.exportIncludes(ctx)
94 p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
95 p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
96 p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
97 p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
98 p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
99
Colin Crossce75d2c2016-10-06 16:12:58 -0700100 // TODO(ccross): verify shared library dependencies
Paul Duffinbce90da2020-03-12 20:17:14 +0000101 srcs := p.prebuiltSrcs()
102 if len(srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700103 builderFlags := flagsToBuilderFlags(flags)
104
Paul Duffinbce90da2020-03-12 20:17:14 +0000105 if len(srcs) > 1 {
106 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
107 return nil
108 }
109
110 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700111
112 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700113 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700114 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Colin Cross88f6fef2018-09-05 14:20:03 -0700115 if p.needsStrip(ctx) {
116 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700117 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700118 in = stripped
119 }
120
Colin Crossb60190a2018-09-04 16:28:17 -0700121 // Optimize out relinking against shared libraries whose interface hasn't changed by
122 // depending on a table of contents file instead of the library itself.
123 tocFile := android.PathForModuleOut(ctx, libName+".toc")
124 p.tocFile = android.OptionalPathForPath(tocFile)
125 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700126 }
127
128 return in
Colin Crossce75d2c2016-10-06 16:12:58 -0700129 }
130
131 return nil
132}
133
Paul Duffinbce90da2020-03-12 20:17:14 +0000134func (p *prebuiltLibraryLinker) prebuiltSrcs() []string {
135 srcs := p.properties.Srcs
136 if p.static() {
137 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
138 }
139 if p.shared() {
140 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
141 }
142
143 return srcs
144}
145
Jiyong Park379de2f2018-12-19 02:47:14 +0900146func (p *prebuiltLibraryLinker) shared() bool {
147 return p.libraryDecorator.shared()
148}
149
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700150func (p *prebuiltLibraryLinker) nativeCoverage() bool {
151 return false
152}
153
Colin Cross33b2fb72019-05-14 14:07:01 -0700154func (p *prebuiltLibraryLinker) disablePrebuilt() {
155 p.properties.Srcs = nil
156}
157
Martin Stjernholmbf37d162020-03-31 16:05:34 +0100158func (p *prebuiltLibraryLinker) skipInstall(mod *Module) {
159 mod.ModuleBase.SkipInstall()
160}
161
Paul Duffinac6e6082019-12-11 15:22:32 +0000162func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700163 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700164 module.compiler = nil
165
166 prebuilt := &prebuiltLibraryLinker{
167 libraryDecorator: library,
168 }
169 module.linker = prebuilt
Martin Stjernholmbf37d162020-03-31 16:05:34 +0100170 module.installer = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700171
Colin Cross74d73e22017-08-02 11:05:49 -0700172 module.AddProperties(&prebuilt.properties)
173
Paul Duffinbce90da2020-03-12 20:17:14 +0000174 srcsSupplier := func() []string {
175 return prebuilt.prebuiltSrcs()
176 }
177
178 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Jiyong Park379de2f2018-12-19 02:47:14 +0900179
Paul Duffinac6e6082019-12-11 15:22:32 +0000180 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900181 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000182 return module, library
183}
184
Paul Duffinbce90da2020-03-12 20:17:14 +0000185// cc_prebuilt_library installs a precompiled shared library that are
186// listed in the srcs property in the device's directory.
187func PrebuiltLibraryFactory() android.Module {
188 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
189
190 // Prebuilt shared libraries can be included in APEXes
191 android.InitApexModule(module)
192
193 return module.Init()
194}
195
Paul Duffinac6e6082019-12-11 15:22:32 +0000196// cc_prebuilt_library_shared installs a precompiled shared library that are
197// listed in the srcs property in the device's directory.
198func PrebuiltSharedLibraryFactory() android.Module {
199 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
200 return module.Init()
201}
202
203func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
204 module, library := NewPrebuiltLibrary(hod)
205 library.BuildOnlyShared()
206
207 // Prebuilt shared libraries can be included in APEXes
208 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900209
Leo Li74f7b972017-05-17 11:30:45 -0700210 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700211}
212
Patrice Arruda3554a982019-03-27 19:09:10 -0700213// cc_prebuilt_library_static installs a precompiled static library that are
214// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900215func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700216 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
217 return module.Init()
218}
219
220func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000221 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700222 library.BuildOnlyStatic()
Leo Li74f7b972017-05-17 11:30:45 -0700223 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700224}
225
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000226type prebuiltObjectProperties struct {
227 Srcs []string `android:"path,arch_variant"`
228}
229
230type prebuiltObjectLinker struct {
231 android.Prebuilt
232 objectLinker
233
234 properties prebuiltObjectProperties
235}
236
237func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
238 return &p.Prebuilt
239}
240
241var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
242
243func (p *prebuiltObjectLinker) link(ctx ModuleContext,
244 flags Flags, deps PathDeps, objs Objects) android.Path {
245 if len(p.properties.Srcs) > 0 {
246 return p.Prebuilt.SingleSourcePath(ctx)
247 }
248 return nil
249}
250
251func newPrebuiltObject() *Module {
252 module := newObject()
253 prebuilt := &prebuiltObjectLinker{
254 objectLinker: objectLinker{
255 baseLinker: NewBaseLinker(nil),
256 },
257 }
258 module.linker = prebuilt
259 module.AddProperties(&prebuilt.properties)
260 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
261 android.InitSdkAwareModule(module)
262 return module
263}
264
265func prebuiltObjectFactory() android.Module {
266 module := newPrebuiltObject()
267 return module.Init()
268}
269
Colin Crossde89fb82017-03-17 13:28:06 -0700270type prebuiltBinaryLinker struct {
271 *binaryDecorator
272 prebuiltLinker
273}
274
275var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
276
Colin Crossde89fb82017-03-17 13:28:06 -0700277func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
278 flags Flags, deps PathDeps, objs Objects) android.Path {
279 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700280 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700281 builderFlags := flagsToBuilderFlags(flags)
282
283 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
284 in := p.Prebuilt.SingleSourcePath(ctx)
285
Colin Crossb60190a2018-09-04 16:28:17 -0700286 p.unstrippedOutputFile = in
287
Colin Cross88f6fef2018-09-05 14:20:03 -0700288 if p.needsStrip(ctx) {
289 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700290 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700291 in = stripped
292 }
Colin Cross94921e72017-08-08 16:20:15 -0700293
294 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700295 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700296 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700297 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700298 Description: "prebuilt",
299 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700300 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700301 })
302
303 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700304 }
305
306 return nil
307}
308
Patrice Arruda3554a982019-03-27 19:09:10 -0700309// cc_prebuilt_binary installs a precompiled executable in srcs property in the
310// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700311func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700312 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
313 return module.Init()
314}
315
316func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
317 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700318 module.compiler = nil
319
320 prebuilt := &prebuiltBinaryLinker{
321 binaryDecorator: binary,
322 }
323 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700324
Colin Cross74d73e22017-08-02 11:05:49 -0700325 module.AddProperties(&prebuilt.properties)
326
327 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700328 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700329}