xref: /aosp_15_r20/build/soong/cc/prebuilt.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// 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	"path/filepath"
19
20	"github.com/google/blueprint/depset"
21	"github.com/google/blueprint/proptools"
22
23	"android/soong/android"
24)
25
26func init() {
27	RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
28}
29
30func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
31	ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
32	ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
33	ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
34	ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
35	ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory)
36	ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory)
37}
38
39type prebuiltLinkerInterface interface {
40	Name(string) string
41	prebuilt() *android.Prebuilt
42	sourceModuleName() string
43}
44
45type prebuiltLinkerProperties struct {
46	// 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
51	// a prebuilt library or binary. Can reference a genrule module that generates an executable file.
52	Srcs proptools.Configurable[[]string] `android:"path,arch_variant"`
53
54	Sanitized Sanitized `android:"arch_variant"`
55
56	// Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
57	// symbols, etc), default true.
58	Check_elf_files *bool
59
60	// if set, add an extra objcopy --prefix-symbols= step
61	Prefix_symbols *string
62
63	// 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"`
67}
68
69type prebuiltLinker struct {
70	android.Prebuilt
71
72	properties prebuiltLinkerProperties
73}
74
75func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
76	return &p.Prebuilt
77}
78
79type prebuiltLibraryInterface interface {
80	libraryInterface
81	prebuiltLinkerInterface
82	disablePrebuilt()
83}
84
85type prebuiltLibraryLinker struct {
86	*libraryDecorator
87	prebuiltLinker
88}
89
90var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
91var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
92
93func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
94
95func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
96	return p.libraryDecorator.linkerDeps(ctx, deps)
97}
98
99func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
100	return p.libraryDecorator.linkerProps()
101}
102
103func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
104	flags Flags, deps PathDeps, objs Objects) android.Path {
105
106	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)
114
115	// TODO(ccross): verify shared library dependencies
116	srcs := p.prebuiltSrcs(ctx)
117	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.
122		return p.linkShared(ctx, flags, deps, objs)
123	}
124
125	if len(srcs) > 0 {
126		if len(srcs) > 1 {
127			ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
128			return nil
129		}
130
131		p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
132
133		in := android.PathForModuleSrc(ctx, srcs[0])
134
135		if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
136			prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
137			transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
138				in, flagsToBuilderFlags(flags), prefixed)
139			in = prefixed
140		}
141
142		if p.static() {
143			depSet := depset.NewBuilder[android.Path](depset.TOPOLOGICAL).Direct(in).Build()
144			android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{
145				StaticLibrary: in,
146
147				TransitiveStaticLibrariesForOrdering: depSet,
148			})
149			return in
150		}
151
152		if p.shared() {
153			p.unstrippedOutputFile = in
154			libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
155			outputFile := android.PathForModuleOut(ctx, libName)
156			var implicits android.Paths
157
158			if p.stripper.NeedsStrip(ctx) {
159				stripFlags := flagsToStripFlags(flags)
160				stripped := android.PathForModuleOut(ctx, "stripped", libName)
161				p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
162				in = stripped
163			}
164
165			// Optimize out relinking against shared libraries whose interface hasn't changed by
166			// depending on a table of contents file instead of the library itself.
167			tocFile := android.PathForModuleOut(ctx, libName+".toc")
168			p.tocFile = android.OptionalPathForPath(tocFile)
169			TransformSharedObjectToToc(ctx, outputFile, tocFile)
170
171			if ctx.Windows() && p.properties.Windows_import_lib != nil {
172				// Consumers of this library actually links to the import library in build
173				// time and dynamically links to the DLL in run time. i.e.
174				// a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
175				importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
176				importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
177				importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
178				implicits = append(implicits, importLibOutputFile)
179
180				ctx.Build(pctx, android.BuildParams{
181					Rule:        android.CpExecutable,
182					Description: "prebuilt import library",
183					Input:       importLibSrc,
184					Output:      importLibOutputFile,
185					Args: map[string]string{
186						"cpFlags": "-L",
187					},
188				})
189			}
190
191			ctx.Build(pctx, android.BuildParams{
192				Rule:        android.CpExecutable,
193				Description: "prebuilt shared library",
194				Implicits:   implicits,
195				Input:       in,
196				Output:      outputFile,
197				Args: map[string]string{
198					"cpFlags": "-L",
199				},
200			})
201
202			android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
203				SharedLibrary: outputFile,
204				Target:        ctx.Target(),
205
206				TableOfContents: p.tocFile,
207				IsStubs:         p.buildStubs(),
208			})
209
210			return outputFile
211		}
212	} else if p.shared() && len(stubInfo) > 0 {
213		// This is a prebuilt which does not have any implementation (nil `srcs`), but provides APIs.
214		// Provide the latest (i.e. `current`) stubs to reverse dependencies.
215		latestStub := stubInfo[len(stubInfo)-1].SharedLibraryInfo.SharedLibrary
216		android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
217			SharedLibrary: latestStub,
218			Target:        ctx.Target(),
219			IsStubs:       true,
220		})
221
222		return latestStub
223	}
224
225	if p.header() {
226		android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{})
227
228		// Need to return an output path so that the AndroidMk logic doesn't skip
229		// the prebuilt header. For compatibility, in case Android.mk files use a
230		// header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
231		// placeholder, just like non-prebuilt header modules do in linkStatic().
232		ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
233		transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
234		return ph
235	}
236
237	return nil
238}
239
240func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
241	sanitize := ctx.Module().(*Module).sanitize
242	srcs := p.properties.Srcs.GetOrDefault(ctx, nil)
243	srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
244	if p.static() {
245		srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs.GetOrDefault(ctx, nil)...)
246		srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
247	}
248	if p.shared() {
249		srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs.GetOrDefault(ctx, nil)...)
250		srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
251	}
252	return srcs
253}
254
255func (p *prebuiltLibraryLinker) shared() bool {
256	return p.libraryDecorator.shared()
257}
258
259func (p *prebuiltLibraryLinker) nativeCoverage() bool {
260	return false
261}
262
263func (p *prebuiltLibraryLinker) disablePrebuilt() {
264	p.properties.Srcs = proptools.NewConfigurable[[]string](nil, nil)
265	p.properties.Sanitized.None.Srcs = nil
266	p.properties.Sanitized.Address.Srcs = nil
267	p.properties.Sanitized.Hwaddress.Srcs = nil
268}
269
270// Implements versionedInterface
271func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
272	return android.RemoveOptionalPrebuiltPrefix(name)
273}
274
275func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
276	module, library := NewLibrary(hod)
277
278	prebuilt := &prebuiltLibraryLinker{
279		libraryDecorator: library,
280	}
281	module.compiler = prebuilt
282	module.linker = prebuilt
283	module.library = prebuilt
284
285	module.AddProperties(&prebuilt.properties)
286
287	if srcsProperty == "" {
288		android.InitPrebuiltModuleWithoutSrcs(module)
289	} else {
290		srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
291			return prebuilt.prebuiltSrcs(ctx)
292		}
293
294		android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
295	}
296
297	return module, library
298}
299
300func (p *prebuiltLibraryLinker) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
301	if p.buildStubs() && p.stubsVersion() != "" {
302		return p.compileModuleLibApiStubs(ctx, flags, deps)
303	}
304	return Objects{}
305}
306
307// cc_prebuilt_library installs a precompiled shared library that are
308// listed in the srcs property in the device's directory.
309func PrebuiltLibraryFactory() android.Module {
310	module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
311
312	// Prebuilt shared libraries can be included in APEXes
313	android.InitApexModule(module)
314
315	return module.Init()
316}
317
318// cc_prebuilt_library_shared installs a precompiled shared library that are
319// listed in the srcs property in the device's directory.
320func PrebuiltSharedLibraryFactory() android.Module {
321	module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
322	return module.Init()
323}
324
325// cc_prebuilt_test_library_shared installs a precompiled shared library
326// to be used as a data dependency of a test-related module (such as cc_test, or
327// cc_test_library).
328func PrebuiltSharedTestLibraryFactory() android.Module {
329	module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
330	library.BuildOnlyShared()
331	library.baseInstaller = NewTestInstaller()
332	return module.Init()
333}
334
335func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
336	module, library := NewPrebuiltLibrary(hod, "srcs")
337	library.BuildOnlyShared()
338
339	// Prebuilt shared libraries can be included in APEXes
340	android.InitApexModule(module)
341
342	return module, library
343}
344
345// cc_prebuilt_library_static installs a precompiled static library that are
346// listed in the srcs property in the device's directory.
347func PrebuiltStaticLibraryFactory() android.Module {
348	module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
349	return module.Init()
350}
351
352func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
353	module, library := NewPrebuiltLibrary(hod, "srcs")
354	library.BuildOnlyStatic()
355
356	return module, library
357}
358
359type prebuiltObjectProperties struct {
360	// Name of the source soong module that gets shadowed by this prebuilt
361	// If unspecified, follows the naming convention that the source module of
362	// the prebuilt is Name() without "prebuilt_" prefix
363	Source_module_name *string
364	Srcs               []string `android:"path,arch_variant"`
365}
366
367type prebuiltObjectLinker struct {
368	android.Prebuilt
369	objectLinker
370
371	properties prebuiltObjectProperties
372}
373
374func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
375	return &p.Prebuilt
376}
377
378func (p *prebuiltObjectLinker) sourceModuleName() string {
379	return proptools.String(p.properties.Source_module_name)
380}
381
382var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
383
384func (p *prebuiltObjectLinker) link(ctx ModuleContext,
385	flags Flags, deps PathDeps, objs Objects) android.Path {
386	if len(p.properties.Srcs) > 0 {
387		// Copy objects to a name matching the final installed name
388		in := p.Prebuilt.SingleSourcePath(ctx)
389		outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
390		ctx.Build(pctx, android.BuildParams{
391			Rule:        android.CpExecutable,
392			Description: "prebuilt",
393			Output:      outputFile,
394			Input:       in,
395		})
396		return outputFile
397	}
398	return nil
399}
400
401func (p *prebuiltObjectLinker) object() bool {
402	return true
403}
404
405func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
406	module := newObject(hod)
407	prebuilt := &prebuiltObjectLinker{
408		objectLinker: objectLinker{
409			baseLinker: NewBaseLinker(nil),
410		},
411	}
412	module.linker = prebuilt
413	module.AddProperties(&prebuilt.properties)
414	android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
415	return module
416}
417
418func PrebuiltObjectFactory() android.Module {
419	module := NewPrebuiltObject(android.HostAndDeviceSupported)
420	return module.Init()
421}
422
423type prebuiltBinaryLinker struct {
424	*binaryDecorator
425	prebuiltLinker
426
427	toolPath android.OptionalPath
428}
429
430var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
431
432func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
433	return p.toolPath
434}
435
436func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
437	flags Flags, deps PathDeps, objs Objects) android.Path {
438	// TODO(ccross): verify shared library dependencies
439	if len(p.properties.Srcs.GetOrDefault(ctx, nil)) > 0 {
440		fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
441		in := p.Prebuilt.SingleSourcePath(ctx)
442		outputFile := android.PathForModuleOut(ctx, fileName)
443		p.unstrippedOutputFile = in
444
445		if ctx.Host() {
446			// Host binaries are symlinked to their prebuilt source locations. That
447			// way they are executed directly from there so the linker resolves their
448			// shared library dependencies relative to that location (using
449			// $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
450			// repository can supply the expected versions of the shared libraries
451			// without interference from what is in the out tree.
452
453			// These shared lib paths may point to copies of the libs in
454			// .intermediates, which isn't where the binary will load them from, but
455			// it's fine for dependency tracking. If a library dependency is updated,
456			// the symlink will get a new timestamp, along with any installed symlinks
457			// handled in make.
458			sharedLibPaths := deps.EarlySharedLibs
459			sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
460			sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
461
462			var fromPath = in.String()
463			if !filepath.IsAbs(fromPath) {
464				fromPath = "$$PWD/" + fromPath
465			}
466
467			ctx.Build(pctx, android.BuildParams{
468				Rule:      android.Symlink,
469				Output:    outputFile,
470				Input:     in,
471				Implicits: sharedLibPaths,
472				Args: map[string]string{
473					"fromPath": fromPath,
474				},
475			})
476
477			p.toolPath = android.OptionalPathForPath(outputFile)
478		} else {
479			if p.stripper.NeedsStrip(ctx) {
480				stripped := android.PathForModuleOut(ctx, "stripped", fileName)
481				p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
482				in = stripped
483			}
484
485			// Copy binaries to a name matching the final installed name
486			ctx.Build(pctx, android.BuildParams{
487				Rule:        android.CpExecutable,
488				Description: "prebuilt",
489				Output:      outputFile,
490				Input:       in,
491			})
492		}
493
494		return outputFile
495	}
496
497	return nil
498}
499
500func (p *prebuiltBinaryLinker) binary() bool {
501	return true
502}
503
504// cc_prebuilt_binary installs a precompiled executable in srcs property in the
505// device's directory, for both the host and device
506func PrebuiltBinaryFactory() android.Module {
507	module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
508	return module.Init()
509}
510
511func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
512	module, binary := newBinary(hod)
513	module.compiler = nil
514
515	prebuilt := &prebuiltBinaryLinker{
516		binaryDecorator: binary,
517	}
518	module.linker = prebuilt
519	module.installer = prebuilt
520
521	module.AddProperties(&prebuilt.properties)
522
523	android.InitConfigurablePrebuiltModule(module, &prebuilt.properties.Srcs)
524	return module, binary
525}
526
527type Sanitized struct {
528	None struct {
529		Srcs []string `android:"path,arch_variant"`
530	} `android:"arch_variant"`
531	Address struct {
532		Srcs []string `android:"path,arch_variant"`
533	} `android:"arch_variant"`
534	Hwaddress struct {
535		Srcs []string `android:"path,arch_variant"`
536	} `android:"arch_variant"`
537}
538
539func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
540	if sanitize == nil {
541		return nil
542	}
543	if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
544		return sanitized.Address.Srcs
545	}
546	if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
547		return sanitized.Hwaddress.Srcs
548	}
549	return sanitized.None.Srcs
550}
551
552func (p *prebuiltLinker) sourceModuleName() string {
553	return proptools.String(p.properties.Source_module_name)
554}
555