blob: 3af65d654f0bce787011610229d65a79b0173efd [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) {
Paul Duffinbce90da2020-03-12 20:17:14 +000026 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000027 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
28 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040029 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000030 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000031 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070032}
33
34type prebuiltLinkerInterface interface {
35 Name(string) string
36 prebuilt() *android.Prebuilt
37}
38
Patrice Arruda3554a982019-03-27 19:09:10 -070039type prebuiltLinkerProperties struct {
40
41 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
42 Srcs []string `android:"path,arch_variant"`
43
44 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
45 // symbols, etc), default true.
46 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080047
48 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
49 // This is needed only if this library is linked by other modules in build time.
50 // Only makes sense for the Windows target.
51 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070052}
53
Colin Crossde89fb82017-03-17 13:28:06 -070054type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070055 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080056
Patrice Arruda3554a982019-03-27 19:09:10 -070057 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070058}
59
Colin Crossde89fb82017-03-17 13:28:06 -070060func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070061 return &p.Prebuilt
62}
63
Colin Cross74d73e22017-08-02 11:05:49 -070064func (p *prebuiltLinker) PrebuiltSrcs() []string {
65 return p.properties.Srcs
66}
67
Colin Cross33b2fb72019-05-14 14:07:01 -070068type prebuiltLibraryInterface interface {
69 libraryInterface
70 prebuiltLinkerInterface
71 disablePrebuilt()
72}
73
Colin Crossde89fb82017-03-17 13:28:06 -070074type prebuiltLibraryLinker struct {
75 *libraryDecorator
76 prebuiltLinker
77}
78
79var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070080var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070081
Yi Kong00981662018-08-13 16:02:49 -070082func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
83
84func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080085 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070086}
87
88func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070089 return flags
Yi Kong00981662018-08-13 16:02:49 -070090}
91
92func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
93 return p.libraryDecorator.linkerProps()
94}
95
Colin Crossce75d2c2016-10-06 16:12:58 -070096func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070097 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +000098
99 p.libraryDecorator.exportIncludes(ctx)
100 p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
101 p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
102 p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
103 p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
104 p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
105
Colin Crossce75d2c2016-10-06 16:12:58 -0700106 // TODO(ccross): verify shared library dependencies
Paul Duffinbce90da2020-03-12 20:17:14 +0000107 srcs := p.prebuiltSrcs()
108 if len(srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700109 builderFlags := flagsToBuilderFlags(flags)
110
Paul Duffinbce90da2020-03-12 20:17:14 +0000111 if len(srcs) > 1 {
112 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
113 return nil
114 }
115
116 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700117
Yo Chianga3ad9b22020-03-18 14:19:07 +0800118 if p.static() {
119 return in
120 }
121
Colin Cross88f6fef2018-09-05 14:20:03 -0700122 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700123 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700124 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800125 outputFile := android.PathForModuleOut(ctx, libName)
126 var implicits android.Paths
127
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200128 if p.stripper.NeedsStrip(ctx) {
129 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700130 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200131 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700132 in = stripped
133 }
134
Colin Crossb60190a2018-09-04 16:28:17 -0700135 // Optimize out relinking against shared libraries whose interface hasn't changed by
136 // depending on a table of contents file instead of the library itself.
137 tocFile := android.PathForModuleOut(ctx, libName+".toc")
138 p.tocFile = android.OptionalPathForPath(tocFile)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800139 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700140
Yo Chianga3ad9b22020-03-18 14:19:07 +0800141 if ctx.Windows() && p.properties.Windows_import_lib != nil {
142 // Consumers of this library actually links to the import library in build
143 // time and dynamically links to the DLL in run time. i.e.
144 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
145 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
146 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
147 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
148 implicits = append(implicits, importLibOutputFile)
149
150 ctx.Build(pctx, android.BuildParams{
151 Rule: android.Cp,
152 Description: "prebuilt import library",
153 Input: importLibSrc,
154 Output: importLibOutputFile,
155 Args: map[string]string{
156 "cpFlags": "-L",
157 },
158 })
159 }
160
161 ctx.Build(pctx, android.BuildParams{
162 Rule: android.Cp,
163 Description: "prebuilt shared library",
164 Implicits: implicits,
165 Input: in,
166 Output: outputFile,
167 Args: map[string]string{
168 "cpFlags": "-L",
169 },
170 })
171
172 return outputFile
173 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700174 }
175
176 return nil
177}
178
Paul Duffinbce90da2020-03-12 20:17:14 +0000179func (p *prebuiltLibraryLinker) prebuiltSrcs() []string {
180 srcs := p.properties.Srcs
181 if p.static() {
182 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
183 }
184 if p.shared() {
185 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
186 }
187
188 return srcs
189}
190
Jiyong Park379de2f2018-12-19 02:47:14 +0900191func (p *prebuiltLibraryLinker) shared() bool {
192 return p.libraryDecorator.shared()
193}
194
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700195func (p *prebuiltLibraryLinker) nativeCoverage() bool {
196 return false
197}
198
Colin Cross33b2fb72019-05-14 14:07:01 -0700199func (p *prebuiltLibraryLinker) disablePrebuilt() {
200 p.properties.Srcs = nil
201}
202
Paul Duffinac6e6082019-12-11 15:22:32 +0000203func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700204 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700205 module.compiler = nil
206
207 prebuilt := &prebuiltLibraryLinker{
208 libraryDecorator: library,
209 }
210 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700211
Colin Cross74d73e22017-08-02 11:05:49 -0700212 module.AddProperties(&prebuilt.properties)
213
Paul Duffinbce90da2020-03-12 20:17:14 +0000214 srcsSupplier := func() []string {
215 return prebuilt.prebuiltSrcs()
216 }
217
218 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Jiyong Park379de2f2018-12-19 02:47:14 +0900219
Paul Duffinac6e6082019-12-11 15:22:32 +0000220 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900221 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000222 return module, library
223}
224
Paul Duffinbce90da2020-03-12 20:17:14 +0000225// cc_prebuilt_library installs a precompiled shared library that are
226// listed in the srcs property in the device's directory.
227func PrebuiltLibraryFactory() android.Module {
228 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
229
230 // Prebuilt shared libraries can be included in APEXes
231 android.InitApexModule(module)
232
233 return module.Init()
234}
235
Paul Duffinac6e6082019-12-11 15:22:32 +0000236// cc_prebuilt_library_shared installs a precompiled shared library that are
237// listed in the srcs property in the device's directory.
238func PrebuiltSharedLibraryFactory() android.Module {
239 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
240 return module.Init()
241}
242
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400243// cc_prebuilt_test_library_shared installs a precompiled shared library
244// to be used as a data dependency of a test-related module (such as cc_test, or
245// cc_test_library).
246func PrebuiltSharedTestLibraryFactory() android.Module {
247 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported)
248 library.BuildOnlyShared()
249 library.baseInstaller = NewTestInstaller()
250 return module.Init()
251}
252
Paul Duffinac6e6082019-12-11 15:22:32 +0000253func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
254 module, library := NewPrebuiltLibrary(hod)
255 library.BuildOnlyShared()
256
257 // Prebuilt shared libraries can be included in APEXes
258 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900259
Leo Li74f7b972017-05-17 11:30:45 -0700260 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700261}
262
Patrice Arruda3554a982019-03-27 19:09:10 -0700263// cc_prebuilt_library_static installs a precompiled static library that are
264// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900265func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700266 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
267 return module.Init()
268}
269
270func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000271 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700272 library.BuildOnlyStatic()
Leo Li74f7b972017-05-17 11:30:45 -0700273 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700274}
275
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000276type prebuiltObjectProperties struct {
277 Srcs []string `android:"path,arch_variant"`
278}
279
280type prebuiltObjectLinker struct {
281 android.Prebuilt
282 objectLinker
283
284 properties prebuiltObjectProperties
285}
286
287func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
288 return &p.Prebuilt
289}
290
291var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
292
293func (p *prebuiltObjectLinker) link(ctx ModuleContext,
294 flags Flags, deps PathDeps, objs Objects) android.Path {
295 if len(p.properties.Srcs) > 0 {
296 return p.Prebuilt.SingleSourcePath(ctx)
297 }
298 return nil
299}
300
Inseob Kim1042d292020-06-01 23:23:05 +0900301func (p *prebuiltObjectLinker) object() bool {
302 return true
303}
304
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000305func newPrebuiltObject() *Module {
306 module := newObject()
307 prebuilt := &prebuiltObjectLinker{
308 objectLinker: objectLinker{
309 baseLinker: NewBaseLinker(nil),
310 },
311 }
312 module.linker = prebuilt
313 module.AddProperties(&prebuilt.properties)
314 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
315 android.InitSdkAwareModule(module)
316 return module
317}
318
319func prebuiltObjectFactory() android.Module {
320 module := newPrebuiltObject()
321 return module.Init()
322}
323
Colin Crossde89fb82017-03-17 13:28:06 -0700324type prebuiltBinaryLinker struct {
325 *binaryDecorator
326 prebuiltLinker
327}
328
329var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
330
Colin Crossde89fb82017-03-17 13:28:06 -0700331func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
332 flags Flags, deps PathDeps, objs Objects) android.Path {
333 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700334 if len(p.properties.Srcs) > 0 {
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200335 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700336
337 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
338 in := p.Prebuilt.SingleSourcePath(ctx)
339
Colin Crossb60190a2018-09-04 16:28:17 -0700340 p.unstrippedOutputFile = in
341
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200342 if p.stripper.NeedsStrip(ctx) {
Colin Cross88f6fef2018-09-05 14:20:03 -0700343 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200344 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700345 in = stripped
346 }
Colin Cross94921e72017-08-08 16:20:15 -0700347
348 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700349 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700350 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700351 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700352 Description: "prebuilt",
353 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700354 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700355 })
356
357 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700358 }
359
360 return nil
361}
362
Inseob Kim7f283f42020-06-01 21:53:49 +0900363func (p *prebuiltBinaryLinker) binary() bool {
364 return true
365}
366
Patrice Arruda3554a982019-03-27 19:09:10 -0700367// cc_prebuilt_binary installs a precompiled executable in srcs property in the
368// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700369func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700370 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
371 return module.Init()
372}
373
374func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
375 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700376 module.compiler = nil
377
378 prebuilt := &prebuiltBinaryLinker{
379 binaryDecorator: binary,
380 }
381 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700382
Colin Cross74d73e22017-08-02 11:05:49 -0700383 module.AddProperties(&prebuilt.properties)
384
385 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700386 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700387}