blob: 1ee533fa80c840825a2b0f30393550f11f83138e [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
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"
Cole Faustd82f0362023-04-12 17:32:19 -0700248 pyVersion2And3 = "PY2ANDPY3"
Cole Faust909d2372023-02-13 23:17:40 +0000249 internalPath = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800250)
251
Cole Faust4d247e62023-01-23 10:14:58 -0800252type basePropertiesProvider interface {
253 getBaseProperties() *BaseProperties
254}
255
Liz Kammerd737d022020-11-16 15:42:51 -0800256// versionSplitMutator creates version variants for modules and appends the version-specific
257// properties for a given variant to the properties in the variant module
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800258func versionSplitMutator() func(android.BottomUpMutatorContext) {
259 return func(mctx android.BottomUpMutatorContext) {
Cole Faust4d247e62023-01-23 10:14:58 -0800260 if base, ok := mctx.Module().(basePropertiesProvider); ok {
261 props := base.getBaseProperties()
262 var versionNames []string
Liz Kammerd737d022020-11-16 15:42:51 -0800263 // collect version specific properties, so that we can merge version-specific properties
264 // into the module's overall properties
Cole Faust4d247e62023-01-23 10:14:58 -0800265 var versionProps []VersionProperties
Liz Kammerdd849a82020-06-12 16:38:45 -0700266 // PY3 is first so that we alias the PY3 variant rather than PY2 if both
267 // are available
Cole Faust4d247e62023-01-23 10:14:58 -0800268 if proptools.BoolDefault(props.Version.Py3.Enabled, true) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800269 versionNames = append(versionNames, pyVersion3)
Cole Faust4d247e62023-01-23 10:14:58 -0800270 versionProps = append(versionProps, props.Version.Py3)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800271 }
Cole Faust4d247e62023-01-23 10:14:58 -0800272 if proptools.BoolDefault(props.Version.Py2.Enabled, false) {
Cole Faustedc4c502022-09-09 19:39:25 -0700273 if !mctx.DeviceConfig().BuildBrokenUsesSoongPython2Modules() &&
Cole Faustedc4c502022-09-09 19:39:25 -0700274 mctx.ModuleName() != "py2-cmd" &&
275 mctx.ModuleName() != "py2-stdlib" {
276 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")
277 }
Liz Kammerdd849a82020-06-12 16:38:45 -0700278 versionNames = append(versionNames, pyVersion2)
Cole Faust4d247e62023-01-23 10:14:58 -0800279 versionProps = append(versionProps, props.Version.Py2)
Liz Kammerdd849a82020-06-12 16:38:45 -0700280 }
Colin Crosse20113d2020-11-22 19:37:44 -0800281 modules := mctx.CreateLocalVariations(versionNames...)
Liz Kammerd737d022020-11-16 15:42:51 -0800282 // Alias module to the first variant
Liz Kammerdd849a82020-06-12 16:38:45 -0700283 if len(versionNames) > 0 {
284 mctx.AliasVariation(versionNames[0])
285 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800286 for i, v := range versionNames {
287 // set the actual version for Python module.
Cole Faust4d247e62023-01-23 10:14:58 -0800288 newProps := modules[i].(basePropertiesProvider).getBaseProperties()
289 newProps.Actual_version = v
Liz Kammerd737d022020-11-16 15:42:51 -0800290 // append versioned properties for the Python module to the overall properties
Cole Faust4d247e62023-01-23 10:14:58 -0800291 err := proptools.AppendMatchingProperties([]interface{}{newProps}, &versionProps[i], nil)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700292 if err != nil {
293 panic(err)
294 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800295 }
296 }
297 }
298}
299
Liz Kammerd737d022020-11-16 15:42:51 -0800300func anyHasExt(paths []string, ext string) bool {
301 for _, p := range paths {
302 if filepath.Ext(p) == ext {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800303 return true
304 }
305 }
306
307 return false
308}
309
Cole Faust4d247e62023-01-23 10:14:58 -0800310func (p *PythonLibraryModule) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800311 return anyHasExt(p.properties.Srcs, ext)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800312}
313
Liz Kammerd737d022020-11-16 15:42:51 -0800314// DepsMutator mutates dependencies for this module:
Colin Crossd079e0b2022-08-16 10:27:33 -0700315// - handles proto dependencies,
316// - if required, specifies launcher and adds launcher dependencies,
317// - applies python version mutations to Python dependencies
Cole Faust4d247e62023-01-23 10:14:58 -0800318func (p *PythonLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700319 android.ProtoDeps(ctx, &p.protoProperties)
320
Colin Crosse20113d2020-11-22 19:37:44 -0800321 versionVariation := []blueprint.Variation{
322 {"python_version", p.properties.Actual_version},
Nan Zhangb8fa1972017-12-22 16:12:00 -0800323 }
Colin Crosse20113d2020-11-22 19:37:44 -0800324
Liz Kammerd737d022020-11-16 15:42:51 -0800325 // If sources contain a proto file, add dependency on libprotobuf-python
326 if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
Colin Crosse20113d2020-11-22 19:37:44 -0800327 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python")
328 }
Liz Kammerd737d022020-11-16 15:42:51 -0800329
330 // Add python library dependencies for this python version variation
Colin Crosse20113d2020-11-22 19:37:44 -0800331 ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700332
Colin Cross1bc63932020-11-22 20:12:45 -0800333 // Emulate the data property for java_data but with the arch variation overridden to "common"
334 // so that it can point to java modules.
335 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
336 ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
Cole Faust5c503d12023-01-24 11:48:08 -0800337
Cole Faust909d2372023-02-13 23:17:40 +0000338 p.AddDepsOnPythonLauncherAndStdlib(ctx, hostStdLibTag, hostLauncherTag, hostlauncherSharedLibTag, false, ctx.Config().BuildOSTarget)
Cole Faust5c503d12023-01-24 11:48:08 -0800339}
340
Cole Faust909d2372023-02-13 23:17:40 +0000341// AddDepsOnPythonLauncherAndStdlib will make the current module depend on the python stdlib,
342// launcher (interpreter), and the launcher's shared libraries. If autorun is true, it will use
343// the autorun launcher instead of the regular one. This function acceps a targetForDeps argument
344// as the target to use for these dependencies. For embedded launcher python binaries, the launcher
345// that will be embedded will be under the same target as the python module itself. But when
346// precompiling python code, we need to get the python launcher built for host, even if we're
347// compiling the python module for device, so we pass a different target to this function.
Cole Faust5c503d12023-01-24 11:48:08 -0800348func (p *PythonLibraryModule) AddDepsOnPythonLauncherAndStdlib(ctx android.BottomUpMutatorContext,
Cole Faust909d2372023-02-13 23:17:40 +0000349 stdLibTag, launcherTag, launcherSharedLibTag blueprint.DependencyTag,
Cole Faust5c503d12023-01-24 11:48:08 -0800350 autorun bool, targetForDeps android.Target) {
351 var stdLib string
352 var launcherModule string
Cole Faust909d2372023-02-13 23:17:40 +0000353 // Add launcher shared lib dependencies. Ideally, these should be
354 // derived from the `shared_libs` property of the launcher. TODO: read these from
355 // the python launcher itself using ctx.OtherModuleProvider() or similar on the result
356 // of ctx.AddFarVariationDependencies()
357 launcherSharedLibDeps := []string{
358 "libsqlite",
359 }
360 // Add launcher-specific dependencies for bionic
361 if targetForDeps.Os.Bionic() {
362 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm")
363 }
364 if targetForDeps.Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() {
365 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc_musl")
366 }
Cole Faust5c503d12023-01-24 11:48:08 -0800367
368 switch p.properties.Actual_version {
369 case pyVersion2:
370 stdLib = "py2-stdlib"
371
372 launcherModule = "py2-launcher"
373 if autorun {
374 launcherModule = "py2-launcher-autorun"
375 }
376
Cole Faust909d2372023-02-13 23:17:40 +0000377 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++")
Cole Faust5c503d12023-01-24 11:48:08 -0800378 case pyVersion3:
Dan Willemsen339a63f2023-08-15 22:17:03 -0400379 var prebuiltStdLib bool
380 if targetForDeps.Os.Bionic() {
381 prebuiltStdLib = false
382 } else if ctx.Config().VendorConfig("cpython3").Bool("force_build_host") {
383 prebuiltStdLib = false
384 } else {
385 prebuiltStdLib = true
386 }
387
388 if prebuiltStdLib {
389 stdLib = "py3-stdlib-prebuilt"
390 } else {
391 stdLib = "py3-stdlib"
392 }
Cole Faust5c503d12023-01-24 11:48:08 -0800393
394 launcherModule = "py3-launcher"
395 if autorun {
396 launcherModule = "py3-launcher-autorun"
397 }
398 if ctx.Config().HostStaticBinaries() && targetForDeps.Os == android.LinuxMusl {
399 launcherModule += "-static"
400 }
Cole Faust909d2372023-02-13 23:17:40 +0000401 if ctx.Device() {
402 launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog")
403 }
Cole Faust5c503d12023-01-24 11:48:08 -0800404 default:
405 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
406 p.properties.Actual_version, ctx.ModuleName()))
407 }
408 targetVariations := targetForDeps.Variations()
409 if ctx.ModuleName() != stdLib {
410 stdLibVariations := make([]blueprint.Variation, 0, len(targetVariations)+1)
411 stdLibVariations = append(stdLibVariations, blueprint.Variation{Mutator: "python_version", Variation: p.properties.Actual_version})
412 stdLibVariations = append(stdLibVariations, targetVariations...)
413 // Using AddFarVariationDependencies for all of these because they can be for a different
414 // platform, like if the python module itself was being compiled for device, we may want
415 // the python interpreter built for host so that we can precompile python sources.
416 ctx.AddFarVariationDependencies(stdLibVariations, stdLibTag, stdLib)
417 }
418 ctx.AddFarVariationDependencies(targetVariations, launcherTag, launcherModule)
Cole Faust909d2372023-02-13 23:17:40 +0000419 ctx.AddFarVariationDependencies(targetVariations, launcherSharedLibTag, launcherSharedLibDeps...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800420}
421
Cole Faust4d247e62023-01-23 10:14:58 -0800422// GenerateAndroidBuildActions performs build actions common to all Python modules
423func (p *PythonLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammerd737d022020-11-16 15:42:51 -0800424 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs)
Colin Cross40213022023-12-13 15:19:49 -0800425 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: expandedSrcs.Strings()})
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700426 // Keep before any early returns.
427 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
428 TestOnly: Bool(p.sourceProperties.Test_only),
429 TopLevelTarget: p.sourceProperties.Top_level_test_target,
430 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800431
432 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800433 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800434
Colin Cross1bc63932020-11-22 20:12:45 -0800435 // Emulate the data property for java_data dependencies.
436 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
437 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
438 }
439
Liz Kammerd737d022020-11-16 15:42:51 -0800440 // Validate pkg_path property
Nan Zhang1db85402017-12-18 13:20:23 -0800441 pkgPath := String(p.properties.Pkg_path)
442 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800443 // TODO: export validation from android/paths.go handling to replace this duplicated functionality
Nan Zhang1db85402017-12-18 13:20:23 -0800444 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
445 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
446 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700447 ctx.PropertyErrorf("pkg_path",
448 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800449 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700450 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800451 }
Liz Kammerd737d022020-11-16 15:42:51 -0800452 }
453 // If property Is_internal is set, prepend pkgPath with internalPath
454 if proptools.BoolDefault(p.properties.Is_internal, false) {
455 pkgPath = filepath.Join(internalPath, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800456 }
457
Liz Kammerd737d022020-11-16 15:42:51 -0800458 // generate src:destination path mappings for this module
Nan Zhang1db85402017-12-18 13:20:23 -0800459 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800460
Liz Kammerd737d022020-11-16 15:42:51 -0800461 // generate the zipfile of all source and data files
Nan Zhang1db85402017-12-18 13:20:23 -0800462 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Dan Willemsenfe2dafc2023-08-24 22:59:16 +0000463 p.precompiledSrcsZip = p.precompileSrcs(ctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800464}
465
Liz Kammerd737d022020-11-16 15:42:51 -0800466func isValidPythonPath(path string) error {
467 identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/")
468 for _, token := range identifiers {
469 if !pathComponentRegexp.MatchString(token) {
470 return fmt.Errorf("the path %q contains invalid subpath %q. "+
471 "Subpaths must be at least one character long. "+
472 "The first character must an underscore or letter. "+
473 "Following characters may be any of: letter, digit, underscore, hyphen.",
474 path, token)
475 }
476 }
477 return nil
478}
479
480// For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path>
481// for python/data files expanded from properties.
Cole Faust4d247e62023-01-23 10:14:58 -0800482func (p *PythonLibraryModule) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800483 expandedSrcs, expandedData android.Paths) {
484 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800485 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800486 destToPySrcs := make(map[string]string)
487 destToPyData := make(map[string]string)
488
Dan Willemsen339a63f2023-08-15 22:17:03 -0400489 // Disable path checks for the stdlib, as it includes a "." in the version string
490 isInternal := proptools.BoolDefault(p.properties.Is_internal, false)
491
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800492 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800493 if s.Ext() != pyExt && s.Ext() != protoExt {
494 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800495 continue
496 }
Nan Zhang1db85402017-12-18 13:20:23 -0800497 runfilesPath := filepath.Join(pkgPath, s.Rel())
Dan Willemsen339a63f2023-08-15 22:17:03 -0400498 if !isInternal {
499 if err := isValidPythonPath(runfilesPath); err != nil {
500 ctx.PropertyErrorf("srcs", err.Error())
501 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800502 }
Liz Kammerd737d022020-11-16 15:42:51 -0800503 if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
504 p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800505 }
506 }
507
508 for _, d := range expandedData {
Raphael Blistein59858462024-05-08 16:15:53 +0000509 if d.Ext() == pyExt {
510 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800511 continue
512 }
Nan Zhang1db85402017-12-18 13:20:23 -0800513 runfilesPath := filepath.Join(pkgPath, d.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800514 if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800515 p.dataPathMappings = append(p.dataPathMappings,
516 pathMapping{dest: runfilesPath, src: d})
517 }
518 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800519}
520
Liz Kammerd737d022020-11-16 15:42:51 -0800521// createSrcsZip registers build actions to zip current module's sources and data.
Cole Faust4d247e62023-01-23 10:14:58 -0800522func (p *PythonLibraryModule) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800523 relativeRootMap := make(map[string]android.Paths)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800524 var protoSrcs android.Paths
Cole Faust5c503d12023-01-24 11:48:08 -0800525 addPathMapping := func(path pathMapping) {
Raphael Blistein59858462024-05-08 16:15:53 +0000526 relativeRoot := strings.TrimSuffix(path.src.String(), path.src.Rel())
527 relativeRootMap[relativeRoot] = append(relativeRootMap[relativeRoot], path.src)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800528 }
Cole Faust5c503d12023-01-24 11:48:08 -0800529
530 // "srcs" or "data" properties may contain filegroups so it might happen that
531 // the root directory for each source path is different.
532 for _, path := range p.srcsPathMappings {
Raphael Blistein59858462024-05-08 16:15:53 +0000533 // handle proto sources separately
534 if path.src.Ext() == protoExt {
535 protoSrcs = append(protoSrcs, path.src)
536 } else {
537 addPathMapping(path)
538 }
Cole Faust5c503d12023-01-24 11:48:08 -0800539 }
540 for _, path := range p.dataPathMappings {
541 addPathMapping(path)
542 }
543
Nan Zhangb8fa1972017-12-22 16:12:00 -0800544 var zips android.Paths
545 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700546 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
547 protoFlags.OutTypeFlag = "--python_out"
548
Cole Faustcaf766b2022-10-21 16:07:56 -0700549 if pkgPath != "" {
Cole Faust43ac21f2022-09-19 11:19:52 -0700550 pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path")
551 rule := android.NewRuleBuilder(pctx, ctx)
552 var stagedProtoSrcs android.Paths
553 for _, srcFile := range protoSrcs {
554 stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel())
Cole Faust43ac21f2022-09-19 11:19:52 -0700555 rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc)
556 stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc)
557 }
558 rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path")
559 protoSrcs = stagedProtoSrcs
Cole Faust43ac21f2022-09-19 11:19:52 -0700560 }
561
Nan Zhangb8fa1972017-12-22 16:12:00 -0800562 for _, srcFile := range protoSrcs {
Cole Faustcaf766b2022-10-21 16:07:56 -0700563 zip := genProto(ctx, srcFile, protoFlags)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800564 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800565 }
566 }
567
Nan Zhangb8fa1972017-12-22 16:12:00 -0800568 if len(relativeRootMap) > 0 {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800569 // in order to keep stable order of soong_zip params, we sort the keys here.
Cole Faust18994c72023-02-28 16:02:16 -0800570 roots := android.SortedKeys(relativeRootMap)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800571
Cole Faust01243362022-06-02 12:11:12 -0700572 // Use -symlinks=false so that the symlinks in the bazel output directory are followed
573 parArgs := []string{"-symlinks=false"}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700574 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800575 // use package path as path prefix
Nan Zhangf0c4e432018-05-22 14:50:18 -0700576 parArgs = append(parArgs, `-P `+pkgPath)
577 }
Liz Kammerd737d022020-11-16 15:42:51 -0800578 paths := android.Paths{}
579 for _, root := range roots {
580 // specify relative root of file in following -f arguments
581 parArgs = append(parArgs, `-C `+root)
582 for _, path := range relativeRootMap[root] {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800583 parArgs = append(parArgs, `-f `+path.String())
Liz Kammerd737d022020-11-16 15:42:51 -0800584 paths = append(paths, path)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800585 }
586 }
587
588 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
589 ctx.Build(pctx, android.BuildParams{
590 Rule: zip,
591 Description: "python library archive",
592 Output: origSrcsZip,
Liz Kammerd737d022020-11-16 15:42:51 -0800593 // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits
594 Implicits: paths,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800595 Args: map[string]string{
596 "args": strings.Join(parArgs, " "),
597 },
598 })
599 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800600 }
Liz Kammerd737d022020-11-16 15:42:51 -0800601 // we may have multiple zips due to separate handling of proto source files
Nan Zhangb8fa1972017-12-22 16:12:00 -0800602 if len(zips) == 1 {
603 return zips[0]
604 } else {
605 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
606 ctx.Build(pctx, android.BuildParams{
607 Rule: combineZip,
608 Description: "combine python library archive",
609 Output: combinedSrcsZip,
610 Inputs: zips,
611 })
612 return combinedSrcsZip
613 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800614}
615
Cole Faust5c503d12023-01-24 11:48:08 -0800616func (p *PythonLibraryModule) precompileSrcs(ctx android.ModuleContext) android.Path {
617 // To precompile the python sources, we need a python interpreter and stdlib built
618 // for host. We then use those to compile the python sources, which may be used on either
619 // host of device. Python bytecode is architecture agnostic, so we're essentially
620 // "cross compiling" for device here purely by virtue of host and device python bytecode
621 // being the same.
622 var stdLib android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400623 var stdLibPkg string
Cole Faust5c503d12023-01-24 11:48:08 -0800624 var launcher android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400625 if proptools.BoolDefault(p.properties.Is_internal, false) {
Cole Faust5c503d12023-01-24 11:48:08 -0800626 stdLib = p.srcsZip
Dan Willemsen339a63f2023-08-15 22:17:03 -0400627 stdLibPkg = p.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800628 } else {
629 ctx.VisitDirectDepsWithTag(hostStdLibTag, func(module android.Module) {
630 if dep, ok := module.(pythonDependency); ok {
631 stdLib = dep.getPrecompiledSrcsZip()
Dan Willemsen339a63f2023-08-15 22:17:03 -0400632 stdLibPkg = dep.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800633 }
634 })
635 }
636 ctx.VisitDirectDepsWithTag(hostLauncherTag, func(module android.Module) {
637 if dep, ok := module.(IntermPathProvider); ok {
638 optionalLauncher := dep.IntermPathForModuleOut()
639 if optionalLauncher.Valid() {
640 launcher = optionalLauncher.Path()
641 }
Cole Faust909d2372023-02-13 23:17:40 +0000642 }
643 })
644 var launcherSharedLibs android.Paths
645 var ldLibraryPath []string
646 ctx.VisitDirectDepsWithTag(hostlauncherSharedLibTag, func(module android.Module) {
647 if dep, ok := module.(IntermPathProvider); ok {
648 optionalPath := dep.IntermPathForModuleOut()
649 if optionalPath.Valid() {
650 launcherSharedLibs = append(launcherSharedLibs, optionalPath.Path())
651 ldLibraryPath = append(ldLibraryPath, filepath.Dir(optionalPath.Path().String()))
Cole Faust5c503d12023-01-24 11:48:08 -0800652 }
653 }
654 })
655
656 out := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszipprecompiled")
657 if stdLib == nil || launcher == nil {
658 // This shouldn't happen in a real build because we'll error out when adding dependencies
659 // on the stdlib and launcher if they don't exist. But some tests set
660 // AllowMissingDependencies.
661 return out
662 }
663 ctx.Build(pctx, android.BuildParams{
664 Rule: precompile,
665 Input: p.srcsZip,
666 Output: out,
667 Implicits: launcherSharedLibs,
668 Description: "Precompile the python sources of " + ctx.ModuleName(),
669 Args: map[string]string{
670 "stdlibZip": stdLib.String(),
Dan Willemsen339a63f2023-08-15 22:17:03 -0400671 "stdlibPkg": stdLibPkg,
Cole Faust5c503d12023-01-24 11:48:08 -0800672 "launcher": launcher.String(),
673 "ldLibraryPath": strings.Join(ldLibraryPath, ":"),
674 },
675 })
676 return out
677}
678
Cole Faust4d247e62023-01-23 10:14:58 -0800679// isPythonLibModule returns whether the given module is a Python library PythonLibraryModule or not
Nan Zhangd4e641b2017-07-12 12:55:28 -0700680func isPythonLibModule(module blueprint.Module) bool {
Cole Faust4d247e62023-01-23 10:14:58 -0800681 if _, ok := module.(*PythonLibraryModule); ok {
682 if _, ok := module.(*PythonBinaryModule); !ok {
683 return true
684 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700685 }
686 return false
687}
688
Liz Kammerd737d022020-11-16 15:42:51 -0800689// collectPathsFromTransitiveDeps checks for source/data files for duplicate paths
690// for module and its transitive dependencies and collects list of data/source file
691// zips for transitive dependencies.
Cole Faust5c503d12023-01-24 11:48:08 -0800692func (p *PythonLibraryModule) collectPathsFromTransitiveDeps(ctx android.ModuleContext, precompiled bool) android.Paths {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800693 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
694 // check duplicates.
695 destToPySrcs := make(map[string]string)
696 destToPyData := make(map[string]string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800697 for _, path := range p.srcsPathMappings {
698 destToPySrcs[path.dest] = path.src.String()
699 }
700 for _, path := range p.dataPathMappings {
701 destToPyData[path.dest] = path.src.String()
702 }
703
Colin Cross6b753602018-06-21 13:03:07 -0700704 seen := make(map[android.Module]bool)
705
Cole Faust4d247e62023-01-23 10:14:58 -0800706 var result android.Paths
707
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800708 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700709 ctx.WalkDeps(func(child, parent android.Module) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800710 // we only collect dependencies tagged as python library deps
Colin Cross6b753602018-06-21 13:03:07 -0700711 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
712 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800713 }
Colin Cross6b753602018-06-21 13:03:07 -0700714 if seen[child] {
715 return false
716 }
717 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800718 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700719 if !isPythonLibModule(child) {
Liz Kammerd737d022020-11-16 15:42:51 -0800720 ctx.PropertyErrorf("libs",
Nan Zhangd4e641b2017-07-12 12:55:28 -0700721 "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 +0000722 ctx.OtherModuleName(child), ctx.ModuleName())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700723 }
Liz Kammerd737d022020-11-16 15:42:51 -0800724 // collect source and data paths, checking that there are no duplicate output file conflicts
725 if dep, ok := child.(pythonDependency); ok {
726 srcs := dep.getSrcsPathMappings()
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800727 for _, path := range srcs {
Liz Kammerd737d022020-11-16 15:42:51 -0800728 checkForDuplicateOutputPath(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700729 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800730 }
Liz Kammerd737d022020-11-16 15:42:51 -0800731 data := dep.getDataPathMappings()
732 for _, path := range data {
733 checkForDuplicateOutputPath(ctx, destToPyData,
734 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
735 }
Cole Faust5c503d12023-01-24 11:48:08 -0800736 if precompiled {
737 result = append(result, dep.getPrecompiledSrcsZip())
738 } else {
739 result = append(result, dep.getSrcsZip())
740 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800741 }
Colin Cross6b753602018-06-21 13:03:07 -0700742 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800743 })
Cole Faust4d247e62023-01-23 10:14:58 -0800744 return result
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800745}
746
Liz Kammerd737d022020-11-16 15:42:51 -0800747// chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which
748// would result in two files being placed in the same location.
749// If there is a duplicate path, an error is thrown and true is returned
750// Otherwise, outputPath: srcPath is added to m and returns false
751func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool {
752 if oldSrcPath, found := m[outputPath]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700753 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800754 " First file: in module %s at path %q."+
755 " Second file: in module %s at path %q.",
Liz Kammerd737d022020-11-16 15:42:51 -0800756 outputPath, curModule, oldSrcPath, otherModule, srcPath)
757 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800758 }
Liz Kammerd737d022020-11-16 15:42:51 -0800759 m[outputPath] = srcPath
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800760
Liz Kammerd737d022020-11-16 15:42:51 -0800761 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800762}
Nan Zhangea568a42017-11-08 21:20:04 -0800763
Liz Kammerd737d022020-11-16 15:42:51 -0800764// InstallInData returns true as Python is not supported in the system partition
Cole Faust4d247e62023-01-23 10:14:58 -0800765func (p *PythonLibraryModule) InstallInData() bool {
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000766 return true
767}
768
Nan Zhangea568a42017-11-08 21:20:04 -0800769var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800770var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800771var String = proptools.String