blob: 8726f02ef88d22fd4213f1a2987da9320398d420 [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
Colin Cross1bc63932020-11-22 20:12:45 -080093 // list of java modules that provide data that should be installed alongside the test.
94 Java_data []string
95
Nan Zhangdb0b9a32017-02-27 10:12:13 -080096 // list of the Python libraries compatible both with Python2 and Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -070097 Libs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080098
99 Version struct {
Liz Kammerd737d022020-11-16 15:42:51 -0800100 // Python2-specific properties, including whether Python2 is supported for this module
101 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700102 Py2 VersionProperties `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800103
Liz Kammerd737d022020-11-16 15:42:51 -0800104 // Python3-specific properties, including whether Python3 is supported for this module
105 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700106 Py3 VersionProperties `android:"arch_variant"`
107 } `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800108
109 // the actual version each module uses after variations created.
110 // this property name is hidden from users' perspectives, and soong will populate it during
111 // runtime.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700112 Actual_version string `blueprint:"mutated"`
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700113
Liz Kammerd737d022020-11-16 15:42:51 -0800114 // whether the module is required to be built with actual_version.
115 // this is set by the python version mutator based on version-specific properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700116 Enabled *bool `blueprint:"mutated"`
117
Liz Kammerd737d022020-11-16 15:42:51 -0800118 // whether the binary is required to be built with embedded launcher for this actual_version.
119 // this is set by the python version mutator based on version-specific properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700120 Embedded_launcher *bool `blueprint:"mutated"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800121}
122
Liz Kammerd737d022020-11-16 15:42:51 -0800123// Used to store files of current module after expanding dependencies
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800124type pathMapping struct {
125 dest string
126 src android.Path
127}
128
Cole Faust4d247e62023-01-23 10:14:58 -0800129type PythonLibraryModule struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800130 android.ModuleBase
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700131 android.DefaultableModuleBase
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800132
Nan Zhangb8fa1972017-12-22 16:12:00 -0800133 properties BaseProperties
134 protoProperties android.ProtoProperties
Nan Zhangd4e641b2017-07-12 12:55:28 -0700135
136 // initialize before calling Init
137 hod android.HostOrDeviceSupported
138 multilib android.Multilib
139
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800140 // the Python files of current module after expanding source dependencies.
141 // pathMapping: <dest: runfile_path, src: source_path>
142 srcsPathMappings []pathMapping
143
144 // the data files of current module after expanding source dependencies.
145 // pathMapping: <dest: runfile_path, src: source_path>
146 dataPathMappings []pathMapping
147
Cole Faust5c503d12023-01-24 11:48:08 -0800148 // The zip file containing the current module's source/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800149 srcsZip android.Path
Cole Faust5c503d12023-01-24 11:48:08 -0800150
151 // The zip file containing the current module's source/data files, with the
152 // source files precompiled.
153 precompiledSrcsZip android.Path
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700154
155 sourceProperties android.SourceProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800156}
157
Liz Kammerd737d022020-11-16 15:42:51 -0800158// newModule generates new Python base module
Cole Faust4d247e62023-01-23 10:14:58 -0800159func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *PythonLibraryModule {
160 return &PythonLibraryModule{
Nan Zhangd4e641b2017-07-12 12:55:28 -0700161 hod: hod,
162 multilib: multilib,
163 }
164}
165
Liz Kammerd737d022020-11-16 15:42:51 -0800166// interface implemented by Python modules to provide source and data mappings and zip to python
167// modules that depend on it
168type pythonDependency interface {
169 getSrcsPathMappings() []pathMapping
170 getDataPathMappings() []pathMapping
171 getSrcsZip() android.Path
Cole Faust5c503d12023-01-24 11:48:08 -0800172 getPrecompiledSrcsZip() android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400173 getPkgPath() string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800174}
175
Liz Kammerd737d022020-11-16 15:42:51 -0800176// getSrcsPathMappings gets this module's path mapping of src source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800177func (p *PythonLibraryModule) getSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800178 return p.srcsPathMappings
179}
180
Liz Kammerd737d022020-11-16 15:42:51 -0800181// getSrcsPathMappings gets this module's path mapping of data source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800182func (p *PythonLibraryModule) getDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800183 return p.dataPathMappings
184}
185
Liz Kammerd737d022020-11-16 15:42:51 -0800186// getSrcsZip returns the filepath where the current module's source/data files are zipped.
Cole Faust4d247e62023-01-23 10:14:58 -0800187func (p *PythonLibraryModule) getSrcsZip() android.Path {
Nan Zhang1db85402017-12-18 13:20:23 -0800188 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800189}
190
Cole Faust5c503d12023-01-24 11:48:08 -0800191// getSrcsZip returns the filepath where the current module's source/data files are zipped.
192func (p *PythonLibraryModule) getPrecompiledSrcsZip() android.Path {
193 return p.precompiledSrcsZip
194}
195
Dan Willemsen339a63f2023-08-15 22:17:03 -0400196// getPkgPath returns the pkg_path value
197func (p *PythonLibraryModule) getPkgPath() string {
198 return String(p.properties.Pkg_path)
199}
200
Cole Faust4d247e62023-01-23 10:14:58 -0800201func (p *PythonLibraryModule) getBaseProperties() *BaseProperties {
202 return &p.properties
203}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800204
Cole Faust4d247e62023-01-23 10:14:58 -0800205var _ pythonDependency = (*PythonLibraryModule)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800206
Cole Faust4d247e62023-01-23 10:14:58 -0800207func (p *PythonLibraryModule) init() android.Module {
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700208 p.AddProperties(&p.properties, &p.protoProperties, &p.sourceProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700209 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700210 android.InitDefaultableModule(p)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700211 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800212}
213
Liz Kammerd737d022020-11-16 15:42:51 -0800214// Python-specific tag to transfer information on the purpose of a dependency.
215// This is used when adding a dependency on a module, which can later be accessed when visiting
216// dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700217type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800218 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700219 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800220}
221
Liz Kammerd737d022020-11-16 15:42:51 -0800222// Python-specific tag that indicates that installed files of this module should depend on installed
223// files of the dependency
Colin Crosse9fe2942020-11-10 18:12:15 -0800224type installDependencyTag struct {
225 blueprint.BaseDependencyTag
Liz Kammerd737d022020-11-16 15:42:51 -0800226 // embedding this struct provides the installation dependency requirement
Colin Crosse9fe2942020-11-10 18:12:15 -0800227 android.InstallAlwaysNeededDependencyTag
228 name string
229}
230
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800231var (
Cole Faust5c503d12023-01-24 11:48:08 -0800232 pythonLibTag = dependencyTag{name: "pythonLib"}
233 javaDataTag = dependencyTag{name: "javaData"}
234 // The python interpreter, with soong module name "py3-launcher" or "py3-launcher-autorun".
Cole Faust909d2372023-02-13 23:17:40 +0000235 launcherTag = dependencyTag{name: "launcher"}
236 launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"}
Cole Faust5c503d12023-01-24 11:48:08 -0800237 // The python interpreter built for host so that we can precompile python sources.
238 // This only works because the precompiled sources don't vary by architecture.
239 // The soong module name is "py3-launcher".
Cole Faust909d2372023-02-13 23:17:40 +0000240 hostLauncherTag = dependencyTag{name: "hostLauncher"}
241 hostlauncherSharedLibTag = dependencyTag{name: "hostlauncherSharedLib"}
242 hostStdLibTag = dependencyTag{name: "hostStdLib"}
243 pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
244 pyExt = ".py"
245 protoExt = ".proto"
246 pyVersion2 = "PY2"
247 pyVersion3 = "PY3"
248 internalPath = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800249)
250
Cole Faust4d247e62023-01-23 10:14:58 -0800251type basePropertiesProvider interface {
252 getBaseProperties() *BaseProperties
253}
254
Colin Crosse0771282024-05-21 15:19:18 -0700255type versionSplitTransitionMutator struct{}
256
257func (versionSplitTransitionMutator) Split(ctx android.BaseModuleContext) []string {
258 if base, ok := ctx.Module().(basePropertiesProvider); ok {
259 props := base.getBaseProperties()
260 var variants []string
261 // PY3 is first so that we alias the PY3 variant rather than PY2 if both
262 // are available
263 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
264 variants = append(variants, pyVersion3)
265 }
266 if proptools.BoolDefault(props.Version.Py2.Enabled, false) {
267 if !ctx.DeviceConfig().BuildBrokenUsesSoongPython2Modules() &&
268 ctx.ModuleName() != "py2-cmd" &&
269 ctx.ModuleName() != "py2-stdlib" {
270 ctx.PropertyErrorf("version.py2.enabled", "Python 2 is no longer supported, please convert to python 3. This error can be temporarily overridden by setting BUILD_BROKEN_USES_SOONG_PYTHON2_MODULES := true in the product configuration")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800271 }
Colin Crosse0771282024-05-21 15:19:18 -0700272 variants = append(variants, pyVersion2)
273 }
274 return variants
275 }
276 return []string{""}
277}
278
279func (versionSplitTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
280 return ""
281}
282
283func (versionSplitTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
284 if incomingVariation != "" {
285 return incomingVariation
286 }
287 if base, ok := ctx.Module().(basePropertiesProvider); ok {
288 props := base.getBaseProperties()
289 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
290 return pyVersion3
291 } else {
292 return pyVersion2
293 }
294 }
295
296 return ""
297}
298
299func (versionSplitTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
300 if variation == "" {
301 return
302 }
303 if base, ok := ctx.Module().(basePropertiesProvider); ok {
304 props := base.getBaseProperties()
305 props.Actual_version = variation
306
307 var versionProps *VersionProperties
308 if variation == pyVersion3 {
309 versionProps = &props.Version.Py3
310 } else if variation == pyVersion2 {
311 versionProps = &props.Version.Py2
312 }
313
314 err := proptools.AppendMatchingProperties([]interface{}{props}, versionProps, nil)
315 if err != nil {
316 panic(err)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800317 }
318 }
319}
320
Liz Kammerd737d022020-11-16 15:42:51 -0800321func anyHasExt(paths []string, ext string) bool {
322 for _, p := range paths {
323 if filepath.Ext(p) == ext {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800324 return true
325 }
326 }
327
328 return false
329}
330
Cole Faust4d247e62023-01-23 10:14:58 -0800331func (p *PythonLibraryModule) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800332 return anyHasExt(p.properties.Srcs, ext)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800333}
334
Liz Kammerd737d022020-11-16 15:42:51 -0800335// DepsMutator mutates dependencies for this module:
Colin Crossd079e0b2022-08-16 10:27:33 -0700336// - handles proto dependencies,
337// - if required, specifies launcher and adds launcher dependencies,
338// - applies python version mutations to Python dependencies
Cole Faust4d247e62023-01-23 10:14:58 -0800339func (p *PythonLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700340 android.ProtoDeps(ctx, &p.protoProperties)
341
Colin Crosse20113d2020-11-22 19:37:44 -0800342 versionVariation := []blueprint.Variation{
343 {"python_version", p.properties.Actual_version},
Nan Zhangb8fa1972017-12-22 16:12:00 -0800344 }
Colin Crosse20113d2020-11-22 19:37:44 -0800345
Liz Kammerd737d022020-11-16 15:42:51 -0800346 // If sources contain a proto file, add dependency on libprotobuf-python
347 if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
Colin Crosse20113d2020-11-22 19:37:44 -0800348 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python")
349 }
Liz Kammerd737d022020-11-16 15:42:51 -0800350
351 // Add python library dependencies for this python version variation
Colin Crosse20113d2020-11-22 19:37:44 -0800352 ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700353
Colin Cross1bc63932020-11-22 20:12:45 -0800354 // Emulate the data property for java_data but with the arch variation overridden to "common"
355 // so that it can point to java modules.
356 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
357 ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
Cole Faust5c503d12023-01-24 11:48:08 -0800358
Cole Faust909d2372023-02-13 23:17:40 +0000359 p.AddDepsOnPythonLauncherAndStdlib(ctx, hostStdLibTag, hostLauncherTag, hostlauncherSharedLibTag, false, ctx.Config().BuildOSTarget)
Cole Faust5c503d12023-01-24 11:48:08 -0800360}
361
Cole Faust909d2372023-02-13 23:17:40 +0000362// AddDepsOnPythonLauncherAndStdlib will make the current module depend on the python stdlib,
363// launcher (interpreter), and the launcher's shared libraries. If autorun is true, it will use
364// the autorun launcher instead of the regular one. This function acceps a targetForDeps argument
365// as the target to use for these dependencies. For embedded launcher python binaries, the launcher
366// that will be embedded will be under the same target as the python module itself. But when
367// precompiling python code, we need to get the python launcher built for host, even if we're
368// compiling the python module for device, so we pass a different target to this function.
Cole Faust5c503d12023-01-24 11:48:08 -0800369func (p *PythonLibraryModule) AddDepsOnPythonLauncherAndStdlib(ctx android.BottomUpMutatorContext,
Cole Faust909d2372023-02-13 23:17:40 +0000370 stdLibTag, launcherTag, launcherSharedLibTag blueprint.DependencyTag,
Cole Faust5c503d12023-01-24 11:48:08 -0800371 autorun bool, targetForDeps android.Target) {
372 var stdLib string
373 var launcherModule string
Cole Faust909d2372023-02-13 23:17:40 +0000374 // Add launcher shared lib dependencies. Ideally, these should be
375 // derived from the `shared_libs` property of the launcher. TODO: read these from
376 // the python launcher itself using ctx.OtherModuleProvider() or similar on the result
377 // of ctx.AddFarVariationDependencies()
378 launcherSharedLibDeps := []string{
379 "libsqlite",
380 }
381 // Add launcher-specific dependencies for bionic
382 if targetForDeps.Os.Bionic() {
383 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm")
384 }
385 if targetForDeps.Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() {
386 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc_musl")
387 }
Cole Faust5c503d12023-01-24 11:48:08 -0800388
389 switch p.properties.Actual_version {
390 case pyVersion2:
391 stdLib = "py2-stdlib"
392
393 launcherModule = "py2-launcher"
394 if autorun {
395 launcherModule = "py2-launcher-autorun"
396 }
397
Cole Faust909d2372023-02-13 23:17:40 +0000398 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++")
Cole Faust5c503d12023-01-24 11:48:08 -0800399 case pyVersion3:
Dan Willemsen339a63f2023-08-15 22:17:03 -0400400 var prebuiltStdLib bool
401 if targetForDeps.Os.Bionic() {
402 prebuiltStdLib = false
403 } else if ctx.Config().VendorConfig("cpython3").Bool("force_build_host") {
404 prebuiltStdLib = false
405 } else {
406 prebuiltStdLib = true
407 }
408
409 if prebuiltStdLib {
410 stdLib = "py3-stdlib-prebuilt"
411 } else {
412 stdLib = "py3-stdlib"
413 }
Cole Faust5c503d12023-01-24 11:48:08 -0800414
415 launcherModule = "py3-launcher"
416 if autorun {
417 launcherModule = "py3-launcher-autorun"
418 }
419 if ctx.Config().HostStaticBinaries() && targetForDeps.Os == android.LinuxMusl {
420 launcherModule += "-static"
421 }
Cole Faust909d2372023-02-13 23:17:40 +0000422 if ctx.Device() {
423 launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog")
424 }
Cole Faust5c503d12023-01-24 11:48:08 -0800425 default:
426 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
427 p.properties.Actual_version, ctx.ModuleName()))
428 }
429 targetVariations := targetForDeps.Variations()
430 if ctx.ModuleName() != stdLib {
431 stdLibVariations := make([]blueprint.Variation, 0, len(targetVariations)+1)
432 stdLibVariations = append(stdLibVariations, blueprint.Variation{Mutator: "python_version", Variation: p.properties.Actual_version})
433 stdLibVariations = append(stdLibVariations, targetVariations...)
434 // Using AddFarVariationDependencies for all of these because they can be for a different
435 // platform, like if the python module itself was being compiled for device, we may want
436 // the python interpreter built for host so that we can precompile python sources.
437 ctx.AddFarVariationDependencies(stdLibVariations, stdLibTag, stdLib)
438 }
439 ctx.AddFarVariationDependencies(targetVariations, launcherTag, launcherModule)
Cole Faust909d2372023-02-13 23:17:40 +0000440 ctx.AddFarVariationDependencies(targetVariations, launcherSharedLibTag, launcherSharedLibDeps...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800441}
442
Cole Faust4d247e62023-01-23 10:14:58 -0800443// GenerateAndroidBuildActions performs build actions common to all Python modules
444func (p *PythonLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammerd737d022020-11-16 15:42:51 -0800445 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs)
Colin Cross40213022023-12-13 15:19:49 -0800446 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: expandedSrcs.Strings()})
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700447 // Keep before any early returns.
448 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
449 TestOnly: Bool(p.sourceProperties.Test_only),
450 TopLevelTarget: p.sourceProperties.Top_level_test_target,
451 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800452
453 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800454 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800455
Colin Cross1bc63932020-11-22 20:12:45 -0800456 // Emulate the data property for java_data dependencies.
457 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
458 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
459 }
460
Liz Kammerd737d022020-11-16 15:42:51 -0800461 // Validate pkg_path property
Nan Zhang1db85402017-12-18 13:20:23 -0800462 pkgPath := String(p.properties.Pkg_path)
463 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800464 // TODO: export validation from android/paths.go handling to replace this duplicated functionality
Nan Zhang1db85402017-12-18 13:20:23 -0800465 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
466 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
467 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700468 ctx.PropertyErrorf("pkg_path",
469 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800470 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700471 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800472 }
Liz Kammerd737d022020-11-16 15:42:51 -0800473 }
474 // If property Is_internal is set, prepend pkgPath with internalPath
475 if proptools.BoolDefault(p.properties.Is_internal, false) {
476 pkgPath = filepath.Join(internalPath, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800477 }
478
Liz Kammerd737d022020-11-16 15:42:51 -0800479 // generate src:destination path mappings for this module
Nan Zhang1db85402017-12-18 13:20:23 -0800480 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800481
Liz Kammerd737d022020-11-16 15:42:51 -0800482 // generate the zipfile of all source and data files
Nan Zhang1db85402017-12-18 13:20:23 -0800483 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Dan Willemsenfe2dafc2023-08-24 22:59:16 +0000484 p.precompiledSrcsZip = p.precompileSrcs(ctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800485}
486
Liz Kammerd737d022020-11-16 15:42:51 -0800487func isValidPythonPath(path string) error {
488 identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/")
489 for _, token := range identifiers {
490 if !pathComponentRegexp.MatchString(token) {
491 return fmt.Errorf("the path %q contains invalid subpath %q. "+
492 "Subpaths must be at least one character long. "+
493 "The first character must an underscore or letter. "+
494 "Following characters may be any of: letter, digit, underscore, hyphen.",
495 path, token)
496 }
497 }
498 return nil
499}
500
501// For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path>
502// for python/data files expanded from properties.
Cole Faust4d247e62023-01-23 10:14:58 -0800503func (p *PythonLibraryModule) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800504 expandedSrcs, expandedData android.Paths) {
505 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800506 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800507 destToPySrcs := make(map[string]string)
508 destToPyData := make(map[string]string)
509
Dan Willemsen339a63f2023-08-15 22:17:03 -0400510 // Disable path checks for the stdlib, as it includes a "." in the version string
511 isInternal := proptools.BoolDefault(p.properties.Is_internal, false)
512
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800513 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800514 if s.Ext() != pyExt && s.Ext() != protoExt {
515 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800516 continue
517 }
Nan Zhang1db85402017-12-18 13:20:23 -0800518 runfilesPath := filepath.Join(pkgPath, s.Rel())
Dan Willemsen339a63f2023-08-15 22:17:03 -0400519 if !isInternal {
520 if err := isValidPythonPath(runfilesPath); err != nil {
521 ctx.PropertyErrorf("srcs", err.Error())
522 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800523 }
Liz Kammerd737d022020-11-16 15:42:51 -0800524 if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
525 p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800526 }
527 }
528
529 for _, d := range expandedData {
Raphael Blistein59858462024-05-08 16:15:53 +0000530 if d.Ext() == pyExt {
531 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800532 continue
533 }
Nan Zhang1db85402017-12-18 13:20:23 -0800534 runfilesPath := filepath.Join(pkgPath, d.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800535 if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800536 p.dataPathMappings = append(p.dataPathMappings,
537 pathMapping{dest: runfilesPath, src: d})
538 }
539 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800540}
541
Liz Kammerd737d022020-11-16 15:42:51 -0800542// createSrcsZip registers build actions to zip current module's sources and data.
Cole Faust4d247e62023-01-23 10:14:58 -0800543func (p *PythonLibraryModule) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800544 relativeRootMap := make(map[string]android.Paths)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800545 var protoSrcs android.Paths
Cole Faust5c503d12023-01-24 11:48:08 -0800546 addPathMapping := func(path pathMapping) {
Raphael Blistein59858462024-05-08 16:15:53 +0000547 relativeRoot := strings.TrimSuffix(path.src.String(), path.src.Rel())
548 relativeRootMap[relativeRoot] = append(relativeRootMap[relativeRoot], path.src)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800549 }
Cole Faust5c503d12023-01-24 11:48:08 -0800550
551 // "srcs" or "data" properties may contain filegroups so it might happen that
552 // the root directory for each source path is different.
553 for _, path := range p.srcsPathMappings {
Raphael Blistein59858462024-05-08 16:15:53 +0000554 // handle proto sources separately
555 if path.src.Ext() == protoExt {
556 protoSrcs = append(protoSrcs, path.src)
557 } else {
558 addPathMapping(path)
559 }
Cole Faust5c503d12023-01-24 11:48:08 -0800560 }
561 for _, path := range p.dataPathMappings {
562 addPathMapping(path)
563 }
564
Nan Zhangb8fa1972017-12-22 16:12:00 -0800565 var zips android.Paths
566 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700567 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
568 protoFlags.OutTypeFlag = "--python_out"
569
Cole Faustcaf766b2022-10-21 16:07:56 -0700570 if pkgPath != "" {
Cole Faust43ac21f2022-09-19 11:19:52 -0700571 pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path")
572 rule := android.NewRuleBuilder(pctx, ctx)
573 var stagedProtoSrcs android.Paths
574 for _, srcFile := range protoSrcs {
575 stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel())
Cole Faust43ac21f2022-09-19 11:19:52 -0700576 rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc)
577 stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc)
578 }
579 rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path")
580 protoSrcs = stagedProtoSrcs
Cole Faust43ac21f2022-09-19 11:19:52 -0700581 }
582
Nan Zhangb8fa1972017-12-22 16:12:00 -0800583 for _, srcFile := range protoSrcs {
Cole Faustcaf766b2022-10-21 16:07:56 -0700584 zip := genProto(ctx, srcFile, protoFlags)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800585 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800586 }
587 }
588
Nan Zhangb8fa1972017-12-22 16:12:00 -0800589 if len(relativeRootMap) > 0 {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800590 // in order to keep stable order of soong_zip params, we sort the keys here.
Cole Faust18994c72023-02-28 16:02:16 -0800591 roots := android.SortedKeys(relativeRootMap)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800592
Cole Faust01243362022-06-02 12:11:12 -0700593 // Use -symlinks=false so that the symlinks in the bazel output directory are followed
594 parArgs := []string{"-symlinks=false"}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700595 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800596 // use package path as path prefix
Nan Zhangf0c4e432018-05-22 14:50:18 -0700597 parArgs = append(parArgs, `-P `+pkgPath)
598 }
Liz Kammerd737d022020-11-16 15:42:51 -0800599 paths := android.Paths{}
600 for _, root := range roots {
601 // specify relative root of file in following -f arguments
602 parArgs = append(parArgs, `-C `+root)
603 for _, path := range relativeRootMap[root] {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800604 parArgs = append(parArgs, `-f `+path.String())
Liz Kammerd737d022020-11-16 15:42:51 -0800605 paths = append(paths, path)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800606 }
607 }
608
609 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
610 ctx.Build(pctx, android.BuildParams{
611 Rule: zip,
612 Description: "python library archive",
613 Output: origSrcsZip,
Liz Kammerd737d022020-11-16 15:42:51 -0800614 // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits
615 Implicits: paths,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800616 Args: map[string]string{
617 "args": strings.Join(parArgs, " "),
618 },
619 })
620 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800621 }
Liz Kammerd737d022020-11-16 15:42:51 -0800622 // we may have multiple zips due to separate handling of proto source files
Nan Zhangb8fa1972017-12-22 16:12:00 -0800623 if len(zips) == 1 {
624 return zips[0]
625 } else {
626 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
627 ctx.Build(pctx, android.BuildParams{
628 Rule: combineZip,
629 Description: "combine python library archive",
630 Output: combinedSrcsZip,
631 Inputs: zips,
632 })
633 return combinedSrcsZip
634 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800635}
636
Cole Faust5c503d12023-01-24 11:48:08 -0800637func (p *PythonLibraryModule) precompileSrcs(ctx android.ModuleContext) android.Path {
638 // To precompile the python sources, we need a python interpreter and stdlib built
639 // for host. We then use those to compile the python sources, which may be used on either
640 // host of device. Python bytecode is architecture agnostic, so we're essentially
641 // "cross compiling" for device here purely by virtue of host and device python bytecode
642 // being the same.
643 var stdLib android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400644 var stdLibPkg string
Cole Faust5c503d12023-01-24 11:48:08 -0800645 var launcher android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400646 if proptools.BoolDefault(p.properties.Is_internal, false) {
Cole Faust5c503d12023-01-24 11:48:08 -0800647 stdLib = p.srcsZip
Dan Willemsen339a63f2023-08-15 22:17:03 -0400648 stdLibPkg = p.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800649 } else {
650 ctx.VisitDirectDepsWithTag(hostStdLibTag, func(module android.Module) {
651 if dep, ok := module.(pythonDependency); ok {
652 stdLib = dep.getPrecompiledSrcsZip()
Dan Willemsen339a63f2023-08-15 22:17:03 -0400653 stdLibPkg = dep.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800654 }
655 })
656 }
657 ctx.VisitDirectDepsWithTag(hostLauncherTag, func(module android.Module) {
658 if dep, ok := module.(IntermPathProvider); ok {
659 optionalLauncher := dep.IntermPathForModuleOut()
660 if optionalLauncher.Valid() {
661 launcher = optionalLauncher.Path()
662 }
Cole Faust909d2372023-02-13 23:17:40 +0000663 }
664 })
665 var launcherSharedLibs android.Paths
666 var ldLibraryPath []string
667 ctx.VisitDirectDepsWithTag(hostlauncherSharedLibTag, func(module android.Module) {
668 if dep, ok := module.(IntermPathProvider); ok {
669 optionalPath := dep.IntermPathForModuleOut()
670 if optionalPath.Valid() {
671 launcherSharedLibs = append(launcherSharedLibs, optionalPath.Path())
672 ldLibraryPath = append(ldLibraryPath, filepath.Dir(optionalPath.Path().String()))
Cole Faust5c503d12023-01-24 11:48:08 -0800673 }
674 }
675 })
676
677 out := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszipprecompiled")
678 if stdLib == nil || launcher == nil {
679 // This shouldn't happen in a real build because we'll error out when adding dependencies
680 // on the stdlib and launcher if they don't exist. But some tests set
681 // AllowMissingDependencies.
682 return out
683 }
684 ctx.Build(pctx, android.BuildParams{
685 Rule: precompile,
686 Input: p.srcsZip,
687 Output: out,
688 Implicits: launcherSharedLibs,
689 Description: "Precompile the python sources of " + ctx.ModuleName(),
690 Args: map[string]string{
691 "stdlibZip": stdLib.String(),
Dan Willemsen339a63f2023-08-15 22:17:03 -0400692 "stdlibPkg": stdLibPkg,
Cole Faust5c503d12023-01-24 11:48:08 -0800693 "launcher": launcher.String(),
694 "ldLibraryPath": strings.Join(ldLibraryPath, ":"),
695 },
696 })
697 return out
698}
699
Cole Faust4d247e62023-01-23 10:14:58 -0800700// isPythonLibModule returns whether the given module is a Python library PythonLibraryModule or not
Nan Zhangd4e641b2017-07-12 12:55:28 -0700701func isPythonLibModule(module blueprint.Module) bool {
Cole Faust4d247e62023-01-23 10:14:58 -0800702 if _, ok := module.(*PythonLibraryModule); ok {
703 if _, ok := module.(*PythonBinaryModule); !ok {
704 return true
705 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700706 }
707 return false
708}
709
Liz Kammerd737d022020-11-16 15:42:51 -0800710// collectPathsFromTransitiveDeps checks for source/data files for duplicate paths
711// for module and its transitive dependencies and collects list of data/source file
712// zips for transitive dependencies.
Cole Faust5c503d12023-01-24 11:48:08 -0800713func (p *PythonLibraryModule) collectPathsFromTransitiveDeps(ctx android.ModuleContext, precompiled bool) android.Paths {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800714 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
715 // check duplicates.
716 destToPySrcs := make(map[string]string)
717 destToPyData := make(map[string]string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800718 for _, path := range p.srcsPathMappings {
719 destToPySrcs[path.dest] = path.src.String()
720 }
721 for _, path := range p.dataPathMappings {
722 destToPyData[path.dest] = path.src.String()
723 }
724
Colin Cross6b753602018-06-21 13:03:07 -0700725 seen := make(map[android.Module]bool)
726
Cole Faust4d247e62023-01-23 10:14:58 -0800727 var result android.Paths
728
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800729 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700730 ctx.WalkDeps(func(child, parent android.Module) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800731 // we only collect dependencies tagged as python library deps
Colin Cross6b753602018-06-21 13:03:07 -0700732 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
733 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800734 }
Colin Cross6b753602018-06-21 13:03:07 -0700735 if seen[child] {
736 return false
737 }
738 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800739 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700740 if !isPythonLibModule(child) {
Liz Kammerd737d022020-11-16 15:42:51 -0800741 ctx.PropertyErrorf("libs",
Nan Zhangd4e641b2017-07-12 12:55:28 -0700742 "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 +0000743 ctx.OtherModuleName(child), ctx.ModuleName())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700744 }
Liz Kammerd737d022020-11-16 15:42:51 -0800745 // collect source and data paths, checking that there are no duplicate output file conflicts
746 if dep, ok := child.(pythonDependency); ok {
747 srcs := dep.getSrcsPathMappings()
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800748 for _, path := range srcs {
Liz Kammerd737d022020-11-16 15:42:51 -0800749 checkForDuplicateOutputPath(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700750 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800751 }
Liz Kammerd737d022020-11-16 15:42:51 -0800752 data := dep.getDataPathMappings()
753 for _, path := range data {
754 checkForDuplicateOutputPath(ctx, destToPyData,
755 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
756 }
Cole Faust5c503d12023-01-24 11:48:08 -0800757 if precompiled {
758 result = append(result, dep.getPrecompiledSrcsZip())
759 } else {
760 result = append(result, dep.getSrcsZip())
761 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800762 }
Colin Cross6b753602018-06-21 13:03:07 -0700763 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800764 })
Cole Faust4d247e62023-01-23 10:14:58 -0800765 return result
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800766}
767
Liz Kammerd737d022020-11-16 15:42:51 -0800768// chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which
769// would result in two files being placed in the same location.
770// If there is a duplicate path, an error is thrown and true is returned
771// Otherwise, outputPath: srcPath is added to m and returns false
772func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool {
773 if oldSrcPath, found := m[outputPath]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700774 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800775 " First file: in module %s at path %q."+
776 " Second file: in module %s at path %q.",
Liz Kammerd737d022020-11-16 15:42:51 -0800777 outputPath, curModule, oldSrcPath, otherModule, srcPath)
778 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800779 }
Liz Kammerd737d022020-11-16 15:42:51 -0800780 m[outputPath] = srcPath
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800781
Liz Kammerd737d022020-11-16 15:42:51 -0800782 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800783}
Nan Zhangea568a42017-11-08 21:20:04 -0800784
Liz Kammerd737d022020-11-16 15:42:51 -0800785// InstallInData returns true as Python is not supported in the system partition
Cole Faust4d247e62023-01-23 10:14:58 -0800786func (p *PythonLibraryModule) InstallInData() bool {
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000787 return true
788}
789
Nan Zhangea568a42017-11-08 21:20:04 -0800790var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800791var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800792var String = proptools.String