blob: 401d91fe34b6d76cadcc878bbdf45e7a7ed00228 [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
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000025 "android/soong/bazel"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080026 "github.com/google/blueprint"
Nan Zhangd4e641b2017-07-12 12:55:28 -070027 "github.com/google/blueprint/proptools"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080028
29 "android/soong/android"
30)
31
32func init() {
Paul Duffind0890452021-03-17 21:57:08 +000033 registerPythonMutators(android.InitRegistrationContext)
34}
35
36func registerPythonMutators(ctx android.RegistrationContext) {
37 ctx.PreDepsMutators(RegisterPythonPreDepsMutators)
Liz Kammerdd849a82020-06-12 16:38:45 -070038}
39
Liz Kammerd737d022020-11-16 15:42:51 -080040// Exported to support other packages using Python modules in tests.
Liz Kammerdd849a82020-06-12 16:38:45 -070041func RegisterPythonPreDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Crosse20113d2020-11-22 19:37:44 -080042 ctx.BottomUp("python_version", versionSplitMutator()).Parallel()
Nan Zhangdb0b9a32017-02-27 10:12:13 -080043}
44
Liz Kammerd737d022020-11-16 15:42:51 -080045// the version-specific properties that apply to python modules.
Nan Zhangd4e641b2017-07-12 12:55:28 -070046type VersionProperties struct {
Liz Kammerd737d022020-11-16 15:42:51 -080047 // whether the module is required to be built with this version.
48 // Defaults to true for Python 3, and false otherwise.
Liz Kammer59c0eae2021-09-17 17:48:05 -040049 Enabled *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -080050
Liz Kammerd737d022020-11-16 15:42:51 -080051 // list of source files specific to this Python version.
52 // Using the syntax ":module", srcs may reference the outputs of other modules that produce source files,
53 // e.g. genrule or filegroup.
Colin Cross27b922f2019-03-04 22:35:41 -080054 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070055
Liz Kammerd737d022020-11-16 15:42:51 -080056 // list of source files that should not be used to build the Python module for this version.
57 // This is most useful to remove files that are not common to all Python versions.
Colin Cross27b922f2019-03-04 22:35:41 -080058 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080059
Liz Kammerd737d022020-11-16 15:42:51 -080060 // list of the Python libraries used only for this Python version.
Nan Zhangd4e641b2017-07-12 12:55:28 -070061 Libs []string `android:"arch_variant"`
62
Liz Kammerd737d022020-11-16 15:42:51 -080063 // whether the binary is required to be built with embedded launcher for this version, defaults to false.
Liz Kammer59c0eae2021-09-17 17:48:05 -040064 Embedded_launcher *bool // TODO(b/174041232): Remove this property
Nan Zhangdb0b9a32017-02-27 10:12:13 -080065}
66
Liz Kammerd737d022020-11-16 15:42:51 -080067// properties that apply to all python modules
Nan Zhangd4e641b2017-07-12 12:55:28 -070068type BaseProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080069 // the package path prefix within the output artifact at which to place the source/data
70 // files of the current module.
71 // eg. Pkg_path = "a/b/c"; Other packages can reference this module by using
72 // (from a.b.c import ...) statement.
Nan Zhangbea09752018-05-31 12:49:33 -070073 // if left unspecified, all the source/data files path is unchanged within zip file.
Liz Kammer59c0eae2021-09-17 17:48:05 -040074 Pkg_path *string
Nan Zhangd4e641b2017-07-12 12:55:28 -070075
76 // true, if the Python module is used internally, eg, Python std libs.
Liz Kammer59c0eae2021-09-17 17:48:05 -040077 Is_internal *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -080078
79 // list of source (.py) files compatible both with Python2 and Python3 used to compile the
80 // Python module.
81 // srcs may reference the outputs of other modules that produce source files like genrule
82 // or filegroup using the syntax ":module".
83 // Srcs has to be non-empty.
Colin Cross27b922f2019-03-04 22:35:41 -080084 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070085
86 // list of source files that should not be used to build the C/C++ module.
87 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080088 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080089
90 // list of files or filegroup modules that provide data that should be installed alongside
91 // the test. the file extension can be arbitrary except for (.py).
Colin Cross27b922f2019-03-04 22:35:41 -080092 Data []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080093
Colin Cross1bc63932020-11-22 20:12:45 -080094 // list of java modules that provide data that should be installed alongside the test.
95 Java_data []string
96
Nan Zhangdb0b9a32017-02-27 10:12:13 -080097 // list of the Python libraries compatible both with Python2 and Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -070098 Libs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080099
100 Version struct {
Liz Kammerd737d022020-11-16 15:42:51 -0800101 // Python2-specific properties, including whether Python2 is supported for this module
102 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700103 Py2 VersionProperties `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800104
Liz Kammerd737d022020-11-16 15:42:51 -0800105 // Python3-specific properties, including whether Python3 is supported for this module
106 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700107 Py3 VersionProperties `android:"arch_variant"`
108 } `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800109
110 // the actual version each module uses after variations created.
111 // this property name is hidden from users' perspectives, and soong will populate it during
112 // runtime.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700113 Actual_version string `blueprint:"mutated"`
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700114
Liz Kammerd737d022020-11-16 15:42:51 -0800115 // whether the module is required to be built with actual_version.
116 // this is set by the python version mutator based on version-specific properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700117 Enabled *bool `blueprint:"mutated"`
118
Liz Kammerd737d022020-11-16 15:42:51 -0800119 // whether the binary is required to be built with embedded launcher for this actual_version.
120 // this is set by the python version mutator based on version-specific properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700121 Embedded_launcher *bool `blueprint:"mutated"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800122}
123
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000124type baseAttributes struct {
125 // TODO(b/200311466): Probably not translate b/c Bazel has no good equiv
126 //Pkg_path bazel.StringAttribute
127 // TODO: Related to Pkg_bath and similarLy gated
128 //Is_internal bazel.BoolAttribute
129 // Combines Srcs and Exclude_srcs
130 Srcs bazel.LabelListAttribute
131 Deps bazel.LabelListAttribute
132 // Combines Data and Java_data (invariant)
133 Data bazel.LabelListAttribute
134}
135
Liz Kammerd737d022020-11-16 15:42:51 -0800136// Used to store files of current module after expanding dependencies
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800137type pathMapping struct {
138 dest string
139 src android.Path
140}
141
Nan Zhangd4e641b2017-07-12 12:55:28 -0700142type Module struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800143 android.ModuleBase
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700144 android.DefaultableModuleBase
Jingwen Chen13b9b422021-03-08 07:32:28 -0500145 android.BazelModuleBase
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800146
Nan Zhangb8fa1972017-12-22 16:12:00 -0800147 properties BaseProperties
148 protoProperties android.ProtoProperties
Nan Zhangd4e641b2017-07-12 12:55:28 -0700149
150 // initialize before calling Init
151 hod android.HostOrDeviceSupported
152 multilib android.Multilib
153
Liz Kammerd737d022020-11-16 15:42:51 -0800154 // interface used to bootstrap .par executable when embedded_launcher is true
155 // this should be set by Python modules which are runnable, e.g. binaries and tests
156 // bootstrapper might be nil (e.g. Python library module).
Nan Zhangd4e641b2017-07-12 12:55:28 -0700157 bootstrapper bootstrapper
158
Liz Kammerd737d022020-11-16 15:42:51 -0800159 // interface that implements functions required for installation
160 // this should be set by Python modules which are runnable, e.g. binaries and tests
161 // installer might be nil (e.g. Python library module).
Nan Zhangd4e641b2017-07-12 12:55:28 -0700162 installer installer
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800163
164 // the Python files of current module after expanding source dependencies.
165 // pathMapping: <dest: runfile_path, src: source_path>
166 srcsPathMappings []pathMapping
167
168 // the data files of current module after expanding source dependencies.
169 // pathMapping: <dest: runfile_path, src: source_path>
170 dataPathMappings []pathMapping
171
Nan Zhang1db85402017-12-18 13:20:23 -0800172 // the zip filepath for zipping current module source/data files.
173 srcsZip android.Path
Nan Zhangd4e641b2017-07-12 12:55:28 -0700174
Nan Zhang1db85402017-12-18 13:20:23 -0800175 // dependency modules' zip filepath for zipping current module source/data files.
176 depsSrcsZips android.Paths
Nan Zhangd4e641b2017-07-12 12:55:28 -0700177
178 // (.intermediate) module output path as installation source.
179 installSource android.OptionalPath
180
Liz Kammerd737d022020-11-16 15:42:51 -0800181 // Map to ensure sub-part of the AndroidMk for this module is only added once
Nan Zhang5323f8e2017-05-10 13:37:54 -0700182 subAndroidMkOnce map[subAndroidMkProvider]bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800183}
184
Liz Kammerd737d022020-11-16 15:42:51 -0800185// newModule generates new Python base module
Nan Zhangd4e641b2017-07-12 12:55:28 -0700186func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
187 return &Module{
188 hod: hod,
189 multilib: multilib,
190 }
191}
192
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000193func (m *Module) makeArchVariantBaseAttributes(ctx android.TopDownMutatorContext) baseAttributes {
194 var attrs baseAttributes
195 archVariantBaseProps := m.GetArchVariantProperties(ctx, &BaseProperties{})
196 for axis, configToProps := range archVariantBaseProps {
197 for config, props := range configToProps {
198 if baseProps, ok := props.(*BaseProperties); ok {
199 attrs.Srcs.SetSelectValue(axis, config,
200 android.BazelLabelForModuleSrcExcludes(ctx, baseProps.Srcs, baseProps.Exclude_srcs))
201 attrs.Deps.SetSelectValue(axis, config,
202 android.BazelLabelForModuleDeps(ctx, baseProps.Libs))
203 data := android.BazelLabelForModuleSrc(ctx, baseProps.Data)
204 data.Append(android.BazelLabelForModuleSrc(ctx, baseProps.Java_data))
205 attrs.Data.SetSelectValue(axis, config, data)
206 }
207 }
208 }
209 return attrs
210}
211
Liz Kammerd737d022020-11-16 15:42:51 -0800212// bootstrapper interface should be implemented for runnable modules, e.g. binary and test
Nan Zhangd4e641b2017-07-12 12:55:28 -0700213type bootstrapper interface {
214 bootstrapperProps() []interface{}
Nan Zhang1db85402017-12-18 13:20:23 -0800215 bootstrap(ctx android.ModuleContext, ActualVersion string, embeddedLauncher bool,
216 srcsPathMappings []pathMapping, srcsZip android.Path,
217 depsSrcsZips android.Paths) android.OptionalPath
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800218
219 autorun() bool
Nan Zhangd4e641b2017-07-12 12:55:28 -0700220}
221
Liz Kammerd737d022020-11-16 15:42:51 -0800222// installer interface should be implemented for installable modules, e.g. binary and test
Nan Zhangd4e641b2017-07-12 12:55:28 -0700223type installer interface {
224 install(ctx android.ModuleContext, path android.Path)
Logan Chien02880e42018-11-06 17:30:35 +0800225 setAndroidMkSharedLibs(sharedLibs []string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800226}
227
Liz Kammerd737d022020-11-16 15:42:51 -0800228// interface implemented by Python modules to provide source and data mappings and zip to python
229// modules that depend on it
230type pythonDependency interface {
231 getSrcsPathMappings() []pathMapping
232 getDataPathMappings() []pathMapping
233 getSrcsZip() android.Path
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800234}
235
Liz Kammerd737d022020-11-16 15:42:51 -0800236// getSrcsPathMappings gets this module's path mapping of src source path : runfiles destination
237func (p *Module) getSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800238 return p.srcsPathMappings
239}
240
Liz Kammerd737d022020-11-16 15:42:51 -0800241// getSrcsPathMappings gets this module's path mapping of data source path : runfiles destination
242func (p *Module) getDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800243 return p.dataPathMappings
244}
245
Liz Kammerd737d022020-11-16 15:42:51 -0800246// getSrcsZip returns the filepath where the current module's source/data files are zipped.
247func (p *Module) getSrcsZip() android.Path {
Nan Zhang1db85402017-12-18 13:20:23 -0800248 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800249}
250
Liz Kammerd737d022020-11-16 15:42:51 -0800251var _ pythonDependency = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800252
Liz Kammerd8dceb02020-11-24 08:36:14 -0800253var _ android.AndroidMkEntriesProvider = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800254
Liz Kammerd737d022020-11-16 15:42:51 -0800255func (p *Module) init(additionalProps ...interface{}) android.Module {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800256 p.AddProperties(&p.properties, &p.protoProperties)
Liz Kammerd737d022020-11-16 15:42:51 -0800257
258 // Add additional properties for bootstrapping/installation
259 // This is currently tied to the bootstrapper interface;
260 // however, these are a combination of properties for the installation and bootstrapping of a module
Nan Zhangd4e641b2017-07-12 12:55:28 -0700261 if p.bootstrapper != nil {
262 p.AddProperties(p.bootstrapper.bootstrapperProps()...)
263 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800264
Nan Zhangd4e641b2017-07-12 12:55:28 -0700265 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700266 android.InitDefaultableModule(p)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800267
Nan Zhangd4e641b2017-07-12 12:55:28 -0700268 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800269}
270
Liz Kammerd737d022020-11-16 15:42:51 -0800271// Python-specific tag to transfer information on the purpose of a dependency.
272// This is used when adding a dependency on a module, which can later be accessed when visiting
273// dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700274type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800275 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700276 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800277}
278
Liz Kammerd737d022020-11-16 15:42:51 -0800279// Python-specific tag that indicates that installed files of this module should depend on installed
280// files of the dependency
Colin Crosse9fe2942020-11-10 18:12:15 -0800281type installDependencyTag struct {
282 blueprint.BaseDependencyTag
Liz Kammerd737d022020-11-16 15:42:51 -0800283 // embedding this struct provides the installation dependency requirement
Colin Crosse9fe2942020-11-10 18:12:15 -0800284 android.InstallAlwaysNeededDependencyTag
285 name string
286}
287
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800288var (
Logan Chien02880e42018-11-06 17:30:35 +0800289 pythonLibTag = dependencyTag{name: "pythonLib"}
Colin Cross1bc63932020-11-22 20:12:45 -0800290 javaDataTag = dependencyTag{name: "javaData"}
Logan Chien02880e42018-11-06 17:30:35 +0800291 launcherTag = dependencyTag{name: "launcher"}
Colin Crosse9fe2942020-11-10 18:12:15 -0800292 launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"}
Liz Kammerd737d022020-11-16 15:42:51 -0800293 pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
Logan Chien02880e42018-11-06 17:30:35 +0800294 pyExt = ".py"
295 protoExt = ".proto"
296 pyVersion2 = "PY2"
297 pyVersion3 = "PY3"
298 initFileName = "__init__.py"
299 mainFileName = "__main__.py"
300 entryPointFile = "entry_point.txt"
301 parFileExt = ".zip"
Liz Kammerd737d022020-11-16 15:42:51 -0800302 internalPath = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800303)
304
Liz Kammerd737d022020-11-16 15:42:51 -0800305// versionSplitMutator creates version variants for modules and appends the version-specific
306// properties for a given variant to the properties in the variant module
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800307func versionSplitMutator() func(android.BottomUpMutatorContext) {
308 return func(mctx android.BottomUpMutatorContext) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700309 if base, ok := mctx.Module().(*Module); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800310 versionNames := []string{}
Liz Kammerd737d022020-11-16 15:42:51 -0800311 // collect version specific properties, so that we can merge version-specific properties
312 // into the module's overall properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700313 versionProps := []VersionProperties{}
Liz Kammerdd849a82020-06-12 16:38:45 -0700314 // PY3 is first so that we alias the PY3 variant rather than PY2 if both
315 // are available
Liz Kammerd737d022020-11-16 15:42:51 -0800316 if proptools.BoolDefault(base.properties.Version.Py3.Enabled, true) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800317 versionNames = append(versionNames, pyVersion3)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700318 versionProps = append(versionProps, base.properties.Version.Py3)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800319 }
Liz Kammerd737d022020-11-16 15:42:51 -0800320 if proptools.BoolDefault(base.properties.Version.Py2.Enabled, false) {
Liz Kammerdd849a82020-06-12 16:38:45 -0700321 versionNames = append(versionNames, pyVersion2)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700322 versionProps = append(versionProps, base.properties.Version.Py2)
Liz Kammerdd849a82020-06-12 16:38:45 -0700323 }
Colin Crosse20113d2020-11-22 19:37:44 -0800324 modules := mctx.CreateLocalVariations(versionNames...)
Liz Kammerd737d022020-11-16 15:42:51 -0800325 // Alias module to the first variant
Liz Kammerdd849a82020-06-12 16:38:45 -0700326 if len(versionNames) > 0 {
327 mctx.AliasVariation(versionNames[0])
328 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800329 for i, v := range versionNames {
330 // set the actual version for Python module.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700331 modules[i].(*Module).properties.Actual_version = v
Liz Kammerd737d022020-11-16 15:42:51 -0800332 // append versioned properties for the Python module to the overall properties
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700333 err := proptools.AppendMatchingProperties([]interface{}{&modules[i].(*Module).properties}, &versionProps[i], nil)
334 if err != nil {
335 panic(err)
336 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800337 }
338 }
339 }
340}
341
Liz Kammerd737d022020-11-16 15:42:51 -0800342// HostToolPath returns a path if appropriate such that this module can be used as a host tool,
343// fulfilling HostToolProvider interface.
Nan Zhangb8bdacf2017-12-06 15:13:10 -0800344func (p *Module) HostToolPath() android.OptionalPath {
Rob Seymour925aa092021-08-10 20:42:03 +0000345 if p.installer != nil {
346 if bin, ok := p.installer.(*binaryDecorator); ok {
347 // TODO: This should only be set when building host binaries -- tests built for device would be
348 // setting this incorrectly.
349 return android.OptionalPathForPath(bin.path)
350 }
Nan Zhangb8bdacf2017-12-06 15:13:10 -0800351 }
Rob Seymour925aa092021-08-10 20:42:03 +0000352
353 return android.OptionalPath{}
354
Nan Zhangb8bdacf2017-12-06 15:13:10 -0800355}
356
Liz Kammerd737d022020-11-16 15:42:51 -0800357// OutputFiles returns output files based on given tag, returns an error if tag is unsupported.
Liz Kammere0070ee2020-06-22 11:52:59 -0700358func (p *Module) OutputFiles(tag string) (android.Paths, error) {
359 switch tag {
360 case "":
361 if outputFile := p.installSource; outputFile.Valid() {
362 return android.Paths{outputFile.Path()}, nil
363 }
364 return android.Paths{}, nil
365 default:
366 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
367 }
368}
369
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700370func (p *Module) isEmbeddedLauncherEnabled() bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800371 return p.installer != nil && Bool(p.properties.Embedded_launcher)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700372}
373
Liz Kammerd737d022020-11-16 15:42:51 -0800374func anyHasExt(paths []string, ext string) bool {
375 for _, p := range paths {
376 if filepath.Ext(p) == ext {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800377 return true
378 }
379 }
380
381 return false
382}
383
Liz Kammerd737d022020-11-16 15:42:51 -0800384func (p *Module) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool {
385 return anyHasExt(p.properties.Srcs, ext)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800386}
387
Liz Kammerd737d022020-11-16 15:42:51 -0800388// DepsMutator mutates dependencies for this module:
389// * handles proto dependencies,
390// * if required, specifies launcher and adds launcher dependencies,
391// * applies python version mutations to Python dependencies
Nan Zhangd4e641b2017-07-12 12:55:28 -0700392func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700393 android.ProtoDeps(ctx, &p.protoProperties)
394
Colin Crosse20113d2020-11-22 19:37:44 -0800395 versionVariation := []blueprint.Variation{
396 {"python_version", p.properties.Actual_version},
Nan Zhangb8fa1972017-12-22 16:12:00 -0800397 }
Colin Crosse20113d2020-11-22 19:37:44 -0800398
Liz Kammerd737d022020-11-16 15:42:51 -0800399 // If sources contain a proto file, add dependency on libprotobuf-python
400 if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
Colin Crosse20113d2020-11-22 19:37:44 -0800401 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python")
402 }
Liz Kammerd737d022020-11-16 15:42:51 -0800403
404 // Add python library dependencies for this python version variation
Colin Crosse20113d2020-11-22 19:37:44 -0800405 ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700406
Liz Kammerd737d022020-11-16 15:42:51 -0800407 // If this module will be installed and has an embedded launcher, we need to add dependencies for:
408 // * standard library
409 // * launcher
410 // * shared dependencies of the launcher
411 if p.installer != nil && p.isEmbeddedLauncherEnabled() {
412 var stdLib string
413 var launcherModule string
414 // Add launcher shared lib dependencies. Ideally, these should be
415 // derived from the `shared_libs` property of the launcher. However, we
416 // cannot read the property at this stage and it will be too late to add
417 // dependencies later.
418 launcherSharedLibDeps := []string{
419 "libsqlite",
420 }
421 // Add launcher-specific dependencies for bionic
422 if ctx.Target().Os.Bionic() {
423 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm")
424 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700425
Liz Kammerd737d022020-11-16 15:42:51 -0800426 switch p.properties.Actual_version {
427 case pyVersion2:
428 stdLib = "py2-stdlib"
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800429
Liz Kammerd737d022020-11-16 15:42:51 -0800430 launcherModule = "py2-launcher"
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800431 if p.bootstrapper.autorun() {
432 launcherModule = "py2-launcher-autorun"
433 }
Liz Kammerd737d022020-11-16 15:42:51 -0800434 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++")
Logan Chien02880e42018-11-06 17:30:35 +0800435
Liz Kammerd737d022020-11-16 15:42:51 -0800436 case pyVersion3:
437 stdLib = "py3-stdlib"
Logan Chien02880e42018-11-06 17:30:35 +0800438
Liz Kammerd737d022020-11-16 15:42:51 -0800439 launcherModule = "py3-launcher"
Dan Willemsen8d4d7be2019-11-04 19:21:04 -0800440 if p.bootstrapper.autorun() {
441 launcherModule = "py3-launcher-autorun"
442 }
Dan Willemsen8d4d7be2019-11-04 19:21:04 -0800443
Dan Willemsend7a1dee2020-01-20 22:08:20 -0800444 if ctx.Device() {
Liz Kammerd737d022020-11-16 15:42:51 -0800445 launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog")
Dan Willemsend7a1dee2020-01-20 22:08:20 -0800446 }
Liz Kammerd737d022020-11-16 15:42:51 -0800447 default:
448 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
449 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700450 }
Liz Kammerd737d022020-11-16 15:42:51 -0800451 ctx.AddVariationDependencies(versionVariation, pythonLibTag, stdLib)
452 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule)
453 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, launcherSharedLibDeps...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800454 }
Colin Cross1bc63932020-11-22 20:12:45 -0800455
456 // Emulate the data property for java_data but with the arch variation overridden to "common"
457 // so that it can point to java modules.
458 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
459 ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800460}
461
Nan Zhangd4e641b2017-07-12 12:55:28 -0700462func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammerd737d022020-11-16 15:42:51 -0800463 p.generatePythonBuildActions(ctx)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700464
Liz Kammerd737d022020-11-16 15:42:51 -0800465 // Only Python binary and test modules have non-empty bootstrapper.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700466 if p.bootstrapper != nil {
Liz Kammerd737d022020-11-16 15:42:51 -0800467 // if the module is being installed, we need to collect all transitive dependencies to embed in
468 // the final par
469 p.collectPathsFromTransitiveDeps(ctx)
470 // bootstrap the module, including resolving main file, getting launcher path, and
471 // registering actions to build the par file
472 // bootstrap returns the binary output path
Nan Zhangd4e641b2017-07-12 12:55:28 -0700473 p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
Liz Kammerd737d022020-11-16 15:42:51 -0800474 p.isEmbeddedLauncherEnabled(), p.srcsPathMappings, p.srcsZip, p.depsSrcsZips)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700475 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700476
Liz Kammerd737d022020-11-16 15:42:51 -0800477 // Only Python binary and test modules have non-empty installer.
Logan Chien02880e42018-11-06 17:30:35 +0800478 if p.installer != nil {
479 var sharedLibs []string
Liz Kammerd737d022020-11-16 15:42:51 -0800480 // if embedded launcher is enabled, we need to collect the shared library depenendencies of the
481 // launcher
Colin Cross0e446152021-05-03 13:35:32 -0700482 for _, dep := range ctx.GetDirectDepsWithTag(launcherSharedLibTag) {
483 sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep))
484 }
485
Logan Chien02880e42018-11-06 17:30:35 +0800486 p.installer.setAndroidMkSharedLibs(sharedLibs)
487
Liz Kammerd737d022020-11-16 15:42:51 -0800488 // Install the par file from installSource
Logan Chien02880e42018-11-06 17:30:35 +0800489 if p.installSource.Valid() {
490 p.installer.install(ctx, p.installSource.Path())
491 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700492 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800493}
494
Liz Kammerd737d022020-11-16 15:42:51 -0800495// generatePythonBuildActions performs build actions common to all Python modules
496func (p *Module) generatePythonBuildActions(ctx android.ModuleContext) {
497 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs)
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800498 requiresSrcs := true
499 if p.bootstrapper != nil && !p.bootstrapper.autorun() {
500 requiresSrcs = false
501 }
502 if len(expandedSrcs) == 0 && requiresSrcs {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800503 ctx.ModuleErrorf("doesn't have any source files!")
504 }
505
506 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800507 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800508
Colin Cross1bc63932020-11-22 20:12:45 -0800509 // Emulate the data property for java_data dependencies.
510 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
511 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
512 }
513
Liz Kammerd737d022020-11-16 15:42:51 -0800514 // Validate pkg_path property
Nan Zhang1db85402017-12-18 13:20:23 -0800515 pkgPath := String(p.properties.Pkg_path)
516 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800517 // TODO: export validation from android/paths.go handling to replace this duplicated functionality
Nan Zhang1db85402017-12-18 13:20:23 -0800518 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
519 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
520 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700521 ctx.PropertyErrorf("pkg_path",
522 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800523 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700524 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800525 }
Liz Kammerd737d022020-11-16 15:42:51 -0800526 }
527 // If property Is_internal is set, prepend pkgPath with internalPath
528 if proptools.BoolDefault(p.properties.Is_internal, false) {
529 pkgPath = filepath.Join(internalPath, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800530 }
531
Liz Kammerd737d022020-11-16 15:42:51 -0800532 // generate src:destination path mappings for this module
Nan Zhang1db85402017-12-18 13:20:23 -0800533 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800534
Liz Kammerd737d022020-11-16 15:42:51 -0800535 // generate the zipfile of all source and data files
Nan Zhang1db85402017-12-18 13:20:23 -0800536 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800537}
538
Liz Kammerd737d022020-11-16 15:42:51 -0800539func isValidPythonPath(path string) error {
540 identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/")
541 for _, token := range identifiers {
542 if !pathComponentRegexp.MatchString(token) {
543 return fmt.Errorf("the path %q contains invalid subpath %q. "+
544 "Subpaths must be at least one character long. "+
545 "The first character must an underscore or letter. "+
546 "Following characters may be any of: letter, digit, underscore, hyphen.",
547 path, token)
548 }
549 }
550 return nil
551}
552
553// For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path>
554// for python/data files expanded from properties.
Nan Zhang1db85402017-12-18 13:20:23 -0800555func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800556 expandedSrcs, expandedData android.Paths) {
557 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800558 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800559 destToPySrcs := make(map[string]string)
560 destToPyData := make(map[string]string)
561
562 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800563 if s.Ext() != pyExt && s.Ext() != protoExt {
564 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800565 continue
566 }
Nan Zhang1db85402017-12-18 13:20:23 -0800567 runfilesPath := filepath.Join(pkgPath, s.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800568 if err := isValidPythonPath(runfilesPath); err != nil {
569 ctx.PropertyErrorf("srcs", err.Error())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800570 }
Liz Kammerd737d022020-11-16 15:42:51 -0800571 if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
572 p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800573 }
574 }
575
576 for _, d := range expandedData {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800577 if d.Ext() == pyExt || d.Ext() == protoExt {
578 ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800579 continue
580 }
Nan Zhang1db85402017-12-18 13:20:23 -0800581 runfilesPath := filepath.Join(pkgPath, d.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800582 if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800583 p.dataPathMappings = append(p.dataPathMappings,
584 pathMapping{dest: runfilesPath, src: d})
585 }
586 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800587}
588
Liz Kammerd737d022020-11-16 15:42:51 -0800589// createSrcsZip registers build actions to zip current module's sources and data.
Nan Zhang1db85402017-12-18 13:20:23 -0800590func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800591 relativeRootMap := make(map[string]android.Paths)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800592 pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
593
Nan Zhangb8fa1972017-12-22 16:12:00 -0800594 var protoSrcs android.Paths
Liz Kammerd737d022020-11-16 15:42:51 -0800595 // "srcs" or "data" properties may contain filegroup so it might happen that
596 // the root directory for each source path is different.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800597 for _, path := range pathMappings {
Liz Kammerd737d022020-11-16 15:42:51 -0800598 // handle proto sources separately
Nan Zhangb8fa1972017-12-22 16:12:00 -0800599 if path.src.Ext() == protoExt {
600 protoSrcs = append(protoSrcs, path.src)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800601 } else {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800602 var relativeRoot string
603 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
604 if v, found := relativeRootMap[relativeRoot]; found {
605 relativeRootMap[relativeRoot] = append(v, path.src)
606 } else {
607 relativeRootMap[relativeRoot] = android.Paths{path.src}
608 }
609 }
610 }
611 var zips android.Paths
612 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700613 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
614 protoFlags.OutTypeFlag = "--python_out"
615
Nan Zhangb8fa1972017-12-22 16:12:00 -0800616 for _, srcFile := range protoSrcs {
Colin Cross19878da2019-03-28 14:45:07 -0700617 zip := genProto(ctx, srcFile, protoFlags, pkgPath)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800618 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800619 }
620 }
621
Nan Zhangb8fa1972017-12-22 16:12:00 -0800622 if len(relativeRootMap) > 0 {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800623 // in order to keep stable order of soong_zip params, we sort the keys here.
Liz Kammerd737d022020-11-16 15:42:51 -0800624 roots := android.SortedStringKeys(relativeRootMap)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800625
626 parArgs := []string{}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700627 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800628 // use package path as path prefix
Nan Zhangf0c4e432018-05-22 14:50:18 -0700629 parArgs = append(parArgs, `-P `+pkgPath)
630 }
Liz Kammerd737d022020-11-16 15:42:51 -0800631 paths := android.Paths{}
632 for _, root := range roots {
633 // specify relative root of file in following -f arguments
634 parArgs = append(parArgs, `-C `+root)
635 for _, path := range relativeRootMap[root] {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800636 parArgs = append(parArgs, `-f `+path.String())
Liz Kammerd737d022020-11-16 15:42:51 -0800637 paths = append(paths, path)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800638 }
639 }
640
641 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
642 ctx.Build(pctx, android.BuildParams{
643 Rule: zip,
644 Description: "python library archive",
645 Output: origSrcsZip,
Liz Kammerd737d022020-11-16 15:42:51 -0800646 // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits
647 Implicits: paths,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800648 Args: map[string]string{
649 "args": strings.Join(parArgs, " "),
650 },
651 })
652 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800653 }
Liz Kammerd737d022020-11-16 15:42:51 -0800654 // we may have multiple zips due to separate handling of proto source files
Nan Zhangb8fa1972017-12-22 16:12:00 -0800655 if len(zips) == 1 {
656 return zips[0]
657 } else {
658 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
659 ctx.Build(pctx, android.BuildParams{
660 Rule: combineZip,
661 Description: "combine python library archive",
662 Output: combinedSrcsZip,
663 Inputs: zips,
664 })
665 return combinedSrcsZip
666 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800667}
668
Liz Kammerd737d022020-11-16 15:42:51 -0800669// isPythonLibModule returns whether the given module is a Python library Module or not
670// This is distinguished by the fact that Python libraries are not installable, while other Python
671// modules are.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700672func isPythonLibModule(module blueprint.Module) bool {
673 if m, ok := module.(*Module); ok {
Liz Kammerd737d022020-11-16 15:42:51 -0800674 // Python library has no bootstrapper or installer
675 if m.bootstrapper == nil && m.installer == nil {
676 return true
Nan Zhangd4e641b2017-07-12 12:55:28 -0700677 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700678 }
679 return false
680}
681
Liz Kammerd737d022020-11-16 15:42:51 -0800682// collectPathsFromTransitiveDeps checks for source/data files for duplicate paths
683// for module and its transitive dependencies and collects list of data/source file
684// zips for transitive dependencies.
685func (p *Module) collectPathsFromTransitiveDeps(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800686 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
687 // check duplicates.
688 destToPySrcs := make(map[string]string)
689 destToPyData := make(map[string]string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800690 for _, path := range p.srcsPathMappings {
691 destToPySrcs[path.dest] = path.src.String()
692 }
693 for _, path := range p.dataPathMappings {
694 destToPyData[path.dest] = path.src.String()
695 }
696
Colin Cross6b753602018-06-21 13:03:07 -0700697 seen := make(map[android.Module]bool)
698
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800699 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700700 ctx.WalkDeps(func(child, parent android.Module) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800701 // we only collect dependencies tagged as python library deps
Colin Cross6b753602018-06-21 13:03:07 -0700702 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
703 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800704 }
Colin Cross6b753602018-06-21 13:03:07 -0700705 if seen[child] {
706 return false
707 }
708 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800709 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700710 if !isPythonLibModule(child) {
Liz Kammerd737d022020-11-16 15:42:51 -0800711 ctx.PropertyErrorf("libs",
Nan Zhangd4e641b2017-07-12 12:55:28 -0700712 "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 +0000713 ctx.OtherModuleName(child), ctx.ModuleName())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700714 }
Liz Kammerd737d022020-11-16 15:42:51 -0800715 // collect source and data paths, checking that there are no duplicate output file conflicts
716 if dep, ok := child.(pythonDependency); ok {
717 srcs := dep.getSrcsPathMappings()
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800718 for _, path := range srcs {
Liz Kammerd737d022020-11-16 15:42:51 -0800719 checkForDuplicateOutputPath(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700720 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800721 }
Liz Kammerd737d022020-11-16 15:42:51 -0800722 data := dep.getDataPathMappings()
723 for _, path := range data {
724 checkForDuplicateOutputPath(ctx, destToPyData,
725 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
726 }
727 p.depsSrcsZips = append(p.depsSrcsZips, dep.getSrcsZip())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800728 }
Colin Cross6b753602018-06-21 13:03:07 -0700729 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800730 })
731}
732
Liz Kammerd737d022020-11-16 15:42:51 -0800733// chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which
734// would result in two files being placed in the same location.
735// If there is a duplicate path, an error is thrown and true is returned
736// Otherwise, outputPath: srcPath is added to m and returns false
737func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool {
738 if oldSrcPath, found := m[outputPath]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700739 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800740 " First file: in module %s at path %q."+
741 " Second file: in module %s at path %q.",
Liz Kammerd737d022020-11-16 15:42:51 -0800742 outputPath, curModule, oldSrcPath, otherModule, srcPath)
743 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800744 }
Liz Kammerd737d022020-11-16 15:42:51 -0800745 m[outputPath] = srcPath
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800746
Liz Kammerd737d022020-11-16 15:42:51 -0800747 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800748}
Nan Zhangea568a42017-11-08 21:20:04 -0800749
Liz Kammerd737d022020-11-16 15:42:51 -0800750// InstallInData returns true as Python is not supported in the system partition
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000751func (p *Module) InstallInData() bool {
752 return true
753}
754
Nan Zhangea568a42017-11-08 21:20:04 -0800755var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800756var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800757var String = proptools.String