blob: ffeeb697b55d19741e53b93e698ffa814cefa018 [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() {
Colin Crossdfee1bc2017-03-17 14:03:18 -070022 android.RegisterModuleType("cc_prebuilt_library_shared", prebuiltSharedLibraryFactory)
Colin Crossde89fb82017-03-17 13:28:06 -070023 android.RegisterModuleType("cc_prebuilt_library_static", prebuiltStaticLibraryFactory)
24 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
Colin Crossde89fb82017-03-17 13:28:06 -070032type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070033 android.Prebuilt
Colin Cross74d73e22017-08-02 11:05:49 -070034 properties struct {
35 Srcs []string `android:"arch_variant"`
36 }
Colin Crossce75d2c2016-10-06 16:12:58 -070037}
38
Colin Crossde89fb82017-03-17 13:28:06 -070039func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070040 return &p.Prebuilt
41}
42
Colin Cross74d73e22017-08-02 11:05:49 -070043func (p *prebuiltLinker) PrebuiltSrcs() []string {
44 return p.properties.Srcs
45}
46
Colin Crossde89fb82017-03-17 13:28:06 -070047type prebuiltLibraryLinker struct {
48 *libraryDecorator
49 prebuiltLinker
50}
51
52var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
53
Yi Kong00981662018-08-13 16:02:49 -070054func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
55
56func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
57 // export_header_lib_headers needs to be passed along
58 return Deps{
59 HeaderLibs: p.baseLinker.Properties.Header_libs,
60 ReexportHeaderLibHeaders: p.baseLinker.Properties.Export_header_lib_headers,
61 }
62}
63
64func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070065 return flags
Yi Kong00981662018-08-13 16:02:49 -070066}
67
68func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
69 return p.libraryDecorator.linkerProps()
70}
71
Colin Crossce75d2c2016-10-06 16:12:58 -070072func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070073 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossce75d2c2016-10-06 16:12:58 -070074 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -070075 if len(p.properties.Srcs) > 0 {
Colin Crossce75d2c2016-10-06 16:12:58 -070076 p.libraryDecorator.exportIncludes(ctx, "-I")
77 p.libraryDecorator.reexportFlags(deps.ReexportedFlags)
78 p.libraryDecorator.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross88f6fef2018-09-05 14:20:03 -070079
80 builderFlags := flagsToBuilderFlags(flags)
81
82 in := p.Prebuilt.SingleSourcePath(ctx)
83
84 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -070085 p.unstrippedOutputFile = in
Colin Cross88f6fef2018-09-05 14:20:03 -070086 libName := ctx.baseModuleName() + flags.Toolchain.ShlibSuffix()
87 if p.needsStrip(ctx) {
88 stripped := android.PathForModuleOut(ctx, "stripped", libName)
89 p.strip(ctx, in, stripped, builderFlags)
90 in = stripped
91 }
92
Colin Crossb60190a2018-09-04 16:28:17 -070093 // Optimize out relinking against shared libraries whose interface hasn't changed by
94 // depending on a table of contents file instead of the library itself.
95 tocFile := android.PathForModuleOut(ctx, libName+".toc")
96 p.tocFile = android.OptionalPathForPath(tocFile)
97 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -070098 }
99
100 return in
Colin Crossce75d2c2016-10-06 16:12:58 -0700101 }
102
103 return nil
104}
105
Jiyong Park379de2f2018-12-19 02:47:14 +0900106func (p *prebuiltLibraryLinker) shared() bool {
107 return p.libraryDecorator.shared()
108}
109
Colin Cross36242852017-06-23 15:06:31 -0700110func prebuiltSharedLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700111 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
112 return module.Init()
113}
114
115func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
116 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700117 library.BuildOnlyShared()
Colin Crossce75d2c2016-10-06 16:12:58 -0700118 module.compiler = nil
119
120 prebuilt := &prebuiltLibraryLinker{
121 libraryDecorator: library,
122 }
123 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700124
Colin Cross74d73e22017-08-02 11:05:49 -0700125 module.AddProperties(&prebuilt.properties)
126
127 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Jiyong Park379de2f2018-12-19 02:47:14 +0900128
129 // Prebuilt libraries can be included in APEXes
130 android.InitApexModule(module)
131
Leo Li74f7b972017-05-17 11:30:45 -0700132 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700133}
134
Colin Cross36242852017-06-23 15:06:31 -0700135func prebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700136 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
137 return module.Init()
138}
139
140func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
141 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700142 library.BuildOnlyStatic()
143 module.compiler = nil
144
145 prebuilt := &prebuiltLibraryLinker{
146 libraryDecorator: library,
147 }
148 module.linker = prebuilt
149
Colin Cross74d73e22017-08-02 11:05:49 -0700150 module.AddProperties(&prebuilt.properties)
151
152 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700153 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700154}
155
156type prebuiltBinaryLinker struct {
157 *binaryDecorator
158 prebuiltLinker
159}
160
161var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
162
Colin Crossde89fb82017-03-17 13:28:06 -0700163func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
164 flags Flags, deps PathDeps, objs Objects) android.Path {
165 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700166 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700167 builderFlags := flagsToBuilderFlags(flags)
168
169 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
170 in := p.Prebuilt.SingleSourcePath(ctx)
171
Colin Crossb60190a2018-09-04 16:28:17 -0700172 p.unstrippedOutputFile = in
173
Colin Cross88f6fef2018-09-05 14:20:03 -0700174 if p.needsStrip(ctx) {
175 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
176 p.strip(ctx, in, stripped, builderFlags)
177 in = stripped
178 }
Colin Cross94921e72017-08-08 16:20:15 -0700179
180 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700181 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700182 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700183 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700184 Description: "prebuilt",
185 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700186 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700187 })
188
189 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700190 }
191
192 return nil
193}
194
Colin Cross36242852017-06-23 15:06:31 -0700195func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700196 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
197 return module.Init()
198}
199
200func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
201 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700202 module.compiler = nil
203
204 prebuilt := &prebuiltBinaryLinker{
205 binaryDecorator: binary,
206 }
207 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700208
Colin Cross74d73e22017-08-02 11:05:49 -0700209 module.AddProperties(&prebuilt.properties)
210
211 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700212 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700213}