blob: 1c98aed87801936f9af9d4aa51edd0df3f716674 [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.
Cole Faust96a692b2024-08-08 14:47:51 -070051 Srcs proptools.Configurable[[]string] `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070052
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 Cross33b2fb72019-05-14 14:07:01 -070078type prebuiltLibraryInterface interface {
79 libraryInterface
80 prebuiltLinkerInterface
81 disablePrebuilt()
82}
83
Colin Crossde89fb82017-03-17 13:28:06 -070084type prebuiltLibraryLinker struct {
85 *libraryDecorator
86 prebuiltLinker
87}
88
89var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070090var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070091
Yi Kong00981662018-08-13 16:02:49 -070092func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
93
94func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080095 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070096}
97
Yi Kong00981662018-08-13 16:02:49 -070098func (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)
Spandan Das357ffcc2024-07-24 18:07:48 +0000116 stubInfo := addStubDependencyProviders(ctx)
117
118 // Stub variants will create a stub .so file from stub .c files
119 if p.buildStubs() && objs.objFiles != nil {
120 // TODO (b/275273834): Make objs.objFiles == nil a hard error when the symbol files have been added to module sdk.
121 return p.linkShared(ctx, flags, deps, objs)
122 }
123
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
Yo Chianga3ad9b22020-03-18 14:19:07 +0800208 return outputFile
209 }
Spandan Das357ffcc2024-07-24 18:07:48 +0000210 } else if p.shared() && len(stubInfo) > 0 {
211 // This is a prebuilt which does not have any implementation (nil `srcs`), but provides APIs.
212 // Provide the latest (i.e. `current`) stubs to reverse dependencies.
213 latestStub := stubInfo[len(stubInfo)-1].SharedLibraryInfo.SharedLibrary
214 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
215 SharedLibrary: latestStub,
216 Target: ctx.Target(),
217 })
218
219 return latestStub
Colin Crossce75d2c2016-10-06 16:12:58 -0700220 }
221
Colin Cross649d8172020-12-10 12:30:21 -0800222 if p.header() {
Colin Cross40213022023-12-13 15:19:49 -0800223 android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{})
Colin Cross649d8172020-12-10 12:30:21 -0800224
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000225 // Need to return an output path so that the AndroidMk logic doesn't skip
226 // the prebuilt header. For compatibility, in case Android.mk files use a
227 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
228 // placeholder, just like non-prebuilt header modules do in linkStatic().
229 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
230 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
231 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800232 }
233
Colin Crossce75d2c2016-10-06 16:12:58 -0700234 return nil
235}
236
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700237func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
238 sanitize := ctx.Module().(*Module).sanitize
Cole Faust96a692b2024-08-08 14:47:51 -0700239 srcs := p.properties.Srcs.GetOrDefault(ctx, nil)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700240 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000241 if p.static() {
Cole Faust96a692b2024-08-08 14:47:51 -0700242 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs.GetOrDefault(ctx, nil)...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700243 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000244 }
245 if p.shared() {
Cole Faust96a692b2024-08-08 14:47:51 -0700246 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs.GetOrDefault(ctx, nil)...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700247 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000248 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000249 return srcs
250}
251
Jiyong Park379de2f2018-12-19 02:47:14 +0900252func (p *prebuiltLibraryLinker) shared() bool {
253 return p.libraryDecorator.shared()
254}
255
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700256func (p *prebuiltLibraryLinker) nativeCoverage() bool {
257 return false
258}
259
Colin Cross33b2fb72019-05-14 14:07:01 -0700260func (p *prebuiltLibraryLinker) disablePrebuilt() {
Cole Faust96a692b2024-08-08 14:47:51 -0700261 p.properties.Srcs = proptools.NewConfigurable[[]string](nil, nil)
Ryan Prichard21e2c102024-02-28 18:59:30 -0800262 p.properties.Sanitized.None.Srcs = nil
263 p.properties.Sanitized.Address.Srcs = nil
264 p.properties.Sanitized.Hwaddress.Srcs = nil
Colin Cross33b2fb72019-05-14 14:07:01 -0700265}
266
Jiyong Park892a98f2020-12-14 09:20:00 +0900267// Implements versionedInterface
268func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100269 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900270}
271
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000272func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700273 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700274
275 prebuilt := &prebuiltLibraryLinker{
276 libraryDecorator: library,
277 }
Spandan Das357ffcc2024-07-24 18:07:48 +0000278 module.compiler = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700279 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700280 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700281
Colin Cross74d73e22017-08-02 11:05:49 -0700282 module.AddProperties(&prebuilt.properties)
283
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000284 if srcsProperty == "" {
285 android.InitPrebuiltModuleWithoutSrcs(module)
286 } else {
287 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
288 return prebuilt.prebuiltSrcs(ctx)
289 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000290
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000291 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
292 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900293
Paul Duffinac6e6082019-12-11 15:22:32 +0000294 return module, library
295}
296
Spandan Das357ffcc2024-07-24 18:07:48 +0000297func (p *prebuiltLibraryLinker) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
298 if p.buildStubs() && p.stubsVersion() != "" {
299 return p.compileModuleLibApiStubs(ctx, flags, deps)
300 }
301 return Objects{}
302}
303
Paul Duffinbce90da2020-03-12 20:17:14 +0000304// cc_prebuilt_library installs a precompiled shared library that are
305// listed in the srcs property in the device's directory.
306func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000307 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000308
309 // Prebuilt shared libraries can be included in APEXes
310 android.InitApexModule(module)
311
312 return module.Init()
313}
314
Paul Duffinac6e6082019-12-11 15:22:32 +0000315// cc_prebuilt_library_shared installs a precompiled shared library that are
316// listed in the srcs property in the device's directory.
317func PrebuiltSharedLibraryFactory() android.Module {
318 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
319 return module.Init()
320}
321
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400322// cc_prebuilt_test_library_shared installs a precompiled shared library
323// to be used as a data dependency of a test-related module (such as cc_test, or
324// cc_test_library).
325func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000326 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400327 library.BuildOnlyShared()
328 library.baseInstaller = NewTestInstaller()
329 return module.Init()
330}
331
Paul Duffinac6e6082019-12-11 15:22:32 +0000332func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000333 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000334 library.BuildOnlyShared()
335
336 // Prebuilt shared libraries can be included in APEXes
337 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900338
Leo Li74f7b972017-05-17 11:30:45 -0700339 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700340}
341
Patrice Arruda3554a982019-03-27 19:09:10 -0700342// cc_prebuilt_library_static installs a precompiled static library that are
343// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900344func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700345 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
346 return module.Init()
347}
348
349func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000350 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700351 library.BuildOnlyStatic()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000352
Leo Li74f7b972017-05-17 11:30:45 -0700353 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700354}
355
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000356type prebuiltObjectProperties struct {
Spandan Das2b6dfb52024-01-19 00:22:22 +0000357 // Name of the source soong module that gets shadowed by this prebuilt
358 // If unspecified, follows the naming convention that the source module of
359 // the prebuilt is Name() without "prebuilt_" prefix
360 Source_module_name *string
361 Srcs []string `android:"path,arch_variant"`
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000362}
363
364type prebuiltObjectLinker struct {
365 android.Prebuilt
366 objectLinker
367
368 properties prebuiltObjectProperties
369}
370
371func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
372 return &p.Prebuilt
373}
374
Spandan Das2b6dfb52024-01-19 00:22:22 +0000375func (p *prebuiltObjectLinker) sourceModuleName() string {
376 return proptools.String(p.properties.Source_module_name)
377}
378
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000379var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
380
381func (p *prebuiltObjectLinker) link(ctx ModuleContext,
382 flags Flags, deps PathDeps, objs Objects) android.Path {
383 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700384 // Copy objects to a name matching the final installed name
385 in := p.Prebuilt.SingleSourcePath(ctx)
386 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
387 ctx.Build(pctx, android.BuildParams{
388 Rule: android.CpExecutable,
389 Description: "prebuilt",
390 Output: outputFile,
391 Input: in,
392 })
393 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000394 }
395 return nil
396}
397
Inseob Kim1042d292020-06-01 23:23:05 +0900398func (p *prebuiltObjectLinker) object() bool {
399 return true
400}
401
Colin Cross7cabd422021-06-25 14:21:04 -0700402func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
403 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000404 prebuilt := &prebuiltObjectLinker{
405 objectLinker: objectLinker{
406 baseLinker: NewBaseLinker(nil),
407 },
408 }
409 module.linker = prebuilt
410 module.AddProperties(&prebuilt.properties)
411 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000412 return module
413}
414
Colin Crossc5075e92022-12-05 16:46:39 -0800415func PrebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700416 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000417 return module.Init()
418}
419
Colin Crossde89fb82017-03-17 13:28:06 -0700420type prebuiltBinaryLinker struct {
421 *binaryDecorator
422 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100423
424 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700425}
426
427var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
428
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100429func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
430 return p.toolPath
431}
432
Colin Crossde89fb82017-03-17 13:28:06 -0700433func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
434 flags Flags, deps PathDeps, objs Objects) android.Path {
435 // TODO(ccross): verify shared library dependencies
Cole Faust96a692b2024-08-08 14:47:51 -0700436 if len(p.properties.Srcs.GetOrDefault(ctx, nil)) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700437 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
438 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100439 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700440 p.unstrippedOutputFile = in
441
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100442 if ctx.Host() {
443 // Host binaries are symlinked to their prebuilt source locations. That
444 // way they are executed directly from there so the linker resolves their
445 // shared library dependencies relative to that location (using
446 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
447 // repository can supply the expected versions of the shared libraries
448 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700449
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100450 // These shared lib paths may point to copies of the libs in
451 // .intermediates, which isn't where the binary will load them from, but
452 // it's fine for dependency tracking. If a library dependency is updated,
453 // the symlink will get a new timestamp, along with any installed symlinks
454 // handled in make.
455 sharedLibPaths := deps.EarlySharedLibs
456 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
457 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
458
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100459 var fromPath = in.String()
460 if !filepath.IsAbs(fromPath) {
461 fromPath = "$$PWD/" + fromPath
462 }
463
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100464 ctx.Build(pctx, android.BuildParams{
465 Rule: android.Symlink,
466 Output: outputFile,
467 Input: in,
468 Implicits: sharedLibPaths,
469 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100470 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100471 },
472 })
473
474 p.toolPath = android.OptionalPathForPath(outputFile)
475 } else {
476 if p.stripper.NeedsStrip(ctx) {
477 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
478 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
479 in = stripped
480 }
481
482 // Copy binaries to a name matching the final installed name
483 ctx.Build(pctx, android.BuildParams{
484 Rule: android.CpExecutable,
485 Description: "prebuilt",
486 Output: outputFile,
487 Input: in,
488 })
489 }
Colin Cross94921e72017-08-08 16:20:15 -0700490
491 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700492 }
493
494 return nil
495}
496
Inseob Kim7f283f42020-06-01 21:53:49 +0900497func (p *prebuiltBinaryLinker) binary() bool {
498 return true
499}
500
Patrice Arruda3554a982019-03-27 19:09:10 -0700501// 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 +0000502// device's directory, for both the host and device
503func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700504 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
505 return module.Init()
506}
507
508func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross8ff10582023-12-07 13:10:56 -0800509 module, binary := newBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700510 module.compiler = nil
511
512 prebuilt := &prebuiltBinaryLinker{
513 binaryDecorator: binary,
514 }
515 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100516 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700517
Colin Cross74d73e22017-08-02 11:05:49 -0700518 module.AddProperties(&prebuilt.properties)
519
Cole Faust96a692b2024-08-08 14:47:51 -0700520 android.InitConfigurablePrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700521 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700522}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700523
524type Sanitized struct {
525 None struct {
526 Srcs []string `android:"path,arch_variant"`
527 } `android:"arch_variant"`
528 Address struct {
529 Srcs []string `android:"path,arch_variant"`
530 } `android:"arch_variant"`
531 Hwaddress struct {
532 Srcs []string `android:"path,arch_variant"`
533 } `android:"arch_variant"`
534}
535
536func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
537 if sanitize == nil {
538 return nil
539 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400540 if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700541 return sanitized.Address.Srcs
542 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400543 if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700544 return sanitized.Hwaddress.Srcs
545 }
546 return sanitized.None.Srcs
547}
Spandan Das2b6dfb52024-01-19 00:22:22 +0000548
549func (p *prebuiltLinker) sourceModuleName() string {
550 return proptools.String(p.properties.Source_module_name)
551}