blob: 62f39382417fe874b267f78ac09f08d07d85fdc5 [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"
Jiyong Park892a98f2020-12-14 09:20:00 +090019 "strings"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000020
21 "android/soong/android"
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)
Colin Crossce75d2c2016-10-06 16:12:58 -070035}
36
37type prebuiltLinkerInterface interface {
38 Name(string) string
39 prebuilt() *android.Prebuilt
40}
41
Patrice Arruda3554a982019-03-27 19:09:10 -070042type prebuiltLinkerProperties struct {
Patrice Arruda3554a982019-03-27 19:09:10 -070043 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
44 Srcs []string `android:"path,arch_variant"`
45
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070046 Sanitized Sanitized `android:"arch_variant"`
47
Patrice Arruda3554a982019-03-27 19:09:10 -070048 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
49 // symbols, etc), default true.
50 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080051
52 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
53 // This is needed only if this library is linked by other modules in build time.
54 // Only makes sense for the Windows target.
55 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070056}
57
Colin Crossde89fb82017-03-17 13:28:06 -070058type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070059 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080060
Patrice Arruda3554a982019-03-27 19:09:10 -070061 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070062}
63
Colin Crossde89fb82017-03-17 13:28:06 -070064func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070065 return &p.Prebuilt
66}
67
Colin Cross74d73e22017-08-02 11:05:49 -070068func (p *prebuiltLinker) PrebuiltSrcs() []string {
69 return p.properties.Srcs
70}
71
Colin Cross33b2fb72019-05-14 14:07:01 -070072type prebuiltLibraryInterface interface {
73 libraryInterface
74 prebuiltLinkerInterface
75 disablePrebuilt()
76}
77
Colin Crossde89fb82017-03-17 13:28:06 -070078type prebuiltLibraryLinker struct {
79 *libraryDecorator
80 prebuiltLinker
81}
82
83var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070084var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070085
Yi Kong00981662018-08-13 16:02:49 -070086func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
87
88func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080089 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070090}
91
92func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070093 return flags
Yi Kong00981662018-08-13 16:02:49 -070094}
95
96func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
97 return p.libraryDecorator.linkerProps()
98}
99
Colin Crossce75d2c2016-10-06 16:12:58 -0700100func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700101 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000102
Colin Cross0de8a1e2020-09-18 14:15:30 -0700103 p.libraryDecorator.flagExporter.exportIncludes(ctx)
104 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
105 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
106 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
107 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
108 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
109
110 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000111
Colin Crossce75d2c2016-10-06 16:12:58 -0700112 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700113 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000114 if len(srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700115 builderFlags := flagsToBuilderFlags(flags)
116
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)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500153 transformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
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
200 return nil
201 }
202
Colin Crossce75d2c2016-10-06 16:12:58 -0700203 return nil
204}
205
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700206func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
207 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000208 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700209 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000210 if p.static() {
211 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700212 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000213 }
214 if p.shared() {
215 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700216 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000217 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000218 return srcs
219}
220
Jiyong Park379de2f2018-12-19 02:47:14 +0900221func (p *prebuiltLibraryLinker) shared() bool {
222 return p.libraryDecorator.shared()
223}
224
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700225func (p *prebuiltLibraryLinker) nativeCoverage() bool {
226 return false
227}
228
Colin Cross33b2fb72019-05-14 14:07:01 -0700229func (p *prebuiltLibraryLinker) disablePrebuilt() {
230 p.properties.Srcs = nil
231}
232
Jiyong Park892a98f2020-12-14 09:20:00 +0900233// Implements versionedInterface
234func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
235 return strings.TrimPrefix(name, "prebuilt_")
236}
237
Paul Duffinac6e6082019-12-11 15:22:32 +0000238func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700239 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700240 module.compiler = nil
241
242 prebuilt := &prebuiltLibraryLinker{
243 libraryDecorator: library,
244 }
245 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700246 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700247
Colin Cross74d73e22017-08-02 11:05:49 -0700248 module.AddProperties(&prebuilt.properties)
249
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000250 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700251 return prebuilt.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000252 }
253
254 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Jiyong Park379de2f2018-12-19 02:47:14 +0900255
Paul Duffinac6e6082019-12-11 15:22:32 +0000256 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900257 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000258 return module, library
259}
260
Paul Duffinbce90da2020-03-12 20:17:14 +0000261// cc_prebuilt_library installs a precompiled shared library that are
262// listed in the srcs property in the device's directory.
263func PrebuiltLibraryFactory() android.Module {
264 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
265
266 // Prebuilt shared libraries can be included in APEXes
267 android.InitApexModule(module)
268
269 return module.Init()
270}
271
Paul Duffinac6e6082019-12-11 15:22:32 +0000272// cc_prebuilt_library_shared installs a precompiled shared library that are
273// listed in the srcs property in the device's directory.
274func PrebuiltSharedLibraryFactory() android.Module {
275 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
276 return module.Init()
277}
278
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400279// cc_prebuilt_test_library_shared installs a precompiled shared library
280// to be used as a data dependency of a test-related module (such as cc_test, or
281// cc_test_library).
282func PrebuiltSharedTestLibraryFactory() android.Module {
283 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported)
284 library.BuildOnlyShared()
285 library.baseInstaller = NewTestInstaller()
286 return module.Init()
287}
288
Paul Duffinac6e6082019-12-11 15:22:32 +0000289func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
290 module, library := NewPrebuiltLibrary(hod)
291 library.BuildOnlyShared()
292
293 // Prebuilt shared libraries can be included in APEXes
294 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900295
Leo Li74f7b972017-05-17 11:30:45 -0700296 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700297}
298
Patrice Arruda3554a982019-03-27 19:09:10 -0700299// cc_prebuilt_library_static installs a precompiled static library that are
300// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900301func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700302 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
303 return module.Init()
304}
305
306func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000307 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700308 library.BuildOnlyStatic()
Liz Kammer3f9e1552021-04-02 18:47:09 -0400309 module.bazelHandler = &prebuiltStaticLibraryBazelHandler{module: module, library: library}
Leo Li74f7b972017-05-17 11:30:45 -0700310 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700311}
312
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000313type prebuiltObjectProperties struct {
314 Srcs []string `android:"path,arch_variant"`
315}
316
317type prebuiltObjectLinker struct {
318 android.Prebuilt
319 objectLinker
320
321 properties prebuiltObjectProperties
322}
323
Liz Kammer3f9e1552021-04-02 18:47:09 -0400324type prebuiltStaticLibraryBazelHandler struct {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000325 android.BazelHandler
Liz Kammer3f9e1552021-04-02 18:47:09 -0400326
327 module *Module
328 library *libraryDecorator
329}
330
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000331func (h *prebuiltStaticLibraryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400332 bazelCtx := ctx.Config().BazelContext
Chris Parsons787fb362021-10-14 18:43:51 -0400333 ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400334 if err != nil {
335 ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err)
336 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400337 if !ok {
338 return false
339 }
Liz Kammerfe23bf32021-04-09 16:17:05 -0400340 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400341 if len(staticLibs) > 1 {
342 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
343 return false
344 }
345
346 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
347
348 // TODO(eakammer):Add stub-related flags if this library is a stub library.
349 // h.library.exportVersioningMacroIfNeeded(ctx)
350
351 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
352 // validation will fail. For now, set this to an empty list.
353 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
354 h.library.collectedSnapshotHeaders = android.Paths{}
355
356 if len(staticLibs) == 0 {
357 h.module.outputFile = android.OptionalPath{}
358 return true
359 }
360
361 out := android.PathForBazelOut(ctx, staticLibs[0])
362 h.module.outputFile = android.OptionalPathForPath(out)
363
364 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build()
365 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
366 StaticLibrary: out,
367
368 TransitiveStaticLibrariesForOrdering: depSet,
369 })
370
371 return true
372}
373
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000374func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
375 return &p.Prebuilt
376}
377
378var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
379
380func (p *prebuiltObjectLinker) link(ctx ModuleContext,
381 flags Flags, deps PathDeps, objs Objects) android.Path {
382 if len(p.properties.Srcs) > 0 {
383 return p.Prebuilt.SingleSourcePath(ctx)
384 }
385 return nil
386}
387
Inseob Kim1042d292020-06-01 23:23:05 +0900388func (p *prebuiltObjectLinker) object() bool {
389 return true
390}
391
Colin Cross7cabd422021-06-25 14:21:04 -0700392func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
393 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000394 prebuilt := &prebuiltObjectLinker{
395 objectLinker: objectLinker{
396 baseLinker: NewBaseLinker(nil),
397 },
398 }
399 module.linker = prebuilt
400 module.AddProperties(&prebuilt.properties)
401 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
402 android.InitSdkAwareModule(module)
403 return module
404}
405
406func prebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700407 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000408 return module.Init()
409}
410
Colin Crossde89fb82017-03-17 13:28:06 -0700411type prebuiltBinaryLinker struct {
412 *binaryDecorator
413 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100414
415 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700416}
417
418var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
419
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100420func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
421 return p.toolPath
422}
423
Colin Crossde89fb82017-03-17 13:28:06 -0700424func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
425 flags Flags, deps PathDeps, objs Objects) android.Path {
426 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700427 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700428 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
429 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100430 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700431 p.unstrippedOutputFile = in
432
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100433 if ctx.Host() {
434 // Host binaries are symlinked to their prebuilt source locations. That
435 // way they are executed directly from there so the linker resolves their
436 // shared library dependencies relative to that location (using
437 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
438 // repository can supply the expected versions of the shared libraries
439 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700440
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100441 // These shared lib paths may point to copies of the libs in
442 // .intermediates, which isn't where the binary will load them from, but
443 // it's fine for dependency tracking. If a library dependency is updated,
444 // the symlink will get a new timestamp, along with any installed symlinks
445 // handled in make.
446 sharedLibPaths := deps.EarlySharedLibs
447 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
448 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
449
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100450 var fromPath = in.String()
451 if !filepath.IsAbs(fromPath) {
452 fromPath = "$$PWD/" + fromPath
453 }
454
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100455 ctx.Build(pctx, android.BuildParams{
456 Rule: android.Symlink,
457 Output: outputFile,
458 Input: in,
459 Implicits: sharedLibPaths,
460 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100461 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100462 },
463 })
464
465 p.toolPath = android.OptionalPathForPath(outputFile)
466 } else {
467 if p.stripper.NeedsStrip(ctx) {
468 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
469 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
470 in = stripped
471 }
472
473 // Copy binaries to a name matching the final installed name
474 ctx.Build(pctx, android.BuildParams{
475 Rule: android.CpExecutable,
476 Description: "prebuilt",
477 Output: outputFile,
478 Input: in,
479 })
480 }
Colin Cross94921e72017-08-08 16:20:15 -0700481
482 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700483 }
484
485 return nil
486}
487
Inseob Kim7f283f42020-06-01 21:53:49 +0900488func (p *prebuiltBinaryLinker) binary() bool {
489 return true
490}
491
Patrice Arruda3554a982019-03-27 19:09:10 -0700492// cc_prebuilt_binary installs a precompiled executable in srcs property in the
493// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700494func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700495 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
496 return module.Init()
497}
498
499func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
500 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700501 module.compiler = nil
502
503 prebuilt := &prebuiltBinaryLinker{
504 binaryDecorator: binary,
505 }
506 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100507 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700508
Colin Cross74d73e22017-08-02 11:05:49 -0700509 module.AddProperties(&prebuilt.properties)
510
511 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700512 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700513}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700514
515type Sanitized struct {
516 None struct {
517 Srcs []string `android:"path,arch_variant"`
518 } `android:"arch_variant"`
519 Address struct {
520 Srcs []string `android:"path,arch_variant"`
521 } `android:"arch_variant"`
522 Hwaddress struct {
523 Srcs []string `android:"path,arch_variant"`
524 } `android:"arch_variant"`
525}
526
527func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
528 if sanitize == nil {
529 return nil
530 }
531 if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil {
532 return sanitized.Address.Srcs
533 }
534 if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil {
535 return sanitized.Hwaddress.Srcs
536 }
537 return sanitized.None.Srcs
538}