blob: 4e6cdd7555955647f4d123df886114c14a398335 [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() {
Jooyung Han344d5432019-08-23 11:17:39 +090022 android.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
23 android.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Colin Crossde89fb82017-03-17 13:28:06 -070024 android.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070025}
26
27type prebuiltLinkerInterface interface {
28 Name(string) string
29 prebuilt() *android.Prebuilt
30}
31
Patrice Arruda3554a982019-03-27 19:09:10 -070032type prebuiltLinkerProperties struct {
33
34 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
35 Srcs []string `android:"path,arch_variant"`
36
37 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
38 // symbols, etc), default true.
39 Check_elf_files *bool
40}
41
Colin Crossde89fb82017-03-17 13:28:06 -070042type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070043 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080044
Patrice Arruda3554a982019-03-27 19:09:10 -070045 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070046}
47
Colin Crossde89fb82017-03-17 13:28:06 -070048func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070049 return &p.Prebuilt
50}
51
Colin Cross74d73e22017-08-02 11:05:49 -070052func (p *prebuiltLinker) PrebuiltSrcs() []string {
53 return p.properties.Srcs
54}
55
Colin Cross33b2fb72019-05-14 14:07:01 -070056type prebuiltLibraryInterface interface {
57 libraryInterface
58 prebuiltLinkerInterface
59 disablePrebuilt()
60}
61
Colin Crossde89fb82017-03-17 13:28:06 -070062type prebuiltLibraryLinker struct {
63 *libraryDecorator
64 prebuiltLinker
65}
66
67var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070068var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070069
Yi Kong00981662018-08-13 16:02:49 -070070func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
71
72func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080073 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070074}
75
76func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070077 return flags
Yi Kong00981662018-08-13 16:02:49 -070078}
79
80func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
81 return p.libraryDecorator.linkerProps()
82}
83
Colin Crossce75d2c2016-10-06 16:12:58 -070084func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070085 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossce75d2c2016-10-06 16:12:58 -070086 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -070087 if len(p.properties.Srcs) > 0 {
Inseob Kim69378442019-06-03 19:10:47 +090088 p.libraryDecorator.exportIncludes(ctx)
89 p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
90 p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
91 p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
92 p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
Colin Cross88f6fef2018-09-05 14:20:03 -070093
94 builderFlags := flagsToBuilderFlags(flags)
95
96 in := p.Prebuilt.SingleSourcePath(ctx)
97
98 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -070099 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700100 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Colin Cross88f6fef2018-09-05 14:20:03 -0700101 if p.needsStrip(ctx) {
102 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700103 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700104 in = stripped
105 }
106
Colin Crossb60190a2018-09-04 16:28:17 -0700107 // Optimize out relinking against shared libraries whose interface hasn't changed by
108 // depending on a table of contents file instead of the library itself.
109 tocFile := android.PathForModuleOut(ctx, libName+".toc")
110 p.tocFile = android.OptionalPathForPath(tocFile)
111 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700112 }
113
114 return in
Colin Crossce75d2c2016-10-06 16:12:58 -0700115 }
116
117 return nil
118}
119
Jiyong Park379de2f2018-12-19 02:47:14 +0900120func (p *prebuiltLibraryLinker) shared() bool {
121 return p.libraryDecorator.shared()
122}
123
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700124func (p *prebuiltLibraryLinker) nativeCoverage() bool {
125 return false
126}
127
Colin Cross33b2fb72019-05-14 14:07:01 -0700128func (p *prebuiltLibraryLinker) disablePrebuilt() {
129 p.properties.Srcs = nil
130}
131
Patrice Arruda3554a982019-03-27 19:09:10 -0700132// cc_prebuilt_library_shared installs a precompiled shared library that are
133// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900134func PrebuiltSharedLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700135 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
136 return module.Init()
137}
138
139func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
140 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700141 library.BuildOnlyShared()
Colin Crossce75d2c2016-10-06 16:12:58 -0700142 module.compiler = nil
143
144 prebuilt := &prebuiltLibraryLinker{
145 libraryDecorator: library,
146 }
147 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700148
Colin Cross74d73e22017-08-02 11:05:49 -0700149 module.AddProperties(&prebuilt.properties)
150
151 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Jiyong Park379de2f2018-12-19 02:47:14 +0900152
153 // Prebuilt libraries can be included in APEXes
154 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900155 android.InitSdkAwareModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900156
Leo Li74f7b972017-05-17 11:30:45 -0700157 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700158}
159
Patrice Arruda3554a982019-03-27 19:09:10 -0700160// cc_prebuilt_library_static installs a precompiled static library that are
161// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900162func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700163 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
164 return module.Init()
165}
166
167func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
168 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700169 library.BuildOnlyStatic()
170 module.compiler = nil
171
172 prebuilt := &prebuiltLibraryLinker{
173 libraryDecorator: library,
174 }
175 module.linker = prebuilt
176
Colin Cross74d73e22017-08-02 11:05:49 -0700177 module.AddProperties(&prebuilt.properties)
178
179 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900180 android.InitSdkAwareModule(module)
Leo Li74f7b972017-05-17 11:30:45 -0700181 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700182}
183
184type prebuiltBinaryLinker struct {
185 *binaryDecorator
186 prebuiltLinker
187}
188
189var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
190
Colin Crossde89fb82017-03-17 13:28:06 -0700191func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
192 flags Flags, deps PathDeps, objs Objects) android.Path {
193 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700194 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700195 builderFlags := flagsToBuilderFlags(flags)
196
197 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
198 in := p.Prebuilt.SingleSourcePath(ctx)
199
Colin Crossb60190a2018-09-04 16:28:17 -0700200 p.unstrippedOutputFile = in
201
Colin Cross88f6fef2018-09-05 14:20:03 -0700202 if p.needsStrip(ctx) {
203 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700204 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700205 in = stripped
206 }
Colin Cross94921e72017-08-08 16:20:15 -0700207
208 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700209 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700210 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700211 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700212 Description: "prebuilt",
213 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700214 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700215 })
216
217 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700218 }
219
220 return nil
221}
222
Patrice Arruda3554a982019-03-27 19:09:10 -0700223// cc_prebuilt_binary installs a precompiled executable in srcs property in the
224// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700225func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700226 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
227 return module.Init()
228}
229
230func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
231 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700232 module.compiler = nil
233
234 prebuilt := &prebuiltBinaryLinker{
235 binaryDecorator: binary,
236 }
237 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700238
Colin Cross74d73e22017-08-02 11:05:49 -0700239 module.AddProperties(&prebuilt.properties)
240
241 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700242 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700243}