blob: 3551eb6ef9eacac1a2203b530536cb2e0a85f97b [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 Crosse20113d2020-11-22 19:37:44 -080041 ctx.BottomUp("python_version", versionSplitMutator()).Parallel()
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
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800154}
155
Liz Kammerd737d022020-11-16 15:42:51 -0800156// newModule generates new Python base module
Cole Faust4d247e62023-01-23 10:14:58 -0800157func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *PythonLibraryModule {
158 return &PythonLibraryModule{
Nan Zhangd4e641b2017-07-12 12:55:28 -0700159 hod: hod,
160 multilib: multilib,
161 }
162}
163
Liz Kammerd737d022020-11-16 15:42:51 -0800164// interface implemented by Python modules to provide source and data mappings and zip to python
165// modules that depend on it
166type pythonDependency interface {
167 getSrcsPathMappings() []pathMapping
168 getDataPathMappings() []pathMapping
169 getSrcsZip() android.Path
Cole Faust5c503d12023-01-24 11:48:08 -0800170 getPrecompiledSrcsZip() android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400171 getPkgPath() string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800172}
173
Liz Kammerd737d022020-11-16 15:42:51 -0800174// getSrcsPathMappings gets this module's path mapping of src source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800175func (p *PythonLibraryModule) getSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800176 return p.srcsPathMappings
177}
178
Liz Kammerd737d022020-11-16 15:42:51 -0800179// getSrcsPathMappings gets this module's path mapping of data source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800180func (p *PythonLibraryModule) getDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800181 return p.dataPathMappings
182}
183
Liz Kammerd737d022020-11-16 15:42:51 -0800184// getSrcsZip returns the filepath where the current module's source/data files are zipped.
Cole Faust4d247e62023-01-23 10:14:58 -0800185func (p *PythonLibraryModule) getSrcsZip() android.Path {
Nan Zhang1db85402017-12-18 13:20:23 -0800186 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800187}
188
Cole Faust5c503d12023-01-24 11:48:08 -0800189// getSrcsZip returns the filepath where the current module's source/data files are zipped.
190func (p *PythonLibraryModule) getPrecompiledSrcsZip() android.Path {
191 return p.precompiledSrcsZip
192}
193
Dan Willemsen339a63f2023-08-15 22:17:03 -0400194// getPkgPath returns the pkg_path value
195func (p *PythonLibraryModule) getPkgPath() string {
196 return String(p.properties.Pkg_path)
197}
198
Cole Faust4d247e62023-01-23 10:14:58 -0800199func (p *PythonLibraryModule) getBaseProperties() *BaseProperties {
200 return &p.properties
201}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800202
Cole Faust4d247e62023-01-23 10:14:58 -0800203var _ pythonDependency = (*PythonLibraryModule)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800204
Cole Faust4d247e62023-01-23 10:14:58 -0800205func (p *PythonLibraryModule) init() android.Module {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800206 p.AddProperties(&p.properties, &p.protoProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700207 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700208 android.InitDefaultableModule(p)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700209 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800210}
211
Liz Kammerd737d022020-11-16 15:42:51 -0800212// Python-specific tag to transfer information on the purpose of a dependency.
213// This is used when adding a dependency on a module, which can later be accessed when visiting
214// dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700215type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800216 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700217 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800218}
219
Liz Kammerd737d022020-11-16 15:42:51 -0800220// Python-specific tag that indicates that installed files of this module should depend on installed
221// files of the dependency
Colin Crosse9fe2942020-11-10 18:12:15 -0800222type installDependencyTag struct {
223 blueprint.BaseDependencyTag
Liz Kammerd737d022020-11-16 15:42:51 -0800224 // embedding this struct provides the installation dependency requirement
Colin Crosse9fe2942020-11-10 18:12:15 -0800225 android.InstallAlwaysNeededDependencyTag
226 name string
227}
228
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800229var (
Cole Faust5c503d12023-01-24 11:48:08 -0800230 pythonLibTag = dependencyTag{name: "pythonLib"}
231 javaDataTag = dependencyTag{name: "javaData"}
232 // The python interpreter, with soong module name "py3-launcher" or "py3-launcher-autorun".
Cole Faust909d2372023-02-13 23:17:40 +0000233 launcherTag = dependencyTag{name: "launcher"}
234 launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"}
Cole Faust5c503d12023-01-24 11:48:08 -0800235 // The python interpreter built for host so that we can precompile python sources.
236 // This only works because the precompiled sources don't vary by architecture.
237 // The soong module name is "py3-launcher".
Cole Faust909d2372023-02-13 23:17:40 +0000238 hostLauncherTag = dependencyTag{name: "hostLauncher"}
239 hostlauncherSharedLibTag = dependencyTag{name: "hostlauncherSharedLib"}
240 hostStdLibTag = dependencyTag{name: "hostStdLib"}
241 pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
242 pyExt = ".py"
243 protoExt = ".proto"
244 pyVersion2 = "PY2"
245 pyVersion3 = "PY3"
Cole Faustd82f0362023-04-12 17:32:19 -0700246 pyVersion2And3 = "PY2ANDPY3"
Cole Faust909d2372023-02-13 23:17:40 +0000247 internalPath = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800248)
249
Cole Faust4d247e62023-01-23 10:14:58 -0800250type basePropertiesProvider interface {
251 getBaseProperties() *BaseProperties
252}
253
Liz Kammerd737d022020-11-16 15:42:51 -0800254// versionSplitMutator creates version variants for modules and appends the version-specific
255// properties for a given variant to the properties in the variant module
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800256func versionSplitMutator() func(android.BottomUpMutatorContext) {
257 return func(mctx android.BottomUpMutatorContext) {
Cole Faust4d247e62023-01-23 10:14:58 -0800258 if base, ok := mctx.Module().(basePropertiesProvider); ok {
259 props := base.getBaseProperties()
260 var versionNames []string
Liz Kammerd737d022020-11-16 15:42:51 -0800261 // collect version specific properties, so that we can merge version-specific properties
262 // into the module's overall properties
Cole Faust4d247e62023-01-23 10:14:58 -0800263 var versionProps []VersionProperties
Liz Kammerdd849a82020-06-12 16:38:45 -0700264 // PY3 is first so that we alias the PY3 variant rather than PY2 if both
265 // are available
Cole Faust4d247e62023-01-23 10:14:58 -0800266 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800267 versionNames = append(versionNames, pyVersion3)
Cole Faust4d247e62023-01-23 10:14:58 -0800268 versionProps = append(versionProps, props.Version.Py3)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800269 }
Cole Faust4d247e62023-01-23 10:14:58 -0800270 if proptools.BoolDefault(props.Version.Py2.Enabled, false) {
Cole Faustedc4c502022-09-09 19:39:25 -0700271 if !mctx.DeviceConfig().BuildBrokenUsesSoongPython2Modules() &&
Cole Faustedc4c502022-09-09 19:39:25 -0700272 mctx.ModuleName() != "py2-cmd" &&
273 mctx.ModuleName() != "py2-stdlib" {
274 mctx.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")
275 }
Liz Kammerdd849a82020-06-12 16:38:45 -0700276 versionNames = append(versionNames, pyVersion2)
Cole Faust4d247e62023-01-23 10:14:58 -0800277 versionProps = append(versionProps, props.Version.Py2)
Liz Kammerdd849a82020-06-12 16:38:45 -0700278 }
Colin Crosse20113d2020-11-22 19:37:44 -0800279 modules := mctx.CreateLocalVariations(versionNames...)
Liz Kammerd737d022020-11-16 15:42:51 -0800280 // Alias module to the first variant
Liz Kammerdd849a82020-06-12 16:38:45 -0700281 if len(versionNames) > 0 {
282 mctx.AliasVariation(versionNames[0])
283 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800284 for i, v := range versionNames {
285 // set the actual version for Python module.
Cole Faust4d247e62023-01-23 10:14:58 -0800286 newProps := modules[i].(basePropertiesProvider).getBaseProperties()
287 newProps.Actual_version = v
Liz Kammerd737d022020-11-16 15:42:51 -0800288 // append versioned properties for the Python module to the overall properties
Cole Faust4d247e62023-01-23 10:14:58 -0800289 err := proptools.AppendMatchingProperties([]interface{}{newProps}, &versionProps[i], nil)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700290 if err != nil {
291 panic(err)
292 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800293 }
294 }
295 }
296}
297
Liz Kammerd737d022020-11-16 15:42:51 -0800298func anyHasExt(paths []string, ext string) bool {
299 for _, p := range paths {
300 if filepath.Ext(p) == ext {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800301 return true
302 }
303 }
304
305 return false
306}
307
Cole Faust4d247e62023-01-23 10:14:58 -0800308func (p *PythonLibraryModule) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800309 return anyHasExt(p.properties.Srcs, ext)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800310}
311
Liz Kammerd737d022020-11-16 15:42:51 -0800312// DepsMutator mutates dependencies for this module:
Colin Crossd079e0b2022-08-16 10:27:33 -0700313// - handles proto dependencies,
314// - if required, specifies launcher and adds launcher dependencies,
315// - applies python version mutations to Python dependencies
Cole Faust4d247e62023-01-23 10:14:58 -0800316func (p *PythonLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700317 android.ProtoDeps(ctx, &p.protoProperties)
318
Colin Crosse20113d2020-11-22 19:37:44 -0800319 versionVariation := []blueprint.Variation{
320 {"python_version", p.properties.Actual_version},
Nan Zhangb8fa1972017-12-22 16:12:00 -0800321 }
Colin Crosse20113d2020-11-22 19:37:44 -0800322
Liz Kammerd737d022020-11-16 15:42:51 -0800323 // If sources contain a proto file, add dependency on libprotobuf-python
324 if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
Colin Crosse20113d2020-11-22 19:37:44 -0800325 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python")
326 }
Liz Kammerd737d022020-11-16 15:42:51 -0800327
328 // Add python library dependencies for this python version variation
Colin Crosse20113d2020-11-22 19:37:44 -0800329 ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700330
Colin Cross1bc63932020-11-22 20:12:45 -0800331 // Emulate the data property for java_data but with the arch variation overridden to "common"
332 // so that it can point to java modules.
333 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
334 ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
Cole Faust5c503d12023-01-24 11:48:08 -0800335
Cole Faust909d2372023-02-13 23:17:40 +0000336 p.AddDepsOnPythonLauncherAndStdlib(ctx, hostStdLibTag, hostLauncherTag, hostlauncherSharedLibTag, false, ctx.Config().BuildOSTarget)
Cole Faust5c503d12023-01-24 11:48:08 -0800337}
338
Cole Faust909d2372023-02-13 23:17:40 +0000339// AddDepsOnPythonLauncherAndStdlib will make the current module depend on the python stdlib,
340// launcher (interpreter), and the launcher's shared libraries. If autorun is true, it will use
341// the autorun launcher instead of the regular one. This function acceps a targetForDeps argument
342// as the target to use for these dependencies. For embedded launcher python binaries, the launcher
343// that will be embedded will be under the same target as the python module itself. But when
344// precompiling python code, we need to get the python launcher built for host, even if we're
345// compiling the python module for device, so we pass a different target to this function.
Cole Faust5c503d12023-01-24 11:48:08 -0800346func (p *PythonLibraryModule) AddDepsOnPythonLauncherAndStdlib(ctx android.BottomUpMutatorContext,
Cole Faust909d2372023-02-13 23:17:40 +0000347 stdLibTag, launcherTag, launcherSharedLibTag blueprint.DependencyTag,
Cole Faust5c503d12023-01-24 11:48:08 -0800348 autorun bool, targetForDeps android.Target) {
349 var stdLib string
350 var launcherModule string
Cole Faust909d2372023-02-13 23:17:40 +0000351 // Add launcher shared lib dependencies. Ideally, these should be
352 // derived from the `shared_libs` property of the launcher. TODO: read these from
353 // the python launcher itself using ctx.OtherModuleProvider() or similar on the result
354 // of ctx.AddFarVariationDependencies()
355 launcherSharedLibDeps := []string{
356 "libsqlite",
357 }
358 // Add launcher-specific dependencies for bionic
359 if targetForDeps.Os.Bionic() {
360 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm")
361 }
362 if targetForDeps.Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() {
363 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc_musl")
364 }
Cole Faust5c503d12023-01-24 11:48:08 -0800365
366 switch p.properties.Actual_version {
367 case pyVersion2:
368 stdLib = "py2-stdlib"
369
370 launcherModule = "py2-launcher"
371 if autorun {
372 launcherModule = "py2-launcher-autorun"
373 }
374
Cole Faust909d2372023-02-13 23:17:40 +0000375 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++")
Cole Faust5c503d12023-01-24 11:48:08 -0800376 case pyVersion3:
Dan Willemsen339a63f2023-08-15 22:17:03 -0400377 var prebuiltStdLib bool
378 if targetForDeps.Os.Bionic() {
379 prebuiltStdLib = false
380 } else if ctx.Config().VendorConfig("cpython3").Bool("force_build_host") {
381 prebuiltStdLib = false
382 } else {
383 prebuiltStdLib = true
384 }
385
386 if prebuiltStdLib {
387 stdLib = "py3-stdlib-prebuilt"
388 } else {
389 stdLib = "py3-stdlib"
390 }
Cole Faust5c503d12023-01-24 11:48:08 -0800391
392 launcherModule = "py3-launcher"
393 if autorun {
394 launcherModule = "py3-launcher-autorun"
395 }
396 if ctx.Config().HostStaticBinaries() && targetForDeps.Os == android.LinuxMusl {
397 launcherModule += "-static"
398 }
Cole Faust909d2372023-02-13 23:17:40 +0000399 if ctx.Device() {
400 launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog")
401 }
Cole Faust5c503d12023-01-24 11:48:08 -0800402 default:
403 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
404 p.properties.Actual_version, ctx.ModuleName()))
405 }
406 targetVariations := targetForDeps.Variations()
407 if ctx.ModuleName() != stdLib {
408 stdLibVariations := make([]blueprint.Variation, 0, len(targetVariations)+1)
409 stdLibVariations = append(stdLibVariations, blueprint.Variation{Mutator: "python_version", Variation: p.properties.Actual_version})
410 stdLibVariations = append(stdLibVariations, targetVariations...)
411 // Using AddFarVariationDependencies for all of these because they can be for a different
412 // platform, like if the python module itself was being compiled for device, we may want
413 // the python interpreter built for host so that we can precompile python sources.
414 ctx.AddFarVariationDependencies(stdLibVariations, stdLibTag, stdLib)
415 }
416 ctx.AddFarVariationDependencies(targetVariations, launcherTag, launcherModule)
Cole Faust909d2372023-02-13 23:17:40 +0000417 ctx.AddFarVariationDependencies(targetVariations, launcherSharedLibTag, launcherSharedLibDeps...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800418}
419
Cole Faust4d247e62023-01-23 10:14:58 -0800420// GenerateAndroidBuildActions performs build actions common to all Python modules
421func (p *PythonLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammerd737d022020-11-16 15:42:51 -0800422 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs)
Colin Cross40213022023-12-13 15:19:49 -0800423 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: expandedSrcs.Strings()})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800424
425 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800426 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800427
Colin Cross1bc63932020-11-22 20:12:45 -0800428 // Emulate the data property for java_data dependencies.
429 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
430 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
431 }
432
Liz Kammerd737d022020-11-16 15:42:51 -0800433 // Validate pkg_path property
Nan Zhang1db85402017-12-18 13:20:23 -0800434 pkgPath := String(p.properties.Pkg_path)
435 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800436 // TODO: export validation from android/paths.go handling to replace this duplicated functionality
Nan Zhang1db85402017-12-18 13:20:23 -0800437 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
438 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
439 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700440 ctx.PropertyErrorf("pkg_path",
441 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800442 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700443 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800444 }
Liz Kammerd737d022020-11-16 15:42:51 -0800445 }
446 // If property Is_internal is set, prepend pkgPath with internalPath
447 if proptools.BoolDefault(p.properties.Is_internal, false) {
448 pkgPath = filepath.Join(internalPath, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800449 }
450
Liz Kammerd737d022020-11-16 15:42:51 -0800451 // generate src:destination path mappings for this module
Nan Zhang1db85402017-12-18 13:20:23 -0800452 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800453
Liz Kammerd737d022020-11-16 15:42:51 -0800454 // generate the zipfile of all source and data files
Nan Zhang1db85402017-12-18 13:20:23 -0800455 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Dan Willemsenfe2dafc2023-08-24 22:59:16 +0000456 p.precompiledSrcsZip = p.precompileSrcs(ctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800457}
458
Liz Kammerd737d022020-11-16 15:42:51 -0800459func isValidPythonPath(path string) error {
460 identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/")
461 for _, token := range identifiers {
462 if !pathComponentRegexp.MatchString(token) {
463 return fmt.Errorf("the path %q contains invalid subpath %q. "+
464 "Subpaths must be at least one character long. "+
465 "The first character must an underscore or letter. "+
466 "Following characters may be any of: letter, digit, underscore, hyphen.",
467 path, token)
468 }
469 }
470 return nil
471}
472
473// For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path>
474// for python/data files expanded from properties.
Cole Faust4d247e62023-01-23 10:14:58 -0800475func (p *PythonLibraryModule) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800476 expandedSrcs, expandedData android.Paths) {
477 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800478 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800479 destToPySrcs := make(map[string]string)
480 destToPyData := make(map[string]string)
481
Dan Willemsen339a63f2023-08-15 22:17:03 -0400482 // Disable path checks for the stdlib, as it includes a "." in the version string
483 isInternal := proptools.BoolDefault(p.properties.Is_internal, false)
484
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800485 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800486 if s.Ext() != pyExt && s.Ext() != protoExt {
487 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800488 continue
489 }
Nan Zhang1db85402017-12-18 13:20:23 -0800490 runfilesPath := filepath.Join(pkgPath, s.Rel())
Dan Willemsen339a63f2023-08-15 22:17:03 -0400491 if !isInternal {
492 if err := isValidPythonPath(runfilesPath); err != nil {
493 ctx.PropertyErrorf("srcs", err.Error())
494 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800495 }
Liz Kammerd737d022020-11-16 15:42:51 -0800496 if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
497 p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800498 }
499 }
500
501 for _, d := range expandedData {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800502 if d.Ext() == pyExt || d.Ext() == protoExt {
503 ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800504 continue
505 }
Nan Zhang1db85402017-12-18 13:20:23 -0800506 runfilesPath := filepath.Join(pkgPath, d.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800507 if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800508 p.dataPathMappings = append(p.dataPathMappings,
509 pathMapping{dest: runfilesPath, src: d})
510 }
511 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800512}
513
Liz Kammerd737d022020-11-16 15:42:51 -0800514// createSrcsZip registers build actions to zip current module's sources and data.
Cole Faust4d247e62023-01-23 10:14:58 -0800515func (p *PythonLibraryModule) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800516 relativeRootMap := make(map[string]android.Paths)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800517 var protoSrcs android.Paths
Cole Faust5c503d12023-01-24 11:48:08 -0800518 addPathMapping := func(path pathMapping) {
Liz Kammerd737d022020-11-16 15:42:51 -0800519 // handle proto sources separately
Nan Zhangb8fa1972017-12-22 16:12:00 -0800520 if path.src.Ext() == protoExt {
521 protoSrcs = append(protoSrcs, path.src)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800522 } else {
Cole Faust4d247e62023-01-23 10:14:58 -0800523 relativeRoot := strings.TrimSuffix(path.src.String(), path.src.Rel())
524 relativeRootMap[relativeRoot] = append(relativeRootMap[relativeRoot], path.src)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800525 }
526 }
Cole Faust5c503d12023-01-24 11:48:08 -0800527
528 // "srcs" or "data" properties may contain filegroups so it might happen that
529 // the root directory for each source path is different.
530 for _, path := range p.srcsPathMappings {
531 addPathMapping(path)
532 }
533 for _, path := range p.dataPathMappings {
534 addPathMapping(path)
535 }
536
Nan Zhangb8fa1972017-12-22 16:12:00 -0800537 var zips android.Paths
538 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700539 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
540 protoFlags.OutTypeFlag = "--python_out"
541
Cole Faustcaf766b2022-10-21 16:07:56 -0700542 if pkgPath != "" {
Cole Faust43ac21f2022-09-19 11:19:52 -0700543 pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path")
544 rule := android.NewRuleBuilder(pctx, ctx)
545 var stagedProtoSrcs android.Paths
546 for _, srcFile := range protoSrcs {
547 stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel())
548 rule.Command().Text("mkdir -p").Flag(filepath.Base(stagedProtoSrc.String()))
549 rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc)
550 stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc)
551 }
552 rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path")
553 protoSrcs = stagedProtoSrcs
Cole Faust43ac21f2022-09-19 11:19:52 -0700554 }
555
Nan Zhangb8fa1972017-12-22 16:12:00 -0800556 for _, srcFile := range protoSrcs {
Cole Faustcaf766b2022-10-21 16:07:56 -0700557 zip := genProto(ctx, srcFile, protoFlags)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800558 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800559 }
560 }
561
Nan Zhangb8fa1972017-12-22 16:12:00 -0800562 if len(relativeRootMap) > 0 {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800563 // in order to keep stable order of soong_zip params, we sort the keys here.
Cole Faust18994c72023-02-28 16:02:16 -0800564 roots := android.SortedKeys(relativeRootMap)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800565
Cole Faust01243362022-06-02 12:11:12 -0700566 // Use -symlinks=false so that the symlinks in the bazel output directory are followed
567 parArgs := []string{"-symlinks=false"}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700568 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800569 // use package path as path prefix
Nan Zhangf0c4e432018-05-22 14:50:18 -0700570 parArgs = append(parArgs, `-P `+pkgPath)
571 }
Liz Kammerd737d022020-11-16 15:42:51 -0800572 paths := android.Paths{}
573 for _, root := range roots {
574 // specify relative root of file in following -f arguments
575 parArgs = append(parArgs, `-C `+root)
576 for _, path := range relativeRootMap[root] {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800577 parArgs = append(parArgs, `-f `+path.String())
Liz Kammerd737d022020-11-16 15:42:51 -0800578 paths = append(paths, path)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800579 }
580 }
581
582 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
583 ctx.Build(pctx, android.BuildParams{
584 Rule: zip,
585 Description: "python library archive",
586 Output: origSrcsZip,
Liz Kammerd737d022020-11-16 15:42:51 -0800587 // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits
588 Implicits: paths,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800589 Args: map[string]string{
590 "args": strings.Join(parArgs, " "),
591 },
592 })
593 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800594 }
Liz Kammerd737d022020-11-16 15:42:51 -0800595 // we may have multiple zips due to separate handling of proto source files
Nan Zhangb8fa1972017-12-22 16:12:00 -0800596 if len(zips) == 1 {
597 return zips[0]
598 } else {
599 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
600 ctx.Build(pctx, android.BuildParams{
601 Rule: combineZip,
602 Description: "combine python library archive",
603 Output: combinedSrcsZip,
604 Inputs: zips,
605 })
606 return combinedSrcsZip
607 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800608}
609
Cole Faust5c503d12023-01-24 11:48:08 -0800610func (p *PythonLibraryModule) precompileSrcs(ctx android.ModuleContext) android.Path {
611 // To precompile the python sources, we need a python interpreter and stdlib built
612 // for host. We then use those to compile the python sources, which may be used on either
613 // host of device. Python bytecode is architecture agnostic, so we're essentially
614 // "cross compiling" for device here purely by virtue of host and device python bytecode
615 // being the same.
616 var stdLib android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400617 var stdLibPkg string
Cole Faust5c503d12023-01-24 11:48:08 -0800618 var launcher android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400619 if proptools.BoolDefault(p.properties.Is_internal, false) {
Cole Faust5c503d12023-01-24 11:48:08 -0800620 stdLib = p.srcsZip
Dan Willemsen339a63f2023-08-15 22:17:03 -0400621 stdLibPkg = p.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800622 } else {
623 ctx.VisitDirectDepsWithTag(hostStdLibTag, func(module android.Module) {
624 if dep, ok := module.(pythonDependency); ok {
625 stdLib = dep.getPrecompiledSrcsZip()
Dan Willemsen339a63f2023-08-15 22:17:03 -0400626 stdLibPkg = dep.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800627 }
628 })
629 }
630 ctx.VisitDirectDepsWithTag(hostLauncherTag, func(module android.Module) {
631 if dep, ok := module.(IntermPathProvider); ok {
632 optionalLauncher := dep.IntermPathForModuleOut()
633 if optionalLauncher.Valid() {
634 launcher = optionalLauncher.Path()
635 }
Cole Faust909d2372023-02-13 23:17:40 +0000636 }
637 })
638 var launcherSharedLibs android.Paths
639 var ldLibraryPath []string
640 ctx.VisitDirectDepsWithTag(hostlauncherSharedLibTag, func(module android.Module) {
641 if dep, ok := module.(IntermPathProvider); ok {
642 optionalPath := dep.IntermPathForModuleOut()
643 if optionalPath.Valid() {
644 launcherSharedLibs = append(launcherSharedLibs, optionalPath.Path())
645 ldLibraryPath = append(ldLibraryPath, filepath.Dir(optionalPath.Path().String()))
Cole Faust5c503d12023-01-24 11:48:08 -0800646 }
647 }
648 })
649
650 out := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszipprecompiled")
651 if stdLib == nil || launcher == nil {
652 // This shouldn't happen in a real build because we'll error out when adding dependencies
653 // on the stdlib and launcher if they don't exist. But some tests set
654 // AllowMissingDependencies.
655 return out
656 }
657 ctx.Build(pctx, android.BuildParams{
658 Rule: precompile,
659 Input: p.srcsZip,
660 Output: out,
661 Implicits: launcherSharedLibs,
662 Description: "Precompile the python sources of " + ctx.ModuleName(),
663 Args: map[string]string{
664 "stdlibZip": stdLib.String(),
Dan Willemsen339a63f2023-08-15 22:17:03 -0400665 "stdlibPkg": stdLibPkg,
Cole Faust5c503d12023-01-24 11:48:08 -0800666 "launcher": launcher.String(),
667 "ldLibraryPath": strings.Join(ldLibraryPath, ":"),
668 },
669 })
670 return out
671}
672
Cole Faust4d247e62023-01-23 10:14:58 -0800673// isPythonLibModule returns whether the given module is a Python library PythonLibraryModule or not
Nan Zhangd4e641b2017-07-12 12:55:28 -0700674func isPythonLibModule(module blueprint.Module) bool {
Cole Faust4d247e62023-01-23 10:14:58 -0800675 if _, ok := module.(*PythonLibraryModule); ok {
676 if _, ok := module.(*PythonBinaryModule); !ok {
677 return true
678 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700679 }
680 return false
681}
682
Liz Kammerd737d022020-11-16 15:42:51 -0800683// collectPathsFromTransitiveDeps checks for source/data files for duplicate paths
684// for module and its transitive dependencies and collects list of data/source file
685// zips for transitive dependencies.
Cole Faust5c503d12023-01-24 11:48:08 -0800686func (p *PythonLibraryModule) collectPathsFromTransitiveDeps(ctx android.ModuleContext, precompiled bool) android.Paths {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800687 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
688 // check duplicates.
689 destToPySrcs := make(map[string]string)
690 destToPyData := make(map[string]string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800691 for _, path := range p.srcsPathMappings {
692 destToPySrcs[path.dest] = path.src.String()
693 }
694 for _, path := range p.dataPathMappings {
695 destToPyData[path.dest] = path.src.String()
696 }
697
Colin Cross6b753602018-06-21 13:03:07 -0700698 seen := make(map[android.Module]bool)
699
Cole Faust4d247e62023-01-23 10:14:58 -0800700 var result android.Paths
701
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800702 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700703 ctx.WalkDeps(func(child, parent android.Module) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800704 // we only collect dependencies tagged as python library deps
Colin Cross6b753602018-06-21 13:03:07 -0700705 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
706 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800707 }
Colin Cross6b753602018-06-21 13:03:07 -0700708 if seen[child] {
709 return false
710 }
711 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800712 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700713 if !isPythonLibModule(child) {
Liz Kammerd737d022020-11-16 15:42:51 -0800714 ctx.PropertyErrorf("libs",
Nan Zhangd4e641b2017-07-12 12:55:28 -0700715 "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 +0000716 ctx.OtherModuleName(child), ctx.ModuleName())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700717 }
Liz Kammerd737d022020-11-16 15:42:51 -0800718 // collect source and data paths, checking that there are no duplicate output file conflicts
719 if dep, ok := child.(pythonDependency); ok {
720 srcs := dep.getSrcsPathMappings()
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800721 for _, path := range srcs {
Liz Kammerd737d022020-11-16 15:42:51 -0800722 checkForDuplicateOutputPath(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700723 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800724 }
Liz Kammerd737d022020-11-16 15:42:51 -0800725 data := dep.getDataPathMappings()
726 for _, path := range data {
727 checkForDuplicateOutputPath(ctx, destToPyData,
728 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
729 }
Cole Faust5c503d12023-01-24 11:48:08 -0800730 if precompiled {
731 result = append(result, dep.getPrecompiledSrcsZip())
732 } else {
733 result = append(result, dep.getSrcsZip())
734 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800735 }
Colin Cross6b753602018-06-21 13:03:07 -0700736 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800737 })
Cole Faust4d247e62023-01-23 10:14:58 -0800738 return result
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800739}
740
Liz Kammerd737d022020-11-16 15:42:51 -0800741// chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which
742// would result in two files being placed in the same location.
743// If there is a duplicate path, an error is thrown and true is returned
744// Otherwise, outputPath: srcPath is added to m and returns false
745func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool {
746 if oldSrcPath, found := m[outputPath]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700747 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800748 " First file: in module %s at path %q."+
749 " Second file: in module %s at path %q.",
Liz Kammerd737d022020-11-16 15:42:51 -0800750 outputPath, curModule, oldSrcPath, otherModule, srcPath)
751 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800752 }
Liz Kammerd737d022020-11-16 15:42:51 -0800753 m[outputPath] = srcPath
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800754
Liz Kammerd737d022020-11-16 15:42:51 -0800755 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800756}
Nan Zhangea568a42017-11-08 21:20:04 -0800757
Liz Kammerd737d022020-11-16 15:42:51 -0800758// InstallInData returns true as Python is not supported in the system partition
Cole Faust4d247e62023-01-23 10:14:58 -0800759func (p *PythonLibraryModule) InstallInData() bool {
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000760 return true
761}
762
Nan Zhangea568a42017-11-08 21:20:04 -0800763var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800764var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800765var String = proptools.String