blob: d3e5743b52dea9f49096be9a5975ebab88ea88ed [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)
Colin Cross40213022023-12-13 15:19:49 -0800450 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: expandedSrcs.Strings()})
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700451 // Keep before any early returns.
452 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
453 TestOnly: Bool(p.sourceProperties.Test_only),
454 TopLevelTarget: p.sourceProperties.Top_level_test_target,
455 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800456
457 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800458 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700459 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, p.properties.Device_common_data)...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800460
Colin Cross1bc63932020-11-22 20:12:45 -0800461 // Emulate the data property for java_data dependencies.
462 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
463 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
464 }
465
Liz Kammerd737d022020-11-16 15:42:51 -0800466 // Validate pkg_path property
Nan Zhang1db85402017-12-18 13:20:23 -0800467 pkgPath := String(p.properties.Pkg_path)
468 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800469 // TODO: export validation from android/paths.go handling to replace this duplicated functionality
Nan Zhang1db85402017-12-18 13:20:23 -0800470 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
471 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
472 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700473 ctx.PropertyErrorf("pkg_path",
474 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800475 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700476 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800477 }
Liz Kammerd737d022020-11-16 15:42:51 -0800478 }
479 // If property Is_internal is set, prepend pkgPath with internalPath
480 if proptools.BoolDefault(p.properties.Is_internal, false) {
481 pkgPath = filepath.Join(internalPath, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800482 }
483
Liz Kammerd737d022020-11-16 15:42:51 -0800484 // generate src:destination path mappings for this module
Nan Zhang1db85402017-12-18 13:20:23 -0800485 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800486
Liz Kammerd737d022020-11-16 15:42:51 -0800487 // generate the zipfile of all source and data files
Nan Zhang1db85402017-12-18 13:20:23 -0800488 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Dan Willemsenfe2dafc2023-08-24 22:59:16 +0000489 p.precompiledSrcsZip = p.precompileSrcs(ctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800490}
491
Liz Kammerd737d022020-11-16 15:42:51 -0800492func isValidPythonPath(path string) error {
493 identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/")
494 for _, token := range identifiers {
495 if !pathComponentRegexp.MatchString(token) {
496 return fmt.Errorf("the path %q contains invalid subpath %q. "+
497 "Subpaths must be at least one character long. "+
498 "The first character must an underscore or letter. "+
499 "Following characters may be any of: letter, digit, underscore, hyphen.",
500 path, token)
501 }
502 }
503 return nil
504}
505
506// For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path>
507// for python/data files expanded from properties.
Cole Faust4d247e62023-01-23 10:14:58 -0800508func (p *PythonLibraryModule) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800509 expandedSrcs, expandedData android.Paths) {
510 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800511 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800512 destToPySrcs := make(map[string]string)
513 destToPyData := make(map[string]string)
514
Dan Willemsen339a63f2023-08-15 22:17:03 -0400515 // Disable path checks for the stdlib, as it includes a "." in the version string
516 isInternal := proptools.BoolDefault(p.properties.Is_internal, false)
517
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800518 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800519 if s.Ext() != pyExt && s.Ext() != protoExt {
520 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800521 continue
522 }
Nan Zhang1db85402017-12-18 13:20:23 -0800523 runfilesPath := filepath.Join(pkgPath, s.Rel())
Dan Willemsen339a63f2023-08-15 22:17:03 -0400524 if !isInternal {
525 if err := isValidPythonPath(runfilesPath); err != nil {
526 ctx.PropertyErrorf("srcs", err.Error())
527 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800528 }
Liz Kammerd737d022020-11-16 15:42:51 -0800529 if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
530 p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800531 }
532 }
533
534 for _, d := range expandedData {
Raphael Blistein59858462024-05-08 16:15:53 +0000535 if d.Ext() == pyExt {
536 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800537 continue
538 }
Nan Zhang1db85402017-12-18 13:20:23 -0800539 runfilesPath := filepath.Join(pkgPath, d.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800540 if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800541 p.dataPathMappings = append(p.dataPathMappings,
542 pathMapping{dest: runfilesPath, src: d})
543 }
544 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800545}
546
Liz Kammerd737d022020-11-16 15:42:51 -0800547// createSrcsZip registers build actions to zip current module's sources and data.
Cole Faust4d247e62023-01-23 10:14:58 -0800548func (p *PythonLibraryModule) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800549 relativeRootMap := make(map[string]android.Paths)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800550 var protoSrcs android.Paths
Cole Faust5c503d12023-01-24 11:48:08 -0800551 addPathMapping := func(path pathMapping) {
Raphael Blistein59858462024-05-08 16:15:53 +0000552 relativeRoot := strings.TrimSuffix(path.src.String(), path.src.Rel())
553 relativeRootMap[relativeRoot] = append(relativeRootMap[relativeRoot], path.src)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800554 }
Cole Faust5c503d12023-01-24 11:48:08 -0800555
556 // "srcs" or "data" properties may contain filegroups so it might happen that
557 // the root directory for each source path is different.
558 for _, path := range p.srcsPathMappings {
Raphael Blistein59858462024-05-08 16:15:53 +0000559 // handle proto sources separately
560 if path.src.Ext() == protoExt {
561 protoSrcs = append(protoSrcs, path.src)
562 } else {
563 addPathMapping(path)
564 }
Cole Faust5c503d12023-01-24 11:48:08 -0800565 }
566 for _, path := range p.dataPathMappings {
567 addPathMapping(path)
568 }
569
Nan Zhangb8fa1972017-12-22 16:12:00 -0800570 var zips android.Paths
571 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700572 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
573 protoFlags.OutTypeFlag = "--python_out"
574
Cole Faustcaf766b2022-10-21 16:07:56 -0700575 if pkgPath != "" {
Cole Faust43ac21f2022-09-19 11:19:52 -0700576 pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path")
577 rule := android.NewRuleBuilder(pctx, ctx)
578 var stagedProtoSrcs android.Paths
579 for _, srcFile := range protoSrcs {
580 stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel())
Cole Faust43ac21f2022-09-19 11:19:52 -0700581 rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc)
582 stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc)
583 }
584 rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path")
585 protoSrcs = stagedProtoSrcs
Cole Faust43ac21f2022-09-19 11:19:52 -0700586 }
587
Nan Zhangb8fa1972017-12-22 16:12:00 -0800588 for _, srcFile := range protoSrcs {
Cole Faustcaf766b2022-10-21 16:07:56 -0700589 zip := genProto(ctx, srcFile, protoFlags)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800590 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800591 }
592 }
593
Nan Zhangb8fa1972017-12-22 16:12:00 -0800594 if len(relativeRootMap) > 0 {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800595 // in order to keep stable order of soong_zip params, we sort the keys here.
Cole Faust18994c72023-02-28 16:02:16 -0800596 roots := android.SortedKeys(relativeRootMap)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800597
Cole Faust01243362022-06-02 12:11:12 -0700598 // Use -symlinks=false so that the symlinks in the bazel output directory are followed
599 parArgs := []string{"-symlinks=false"}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700600 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800601 // use package path as path prefix
Nan Zhangf0c4e432018-05-22 14:50:18 -0700602 parArgs = append(parArgs, `-P `+pkgPath)
603 }
Liz Kammerd737d022020-11-16 15:42:51 -0800604 paths := android.Paths{}
605 for _, root := range roots {
606 // specify relative root of file in following -f arguments
607 parArgs = append(parArgs, `-C `+root)
608 for _, path := range relativeRootMap[root] {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800609 parArgs = append(parArgs, `-f `+path.String())
Liz Kammerd737d022020-11-16 15:42:51 -0800610 paths = append(paths, path)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800611 }
612 }
613
614 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
615 ctx.Build(pctx, android.BuildParams{
616 Rule: zip,
617 Description: "python library archive",
618 Output: origSrcsZip,
Liz Kammerd737d022020-11-16 15:42:51 -0800619 // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits
620 Implicits: paths,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800621 Args: map[string]string{
622 "args": strings.Join(parArgs, " "),
623 },
624 })
625 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800626 }
Liz Kammerd737d022020-11-16 15:42:51 -0800627 // we may have multiple zips due to separate handling of proto source files
Nan Zhangb8fa1972017-12-22 16:12:00 -0800628 if len(zips) == 1 {
629 return zips[0]
630 } else {
631 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
632 ctx.Build(pctx, android.BuildParams{
633 Rule: combineZip,
634 Description: "combine python library archive",
635 Output: combinedSrcsZip,
636 Inputs: zips,
637 })
638 return combinedSrcsZip
639 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800640}
641
Cole Faust5c503d12023-01-24 11:48:08 -0800642func (p *PythonLibraryModule) precompileSrcs(ctx android.ModuleContext) android.Path {
643 // To precompile the python sources, we need a python interpreter and stdlib built
644 // for host. We then use those to compile the python sources, which may be used on either
645 // host of device. Python bytecode is architecture agnostic, so we're essentially
646 // "cross compiling" for device here purely by virtue of host and device python bytecode
647 // being the same.
648 var stdLib android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400649 var stdLibPkg string
Cole Faust5c503d12023-01-24 11:48:08 -0800650 var launcher android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400651 if proptools.BoolDefault(p.properties.Is_internal, false) {
Cole Faust5c503d12023-01-24 11:48:08 -0800652 stdLib = p.srcsZip
Dan Willemsen339a63f2023-08-15 22:17:03 -0400653 stdLibPkg = p.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800654 } else {
655 ctx.VisitDirectDepsWithTag(hostStdLibTag, func(module android.Module) {
656 if dep, ok := module.(pythonDependency); ok {
657 stdLib = dep.getPrecompiledSrcsZip()
Dan Willemsen339a63f2023-08-15 22:17:03 -0400658 stdLibPkg = dep.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800659 }
660 })
661 }
662 ctx.VisitDirectDepsWithTag(hostLauncherTag, func(module android.Module) {
663 if dep, ok := module.(IntermPathProvider); ok {
664 optionalLauncher := dep.IntermPathForModuleOut()
665 if optionalLauncher.Valid() {
666 launcher = optionalLauncher.Path()
667 }
Cole Faust909d2372023-02-13 23:17:40 +0000668 }
669 })
670 var launcherSharedLibs android.Paths
671 var ldLibraryPath []string
672 ctx.VisitDirectDepsWithTag(hostlauncherSharedLibTag, func(module android.Module) {
673 if dep, ok := module.(IntermPathProvider); ok {
674 optionalPath := dep.IntermPathForModuleOut()
675 if optionalPath.Valid() {
676 launcherSharedLibs = append(launcherSharedLibs, optionalPath.Path())
677 ldLibraryPath = append(ldLibraryPath, filepath.Dir(optionalPath.Path().String()))
Cole Faust5c503d12023-01-24 11:48:08 -0800678 }
679 }
680 })
681
682 out := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszipprecompiled")
683 if stdLib == nil || launcher == nil {
684 // This shouldn't happen in a real build because we'll error out when adding dependencies
685 // on the stdlib and launcher if they don't exist. But some tests set
686 // AllowMissingDependencies.
687 return out
688 }
689 ctx.Build(pctx, android.BuildParams{
690 Rule: precompile,
691 Input: p.srcsZip,
692 Output: out,
693 Implicits: launcherSharedLibs,
694 Description: "Precompile the python sources of " + ctx.ModuleName(),
695 Args: map[string]string{
696 "stdlibZip": stdLib.String(),
Dan Willemsen339a63f2023-08-15 22:17:03 -0400697 "stdlibPkg": stdLibPkg,
Cole Faust5c503d12023-01-24 11:48:08 -0800698 "launcher": launcher.String(),
699 "ldLibraryPath": strings.Join(ldLibraryPath, ":"),
700 },
701 })
702 return out
703}
704
Cole Faust4d247e62023-01-23 10:14:58 -0800705// isPythonLibModule returns whether the given module is a Python library PythonLibraryModule or not
Nan Zhangd4e641b2017-07-12 12:55:28 -0700706func isPythonLibModule(module blueprint.Module) bool {
Cole Faust4d247e62023-01-23 10:14:58 -0800707 if _, ok := module.(*PythonLibraryModule); ok {
708 if _, ok := module.(*PythonBinaryModule); !ok {
709 return true
710 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700711 }
712 return false
713}
714
Liz Kammerd737d022020-11-16 15:42:51 -0800715// collectPathsFromTransitiveDeps checks for source/data files for duplicate paths
716// for module and its transitive dependencies and collects list of data/source file
717// zips for transitive dependencies.
Cole Faust5c503d12023-01-24 11:48:08 -0800718func (p *PythonLibraryModule) collectPathsFromTransitiveDeps(ctx android.ModuleContext, precompiled bool) android.Paths {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800719 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
720 // check duplicates.
721 destToPySrcs := make(map[string]string)
722 destToPyData := make(map[string]string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800723 for _, path := range p.srcsPathMappings {
724 destToPySrcs[path.dest] = path.src.String()
725 }
726 for _, path := range p.dataPathMappings {
727 destToPyData[path.dest] = path.src.String()
728 }
729
Colin Cross6b753602018-06-21 13:03:07 -0700730 seen := make(map[android.Module]bool)
731
Cole Faust4d247e62023-01-23 10:14:58 -0800732 var result android.Paths
733
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800734 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700735 ctx.WalkDeps(func(child, parent android.Module) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800736 // we only collect dependencies tagged as python library deps
Colin Cross6b753602018-06-21 13:03:07 -0700737 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
738 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800739 }
Colin Cross6b753602018-06-21 13:03:07 -0700740 if seen[child] {
741 return false
742 }
743 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800744 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700745 if !isPythonLibModule(child) {
Liz Kammerd737d022020-11-16 15:42:51 -0800746 ctx.PropertyErrorf("libs",
Nan Zhangd4e641b2017-07-12 12:55:28 -0700747 "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 +0000748 ctx.OtherModuleName(child), ctx.ModuleName())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700749 }
Liz Kammerd737d022020-11-16 15:42:51 -0800750 // collect source and data paths, checking that there are no duplicate output file conflicts
751 if dep, ok := child.(pythonDependency); ok {
752 srcs := dep.getSrcsPathMappings()
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800753 for _, path := range srcs {
Liz Kammerd737d022020-11-16 15:42:51 -0800754 checkForDuplicateOutputPath(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700755 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800756 }
Liz Kammerd737d022020-11-16 15:42:51 -0800757 data := dep.getDataPathMappings()
758 for _, path := range data {
759 checkForDuplicateOutputPath(ctx, destToPyData,
760 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
761 }
Cole Faust5c503d12023-01-24 11:48:08 -0800762 if precompiled {
763 result = append(result, dep.getPrecompiledSrcsZip())
764 } else {
765 result = append(result, dep.getSrcsZip())
766 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800767 }
Colin Cross6b753602018-06-21 13:03:07 -0700768 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800769 })
Cole Faust4d247e62023-01-23 10:14:58 -0800770 return result
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800771}
772
Liz Kammerd737d022020-11-16 15:42:51 -0800773// chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which
774// would result in two files being placed in the same location.
775// If there is a duplicate path, an error is thrown and true is returned
776// Otherwise, outputPath: srcPath is added to m and returns false
777func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool {
778 if oldSrcPath, found := m[outputPath]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700779 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800780 " First file: in module %s at path %q."+
781 " Second file: in module %s at path %q.",
Liz Kammerd737d022020-11-16 15:42:51 -0800782 outputPath, curModule, oldSrcPath, otherModule, srcPath)
783 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800784 }
Liz Kammerd737d022020-11-16 15:42:51 -0800785 m[outputPath] = srcPath
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800786
Liz Kammerd737d022020-11-16 15:42:51 -0800787 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800788}
Nan Zhangea568a42017-11-08 21:20:04 -0800789
Liz Kammerd737d022020-11-16 15:42:51 -0800790// InstallInData returns true as Python is not supported in the system partition
Cole Faust4d247e62023-01-23 10:14:58 -0800791func (p *PythonLibraryModule) InstallInData() bool {
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000792 return true
793}
794
Nan Zhangea568a42017-11-08 21:20:04 -0800795var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800796var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800797var String = proptools.String