blob: 914b77e2ff012866b73376f17c847fda76d7adfd [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
Inseob Kim25f5ae42025-01-07 22:27:58 +090098 // Same as data, but will add dependencies on modules via a device os variation and the
99 // device's first supported arch's variation. Useful for a host test that wants to embed a
100 // module built for device.
101 Device_first_data []string `android:"path_device_first"`
102
Colin Cross1bc63932020-11-22 20:12:45 -0800103 // list of java modules that provide data that should be installed alongside the test.
104 Java_data []string
105
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800106 // list of the Python libraries compatible both with Python2 and Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700107 Libs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800108
109 Version struct {
Liz Kammerd737d022020-11-16 15:42:51 -0800110 // Python2-specific properties, including whether Python2 is supported for this module
111 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700112 Py2 VersionProperties `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800113
Liz Kammerd737d022020-11-16 15:42:51 -0800114 // Python3-specific properties, including whether Python3 is supported for this module
115 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700116 Py3 VersionProperties `android:"arch_variant"`
117 } `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800118
119 // the actual version each module uses after variations created.
120 // this property name is hidden from users' perspectives, and soong will populate it during
121 // runtime.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700122 Actual_version string `blueprint:"mutated"`
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700123
Liz Kammerd737d022020-11-16 15:42:51 -0800124 // whether the module is required to be built with actual_version.
125 // this is set by the python version mutator based on version-specific properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700126 Enabled *bool `blueprint:"mutated"`
127
Liz Kammerd737d022020-11-16 15:42:51 -0800128 // whether the binary is required to be built with embedded launcher for this actual_version.
129 // this is set by the python version mutator based on version-specific properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700130 Embedded_launcher *bool `blueprint:"mutated"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800131}
132
Liz Kammerd737d022020-11-16 15:42:51 -0800133// Used to store files of current module after expanding dependencies
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800134type pathMapping struct {
135 dest string
136 src android.Path
137}
138
Cole Faust4d247e62023-01-23 10:14:58 -0800139type PythonLibraryModule struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800140 android.ModuleBase
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700141 android.DefaultableModuleBase
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800142
Nan Zhangb8fa1972017-12-22 16:12:00 -0800143 properties BaseProperties
144 protoProperties android.ProtoProperties
Nan Zhangd4e641b2017-07-12 12:55:28 -0700145
146 // initialize before calling Init
147 hod android.HostOrDeviceSupported
148 multilib android.Multilib
149
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800150 // the Python files of current module after expanding source dependencies.
151 // pathMapping: <dest: runfile_path, src: source_path>
152 srcsPathMappings []pathMapping
153
154 // the data files of current module after expanding source dependencies.
155 // pathMapping: <dest: runfile_path, src: source_path>
156 dataPathMappings []pathMapping
157
Cole Faust5c503d12023-01-24 11:48:08 -0800158 // The zip file containing the current module's source/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800159 srcsZip android.Path
Cole Faust5c503d12023-01-24 11:48:08 -0800160
161 // The zip file containing the current module's source/data files, with the
162 // source files precompiled.
163 precompiledSrcsZip android.Path
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700164
165 sourceProperties android.SourceProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800166}
167
Liz Kammerd737d022020-11-16 15:42:51 -0800168// newModule generates new Python base module
Cole Faust4d247e62023-01-23 10:14:58 -0800169func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *PythonLibraryModule {
170 return &PythonLibraryModule{
Nan Zhangd4e641b2017-07-12 12:55:28 -0700171 hod: hod,
172 multilib: multilib,
173 }
174}
175
Liz Kammerd737d022020-11-16 15:42:51 -0800176// interface implemented by Python modules to provide source and data mappings and zip to python
177// modules that depend on it
178type pythonDependency interface {
179 getSrcsPathMappings() []pathMapping
180 getDataPathMappings() []pathMapping
181 getSrcsZip() android.Path
Cole Faust5c503d12023-01-24 11:48:08 -0800182 getPrecompiledSrcsZip() android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400183 getPkgPath() string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800184}
185
Liz Kammerd737d022020-11-16 15:42:51 -0800186// getSrcsPathMappings gets this module's path mapping of src source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800187func (p *PythonLibraryModule) getSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800188 return p.srcsPathMappings
189}
190
Liz Kammerd737d022020-11-16 15:42:51 -0800191// getSrcsPathMappings gets this module's path mapping of data source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800192func (p *PythonLibraryModule) getDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800193 return p.dataPathMappings
194}
195
Liz Kammerd737d022020-11-16 15:42:51 -0800196// getSrcsZip returns the filepath where the current module's source/data files are zipped.
Cole Faust4d247e62023-01-23 10:14:58 -0800197func (p *PythonLibraryModule) getSrcsZip() android.Path {
Nan Zhang1db85402017-12-18 13:20:23 -0800198 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800199}
200
Cole Faust5c503d12023-01-24 11:48:08 -0800201// getSrcsZip returns the filepath where the current module's source/data files are zipped.
202func (p *PythonLibraryModule) getPrecompiledSrcsZip() android.Path {
203 return p.precompiledSrcsZip
204}
205
Dan Willemsen339a63f2023-08-15 22:17:03 -0400206// getPkgPath returns the pkg_path value
207func (p *PythonLibraryModule) getPkgPath() string {
208 return String(p.properties.Pkg_path)
209}
210
Cole Faust4d247e62023-01-23 10:14:58 -0800211func (p *PythonLibraryModule) getBaseProperties() *BaseProperties {
212 return &p.properties
213}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800214
Cole Faust4d247e62023-01-23 10:14:58 -0800215var _ pythonDependency = (*PythonLibraryModule)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800216
Cole Faust4d247e62023-01-23 10:14:58 -0800217func (p *PythonLibraryModule) init() android.Module {
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700218 p.AddProperties(&p.properties, &p.protoProperties, &p.sourceProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700219 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700220 android.InitDefaultableModule(p)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700221 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800222}
223
Liz Kammerd737d022020-11-16 15:42:51 -0800224// Python-specific tag to transfer information on the purpose of a dependency.
225// This is used when adding a dependency on a module, which can later be accessed when visiting
226// dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700227type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800228 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700229 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800230}
231
Liz Kammerd737d022020-11-16 15:42:51 -0800232// Python-specific tag that indicates that installed files of this module should depend on installed
233// files of the dependency
Colin Crosse9fe2942020-11-10 18:12:15 -0800234type installDependencyTag struct {
235 blueprint.BaseDependencyTag
Liz Kammerd737d022020-11-16 15:42:51 -0800236 // embedding this struct provides the installation dependency requirement
Colin Crosse9fe2942020-11-10 18:12:15 -0800237 android.InstallAlwaysNeededDependencyTag
238 name string
239}
240
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800241var (
Cole Faust5c503d12023-01-24 11:48:08 -0800242 pythonLibTag = dependencyTag{name: "pythonLib"}
243 javaDataTag = dependencyTag{name: "javaData"}
244 // The python interpreter, with soong module name "py3-launcher" or "py3-launcher-autorun".
Cole Faust909d2372023-02-13 23:17:40 +0000245 launcherTag = dependencyTag{name: "launcher"}
246 launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"}
Cole Faust5c503d12023-01-24 11:48:08 -0800247 // The python interpreter built for host so that we can precompile python sources.
248 // This only works because the precompiled sources don't vary by architecture.
249 // The soong module name is "py3-launcher".
Cole Faust909d2372023-02-13 23:17:40 +0000250 hostLauncherTag = dependencyTag{name: "hostLauncher"}
251 hostlauncherSharedLibTag = dependencyTag{name: "hostlauncherSharedLib"}
252 hostStdLibTag = dependencyTag{name: "hostStdLib"}
253 pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
254 pyExt = ".py"
255 protoExt = ".proto"
256 pyVersion2 = "PY2"
257 pyVersion3 = "PY3"
258 internalPath = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800259)
260
Cole Faust4d247e62023-01-23 10:14:58 -0800261type basePropertiesProvider interface {
262 getBaseProperties() *BaseProperties
263}
264
Colin Crosse0771282024-05-21 15:19:18 -0700265type versionSplitTransitionMutator struct{}
266
267func (versionSplitTransitionMutator) Split(ctx android.BaseModuleContext) []string {
268 if base, ok := ctx.Module().(basePropertiesProvider); ok {
269 props := base.getBaseProperties()
270 var variants []string
271 // PY3 is first so that we alias the PY3 variant rather than PY2 if both
272 // are available
273 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
274 variants = append(variants, pyVersion3)
275 }
276 if proptools.BoolDefault(props.Version.Py2.Enabled, false) {
Cole Faust4ce4f882024-09-09 18:08:49 -0700277 if ctx.ModuleName() != "py2-cmd" &&
Colin Crosse0771282024-05-21 15:19:18 -0700278 ctx.ModuleName() != "py2-stdlib" {
Cole Faust4ce4f882024-09-09 18:08:49 -0700279 ctx.PropertyErrorf("version.py2.enabled", "Python 2 is no longer supported, please convert to python 3.")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800280 }
Colin Crosse0771282024-05-21 15:19:18 -0700281 variants = append(variants, pyVersion2)
282 }
283 return variants
284 }
285 return []string{""}
286}
287
288func (versionSplitTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
289 return ""
290}
291
292func (versionSplitTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
293 if incomingVariation != "" {
294 return incomingVariation
295 }
296 if base, ok := ctx.Module().(basePropertiesProvider); ok {
297 props := base.getBaseProperties()
298 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
299 return pyVersion3
300 } else {
301 return pyVersion2
302 }
303 }
304
305 return ""
306}
307
308func (versionSplitTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
309 if variation == "" {
310 return
311 }
312 if base, ok := ctx.Module().(basePropertiesProvider); ok {
313 props := base.getBaseProperties()
314 props.Actual_version = variation
315
316 var versionProps *VersionProperties
317 if variation == pyVersion3 {
318 versionProps = &props.Version.Py3
319 } else if variation == pyVersion2 {
320 versionProps = &props.Version.Py2
321 }
322
323 err := proptools.AppendMatchingProperties([]interface{}{props}, versionProps, nil)
324 if err != nil {
325 panic(err)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800326 }
327 }
328}
329
Liz Kammerd737d022020-11-16 15:42:51 -0800330func anyHasExt(paths []string, ext string) bool {
331 for _, p := range paths {
332 if filepath.Ext(p) == ext {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800333 return true
334 }
335 }
336
337 return false
338}
339
Cole Faust4d247e62023-01-23 10:14:58 -0800340func (p *PythonLibraryModule) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800341 return anyHasExt(p.properties.Srcs, ext)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800342}
343
Liz Kammerd737d022020-11-16 15:42:51 -0800344// DepsMutator mutates dependencies for this module:
Colin Crossd079e0b2022-08-16 10:27:33 -0700345// - handles proto dependencies,
346// - if required, specifies launcher and adds launcher dependencies,
347// - applies python version mutations to Python dependencies
Cole Faust4d247e62023-01-23 10:14:58 -0800348func (p *PythonLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700349 android.ProtoDeps(ctx, &p.protoProperties)
350
Colin Crosse20113d2020-11-22 19:37:44 -0800351 versionVariation := []blueprint.Variation{
352 {"python_version", p.properties.Actual_version},
Nan Zhangb8fa1972017-12-22 16:12:00 -0800353 }
Colin Crosse20113d2020-11-22 19:37:44 -0800354
Liz Kammerd737d022020-11-16 15:42:51 -0800355 // If sources contain a proto file, add dependency on libprotobuf-python
356 if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
Colin Crosse20113d2020-11-22 19:37:44 -0800357 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python")
358 }
Liz Kammerd737d022020-11-16 15:42:51 -0800359
360 // Add python library dependencies for this python version variation
Colin Crosse20113d2020-11-22 19:37:44 -0800361 ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700362
Colin Cross1bc63932020-11-22 20:12:45 -0800363 // Emulate the data property for java_data but with the arch variation overridden to "common"
364 // so that it can point to java modules.
365 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
366 ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
Cole Faust5c503d12023-01-24 11:48:08 -0800367
Cole Faust909d2372023-02-13 23:17:40 +0000368 p.AddDepsOnPythonLauncherAndStdlib(ctx, hostStdLibTag, hostLauncherTag, hostlauncherSharedLibTag, false, ctx.Config().BuildOSTarget)
Cole Faust5c503d12023-01-24 11:48:08 -0800369}
370
Cole Faust909d2372023-02-13 23:17:40 +0000371// AddDepsOnPythonLauncherAndStdlib will make the current module depend on the python stdlib,
372// launcher (interpreter), and the launcher's shared libraries. If autorun is true, it will use
373// the autorun launcher instead of the regular one. This function acceps a targetForDeps argument
374// as the target to use for these dependencies. For embedded launcher python binaries, the launcher
375// that will be embedded will be under the same target as the python module itself. But when
376// precompiling python code, we need to get the python launcher built for host, even if we're
377// compiling the python module for device, so we pass a different target to this function.
Cole Faust5c503d12023-01-24 11:48:08 -0800378func (p *PythonLibraryModule) AddDepsOnPythonLauncherAndStdlib(ctx android.BottomUpMutatorContext,
Cole Faust909d2372023-02-13 23:17:40 +0000379 stdLibTag, launcherTag, launcherSharedLibTag blueprint.DependencyTag,
Cole Faust5c503d12023-01-24 11:48:08 -0800380 autorun bool, targetForDeps android.Target) {
381 var stdLib string
382 var launcherModule string
Cole Faust909d2372023-02-13 23:17:40 +0000383 // Add launcher shared lib dependencies. Ideally, these should be
384 // derived from the `shared_libs` property of the launcher. TODO: read these from
385 // the python launcher itself using ctx.OtherModuleProvider() or similar on the result
386 // of ctx.AddFarVariationDependencies()
387 launcherSharedLibDeps := []string{
388 "libsqlite",
389 }
390 // Add launcher-specific dependencies for bionic
391 if targetForDeps.Os.Bionic() {
392 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm")
393 }
394 if targetForDeps.Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() {
395 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc_musl")
396 }
Cole Faust5c503d12023-01-24 11:48:08 -0800397
398 switch p.properties.Actual_version {
399 case pyVersion2:
400 stdLib = "py2-stdlib"
401
402 launcherModule = "py2-launcher"
403 if autorun {
404 launcherModule = "py2-launcher-autorun"
405 }
406
Cole Faust909d2372023-02-13 23:17:40 +0000407 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++")
Cole Faust5c503d12023-01-24 11:48:08 -0800408 case pyVersion3:
Dan Willemsen339a63f2023-08-15 22:17:03 -0400409 var prebuiltStdLib bool
410 if targetForDeps.Os.Bionic() {
411 prebuiltStdLib = false
412 } else if ctx.Config().VendorConfig("cpython3").Bool("force_build_host") {
413 prebuiltStdLib = false
414 } else {
415 prebuiltStdLib = true
416 }
417
418 if prebuiltStdLib {
419 stdLib = "py3-stdlib-prebuilt"
420 } else {
421 stdLib = "py3-stdlib"
422 }
Cole Faust5c503d12023-01-24 11:48:08 -0800423
424 launcherModule = "py3-launcher"
425 if autorun {
426 launcherModule = "py3-launcher-autorun"
427 }
428 if ctx.Config().HostStaticBinaries() && targetForDeps.Os == android.LinuxMusl {
429 launcherModule += "-static"
430 }
Cole Faust909d2372023-02-13 23:17:40 +0000431 if ctx.Device() {
432 launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog")
433 }
Cole Faust5c503d12023-01-24 11:48:08 -0800434 default:
435 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
436 p.properties.Actual_version, ctx.ModuleName()))
437 }
438 targetVariations := targetForDeps.Variations()
439 if ctx.ModuleName() != stdLib {
440 stdLibVariations := make([]blueprint.Variation, 0, len(targetVariations)+1)
441 stdLibVariations = append(stdLibVariations, blueprint.Variation{Mutator: "python_version", Variation: p.properties.Actual_version})
442 stdLibVariations = append(stdLibVariations, targetVariations...)
443 // Using AddFarVariationDependencies for all of these because they can be for a different
444 // platform, like if the python module itself was being compiled for device, we may want
445 // the python interpreter built for host so that we can precompile python sources.
446 ctx.AddFarVariationDependencies(stdLibVariations, stdLibTag, stdLib)
447 }
448 ctx.AddFarVariationDependencies(targetVariations, launcherTag, launcherModule)
Cole Faust909d2372023-02-13 23:17:40 +0000449 ctx.AddFarVariationDependencies(targetVariations, launcherSharedLibTag, launcherSharedLibDeps...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800450}
451
Cole Faust4d247e62023-01-23 10:14:58 -0800452// GenerateAndroidBuildActions performs build actions common to all Python modules
453func (p *PythonLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammerd737d022020-11-16 15:42:51 -0800454 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs)
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700455 // Keep before any early returns.
456 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
457 TestOnly: Bool(p.sourceProperties.Test_only),
458 TopLevelTarget: p.sourceProperties.Top_level_test_target,
459 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800460
461 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800462 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700463 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, p.properties.Device_common_data)...)
Inseob Kim25f5ae42025-01-07 22:27:58 +0900464 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, p.properties.Device_first_data)...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800465
Colin Cross1bc63932020-11-22 20:12:45 -0800466 // Emulate the data property for java_data dependencies.
467 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
468 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
469 }
470
Liz Kammerd737d022020-11-16 15:42:51 -0800471 // Validate pkg_path property
Nan Zhang1db85402017-12-18 13:20:23 -0800472 pkgPath := String(p.properties.Pkg_path)
473 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800474 // TODO: export validation from android/paths.go handling to replace this duplicated functionality
Nan Zhang1db85402017-12-18 13:20:23 -0800475 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
476 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
477 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700478 ctx.PropertyErrorf("pkg_path",
479 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800480 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700481 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800482 }
Liz Kammerd737d022020-11-16 15:42:51 -0800483 }
484 // If property Is_internal is set, prepend pkgPath with internalPath
485 if proptools.BoolDefault(p.properties.Is_internal, false) {
486 pkgPath = filepath.Join(internalPath, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800487 }
488
Liz Kammerd737d022020-11-16 15:42:51 -0800489 // generate src:destination path mappings for this module
Nan Zhang1db85402017-12-18 13:20:23 -0800490 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800491
Liz Kammerd737d022020-11-16 15:42:51 -0800492 // generate the zipfile of all source and data files
Nan Zhang1db85402017-12-18 13:20:23 -0800493 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Dan Willemsenfe2dafc2023-08-24 22:59:16 +0000494 p.precompiledSrcsZip = p.precompileSrcs(ctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800495}
496
Liz Kammerd737d022020-11-16 15:42:51 -0800497func isValidPythonPath(path string) error {
498 identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/")
499 for _, token := range identifiers {
500 if !pathComponentRegexp.MatchString(token) {
501 return fmt.Errorf("the path %q contains invalid subpath %q. "+
502 "Subpaths must be at least one character long. "+
503 "The first character must an underscore or letter. "+
504 "Following characters may be any of: letter, digit, underscore, hyphen.",
505 path, token)
506 }
507 }
508 return nil
509}
510
511// For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path>
512// for python/data files expanded from properties.
Cole Faust4d247e62023-01-23 10:14:58 -0800513func (p *PythonLibraryModule) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800514 expandedSrcs, expandedData android.Paths) {
515 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800516 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800517 destToPySrcs := make(map[string]string)
518 destToPyData := make(map[string]string)
519
Dan Willemsen339a63f2023-08-15 22:17:03 -0400520 // Disable path checks for the stdlib, as it includes a "." in the version string
521 isInternal := proptools.BoolDefault(p.properties.Is_internal, false)
522
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800523 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800524 if s.Ext() != pyExt && s.Ext() != protoExt {
525 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800526 continue
527 }
Nan Zhang1db85402017-12-18 13:20:23 -0800528 runfilesPath := filepath.Join(pkgPath, s.Rel())
Dan Willemsen339a63f2023-08-15 22:17:03 -0400529 if !isInternal {
530 if err := isValidPythonPath(runfilesPath); err != nil {
531 ctx.PropertyErrorf("srcs", err.Error())
532 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800533 }
Liz Kammerd737d022020-11-16 15:42:51 -0800534 if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
535 p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800536 }
537 }
538
539 for _, d := range expandedData {
Raphael Blistein59858462024-05-08 16:15:53 +0000540 if d.Ext() == pyExt {
541 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800542 continue
543 }
Nan Zhang1db85402017-12-18 13:20:23 -0800544 runfilesPath := filepath.Join(pkgPath, d.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800545 if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800546 p.dataPathMappings = append(p.dataPathMappings,
547 pathMapping{dest: runfilesPath, src: d})
548 }
549 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800550}
551
Liz Kammerd737d022020-11-16 15:42:51 -0800552// createSrcsZip registers build actions to zip current module's sources and data.
Cole Faust4d247e62023-01-23 10:14:58 -0800553func (p *PythonLibraryModule) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800554 relativeRootMap := make(map[string]android.Paths)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800555 var protoSrcs android.Paths
Cole Faust5c503d12023-01-24 11:48:08 -0800556 addPathMapping := func(path pathMapping) {
Raphael Blistein59858462024-05-08 16:15:53 +0000557 relativeRoot := strings.TrimSuffix(path.src.String(), path.src.Rel())
558 relativeRootMap[relativeRoot] = append(relativeRootMap[relativeRoot], path.src)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800559 }
Cole Faust5c503d12023-01-24 11:48:08 -0800560
561 // "srcs" or "data" properties may contain filegroups so it might happen that
562 // the root directory for each source path is different.
563 for _, path := range p.srcsPathMappings {
Raphael Blistein59858462024-05-08 16:15:53 +0000564 // handle proto sources separately
565 if path.src.Ext() == protoExt {
566 protoSrcs = append(protoSrcs, path.src)
567 } else {
568 addPathMapping(path)
569 }
Cole Faust5c503d12023-01-24 11:48:08 -0800570 }
571 for _, path := range p.dataPathMappings {
572 addPathMapping(path)
573 }
574
Nan Zhangb8fa1972017-12-22 16:12:00 -0800575 var zips android.Paths
576 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700577 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
578 protoFlags.OutTypeFlag = "--python_out"
579
Cole Faustcaf766b2022-10-21 16:07:56 -0700580 if pkgPath != "" {
Cole Faust43ac21f2022-09-19 11:19:52 -0700581 pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path")
582 rule := android.NewRuleBuilder(pctx, ctx)
583 var stagedProtoSrcs android.Paths
584 for _, srcFile := range protoSrcs {
585 stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel())
Cole Faust43ac21f2022-09-19 11:19:52 -0700586 rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc)
587 stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc)
588 }
589 rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path")
590 protoSrcs = stagedProtoSrcs
Cole Faust43ac21f2022-09-19 11:19:52 -0700591 }
592
Nan Zhangb8fa1972017-12-22 16:12:00 -0800593 for _, srcFile := range protoSrcs {
Cole Faustcaf766b2022-10-21 16:07:56 -0700594 zip := genProto(ctx, srcFile, protoFlags)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800595 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800596 }
597 }
598
Nan Zhangb8fa1972017-12-22 16:12:00 -0800599 if len(relativeRootMap) > 0 {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800600 // in order to keep stable order of soong_zip params, we sort the keys here.
Cole Faust18994c72023-02-28 16:02:16 -0800601 roots := android.SortedKeys(relativeRootMap)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800602
Cole Faust01243362022-06-02 12:11:12 -0700603 // Use -symlinks=false so that the symlinks in the bazel output directory are followed
604 parArgs := []string{"-symlinks=false"}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700605 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800606 // use package path as path prefix
Nan Zhangf0c4e432018-05-22 14:50:18 -0700607 parArgs = append(parArgs, `-P `+pkgPath)
608 }
Liz Kammerd737d022020-11-16 15:42:51 -0800609 paths := android.Paths{}
610 for _, root := range roots {
611 // specify relative root of file in following -f arguments
612 parArgs = append(parArgs, `-C `+root)
613 for _, path := range relativeRootMap[root] {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800614 parArgs = append(parArgs, `-f `+path.String())
Liz Kammerd737d022020-11-16 15:42:51 -0800615 paths = append(paths, path)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800616 }
617 }
618
619 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
620 ctx.Build(pctx, android.BuildParams{
621 Rule: zip,
622 Description: "python library archive",
623 Output: origSrcsZip,
Liz Kammerd737d022020-11-16 15:42:51 -0800624 // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits
625 Implicits: paths,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800626 Args: map[string]string{
627 "args": strings.Join(parArgs, " "),
628 },
629 })
630 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800631 }
Liz Kammerd737d022020-11-16 15:42:51 -0800632 // we may have multiple zips due to separate handling of proto source files
Nan Zhangb8fa1972017-12-22 16:12:00 -0800633 if len(zips) == 1 {
634 return zips[0]
635 } else {
636 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
637 ctx.Build(pctx, android.BuildParams{
638 Rule: combineZip,
639 Description: "combine python library archive",
640 Output: combinedSrcsZip,
641 Inputs: zips,
642 })
643 return combinedSrcsZip
644 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800645}
646
Cole Faust5c503d12023-01-24 11:48:08 -0800647func (p *PythonLibraryModule) precompileSrcs(ctx android.ModuleContext) android.Path {
648 // To precompile the python sources, we need a python interpreter and stdlib built
649 // for host. We then use those to compile the python sources, which may be used on either
650 // host of device. Python bytecode is architecture agnostic, so we're essentially
651 // "cross compiling" for device here purely by virtue of host and device python bytecode
652 // being the same.
653 var stdLib android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400654 var stdLibPkg string
Cole Faust5c503d12023-01-24 11:48:08 -0800655 var launcher android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400656 if proptools.BoolDefault(p.properties.Is_internal, false) {
Cole Faust5c503d12023-01-24 11:48:08 -0800657 stdLib = p.srcsZip
Dan Willemsen339a63f2023-08-15 22:17:03 -0400658 stdLibPkg = p.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800659 } else {
660 ctx.VisitDirectDepsWithTag(hostStdLibTag, func(module android.Module) {
661 if dep, ok := module.(pythonDependency); ok {
662 stdLib = dep.getPrecompiledSrcsZip()
Dan Willemsen339a63f2023-08-15 22:17:03 -0400663 stdLibPkg = dep.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800664 }
665 })
666 }
667 ctx.VisitDirectDepsWithTag(hostLauncherTag, func(module android.Module) {
668 if dep, ok := module.(IntermPathProvider); ok {
669 optionalLauncher := dep.IntermPathForModuleOut()
670 if optionalLauncher.Valid() {
671 launcher = optionalLauncher.Path()
672 }
Cole Faust909d2372023-02-13 23:17:40 +0000673 }
674 })
675 var launcherSharedLibs android.Paths
676 var ldLibraryPath []string
677 ctx.VisitDirectDepsWithTag(hostlauncherSharedLibTag, func(module android.Module) {
678 if dep, ok := module.(IntermPathProvider); ok {
679 optionalPath := dep.IntermPathForModuleOut()
680 if optionalPath.Valid() {
681 launcherSharedLibs = append(launcherSharedLibs, optionalPath.Path())
682 ldLibraryPath = append(ldLibraryPath, filepath.Dir(optionalPath.Path().String()))
Cole Faust5c503d12023-01-24 11:48:08 -0800683 }
684 }
685 })
686
687 out := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszipprecompiled")
688 if stdLib == nil || launcher == nil {
689 // This shouldn't happen in a real build because we'll error out when adding dependencies
690 // on the stdlib and launcher if they don't exist. But some tests set
691 // AllowMissingDependencies.
692 return out
693 }
694 ctx.Build(pctx, android.BuildParams{
695 Rule: precompile,
696 Input: p.srcsZip,
697 Output: out,
698 Implicits: launcherSharedLibs,
699 Description: "Precompile the python sources of " + ctx.ModuleName(),
700 Args: map[string]string{
701 "stdlibZip": stdLib.String(),
Dan Willemsen339a63f2023-08-15 22:17:03 -0400702 "stdlibPkg": stdLibPkg,
Cole Faust5c503d12023-01-24 11:48:08 -0800703 "launcher": launcher.String(),
704 "ldLibraryPath": strings.Join(ldLibraryPath, ":"),
705 },
706 })
707 return out
708}
709
Cole Faust4d247e62023-01-23 10:14:58 -0800710// isPythonLibModule returns whether the given module is a Python library PythonLibraryModule or not
Nan Zhangd4e641b2017-07-12 12:55:28 -0700711func isPythonLibModule(module blueprint.Module) bool {
Cole Faust4d247e62023-01-23 10:14:58 -0800712 if _, ok := module.(*PythonLibraryModule); ok {
713 if _, ok := module.(*PythonBinaryModule); !ok {
714 return true
715 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700716 }
717 return false
718}
719
Liz Kammerd737d022020-11-16 15:42:51 -0800720// collectPathsFromTransitiveDeps checks for source/data files for duplicate paths
721// for module and its transitive dependencies and collects list of data/source file
722// zips for transitive dependencies.
Cole Faust5c503d12023-01-24 11:48:08 -0800723func (p *PythonLibraryModule) collectPathsFromTransitiveDeps(ctx android.ModuleContext, precompiled bool) android.Paths {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800724 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
725 // check duplicates.
726 destToPySrcs := make(map[string]string)
727 destToPyData := make(map[string]string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800728 for _, path := range p.srcsPathMappings {
729 destToPySrcs[path.dest] = path.src.String()
730 }
731 for _, path := range p.dataPathMappings {
732 destToPyData[path.dest] = path.src.String()
733 }
734
Colin Cross6b753602018-06-21 13:03:07 -0700735 seen := make(map[android.Module]bool)
736
Cole Faust4d247e62023-01-23 10:14:58 -0800737 var result android.Paths
738
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800739 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700740 ctx.WalkDeps(func(child, parent android.Module) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800741 // we only collect dependencies tagged as python library deps
Colin Cross6b753602018-06-21 13:03:07 -0700742 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
743 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800744 }
Colin Cross6b753602018-06-21 13:03:07 -0700745 if seen[child] {
746 return false
747 }
748 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800749 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700750 if !isPythonLibModule(child) {
Liz Kammerd737d022020-11-16 15:42:51 -0800751 ctx.PropertyErrorf("libs",
Nan Zhangd4e641b2017-07-12 12:55:28 -0700752 "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 +0000753 ctx.OtherModuleName(child), ctx.ModuleName())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700754 }
Liz Kammerd737d022020-11-16 15:42:51 -0800755 // collect source and data paths, checking that there are no duplicate output file conflicts
756 if dep, ok := child.(pythonDependency); ok {
757 srcs := dep.getSrcsPathMappings()
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800758 for _, path := range srcs {
Liz Kammerd737d022020-11-16 15:42:51 -0800759 checkForDuplicateOutputPath(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700760 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800761 }
Liz Kammerd737d022020-11-16 15:42:51 -0800762 data := dep.getDataPathMappings()
763 for _, path := range data {
764 checkForDuplicateOutputPath(ctx, destToPyData,
765 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
766 }
Cole Faust5c503d12023-01-24 11:48:08 -0800767 if precompiled {
768 result = append(result, dep.getPrecompiledSrcsZip())
769 } else {
770 result = append(result, dep.getSrcsZip())
771 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800772 }
Colin Cross6b753602018-06-21 13:03:07 -0700773 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800774 })
Cole Faust4d247e62023-01-23 10:14:58 -0800775 return result
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800776}
777
Liz Kammerd737d022020-11-16 15:42:51 -0800778// chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which
779// would result in two files being placed in the same location.
780// If there is a duplicate path, an error is thrown and true is returned
781// Otherwise, outputPath: srcPath is added to m and returns false
782func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool {
783 if oldSrcPath, found := m[outputPath]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700784 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800785 " First file: in module %s at path %q."+
786 " Second file: in module %s at path %q.",
Liz Kammerd737d022020-11-16 15:42:51 -0800787 outputPath, curModule, oldSrcPath, otherModule, srcPath)
788 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800789 }
Liz Kammerd737d022020-11-16 15:42:51 -0800790 m[outputPath] = srcPath
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800791
Liz Kammerd737d022020-11-16 15:42:51 -0800792 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800793}
Nan Zhangea568a42017-11-08 21:20:04 -0800794
Liz Kammerd737d022020-11-16 15:42:51 -0800795// InstallInData returns true as Python is not supported in the system partition
Cole Faust4d247e62023-01-23 10:14:58 -0800796func (p *PythonLibraryModule) InstallInData() bool {
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000797 return true
798}
799
Nan Zhangea568a42017-11-08 21:20:04 -0800800var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800801var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800802var String = proptools.String