blob: fc9cc17d37bf028223b259b67324e7edafed5bbc [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
Paul Duffinac6e6082019-12-11 15:22:32 +0000158func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700159 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700160 module.compiler = nil
161
162 prebuilt := &prebuiltLibraryLinker{
163 libraryDecorator: library,
164 }
165 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700166
Colin Cross74d73e22017-08-02 11:05:49 -0700167 module.AddProperties(&prebuilt.properties)
168
Paul Duffinbce90da2020-03-12 20:17:14 +0000169 srcsSupplier := func() []string {
170 return prebuilt.prebuiltSrcs()
171 }
172
173 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Jiyong Park379de2f2018-12-19 02:47:14 +0900174
Paul Duffinac6e6082019-12-11 15:22:32 +0000175 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900176 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000177 return module, library
178}
179
Paul Duffinbce90da2020-03-12 20:17:14 +0000180// cc_prebuilt_library installs a precompiled shared library that are
181// listed in the srcs property in the device's directory.
182func PrebuiltLibraryFactory() android.Module {
183 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
184
185 // Prebuilt shared libraries can be included in APEXes
186 android.InitApexModule(module)
187
188 return module.Init()
189}
190
Paul Duffinac6e6082019-12-11 15:22:32 +0000191// cc_prebuilt_library_shared installs a precompiled shared library that are
192// listed in the srcs property in the device's directory.
193func PrebuiltSharedLibraryFactory() android.Module {
194 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
195 return module.Init()
196}
197
198func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
199 module, library := NewPrebuiltLibrary(hod)
200 library.BuildOnlyShared()
201
202 // Prebuilt shared libraries can be included in APEXes
203 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900204
Leo Li74f7b972017-05-17 11:30:45 -0700205 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700206}
207
Patrice Arruda3554a982019-03-27 19:09:10 -0700208// cc_prebuilt_library_static installs a precompiled static library that are
209// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900210func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700211 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
212 return module.Init()
213}
214
215func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000216 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700217 library.BuildOnlyStatic()
Leo Li74f7b972017-05-17 11:30:45 -0700218 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700219}
220
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000221type prebuiltObjectProperties struct {
222 Srcs []string `android:"path,arch_variant"`
223}
224
225type prebuiltObjectLinker struct {
226 android.Prebuilt
227 objectLinker
228
229 properties prebuiltObjectProperties
230}
231
232func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
233 return &p.Prebuilt
234}
235
236var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
237
238func (p *prebuiltObjectLinker) link(ctx ModuleContext,
239 flags Flags, deps PathDeps, objs Objects) android.Path {
240 if len(p.properties.Srcs) > 0 {
241 return p.Prebuilt.SingleSourcePath(ctx)
242 }
243 return nil
244}
245
246func newPrebuiltObject() *Module {
247 module := newObject()
248 prebuilt := &prebuiltObjectLinker{
249 objectLinker: objectLinker{
250 baseLinker: NewBaseLinker(nil),
251 },
252 }
253 module.linker = prebuilt
254 module.AddProperties(&prebuilt.properties)
255 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
256 android.InitSdkAwareModule(module)
257 return module
258}
259
260func prebuiltObjectFactory() android.Module {
261 module := newPrebuiltObject()
262 return module.Init()
263}
264
Colin Crossde89fb82017-03-17 13:28:06 -0700265type prebuiltBinaryLinker struct {
266 *binaryDecorator
267 prebuiltLinker
268}
269
270var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
271
Colin Crossde89fb82017-03-17 13:28:06 -0700272func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
273 flags Flags, deps PathDeps, objs Objects) android.Path {
274 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700275 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700276 builderFlags := flagsToBuilderFlags(flags)
277
278 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
279 in := p.Prebuilt.SingleSourcePath(ctx)
280
Colin Crossb60190a2018-09-04 16:28:17 -0700281 p.unstrippedOutputFile = in
282
Colin Cross88f6fef2018-09-05 14:20:03 -0700283 if p.needsStrip(ctx) {
284 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700285 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700286 in = stripped
287 }
Colin Cross94921e72017-08-08 16:20:15 -0700288
289 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700290 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700291 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700292 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700293 Description: "prebuilt",
294 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700295 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700296 })
297
298 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700299 }
300
301 return nil
302}
303
Patrice Arruda3554a982019-03-27 19:09:10 -0700304// cc_prebuilt_binary installs a precompiled executable in srcs property in the
305// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700306func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700307 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
308 return module.Init()
309}
310
311func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
312 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700313 module.compiler = nil
314
315 prebuilt := &prebuiltBinaryLinker{
316 binaryDecorator: binary,
317 }
318 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700319
Colin Cross74d73e22017-08-02 11:05:49 -0700320 module.AddProperties(&prebuilt.properties)
321
322 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700323 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700324}