blob: be9411bbf22569273ba4f33d35cf6293fb8a20b9 [file] [log] [blame]
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001// 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 "Base" module type for building Python program.
18
19import (
20 "fmt"
21 "path/filepath"
22 "regexp"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080023 "strings"
24
25 "github.com/google/blueprint"
Nan Zhangd4e641b2017-07-12 12:55:28 -070026 "github.com/google/blueprint/proptools"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080027
28 "android/soong/android"
29)
30
31func init() {
Paul Duffind0890452021-03-17 21:57:08 +000032 registerPythonMutators(android.InitRegistrationContext)
33}
34
35func registerPythonMutators(ctx android.RegistrationContext) {
36 ctx.PreDepsMutators(RegisterPythonPreDepsMutators)
Liz Kammerdd849a82020-06-12 16:38:45 -070037}
38
Liz Kammerd737d022020-11-16 15:42:51 -080039// Exported to support other packages using Python modules in tests.
Liz Kammerdd849a82020-06-12 16:38:45 -070040func RegisterPythonPreDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Crosse0771282024-05-21 15:19:18 -070041 ctx.Transition("python_version", &versionSplitTransitionMutator{})
Nan Zhangdb0b9a32017-02-27 10:12:13 -080042}
43
Liz Kammerd737d022020-11-16 15:42:51 -080044// the version-specific properties that apply to python modules.
Nan Zhangd4e641b2017-07-12 12:55:28 -070045type VersionProperties struct {
Liz Kammerd737d022020-11-16 15:42:51 -080046 // whether the module is required to be built with this version.
47 // Defaults to true for Python 3, and false otherwise.
Liz Kammer59c0eae2021-09-17 17:48:05 -040048 Enabled *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -080049
Liz Kammerd737d022020-11-16 15:42:51 -080050 // list of source files specific to this Python version.
51 // Using the syntax ":module", srcs may reference the outputs of other modules that produce source files,
52 // e.g. genrule or filegroup.
Colin Cross27b922f2019-03-04 22:35:41 -080053 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070054
Liz Kammerd737d022020-11-16 15:42:51 -080055 // list of source files that should not be used to build the Python module for this version.
56 // This is most useful to remove files that are not common to all Python versions.
Colin Cross27b922f2019-03-04 22:35:41 -080057 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080058
Liz Kammerd737d022020-11-16 15:42:51 -080059 // list of the Python libraries used only for this Python version.
Nan Zhangd4e641b2017-07-12 12:55:28 -070060 Libs []string `android:"arch_variant"`
61
Cole Faustf09101e2024-04-18 18:33:15 +000062 // whether the binary is required to be built with embedded launcher for this version, defaults to true.
Liz Kammer59c0eae2021-09-17 17:48:05 -040063 Embedded_launcher *bool // TODO(b/174041232): Remove this property
Nan Zhangdb0b9a32017-02-27 10:12:13 -080064}
65
Liz Kammerd737d022020-11-16 15:42:51 -080066// properties that apply to all python modules
Nan Zhangd4e641b2017-07-12 12:55:28 -070067type BaseProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080068 // the package path prefix within the output artifact at which to place the source/data
69 // files of the current module.
70 // eg. Pkg_path = "a/b/c"; Other packages can reference this module by using
71 // (from a.b.c import ...) statement.
Nan Zhangbea09752018-05-31 12:49:33 -070072 // if left unspecified, all the source/data files path is unchanged within zip file.
Liz Kammer59c0eae2021-09-17 17:48:05 -040073 Pkg_path *string
Nan Zhangd4e641b2017-07-12 12:55:28 -070074
75 // true, if the Python module is used internally, eg, Python std libs.
Liz Kammer59c0eae2021-09-17 17:48:05 -040076 Is_internal *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -080077
78 // list of source (.py) files compatible both with Python2 and Python3 used to compile the
79 // Python module.
80 // srcs may reference the outputs of other modules that produce source files like genrule
81 // or filegroup using the syntax ":module".
82 // Srcs has to be non-empty.
Colin Cross27b922f2019-03-04 22:35:41 -080083 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070084
85 // list of source files that should not be used to build the C/C++ module.
86 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080087 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080088
89 // list of files or filegroup modules that provide data that should be installed alongside
90 // the test. the file extension can be arbitrary except for (.py).
Colin Cross27b922f2019-03-04 22:35:41 -080091 Data []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080092
Cole Faust65cb40a2024-10-21 15:41:42 -070093 // Same as data, but will add dependencies on modules using the device's os variation and
94 // the common arch variation. Useful for a host test that wants to embed a module built for
95 // device.
96 Device_common_data []string `android:"path_device_common"`
97
Colin Cross1bc63932020-11-22 20:12:45 -080098 // list of java modules that provide data that should be installed alongside the test.
99 Java_data []string
100
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800101 // list of the Python libraries compatible both with Python2 and Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700102 Libs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800103
104 Version struct {
Liz Kammerd737d022020-11-16 15:42:51 -0800105 // Python2-specific properties, including whether Python2 is supported for this module
106 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700107 Py2 VersionProperties `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800108
Liz Kammerd737d022020-11-16 15:42:51 -0800109 // Python3-specific properties, including whether Python3 is supported for this module
110 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700111 Py3 VersionProperties `android:"arch_variant"`
112 } `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800113
114 // the actual version each module uses after variations created.
115 // this property name is hidden from users' perspectives, and soong will populate it during
116 // runtime.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700117 Actual_version string `blueprint:"mutated"`
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700118
Liz Kammerd737d022020-11-16 15:42:51 -0800119 // whether the module is required to be built with actual_version.
120 // this is set by the python version mutator based on version-specific properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700121 Enabled *bool `blueprint:"mutated"`
122
Liz Kammerd737d022020-11-16 15:42:51 -0800123 // whether the binary is required to be built with embedded launcher for this actual_version.
124 // this is set by the python version mutator based on version-specific properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700125 Embedded_launcher *bool `blueprint:"mutated"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800126}
127
Liz Kammerd737d022020-11-16 15:42:51 -0800128// Used to store files of current module after expanding dependencies
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800129type pathMapping struct {
130 dest string
131 src android.Path
132}
133
Cole Faust4d247e62023-01-23 10:14:58 -0800134type PythonLibraryModule struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800135 android.ModuleBase
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700136 android.DefaultableModuleBase
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800137
Nan Zhangb8fa1972017-12-22 16:12:00 -0800138 properties BaseProperties
139 protoProperties android.ProtoProperties
Nan Zhangd4e641b2017-07-12 12:55:28 -0700140
141 // initialize before calling Init
142 hod android.HostOrDeviceSupported
143 multilib android.Multilib
144
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800145 // the Python files of current module after expanding source dependencies.
146 // pathMapping: <dest: runfile_path, src: source_path>
147 srcsPathMappings []pathMapping
148
149 // the data files of current module after expanding source dependencies.
150 // pathMapping: <dest: runfile_path, src: source_path>
151 dataPathMappings []pathMapping
152
Cole Faust5c503d12023-01-24 11:48:08 -0800153 // The zip file containing the current module's source/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800154 srcsZip android.Path
Cole Faust5c503d12023-01-24 11:48:08 -0800155
156 // The zip file containing the current module's source/data files, with the
157 // source files precompiled.
158 precompiledSrcsZip android.Path
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700159
160 sourceProperties android.SourceProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800161}
162
Liz Kammerd737d022020-11-16 15:42:51 -0800163// newModule generates new Python base module
Cole Faust4d247e62023-01-23 10:14:58 -0800164func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *PythonLibraryModule {
165 return &PythonLibraryModule{
Nan Zhangd4e641b2017-07-12 12:55:28 -0700166 hod: hod,
167 multilib: multilib,
168 }
169}
170
Liz Kammerd737d022020-11-16 15:42:51 -0800171// interface implemented by Python modules to provide source and data mappings and zip to python
172// modules that depend on it
173type pythonDependency interface {
174 getSrcsPathMappings() []pathMapping
175 getDataPathMappings() []pathMapping
176 getSrcsZip() android.Path
Cole Faust5c503d12023-01-24 11:48:08 -0800177 getPrecompiledSrcsZip() android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400178 getPkgPath() string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800179}
180
Liz Kammerd737d022020-11-16 15:42:51 -0800181// getSrcsPathMappings gets this module's path mapping of src source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800182func (p *PythonLibraryModule) getSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800183 return p.srcsPathMappings
184}
185
Liz Kammerd737d022020-11-16 15:42:51 -0800186// getSrcsPathMappings gets this module's path mapping of data source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800187func (p *PythonLibraryModule) getDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800188 return p.dataPathMappings
189}
190
Liz Kammerd737d022020-11-16 15:42:51 -0800191// getSrcsZip returns the filepath where the current module's source/data files are zipped.
Cole Faust4d247e62023-01-23 10:14:58 -0800192func (p *PythonLibraryModule) getSrcsZip() android.Path {
Nan Zhang1db85402017-12-18 13:20:23 -0800193 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800194}
195
Cole Faust5c503d12023-01-24 11:48:08 -0800196// getSrcsZip returns the filepath where the current module's source/data files are zipped.
197func (p *PythonLibraryModule) getPrecompiledSrcsZip() android.Path {
198 return p.precompiledSrcsZip
199}
200
Dan Willemsen339a63f2023-08-15 22:17:03 -0400201// getPkgPath returns the pkg_path value
202func (p *PythonLibraryModule) getPkgPath() string {
203 return String(p.properties.Pkg_path)
204}
205
Cole Faust4d247e62023-01-23 10:14:58 -0800206func (p *PythonLibraryModule) getBaseProperties() *BaseProperties {
207 return &p.properties
208}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800209
Cole Faust4d247e62023-01-23 10:14:58 -0800210var _ pythonDependency = (*PythonLibraryModule)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800211
Cole Faust4d247e62023-01-23 10:14:58 -0800212func (p *PythonLibraryModule) init() android.Module {
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700213 p.AddProperties(&p.properties, &p.protoProperties, &p.sourceProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700214 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700215 android.InitDefaultableModule(p)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700216 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800217}
218
Liz Kammerd737d022020-11-16 15:42:51 -0800219// Python-specific tag to transfer information on the purpose of a dependency.
220// This is used when adding a dependency on a module, which can later be accessed when visiting
221// dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700222type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800223 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700224 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800225}
226
Liz Kammerd737d022020-11-16 15:42:51 -0800227// Python-specific tag that indicates that installed files of this module should depend on installed
228// files of the dependency
Colin Crosse9fe2942020-11-10 18:12:15 -0800229type installDependencyTag struct {
230 blueprint.BaseDependencyTag
Liz Kammerd737d022020-11-16 15:42:51 -0800231 // embedding this struct provides the installation dependency requirement
Colin Crosse9fe2942020-11-10 18:12:15 -0800232 android.InstallAlwaysNeededDependencyTag
233 name string
234}
235
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800236var (
Cole Faust5c503d12023-01-24 11:48:08 -0800237 pythonLibTag = dependencyTag{name: "pythonLib"}
238 javaDataTag = dependencyTag{name: "javaData"}
239 // The python interpreter, with soong module name "py3-launcher" or "py3-launcher-autorun".
Cole Faust909d2372023-02-13 23:17:40 +0000240 launcherTag = dependencyTag{name: "launcher"}
241 launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"}
Cole Faust5c503d12023-01-24 11:48:08 -0800242 // The python interpreter built for host so that we can precompile python sources.
243 // This only works because the precompiled sources don't vary by architecture.
244 // The soong module name is "py3-launcher".
Cole Faust909d2372023-02-13 23:17:40 +0000245 hostLauncherTag = dependencyTag{name: "hostLauncher"}
246 hostlauncherSharedLibTag = dependencyTag{name: "hostlauncherSharedLib"}
247 hostStdLibTag = dependencyTag{name: "hostStdLib"}
248 pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
249 pyExt = ".py"
250 protoExt = ".proto"
251 pyVersion2 = "PY2"
252 pyVersion3 = "PY3"
253 internalPath = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800254)
255
Cole Faust4d247e62023-01-23 10:14:58 -0800256type basePropertiesProvider interface {
257 getBaseProperties() *BaseProperties
258}
259
Colin Crosse0771282024-05-21 15:19:18 -0700260type versionSplitTransitionMutator struct{}
261
262func (versionSplitTransitionMutator) Split(ctx android.BaseModuleContext) []string {
263 if base, ok := ctx.Module().(basePropertiesProvider); ok {
264 props := base.getBaseProperties()
265 var variants []string
266 // PY3 is first so that we alias the PY3 variant rather than PY2 if both
267 // are available
268 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
269 variants = append(variants, pyVersion3)
270 }
271 if proptools.BoolDefault(props.Version.Py2.Enabled, false) {
Cole Faust4ce4f882024-09-09 18:08:49 -0700272 if ctx.ModuleName() != "py2-cmd" &&
Colin Crosse0771282024-05-21 15:19:18 -0700273 ctx.ModuleName() != "py2-stdlib" {
Cole Faust4ce4f882024-09-09 18:08:49 -0700274 ctx.PropertyErrorf("version.py2.enabled", "Python 2 is no longer supported, please convert to python 3.")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800275 }
Colin Crosse0771282024-05-21 15:19:18 -0700276 variants = append(variants, pyVersion2)
277 }
278 return variants
279 }
280 return []string{""}
281}
282
283func (versionSplitTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
284 return ""
285}
286
287func (versionSplitTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
288 if incomingVariation != "" {
289 return incomingVariation
290 }
291 if base, ok := ctx.Module().(basePropertiesProvider); ok {
292 props := base.getBaseProperties()
293 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
294 return pyVersion3
295 } else {
296 return pyVersion2
297 }
298 }
299
300 return ""
301}
302
303func (versionSplitTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
304 if variation == "" {
305 return
306 }
307 if base, ok := ctx.Module().(basePropertiesProvider); ok {
308 props := base.getBaseProperties()
309 props.Actual_version = variation
310
311 var versionProps *VersionProperties
312 if variation == pyVersion3 {
313 versionProps = &props.Version.Py3
314 } else if variation == pyVersion2 {
315 versionProps = &props.Version.Py2
316 }
317
318 err := proptools.AppendMatchingProperties([]interface{}{props}, versionProps, nil)
319 if err != nil {
320 panic(err)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800321 }
322 }
323}
324
Liz Kammerd737d022020-11-16 15:42:51 -0800325func anyHasExt(paths []string, ext string) bool {
326 for _, p := range paths {
327 if filepath.Ext(p) == ext {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800328 return true
329 }
330 }
331
332 return false
333}
334
Cole Faust4d247e62023-01-23 10:14:58 -0800335func (p *PythonLibraryModule) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800336 return anyHasExt(p.properties.Srcs, ext)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800337}
338
Liz Kammerd737d022020-11-16 15:42:51 -0800339// DepsMutator mutates dependencies for this module:
Colin Crossd079e0b2022-08-16 10:27:33 -0700340// - handles proto dependencies,
341// - if required, specifies launcher and adds launcher dependencies,
342// - applies python version mutations to Python dependencies
Cole Faust4d247e62023-01-23 10:14:58 -0800343func (p *PythonLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700344 android.ProtoDeps(ctx, &p.protoProperties)
345
Colin Crosse20113d2020-11-22 19:37:44 -0800346 versionVariation := []blueprint.Variation{
347 {"python_version", p.properties.Actual_version},
Nan Zhangb8fa1972017-12-22 16:12:00 -0800348 }
Colin Crosse20113d2020-11-22 19:37:44 -0800349
Liz Kammerd737d022020-11-16 15:42:51 -0800350 // If sources contain a proto file, add dependency on libprotobuf-python
351 if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
Colin Crosse20113d2020-11-22 19:37:44 -0800352 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python")
353 }
Liz Kammerd737d022020-11-16 15:42:51 -0800354
355 // Add python library dependencies for this python version variation
Colin Crosse20113d2020-11-22 19:37:44 -0800356 ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700357
Colin Cross1bc63932020-11-22 20:12:45 -0800358 // Emulate the data property for java_data but with the arch variation overridden to "common"
359 // so that it can point to java modules.
360 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
361 ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
Cole Faust5c503d12023-01-24 11:48:08 -0800362
Cole Faust909d2372023-02-13 23:17:40 +0000363 p.AddDepsOnPythonLauncherAndStdlib(ctx, hostStdLibTag, hostLauncherTag, hostlauncherSharedLibTag, false, ctx.Config().BuildOSTarget)
Cole Faust5c503d12023-01-24 11:48:08 -0800364}
365
Cole Faust909d2372023-02-13 23:17:40 +0000366// AddDepsOnPythonLauncherAndStdlib will make the current module depend on the python stdlib,
367// launcher (interpreter), and the launcher's shared libraries. If autorun is true, it will use
368// the autorun launcher instead of the regular one. This function acceps a targetForDeps argument
369// as the target to use for these dependencies. For embedded launcher python binaries, the launcher
370// that will be embedded will be under the same target as the python module itself. But when
371// precompiling python code, we need to get the python launcher built for host, even if we're
372// compiling the python module for device, so we pass a different target to this function.
Cole Faust5c503d12023-01-24 11:48:08 -0800373func (p *PythonLibraryModule) AddDepsOnPythonLauncherAndStdlib(ctx android.BottomUpMutatorContext,
Cole Faust909d2372023-02-13 23:17:40 +0000374 stdLibTag, launcherTag, launcherSharedLibTag blueprint.DependencyTag,
Cole Faust5c503d12023-01-24 11:48:08 -0800375 autorun bool, targetForDeps android.Target) {
376 var stdLib string
377 var launcherModule string
Cole Faust909d2372023-02-13 23:17:40 +0000378 // Add launcher shared lib dependencies. Ideally, these should be
379 // derived from the `shared_libs` property of the launcher. TODO: read these from
380 // the python launcher itself using ctx.OtherModuleProvider() or similar on the result
381 // of ctx.AddFarVariationDependencies()
382 launcherSharedLibDeps := []string{
383 "libsqlite",
384 }
385 // Add launcher-specific dependencies for bionic
386 if targetForDeps.Os.Bionic() {
387 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm")
388 }
389 if targetForDeps.Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() {
390 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc_musl")
391 }
Cole Faust5c503d12023-01-24 11:48:08 -0800392
393 switch p.properties.Actual_version {
394 case pyVersion2:
395 stdLib = "py2-stdlib"
396
397 launcherModule = "py2-launcher"
398 if autorun {
399 launcherModule = "py2-launcher-autorun"
400 }
401
Cole Faust909d2372023-02-13 23:17:40 +0000402 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++")
Cole Faust5c503d12023-01-24 11:48:08 -0800403 case pyVersion3:
Dan Willemsen339a63f2023-08-15 22:17:03 -0400404 var prebuiltStdLib bool
405 if targetForDeps.Os.Bionic() {
406 prebuiltStdLib = false
407 } else if ctx.Config().VendorConfig("cpython3").Bool("force_build_host") {
408 prebuiltStdLib = false
409 } else {
410 prebuiltStdLib = true
411 }
412
413 if prebuiltStdLib {
414 stdLib = "py3-stdlib-prebuilt"
415 } else {
416 stdLib = "py3-stdlib"
417 }
Cole Faust5c503d12023-01-24 11:48:08 -0800418
419 launcherModule = "py3-launcher"
420 if autorun {
421 launcherModule = "py3-launcher-autorun"
422 }
423 if ctx.Config().HostStaticBinaries() && targetForDeps.Os == android.LinuxMusl {
424 launcherModule += "-static"
425 }
Cole Faust909d2372023-02-13 23:17:40 +0000426 if ctx.Device() {
427 launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog")
428 }
Cole Faust5c503d12023-01-24 11:48:08 -0800429 default:
430 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
431 p.properties.Actual_version, ctx.ModuleName()))
432 }
433 targetVariations := targetForDeps.Variations()
434 if ctx.ModuleName() != stdLib {
435 stdLibVariations := make([]blueprint.Variation, 0, len(targetVariations)+1)
436 stdLibVariations = append(stdLibVariations, blueprint.Variation{Mutator: "python_version", Variation: p.properties.Actual_version})
437 stdLibVariations = append(stdLibVariations, targetVariations...)
438 // Using AddFarVariationDependencies for all of these because they can be for a different
439 // platform, like if the python module itself was being compiled for device, we may want
440 // the python interpreter built for host so that we can precompile python sources.
441 ctx.AddFarVariationDependencies(stdLibVariations, stdLibTag, stdLib)
442 }
443 ctx.AddFarVariationDependencies(targetVariations, launcherTag, launcherModule)
Cole Faust909d2372023-02-13 23:17:40 +0000444 ctx.AddFarVariationDependencies(targetVariations, launcherSharedLibTag, launcherSharedLibDeps...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800445}
446
Cole Faust4d247e62023-01-23 10:14:58 -0800447// GenerateAndroidBuildActions performs build actions common to all Python modules
448func (p *PythonLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammerd737d022020-11-16 15:42:51 -0800449 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs)
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700450 // Keep before any early returns.
451 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
452 TestOnly: Bool(p.sourceProperties.Test_only),
453 TopLevelTarget: p.sourceProperties.Top_level_test_target,
454 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800455
456 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800457 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700458 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, p.properties.Device_common_data)...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800459
Colin Cross1bc63932020-11-22 20:12:45 -0800460 // Emulate the data property for java_data dependencies.
461 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
462 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
463 }
464
Liz Kammerd737d022020-11-16 15:42:51 -0800465 // Validate pkg_path property
Nan Zhang1db85402017-12-18 13:20:23 -0800466 pkgPath := String(p.properties.Pkg_path)
467 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800468 // TODO: export validation from android/paths.go handling to replace this duplicated functionality
Nan Zhang1db85402017-12-18 13:20:23 -0800469 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
470 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
471 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700472 ctx.PropertyErrorf("pkg_path",
473 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800474 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700475 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800476 }
Liz Kammerd737d022020-11-16 15:42:51 -0800477 }
478 // If property Is_internal is set, prepend pkgPath with internalPath
479 if proptools.BoolDefault(p.properties.Is_internal, false) {
480 pkgPath = filepath.Join(internalPath, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800481 }
482
Liz Kammerd737d022020-11-16 15:42:51 -0800483 // generate src:destination path mappings for this module
Nan Zhang1db85402017-12-18 13:20:23 -0800484 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800485
Liz Kammerd737d022020-11-16 15:42:51 -0800486 // generate the zipfile of all source and data files
Nan Zhang1db85402017-12-18 13:20:23 -0800487 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Dan Willemsenfe2dafc2023-08-24 22:59:16 +0000488 p.precompiledSrcsZip = p.precompileSrcs(ctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800489}
490
Liz Kammerd737d022020-11-16 15:42:51 -0800491func isValidPythonPath(path string) error {
492 identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/")
493 for _, token := range identifiers {
494 if !pathComponentRegexp.MatchString(token) {
495 return fmt.Errorf("the path %q contains invalid subpath %q. "+
496 "Subpaths must be at least one character long. "+
497 "The first character must an underscore or letter. "+
498 "Following characters may be any of: letter, digit, underscore, hyphen.",
499 path, token)
500 }
501 }
502 return nil
503}
504
505// For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path>
506// for python/data files expanded from properties.
Cole Faust4d247e62023-01-23 10:14:58 -0800507func (p *PythonLibraryModule) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800508 expandedSrcs, expandedData android.Paths) {
509 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800510 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800511 destToPySrcs := make(map[string]string)
512 destToPyData := make(map[string]string)
513
Dan Willemsen339a63f2023-08-15 22:17:03 -0400514 // Disable path checks for the stdlib, as it includes a "." in the version string
515 isInternal := proptools.BoolDefault(p.properties.Is_internal, false)
516
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800517 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800518 if s.Ext() != pyExt && s.Ext() != protoExt {
519 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800520 continue
521 }
Nan Zhang1db85402017-12-18 13:20:23 -0800522 runfilesPath := filepath.Join(pkgPath, s.Rel())
Dan Willemsen339a63f2023-08-15 22:17:03 -0400523 if !isInternal {
524 if err := isValidPythonPath(runfilesPath); err != nil {
525 ctx.PropertyErrorf("srcs", err.Error())
526 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800527 }
Liz Kammerd737d022020-11-16 15:42:51 -0800528 if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
529 p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800530 }
531 }
532
533 for _, d := range expandedData {
Raphael Blistein59858462024-05-08 16:15:53 +0000534 if d.Ext() == pyExt {
535 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800536 continue
537 }
Nan Zhang1db85402017-12-18 13:20:23 -0800538 runfilesPath := filepath.Join(pkgPath, d.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800539 if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800540 p.dataPathMappings = append(p.dataPathMappings,
541 pathMapping{dest: runfilesPath, src: d})
542 }
543 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800544}
545
Liz Kammerd737d022020-11-16 15:42:51 -0800546// createSrcsZip registers build actions to zip current module's sources and data.
Cole Faust4d247e62023-01-23 10:14:58 -0800547func (p *PythonLibraryModule) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800548 relativeRootMap := make(map[string]android.Paths)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800549 var protoSrcs android.Paths
Cole Faust5c503d12023-01-24 11:48:08 -0800550 addPathMapping := func(path pathMapping) {
Raphael Blistein59858462024-05-08 16:15:53 +0000551 relativeRoot := strings.TrimSuffix(path.src.String(), path.src.Rel())
552 relativeRootMap[relativeRoot] = append(relativeRootMap[relativeRoot], path.src)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800553 }
Cole Faust5c503d12023-01-24 11:48:08 -0800554
555 // "srcs" or "data" properties may contain filegroups so it might happen that
556 // the root directory for each source path is different.
557 for _, path := range p.srcsPathMappings {
Raphael Blistein59858462024-05-08 16:15:53 +0000558 // handle proto sources separately
559 if path.src.Ext() == protoExt {
560 protoSrcs = append(protoSrcs, path.src)
561 } else {
562 addPathMapping(path)
563 }
Cole Faust5c503d12023-01-24 11:48:08 -0800564 }
565 for _, path := range p.dataPathMappings {
566 addPathMapping(path)
567 }
568
Nan Zhangb8fa1972017-12-22 16:12:00 -0800569 var zips android.Paths
570 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700571 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
572 protoFlags.OutTypeFlag = "--python_out"
573
Cole Faustcaf766b2022-10-21 16:07:56 -0700574 if pkgPath != "" {
Cole Faust43ac21f2022-09-19 11:19:52 -0700575 pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path")
576 rule := android.NewRuleBuilder(pctx, ctx)
577 var stagedProtoSrcs android.Paths
578 for _, srcFile := range protoSrcs {
579 stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel())
Cole Faust43ac21f2022-09-19 11:19:52 -0700580 rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc)
581 stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc)
582 }
583 rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path")
584 protoSrcs = stagedProtoSrcs
Cole Faust43ac21f2022-09-19 11:19:52 -0700585 }
586
Nan Zhangb8fa1972017-12-22 16:12:00 -0800587 for _, srcFile := range protoSrcs {
Cole Faustcaf766b2022-10-21 16:07:56 -0700588 zip := genProto(ctx, srcFile, protoFlags)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800589 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800590 }
591 }
592
Nan Zhangb8fa1972017-12-22 16:12:00 -0800593 if len(relativeRootMap) > 0 {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800594 // in order to keep stable order of soong_zip params, we sort the keys here.
Cole Faust18994c72023-02-28 16:02:16 -0800595 roots := android.SortedKeys(relativeRootMap)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800596
Cole Faust01243362022-06-02 12:11:12 -0700597 // Use -symlinks=false so that the symlinks in the bazel output directory are followed
598 parArgs := []string{"-symlinks=false"}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700599 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800600 // use package path as path prefix
Nan Zhangf0c4e432018-05-22 14:50:18 -0700601 parArgs = append(parArgs, `-P `+pkgPath)
602 }
Liz Kammerd737d022020-11-16 15:42:51 -0800603 paths := android.Paths{}
604 for _, root := range roots {
605 // specify relative root of file in following -f arguments
606 parArgs = append(parArgs, `-C `+root)
607 for _, path := range relativeRootMap[root] {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800608 parArgs = append(parArgs, `-f `+path.String())
Liz Kammerd737d022020-11-16 15:42:51 -0800609 paths = append(paths, path)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800610 }
611 }
612
613 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
614 ctx.Build(pctx, android.BuildParams{
615 Rule: zip,
616 Description: "python library archive",
617 Output: origSrcsZip,
Liz Kammerd737d022020-11-16 15:42:51 -0800618 // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits
619 Implicits: paths,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800620 Args: map[string]string{
621 "args": strings.Join(parArgs, " "),
622 },
623 })
624 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800625 }
Liz Kammerd737d022020-11-16 15:42:51 -0800626 // we may have multiple zips due to separate handling of proto source files
Nan Zhangb8fa1972017-12-22 16:12:00 -0800627 if len(zips) == 1 {
628 return zips[0]
629 } else {
630 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
631 ctx.Build(pctx, android.BuildParams{
632 Rule: combineZip,
633 Description: "combine python library archive",
634 Output: combinedSrcsZip,
635 Inputs: zips,
636 })
637 return combinedSrcsZip
638 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800639}
640
Cole Faust5c503d12023-01-24 11:48:08 -0800641func (p *PythonLibraryModule) precompileSrcs(ctx android.ModuleContext) android.Path {
642 // To precompile the python sources, we need a python interpreter and stdlib built
643 // for host. We then use those to compile the python sources, which may be used on either
644 // host of device. Python bytecode is architecture agnostic, so we're essentially
645 // "cross compiling" for device here purely by virtue of host and device python bytecode
646 // being the same.
647 var stdLib android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400648 var stdLibPkg string
Cole Faust5c503d12023-01-24 11:48:08 -0800649 var launcher android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400650 if proptools.BoolDefault(p.properties.Is_internal, false) {
Cole Faust5c503d12023-01-24 11:48:08 -0800651 stdLib = p.srcsZip
Dan Willemsen339a63f2023-08-15 22:17:03 -0400652 stdLibPkg = p.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800653 } else {
654 ctx.VisitDirectDepsWithTag(hostStdLibTag, func(module android.Module) {
655 if dep, ok := module.(pythonDependency); ok {
656 stdLib = dep.getPrecompiledSrcsZip()
Dan Willemsen339a63f2023-08-15 22:17:03 -0400657 stdLibPkg = dep.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800658 }
659 })
660 }
661 ctx.VisitDirectDepsWithTag(hostLauncherTag, func(module android.Module) {
662 if dep, ok := module.(IntermPathProvider); ok {
663 optionalLauncher := dep.IntermPathForModuleOut()
664 if optionalLauncher.Valid() {
665 launcher = optionalLauncher.Path()
666 }
Cole Faust909d2372023-02-13 23:17:40 +0000667 }
668 })
669 var launcherSharedLibs android.Paths
670 var ldLibraryPath []string
671 ctx.VisitDirectDepsWithTag(hostlauncherSharedLibTag, func(module android.Module) {
672 if dep, ok := module.(IntermPathProvider); ok {
673 optionalPath := dep.IntermPathForModuleOut()
674 if optionalPath.Valid() {
675 launcherSharedLibs = append(launcherSharedLibs, optionalPath.Path())
676 ldLibraryPath = append(ldLibraryPath, filepath.Dir(optionalPath.Path().String()))
Cole Faust5c503d12023-01-24 11:48:08 -0800677 }
678 }
679 })
680
681 out := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszipprecompiled")
682 if stdLib == nil || launcher == nil {
683 // This shouldn't happen in a real build because we'll error out when adding dependencies
684 // on the stdlib and launcher if they don't exist. But some tests set
685 // AllowMissingDependencies.
686 return out
687 }
688 ctx.Build(pctx, android.BuildParams{
689 Rule: precompile,
690 Input: p.srcsZip,
691 Output: out,
692 Implicits: launcherSharedLibs,
693 Description: "Precompile the python sources of " + ctx.ModuleName(),
694 Args: map[string]string{
695 "stdlibZip": stdLib.String(),
Dan Willemsen339a63f2023-08-15 22:17:03 -0400696 "stdlibPkg": stdLibPkg,
Cole Faust5c503d12023-01-24 11:48:08 -0800697 "launcher": launcher.String(),
698 "ldLibraryPath": strings.Join(ldLibraryPath, ":"),
699 },
700 })
701 return out
702}
703
Cole Faust4d247e62023-01-23 10:14:58 -0800704// isPythonLibModule returns whether the given module is a Python library PythonLibraryModule or not
Nan Zhangd4e641b2017-07-12 12:55:28 -0700705func isPythonLibModule(module blueprint.Module) bool {
Cole Faust4d247e62023-01-23 10:14:58 -0800706 if _, ok := module.(*PythonLibraryModule); ok {
707 if _, ok := module.(*PythonBinaryModule); !ok {
708 return true
709 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700710 }
711 return false
712}
713
Liz Kammerd737d022020-11-16 15:42:51 -0800714// collectPathsFromTransitiveDeps checks for source/data files for duplicate paths
715// for module and its transitive dependencies and collects list of data/source file
716// zips for transitive dependencies.
Cole Faust5c503d12023-01-24 11:48:08 -0800717func (p *PythonLibraryModule) collectPathsFromTransitiveDeps(ctx android.ModuleContext, precompiled bool) android.Paths {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800718 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
719 // check duplicates.
720 destToPySrcs := make(map[string]string)
721 destToPyData := make(map[string]string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800722 for _, path := range p.srcsPathMappings {
723 destToPySrcs[path.dest] = path.src.String()
724 }
725 for _, path := range p.dataPathMappings {
726 destToPyData[path.dest] = path.src.String()
727 }
728
Colin Cross6b753602018-06-21 13:03:07 -0700729 seen := make(map[android.Module]bool)
730
Cole Faust4d247e62023-01-23 10:14:58 -0800731 var result android.Paths
732
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800733 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700734 ctx.WalkDeps(func(child, parent android.Module) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800735 // we only collect dependencies tagged as python library deps
Colin Cross6b753602018-06-21 13:03:07 -0700736 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
737 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800738 }
Colin Cross6b753602018-06-21 13:03:07 -0700739 if seen[child] {
740 return false
741 }
742 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800743 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700744 if !isPythonLibModule(child) {
Liz Kammerd737d022020-11-16 15:42:51 -0800745 ctx.PropertyErrorf("libs",
Nan Zhangd4e641b2017-07-12 12:55:28 -0700746 "the dependency %q of module %q is not Python library!",
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxd75507f2021-08-20 21:02:43 +0000747 ctx.OtherModuleName(child), ctx.ModuleName())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700748 }
Liz Kammerd737d022020-11-16 15:42:51 -0800749 // collect source and data paths, checking that there are no duplicate output file conflicts
750 if dep, ok := child.(pythonDependency); ok {
751 srcs := dep.getSrcsPathMappings()
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800752 for _, path := range srcs {
Liz Kammerd737d022020-11-16 15:42:51 -0800753 checkForDuplicateOutputPath(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700754 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800755 }
Liz Kammerd737d022020-11-16 15:42:51 -0800756 data := dep.getDataPathMappings()
757 for _, path := range data {
758 checkForDuplicateOutputPath(ctx, destToPyData,
759 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
760 }
Cole Faust5c503d12023-01-24 11:48:08 -0800761 if precompiled {
762 result = append(result, dep.getPrecompiledSrcsZip())
763 } else {
764 result = append(result, dep.getSrcsZip())
765 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800766 }
Colin Cross6b753602018-06-21 13:03:07 -0700767 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800768 })
Cole Faust4d247e62023-01-23 10:14:58 -0800769 return result
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800770}
771
Liz Kammerd737d022020-11-16 15:42:51 -0800772// chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which
773// would result in two files being placed in the same location.
774// If there is a duplicate path, an error is thrown and true is returned
775// Otherwise, outputPath: srcPath is added to m and returns false
776func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool {
777 if oldSrcPath, found := m[outputPath]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700778 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800779 " First file: in module %s at path %q."+
780 " Second file: in module %s at path %q.",
Liz Kammerd737d022020-11-16 15:42:51 -0800781 outputPath, curModule, oldSrcPath, otherModule, srcPath)
782 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800783 }
Liz Kammerd737d022020-11-16 15:42:51 -0800784 m[outputPath] = srcPath
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800785
Liz Kammerd737d022020-11-16 15:42:51 -0800786 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800787}
Nan Zhangea568a42017-11-08 21:20:04 -0800788
Liz Kammerd737d022020-11-16 15:42:51 -0800789// InstallInData returns true as Python is not supported in the system partition
Cole Faust4d247e62023-01-23 10:14:58 -0800790func (p *PythonLibraryModule) InstallInData() bool {
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000791 return true
792}
793
Nan Zhangea568a42017-11-08 21:20:04 -0800794var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800795var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800796var String = proptools.String