blob: 4f77d41c8f25824a3e6e453cd6f9cb3515a3734d [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) {
26 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
27 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
28 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070029}
30
31type prebuiltLinkerInterface interface {
32 Name(string) string
33 prebuilt() *android.Prebuilt
34}
35
Patrice Arruda3554a982019-03-27 19:09:10 -070036type prebuiltLinkerProperties struct {
37
38 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
39 Srcs []string `android:"path,arch_variant"`
40
41 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
42 // symbols, etc), default true.
43 Check_elf_files *bool
44}
45
Colin Crossde89fb82017-03-17 13:28:06 -070046type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070047 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080048
Patrice Arruda3554a982019-03-27 19:09:10 -070049 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070050}
51
Colin Crossde89fb82017-03-17 13:28:06 -070052func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070053 return &p.Prebuilt
54}
55
Colin Cross74d73e22017-08-02 11:05:49 -070056func (p *prebuiltLinker) PrebuiltSrcs() []string {
57 return p.properties.Srcs
58}
59
Colin Cross33b2fb72019-05-14 14:07:01 -070060type prebuiltLibraryInterface interface {
61 libraryInterface
62 prebuiltLinkerInterface
63 disablePrebuilt()
64}
65
Colin Crossde89fb82017-03-17 13:28:06 -070066type prebuiltLibraryLinker struct {
67 *libraryDecorator
68 prebuiltLinker
69}
70
71var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070072var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070073
Yi Kong00981662018-08-13 16:02:49 -070074func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
75
76func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080077 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070078}
79
80func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070081 return flags
Yi Kong00981662018-08-13 16:02:49 -070082}
83
84func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
85 return p.libraryDecorator.linkerProps()
86}
87
Colin Crossce75d2c2016-10-06 16:12:58 -070088func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070089 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossce75d2c2016-10-06 16:12:58 -070090 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -070091 if len(p.properties.Srcs) > 0 {
Inseob Kim69378442019-06-03 19:10:47 +090092 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...)
Inseob Kimd110f872019-12-06 13:15:38 +090097 p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
Colin Cross88f6fef2018-09-05 14:20:03 -070098
99 builderFlags := flagsToBuilderFlags(flags)
100
101 in := p.Prebuilt.SingleSourcePath(ctx)
102
103 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700104 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700105 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Colin Cross88f6fef2018-09-05 14:20:03 -0700106 if p.needsStrip(ctx) {
107 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700108 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700109 in = stripped
110 }
111
Colin Crossb60190a2018-09-04 16:28:17 -0700112 // Optimize out relinking against shared libraries whose interface hasn't changed by
113 // depending on a table of contents file instead of the library itself.
114 tocFile := android.PathForModuleOut(ctx, libName+".toc")
115 p.tocFile = android.OptionalPathForPath(tocFile)
116 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700117 }
118
119 return in
Colin Crossce75d2c2016-10-06 16:12:58 -0700120 }
121
122 return nil
123}
124
Jiyong Park379de2f2018-12-19 02:47:14 +0900125func (p *prebuiltLibraryLinker) shared() bool {
126 return p.libraryDecorator.shared()
127}
128
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700129func (p *prebuiltLibraryLinker) nativeCoverage() bool {
130 return false
131}
132
Colin Cross33b2fb72019-05-14 14:07:01 -0700133func (p *prebuiltLibraryLinker) disablePrebuilt() {
134 p.properties.Srcs = nil
135}
136
Martin Stjernholm2a6e9d02020-03-31 16:05:34 +0100137func (p *prebuiltLibraryLinker) skipInstall(mod *Module) {
138 mod.ModuleBase.SkipInstall()
139}
140
Paul Duffinac6e6082019-12-11 15:22:32 +0000141func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700142 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700143 module.compiler = nil
144
145 prebuilt := &prebuiltLibraryLinker{
146 libraryDecorator: library,
147 }
148 module.linker = prebuilt
Martin Stjernholm2a6e9d02020-03-31 16:05:34 +0100149 module.installer = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700150
Colin Cross74d73e22017-08-02 11:05:49 -0700151 module.AddProperties(&prebuilt.properties)
152
153 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Jiyong Park379de2f2018-12-19 02:47:14 +0900154
Paul Duffinac6e6082019-12-11 15:22:32 +0000155 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900156 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000157 return module, library
158}
159
160// cc_prebuilt_library_shared installs a precompiled shared library that are
161// listed in the srcs property in the device's directory.
162func PrebuiltSharedLibraryFactory() android.Module {
163 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
164 return module.Init()
165}
166
167func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
168 module, library := NewPrebuiltLibrary(hod)
169 library.BuildOnlyShared()
170
171 // Prebuilt shared libraries can be included in APEXes
172 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900173
Leo Li74f7b972017-05-17 11:30:45 -0700174 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700175}
176
Patrice Arruda3554a982019-03-27 19:09:10 -0700177// cc_prebuilt_library_static installs a precompiled static library that are
178// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900179func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700180 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
181 return module.Init()
182}
183
184func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000185 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700186 library.BuildOnlyStatic()
Leo Li74f7b972017-05-17 11:30:45 -0700187 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700188}
189
190type prebuiltBinaryLinker struct {
191 *binaryDecorator
192 prebuiltLinker
193}
194
195var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
196
Colin Crossde89fb82017-03-17 13:28:06 -0700197func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
198 flags Flags, deps PathDeps, objs Objects) android.Path {
199 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700200 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700201 builderFlags := flagsToBuilderFlags(flags)
202
203 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
204 in := p.Prebuilt.SingleSourcePath(ctx)
205
Colin Crossb60190a2018-09-04 16:28:17 -0700206 p.unstrippedOutputFile = in
207
Colin Cross88f6fef2018-09-05 14:20:03 -0700208 if p.needsStrip(ctx) {
209 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700210 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700211 in = stripped
212 }
Colin Cross94921e72017-08-08 16:20:15 -0700213
214 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700215 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700216 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700217 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700218 Description: "prebuilt",
219 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700220 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700221 })
222
223 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700224 }
225
226 return nil
227}
228
Patrice Arruda3554a982019-03-27 19:09:10 -0700229// cc_prebuilt_binary installs a precompiled executable in srcs property in the
230// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700231func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700232 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
233 return module.Init()
234}
235
236func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
237 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700238 module.compiler = nil
239
240 prebuilt := &prebuiltBinaryLinker{
241 binaryDecorator: binary,
242 }
243 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700244
Colin Cross74d73e22017-08-02 11:05:49 -0700245 module.AddProperties(&prebuilt.properties)
246
247 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700248 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700249}