blob: 3829b1e739d05eee5b57d9301b245779eba984a7 [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"
Martin Stjernholm14ee8322020-09-21 21:45:49 +010019 "path/filepath"
Jiyong Park892a98f2020-12-14 09:20:00 +090020 "strings"
Colin Crossce75d2c2016-10-06 16:12:58 -070021)
22
23func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000024 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
25}
26
27func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000028 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000029 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
30 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040031 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000032 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000033 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070034}
35
36type prebuiltLinkerInterface interface {
37 Name(string) string
38 prebuilt() *android.Prebuilt
39}
40
Patrice Arruda3554a982019-03-27 19:09:10 -070041type prebuiltLinkerProperties struct {
Patrice Arruda3554a982019-03-27 19:09:10 -070042 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
43 Srcs []string `android:"path,arch_variant"`
44
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070045 Sanitized Sanitized `android:"arch_variant"`
46
Patrice Arruda3554a982019-03-27 19:09:10 -070047 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
48 // symbols, etc), default true.
49 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080050
51 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
52 // This is needed only if this library is linked by other modules in build time.
53 // Only makes sense for the Windows target.
54 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070055}
56
Colin Crossde89fb82017-03-17 13:28:06 -070057type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070058 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080059
Patrice Arruda3554a982019-03-27 19:09:10 -070060 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070061}
62
Colin Crossde89fb82017-03-17 13:28:06 -070063func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070064 return &p.Prebuilt
65}
66
Colin Cross74d73e22017-08-02 11:05:49 -070067func (p *prebuiltLinker) PrebuiltSrcs() []string {
68 return p.properties.Srcs
69}
70
Colin Cross33b2fb72019-05-14 14:07:01 -070071type prebuiltLibraryInterface interface {
72 libraryInterface
73 prebuiltLinkerInterface
74 disablePrebuilt()
75}
76
Colin Crossde89fb82017-03-17 13:28:06 -070077type prebuiltLibraryLinker struct {
78 *libraryDecorator
79 prebuiltLinker
80}
81
82var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070083var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070084
Yi Kong00981662018-08-13 16:02:49 -070085func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
86
87func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080088 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070089}
90
91func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070092 return flags
Yi Kong00981662018-08-13 16:02:49 -070093}
94
95func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
96 return p.libraryDecorator.linkerProps()
97}
98
Colin Crossce75d2c2016-10-06 16:12:58 -070099func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700100 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000101
Colin Cross0de8a1e2020-09-18 14:15:30 -0700102 p.libraryDecorator.flagExporter.exportIncludes(ctx)
103 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
104 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
105 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
106 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
107 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
108
109 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000110
Colin Crossce75d2c2016-10-06 16:12:58 -0700111 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700112 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000113 if len(srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700114 builderFlags := flagsToBuilderFlags(flags)
115
Paul Duffinbce90da2020-03-12 20:17:14 +0000116 if len(srcs) > 1 {
117 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
118 return nil
119 }
120
Jiyong Park892a98f2020-12-14 09:20:00 +0900121 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
122
Paul Duffinbce90da2020-03-12 20:17:14 +0000123 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700124
Yo Chianga3ad9b22020-03-18 14:19:07 +0800125 if p.static() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700126 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
127 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
128 StaticLibrary: in,
129
130 TransitiveStaticLibrariesForOrdering: depSet,
131 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800132 return in
133 }
134
Colin Cross88f6fef2018-09-05 14:20:03 -0700135 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700136 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700137 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800138 outputFile := android.PathForModuleOut(ctx, libName)
139 var implicits android.Paths
140
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200141 if p.stripper.NeedsStrip(ctx) {
142 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700143 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200144 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700145 in = stripped
146 }
147
Colin Crossb60190a2018-09-04 16:28:17 -0700148 // Optimize out relinking against shared libraries whose interface hasn't changed by
149 // depending on a table of contents file instead of the library itself.
150 tocFile := android.PathForModuleOut(ctx, libName+".toc")
151 p.tocFile = android.OptionalPathForPath(tocFile)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500152 transformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700153
Yo Chianga3ad9b22020-03-18 14:19:07 +0800154 if ctx.Windows() && p.properties.Windows_import_lib != nil {
155 // Consumers of this library actually links to the import library in build
156 // time and dynamically links to the DLL in run time. i.e.
157 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
158 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
159 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
160 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
161 implicits = append(implicits, importLibOutputFile)
162
163 ctx.Build(pctx, android.BuildParams{
164 Rule: android.Cp,
165 Description: "prebuilt import library",
166 Input: importLibSrc,
167 Output: importLibOutputFile,
168 Args: map[string]string{
169 "cpFlags": "-L",
170 },
171 })
172 }
173
174 ctx.Build(pctx, android.BuildParams{
175 Rule: android.Cp,
176 Description: "prebuilt shared library",
177 Implicits: implicits,
178 Input: in,
179 Output: outputFile,
180 Args: map[string]string{
181 "cpFlags": "-L",
182 },
183 })
184
Colin Cross0de8a1e2020-09-18 14:15:30 -0700185 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
186 SharedLibrary: outputFile,
187 UnstrippedSharedLibrary: p.unstrippedOutputFile,
188
189 TableOfContents: p.tocFile,
190 })
191
Yo Chianga3ad9b22020-03-18 14:19:07 +0800192 return outputFile
193 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700194 }
195
Colin Cross649d8172020-12-10 12:30:21 -0800196 if p.header() {
197 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
198
199 return nil
200 }
201
Colin Crossce75d2c2016-10-06 16:12:58 -0700202 return nil
203}
204
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700205func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
206 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000207 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700208 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000209 if p.static() {
210 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700211 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000212 }
213 if p.shared() {
214 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700215 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000216 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000217 return srcs
218}
219
Jiyong Park379de2f2018-12-19 02:47:14 +0900220func (p *prebuiltLibraryLinker) shared() bool {
221 return p.libraryDecorator.shared()
222}
223
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700224func (p *prebuiltLibraryLinker) nativeCoverage() bool {
225 return false
226}
227
Colin Cross33b2fb72019-05-14 14:07:01 -0700228func (p *prebuiltLibraryLinker) disablePrebuilt() {
229 p.properties.Srcs = nil
230}
231
Jiyong Park892a98f2020-12-14 09:20:00 +0900232// Implements versionedInterface
233func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
234 return strings.TrimPrefix(name, "prebuilt_")
235}
236
Paul Duffinac6e6082019-12-11 15:22:32 +0000237func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700238 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700239 module.compiler = nil
240
241 prebuilt := &prebuiltLibraryLinker{
242 libraryDecorator: library,
243 }
244 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700245 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700246
Colin Cross74d73e22017-08-02 11:05:49 -0700247 module.AddProperties(&prebuilt.properties)
248
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000249 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700250 return prebuilt.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000251 }
252
253 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Jiyong Park379de2f2018-12-19 02:47:14 +0900254
Paul Duffinac6e6082019-12-11 15:22:32 +0000255 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900256 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000257 return module, library
258}
259
Paul Duffinbce90da2020-03-12 20:17:14 +0000260// cc_prebuilt_library installs a precompiled shared library that are
261// listed in the srcs property in the device's directory.
262func PrebuiltLibraryFactory() android.Module {
263 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
264
265 // Prebuilt shared libraries can be included in APEXes
266 android.InitApexModule(module)
267
268 return module.Init()
269}
270
Paul Duffinac6e6082019-12-11 15:22:32 +0000271// cc_prebuilt_library_shared installs a precompiled shared library that are
272// listed in the srcs property in the device's directory.
273func PrebuiltSharedLibraryFactory() android.Module {
274 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
275 return module.Init()
276}
277
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400278// cc_prebuilt_test_library_shared installs a precompiled shared library
279// to be used as a data dependency of a test-related module (such as cc_test, or
280// cc_test_library).
281func PrebuiltSharedTestLibraryFactory() android.Module {
282 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported)
283 library.BuildOnlyShared()
284 library.baseInstaller = NewTestInstaller()
285 return module.Init()
286}
287
Paul Duffinac6e6082019-12-11 15:22:32 +0000288func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
289 module, library := NewPrebuiltLibrary(hod)
290 library.BuildOnlyShared()
291
292 // Prebuilt shared libraries can be included in APEXes
293 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900294
Leo Li74f7b972017-05-17 11:30:45 -0700295 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700296}
297
Patrice Arruda3554a982019-03-27 19:09:10 -0700298// cc_prebuilt_library_static installs a precompiled static library that are
299// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900300func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700301 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
302 return module.Init()
303}
304
305func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000306 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700307 library.BuildOnlyStatic()
Liz Kammer3f9e1552021-04-02 18:47:09 -0400308 module.bazelHandler = &prebuiltStaticLibraryBazelHandler{module: module, library: library}
Leo Li74f7b972017-05-17 11:30:45 -0700309 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700310}
311
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000312type prebuiltObjectProperties struct {
313 Srcs []string `android:"path,arch_variant"`
314}
315
316type prebuiltObjectLinker struct {
317 android.Prebuilt
318 objectLinker
319
320 properties prebuiltObjectProperties
321}
322
Liz Kammer3f9e1552021-04-02 18:47:09 -0400323type prebuiltStaticLibraryBazelHandler struct {
324 bazelHandler
325
326 module *Module
327 library *libraryDecorator
328}
329
330func (h *prebuiltStaticLibraryBazelHandler) generateBazelBuildActions(ctx android.ModuleContext, label string) bool {
331 bazelCtx := ctx.Config().BazelContext
332 staticLibs, ok := bazelCtx.GetPrebuiltCcStaticLibraryFiles(label, ctx.Arch().ArchType)
333 if !ok {
334 return false
335 }
336 if len(staticLibs) > 1 {
337 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
338 return false
339 }
340
341 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
342
343 // TODO(eakammer):Add stub-related flags if this library is a stub library.
344 // h.library.exportVersioningMacroIfNeeded(ctx)
345
346 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
347 // validation will fail. For now, set this to an empty list.
348 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
349 h.library.collectedSnapshotHeaders = android.Paths{}
350
351 if len(staticLibs) == 0 {
352 h.module.outputFile = android.OptionalPath{}
353 return true
354 }
355
356 out := android.PathForBazelOut(ctx, staticLibs[0])
357 h.module.outputFile = android.OptionalPathForPath(out)
358
359 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build()
360 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
361 StaticLibrary: out,
362
363 TransitiveStaticLibrariesForOrdering: depSet,
364 })
365
366 return true
367}
368
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000369func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
370 return &p.Prebuilt
371}
372
373var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
374
375func (p *prebuiltObjectLinker) link(ctx ModuleContext,
376 flags Flags, deps PathDeps, objs Objects) android.Path {
377 if len(p.properties.Srcs) > 0 {
378 return p.Prebuilt.SingleSourcePath(ctx)
379 }
380 return nil
381}
382
Inseob Kim1042d292020-06-01 23:23:05 +0900383func (p *prebuiltObjectLinker) object() bool {
384 return true
385}
386
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000387func newPrebuiltObject() *Module {
388 module := newObject()
389 prebuilt := &prebuiltObjectLinker{
390 objectLinker: objectLinker{
391 baseLinker: NewBaseLinker(nil),
392 },
393 }
394 module.linker = prebuilt
395 module.AddProperties(&prebuilt.properties)
396 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
397 android.InitSdkAwareModule(module)
398 return module
399}
400
401func prebuiltObjectFactory() android.Module {
402 module := newPrebuiltObject()
403 return module.Init()
404}
405
Colin Crossde89fb82017-03-17 13:28:06 -0700406type prebuiltBinaryLinker struct {
407 *binaryDecorator
408 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100409
410 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700411}
412
413var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
414
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100415func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
416 return p.toolPath
417}
418
Colin Crossde89fb82017-03-17 13:28:06 -0700419func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
420 flags Flags, deps PathDeps, objs Objects) android.Path {
421 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700422 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700423 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
424 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100425 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700426 p.unstrippedOutputFile = in
427
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100428 if ctx.Host() {
429 // Host binaries are symlinked to their prebuilt source locations. That
430 // way they are executed directly from there so the linker resolves their
431 // shared library dependencies relative to that location (using
432 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
433 // repository can supply the expected versions of the shared libraries
434 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700435
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100436 // These shared lib paths may point to copies of the libs in
437 // .intermediates, which isn't where the binary will load them from, but
438 // it's fine for dependency tracking. If a library dependency is updated,
439 // the symlink will get a new timestamp, along with any installed symlinks
440 // handled in make.
441 sharedLibPaths := deps.EarlySharedLibs
442 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
443 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
444
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100445 var fromPath = in.String()
446 if !filepath.IsAbs(fromPath) {
447 fromPath = "$$PWD/" + fromPath
448 }
449
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100450 ctx.Build(pctx, android.BuildParams{
451 Rule: android.Symlink,
452 Output: outputFile,
453 Input: in,
454 Implicits: sharedLibPaths,
455 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100456 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100457 },
458 })
459
460 p.toolPath = android.OptionalPathForPath(outputFile)
461 } else {
462 if p.stripper.NeedsStrip(ctx) {
463 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
464 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
465 in = stripped
466 }
467
468 // Copy binaries to a name matching the final installed name
469 ctx.Build(pctx, android.BuildParams{
470 Rule: android.CpExecutable,
471 Description: "prebuilt",
472 Output: outputFile,
473 Input: in,
474 })
475 }
Colin Cross94921e72017-08-08 16:20:15 -0700476
477 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700478 }
479
480 return nil
481}
482
Inseob Kim7f283f42020-06-01 21:53:49 +0900483func (p *prebuiltBinaryLinker) binary() bool {
484 return true
485}
486
Patrice Arruda3554a982019-03-27 19:09:10 -0700487// cc_prebuilt_binary installs a precompiled executable in srcs property in the
488// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700489func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700490 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
491 return module.Init()
492}
493
494func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
495 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700496 module.compiler = nil
497
498 prebuilt := &prebuiltBinaryLinker{
499 binaryDecorator: binary,
500 }
501 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100502 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700503
Colin Cross74d73e22017-08-02 11:05:49 -0700504 module.AddProperties(&prebuilt.properties)
505
506 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700507 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700508}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700509
510type Sanitized struct {
511 None struct {
512 Srcs []string `android:"path,arch_variant"`
513 } `android:"arch_variant"`
514 Address struct {
515 Srcs []string `android:"path,arch_variant"`
516 } `android:"arch_variant"`
517 Hwaddress struct {
518 Srcs []string `android:"path,arch_variant"`
519 } `android:"arch_variant"`
520}
521
522func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
523 if sanitize == nil {
524 return nil
525 }
526 if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil {
527 return sanitized.Address.Srcs
528 }
529 if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil {
530 return sanitized.Hwaddress.Srcs
531 }
532 return sanitized.None.Srcs
533}