blob: c303fda99b8cdd5e1ccd125e69c7ff2027e008bc [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 (
Martin Stjernholm14ee8322020-09-21 21:45:49 +010018 "path/filepath"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000019
20 "android/soong/android"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -040021 "android/soong/bazel"
Colin Crossce75d2c2016-10-06 16:12:58 -070022)
23
24func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000025 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
26}
27
28func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000029 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000030 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
31 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040032 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000033 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000034 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -040035
36 android.RegisterBp2BuildMutator("cc_prebuilt_library_shared", PrebuiltLibrarySharedBp2Build)
Colin Crossce75d2c2016-10-06 16:12:58 -070037}
38
39type prebuiltLinkerInterface interface {
40 Name(string) string
41 prebuilt() *android.Prebuilt
42}
43
Patrice Arruda3554a982019-03-27 19:09:10 -070044type prebuiltLinkerProperties struct {
Patrice Arruda3554a982019-03-27 19:09:10 -070045 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
46 Srcs []string `android:"path,arch_variant"`
47
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070048 Sanitized Sanitized `android:"arch_variant"`
49
Patrice Arruda3554a982019-03-27 19:09:10 -070050 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
51 // symbols, etc), default true.
52 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080053
54 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
55 // This is needed only if this library is linked by other modules in build time.
56 // Only makes sense for the Windows target.
57 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070058}
59
Colin Crossde89fb82017-03-17 13:28:06 -070060type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070061 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080062
Patrice Arruda3554a982019-03-27 19:09:10 -070063 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070064}
65
Colin Crossde89fb82017-03-17 13:28:06 -070066func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070067 return &p.Prebuilt
68}
69
Colin Cross74d73e22017-08-02 11:05:49 -070070func (p *prebuiltLinker) PrebuiltSrcs() []string {
71 return p.properties.Srcs
72}
73
Colin Cross33b2fb72019-05-14 14:07:01 -070074type prebuiltLibraryInterface interface {
75 libraryInterface
76 prebuiltLinkerInterface
77 disablePrebuilt()
78}
79
Colin Crossde89fb82017-03-17 13:28:06 -070080type prebuiltLibraryLinker struct {
81 *libraryDecorator
82 prebuiltLinker
83}
84
85var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070086var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070087
Yi Kong00981662018-08-13 16:02:49 -070088func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
89
90func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080091 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070092}
93
94func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070095 return flags
Yi Kong00981662018-08-13 16:02:49 -070096}
97
98func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
99 return p.libraryDecorator.linkerProps()
100}
101
Colin Crossce75d2c2016-10-06 16:12:58 -0700102func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700103 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000104
Colin Cross0de8a1e2020-09-18 14:15:30 -0700105 p.libraryDecorator.flagExporter.exportIncludes(ctx)
106 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
107 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
108 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
109 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
110 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
111
112 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000113
Colin Crossce75d2c2016-10-06 16:12:58 -0700114 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700115 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000116 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000117 if len(srcs) > 1 {
118 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
119 return nil
120 }
121
Jiyong Park892a98f2020-12-14 09:20:00 +0900122 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
123
Paul Duffinbce90da2020-03-12 20:17:14 +0000124 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700125
Yo Chianga3ad9b22020-03-18 14:19:07 +0800126 if p.static() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700127 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
128 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
129 StaticLibrary: in,
130
131 TransitiveStaticLibrariesForOrdering: depSet,
132 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800133 return in
134 }
135
Colin Cross88f6fef2018-09-05 14:20:03 -0700136 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700137 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700138 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800139 outputFile := android.PathForModuleOut(ctx, libName)
140 var implicits android.Paths
141
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200142 if p.stripper.NeedsStrip(ctx) {
143 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700144 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200145 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700146 in = stripped
147 }
148
Colin Crossb60190a2018-09-04 16:28:17 -0700149 // Optimize out relinking against shared libraries whose interface hasn't changed by
150 // depending on a table of contents file instead of the library itself.
151 tocFile := android.PathForModuleOut(ctx, libName+".toc")
152 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400153 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700154
Yo Chianga3ad9b22020-03-18 14:19:07 +0800155 if ctx.Windows() && p.properties.Windows_import_lib != nil {
156 // Consumers of this library actually links to the import library in build
157 // time and dynamically links to the DLL in run time. i.e.
158 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
159 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
160 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
161 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
162 implicits = append(implicits, importLibOutputFile)
163
164 ctx.Build(pctx, android.BuildParams{
165 Rule: android.Cp,
166 Description: "prebuilt import library",
167 Input: importLibSrc,
168 Output: importLibOutputFile,
169 Args: map[string]string{
170 "cpFlags": "-L",
171 },
172 })
173 }
174
175 ctx.Build(pctx, android.BuildParams{
176 Rule: android.Cp,
177 Description: "prebuilt shared library",
178 Implicits: implicits,
179 Input: in,
180 Output: outputFile,
181 Args: map[string]string{
182 "cpFlags": "-L",
183 },
184 })
185
Colin Cross0de8a1e2020-09-18 14:15:30 -0700186 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400187 SharedLibrary: outputFile,
188 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700189
190 TableOfContents: p.tocFile,
191 })
192
Yo Chianga3ad9b22020-03-18 14:19:07 +0800193 return outputFile
194 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700195 }
196
Colin Cross649d8172020-12-10 12:30:21 -0800197 if p.header() {
198 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
199
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000200 // Need to return an output path so that the AndroidMk logic doesn't skip
201 // the prebuilt header. For compatibility, in case Android.mk files use a
202 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
203 // placeholder, just like non-prebuilt header modules do in linkStatic().
204 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
205 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
206 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800207 }
208
Colin Crossce75d2c2016-10-06 16:12:58 -0700209 return nil
210}
211
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700212func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
213 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000214 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700215 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000216 if p.static() {
217 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700218 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000219 }
220 if p.shared() {
221 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700222 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000223 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000224 return srcs
225}
226
Jiyong Park379de2f2018-12-19 02:47:14 +0900227func (p *prebuiltLibraryLinker) shared() bool {
228 return p.libraryDecorator.shared()
229}
230
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700231func (p *prebuiltLibraryLinker) nativeCoverage() bool {
232 return false
233}
234
Colin Cross33b2fb72019-05-14 14:07:01 -0700235func (p *prebuiltLibraryLinker) disablePrebuilt() {
236 p.properties.Srcs = nil
237}
238
Jiyong Park892a98f2020-12-14 09:20:00 +0900239// Implements versionedInterface
240func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100241 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900242}
243
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000244func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700245 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700246 module.compiler = nil
247
248 prebuilt := &prebuiltLibraryLinker{
249 libraryDecorator: library,
250 }
251 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700252 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700253
Colin Cross74d73e22017-08-02 11:05:49 -0700254 module.AddProperties(&prebuilt.properties)
255
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000256 if srcsProperty == "" {
257 android.InitPrebuiltModuleWithoutSrcs(module)
258 } else {
259 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
260 return prebuilt.prebuiltSrcs(ctx)
261 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000262
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000263 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
264 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900265
Paul Duffinac6e6082019-12-11 15:22:32 +0000266 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900267 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000268 return module, library
269}
270
Paul Duffinbce90da2020-03-12 20:17:14 +0000271// cc_prebuilt_library installs a precompiled shared library that are
272// listed in the srcs property in the device's directory.
273func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000274 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000275
276 // Prebuilt shared libraries can be included in APEXes
277 android.InitApexModule(module)
278
279 return module.Init()
280}
281
Paul Duffinac6e6082019-12-11 15:22:32 +0000282// cc_prebuilt_library_shared installs a precompiled shared library that are
283// listed in the srcs property in the device's directory.
284func PrebuiltSharedLibraryFactory() android.Module {
285 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
286 return module.Init()
287}
288
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400289// cc_prebuilt_test_library_shared installs a precompiled shared library
290// to be used as a data dependency of a test-related module (such as cc_test, or
291// cc_test_library).
292func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000293 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400294 library.BuildOnlyShared()
295 library.baseInstaller = NewTestInstaller()
296 return module.Init()
297}
298
Paul Duffinac6e6082019-12-11 15:22:32 +0000299func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000300 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000301 library.BuildOnlyShared()
302
303 // Prebuilt shared libraries can be included in APEXes
304 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900305
Leo Li74f7b972017-05-17 11:30:45 -0700306 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700307}
308
Patrice Arruda3554a982019-03-27 19:09:10 -0700309// cc_prebuilt_library_static installs a precompiled static library that are
310// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900311func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700312 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
313 return module.Init()
314}
315
316func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000317 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700318 library.BuildOnlyStatic()
Liz Kammer3f9e1552021-04-02 18:47:09 -0400319 module.bazelHandler = &prebuiltStaticLibraryBazelHandler{module: module, library: library}
Leo Li74f7b972017-05-17 11:30:45 -0700320 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700321}
322
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400323type bazelPrebuiltLibrarySharedAttributes struct {
324 Shared_library bazel.LabelAttribute
325}
326
327func PrebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext) {
328 module, ok := ctx.Module().(*Module)
329 if !ok {
330 // Not a cc module
331 return
332 }
333 if !module.ConvertWithBp2build(ctx) {
334 return
335 }
336 if ctx.ModuleType() != "cc_prebuilt_library_shared" {
337 return
338 }
339
340 prebuiltLibrarySharedBp2BuildInternal(ctx, module)
341}
342
343func prebuiltLibrarySharedBp2BuildInternal(ctx android.TopDownMutatorContext, module *Module) {
344 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module)
345
346 attrs := &bazelPrebuiltLibrarySharedAttributes{
347 Shared_library: prebuiltAttrs.Src,
348 }
349
350 props := bazel.BazelTargetModuleProperties{
351 Rule_class: "prebuilt_library_shared",
352 Bzl_load_location: "//build/bazel/rules:prebuilt_library_shared.bzl",
353 }
354
355 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
356 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs)
357}
358
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000359type prebuiltObjectProperties struct {
360 Srcs []string `android:"path,arch_variant"`
361}
362
363type prebuiltObjectLinker struct {
364 android.Prebuilt
365 objectLinker
366
367 properties prebuiltObjectProperties
368}
369
Liz Kammer3f9e1552021-04-02 18:47:09 -0400370type prebuiltStaticLibraryBazelHandler struct {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000371 android.BazelHandler
Liz Kammer3f9e1552021-04-02 18:47:09 -0400372
373 module *Module
374 library *libraryDecorator
375}
376
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000377func (h *prebuiltStaticLibraryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400378 bazelCtx := ctx.Config().BazelContext
Chris Parsons787fb362021-10-14 18:43:51 -0400379 ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400380 if err != nil {
381 ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err)
382 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400383 if !ok {
384 return false
385 }
Liz Kammerfe23bf32021-04-09 16:17:05 -0400386 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400387 if len(staticLibs) > 1 {
388 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
389 return false
390 }
391
392 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
393
394 // TODO(eakammer):Add stub-related flags if this library is a stub library.
395 // h.library.exportVersioningMacroIfNeeded(ctx)
396
397 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
398 // validation will fail. For now, set this to an empty list.
399 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
400 h.library.collectedSnapshotHeaders = android.Paths{}
401
402 if len(staticLibs) == 0 {
403 h.module.outputFile = android.OptionalPath{}
404 return true
405 }
406
407 out := android.PathForBazelOut(ctx, staticLibs[0])
408 h.module.outputFile = android.OptionalPathForPath(out)
409
410 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build()
411 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
412 StaticLibrary: out,
413
414 TransitiveStaticLibrariesForOrdering: depSet,
415 })
416
417 return true
418}
419
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000420func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
421 return &p.Prebuilt
422}
423
424var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
425
426func (p *prebuiltObjectLinker) link(ctx ModuleContext,
427 flags Flags, deps PathDeps, objs Objects) android.Path {
428 if len(p.properties.Srcs) > 0 {
429 return p.Prebuilt.SingleSourcePath(ctx)
430 }
431 return nil
432}
433
Inseob Kim1042d292020-06-01 23:23:05 +0900434func (p *prebuiltObjectLinker) object() bool {
435 return true
436}
437
Colin Cross7cabd422021-06-25 14:21:04 -0700438func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
439 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000440 prebuilt := &prebuiltObjectLinker{
441 objectLinker: objectLinker{
442 baseLinker: NewBaseLinker(nil),
443 },
444 }
445 module.linker = prebuilt
446 module.AddProperties(&prebuilt.properties)
447 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
448 android.InitSdkAwareModule(module)
449 return module
450}
451
452func prebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700453 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000454 return module.Init()
455}
456
Colin Crossde89fb82017-03-17 13:28:06 -0700457type prebuiltBinaryLinker struct {
458 *binaryDecorator
459 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100460
461 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700462}
463
464var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
465
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100466func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
467 return p.toolPath
468}
469
Colin Crossde89fb82017-03-17 13:28:06 -0700470func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
471 flags Flags, deps PathDeps, objs Objects) android.Path {
472 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700473 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700474 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
475 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100476 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700477 p.unstrippedOutputFile = in
478
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100479 if ctx.Host() {
480 // Host binaries are symlinked to their prebuilt source locations. That
481 // way they are executed directly from there so the linker resolves their
482 // shared library dependencies relative to that location (using
483 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
484 // repository can supply the expected versions of the shared libraries
485 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700486
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100487 // These shared lib paths may point to copies of the libs in
488 // .intermediates, which isn't where the binary will load them from, but
489 // it's fine for dependency tracking. If a library dependency is updated,
490 // the symlink will get a new timestamp, along with any installed symlinks
491 // handled in make.
492 sharedLibPaths := deps.EarlySharedLibs
493 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
494 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
495
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100496 var fromPath = in.String()
497 if !filepath.IsAbs(fromPath) {
498 fromPath = "$$PWD/" + fromPath
499 }
500
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100501 ctx.Build(pctx, android.BuildParams{
502 Rule: android.Symlink,
503 Output: outputFile,
504 Input: in,
505 Implicits: sharedLibPaths,
506 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100507 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100508 },
509 })
510
511 p.toolPath = android.OptionalPathForPath(outputFile)
512 } else {
513 if p.stripper.NeedsStrip(ctx) {
514 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
515 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
516 in = stripped
517 }
518
519 // Copy binaries to a name matching the final installed name
520 ctx.Build(pctx, android.BuildParams{
521 Rule: android.CpExecutable,
522 Description: "prebuilt",
523 Output: outputFile,
524 Input: in,
525 })
526 }
Colin Cross94921e72017-08-08 16:20:15 -0700527
528 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700529 }
530
531 return nil
532}
533
Inseob Kim7f283f42020-06-01 21:53:49 +0900534func (p *prebuiltBinaryLinker) binary() bool {
535 return true
536}
537
Patrice Arruda3554a982019-03-27 19:09:10 -0700538// cc_prebuilt_binary installs a precompiled executable in srcs property in the
539// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700540func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700541 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
542 return module.Init()
543}
544
545func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
546 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700547 module.compiler = nil
548
549 prebuilt := &prebuiltBinaryLinker{
550 binaryDecorator: binary,
551 }
552 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100553 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700554
Colin Cross74d73e22017-08-02 11:05:49 -0700555 module.AddProperties(&prebuilt.properties)
556
557 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700558 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700559}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700560
561type Sanitized struct {
562 None struct {
563 Srcs []string `android:"path,arch_variant"`
564 } `android:"arch_variant"`
565 Address struct {
566 Srcs []string `android:"path,arch_variant"`
567 } `android:"arch_variant"`
568 Hwaddress struct {
569 Srcs []string `android:"path,arch_variant"`
570 } `android:"arch_variant"`
571}
572
573func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
574 if sanitize == nil {
575 return nil
576 }
577 if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil {
578 return sanitized.Address.Srcs
579 }
580 if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil {
581 return sanitized.Hwaddress.Srcs
582 }
583 return sanitized.None.Srcs
584}