• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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"
19)
20
21func init() {
22	android.RegisterModuleType("cc_prebuilt_library_shared", prebuiltSharedLibraryFactory)
23	android.RegisterModuleType("cc_prebuilt_library_static", prebuiltStaticLibraryFactory)
24	android.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
25}
26
27type prebuiltLinkerInterface interface {
28	Name(string) string
29	prebuilt() *android.Prebuilt
30}
31
32type 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
42type prebuiltLinker struct {
43	android.Prebuilt
44
45	properties prebuiltLinkerProperties
46}
47
48func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
49	return &p.Prebuilt
50}
51
52func (p *prebuiltLinker) PrebuiltSrcs() []string {
53	return p.properties.Srcs
54}
55
56type prebuiltLibraryInterface interface {
57	libraryInterface
58	prebuiltLinkerInterface
59	disablePrebuilt()
60}
61
62type prebuiltLibraryLinker struct {
63	*libraryDecorator
64	prebuiltLinker
65}
66
67var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
68var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
69
70func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
71
72func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
73	return p.libraryDecorator.linkerDeps(ctx, deps)
74}
75
76func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
77	return flags
78}
79
80func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
81	return p.libraryDecorator.linkerProps()
82}
83
84func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
85	flags Flags, deps PathDeps, objs Objects) android.Path {
86	// TODO(ccross): verify shared library dependencies
87	if len(p.properties.Srcs) > 0 {
88		p.libraryDecorator.exportIncludes(ctx, "-I")
89		p.libraryDecorator.reexportFlags(deps.ReexportedFlags)
90		p.libraryDecorator.reexportDeps(deps.ReexportedFlagsDeps)
91
92		builderFlags := flagsToBuilderFlags(flags)
93
94		in := p.Prebuilt.SingleSourcePath(ctx)
95
96		if p.shared() {
97			p.unstrippedOutputFile = in
98			libName := ctx.baseModuleName() + flags.Toolchain.ShlibSuffix()
99			if p.needsStrip(ctx) {
100				stripped := android.PathForModuleOut(ctx, "stripped", libName)
101				p.strip(ctx, in, stripped, builderFlags)
102				in = stripped
103			}
104
105			// Optimize out relinking against shared libraries whose interface hasn't changed by
106			// depending on a table of contents file instead of the library itself.
107			tocFile := android.PathForModuleOut(ctx, libName+".toc")
108			p.tocFile = android.OptionalPathForPath(tocFile)
109			TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
110		}
111
112		return in
113	}
114
115	return nil
116}
117
118func (p *prebuiltLibraryLinker) shared() bool {
119	return p.libraryDecorator.shared()
120}
121
122func (p *prebuiltLibraryLinker) nativeCoverage() bool {
123	return false
124}
125
126func (p *prebuiltLibraryLinker) disablePrebuilt() {
127	p.properties.Srcs = nil
128}
129
130// cc_prebuilt_library_shared installs a precompiled shared library that are
131// listed in the srcs property in the device's directory.
132func prebuiltSharedLibraryFactory() android.Module {
133	module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
134	return module.Init()
135}
136
137func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
138	module, library := NewLibrary(hod)
139	library.BuildOnlyShared()
140	module.compiler = nil
141
142	prebuilt := &prebuiltLibraryLinker{
143		libraryDecorator: library,
144	}
145	module.linker = prebuilt
146
147	module.AddProperties(&prebuilt.properties)
148
149	android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
150
151	// Prebuilt libraries can be included in APEXes
152	android.InitApexModule(module)
153
154	return module, library
155}
156
157// cc_prebuilt_library_static installs a precompiled static library that are
158// listed in the srcs property in the device's directory.
159func prebuiltStaticLibraryFactory() android.Module {
160	module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
161	return module.Init()
162}
163
164func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
165	module, library := NewLibrary(hod)
166	library.BuildOnlyStatic()
167	module.compiler = nil
168
169	prebuilt := &prebuiltLibraryLinker{
170		libraryDecorator: library,
171	}
172	module.linker = prebuilt
173
174	module.AddProperties(&prebuilt.properties)
175
176	android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
177	return module, library
178}
179
180type prebuiltBinaryLinker struct {
181	*binaryDecorator
182	prebuiltLinker
183}
184
185var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
186
187func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
188	flags Flags, deps PathDeps, objs Objects) android.Path {
189	// TODO(ccross): verify shared library dependencies
190	if len(p.properties.Srcs) > 0 {
191		builderFlags := flagsToBuilderFlags(flags)
192
193		fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
194		in := p.Prebuilt.SingleSourcePath(ctx)
195
196		p.unstrippedOutputFile = in
197
198		if p.needsStrip(ctx) {
199			stripped := android.PathForModuleOut(ctx, "stripped", fileName)
200			p.strip(ctx, in, stripped, builderFlags)
201			in = stripped
202		}
203
204		// Copy binaries to a name matching the final installed name
205		outputFile := android.PathForModuleOut(ctx, fileName)
206		ctx.Build(pctx, android.BuildParams{
207			Rule:        android.CpExecutable,
208			Description: "prebuilt",
209			Output:      outputFile,
210			Input:       in,
211		})
212
213		return outputFile
214	}
215
216	return nil
217}
218
219// cc_prebuilt_binary installs a precompiled executable in srcs property in the
220// device's directory.
221func prebuiltBinaryFactory() android.Module {
222	module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
223	return module.Init()
224}
225
226func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
227	module, binary := NewBinary(hod)
228	module.compiler = nil
229
230	prebuilt := &prebuiltBinaryLinker{
231		binaryDecorator: binary,
232	}
233	module.linker = prebuilt
234
235	module.AddProperties(&prebuilt.properties)
236
237	android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
238	return module, binary
239}
240