blob: 09af62ea55f43ffa0c3b44c738e5a7a709bd0763 [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
Yu Liue98f7062025-01-17 22:52:43 +000025 "android/soong/cc"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080026 "github.com/google/blueprint"
Nan Zhangd4e641b2017-07-12 12:55:28 -070027 "github.com/google/blueprint/proptools"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080028
29 "android/soong/android"
30)
31
Yu Liue98f7062025-01-17 22:52:43 +000032type PythonLibraryInfo struct {
33 SrcsPathMappings []pathMapping
34 DataPathMappings []pathMapping
35 SrcsZip android.Path
36 PrecompiledSrcsZip android.Path
37 PkgPath string
38}
39
40var PythonLibraryInfoProvider = blueprint.NewProvider[PythonLibraryInfo]()
41
Nan Zhangdb0b9a32017-02-27 10:12:13 -080042func init() {
Paul Duffind0890452021-03-17 21:57:08 +000043 registerPythonMutators(android.InitRegistrationContext)
44}
45
46func registerPythonMutators(ctx android.RegistrationContext) {
47 ctx.PreDepsMutators(RegisterPythonPreDepsMutators)
Liz Kammerdd849a82020-06-12 16:38:45 -070048}
49
Liz Kammerd737d022020-11-16 15:42:51 -080050// Exported to support other packages using Python modules in tests.
Liz Kammerdd849a82020-06-12 16:38:45 -070051func RegisterPythonPreDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Crosse0771282024-05-21 15:19:18 -070052 ctx.Transition("python_version", &versionSplitTransitionMutator{})
Nan Zhangdb0b9a32017-02-27 10:12:13 -080053}
54
Liz Kammerd737d022020-11-16 15:42:51 -080055// the version-specific properties that apply to python modules.
Nan Zhangd4e641b2017-07-12 12:55:28 -070056type VersionProperties struct {
Liz Kammerd737d022020-11-16 15:42:51 -080057 // whether the module is required to be built with this version.
58 // Defaults to true for Python 3, and false otherwise.
Liz Kammer59c0eae2021-09-17 17:48:05 -040059 Enabled *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -080060
Liz Kammerd737d022020-11-16 15:42:51 -080061 // list of source files specific to this Python version.
62 // Using the syntax ":module", srcs may reference the outputs of other modules that produce source files,
63 // e.g. genrule or filegroup.
Colin Cross27b922f2019-03-04 22:35:41 -080064 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070065
Liz Kammerd737d022020-11-16 15:42:51 -080066 // list of source files that should not be used to build the Python module for this version.
67 // This is most useful to remove files that are not common to all Python versions.
Colin Cross27b922f2019-03-04 22:35:41 -080068 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080069
Liz Kammerd737d022020-11-16 15:42:51 -080070 // list of the Python libraries used only for this Python version.
Nan Zhangd4e641b2017-07-12 12:55:28 -070071 Libs []string `android:"arch_variant"`
72
Cole Faustf09101e2024-04-18 18:33:15 +000073 // whether the binary is required to be built with embedded launcher for this version, defaults to true.
Liz Kammer59c0eae2021-09-17 17:48:05 -040074 Embedded_launcher *bool // TODO(b/174041232): Remove this property
Nan Zhangdb0b9a32017-02-27 10:12:13 -080075}
76
Liz Kammerd737d022020-11-16 15:42:51 -080077// properties that apply to all python modules
Nan Zhangd4e641b2017-07-12 12:55:28 -070078type BaseProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080079 // the package path prefix within the output artifact at which to place the source/data
80 // files of the current module.
81 // eg. Pkg_path = "a/b/c"; Other packages can reference this module by using
82 // (from a.b.c import ...) statement.
Nan Zhangbea09752018-05-31 12:49:33 -070083 // if left unspecified, all the source/data files path is unchanged within zip file.
Liz Kammer59c0eae2021-09-17 17:48:05 -040084 Pkg_path *string
Nan Zhangd4e641b2017-07-12 12:55:28 -070085
86 // true, if the Python module is used internally, eg, Python std libs.
Liz Kammer59c0eae2021-09-17 17:48:05 -040087 Is_internal *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -080088
89 // list of source (.py) files compatible both with Python2 and Python3 used to compile the
90 // Python module.
91 // srcs may reference the outputs of other modules that produce source files like genrule
92 // or filegroup using the syntax ":module".
93 // Srcs has to be non-empty.
Colin Cross27b922f2019-03-04 22:35:41 -080094 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070095
96 // list of source files that should not be used to build the C/C++ module.
97 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080098 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080099
100 // list of files or filegroup modules that provide data that should be installed alongside
101 // the test. the file extension can be arbitrary except for (.py).
Colin Cross27b922f2019-03-04 22:35:41 -0800102 Data []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800103
Cole Faust65cb40a2024-10-21 15:41:42 -0700104 // Same as data, but will add dependencies on modules using the device's os variation and
105 // the common arch variation. Useful for a host test that wants to embed a module built for
106 // device.
107 Device_common_data []string `android:"path_device_common"`
108
Inseob Kim25f5ae42025-01-07 22:27:58 +0900109 // Same as data, but will add dependencies on modules via a device os variation and the
110 // device's first supported arch's variation. Useful for a host test that wants to embed a
111 // module built for device.
112 Device_first_data []string `android:"path_device_first"`
113
Colin Cross1bc63932020-11-22 20:12:45 -0800114 // list of java modules that provide data that should be installed alongside the test.
115 Java_data []string
116
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800117 // list of the Python libraries compatible both with Python2 and Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700118 Libs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800119
120 Version struct {
Liz Kammerd737d022020-11-16 15:42:51 -0800121 // Python2-specific properties, including whether Python2 is supported for this module
122 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700123 Py2 VersionProperties `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800124
Liz Kammerd737d022020-11-16 15:42:51 -0800125 // Python3-specific properties, including whether Python3 is supported for this module
126 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700127 Py3 VersionProperties `android:"arch_variant"`
128 } `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800129
130 // the actual version each module uses after variations created.
131 // this property name is hidden from users' perspectives, and soong will populate it during
132 // runtime.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700133 Actual_version string `blueprint:"mutated"`
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700134
Liz Kammerd737d022020-11-16 15:42:51 -0800135 // whether the module is required to be built with actual_version.
136 // this is set by the python version mutator based on version-specific properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700137 Enabled *bool `blueprint:"mutated"`
138
Liz Kammerd737d022020-11-16 15:42:51 -0800139 // whether the binary is required to be built with embedded launcher for this actual_version.
140 // this is set by the python version mutator based on version-specific properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700141 Embedded_launcher *bool `blueprint:"mutated"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800142}
143
Liz Kammerd737d022020-11-16 15:42:51 -0800144// Used to store files of current module after expanding dependencies
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800145type pathMapping struct {
146 dest string
147 src android.Path
148}
149
Cole Faust4d247e62023-01-23 10:14:58 -0800150type PythonLibraryModule struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800151 android.ModuleBase
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700152 android.DefaultableModuleBase
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800153
Nan Zhangb8fa1972017-12-22 16:12:00 -0800154 properties BaseProperties
155 protoProperties android.ProtoProperties
Nan Zhangd4e641b2017-07-12 12:55:28 -0700156
157 // initialize before calling Init
158 hod android.HostOrDeviceSupported
159 multilib android.Multilib
160
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800161 // the Python files of current module after expanding source dependencies.
162 // pathMapping: <dest: runfile_path, src: source_path>
163 srcsPathMappings []pathMapping
164
165 // the data files of current module after expanding source dependencies.
166 // pathMapping: <dest: runfile_path, src: source_path>
167 dataPathMappings []pathMapping
168
Cole Faust5c503d12023-01-24 11:48:08 -0800169 // The zip file containing the current module's source/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800170 srcsZip android.Path
Cole Faust5c503d12023-01-24 11:48:08 -0800171
172 // The zip file containing the current module's source/data files, with the
173 // source files precompiled.
174 precompiledSrcsZip android.Path
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700175
176 sourceProperties android.SourceProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800177}
178
Liz Kammerd737d022020-11-16 15:42:51 -0800179// newModule generates new Python base module
Cole Faust4d247e62023-01-23 10:14:58 -0800180func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *PythonLibraryModule {
181 return &PythonLibraryModule{
Nan Zhangd4e641b2017-07-12 12:55:28 -0700182 hod: hod,
183 multilib: multilib,
184 }
185}
186
Liz Kammerd737d022020-11-16 15:42:51 -0800187// getSrcsPathMappings gets this module's path mapping of src source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800188func (p *PythonLibraryModule) getSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800189 return p.srcsPathMappings
190}
191
Liz Kammerd737d022020-11-16 15:42:51 -0800192// getSrcsPathMappings gets this module's path mapping of data source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800193func (p *PythonLibraryModule) getDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800194 return p.dataPathMappings
195}
196
Liz Kammerd737d022020-11-16 15:42:51 -0800197// getSrcsZip returns the filepath where the current module's source/data files are zipped.
Cole Faust4d247e62023-01-23 10:14:58 -0800198func (p *PythonLibraryModule) getSrcsZip() android.Path {
Nan Zhang1db85402017-12-18 13:20:23 -0800199 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800200}
201
Cole Faust5c503d12023-01-24 11:48:08 -0800202// getSrcsZip returns the filepath where the current module's source/data files are zipped.
203func (p *PythonLibraryModule) getPrecompiledSrcsZip() android.Path {
204 return p.precompiledSrcsZip
205}
206
Dan Willemsen339a63f2023-08-15 22:17:03 -0400207// getPkgPath returns the pkg_path value
208func (p *PythonLibraryModule) getPkgPath() string {
209 return String(p.properties.Pkg_path)
210}
211
Cole Faust4d247e62023-01-23 10:14:58 -0800212func (p *PythonLibraryModule) getBaseProperties() *BaseProperties {
213 return &p.properties
214}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800215
Cole Faust4d247e62023-01-23 10:14:58 -0800216func (p *PythonLibraryModule) init() android.Module {
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700217 p.AddProperties(&p.properties, &p.protoProperties, &p.sourceProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700218 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700219 android.InitDefaultableModule(p)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700220 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800221}
222
Liz Kammerd737d022020-11-16 15:42:51 -0800223// Python-specific tag to transfer information on the purpose of a dependency.
224// This is used when adding a dependency on a module, which can later be accessed when visiting
225// dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700226type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800227 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700228 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800229}
230
Liz Kammerd737d022020-11-16 15:42:51 -0800231// Python-specific tag that indicates that installed files of this module should depend on installed
232// files of the dependency
Colin Crosse9fe2942020-11-10 18:12:15 -0800233type installDependencyTag struct {
234 blueprint.BaseDependencyTag
Liz Kammerd737d022020-11-16 15:42:51 -0800235 // embedding this struct provides the installation dependency requirement
Colin Crosse9fe2942020-11-10 18:12:15 -0800236 android.InstallAlwaysNeededDependencyTag
237 name string
238}
239
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800240var (
Cole Faust5c503d12023-01-24 11:48:08 -0800241 pythonLibTag = dependencyTag{name: "pythonLib"}
242 javaDataTag = dependencyTag{name: "javaData"}
243 // The python interpreter, with soong module name "py3-launcher" or "py3-launcher-autorun".
Cole Faust909d2372023-02-13 23:17:40 +0000244 launcherTag = dependencyTag{name: "launcher"}
245 launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"}
Cole Faust5c503d12023-01-24 11:48:08 -0800246 // The python interpreter built for host so that we can precompile python sources.
247 // This only works because the precompiled sources don't vary by architecture.
248 // The soong module name is "py3-launcher".
Cole Faust909d2372023-02-13 23:17:40 +0000249 hostLauncherTag = dependencyTag{name: "hostLauncher"}
250 hostlauncherSharedLibTag = dependencyTag{name: "hostlauncherSharedLib"}
251 hostStdLibTag = dependencyTag{name: "hostStdLib"}
252 pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
253 pyExt = ".py"
254 protoExt = ".proto"
255 pyVersion2 = "PY2"
256 pyVersion3 = "PY3"
257 internalPath = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800258)
259
Cole Faust4d247e62023-01-23 10:14:58 -0800260type basePropertiesProvider interface {
261 getBaseProperties() *BaseProperties
262}
263
Colin Crosse0771282024-05-21 15:19:18 -0700264type versionSplitTransitionMutator struct{}
265
266func (versionSplitTransitionMutator) Split(ctx android.BaseModuleContext) []string {
267 if base, ok := ctx.Module().(basePropertiesProvider); ok {
268 props := base.getBaseProperties()
269 var variants []string
270 // PY3 is first so that we alias the PY3 variant rather than PY2 if both
271 // are available
272 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
273 variants = append(variants, pyVersion3)
274 }
275 if proptools.BoolDefault(props.Version.Py2.Enabled, false) {
Cole Faust4ce4f882024-09-09 18:08:49 -0700276 if ctx.ModuleName() != "py2-cmd" &&
Colin Crosse0771282024-05-21 15:19:18 -0700277 ctx.ModuleName() != "py2-stdlib" {
Cole Faust4ce4f882024-09-09 18:08:49 -0700278 ctx.PropertyErrorf("version.py2.enabled", "Python 2 is no longer supported, please convert to python 3.")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800279 }
Colin Crosse0771282024-05-21 15:19:18 -0700280 variants = append(variants, pyVersion2)
281 }
282 return variants
283 }
284 return []string{""}
285}
286
287func (versionSplitTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
288 return ""
289}
290
291func (versionSplitTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
292 if incomingVariation != "" {
293 return incomingVariation
294 }
295 if base, ok := ctx.Module().(basePropertiesProvider); ok {
296 props := base.getBaseProperties()
297 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
298 return pyVersion3
299 } else {
300 return pyVersion2
301 }
302 }
303
304 return ""
305}
306
307func (versionSplitTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
308 if variation == "" {
309 return
310 }
311 if base, ok := ctx.Module().(basePropertiesProvider); ok {
312 props := base.getBaseProperties()
313 props.Actual_version = variation
314
315 var versionProps *VersionProperties
316 if variation == pyVersion3 {
317 versionProps = &props.Version.Py3
318 } else if variation == pyVersion2 {
319 versionProps = &props.Version.Py2
320 }
321
322 err := proptools.AppendMatchingProperties([]interface{}{props}, versionProps, nil)
323 if err != nil {
324 panic(err)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800325 }
326 }
327}
328
Liz Kammerd737d022020-11-16 15:42:51 -0800329func anyHasExt(paths []string, ext string) bool {
330 for _, p := range paths {
331 if filepath.Ext(p) == ext {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800332 return true
333 }
334 }
335
336 return false
337}
338
Cole Faust4d247e62023-01-23 10:14:58 -0800339func (p *PythonLibraryModule) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800340 return anyHasExt(p.properties.Srcs, ext)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800341}
342
Liz Kammerd737d022020-11-16 15:42:51 -0800343// DepsMutator mutates dependencies for this module:
Colin Crossd079e0b2022-08-16 10:27:33 -0700344// - handles proto dependencies,
345// - if required, specifies launcher and adds launcher dependencies,
346// - applies python version mutations to Python dependencies
Cole Faust4d247e62023-01-23 10:14:58 -0800347func (p *PythonLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700348 android.ProtoDeps(ctx, &p.protoProperties)
349
Colin Crosse20113d2020-11-22 19:37:44 -0800350 versionVariation := []blueprint.Variation{
351 {"python_version", p.properties.Actual_version},
Nan Zhangb8fa1972017-12-22 16:12:00 -0800352 }
Colin Crosse20113d2020-11-22 19:37:44 -0800353
Liz Kammerd737d022020-11-16 15:42:51 -0800354 // If sources contain a proto file, add dependency on libprotobuf-python
355 if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
Colin Crosse20113d2020-11-22 19:37:44 -0800356 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python")
357 }
Liz Kammerd737d022020-11-16 15:42:51 -0800358
359 // Add python library dependencies for this python version variation
Colin Crosse20113d2020-11-22 19:37:44 -0800360 ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700361
Colin Cross1bc63932020-11-22 20:12:45 -0800362 // Emulate the data property for java_data but with the arch variation overridden to "common"
363 // so that it can point to java modules.
364 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
365 ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
Cole Faust5c503d12023-01-24 11:48:08 -0800366
Cole Faust909d2372023-02-13 23:17:40 +0000367 p.AddDepsOnPythonLauncherAndStdlib(ctx, hostStdLibTag, hostLauncherTag, hostlauncherSharedLibTag, false, ctx.Config().BuildOSTarget)
Cole Faust5c503d12023-01-24 11:48:08 -0800368}
369
Cole Faust909d2372023-02-13 23:17:40 +0000370// AddDepsOnPythonLauncherAndStdlib will make the current module depend on the python stdlib,
371// launcher (interpreter), and the launcher's shared libraries. If autorun is true, it will use
372// the autorun launcher instead of the regular one. This function acceps a targetForDeps argument
373// as the target to use for these dependencies. For embedded launcher python binaries, the launcher
374// that will be embedded will be under the same target as the python module itself. But when
375// precompiling python code, we need to get the python launcher built for host, even if we're
376// compiling the python module for device, so we pass a different target to this function.
Cole Faust5c503d12023-01-24 11:48:08 -0800377func (p *PythonLibraryModule) AddDepsOnPythonLauncherAndStdlib(ctx android.BottomUpMutatorContext,
Cole Faust909d2372023-02-13 23:17:40 +0000378 stdLibTag, launcherTag, launcherSharedLibTag blueprint.DependencyTag,
Cole Faust5c503d12023-01-24 11:48:08 -0800379 autorun bool, targetForDeps android.Target) {
380 var stdLib string
381 var launcherModule string
Cole Faust909d2372023-02-13 23:17:40 +0000382 // Add launcher shared lib dependencies. Ideally, these should be
383 // derived from the `shared_libs` property of the launcher. TODO: read these from
384 // the python launcher itself using ctx.OtherModuleProvider() or similar on the result
385 // of ctx.AddFarVariationDependencies()
386 launcherSharedLibDeps := []string{
387 "libsqlite",
388 }
389 // Add launcher-specific dependencies for bionic
390 if targetForDeps.Os.Bionic() {
391 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm")
392 }
393 if targetForDeps.Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() {
394 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc_musl")
395 }
Cole Faust5c503d12023-01-24 11:48:08 -0800396
397 switch p.properties.Actual_version {
398 case pyVersion2:
399 stdLib = "py2-stdlib"
400
401 launcherModule = "py2-launcher"
402 if autorun {
403 launcherModule = "py2-launcher-autorun"
404 }
405
Cole Faust909d2372023-02-13 23:17:40 +0000406 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++")
Cole Faust5c503d12023-01-24 11:48:08 -0800407 case pyVersion3:
Dan Willemsen339a63f2023-08-15 22:17:03 -0400408 var prebuiltStdLib bool
409 if targetForDeps.Os.Bionic() {
410 prebuiltStdLib = false
411 } else if ctx.Config().VendorConfig("cpython3").Bool("force_build_host") {
412 prebuiltStdLib = false
413 } else {
414 prebuiltStdLib = true
415 }
416
417 if prebuiltStdLib {
418 stdLib = "py3-stdlib-prebuilt"
419 } else {
420 stdLib = "py3-stdlib"
421 }
Cole Faust5c503d12023-01-24 11:48:08 -0800422
423 launcherModule = "py3-launcher"
424 if autorun {
425 launcherModule = "py3-launcher-autorun"
426 }
427 if ctx.Config().HostStaticBinaries() && targetForDeps.Os == android.LinuxMusl {
428 launcherModule += "-static"
429 }
Cole Faust909d2372023-02-13 23:17:40 +0000430 if ctx.Device() {
431 launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog")
432 }
Cole Faust5c503d12023-01-24 11:48:08 -0800433 default:
434 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
435 p.properties.Actual_version, ctx.ModuleName()))
436 }
437 targetVariations := targetForDeps.Variations()
438 if ctx.ModuleName() != stdLib {
439 stdLibVariations := make([]blueprint.Variation, 0, len(targetVariations)+1)
440 stdLibVariations = append(stdLibVariations, blueprint.Variation{Mutator: "python_version", Variation: p.properties.Actual_version})
441 stdLibVariations = append(stdLibVariations, targetVariations...)
442 // Using AddFarVariationDependencies for all of these because they can be for a different
443 // platform, like if the python module itself was being compiled for device, we may want
444 // the python interpreter built for host so that we can precompile python sources.
445 ctx.AddFarVariationDependencies(stdLibVariations, stdLibTag, stdLib)
446 }
447 ctx.AddFarVariationDependencies(targetVariations, launcherTag, launcherModule)
Cole Faust909d2372023-02-13 23:17:40 +0000448 ctx.AddFarVariationDependencies(targetVariations, launcherSharedLibTag, launcherSharedLibDeps...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800449}
450
Cole Faust4d247e62023-01-23 10:14:58 -0800451// GenerateAndroidBuildActions performs build actions common to all Python modules
452func (p *PythonLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammerd737d022020-11-16 15:42:51 -0800453 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs)
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700454 // Keep before any early returns.
455 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
456 TestOnly: Bool(p.sourceProperties.Test_only),
457 TopLevelTarget: p.sourceProperties.Top_level_test_target,
458 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800459
460 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800461 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700462 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, p.properties.Device_common_data)...)
Inseob Kim25f5ae42025-01-07 22:27:58 +0900463 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, p.properties.Device_first_data)...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800464
Colin Cross1bc63932020-11-22 20:12:45 -0800465 // Emulate the data property for java_data dependencies.
Yu Liue98f7062025-01-17 22:52:43 +0000466 for _, javaData := range ctx.GetDirectDepsProxyWithTag(javaDataTag) {
Colin Cross1bc63932020-11-22 20:12:45 -0800467 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
468 }
469
Liz Kammerd737d022020-11-16 15:42:51 -0800470 // Validate pkg_path property
Nan Zhang1db85402017-12-18 13:20:23 -0800471 pkgPath := String(p.properties.Pkg_path)
472 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800473 // TODO: export validation from android/paths.go handling to replace this duplicated functionality
Nan Zhang1db85402017-12-18 13:20:23 -0800474 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
475 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
476 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700477 ctx.PropertyErrorf("pkg_path",
478 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800479 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700480 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800481 }
Liz Kammerd737d022020-11-16 15:42:51 -0800482 }
483 // If property Is_internal is set, prepend pkgPath with internalPath
484 if proptools.BoolDefault(p.properties.Is_internal, false) {
485 pkgPath = filepath.Join(internalPath, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800486 }
487
Liz Kammerd737d022020-11-16 15:42:51 -0800488 // generate src:destination path mappings for this module
Nan Zhang1db85402017-12-18 13:20:23 -0800489 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800490
Liz Kammerd737d022020-11-16 15:42:51 -0800491 // generate the zipfile of all source and data files
Nan Zhang1db85402017-12-18 13:20:23 -0800492 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Dan Willemsenfe2dafc2023-08-24 22:59:16 +0000493 p.precompiledSrcsZip = p.precompileSrcs(ctx)
Yu Liue98f7062025-01-17 22:52:43 +0000494
495 android.SetProvider(ctx, PythonLibraryInfoProvider, PythonLibraryInfo{
496 SrcsPathMappings: p.getSrcsPathMappings(),
497 DataPathMappings: p.getDataPathMappings(),
498 SrcsZip: p.getSrcsZip(),
499 PkgPath: p.getPkgPath(),
500 PrecompiledSrcsZip: p.getPrecompiledSrcsZip(),
501 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800502}
503
Liz Kammerd737d022020-11-16 15:42:51 -0800504func isValidPythonPath(path string) error {
505 identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/")
506 for _, token := range identifiers {
507 if !pathComponentRegexp.MatchString(token) {
508 return fmt.Errorf("the path %q contains invalid subpath %q. "+
509 "Subpaths must be at least one character long. "+
510 "The first character must an underscore or letter. "+
511 "Following characters may be any of: letter, digit, underscore, hyphen.",
512 path, token)
513 }
514 }
515 return nil
516}
517
518// For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path>
519// for python/data files expanded from properties.
Cole Faust4d247e62023-01-23 10:14:58 -0800520func (p *PythonLibraryModule) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800521 expandedSrcs, expandedData android.Paths) {
522 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800523 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800524 destToPySrcs := make(map[string]string)
525 destToPyData := make(map[string]string)
526
Dan Willemsen339a63f2023-08-15 22:17:03 -0400527 // Disable path checks for the stdlib, as it includes a "." in the version string
528 isInternal := proptools.BoolDefault(p.properties.Is_internal, false)
529
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800530 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800531 if s.Ext() != pyExt && s.Ext() != protoExt {
532 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800533 continue
534 }
Nan Zhang1db85402017-12-18 13:20:23 -0800535 runfilesPath := filepath.Join(pkgPath, s.Rel())
Dan Willemsen339a63f2023-08-15 22:17:03 -0400536 if !isInternal {
537 if err := isValidPythonPath(runfilesPath); err != nil {
538 ctx.PropertyErrorf("srcs", err.Error())
539 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800540 }
Liz Kammerd737d022020-11-16 15:42:51 -0800541 if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
542 p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800543 }
544 }
545
546 for _, d := range expandedData {
Raphael Blistein59858462024-05-08 16:15:53 +0000547 if d.Ext() == pyExt {
548 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800549 continue
550 }
Nan Zhang1db85402017-12-18 13:20:23 -0800551 runfilesPath := filepath.Join(pkgPath, d.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800552 if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800553 p.dataPathMappings = append(p.dataPathMappings,
554 pathMapping{dest: runfilesPath, src: d})
555 }
556 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800557}
558
Liz Kammerd737d022020-11-16 15:42:51 -0800559// createSrcsZip registers build actions to zip current module's sources and data.
Cole Faust4d247e62023-01-23 10:14:58 -0800560func (p *PythonLibraryModule) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800561 relativeRootMap := make(map[string]android.Paths)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800562 var protoSrcs android.Paths
Cole Faust5c503d12023-01-24 11:48:08 -0800563 addPathMapping := func(path pathMapping) {
Raphael Blistein59858462024-05-08 16:15:53 +0000564 relativeRoot := strings.TrimSuffix(path.src.String(), path.src.Rel())
565 relativeRootMap[relativeRoot] = append(relativeRootMap[relativeRoot], path.src)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800566 }
Cole Faust5c503d12023-01-24 11:48:08 -0800567
568 // "srcs" or "data" properties may contain filegroups so it might happen that
569 // the root directory for each source path is different.
570 for _, path := range p.srcsPathMappings {
Raphael Blistein59858462024-05-08 16:15:53 +0000571 // handle proto sources separately
572 if path.src.Ext() == protoExt {
573 protoSrcs = append(protoSrcs, path.src)
574 } else {
575 addPathMapping(path)
576 }
Cole Faust5c503d12023-01-24 11:48:08 -0800577 }
578 for _, path := range p.dataPathMappings {
579 addPathMapping(path)
580 }
581
Nan Zhangb8fa1972017-12-22 16:12:00 -0800582 var zips android.Paths
583 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700584 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
585 protoFlags.OutTypeFlag = "--python_out"
586
Cole Faustcaf766b2022-10-21 16:07:56 -0700587 if pkgPath != "" {
Cole Faust43ac21f2022-09-19 11:19:52 -0700588 pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path")
589 rule := android.NewRuleBuilder(pctx, ctx)
590 var stagedProtoSrcs android.Paths
591 for _, srcFile := range protoSrcs {
592 stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel())
Cole Faust43ac21f2022-09-19 11:19:52 -0700593 rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc)
594 stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc)
595 }
596 rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path")
597 protoSrcs = stagedProtoSrcs
Cole Faust43ac21f2022-09-19 11:19:52 -0700598 }
599
Nan Zhangb8fa1972017-12-22 16:12:00 -0800600 for _, srcFile := range protoSrcs {
Cole Faustcaf766b2022-10-21 16:07:56 -0700601 zip := genProto(ctx, srcFile, protoFlags)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800602 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800603 }
604 }
605
Nan Zhangb8fa1972017-12-22 16:12:00 -0800606 if len(relativeRootMap) > 0 {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800607 // in order to keep stable order of soong_zip params, we sort the keys here.
Cole Faust18994c72023-02-28 16:02:16 -0800608 roots := android.SortedKeys(relativeRootMap)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800609
Cole Faust01243362022-06-02 12:11:12 -0700610 // Use -symlinks=false so that the symlinks in the bazel output directory are followed
611 parArgs := []string{"-symlinks=false"}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700612 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800613 // use package path as path prefix
Nan Zhangf0c4e432018-05-22 14:50:18 -0700614 parArgs = append(parArgs, `-P `+pkgPath)
615 }
Liz Kammerd737d022020-11-16 15:42:51 -0800616 paths := android.Paths{}
617 for _, root := range roots {
618 // specify relative root of file in following -f arguments
619 parArgs = append(parArgs, `-C `+root)
620 for _, path := range relativeRootMap[root] {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800621 parArgs = append(parArgs, `-f `+path.String())
Liz Kammerd737d022020-11-16 15:42:51 -0800622 paths = append(paths, path)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800623 }
624 }
625
626 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
627 ctx.Build(pctx, android.BuildParams{
628 Rule: zip,
629 Description: "python library archive",
630 Output: origSrcsZip,
Liz Kammerd737d022020-11-16 15:42:51 -0800631 // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits
632 Implicits: paths,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800633 Args: map[string]string{
634 "args": strings.Join(parArgs, " "),
635 },
636 })
637 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800638 }
Liz Kammerd737d022020-11-16 15:42:51 -0800639 // we may have multiple zips due to separate handling of proto source files
Nan Zhangb8fa1972017-12-22 16:12:00 -0800640 if len(zips) == 1 {
641 return zips[0]
642 } else {
643 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
644 ctx.Build(pctx, android.BuildParams{
645 Rule: combineZip,
646 Description: "combine python library archive",
647 Output: combinedSrcsZip,
648 Inputs: zips,
649 })
650 return combinedSrcsZip
651 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800652}
653
Cole Faust5c503d12023-01-24 11:48:08 -0800654func (p *PythonLibraryModule) precompileSrcs(ctx android.ModuleContext) android.Path {
655 // To precompile the python sources, we need a python interpreter and stdlib built
656 // for host. We then use those to compile the python sources, which may be used on either
657 // host of device. Python bytecode is architecture agnostic, so we're essentially
658 // "cross compiling" for device here purely by virtue of host and device python bytecode
659 // being the same.
660 var stdLib android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400661 var stdLibPkg string
Cole Faust5c503d12023-01-24 11:48:08 -0800662 var launcher android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400663 if proptools.BoolDefault(p.properties.Is_internal, false) {
Cole Faust5c503d12023-01-24 11:48:08 -0800664 stdLib = p.srcsZip
Dan Willemsen339a63f2023-08-15 22:17:03 -0400665 stdLibPkg = p.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800666 } else {
Yu Liue98f7062025-01-17 22:52:43 +0000667 ctx.VisitDirectDepsProxyWithTag(hostStdLibTag, func(module android.ModuleProxy) {
668 if dep, ok := android.OtherModuleProvider(ctx, module, PythonLibraryInfoProvider); ok {
669 stdLib = dep.PrecompiledSrcsZip
670 stdLibPkg = dep.PkgPath
Cole Faust5c503d12023-01-24 11:48:08 -0800671 }
672 })
673 }
Yu Liue98f7062025-01-17 22:52:43 +0000674 ctx.VisitDirectDepsProxyWithTag(hostLauncherTag, func(module android.ModuleProxy) {
675 if dep, ok := android.OtherModuleProvider(ctx, module, cc.LinkableInfoProvider); ok {
676 optionalLauncher := dep.OutputFile
Cole Faust5c503d12023-01-24 11:48:08 -0800677 if optionalLauncher.Valid() {
678 launcher = optionalLauncher.Path()
679 }
Cole Faust909d2372023-02-13 23:17:40 +0000680 }
681 })
682 var launcherSharedLibs android.Paths
683 var ldLibraryPath []string
Yu Liue98f7062025-01-17 22:52:43 +0000684 ctx.VisitDirectDepsProxyWithTag(hostlauncherSharedLibTag, func(module android.ModuleProxy) {
685 if dep, ok := android.OtherModuleProvider(ctx, module, cc.LinkableInfoProvider); ok {
686 optionalPath := dep.OutputFile
Cole Faust909d2372023-02-13 23:17:40 +0000687 if optionalPath.Valid() {
688 launcherSharedLibs = append(launcherSharedLibs, optionalPath.Path())
689 ldLibraryPath = append(ldLibraryPath, filepath.Dir(optionalPath.Path().String()))
Cole Faust5c503d12023-01-24 11:48:08 -0800690 }
691 }
692 })
693
694 out := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszipprecompiled")
695 if stdLib == nil || launcher == nil {
696 // This shouldn't happen in a real build because we'll error out when adding dependencies
697 // on the stdlib and launcher if they don't exist. But some tests set
698 // AllowMissingDependencies.
699 return out
700 }
701 ctx.Build(pctx, android.BuildParams{
702 Rule: precompile,
703 Input: p.srcsZip,
704 Output: out,
705 Implicits: launcherSharedLibs,
706 Description: "Precompile the python sources of " + ctx.ModuleName(),
707 Args: map[string]string{
708 "stdlibZip": stdLib.String(),
Dan Willemsen339a63f2023-08-15 22:17:03 -0400709 "stdlibPkg": stdLibPkg,
Cole Faust5c503d12023-01-24 11:48:08 -0800710 "launcher": launcher.String(),
711 "ldLibraryPath": strings.Join(ldLibraryPath, ":"),
712 },
713 })
714 return out
715}
716
Liz Kammerd737d022020-11-16 15:42:51 -0800717// collectPathsFromTransitiveDeps checks for source/data files for duplicate paths
718// for module and its transitive dependencies and collects list of data/source file
719// zips for transitive dependencies.
Cole Faust5c503d12023-01-24 11:48:08 -0800720func (p *PythonLibraryModule) collectPathsFromTransitiveDeps(ctx android.ModuleContext, precompiled bool) android.Paths {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800721 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
722 // check duplicates.
723 destToPySrcs := make(map[string]string)
724 destToPyData := make(map[string]string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800725 for _, path := range p.srcsPathMappings {
726 destToPySrcs[path.dest] = path.src.String()
727 }
728 for _, path := range p.dataPathMappings {
729 destToPyData[path.dest] = path.src.String()
730 }
731
Colin Cross6b753602018-06-21 13:03:07 -0700732 seen := make(map[android.Module]bool)
733
Cole Faust4d247e62023-01-23 10:14:58 -0800734 var result android.Paths
735
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800736 // visit all its dependencies in depth first.
Yu Liue98f7062025-01-17 22:52:43 +0000737 ctx.WalkDepsProxy(func(child, _ android.ModuleProxy) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800738 // we only collect dependencies tagged as python library deps
Colin Cross6b753602018-06-21 13:03:07 -0700739 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
740 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800741 }
Colin Cross6b753602018-06-21 13:03:07 -0700742 if seen[child] {
743 return false
744 }
745 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800746 // Python modules only can depend on Python libraries.
Yu Liue98f7062025-01-17 22:52:43 +0000747 dep, isLibrary := android.OtherModuleProvider(ctx, child, PythonLibraryInfoProvider)
748 _, isBinary := android.OtherModuleProvider(ctx, child, PythonBinaryInfoProvider)
749 if !isLibrary || isBinary {
Liz Kammerd737d022020-11-16 15:42:51 -0800750 ctx.PropertyErrorf("libs",
Nan Zhangd4e641b2017-07-12 12:55:28 -0700751 "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 +0000752 ctx.OtherModuleName(child), ctx.ModuleName())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700753 }
Liz Kammerd737d022020-11-16 15:42:51 -0800754 // collect source and data paths, checking that there are no duplicate output file conflicts
Yu Liue98f7062025-01-17 22:52:43 +0000755 if isLibrary {
756 srcs := dep.SrcsPathMappings
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800757 for _, path := range srcs {
Liz Kammerd737d022020-11-16 15:42:51 -0800758 checkForDuplicateOutputPath(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700759 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800760 }
Yu Liue98f7062025-01-17 22:52:43 +0000761 data := dep.DataPathMappings
Liz Kammerd737d022020-11-16 15:42:51 -0800762 for _, path := range data {
763 checkForDuplicateOutputPath(ctx, destToPyData,
764 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
765 }
Cole Faust5c503d12023-01-24 11:48:08 -0800766 if precompiled {
Yu Liue98f7062025-01-17 22:52:43 +0000767 result = append(result, dep.PrecompiledSrcsZip)
Cole Faust5c503d12023-01-24 11:48:08 -0800768 } else {
Yu Liue98f7062025-01-17 22:52:43 +0000769 result = append(result, dep.SrcsZip)
Cole Faust5c503d12023-01-24 11:48:08 -0800770 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800771 }
Colin Cross6b753602018-06-21 13:03:07 -0700772 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800773 })
Cole Faust4d247e62023-01-23 10:14:58 -0800774 return result
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800775}
776
Liz Kammerd737d022020-11-16 15:42:51 -0800777// chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which
778// would result in two files being placed in the same location.
779// If there is a duplicate path, an error is thrown and true is returned
780// Otherwise, outputPath: srcPath is added to m and returns false
781func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool {
782 if oldSrcPath, found := m[outputPath]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700783 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800784 " First file: in module %s at path %q."+
785 " Second file: in module %s at path %q.",
Liz Kammerd737d022020-11-16 15:42:51 -0800786 outputPath, curModule, oldSrcPath, otherModule, srcPath)
787 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800788 }
Liz Kammerd737d022020-11-16 15:42:51 -0800789 m[outputPath] = srcPath
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800790
Liz Kammerd737d022020-11-16 15:42:51 -0800791 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800792}
Nan Zhangea568a42017-11-08 21:20:04 -0800793
Liz Kammerd737d022020-11-16 15:42:51 -0800794// InstallInData returns true as Python is not supported in the system partition
Cole Faust4d247e62023-01-23 10:14:58 -0800795func (p *PythonLibraryModule) InstallInData() bool {
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000796 return true
797}
798
Nan Zhangea568a42017-11-08 21:20:04 -0800799var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800800var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800801var String = proptools.String