blob: 3dbd3e34c48c1fff5c2a359eb52f5c35562ecce3 [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)
29 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070030}
31
32type prebuiltLinkerInterface interface {
33 Name(string) string
34 prebuilt() *android.Prebuilt
35}
36
Patrice Arruda3554a982019-03-27 19:09:10 -070037type prebuiltLinkerProperties struct {
38
39 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
40 Srcs []string `android:"path,arch_variant"`
41
42 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
43 // symbols, etc), default true.
44 Check_elf_files *bool
45}
46
Colin Crossde89fb82017-03-17 13:28:06 -070047type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070048 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080049
Patrice Arruda3554a982019-03-27 19:09:10 -070050 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070051}
52
Colin Crossde89fb82017-03-17 13:28:06 -070053func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070054 return &p.Prebuilt
55}
56
Colin Cross74d73e22017-08-02 11:05:49 -070057func (p *prebuiltLinker) PrebuiltSrcs() []string {
58 return p.properties.Srcs
59}
60
Colin Cross33b2fb72019-05-14 14:07:01 -070061type prebuiltLibraryInterface interface {
62 libraryInterface
63 prebuiltLinkerInterface
64 disablePrebuilt()
65}
66
Colin Crossde89fb82017-03-17 13:28:06 -070067type prebuiltLibraryLinker struct {
68 *libraryDecorator
69 prebuiltLinker
70}
71
72var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070073var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070074
Yi Kong00981662018-08-13 16:02:49 -070075func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
76
77func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080078 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070079}
80
81func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070082 return flags
Yi Kong00981662018-08-13 16:02:49 -070083}
84
85func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
86 return p.libraryDecorator.linkerProps()
87}
88
Colin Crossce75d2c2016-10-06 16:12:58 -070089func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070090 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffin853b8db2020-02-21 10:57:00 +000091
92 p.libraryDecorator.exportIncludes(ctx)
93 p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
94 p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
95 p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
96 p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
97 p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
98
Colin Crossce75d2c2016-10-06 16:12:58 -070099 // TODO(ccross): verify shared library dependencies
Paul Duffinb8a89a42020-03-12 20:17:14 +0000100 srcs := p.prebuiltSrcs()
101 if len(srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700102 builderFlags := flagsToBuilderFlags(flags)
103
Paul Duffinb8a89a42020-03-12 20:17:14 +0000104 if len(srcs) > 1 {
105 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
106 return nil
107 }
108
109 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700110
111 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700112 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700113 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Colin Cross88f6fef2018-09-05 14:20:03 -0700114 if p.needsStrip(ctx) {
115 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700116 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700117 in = stripped
118 }
119
Colin Crossb60190a2018-09-04 16:28:17 -0700120 // Optimize out relinking against shared libraries whose interface hasn't changed by
121 // depending on a table of contents file instead of the library itself.
122 tocFile := android.PathForModuleOut(ctx, libName+".toc")
123 p.tocFile = android.OptionalPathForPath(tocFile)
124 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700125 }
126
127 return in
Colin Crossce75d2c2016-10-06 16:12:58 -0700128 }
129
130 return nil
131}
132
Paul Duffinb8a89a42020-03-12 20:17:14 +0000133func (p *prebuiltLibraryLinker) prebuiltSrcs() []string {
134 srcs := p.properties.Srcs
135 if p.static() {
136 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
137 }
138 if p.shared() {
139 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
140 }
141
142 return srcs
143}
144
Jiyong Park379de2f2018-12-19 02:47:14 +0900145func (p *prebuiltLibraryLinker) shared() bool {
146 return p.libraryDecorator.shared()
147}
148
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700149func (p *prebuiltLibraryLinker) nativeCoverage() bool {
150 return false
151}
152
Colin Cross33b2fb72019-05-14 14:07:01 -0700153func (p *prebuiltLibraryLinker) disablePrebuilt() {
154 p.properties.Srcs = nil
155}
156
Martin Stjernholm2a6e9d02020-03-31 16:05:34 +0100157func (p *prebuiltLibraryLinker) skipInstall(mod *Module) {
158 mod.ModuleBase.SkipInstall()
159}
160
Paul Duffinac6e6082019-12-11 15:22:32 +0000161func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700162 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700163 module.compiler = nil
164
165 prebuilt := &prebuiltLibraryLinker{
166 libraryDecorator: library,
167 }
168 module.linker = prebuilt
Martin Stjernholm2a6e9d02020-03-31 16:05:34 +0100169 module.installer = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700170
Colin Cross74d73e22017-08-02 11:05:49 -0700171 module.AddProperties(&prebuilt.properties)
172
Paul Duffinb8a89a42020-03-12 20:17:14 +0000173 srcsSupplier := func() []string {
174 return prebuilt.prebuiltSrcs()
175 }
176
177 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Jiyong Park379de2f2018-12-19 02:47:14 +0900178
Paul Duffinac6e6082019-12-11 15:22:32 +0000179 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900180 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000181 return module, library
182}
183
Paul Duffinb8a89a42020-03-12 20:17:14 +0000184// cc_prebuilt_library installs a precompiled shared library that are
185// listed in the srcs property in the device's directory.
186func PrebuiltLibraryFactory() android.Module {
187 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
188
189 // Prebuilt shared libraries can be included in APEXes
190 android.InitApexModule(module)
191
192 return module.Init()
193}
194
Paul Duffinac6e6082019-12-11 15:22:32 +0000195// cc_prebuilt_library_shared installs a precompiled shared library that are
196// listed in the srcs property in the device's directory.
197func PrebuiltSharedLibraryFactory() android.Module {
198 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
199 return module.Init()
200}
201
202func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
203 module, library := NewPrebuiltLibrary(hod)
204 library.BuildOnlyShared()
205
206 // Prebuilt shared libraries can be included in APEXes
207 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900208
Leo Li74f7b972017-05-17 11:30:45 -0700209 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700210}
211
Patrice Arruda3554a982019-03-27 19:09:10 -0700212// cc_prebuilt_library_static installs a precompiled static library that are
213// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900214func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700215 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
216 return module.Init()
217}
218
219func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000220 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700221 library.BuildOnlyStatic()
Leo Li74f7b972017-05-17 11:30:45 -0700222 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700223}
224
225type prebuiltBinaryLinker struct {
226 *binaryDecorator
227 prebuiltLinker
228}
229
230var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
231
Colin Crossde89fb82017-03-17 13:28:06 -0700232func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
233 flags Flags, deps PathDeps, objs Objects) android.Path {
234 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700235 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700236 builderFlags := flagsToBuilderFlags(flags)
237
238 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
239 in := p.Prebuilt.SingleSourcePath(ctx)
240
Colin Crossb60190a2018-09-04 16:28:17 -0700241 p.unstrippedOutputFile = in
242
Colin Cross88f6fef2018-09-05 14:20:03 -0700243 if p.needsStrip(ctx) {
244 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700245 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700246 in = stripped
247 }
Colin Cross94921e72017-08-08 16:20:15 -0700248
249 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700250 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700251 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700252 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700253 Description: "prebuilt",
254 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700255 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700256 })
257
258 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700259 }
260
261 return nil
262}
263
Patrice Arruda3554a982019-03-27 19:09:10 -0700264// cc_prebuilt_binary installs a precompiled executable in srcs property in the
265// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700266func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700267 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
268 return module.Init()
269}
270
271func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
272 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700273 module.compiler = nil
274
275 prebuilt := &prebuiltBinaryLinker{
276 binaryDecorator: binary,
277 }
278 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700279
Colin Cross74d73e22017-08-02 11:05:49 -0700280 module.AddProperties(&prebuilt.properties)
281
282 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700283 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700284}