xref: /aosp_15_r20/build/soong/python/binary.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2017 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 python
16
17// This file contains the module types for building Python binary.
18
19import (
20	"fmt"
21	"path/filepath"
22	"strings"
23
24	"android/soong/android"
25)
26
27func init() {
28	registerPythonBinaryComponents(android.InitRegistrationContext)
29}
30
31func registerPythonBinaryComponents(ctx android.RegistrationContext) {
32	ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory)
33}
34
35type BinaryProperties struct {
36	// the name of the source file that is the main entry point of the program.
37	// this file must also be listed in srcs.
38	// If left unspecified, module name is used instead.
39	// If name doesn’t match any filename in srcs, main must be specified.
40	Main *string
41
42	// set the name of the output binary.
43	Stem *string `android:"arch_variant"`
44
45	// append to the name of the output binary.
46	Suffix *string `android:"arch_variant"`
47
48	// list of compatibility suites (for example "cts", "vts") that the module should be
49	// installed into.
50	Test_suites []string `android:"arch_variant"`
51
52	// whether to use `main` when starting the executable. The default is true, when set to
53	// false it will act much like the normal `python` executable, but with the sources and
54	// libraries automatically included in the PYTHONPATH.
55	Autorun *bool `android:"arch_variant"`
56
57	// Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
58	// doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
59	// explicitly.
60	Auto_gen_config *bool
61}
62
63type PythonBinaryModule struct {
64	PythonLibraryModule
65	binaryProperties BinaryProperties
66
67	// (.intermediate) module output path as installation source.
68	installSource android.Path
69
70	// Final installation path.
71	installedDest android.Path
72
73	androidMkSharedLibs []string
74}
75
76var _ android.AndroidMkEntriesProvider = (*PythonBinaryModule)(nil)
77var _ android.Module = (*PythonBinaryModule)(nil)
78
79type IntermPathProvider interface {
80	IntermPathForModuleOut() android.OptionalPath
81}
82
83func NewBinary(hod android.HostOrDeviceSupported) *PythonBinaryModule {
84	return &PythonBinaryModule{
85		PythonLibraryModule: *newModule(hod, android.MultilibFirst),
86	}
87}
88
89func PythonBinaryHostFactory() android.Module {
90	return NewBinary(android.HostSupported).init()
91}
92
93func (p *PythonBinaryModule) init() android.Module {
94	p.AddProperties(&p.properties, &p.protoProperties)
95	p.AddProperties(&p.binaryProperties)
96	android.InitAndroidArchModule(p, p.hod, p.multilib)
97	android.InitDefaultableModule(p)
98	return p
99}
100
101func (p *PythonBinaryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
102	p.PythonLibraryModule.GenerateAndroidBuildActions(ctx)
103	p.buildBinary(ctx)
104	p.installedDest = ctx.InstallFile(installDir(ctx, "bin", "", ""),
105		p.installSource.Base(), p.installSource)
106	ctx.SetOutputFiles(android.Paths{p.installSource}, "")
107}
108
109func (p *PythonBinaryModule) buildBinary(ctx android.ModuleContext) {
110	embeddedLauncher := p.isEmbeddedLauncherEnabled()
111	depsSrcsZips := p.collectPathsFromTransitiveDeps(ctx, embeddedLauncher)
112	main := ""
113	if p.autorun() {
114		main = p.getPyMainFile(ctx, p.srcsPathMappings)
115	}
116
117	var launcherPath android.OptionalPath
118	if embeddedLauncher {
119		ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) {
120			if provider, ok := m.(IntermPathProvider); ok {
121				if launcherPath.Valid() {
122					panic(fmt.Errorf("launcher path was found before: %q",
123						launcherPath))
124				}
125				launcherPath = provider.IntermPathForModuleOut()
126			}
127		})
128	}
129	srcsZips := make(android.Paths, 0, len(depsSrcsZips)+1)
130	if embeddedLauncher {
131		srcsZips = append(srcsZips, p.precompiledSrcsZip)
132	} else {
133		srcsZips = append(srcsZips, p.srcsZip)
134	}
135	srcsZips = append(srcsZips, depsSrcsZips...)
136	p.installSource = registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath,
137		p.getHostInterpreterName(ctx, p.properties.Actual_version),
138		main, p.getStem(ctx), srcsZips)
139
140	var sharedLibs []string
141	// if embedded launcher is enabled, we need to collect the shared library dependencies of the
142	// launcher
143	for _, dep := range ctx.GetDirectDepsWithTag(launcherSharedLibTag) {
144		sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep))
145	}
146	p.androidMkSharedLibs = sharedLibs
147}
148
149func (p *PythonBinaryModule) AndroidMkEntries() []android.AndroidMkEntries {
150	entries := android.AndroidMkEntries{OutputFile: android.OptionalPathForPath(p.installSource)}
151
152	entries.Class = "EXECUTABLES"
153
154	entries.ExtraEntries = append(entries.ExtraEntries,
155		func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
156			entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...)
157		})
158
159	entries.Required = append(entries.Required, "libc++")
160	entries.ExtraEntries = append(entries.ExtraEntries,
161		func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
162			path, file := filepath.Split(p.installedDest.String())
163			stem := strings.TrimSuffix(file, filepath.Ext(file))
164
165			entries.SetString("LOCAL_MODULE_SUFFIX", filepath.Ext(file))
166			entries.SetString("LOCAL_MODULE_PATH", path)
167			entries.SetString("LOCAL_MODULE_STEM", stem)
168			entries.AddStrings("LOCAL_SHARED_LIBRARIES", p.androidMkSharedLibs...)
169			entries.SetBool("LOCAL_CHECK_ELF_FILES", false)
170		})
171
172	return []android.AndroidMkEntries{entries}
173}
174
175func (p *PythonBinaryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
176	p.PythonLibraryModule.DepsMutator(ctx)
177
178	if p.isEmbeddedLauncherEnabled() {
179		p.AddDepsOnPythonLauncherAndStdlib(ctx, pythonLibTag, launcherTag, launcherSharedLibTag, p.autorun(), ctx.Target())
180	}
181}
182
183// HostToolPath returns a path if appropriate such that this module can be used as a host tool,
184// fulfilling the android.HostToolProvider interface.
185func (p *PythonBinaryModule) HostToolPath() android.OptionalPath {
186	// TODO: This should only be set when building host binaries -- tests built for device would be
187	// setting this incorrectly.
188	return android.OptionalPathForPath(p.installedDest)
189}
190
191func (p *PythonBinaryModule) isEmbeddedLauncherEnabled() bool {
192	return BoolDefault(p.properties.Embedded_launcher, true)
193}
194
195func (b *PythonBinaryModule) autorun() bool {
196	return BoolDefault(b.binaryProperties.Autorun, true)
197}
198
199// get host interpreter name.
200func (p *PythonBinaryModule) getHostInterpreterName(ctx android.ModuleContext,
201	actualVersion string) string {
202	var interp string
203	switch actualVersion {
204	case pyVersion2:
205		interp = "python2.7"
206	case pyVersion3:
207		interp = "python3"
208	default:
209		panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.",
210			actualVersion, ctx.ModuleName()))
211	}
212
213	return interp
214}
215
216// find main program path within runfiles tree.
217func (p *PythonBinaryModule) getPyMainFile(ctx android.ModuleContext,
218	srcsPathMappings []pathMapping) string {
219	var main string
220	if String(p.binaryProperties.Main) == "" {
221		main = ctx.ModuleName() + pyExt
222	} else {
223		main = String(p.binaryProperties.Main)
224	}
225
226	for _, path := range srcsPathMappings {
227		if main == path.src.Rel() {
228			return path.dest
229		}
230	}
231	ctx.PropertyErrorf("main", "%q is not listed in srcs.", main)
232
233	return ""
234}
235
236func (p *PythonBinaryModule) getStem(ctx android.ModuleContext) string {
237	stem := ctx.ModuleName()
238	if String(p.binaryProperties.Stem) != "" {
239		stem = String(p.binaryProperties.Stem)
240	}
241
242	return stem + String(p.binaryProperties.Suffix)
243}
244
245func installDir(ctx android.ModuleContext, dir, dir64, relative string) android.InstallPath {
246	if ctx.Arch().ArchType.Multilib == "lib64" && dir64 != "" {
247		dir = dir64
248	}
249	if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
250		dir = filepath.Join(dir, ctx.Arch().ArchType.String())
251	}
252	return android.PathForModuleInstall(ctx, dir, relative)
253}
254