blob: 7f21721ccaf80dbf14f7985e7d208eb6e14c83c0 [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)
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 Duffinf5ea9e12020-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 Duffinbce90da2020-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 Duffinbce90da2020-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 Duffinbce90da2020-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
Paul Duffinac6e6082019-12-11 15:22:32 +0000157func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700158 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700159 module.compiler = nil
160
161 prebuilt := &prebuiltLibraryLinker{
162 libraryDecorator: library,
163 }
164 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700165
Colin Cross74d73e22017-08-02 11:05:49 -0700166 module.AddProperties(&prebuilt.properties)
167
Paul Duffinbce90da2020-03-12 20:17:14 +0000168 srcsSupplier := func() []string {
169 return prebuilt.prebuiltSrcs()
170 }
171
172 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Jiyong Park379de2f2018-12-19 02:47:14 +0900173
Paul Duffinac6e6082019-12-11 15:22:32 +0000174 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900175 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000176 return module, library
177}
178
Paul Duffinbce90da2020-03-12 20:17:14 +0000179// cc_prebuilt_library installs a precompiled shared library that are
180// listed in the srcs property in the device's directory.
181func PrebuiltLibraryFactory() android.Module {
182 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
183
184 // Prebuilt shared libraries can be included in APEXes
185 android.InitApexModule(module)
186
187 return module.Init()
188}
189
Paul Duffinac6e6082019-12-11 15:22:32 +0000190// cc_prebuilt_library_shared installs a precompiled shared library that are
191// listed in the srcs property in the device's directory.
192func PrebuiltSharedLibraryFactory() android.Module {
193 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
194 return module.Init()
195}
196
197func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
198 module, library := NewPrebuiltLibrary(hod)
199 library.BuildOnlyShared()
200
201 // Prebuilt shared libraries can be included in APEXes
202 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900203
Leo Li74f7b972017-05-17 11:30:45 -0700204 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700205}
206
Patrice Arruda3554a982019-03-27 19:09:10 -0700207// cc_prebuilt_library_static installs a precompiled static library that are
208// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900209func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700210 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
211 return module.Init()
212}
213
214func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000215 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700216 library.BuildOnlyStatic()
Leo Li74f7b972017-05-17 11:30:45 -0700217 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700218}
219
220type prebuiltBinaryLinker struct {
221 *binaryDecorator
222 prebuiltLinker
223}
224
225var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
226
Colin Crossde89fb82017-03-17 13:28:06 -0700227func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
228 flags Flags, deps PathDeps, objs Objects) android.Path {
229 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700230 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700231 builderFlags := flagsToBuilderFlags(flags)
232
233 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
234 in := p.Prebuilt.SingleSourcePath(ctx)
235
Colin Crossb60190a2018-09-04 16:28:17 -0700236 p.unstrippedOutputFile = in
237
Colin Cross88f6fef2018-09-05 14:20:03 -0700238 if p.needsStrip(ctx) {
239 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700240 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700241 in = stripped
242 }
Colin Cross94921e72017-08-08 16:20:15 -0700243
244 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700245 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700246 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700247 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700248 Description: "prebuilt",
249 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700250 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700251 })
252
253 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700254 }
255
256 return nil
257}
258
Patrice Arruda3554a982019-03-27 19:09:10 -0700259// cc_prebuilt_binary installs a precompiled executable in srcs property in the
260// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700261func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700262 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
263 return module.Init()
264}
265
266func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
267 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700268 module.compiler = nil
269
270 prebuilt := &prebuiltBinaryLinker{
271 binaryDecorator: binary,
272 }
273 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700274
Colin Cross74d73e22017-08-02 11:05:49 -0700275 module.AddProperties(&prebuilt.properties)
276
277 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700278 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700279}