blob: 01ac86c71e15bd7cb73ea92bf470835b00fe8513 [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) {
Cole Faust4ce4f882024-09-09 18:08:49 -0700267 if ctx.ModuleName() != "py2-cmd" &&
Colin Crosse0771282024-05-21 15:19:18 -0700268 ctx.ModuleName() != "py2-stdlib" {
Cole Faust4ce4f882024-09-09 18:08:49 -0700269 ctx.PropertyErrorf("version.py2.enabled", "Python 2 is no longer supported, please convert to python 3.")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800270 }
Colin Crosse0771282024-05-21 15:19:18 -0700271 variants = append(variants, pyVersion2)
272 }
273 return variants
274 }
275 return []string{""}
276}
277
278func (versionSplitTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
279 return ""
280}
281
282func (versionSplitTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
283 if incomingVariation != "" {
284 return incomingVariation
285 }
286 if base, ok := ctx.Module().(basePropertiesProvider); ok {
287 props := base.getBaseProperties()
288 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
289 return pyVersion3
290 } else {
291 return pyVersion2
292 }
293 }
294
295 return ""
296}
297
298func (versionSplitTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
299 if variation == "" {
300 return
301 }
302 if base, ok := ctx.Module().(basePropertiesProvider); ok {
303 props := base.getBaseProperties()
304 props.Actual_version = variation
305
306 var versionProps *VersionProperties
307 if variation == pyVersion3 {
308 versionProps = &props.Version.Py3
309 } else if variation == pyVersion2 {
310 versionProps = &props.Version.Py2
311 }
312
313 err := proptools.AppendMatchingProperties([]interface{}{props}, versionProps, nil)
314 if err != nil {
315 panic(err)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800316 }
317 }
318}
319
Liz Kammerd737d022020-11-16 15:42:51 -0800320func anyHasExt(paths []string, ext string) bool {
321 for _, p := range paths {
322 if filepath.Ext(p) == ext {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800323 return true
324 }
325 }
326
327 return false
328}
329
Cole Faust4d247e62023-01-23 10:14:58 -0800330func (p *PythonLibraryModule) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800331 return anyHasExt(p.properties.Srcs, ext)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800332}
333
Liz Kammerd737d022020-11-16 15:42:51 -0800334// DepsMutator mutates dependencies for this module:
Colin Crossd079e0b2022-08-16 10:27:33 -0700335// - handles proto dependencies,
336// - if required, specifies launcher and adds launcher dependencies,
337// - applies python version mutations to Python dependencies
Cole Faust4d247e62023-01-23 10:14:58 -0800338func (p *PythonLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700339 android.ProtoDeps(ctx, &p.protoProperties)
340
Colin Crosse20113d2020-11-22 19:37:44 -0800341 versionVariation := []blueprint.Variation{
342 {"python_version", p.properties.Actual_version},
Nan Zhangb8fa1972017-12-22 16:12:00 -0800343 }
Colin Crosse20113d2020-11-22 19:37:44 -0800344
Liz Kammerd737d022020-11-16 15:42:51 -0800345 // If sources contain a proto file, add dependency on libprotobuf-python
346 if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
Colin Crosse20113d2020-11-22 19:37:44 -0800347 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python")
348 }
Liz Kammerd737d022020-11-16 15:42:51 -0800349
350 // Add python library dependencies for this python version variation
Colin Crosse20113d2020-11-22 19:37:44 -0800351 ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700352
Colin Cross1bc63932020-11-22 20:12:45 -0800353 // Emulate the data property for java_data but with the arch variation overridden to "common"
354 // so that it can point to java modules.
355 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
356 ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
Cole Faust5c503d12023-01-24 11:48:08 -0800357
Cole Faust909d2372023-02-13 23:17:40 +0000358 p.AddDepsOnPythonLauncherAndStdlib(ctx, hostStdLibTag, hostLauncherTag, hostlauncherSharedLibTag, false, ctx.Config().BuildOSTarget)
Cole Faust5c503d12023-01-24 11:48:08 -0800359}
360
Cole Faust909d2372023-02-13 23:17:40 +0000361// AddDepsOnPythonLauncherAndStdlib will make the current module depend on the python stdlib,
362// launcher (interpreter), and the launcher's shared libraries. If autorun is true, it will use
363// the autorun launcher instead of the regular one. This function acceps a targetForDeps argument
364// as the target to use for these dependencies. For embedded launcher python binaries, the launcher
365// that will be embedded will be under the same target as the python module itself. But when
366// precompiling python code, we need to get the python launcher built for host, even if we're
367// compiling the python module for device, so we pass a different target to this function.
Cole Faust5c503d12023-01-24 11:48:08 -0800368func (p *PythonLibraryModule) AddDepsOnPythonLauncherAndStdlib(ctx android.BottomUpMutatorContext,
Cole Faust909d2372023-02-13 23:17:40 +0000369 stdLibTag, launcherTag, launcherSharedLibTag blueprint.DependencyTag,
Cole Faust5c503d12023-01-24 11:48:08 -0800370 autorun bool, targetForDeps android.Target) {
371 var stdLib string
372 var launcherModule string
Cole Faust909d2372023-02-13 23:17:40 +0000373 // Add launcher shared lib dependencies. Ideally, these should be
374 // derived from the `shared_libs` property of the launcher. TODO: read these from
375 // the python launcher itself using ctx.OtherModuleProvider() or similar on the result
376 // of ctx.AddFarVariationDependencies()
377 launcherSharedLibDeps := []string{
378 "libsqlite",
379 }
380 // Add launcher-specific dependencies for bionic
381 if targetForDeps.Os.Bionic() {
382 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm")
383 }
384 if targetForDeps.Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() {
385 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc_musl")
386 }
Cole Faust5c503d12023-01-24 11:48:08 -0800387
388 switch p.properties.Actual_version {
389 case pyVersion2:
390 stdLib = "py2-stdlib"
391
392 launcherModule = "py2-launcher"
393 if autorun {
394 launcherModule = "py2-launcher-autorun"
395 }
396
Cole Faust909d2372023-02-13 23:17:40 +0000397 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++")
Cole Faust5c503d12023-01-24 11:48:08 -0800398 case pyVersion3:
Dan Willemsen339a63f2023-08-15 22:17:03 -0400399 var prebuiltStdLib bool
400 if targetForDeps.Os.Bionic() {
401 prebuiltStdLib = false
402 } else if ctx.Config().VendorConfig("cpython3").Bool("force_build_host") {
403 prebuiltStdLib = false
404 } else {
405 prebuiltStdLib = true
406 }
407
408 if prebuiltStdLib {
409 stdLib = "py3-stdlib-prebuilt"
410 } else {
411 stdLib = "py3-stdlib"
412 }
Cole Faust5c503d12023-01-24 11:48:08 -0800413
414 launcherModule = "py3-launcher"
415 if autorun {
416 launcherModule = "py3-launcher-autorun"
417 }
418 if ctx.Config().HostStaticBinaries() && targetForDeps.Os == android.LinuxMusl {
419 launcherModule += "-static"
420 }
Cole Faust909d2372023-02-13 23:17:40 +0000421 if ctx.Device() {
422 launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog")
423 }
Cole Faust5c503d12023-01-24 11:48:08 -0800424 default:
425 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
426 p.properties.Actual_version, ctx.ModuleName()))
427 }
428 targetVariations := targetForDeps.Variations()
429 if ctx.ModuleName() != stdLib {
430 stdLibVariations := make([]blueprint.Variation, 0, len(targetVariations)+1)
431 stdLibVariations = append(stdLibVariations, blueprint.Variation{Mutator: "python_version", Variation: p.properties.Actual_version})
432 stdLibVariations = append(stdLibVariations, targetVariations...)
433 // Using AddFarVariationDependencies for all of these because they can be for a different
434 // platform, like if the python module itself was being compiled for device, we may want
435 // the python interpreter built for host so that we can precompile python sources.
436 ctx.AddFarVariationDependencies(stdLibVariations, stdLibTag, stdLib)
437 }
438 ctx.AddFarVariationDependencies(targetVariations, launcherTag, launcherModule)
Cole Faust909d2372023-02-13 23:17:40 +0000439 ctx.AddFarVariationDependencies(targetVariations, launcherSharedLibTag, launcherSharedLibDeps...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800440}
441
Cole Faust4d247e62023-01-23 10:14:58 -0800442// GenerateAndroidBuildActions performs build actions common to all Python modules
443func (p *PythonLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammerd737d022020-11-16 15:42:51 -0800444 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs)
Colin Cross40213022023-12-13 15:19:49 -0800445 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: expandedSrcs.Strings()})
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700446 // Keep before any early returns.
447 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
448 TestOnly: Bool(p.sourceProperties.Test_only),
449 TopLevelTarget: p.sourceProperties.Top_level_test_target,
450 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800451
452 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800453 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800454
Colin Cross1bc63932020-11-22 20:12:45 -0800455 // Emulate the data property for java_data dependencies.
456 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
457 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
458 }
459
Liz Kammerd737d022020-11-16 15:42:51 -0800460 // Validate pkg_path property
Nan Zhang1db85402017-12-18 13:20:23 -0800461 pkgPath := String(p.properties.Pkg_path)
462 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800463 // TODO: export validation from android/paths.go handling to replace this duplicated functionality
Nan Zhang1db85402017-12-18 13:20:23 -0800464 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
465 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
466 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700467 ctx.PropertyErrorf("pkg_path",
468 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800469 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700470 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800471 }
Liz Kammerd737d022020-11-16 15:42:51 -0800472 }
473 // If property Is_internal is set, prepend pkgPath with internalPath
474 if proptools.BoolDefault(p.properties.Is_internal, false) {
475 pkgPath = filepath.Join(internalPath, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800476 }
477
Liz Kammerd737d022020-11-16 15:42:51 -0800478 // generate src:destination path mappings for this module
Nan Zhang1db85402017-12-18 13:20:23 -0800479 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800480
Liz Kammerd737d022020-11-16 15:42:51 -0800481 // generate the zipfile of all source and data files
Nan Zhang1db85402017-12-18 13:20:23 -0800482 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Dan Willemsenfe2dafc2023-08-24 22:59:16 +0000483 p.precompiledSrcsZip = p.precompileSrcs(ctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800484}
485
Liz Kammerd737d022020-11-16 15:42:51 -0800486func isValidPythonPath(path string) error {
487 identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/")
488 for _, token := range identifiers {
489 if !pathComponentRegexp.MatchString(token) {
490 return fmt.Errorf("the path %q contains invalid subpath %q. "+
491 "Subpaths must be at least one character long. "+
492 "The first character must an underscore or letter. "+
493 "Following characters may be any of: letter, digit, underscore, hyphen.",
494 path, token)
495 }
496 }
497 return nil
498}
499
500// For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path>
501// for python/data files expanded from properties.
Cole Faust4d247e62023-01-23 10:14:58 -0800502func (p *PythonLibraryModule) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800503 expandedSrcs, expandedData android.Paths) {
504 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800505 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800506 destToPySrcs := make(map[string]string)
507 destToPyData := make(map[string]string)
508
Dan Willemsen339a63f2023-08-15 22:17:03 -0400509 // Disable path checks for the stdlib, as it includes a "." in the version string
510 isInternal := proptools.BoolDefault(p.properties.Is_internal, false)
511
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800512 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800513 if s.Ext() != pyExt && s.Ext() != protoExt {
514 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800515 continue
516 }
Nan Zhang1db85402017-12-18 13:20:23 -0800517 runfilesPath := filepath.Join(pkgPath, s.Rel())
Dan Willemsen339a63f2023-08-15 22:17:03 -0400518 if !isInternal {
519 if err := isValidPythonPath(runfilesPath); err != nil {
520 ctx.PropertyErrorf("srcs", err.Error())
521 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800522 }
Liz Kammerd737d022020-11-16 15:42:51 -0800523 if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
524 p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800525 }
526 }
527
528 for _, d := range expandedData {
Raphael Blistein59858462024-05-08 16:15:53 +0000529 if d.Ext() == pyExt {
530 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800531 continue
532 }
Nan Zhang1db85402017-12-18 13:20:23 -0800533 runfilesPath := filepath.Join(pkgPath, d.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800534 if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800535 p.dataPathMappings = append(p.dataPathMappings,
536 pathMapping{dest: runfilesPath, src: d})
537 }
538 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800539}
540
Liz Kammerd737d022020-11-16 15:42:51 -0800541// createSrcsZip registers build actions to zip current module's sources and data.
Cole Faust4d247e62023-01-23 10:14:58 -0800542func (p *PythonLibraryModule) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800543 relativeRootMap := make(map[string]android.Paths)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800544 var protoSrcs android.Paths
Cole Faust5c503d12023-01-24 11:48:08 -0800545 addPathMapping := func(path pathMapping) {
Raphael Blistein59858462024-05-08 16:15:53 +0000546 relativeRoot := strings.TrimSuffix(path.src.String(), path.src.Rel())
547 relativeRootMap[relativeRoot] = append(relativeRootMap[relativeRoot], path.src)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800548 }
Cole Faust5c503d12023-01-24 11:48:08 -0800549
550 // "srcs" or "data" properties may contain filegroups so it might happen that
551 // the root directory for each source path is different.
552 for _, path := range p.srcsPathMappings {
Raphael Blistein59858462024-05-08 16:15:53 +0000553 // handle proto sources separately
554 if path.src.Ext() == protoExt {
555 protoSrcs = append(protoSrcs, path.src)
556 } else {
557 addPathMapping(path)
558 }
Cole Faust5c503d12023-01-24 11:48:08 -0800559 }
560 for _, path := range p.dataPathMappings {
561 addPathMapping(path)
562 }
563
Nan Zhangb8fa1972017-12-22 16:12:00 -0800564 var zips android.Paths
565 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700566 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
567 protoFlags.OutTypeFlag = "--python_out"
568
Cole Faustcaf766b2022-10-21 16:07:56 -0700569 if pkgPath != "" {
Cole Faust43ac21f2022-09-19 11:19:52 -0700570 pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path")
571 rule := android.NewRuleBuilder(pctx, ctx)
572 var stagedProtoSrcs android.Paths
573 for _, srcFile := range protoSrcs {
574 stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel())
Cole Faust43ac21f2022-09-19 11:19:52 -0700575 rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc)
576 stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc)
577 }
578 rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path")
579 protoSrcs = stagedProtoSrcs
Cole Faust43ac21f2022-09-19 11:19:52 -0700580 }
581
Nan Zhangb8fa1972017-12-22 16:12:00 -0800582 for _, srcFile := range protoSrcs {
Cole Faustcaf766b2022-10-21 16:07:56 -0700583 zip := genProto(ctx, srcFile, protoFlags)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800584 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800585 }
586 }
587
Nan Zhangb8fa1972017-12-22 16:12:00 -0800588 if len(relativeRootMap) > 0 {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800589 // in order to keep stable order of soong_zip params, we sort the keys here.
Cole Faust18994c72023-02-28 16:02:16 -0800590 roots := android.SortedKeys(relativeRootMap)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800591
Cole Faust01243362022-06-02 12:11:12 -0700592 // Use -symlinks=false so that the symlinks in the bazel output directory are followed
593 parArgs := []string{"-symlinks=false"}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700594 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800595 // use package path as path prefix
Nan Zhangf0c4e432018-05-22 14:50:18 -0700596 parArgs = append(parArgs, `-P `+pkgPath)
597 }
Liz Kammerd737d022020-11-16 15:42:51 -0800598 paths := android.Paths{}
599 for _, root := range roots {
600 // specify relative root of file in following -f arguments
601 parArgs = append(parArgs, `-C `+root)
602 for _, path := range relativeRootMap[root] {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800603 parArgs = append(parArgs, `-f `+path.String())
Liz Kammerd737d022020-11-16 15:42:51 -0800604 paths = append(paths, path)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800605 }
606 }
607
608 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
609 ctx.Build(pctx, android.BuildParams{
610 Rule: zip,
611 Description: "python library archive",
612 Output: origSrcsZip,
Liz Kammerd737d022020-11-16 15:42:51 -0800613 // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits
614 Implicits: paths,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800615 Args: map[string]string{
616 "args": strings.Join(parArgs, " "),
617 },
618 })
619 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800620 }
Liz Kammerd737d022020-11-16 15:42:51 -0800621 // we may have multiple zips due to separate handling of proto source files
Nan Zhangb8fa1972017-12-22 16:12:00 -0800622 if len(zips) == 1 {
623 return zips[0]
624 } else {
625 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
626 ctx.Build(pctx, android.BuildParams{
627 Rule: combineZip,
628 Description: "combine python library archive",
629 Output: combinedSrcsZip,
630 Inputs: zips,
631 })
632 return combinedSrcsZip
633 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800634}
635
Cole Faust5c503d12023-01-24 11:48:08 -0800636func (p *PythonLibraryModule) precompileSrcs(ctx android.ModuleContext) android.Path {
637 // To precompile the python sources, we need a python interpreter and stdlib built
638 // for host. We then use those to compile the python sources, which may be used on either
639 // host of device. Python bytecode is architecture agnostic, so we're essentially
640 // "cross compiling" for device here purely by virtue of host and device python bytecode
641 // being the same.
642 var stdLib android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400643 var stdLibPkg string
Cole Faust5c503d12023-01-24 11:48:08 -0800644 var launcher android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400645 if proptools.BoolDefault(p.properties.Is_internal, false) {
Cole Faust5c503d12023-01-24 11:48:08 -0800646 stdLib = p.srcsZip
Dan Willemsen339a63f2023-08-15 22:17:03 -0400647 stdLibPkg = p.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800648 } else {
649 ctx.VisitDirectDepsWithTag(hostStdLibTag, func(module android.Module) {
650 if dep, ok := module.(pythonDependency); ok {
651 stdLib = dep.getPrecompiledSrcsZip()
Dan Willemsen339a63f2023-08-15 22:17:03 -0400652 stdLibPkg = dep.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800653 }
654 })
655 }
656 ctx.VisitDirectDepsWithTag(hostLauncherTag, func(module android.Module) {
657 if dep, ok := module.(IntermPathProvider); ok {
658 optionalLauncher := dep.IntermPathForModuleOut()
659 if optionalLauncher.Valid() {
660 launcher = optionalLauncher.Path()
661 }
Cole Faust909d2372023-02-13 23:17:40 +0000662 }
663 })
664 var launcherSharedLibs android.Paths
665 var ldLibraryPath []string
666 ctx.VisitDirectDepsWithTag(hostlauncherSharedLibTag, func(module android.Module) {
667 if dep, ok := module.(IntermPathProvider); ok {
668 optionalPath := dep.IntermPathForModuleOut()
669 if optionalPath.Valid() {
670 launcherSharedLibs = append(launcherSharedLibs, optionalPath.Path())
671 ldLibraryPath = append(ldLibraryPath, filepath.Dir(optionalPath.Path().String()))
Cole Faust5c503d12023-01-24 11:48:08 -0800672 }
673 }
674 })
675
676 out := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszipprecompiled")
677 if stdLib == nil || launcher == nil {
678 // This shouldn't happen in a real build because we'll error out when adding dependencies
679 // on the stdlib and launcher if they don't exist. But some tests set
680 // AllowMissingDependencies.
681 return out
682 }
683 ctx.Build(pctx, android.BuildParams{
684 Rule: precompile,
685 Input: p.srcsZip,
686 Output: out,
687 Implicits: launcherSharedLibs,
688 Description: "Precompile the python sources of " + ctx.ModuleName(),
689 Args: map[string]string{
690 "stdlibZip": stdLib.String(),
Dan Willemsen339a63f2023-08-15 22:17:03 -0400691 "stdlibPkg": stdLibPkg,
Cole Faust5c503d12023-01-24 11:48:08 -0800692 "launcher": launcher.String(),
693 "ldLibraryPath": strings.Join(ldLibraryPath, ":"),
694 },
695 })
696 return out
697}
698
Cole Faust4d247e62023-01-23 10:14:58 -0800699// isPythonLibModule returns whether the given module is a Python library PythonLibraryModule or not
Nan Zhangd4e641b2017-07-12 12:55:28 -0700700func isPythonLibModule(module blueprint.Module) bool {
Cole Faust4d247e62023-01-23 10:14:58 -0800701 if _, ok := module.(*PythonLibraryModule); ok {
702 if _, ok := module.(*PythonBinaryModule); !ok {
703 return true
704 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700705 }
706 return false
707}
708
Liz Kammerd737d022020-11-16 15:42:51 -0800709// collectPathsFromTransitiveDeps checks for source/data files for duplicate paths
710// for module and its transitive dependencies and collects list of data/source file
711// zips for transitive dependencies.
Cole Faust5c503d12023-01-24 11:48:08 -0800712func (p *PythonLibraryModule) collectPathsFromTransitiveDeps(ctx android.ModuleContext, precompiled bool) android.Paths {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800713 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
714 // check duplicates.
715 destToPySrcs := make(map[string]string)
716 destToPyData := make(map[string]string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800717 for _, path := range p.srcsPathMappings {
718 destToPySrcs[path.dest] = path.src.String()
719 }
720 for _, path := range p.dataPathMappings {
721 destToPyData[path.dest] = path.src.String()
722 }
723
Colin Cross6b753602018-06-21 13:03:07 -0700724 seen := make(map[android.Module]bool)
725
Cole Faust4d247e62023-01-23 10:14:58 -0800726 var result android.Paths
727
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800728 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700729 ctx.WalkDeps(func(child, parent android.Module) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800730 // we only collect dependencies tagged as python library deps
Colin Cross6b753602018-06-21 13:03:07 -0700731 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
732 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800733 }
Colin Cross6b753602018-06-21 13:03:07 -0700734 if seen[child] {
735 return false
736 }
737 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800738 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700739 if !isPythonLibModule(child) {
Liz Kammerd737d022020-11-16 15:42:51 -0800740 ctx.PropertyErrorf("libs",
Nan Zhangd4e641b2017-07-12 12:55:28 -0700741 "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 +0000742 ctx.OtherModuleName(child), ctx.ModuleName())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700743 }
Liz Kammerd737d022020-11-16 15:42:51 -0800744 // collect source and data paths, checking that there are no duplicate output file conflicts
745 if dep, ok := child.(pythonDependency); ok {
746 srcs := dep.getSrcsPathMappings()
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800747 for _, path := range srcs {
Liz Kammerd737d022020-11-16 15:42:51 -0800748 checkForDuplicateOutputPath(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700749 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800750 }
Liz Kammerd737d022020-11-16 15:42:51 -0800751 data := dep.getDataPathMappings()
752 for _, path := range data {
753 checkForDuplicateOutputPath(ctx, destToPyData,
754 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
755 }
Cole Faust5c503d12023-01-24 11:48:08 -0800756 if precompiled {
757 result = append(result, dep.getPrecompiledSrcsZip())
758 } else {
759 result = append(result, dep.getSrcsZip())
760 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800761 }
Colin Cross6b753602018-06-21 13:03:07 -0700762 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800763 })
Cole Faust4d247e62023-01-23 10:14:58 -0800764 return result
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800765}
766
Liz Kammerd737d022020-11-16 15:42:51 -0800767// chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which
768// would result in two files being placed in the same location.
769// If there is a duplicate path, an error is thrown and true is returned
770// Otherwise, outputPath: srcPath is added to m and returns false
771func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool {
772 if oldSrcPath, found := m[outputPath]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700773 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800774 " First file: in module %s at path %q."+
775 " Second file: in module %s at path %q.",
Liz Kammerd737d022020-11-16 15:42:51 -0800776 outputPath, curModule, oldSrcPath, otherModule, srcPath)
777 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800778 }
Liz Kammerd737d022020-11-16 15:42:51 -0800779 m[outputPath] = srcPath
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800780
Liz Kammerd737d022020-11-16 15:42:51 -0800781 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800782}
Nan Zhangea568a42017-11-08 21:20:04 -0800783
Liz Kammerd737d022020-11-16 15:42:51 -0800784// InstallInData returns true as Python is not supported in the system partition
Cole Faust4d247e62023-01-23 10:14:58 -0800785func (p *PythonLibraryModule) InstallInData() bool {
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000786 return true
787}
788
Nan Zhangea568a42017-11-08 21:20:04 -0800789var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800790var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800791var String = proptools.String