blob: 32676d6f59ffa253c176be8156291a303bb1d3e5 [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...)
Inseob Kimd110f872019-12-06 13:15:38 +090093 p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
Colin Cross88f6fef2018-09-05 14:20:03 -070094
95 builderFlags := flagsToBuilderFlags(flags)
96
97 in := p.Prebuilt.SingleSourcePath(ctx)
98
99 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700100 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700101 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Colin Cross88f6fef2018-09-05 14:20:03 -0700102 if p.needsStrip(ctx) {
103 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700104 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700105 in = stripped
106 }
107
Colin Crossb60190a2018-09-04 16:28:17 -0700108 // Optimize out relinking against shared libraries whose interface hasn't changed by
109 // depending on a table of contents file instead of the library itself.
110 tocFile := android.PathForModuleOut(ctx, libName+".toc")
111 p.tocFile = android.OptionalPathForPath(tocFile)
112 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700113 }
114
115 return in
Colin Crossce75d2c2016-10-06 16:12:58 -0700116 }
117
118 return nil
119}
120
Jiyong Park379de2f2018-12-19 02:47:14 +0900121func (p *prebuiltLibraryLinker) shared() bool {
122 return p.libraryDecorator.shared()
123}
124
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700125func (p *prebuiltLibraryLinker) nativeCoverage() bool {
126 return false
127}
128
Colin Cross33b2fb72019-05-14 14:07:01 -0700129func (p *prebuiltLibraryLinker) disablePrebuilt() {
130 p.properties.Srcs = nil
131}
132
Patrice Arruda3554a982019-03-27 19:09:10 -0700133// cc_prebuilt_library_shared installs a precompiled shared library that are
134// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900135func PrebuiltSharedLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700136 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
137 return module.Init()
138}
139
140func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
141 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700142 library.BuildOnlyShared()
Colin Crossce75d2c2016-10-06 16:12:58 -0700143 module.compiler = nil
144
145 prebuilt := &prebuiltLibraryLinker{
146 libraryDecorator: library,
147 }
148 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700149
Colin Cross74d73e22017-08-02 11:05:49 -0700150 module.AddProperties(&prebuilt.properties)
151
152 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Jiyong Park379de2f2018-12-19 02:47:14 +0900153
154 // Prebuilt libraries can be included in APEXes
155 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900156 android.InitSdkAwareModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900157
Leo Li74f7b972017-05-17 11:30:45 -0700158 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700159}
160
Patrice Arruda3554a982019-03-27 19:09:10 -0700161// cc_prebuilt_library_static installs a precompiled static library that are
162// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900163func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700164 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
165 return module.Init()
166}
167
168func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
169 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700170 library.BuildOnlyStatic()
171 module.compiler = nil
172
173 prebuilt := &prebuiltLibraryLinker{
174 libraryDecorator: library,
175 }
176 module.linker = prebuilt
177
Colin Cross74d73e22017-08-02 11:05:49 -0700178 module.AddProperties(&prebuilt.properties)
179
180 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900181 android.InitSdkAwareModule(module)
Leo Li74f7b972017-05-17 11:30:45 -0700182 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700183}
184
185type prebuiltBinaryLinker struct {
186 *binaryDecorator
187 prebuiltLinker
188}
189
190var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
191
Colin Crossde89fb82017-03-17 13:28:06 -0700192func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
193 flags Flags, deps PathDeps, objs Objects) android.Path {
194 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700195 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700196 builderFlags := flagsToBuilderFlags(flags)
197
198 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
199 in := p.Prebuilt.SingleSourcePath(ctx)
200
Colin Crossb60190a2018-09-04 16:28:17 -0700201 p.unstrippedOutputFile = in
202
Colin Cross88f6fef2018-09-05 14:20:03 -0700203 if p.needsStrip(ctx) {
204 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700205 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700206 in = stripped
207 }
Colin Cross94921e72017-08-08 16:20:15 -0700208
209 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700210 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700211 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700212 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700213 Description: "prebuilt",
214 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700215 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700216 })
217
218 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700219 }
220
221 return nil
222}
223
Patrice Arruda3554a982019-03-27 19:09:10 -0700224// cc_prebuilt_binary installs a precompiled executable in srcs property in the
225// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700226func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700227 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
228 return module.Init()
229}
230
231func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
232 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700233 module.compiler = nil
234
235 prebuilt := &prebuiltBinaryLinker{
236 binaryDecorator: binary,
237 }
238 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700239
Colin Cross74d73e22017-08-02 11:05:49 -0700240 module.AddProperties(&prebuilt.properties)
241
242 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700243 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700244}