blob: b7c0bf2fdaacd7e09746c42dab1e6f0b0ca2e635 [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 Duffinb8a89a42020-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 Stjernholm39978522020-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 Duffin853b8db2020-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 Duffinb8a89a42020-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 Duffinb8a89a42020-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 Duffinb8a89a42020-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 Stjernholm2a6e9d02020-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 Stjernholm2a6e9d02020-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 Duffinb8a89a42020-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 Duffinb8a89a42020-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 Stjernholm39978522020-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
Inseob Kim502679e2020-06-01 23:23:05 +0900251func (p *prebuiltObjectLinker) object() bool {
252 return true
253}
254
Martin Stjernholm39978522020-03-11 21:45:49 +0000255func newPrebuiltObject() *Module {
256 module := newObject()
257 prebuilt := &prebuiltObjectLinker{
258 objectLinker: objectLinker{
259 baseLinker: NewBaseLinker(nil),
260 },
261 }
262 module.linker = prebuilt
263 module.AddProperties(&prebuilt.properties)
264 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
265 android.InitSdkAwareModule(module)
266 return module
267}
268
269func prebuiltObjectFactory() android.Module {
270 module := newPrebuiltObject()
271 return module.Init()
272}
273
Colin Crossde89fb82017-03-17 13:28:06 -0700274type prebuiltBinaryLinker struct {
275 *binaryDecorator
276 prebuiltLinker
277}
278
279var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
280
Colin Crossde89fb82017-03-17 13:28:06 -0700281func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
282 flags Flags, deps PathDeps, objs Objects) android.Path {
283 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700284 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700285 builderFlags := flagsToBuilderFlags(flags)
286
287 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
288 in := p.Prebuilt.SingleSourcePath(ctx)
289
Colin Crossb60190a2018-09-04 16:28:17 -0700290 p.unstrippedOutputFile = in
291
Colin Cross88f6fef2018-09-05 14:20:03 -0700292 if p.needsStrip(ctx) {
293 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700294 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700295 in = stripped
296 }
Colin Cross94921e72017-08-08 16:20:15 -0700297
298 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700299 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700300 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700301 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700302 Description: "prebuilt",
303 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700304 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700305 })
306
307 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700308 }
309
310 return nil
311}
312
Inseob Kim4d8d8fe2020-06-01 21:53:49 +0900313func (p *prebuiltBinaryLinker) binary() bool {
314 return true
315}
316
Patrice Arruda3554a982019-03-27 19:09:10 -0700317// cc_prebuilt_binary installs a precompiled executable in srcs property in the
318// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700319func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700320 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
321 return module.Init()
322}
323
324func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
325 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700326 module.compiler = nil
327
328 prebuilt := &prebuiltBinaryLinker{
329 binaryDecorator: binary,
330 }
331 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700332
Colin Cross74d73e22017-08-02 11:05:49 -0700333 module.AddProperties(&prebuilt.properties)
334
335 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700336 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700337}