blob: e9f790f73e43192c83add66217ef69b166154493 [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
Spandan Das2b6dfb52024-01-19 00:22:22 +000020 "github.com/google/blueprint/proptools"
21
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000022 "android/soong/android"
Colin Crossce75d2c2016-10-06 16:12:58 -070023)
24
25func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000026 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
27}
28
29func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000030 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000031 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
32 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040033 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Colin Crossc5075e92022-12-05 16:46:39 -080034 ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +000035 ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070036}
37
38type prebuiltLinkerInterface interface {
39 Name(string) string
40 prebuilt() *android.Prebuilt
Spandan Das2b6dfb52024-01-19 00:22:22 +000041 sourceModuleName() string
Colin Crossce75d2c2016-10-06 16:12:58 -070042}
43
Patrice Arruda3554a982019-03-27 19:09:10 -070044type prebuiltLinkerProperties struct {
Spandan Das2b6dfb52024-01-19 00:22:22 +000045 // Name of the source soong module that gets shadowed by this prebuilt
46 // If unspecified, follows the naming convention that the source module of
47 // the prebuilt is Name() without "prebuilt_" prefix
48 Source_module_name *string
49
Patrice Arruda3554a982019-03-27 19:09:10 -070050 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
51 Srcs []string `android:"path,arch_variant"`
52
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070053 Sanitized Sanitized `android:"arch_variant"`
54
Patrice Arruda3554a982019-03-27 19:09:10 -070055 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
56 // symbols, etc), default true.
57 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080058
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +010059 // if set, add an extra objcopy --prefix-symbols= step
60 Prefix_symbols *string
61
Yo Chianga3ad9b22020-03-18 14:19:07 +080062 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
63 // This is needed only if this library is linked by other modules in build time.
64 // Only makes sense for the Windows target.
65 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070066}
67
Colin Crossde89fb82017-03-17 13:28:06 -070068type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070069 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080070
Patrice Arruda3554a982019-03-27 19:09:10 -070071 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070072}
73
Colin Crossde89fb82017-03-17 13:28:06 -070074func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070075 return &p.Prebuilt
76}
77
Colin Cross74d73e22017-08-02 11:05:49 -070078func (p *prebuiltLinker) PrebuiltSrcs() []string {
79 return p.properties.Srcs
80}
81
Colin Cross33b2fb72019-05-14 14:07:01 -070082type prebuiltLibraryInterface interface {
83 libraryInterface
84 prebuiltLinkerInterface
85 disablePrebuilt()
86}
87
Colin Crossde89fb82017-03-17 13:28:06 -070088type prebuiltLibraryLinker struct {
89 *libraryDecorator
90 prebuiltLinker
91}
92
93var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070094var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070095
Yi Kong00981662018-08-13 16:02:49 -070096func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
97
98func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080099 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -0700100}
101
102func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -0700103 return flags
Yi Kong00981662018-08-13 16:02:49 -0700104}
105
106func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
107 return p.libraryDecorator.linkerProps()
108}
109
Colin Crossce75d2c2016-10-06 16:12:58 -0700110func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700111 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000112
Colin Cross0de8a1e2020-09-18 14:15:30 -0700113 p.libraryDecorator.flagExporter.exportIncludes(ctx)
114 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
115 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
116 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
117 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
118 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
119
120 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000121
Colin Crossce75d2c2016-10-06 16:12:58 -0700122 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700123 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000124 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000125 if len(srcs) > 1 {
126 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
127 return nil
128 }
129
Jiyong Park892a98f2020-12-14 09:20:00 +0900130 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
131
Paul Duffinbce90da2020-03-12 20:17:14 +0000132 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700133
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +0100134 if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
135 prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
136 transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
137 in, flagsToBuilderFlags(flags), prefixed)
138 in = prefixed
139 }
140
Yo Chianga3ad9b22020-03-18 14:19:07 +0800141 if p.static() {
Colin Crossc85750b2022-04-21 12:50:51 -0700142 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(in).Build()
Colin Cross40213022023-12-13 15:19:49 -0800143 android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700144 StaticLibrary: in,
145
146 TransitiveStaticLibrariesForOrdering: depSet,
147 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800148 return in
149 }
150
Colin Cross88f6fef2018-09-05 14:20:03 -0700151 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700152 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700153 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800154 outputFile := android.PathForModuleOut(ctx, libName)
155 var implicits android.Paths
156
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200157 if p.stripper.NeedsStrip(ctx) {
158 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700159 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200160 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700161 in = stripped
162 }
163
Colin Crossb60190a2018-09-04 16:28:17 -0700164 // Optimize out relinking against shared libraries whose interface hasn't changed by
165 // depending on a table of contents file instead of the library itself.
166 tocFile := android.PathForModuleOut(ctx, libName+".toc")
167 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400168 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700169
Yo Chianga3ad9b22020-03-18 14:19:07 +0800170 if ctx.Windows() && p.properties.Windows_import_lib != nil {
171 // Consumers of this library actually links to the import library in build
172 // time and dynamically links to the DLL in run time. i.e.
173 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
174 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
175 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
176 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
177 implicits = append(implicits, importLibOutputFile)
178
179 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400180 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800181 Description: "prebuilt import library",
182 Input: importLibSrc,
183 Output: importLibOutputFile,
184 Args: map[string]string{
185 "cpFlags": "-L",
186 },
187 })
188 }
189
190 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400191 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800192 Description: "prebuilt shared library",
193 Implicits: implicits,
194 Input: in,
195 Output: outputFile,
196 Args: map[string]string{
197 "cpFlags": "-L",
198 },
199 })
200
Colin Cross40213022023-12-13 15:19:49 -0800201 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400202 SharedLibrary: outputFile,
203 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700204
205 TableOfContents: p.tocFile,
206 })
207
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000208 // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub
Jooyung Hane5f063e2023-03-23 14:31:19 +0900209 // library as their source and must not be installed, but other prebuilts like
210 // libclang_rt.* libraries set `stubs` property because they are LLNDK libraries,
211 // but use an implementation library as their source and need to be installed.
212 // This discrepancy should be resolved without the prefix hack below.
213 isModuleSdkPrebuilts := android.HasAnyPrefix(ctx.ModuleDir(), []string{
214 "prebuilts/runtime/mainline/", "prebuilts/module_sdk/"})
215 if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() && isModuleSdkPrebuilts {
Jingwen Chen8ac7d7d2023-03-20 11:05:16 +0000216 ctx.Module().MakeUninstallable()
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000217 }
218
Yo Chianga3ad9b22020-03-18 14:19:07 +0800219 return outputFile
220 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700221 }
222
Colin Cross649d8172020-12-10 12:30:21 -0800223 if p.header() {
Colin Cross40213022023-12-13 15:19:49 -0800224 android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{})
Colin Cross649d8172020-12-10 12:30:21 -0800225
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000226 // Need to return an output path so that the AndroidMk logic doesn't skip
227 // the prebuilt header. For compatibility, in case Android.mk files use a
228 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
229 // placeholder, just like non-prebuilt header modules do in linkStatic().
230 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
231 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
232 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800233 }
234
Colin Crossce75d2c2016-10-06 16:12:58 -0700235 return nil
236}
237
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700238func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
239 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000240 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700241 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000242 if p.static() {
243 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700244 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000245 }
246 if p.shared() {
247 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700248 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000249 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000250 return srcs
251}
252
Jiyong Park379de2f2018-12-19 02:47:14 +0900253func (p *prebuiltLibraryLinker) shared() bool {
254 return p.libraryDecorator.shared()
255}
256
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700257func (p *prebuiltLibraryLinker) nativeCoverage() bool {
258 return false
259}
260
Colin Cross33b2fb72019-05-14 14:07:01 -0700261func (p *prebuiltLibraryLinker) disablePrebuilt() {
262 p.properties.Srcs = nil
Ryan Prichard21e2c102024-02-28 18:59:30 -0800263 p.properties.Sanitized.None.Srcs = nil
264 p.properties.Sanitized.Address.Srcs = nil
265 p.properties.Sanitized.Hwaddress.Srcs = nil
Colin Cross33b2fb72019-05-14 14:07:01 -0700266}
267
Jiyong Park892a98f2020-12-14 09:20:00 +0900268// Implements versionedInterface
269func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100270 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900271}
272
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000273func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700274 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700275 module.compiler = nil
276
277 prebuilt := &prebuiltLibraryLinker{
278 libraryDecorator: library,
279 }
280 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700281 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700282
Colin Cross74d73e22017-08-02 11:05:49 -0700283 module.AddProperties(&prebuilt.properties)
284
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000285 if srcsProperty == "" {
286 android.InitPrebuiltModuleWithoutSrcs(module)
287 } else {
288 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
289 return prebuilt.prebuiltSrcs(ctx)
290 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000291
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000292 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
293 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900294
Paul Duffinac6e6082019-12-11 15:22:32 +0000295 return module, library
296}
297
Paul Duffinbce90da2020-03-12 20:17:14 +0000298// cc_prebuilt_library installs a precompiled shared library that are
299// listed in the srcs property in the device's directory.
300func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000301 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000302
303 // Prebuilt shared libraries can be included in APEXes
304 android.InitApexModule(module)
305
306 return module.Init()
307}
308
Paul Duffinac6e6082019-12-11 15:22:32 +0000309// cc_prebuilt_library_shared installs a precompiled shared library that are
310// listed in the srcs property in the device's directory.
311func PrebuiltSharedLibraryFactory() android.Module {
312 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
313 return module.Init()
314}
315
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400316// cc_prebuilt_test_library_shared installs a precompiled shared library
317// to be used as a data dependency of a test-related module (such as cc_test, or
318// cc_test_library).
319func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000320 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400321 library.BuildOnlyShared()
322 library.baseInstaller = NewTestInstaller()
323 return module.Init()
324}
325
Paul Duffinac6e6082019-12-11 15:22:32 +0000326func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000327 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000328 library.BuildOnlyShared()
329
330 // Prebuilt shared libraries can be included in APEXes
331 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900332
Leo Li74f7b972017-05-17 11:30:45 -0700333 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700334}
335
Patrice Arruda3554a982019-03-27 19:09:10 -0700336// cc_prebuilt_library_static installs a precompiled static library that are
337// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900338func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700339 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
340 return module.Init()
341}
342
343func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000344 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700345 library.BuildOnlyStatic()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000346
Leo Li74f7b972017-05-17 11:30:45 -0700347 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700348}
349
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000350type prebuiltObjectProperties struct {
Spandan Das2b6dfb52024-01-19 00:22:22 +0000351 // Name of the source soong module that gets shadowed by this prebuilt
352 // If unspecified, follows the naming convention that the source module of
353 // the prebuilt is Name() without "prebuilt_" prefix
354 Source_module_name *string
355 Srcs []string `android:"path,arch_variant"`
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000356}
357
358type prebuiltObjectLinker struct {
359 android.Prebuilt
360 objectLinker
361
362 properties prebuiltObjectProperties
363}
364
365func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
366 return &p.Prebuilt
367}
368
Spandan Das2b6dfb52024-01-19 00:22:22 +0000369func (p *prebuiltObjectLinker) sourceModuleName() string {
370 return proptools.String(p.properties.Source_module_name)
371}
372
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000373var _ 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 {
Colin Crossee02aed2022-04-15 15:16:02 -0700378 // Copy objects to a name matching the final installed name
379 in := p.Prebuilt.SingleSourcePath(ctx)
380 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
381 ctx.Build(pctx, android.BuildParams{
382 Rule: android.CpExecutable,
383 Description: "prebuilt",
384 Output: outputFile,
385 Input: in,
386 })
387 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000388 }
389 return nil
390}
391
Inseob Kim1042d292020-06-01 23:23:05 +0900392func (p *prebuiltObjectLinker) object() bool {
393 return true
394}
395
Colin Cross7cabd422021-06-25 14:21:04 -0700396func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
397 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000398 prebuilt := &prebuiltObjectLinker{
399 objectLinker: objectLinker{
400 baseLinker: NewBaseLinker(nil),
401 },
402 }
403 module.linker = prebuilt
404 module.AddProperties(&prebuilt.properties)
405 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000406 return module
407}
408
Colin Crossc5075e92022-12-05 16:46:39 -0800409func PrebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700410 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000411 return module.Init()
412}
413
Colin Crossde89fb82017-03-17 13:28:06 -0700414type prebuiltBinaryLinker struct {
415 *binaryDecorator
416 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100417
418 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700419}
420
421var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
422
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100423func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
424 return p.toolPath
425}
426
Colin Crossde89fb82017-03-17 13:28:06 -0700427func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
428 flags Flags, deps PathDeps, objs Objects) android.Path {
429 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700430 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700431 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
432 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100433 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700434 p.unstrippedOutputFile = in
435
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100436 if ctx.Host() {
437 // Host binaries are symlinked to their prebuilt source locations. That
438 // way they are executed directly from there so the linker resolves their
439 // shared library dependencies relative to that location (using
440 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
441 // repository can supply the expected versions of the shared libraries
442 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700443
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100444 // These shared lib paths may point to copies of the libs in
445 // .intermediates, which isn't where the binary will load them from, but
446 // it's fine for dependency tracking. If a library dependency is updated,
447 // the symlink will get a new timestamp, along with any installed symlinks
448 // handled in make.
449 sharedLibPaths := deps.EarlySharedLibs
450 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
451 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
452
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100453 var fromPath = in.String()
454 if !filepath.IsAbs(fromPath) {
455 fromPath = "$$PWD/" + fromPath
456 }
457
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100458 ctx.Build(pctx, android.BuildParams{
459 Rule: android.Symlink,
460 Output: outputFile,
461 Input: in,
462 Implicits: sharedLibPaths,
463 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100464 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100465 },
466 })
467
468 p.toolPath = android.OptionalPathForPath(outputFile)
469 } else {
470 if p.stripper.NeedsStrip(ctx) {
471 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
472 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
473 in = stripped
474 }
475
476 // Copy binaries to a name matching the final installed name
477 ctx.Build(pctx, android.BuildParams{
478 Rule: android.CpExecutable,
479 Description: "prebuilt",
480 Output: outputFile,
481 Input: in,
482 })
483 }
Colin Cross94921e72017-08-08 16:20:15 -0700484
485 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700486 }
487
488 return nil
489}
490
Inseob Kim7f283f42020-06-01 21:53:49 +0900491func (p *prebuiltBinaryLinker) binary() bool {
492 return true
493}
494
Patrice Arruda3554a982019-03-27 19:09:10 -0700495// cc_prebuilt_binary installs a precompiled executable in srcs property in the
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000496// device's directory, for both the host and device
497func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700498 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
499 return module.Init()
500}
501
502func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross8ff10582023-12-07 13:10:56 -0800503 module, binary := newBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700504 module.compiler = nil
505
506 prebuilt := &prebuiltBinaryLinker{
507 binaryDecorator: binary,
508 }
509 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100510 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700511
Colin Cross74d73e22017-08-02 11:05:49 -0700512 module.AddProperties(&prebuilt.properties)
513
514 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700515 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700516}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700517
518type Sanitized struct {
519 None struct {
520 Srcs []string `android:"path,arch_variant"`
521 } `android:"arch_variant"`
522 Address struct {
523 Srcs []string `android:"path,arch_variant"`
524 } `android:"arch_variant"`
525 Hwaddress struct {
526 Srcs []string `android:"path,arch_variant"`
527 } `android:"arch_variant"`
528}
529
530func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
531 if sanitize == nil {
532 return nil
533 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400534 if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700535 return sanitized.Address.Srcs
536 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400537 if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700538 return sanitized.Hwaddress.Srcs
539 }
540 return sanitized.None.Srcs
541}
Spandan Das2b6dfb52024-01-19 00:22:22 +0000542
543func (p *prebuiltLinker) sourceModuleName() string {
544 return proptools.String(p.properties.Source_module_name)
545}