blob: 299fb5148c94058b727a7ccc32b8a8dcc088528c [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"
Spandan Das59223332024-08-15 21:54:09 +000019 "strings"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000020
Spandan Das2b6dfb52024-01-19 00:22:22 +000021 "github.com/google/blueprint/proptools"
22
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000023 "android/soong/android"
Colin Crossce75d2c2016-10-06 16:12:58 -070024)
25
26func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000027 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
28}
29
30func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000031 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000032 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
33 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040034 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Colin Crossc5075e92022-12-05 16:46:39 -080035 ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +000036 ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070037}
38
39type prebuiltLinkerInterface interface {
40 Name(string) string
41 prebuilt() *android.Prebuilt
Spandan Das2b6dfb52024-01-19 00:22:22 +000042 sourceModuleName() string
Colin Crossce75d2c2016-10-06 16:12:58 -070043}
44
Patrice Arruda3554a982019-03-27 19:09:10 -070045type prebuiltLinkerProperties struct {
Spandan Das2b6dfb52024-01-19 00:22:22 +000046 // Name of the source soong module that gets shadowed by this prebuilt
47 // If unspecified, follows the naming convention that the source module of
48 // the prebuilt is Name() without "prebuilt_" prefix
49 Source_module_name *string
50
Patrice Arruda3554a982019-03-27 19:09:10 -070051 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
Cole Faust96a692b2024-08-08 14:47:51 -070052 Srcs proptools.Configurable[[]string] `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070053
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070054 Sanitized Sanitized `android:"arch_variant"`
55
Patrice Arruda3554a982019-03-27 19:09:10 -070056 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
57 // symbols, etc), default true.
58 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080059
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +010060 // if set, add an extra objcopy --prefix-symbols= step
61 Prefix_symbols *string
62
Yo Chianga3ad9b22020-03-18 14:19:07 +080063 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
64 // This is needed only if this library is linked by other modules in build time.
65 // Only makes sense for the Windows target.
66 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070067}
68
Colin Crossde89fb82017-03-17 13:28:06 -070069type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070070 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080071
Patrice Arruda3554a982019-03-27 19:09:10 -070072 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070073}
74
Colin Crossde89fb82017-03-17 13:28:06 -070075func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070076 return &p.Prebuilt
77}
78
Colin Cross33b2fb72019-05-14 14:07:01 -070079type prebuiltLibraryInterface interface {
80 libraryInterface
81 prebuiltLinkerInterface
82 disablePrebuilt()
83}
84
Colin Crossde89fb82017-03-17 13:28:06 -070085type prebuiltLibraryLinker struct {
86 *libraryDecorator
87 prebuiltLinker
88}
89
90var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070091var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070092
Yi Kong00981662018-08-13 16:02:49 -070093func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
94
95func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080096 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070097}
98
Yi Kong00981662018-08-13 16:02:49 -070099func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
100 return p.libraryDecorator.linkerProps()
101}
102
Colin Crossce75d2c2016-10-06 16:12:58 -0700103func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700104 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000105
Colin Cross0de8a1e2020-09-18 14:15:30 -0700106 p.libraryDecorator.flagExporter.exportIncludes(ctx)
107 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
108 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
109 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
110 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
111 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
112
113 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000114
Colin Crossce75d2c2016-10-06 16:12:58 -0700115 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700116 srcs := p.prebuiltSrcs(ctx)
Spandan Das357ffcc2024-07-24 18:07:48 +0000117 stubInfo := addStubDependencyProviders(ctx)
118
119 // Stub variants will create a stub .so file from stub .c files
120 if p.buildStubs() && objs.objFiles != nil {
121 // TODO (b/275273834): Make objs.objFiles == nil a hard error when the symbol files have been added to module sdk.
Spandan Das59223332024-08-15 21:54:09 +0000122
123 // The map.txt files of libclang_rt.* contain version information, but the checked in .so files do not.
124 // e.g. libclang_rt.* libs impl
125 // $ nm -D prebuilts/../libclang_rt.hwasan-aarch64-android.so
126 // __hwasan_init
127
128 // stubs generated from .map.txt
129 // $ nm -D out/soong/.intermediates/../<stubs>/libclang_rt.hwasan-aarch64-android.so
130 // __hwasan_init@@LIBCLANG_RT_ASAN
131
132 // Special-case libclang_rt.* libs to account for this discrepancy.
133 // TODO (spandandas): Remove this special case https://r.android.com/3236596 has been submitted, and a new set of map.txt
134 // files of libclang_rt.* libs have been generated.
135 if strings.Contains(ctx.ModuleName(), "libclang_rt.") {
136 p.versionScriptPath = android.OptionalPathForPath(nil)
137 }
Spandan Das357ffcc2024-07-24 18:07:48 +0000138 return p.linkShared(ctx, flags, deps, objs)
139 }
140
Paul Duffinbce90da2020-03-12 20:17:14 +0000141 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000142 if len(srcs) > 1 {
143 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
144 return nil
145 }
146
Jiyong Park892a98f2020-12-14 09:20:00 +0900147 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
148
Paul Duffinbce90da2020-03-12 20:17:14 +0000149 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700150
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +0100151 if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
152 prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
153 transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
154 in, flagsToBuilderFlags(flags), prefixed)
155 in = prefixed
156 }
157
Yo Chianga3ad9b22020-03-18 14:19:07 +0800158 if p.static() {
Colin Crossc85750b2022-04-21 12:50:51 -0700159 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(in).Build()
Colin Cross40213022023-12-13 15:19:49 -0800160 android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700161 StaticLibrary: in,
162
163 TransitiveStaticLibrariesForOrdering: depSet,
164 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800165 return in
166 }
167
Colin Cross88f6fef2018-09-05 14:20:03 -0700168 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700169 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700170 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800171 outputFile := android.PathForModuleOut(ctx, libName)
172 var implicits android.Paths
173
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200174 if p.stripper.NeedsStrip(ctx) {
175 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700176 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200177 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700178 in = stripped
179 }
180
Colin Crossb60190a2018-09-04 16:28:17 -0700181 // Optimize out relinking against shared libraries whose interface hasn't changed by
182 // depending on a table of contents file instead of the library itself.
183 tocFile := android.PathForModuleOut(ctx, libName+".toc")
184 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400185 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700186
Yo Chianga3ad9b22020-03-18 14:19:07 +0800187 if ctx.Windows() && p.properties.Windows_import_lib != nil {
188 // Consumers of this library actually links to the import library in build
189 // time and dynamically links to the DLL in run time. i.e.
190 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
191 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
192 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
193 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
194 implicits = append(implicits, importLibOutputFile)
195
196 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400197 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800198 Description: "prebuilt import library",
199 Input: importLibSrc,
200 Output: importLibOutputFile,
201 Args: map[string]string{
202 "cpFlags": "-L",
203 },
204 })
205 }
206
207 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400208 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800209 Description: "prebuilt shared library",
210 Implicits: implicits,
211 Input: in,
212 Output: outputFile,
213 Args: map[string]string{
214 "cpFlags": "-L",
215 },
216 })
217
Colin Cross40213022023-12-13 15:19:49 -0800218 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400219 SharedLibrary: outputFile,
220 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700221
222 TableOfContents: p.tocFile,
223 })
224
Yo Chianga3ad9b22020-03-18 14:19:07 +0800225 return outputFile
226 }
Spandan Das357ffcc2024-07-24 18:07:48 +0000227 } else if p.shared() && len(stubInfo) > 0 {
228 // This is a prebuilt which does not have any implementation (nil `srcs`), but provides APIs.
229 // Provide the latest (i.e. `current`) stubs to reverse dependencies.
230 latestStub := stubInfo[len(stubInfo)-1].SharedLibraryInfo.SharedLibrary
231 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
232 SharedLibrary: latestStub,
233 Target: ctx.Target(),
234 })
235
236 return latestStub
Colin Crossce75d2c2016-10-06 16:12:58 -0700237 }
238
Colin Cross649d8172020-12-10 12:30:21 -0800239 if p.header() {
Colin Cross40213022023-12-13 15:19:49 -0800240 android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{})
Colin Cross649d8172020-12-10 12:30:21 -0800241
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000242 // Need to return an output path so that the AndroidMk logic doesn't skip
243 // the prebuilt header. For compatibility, in case Android.mk files use a
244 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
245 // placeholder, just like non-prebuilt header modules do in linkStatic().
246 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
247 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
248 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800249 }
250
Colin Crossce75d2c2016-10-06 16:12:58 -0700251 return nil
252}
253
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700254func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
255 sanitize := ctx.Module().(*Module).sanitize
Cole Faust96a692b2024-08-08 14:47:51 -0700256 srcs := p.properties.Srcs.GetOrDefault(ctx, nil)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700257 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000258 if p.static() {
Cole Faust96a692b2024-08-08 14:47:51 -0700259 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs.GetOrDefault(ctx, nil)...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700260 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000261 }
262 if p.shared() {
Cole Faust96a692b2024-08-08 14:47:51 -0700263 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs.GetOrDefault(ctx, nil)...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700264 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000265 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000266 return srcs
267}
268
Jiyong Park379de2f2018-12-19 02:47:14 +0900269func (p *prebuiltLibraryLinker) shared() bool {
270 return p.libraryDecorator.shared()
271}
272
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700273func (p *prebuiltLibraryLinker) nativeCoverage() bool {
274 return false
275}
276
Colin Cross33b2fb72019-05-14 14:07:01 -0700277func (p *prebuiltLibraryLinker) disablePrebuilt() {
Cole Faust96a692b2024-08-08 14:47:51 -0700278 p.properties.Srcs = proptools.NewConfigurable[[]string](nil, nil)
Ryan Prichard21e2c102024-02-28 18:59:30 -0800279 p.properties.Sanitized.None.Srcs = nil
280 p.properties.Sanitized.Address.Srcs = nil
281 p.properties.Sanitized.Hwaddress.Srcs = nil
Colin Cross33b2fb72019-05-14 14:07:01 -0700282}
283
Jiyong Park892a98f2020-12-14 09:20:00 +0900284// Implements versionedInterface
285func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100286 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900287}
288
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000289func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700290 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700291
292 prebuilt := &prebuiltLibraryLinker{
293 libraryDecorator: library,
294 }
Spandan Das357ffcc2024-07-24 18:07:48 +0000295 module.compiler = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700296 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700297 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700298
Colin Cross74d73e22017-08-02 11:05:49 -0700299 module.AddProperties(&prebuilt.properties)
300
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000301 if srcsProperty == "" {
302 android.InitPrebuiltModuleWithoutSrcs(module)
303 } else {
304 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
305 return prebuilt.prebuiltSrcs(ctx)
306 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000307
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000308 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
309 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900310
Paul Duffinac6e6082019-12-11 15:22:32 +0000311 return module, library
312}
313
Spandan Das357ffcc2024-07-24 18:07:48 +0000314func (p *prebuiltLibraryLinker) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
315 if p.buildStubs() && p.stubsVersion() != "" {
316 return p.compileModuleLibApiStubs(ctx, flags, deps)
317 }
318 return Objects{}
319}
320
Paul Duffinbce90da2020-03-12 20:17:14 +0000321// cc_prebuilt_library installs a precompiled shared library that are
322// listed in the srcs property in the device's directory.
323func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000324 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000325
326 // Prebuilt shared libraries can be included in APEXes
327 android.InitApexModule(module)
328
329 return module.Init()
330}
331
Paul Duffinac6e6082019-12-11 15:22:32 +0000332// cc_prebuilt_library_shared installs a precompiled shared library that are
333// listed in the srcs property in the device's directory.
334func PrebuiltSharedLibraryFactory() android.Module {
335 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
336 return module.Init()
337}
338
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400339// cc_prebuilt_test_library_shared installs a precompiled shared library
340// to be used as a data dependency of a test-related module (such as cc_test, or
341// cc_test_library).
342func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000343 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400344 library.BuildOnlyShared()
345 library.baseInstaller = NewTestInstaller()
346 return module.Init()
347}
348
Paul Duffinac6e6082019-12-11 15:22:32 +0000349func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000350 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000351 library.BuildOnlyShared()
352
353 // Prebuilt shared libraries can be included in APEXes
354 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900355
Leo Li74f7b972017-05-17 11:30:45 -0700356 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700357}
358
Patrice Arruda3554a982019-03-27 19:09:10 -0700359// cc_prebuilt_library_static installs a precompiled static library that are
360// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900361func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700362 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
363 return module.Init()
364}
365
366func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000367 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700368 library.BuildOnlyStatic()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000369
Leo Li74f7b972017-05-17 11:30:45 -0700370 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700371}
372
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000373type prebuiltObjectProperties struct {
Spandan Das2b6dfb52024-01-19 00:22:22 +0000374 // Name of the source soong module that gets shadowed by this prebuilt
375 // If unspecified, follows the naming convention that the source module of
376 // the prebuilt is Name() without "prebuilt_" prefix
377 Source_module_name *string
378 Srcs []string `android:"path,arch_variant"`
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000379}
380
381type prebuiltObjectLinker struct {
382 android.Prebuilt
383 objectLinker
384
385 properties prebuiltObjectProperties
386}
387
388func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
389 return &p.Prebuilt
390}
391
Spandan Das2b6dfb52024-01-19 00:22:22 +0000392func (p *prebuiltObjectLinker) sourceModuleName() string {
393 return proptools.String(p.properties.Source_module_name)
394}
395
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000396var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
397
398func (p *prebuiltObjectLinker) link(ctx ModuleContext,
399 flags Flags, deps PathDeps, objs Objects) android.Path {
400 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700401 // Copy objects to a name matching the final installed name
402 in := p.Prebuilt.SingleSourcePath(ctx)
403 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
404 ctx.Build(pctx, android.BuildParams{
405 Rule: android.CpExecutable,
406 Description: "prebuilt",
407 Output: outputFile,
408 Input: in,
409 })
410 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000411 }
412 return nil
413}
414
Inseob Kim1042d292020-06-01 23:23:05 +0900415func (p *prebuiltObjectLinker) object() bool {
416 return true
417}
418
Colin Cross7cabd422021-06-25 14:21:04 -0700419func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
420 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000421 prebuilt := &prebuiltObjectLinker{
422 objectLinker: objectLinker{
423 baseLinker: NewBaseLinker(nil),
424 },
425 }
426 module.linker = prebuilt
427 module.AddProperties(&prebuilt.properties)
428 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000429 return module
430}
431
Colin Crossc5075e92022-12-05 16:46:39 -0800432func PrebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700433 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000434 return module.Init()
435}
436
Colin Crossde89fb82017-03-17 13:28:06 -0700437type prebuiltBinaryLinker struct {
438 *binaryDecorator
439 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100440
441 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700442}
443
444var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
445
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100446func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
447 return p.toolPath
448}
449
Colin Crossde89fb82017-03-17 13:28:06 -0700450func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
451 flags Flags, deps PathDeps, objs Objects) android.Path {
452 // TODO(ccross): verify shared library dependencies
Cole Faust96a692b2024-08-08 14:47:51 -0700453 if len(p.properties.Srcs.GetOrDefault(ctx, nil)) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700454 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
455 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100456 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700457 p.unstrippedOutputFile = in
458
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100459 if ctx.Host() {
460 // Host binaries are symlinked to their prebuilt source locations. That
461 // way they are executed directly from there so the linker resolves their
462 // shared library dependencies relative to that location (using
463 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
464 // repository can supply the expected versions of the shared libraries
465 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700466
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100467 // These shared lib paths may point to copies of the libs in
468 // .intermediates, which isn't where the binary will load them from, but
469 // it's fine for dependency tracking. If a library dependency is updated,
470 // the symlink will get a new timestamp, along with any installed symlinks
471 // handled in make.
472 sharedLibPaths := deps.EarlySharedLibs
473 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
474 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
475
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100476 var fromPath = in.String()
477 if !filepath.IsAbs(fromPath) {
478 fromPath = "$$PWD/" + fromPath
479 }
480
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100481 ctx.Build(pctx, android.BuildParams{
482 Rule: android.Symlink,
483 Output: outputFile,
484 Input: in,
485 Implicits: sharedLibPaths,
486 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100487 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100488 },
489 })
490
491 p.toolPath = android.OptionalPathForPath(outputFile)
492 } else {
493 if p.stripper.NeedsStrip(ctx) {
494 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
495 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
496 in = stripped
497 }
498
499 // Copy binaries to a name matching the final installed name
500 ctx.Build(pctx, android.BuildParams{
501 Rule: android.CpExecutable,
502 Description: "prebuilt",
503 Output: outputFile,
504 Input: in,
505 })
506 }
Colin Cross94921e72017-08-08 16:20:15 -0700507
508 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700509 }
510
511 return nil
512}
513
Inseob Kim7f283f42020-06-01 21:53:49 +0900514func (p *prebuiltBinaryLinker) binary() bool {
515 return true
516}
517
Patrice Arruda3554a982019-03-27 19:09:10 -0700518// 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 +0000519// device's directory, for both the host and device
520func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700521 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
522 return module.Init()
523}
524
525func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross8ff10582023-12-07 13:10:56 -0800526 module, binary := newBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700527 module.compiler = nil
528
529 prebuilt := &prebuiltBinaryLinker{
530 binaryDecorator: binary,
531 }
532 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100533 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700534
Colin Cross74d73e22017-08-02 11:05:49 -0700535 module.AddProperties(&prebuilt.properties)
536
Cole Faust96a692b2024-08-08 14:47:51 -0700537 android.InitConfigurablePrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700538 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700539}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700540
541type Sanitized struct {
542 None struct {
543 Srcs []string `android:"path,arch_variant"`
544 } `android:"arch_variant"`
545 Address struct {
546 Srcs []string `android:"path,arch_variant"`
547 } `android:"arch_variant"`
548 Hwaddress struct {
549 Srcs []string `android:"path,arch_variant"`
550 } `android:"arch_variant"`
551}
552
553func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
554 if sanitize == nil {
555 return nil
556 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400557 if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700558 return sanitized.Address.Srcs
559 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400560 if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700561 return sanitized.Hwaddress.Srcs
562 }
563 return sanitized.None.Srcs
564}
Spandan Das2b6dfb52024-01-19 00:22:22 +0000565
566func (p *prebuiltLinker) sourceModuleName() string {
567 return proptools.String(p.properties.Source_module_name)
568}