blob: de21e39bda317a506d6ba361827391c76adff18a [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"
Brett Brotherton8c7c65e2025-03-12 20:56:07 -070023 "sort"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080024 "strings"
25
Yu Liue98f7062025-01-17 22:52:43 +000026 "android/soong/cc"
Cole Faustd85c6772025-01-31 10:54:46 -080027
Nan Zhangdb0b9a32017-02-27 10:12:13 -080028 "github.com/google/blueprint"
Brett Brotherton8c7c65e2025-03-12 20:56:07 -070029 "github.com/google/blueprint/depset"
Nan Zhangd4e641b2017-07-12 12:55:28 -070030 "github.com/google/blueprint/proptools"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080031
32 "android/soong/android"
33)
34
Yu Liue98f7062025-01-17 22:52:43 +000035type PythonLibraryInfo struct {
36 SrcsPathMappings []pathMapping
37 DataPathMappings []pathMapping
38 SrcsZip android.Path
39 PrecompiledSrcsZip android.Path
40 PkgPath string
Brett Brotherton8c7c65e2025-03-12 20:56:07 -070041 BundleSharedLibs android.Paths
Yu Liue98f7062025-01-17 22:52:43 +000042}
43
44var PythonLibraryInfoProvider = blueprint.NewProvider[PythonLibraryInfo]()
45
Liz Kammerd737d022020-11-16 15:42:51 -080046// the version-specific properties that apply to python modules.
Nan Zhangd4e641b2017-07-12 12:55:28 -070047type VersionProperties struct {
Liz Kammerd737d022020-11-16 15:42:51 -080048 // whether the module is required to be built with this version.
49 // Defaults to true for Python 3, and false otherwise.
Liz Kammer59c0eae2021-09-17 17:48:05 -040050 Enabled *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -080051
Liz Kammerd737d022020-11-16 15:42:51 -080052 // list of source files specific to this Python version.
53 // Using the syntax ":module", srcs may reference the outputs of other modules that produce source files,
54 // e.g. genrule or filegroup.
Colin Cross27b922f2019-03-04 22:35:41 -080055 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070056
Liz Kammerd737d022020-11-16 15:42:51 -080057 // list of source files that should not be used to build the Python module for this version.
58 // This is most useful to remove files that are not common to all Python versions.
Colin Cross27b922f2019-03-04 22:35:41 -080059 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080060
Liz Kammerd737d022020-11-16 15:42:51 -080061 // list of the Python libraries used only for this Python version.
Nan Zhangd4e641b2017-07-12 12:55:28 -070062 Libs []string `android:"arch_variant"`
63
Cole Faustf09101e2024-04-18 18:33:15 +000064 // whether the binary is required to be built with embedded launcher for this version, defaults to true.
Liz Kammer59c0eae2021-09-17 17:48:05 -040065 Embedded_launcher *bool // TODO(b/174041232): Remove this property
Nan Zhangdb0b9a32017-02-27 10:12:13 -080066}
67
Liz Kammerd737d022020-11-16 15:42:51 -080068// properties that apply to all python modules
Nan Zhangd4e641b2017-07-12 12:55:28 -070069type BaseProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080070 // the package path prefix within the output artifact at which to place the source/data
71 // files of the current module.
72 // eg. Pkg_path = "a/b/c"; Other packages can reference this module by using
73 // (from a.b.c import ...) statement.
Nan Zhangbea09752018-05-31 12:49:33 -070074 // if left unspecified, all the source/data files path is unchanged within zip file.
Liz Kammer59c0eae2021-09-17 17:48:05 -040075 Pkg_path *string
Nan Zhangd4e641b2017-07-12 12:55:28 -070076
77 // true, if the Python module is used internally, eg, Python std libs.
Liz Kammer59c0eae2021-09-17 17:48:05 -040078 Is_internal *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -080079
80 // list of source (.py) files compatible both with Python2 and Python3 used to compile the
81 // Python module.
82 // srcs may reference the outputs of other modules that produce source files like genrule
83 // or filegroup using the syntax ":module".
84 // Srcs has to be non-empty.
Colin Cross27b922f2019-03-04 22:35:41 -080085 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070086
87 // list of source files that should not be used to build the C/C++ module.
88 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080089 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080090
91 // list of files or filegroup modules that provide data that should be installed alongside
92 // the test. the file extension can be arbitrary except for (.py).
Colin Cross27b922f2019-03-04 22:35:41 -080093 Data []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080094
Cole Faust65cb40a2024-10-21 15:41:42 -070095 // Same as data, but will add dependencies on modules using the device's os variation and
96 // the common arch variation. Useful for a host test that wants to embed a module built for
97 // device.
98 Device_common_data []string `android:"path_device_common"`
99
Inseob Kim25f5ae42025-01-07 22:27:58 +0900100 // Same as data, but will add dependencies on modules via a device os variation and the
101 // device's first supported arch's variation. Useful for a host test that wants to embed a
102 // module built for device.
103 Device_first_data []string `android:"path_device_first"`
104
Colin Cross1bc63932020-11-22 20:12:45 -0800105 // list of java modules that provide data that should be installed alongside the test.
106 Java_data []string
107
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800108 // list of the Python libraries compatible both with Python2 and Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700109 Libs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800110
Brett Brotherton8c7c65e2025-03-12 20:56:07 -0700111 // TODO: b/403060602 - add unit tests for this property and related code
112 // list of shared libraries that should be packaged with the python code for this module.
113 Shared_libs []string `android:"arch_variant"`
114
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800115 Version struct {
Liz Kammerd737d022020-11-16 15:42:51 -0800116 // Python2-specific properties, including whether Python2 is supported for this module
117 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700118 Py2 VersionProperties `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800119
Liz Kammerd737d022020-11-16 15:42:51 -0800120 // Python3-specific properties, including whether Python3 is supported for this module
121 // and version-specific sources, exclusions and dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700122 Py3 VersionProperties `android:"arch_variant"`
123 } `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800124
Cole Faustd85c6772025-01-31 10:54:46 -0800125 // This enabled property is to accept the collapsed enabled property from the VersionProperties.
126 // It is unused now, as all builds should be python3.
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700127 Enabled *bool `blueprint:"mutated"`
128
Cole Faustd85c6772025-01-31 10:54:46 -0800129 // whether the binary is required to be built with an embedded python interpreter, defaults to
130 // true. This allows taking the resulting binary outside of the build and running it on machines
131 // that don't have python installed or may have an older version of python.
132 Embedded_launcher *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800133}
134
Liz Kammerd737d022020-11-16 15:42:51 -0800135// Used to store files of current module after expanding dependencies
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800136type pathMapping struct {
137 dest string
138 src android.Path
139}
140
Cole Faust4d247e62023-01-23 10:14:58 -0800141type PythonLibraryModule struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800142 android.ModuleBase
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700143 android.DefaultableModuleBase
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800144
Nan Zhangb8fa1972017-12-22 16:12:00 -0800145 properties BaseProperties
146 protoProperties android.ProtoProperties
Nan Zhangd4e641b2017-07-12 12:55:28 -0700147
148 // initialize before calling Init
149 hod android.HostOrDeviceSupported
150 multilib android.Multilib
151
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800152 // the Python files of current module after expanding source dependencies.
153 // pathMapping: <dest: runfile_path, src: source_path>
154 srcsPathMappings []pathMapping
155
156 // the data files of current module after expanding source dependencies.
157 // pathMapping: <dest: runfile_path, src: source_path>
158 dataPathMappings []pathMapping
159
Cole Faust5c503d12023-01-24 11:48:08 -0800160 // The zip file containing the current module's source/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800161 srcsZip android.Path
Cole Faust5c503d12023-01-24 11:48:08 -0800162
163 // The zip file containing the current module's source/data files, with the
164 // source files precompiled.
165 precompiledSrcsZip android.Path
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700166
167 sourceProperties android.SourceProperties
Brett Brotherton8c7c65e2025-03-12 20:56:07 -0700168
169 // The shared libraries that should be bundled with the python code for
170 // any standalone python binaries that depend on this module.
171 bundleSharedLibs android.Paths
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800172}
173
Liz Kammerd737d022020-11-16 15:42:51 -0800174// newModule generates new Python base module
Cole Faust4d247e62023-01-23 10:14:58 -0800175func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *PythonLibraryModule {
176 return &PythonLibraryModule{
Nan Zhangd4e641b2017-07-12 12:55:28 -0700177 hod: hod,
178 multilib: multilib,
179 }
180}
181
Liz Kammerd737d022020-11-16 15:42:51 -0800182// getSrcsPathMappings gets this module's path mapping of src source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800183func (p *PythonLibraryModule) getSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800184 return p.srcsPathMappings
185}
186
Liz Kammerd737d022020-11-16 15:42:51 -0800187// getSrcsPathMappings gets this module's path mapping of data source path : runfiles destination
Cole Faust4d247e62023-01-23 10:14:58 -0800188func (p *PythonLibraryModule) getDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800189 return p.dataPathMappings
190}
191
Liz Kammerd737d022020-11-16 15:42:51 -0800192// getSrcsZip returns the filepath where the current module's source/data files are zipped.
Cole Faust4d247e62023-01-23 10:14:58 -0800193func (p *PythonLibraryModule) getSrcsZip() android.Path {
Nan Zhang1db85402017-12-18 13:20:23 -0800194 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800195}
196
Cole Faust5c503d12023-01-24 11:48:08 -0800197// getSrcsZip returns the filepath where the current module's source/data files are zipped.
198func (p *PythonLibraryModule) getPrecompiledSrcsZip() android.Path {
199 return p.precompiledSrcsZip
200}
201
Dan Willemsen339a63f2023-08-15 22:17:03 -0400202// getPkgPath returns the pkg_path value
203func (p *PythonLibraryModule) getPkgPath() string {
204 return String(p.properties.Pkg_path)
205}
206
Cole Faust4d247e62023-01-23 10:14:58 -0800207func (p *PythonLibraryModule) getBaseProperties() *BaseProperties {
208 return &p.properties
209}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800210
Brett Brotherton8c7c65e2025-03-12 20:56:07 -0700211func (p *PythonLibraryModule) getBundleSharedLibs() android.Paths {
212 return p.bundleSharedLibs
213}
214
Cole Faust4d247e62023-01-23 10:14:58 -0800215func (p *PythonLibraryModule) init() android.Module {
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700216 p.AddProperties(&p.properties, &p.protoProperties, &p.sourceProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700217 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700218 android.InitDefaultableModule(p)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700219 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800220}
221
Liz Kammerd737d022020-11-16 15:42:51 -0800222// Python-specific tag to transfer information on the purpose of a dependency.
223// This is used when adding a dependency on a module, which can later be accessed when visiting
224// dependencies.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700225type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800226 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700227 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800228}
229
Liz Kammerd737d022020-11-16 15:42:51 -0800230// Python-specific tag that indicates that installed files of this module should depend on installed
231// files of the dependency
Colin Crosse9fe2942020-11-10 18:12:15 -0800232type installDependencyTag struct {
233 blueprint.BaseDependencyTag
Liz Kammerd737d022020-11-16 15:42:51 -0800234 // embedding this struct provides the installation dependency requirement
Colin Crosse9fe2942020-11-10 18:12:15 -0800235 android.InstallAlwaysNeededDependencyTag
236 name string
237}
238
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800239var (
Cole Faust5c503d12023-01-24 11:48:08 -0800240 pythonLibTag = dependencyTag{name: "pythonLib"}
241 javaDataTag = dependencyTag{name: "javaData"}
Brett Brotherton8c7c65e2025-03-12 20:56:07 -0700242 sharedLibTag = dependencyTag{name: "sharedLib"}
Cole Faust5c503d12023-01-24 11:48:08 -0800243 // The python interpreter, with soong module name "py3-launcher" or "py3-launcher-autorun".
Cole Faust909d2372023-02-13 23:17:40 +0000244 launcherTag = dependencyTag{name: "launcher"}
245 launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"}
Cole Faust5c503d12023-01-24 11:48:08 -0800246 // The python interpreter built for host so that we can precompile python sources.
247 // This only works because the precompiled sources don't vary by architecture.
248 // The soong module name is "py3-launcher".
Cole Faust909d2372023-02-13 23:17:40 +0000249 hostLauncherTag = dependencyTag{name: "hostLauncher"}
250 hostlauncherSharedLibTag = dependencyTag{name: "hostlauncherSharedLib"}
251 hostStdLibTag = dependencyTag{name: "hostStdLib"}
252 pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
253 pyExt = ".py"
254 protoExt = ".proto"
Cole Faust909d2372023-02-13 23:17:40 +0000255 internalPath = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800256)
257
Cole Faust4d247e62023-01-23 10:14:58 -0800258type basePropertiesProvider interface {
259 getBaseProperties() *BaseProperties
260}
261
Liz Kammerd737d022020-11-16 15:42:51 -0800262func anyHasExt(paths []string, ext string) bool {
263 for _, p := range paths {
264 if filepath.Ext(p) == ext {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800265 return true
266 }
267 }
268
269 return false
270}
271
Cole Faust4d247e62023-01-23 10:14:58 -0800272func (p *PythonLibraryModule) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800273 return anyHasExt(p.properties.Srcs, ext)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800274}
275
Liz Kammerd737d022020-11-16 15:42:51 -0800276// DepsMutator mutates dependencies for this module:
Colin Crossd079e0b2022-08-16 10:27:33 -0700277// - handles proto dependencies,
278// - if required, specifies launcher and adds launcher dependencies,
279// - applies python version mutations to Python dependencies
Cole Faust4d247e62023-01-23 10:14:58 -0800280func (p *PythonLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Cole Faustd85c6772025-01-31 10:54:46 -0800281 // Flatten the version.py3 props down into the main property struct. Leftover from when
282 // there was both python2 and 3 in the build, and properties could be different between them.
283 if base, ok := ctx.Module().(basePropertiesProvider); ok {
284 props := base.getBaseProperties()
Colin Crossfe17f6f2019-03-28 19:30:56 -0700285
Cole Faustd85c6772025-01-31 10:54:46 -0800286 err := proptools.AppendMatchingProperties([]interface{}{props}, &props.Version.Py3, nil)
287 if err != nil {
288 panic(err)
289 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800290 }
Colin Crosse20113d2020-11-22 19:37:44 -0800291
Cole Faustd85c6772025-01-31 10:54:46 -0800292 android.ProtoDeps(ctx, &p.protoProperties)
293
Liz Kammerd737d022020-11-16 15:42:51 -0800294 // If sources contain a proto file, add dependency on libprotobuf-python
295 if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
Cole Faustd85c6772025-01-31 10:54:46 -0800296 ctx.AddDependency(ctx.Module(), pythonLibTag, "libprotobuf-python")
Colin Crosse20113d2020-11-22 19:37:44 -0800297 }
Liz Kammerd737d022020-11-16 15:42:51 -0800298
299 // Add python library dependencies for this python version variation
Cole Faustd85c6772025-01-31 10:54:46 -0800300 ctx.AddDependency(ctx.Module(), pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700301
Colin Cross1bc63932020-11-22 20:12:45 -0800302 // Emulate the data property for java_data but with the arch variation overridden to "common"
303 // so that it can point to java modules.
304 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
305 ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
Cole Faust5c503d12023-01-24 11:48:08 -0800306
Brett Brotherton8c7c65e2025-03-12 20:56:07 -0700307 if ctx.Host() {
308 ctx.AddVariationDependencies(ctx.Config().BuildOSTarget.Variations(), sharedLibTag, p.properties.Shared_libs...)
309 } else if len(p.properties.Shared_libs) > 0 {
310 ctx.PropertyErrorf("shared_libs", "shared_libs is not supported for device builds")
311 }
312
Cole Faust909d2372023-02-13 23:17:40 +0000313 p.AddDepsOnPythonLauncherAndStdlib(ctx, hostStdLibTag, hostLauncherTag, hostlauncherSharedLibTag, false, ctx.Config().BuildOSTarget)
Cole Faust5c503d12023-01-24 11:48:08 -0800314}
315
Cole Faust909d2372023-02-13 23:17:40 +0000316// AddDepsOnPythonLauncherAndStdlib will make the current module depend on the python stdlib,
317// launcher (interpreter), and the launcher's shared libraries. If autorun is true, it will use
318// the autorun launcher instead of the regular one. This function acceps a targetForDeps argument
319// as the target to use for these dependencies. For embedded launcher python binaries, the launcher
320// that will be embedded will be under the same target as the python module itself. But when
321// precompiling python code, we need to get the python launcher built for host, even if we're
322// compiling the python module for device, so we pass a different target to this function.
Cole Faust5c503d12023-01-24 11:48:08 -0800323func (p *PythonLibraryModule) AddDepsOnPythonLauncherAndStdlib(ctx android.BottomUpMutatorContext,
Cole Faust909d2372023-02-13 23:17:40 +0000324 stdLibTag, launcherTag, launcherSharedLibTag blueprint.DependencyTag,
Cole Faust5c503d12023-01-24 11:48:08 -0800325 autorun bool, targetForDeps android.Target) {
326 var stdLib string
327 var launcherModule string
Cole Faust909d2372023-02-13 23:17:40 +0000328 // Add launcher shared lib dependencies. Ideally, these should be
329 // derived from the `shared_libs` property of the launcher. TODO: read these from
330 // the python launcher itself using ctx.OtherModuleProvider() or similar on the result
331 // of ctx.AddFarVariationDependencies()
332 launcherSharedLibDeps := []string{
333 "libsqlite",
334 }
335 // Add launcher-specific dependencies for bionic
336 if targetForDeps.Os.Bionic() {
337 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm")
338 }
339 if targetForDeps.Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() {
340 launcherSharedLibDeps = append(launcherSharedLibDeps, "libc_musl")
341 }
Cole Faust5c503d12023-01-24 11:48:08 -0800342
Cole Faustd85c6772025-01-31 10:54:46 -0800343 var prebuiltStdLib bool
344 if targetForDeps.Os.Bionic() {
345 prebuiltStdLib = false
346 } else if ctx.Config().VendorConfig("cpython3").Bool("force_build_host") {
347 prebuiltStdLib = false
348 } else {
349 prebuiltStdLib = true
Cole Faust5c503d12023-01-24 11:48:08 -0800350 }
Cole Faustd85c6772025-01-31 10:54:46 -0800351
352 if prebuiltStdLib {
353 stdLib = "py3-stdlib-prebuilt"
354 } else {
355 stdLib = "py3-stdlib"
356 }
357
358 launcherModule = "py3-launcher"
359 if autorun {
360 launcherModule = "py3-launcher-autorun"
361 }
362 if ctx.Config().HostStaticBinaries() && targetForDeps.Os == android.LinuxMusl {
363 launcherModule += "-static"
364 }
365 if ctx.Device() {
366 launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog")
367 }
368
Cole Faust5c503d12023-01-24 11:48:08 -0800369 targetVariations := targetForDeps.Variations()
370 if ctx.ModuleName() != stdLib {
Cole Faust5c503d12023-01-24 11:48:08 -0800371 // Using AddFarVariationDependencies for all of these because they can be for a different
372 // platform, like if the python module itself was being compiled for device, we may want
373 // the python interpreter built for host so that we can precompile python sources.
Cole Faustd85c6772025-01-31 10:54:46 -0800374 ctx.AddFarVariationDependencies(targetVariations, stdLibTag, stdLib)
Cole Faust5c503d12023-01-24 11:48:08 -0800375 }
376 ctx.AddFarVariationDependencies(targetVariations, launcherTag, launcherModule)
Cole Faust909d2372023-02-13 23:17:40 +0000377 ctx.AddFarVariationDependencies(targetVariations, launcherSharedLibTag, launcherSharedLibDeps...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800378}
379
Cole Faust4d247e62023-01-23 10:14:58 -0800380// GenerateAndroidBuildActions performs build actions common to all Python modules
381func (p *PythonLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd85c6772025-01-31 10:54:46 -0800382 if proptools.BoolDefault(p.properties.Version.Py2.Enabled, false) {
383 ctx.PropertyErrorf("version.py2.enabled", "Python 2 is no longer supported, please convert to python 3.")
384 }
Liz Kammerd737d022020-11-16 15:42:51 -0800385 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs)
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -0700386 // Keep before any early returns.
387 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
388 TestOnly: Bool(p.sourceProperties.Test_only),
389 TopLevelTarget: p.sourceProperties.Top_level_test_target,
390 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800391
392 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800393 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700394 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, p.properties.Device_common_data)...)
Inseob Kim25f5ae42025-01-07 22:27:58 +0900395 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, p.properties.Device_first_data)...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800396
Colin Cross1bc63932020-11-22 20:12:45 -0800397 // Emulate the data property for java_data dependencies.
Yu Liue98f7062025-01-17 22:52:43 +0000398 for _, javaData := range ctx.GetDirectDepsProxyWithTag(javaDataTag) {
Colin Cross1bc63932020-11-22 20:12:45 -0800399 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
400 }
401
Brett Brotherton8c7c65e2025-03-12 20:56:07 -0700402 var directImplementationDeps android.Paths
403 var transitiveImplementationDeps []depset.DepSet[android.Path]
404 ctx.VisitDirectDepsProxyWithTag(sharedLibTag, func(dep android.ModuleProxy) {
405 sharedLibInfo, _ := android.OtherModuleProvider(ctx, dep, cc.SharedLibraryInfoProvider)
406 if sharedLibInfo.SharedLibrary != nil {
407 expandedData = append(expandedData, android.OutputFilesForModule(ctx, dep, "")...)
408 directImplementationDeps = append(directImplementationDeps, android.OutputFilesForModule(ctx, dep, "")...)
409 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
410 transitiveImplementationDeps = append(transitiveImplementationDeps, info.ImplementationDeps)
411 p.bundleSharedLibs = append(p.bundleSharedLibs, info.ImplementationDeps.ToList()...)
412 }
413 } else {
414 ctx.PropertyErrorf("shared_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
415 }
416 })
417 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
418 ImplementationDeps: depset.New(depset.PREORDER, directImplementationDeps, transitiveImplementationDeps),
419 })
420
Liz Kammerd737d022020-11-16 15:42:51 -0800421 // Validate pkg_path property
Nan Zhang1db85402017-12-18 13:20:23 -0800422 pkgPath := String(p.properties.Pkg_path)
423 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800424 // TODO: export validation from android/paths.go handling to replace this duplicated functionality
Nan Zhang1db85402017-12-18 13:20:23 -0800425 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
426 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
427 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700428 ctx.PropertyErrorf("pkg_path",
429 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800430 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700431 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800432 }
Liz Kammerd737d022020-11-16 15:42:51 -0800433 }
434 // If property Is_internal is set, prepend pkgPath with internalPath
435 if proptools.BoolDefault(p.properties.Is_internal, false) {
436 pkgPath = filepath.Join(internalPath, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800437 }
438
Liz Kammerd737d022020-11-16 15:42:51 -0800439 // generate src:destination path mappings for this module
Nan Zhang1db85402017-12-18 13:20:23 -0800440 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800441
Liz Kammerd737d022020-11-16 15:42:51 -0800442 // generate the zipfile of all source and data files
Nan Zhang1db85402017-12-18 13:20:23 -0800443 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Dan Willemsen7b90bcd2025-02-11 13:11:23 -0500444 p.precompiledSrcsZip = p.precompileSrcs(ctx)
Yu Liue98f7062025-01-17 22:52:43 +0000445
446 android.SetProvider(ctx, PythonLibraryInfoProvider, PythonLibraryInfo{
447 SrcsPathMappings: p.getSrcsPathMappings(),
448 DataPathMappings: p.getDataPathMappings(),
449 SrcsZip: p.getSrcsZip(),
450 PkgPath: p.getPkgPath(),
451 PrecompiledSrcsZip: p.getPrecompiledSrcsZip(),
Brett Brotherton8c7c65e2025-03-12 20:56:07 -0700452 BundleSharedLibs: p.getBundleSharedLibs(),
Yu Liue98f7062025-01-17 22:52:43 +0000453 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800454}
455
Liz Kammerd737d022020-11-16 15:42:51 -0800456func isValidPythonPath(path string) error {
457 identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/")
458 for _, token := range identifiers {
459 if !pathComponentRegexp.MatchString(token) {
460 return fmt.Errorf("the path %q contains invalid subpath %q. "+
461 "Subpaths must be at least one character long. "+
462 "The first character must an underscore or letter. "+
463 "Following characters may be any of: letter, digit, underscore, hyphen.",
464 path, token)
465 }
466 }
467 return nil
468}
469
470// For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path>
471// for python/data files expanded from properties.
Cole Faust4d247e62023-01-23 10:14:58 -0800472func (p *PythonLibraryModule) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800473 expandedSrcs, expandedData android.Paths) {
474 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800475 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800476 destToPySrcs := make(map[string]string)
477 destToPyData := make(map[string]string)
478
Dan Willemsen339a63f2023-08-15 22:17:03 -0400479 // Disable path checks for the stdlib, as it includes a "." in the version string
480 isInternal := proptools.BoolDefault(p.properties.Is_internal, false)
481
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800482 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800483 if s.Ext() != pyExt && s.Ext() != protoExt {
484 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800485 continue
486 }
Nan Zhang1db85402017-12-18 13:20:23 -0800487 runfilesPath := filepath.Join(pkgPath, s.Rel())
Dan Willemsen339a63f2023-08-15 22:17:03 -0400488 if !isInternal {
489 if err := isValidPythonPath(runfilesPath); err != nil {
490 ctx.PropertyErrorf("srcs", err.Error())
491 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800492 }
Liz Kammerd737d022020-11-16 15:42:51 -0800493 if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
494 p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s})
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800495 }
496 }
497
498 for _, d := range expandedData {
Raphael Blistein59858462024-05-08 16:15:53 +0000499 if d.Ext() == pyExt {
500 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800501 continue
502 }
Nan Zhang1db85402017-12-18 13:20:23 -0800503 runfilesPath := filepath.Join(pkgPath, d.Rel())
Liz Kammerd737d022020-11-16 15:42:51 -0800504 if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800505 p.dataPathMappings = append(p.dataPathMappings,
506 pathMapping{dest: runfilesPath, src: d})
507 }
508 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800509}
510
Liz Kammerd737d022020-11-16 15:42:51 -0800511// createSrcsZip registers build actions to zip current module's sources and data.
Cole Faust4d247e62023-01-23 10:14:58 -0800512func (p *PythonLibraryModule) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800513 relativeRootMap := make(map[string]android.Paths)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800514 var protoSrcs android.Paths
Cole Faust5c503d12023-01-24 11:48:08 -0800515 addPathMapping := func(path pathMapping) {
Raphael Blistein59858462024-05-08 16:15:53 +0000516 relativeRoot := strings.TrimSuffix(path.src.String(), path.src.Rel())
517 relativeRootMap[relativeRoot] = append(relativeRootMap[relativeRoot], path.src)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800518 }
Cole Faust5c503d12023-01-24 11:48:08 -0800519
520 // "srcs" or "data" properties may contain filegroups so it might happen that
521 // the root directory for each source path is different.
522 for _, path := range p.srcsPathMappings {
Raphael Blistein59858462024-05-08 16:15:53 +0000523 // handle proto sources separately
524 if path.src.Ext() == protoExt {
525 protoSrcs = append(protoSrcs, path.src)
526 } else {
527 addPathMapping(path)
528 }
Cole Faust5c503d12023-01-24 11:48:08 -0800529 }
530 for _, path := range p.dataPathMappings {
531 addPathMapping(path)
532 }
533
Nan Zhangb8fa1972017-12-22 16:12:00 -0800534 var zips android.Paths
535 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700536 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
537 protoFlags.OutTypeFlag = "--python_out"
538
Cole Faustcaf766b2022-10-21 16:07:56 -0700539 if pkgPath != "" {
Cole Faust43ac21f2022-09-19 11:19:52 -0700540 pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path")
541 rule := android.NewRuleBuilder(pctx, ctx)
542 var stagedProtoSrcs android.Paths
543 for _, srcFile := range protoSrcs {
544 stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel())
Cole Faust43ac21f2022-09-19 11:19:52 -0700545 rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc)
546 stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc)
547 }
548 rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path")
549 protoSrcs = stagedProtoSrcs
Cole Faust43ac21f2022-09-19 11:19:52 -0700550 }
551
Nan Zhangb8fa1972017-12-22 16:12:00 -0800552 for _, srcFile := range protoSrcs {
Cole Faustcaf766b2022-10-21 16:07:56 -0700553 zip := genProto(ctx, srcFile, protoFlags)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800554 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800555 }
556 }
557
Nan Zhangb8fa1972017-12-22 16:12:00 -0800558 if len(relativeRootMap) > 0 {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800559 // in order to keep stable order of soong_zip params, we sort the keys here.
Cole Faust18994c72023-02-28 16:02:16 -0800560 roots := android.SortedKeys(relativeRootMap)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800561
Cole Faust01243362022-06-02 12:11:12 -0700562 // Use -symlinks=false so that the symlinks in the bazel output directory are followed
563 parArgs := []string{"-symlinks=false"}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700564 if pkgPath != "" {
Liz Kammerd737d022020-11-16 15:42:51 -0800565 // use package path as path prefix
Nan Zhangf0c4e432018-05-22 14:50:18 -0700566 parArgs = append(parArgs, `-P `+pkgPath)
567 }
Liz Kammerd737d022020-11-16 15:42:51 -0800568 paths := android.Paths{}
569 for _, root := range roots {
570 // specify relative root of file in following -f arguments
571 parArgs = append(parArgs, `-C `+root)
572 for _, path := range relativeRootMap[root] {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800573 parArgs = append(parArgs, `-f `+path.String())
Liz Kammerd737d022020-11-16 15:42:51 -0800574 paths = append(paths, path)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800575 }
576 }
577
578 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
579 ctx.Build(pctx, android.BuildParams{
580 Rule: zip,
581 Description: "python library archive",
582 Output: origSrcsZip,
Liz Kammerd737d022020-11-16 15:42:51 -0800583 // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits
584 Implicits: paths,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800585 Args: map[string]string{
586 "args": strings.Join(parArgs, " "),
587 },
588 })
589 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800590 }
Liz Kammerd737d022020-11-16 15:42:51 -0800591 // we may have multiple zips due to separate handling of proto source files
Nan Zhangb8fa1972017-12-22 16:12:00 -0800592 if len(zips) == 1 {
593 return zips[0]
594 } else {
595 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
596 ctx.Build(pctx, android.BuildParams{
597 Rule: combineZip,
598 Description: "combine python library archive",
599 Output: combinedSrcsZip,
600 Inputs: zips,
601 })
602 return combinedSrcsZip
603 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800604}
605
Cole Faust5c503d12023-01-24 11:48:08 -0800606func (p *PythonLibraryModule) precompileSrcs(ctx android.ModuleContext) android.Path {
607 // To precompile the python sources, we need a python interpreter and stdlib built
608 // for host. We then use those to compile the python sources, which may be used on either
609 // host of device. Python bytecode is architecture agnostic, so we're essentially
610 // "cross compiling" for device here purely by virtue of host and device python bytecode
611 // being the same.
612 var stdLib android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400613 var stdLibPkg string
Cole Faust5c503d12023-01-24 11:48:08 -0800614 var launcher android.Path
Dan Willemsen339a63f2023-08-15 22:17:03 -0400615 if proptools.BoolDefault(p.properties.Is_internal, false) {
Cole Faust5c503d12023-01-24 11:48:08 -0800616 stdLib = p.srcsZip
Dan Willemsen339a63f2023-08-15 22:17:03 -0400617 stdLibPkg = p.getPkgPath()
Cole Faust5c503d12023-01-24 11:48:08 -0800618 } else {
Yu Liue98f7062025-01-17 22:52:43 +0000619 ctx.VisitDirectDepsProxyWithTag(hostStdLibTag, func(module android.ModuleProxy) {
620 if dep, ok := android.OtherModuleProvider(ctx, module, PythonLibraryInfoProvider); ok {
621 stdLib = dep.PrecompiledSrcsZip
622 stdLibPkg = dep.PkgPath
Cole Faust5c503d12023-01-24 11:48:08 -0800623 }
624 })
625 }
Yu Liue98f7062025-01-17 22:52:43 +0000626 ctx.VisitDirectDepsProxyWithTag(hostLauncherTag, func(module android.ModuleProxy) {
627 if dep, ok := android.OtherModuleProvider(ctx, module, cc.LinkableInfoProvider); ok {
628 optionalLauncher := dep.OutputFile
Cole Faust5c503d12023-01-24 11:48:08 -0800629 if optionalLauncher.Valid() {
630 launcher = optionalLauncher.Path()
631 }
Cole Faust909d2372023-02-13 23:17:40 +0000632 }
633 })
634 var launcherSharedLibs android.Paths
635 var ldLibraryPath []string
Yu Liue98f7062025-01-17 22:52:43 +0000636 ctx.VisitDirectDepsProxyWithTag(hostlauncherSharedLibTag, func(module android.ModuleProxy) {
637 if dep, ok := android.OtherModuleProvider(ctx, module, cc.LinkableInfoProvider); ok {
638 optionalPath := dep.OutputFile
Cole Faust909d2372023-02-13 23:17:40 +0000639 if optionalPath.Valid() {
640 launcherSharedLibs = append(launcherSharedLibs, optionalPath.Path())
641 ldLibraryPath = append(ldLibraryPath, filepath.Dir(optionalPath.Path().String()))
Cole Faust5c503d12023-01-24 11:48:08 -0800642 }
643 }
644 })
645
646 out := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszipprecompiled")
647 if stdLib == nil || launcher == nil {
648 // This shouldn't happen in a real build because we'll error out when adding dependencies
649 // on the stdlib and launcher if they don't exist. But some tests set
650 // AllowMissingDependencies.
651 return out
652 }
653 ctx.Build(pctx, android.BuildParams{
654 Rule: precompile,
655 Input: p.srcsZip,
656 Output: out,
657 Implicits: launcherSharedLibs,
658 Description: "Precompile the python sources of " + ctx.ModuleName(),
659 Args: map[string]string{
660 "stdlibZip": stdLib.String(),
Dan Willemsen339a63f2023-08-15 22:17:03 -0400661 "stdlibPkg": stdLibPkg,
Cole Faust5c503d12023-01-24 11:48:08 -0800662 "launcher": launcher.String(),
663 "ldLibraryPath": strings.Join(ldLibraryPath, ":"),
664 },
665 })
666 return out
667}
668
Liz Kammerd737d022020-11-16 15:42:51 -0800669// collectPathsFromTransitiveDeps checks for source/data files for duplicate paths
670// for module and its transitive dependencies and collects list of data/source file
671// zips for transitive dependencies.
Cole Faust5c503d12023-01-24 11:48:08 -0800672func (p *PythonLibraryModule) collectPathsFromTransitiveDeps(ctx android.ModuleContext, precompiled bool) android.Paths {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800673 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
674 // check duplicates.
675 destToPySrcs := make(map[string]string)
676 destToPyData := make(map[string]string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800677 for _, path := range p.srcsPathMappings {
678 destToPySrcs[path.dest] = path.src.String()
679 }
680 for _, path := range p.dataPathMappings {
681 destToPyData[path.dest] = path.src.String()
682 }
683
Colin Cross6b753602018-06-21 13:03:07 -0700684 seen := make(map[android.Module]bool)
685
Cole Faust4d247e62023-01-23 10:14:58 -0800686 var result android.Paths
687
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800688 // visit all its dependencies in depth first.
Yu Liue98f7062025-01-17 22:52:43 +0000689 ctx.WalkDepsProxy(func(child, _ android.ModuleProxy) bool {
Liz Kammerd737d022020-11-16 15:42:51 -0800690 // we only collect dependencies tagged as python library deps
Colin Cross6b753602018-06-21 13:03:07 -0700691 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
692 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800693 }
Colin Cross6b753602018-06-21 13:03:07 -0700694 if seen[child] {
695 return false
696 }
697 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800698 // Python modules only can depend on Python libraries.
Yu Liue98f7062025-01-17 22:52:43 +0000699 dep, isLibrary := android.OtherModuleProvider(ctx, child, PythonLibraryInfoProvider)
700 _, isBinary := android.OtherModuleProvider(ctx, child, PythonBinaryInfoProvider)
701 if !isLibrary || isBinary {
Liz Kammerd737d022020-11-16 15:42:51 -0800702 ctx.PropertyErrorf("libs",
Nan Zhangd4e641b2017-07-12 12:55:28 -0700703 "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 +0000704 ctx.OtherModuleName(child), ctx.ModuleName())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700705 }
Liz Kammerd737d022020-11-16 15:42:51 -0800706 // collect source and data paths, checking that there are no duplicate output file conflicts
Yu Liue98f7062025-01-17 22:52:43 +0000707 if isLibrary {
708 srcs := dep.SrcsPathMappings
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800709 for _, path := range srcs {
Liz Kammerd737d022020-11-16 15:42:51 -0800710 checkForDuplicateOutputPath(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700711 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800712 }
Yu Liue98f7062025-01-17 22:52:43 +0000713 data := dep.DataPathMappings
Liz Kammerd737d022020-11-16 15:42:51 -0800714 for _, path := range data {
715 checkForDuplicateOutputPath(ctx, destToPyData,
716 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
717 }
Cole Faust5c503d12023-01-24 11:48:08 -0800718 if precompiled {
Yu Liue98f7062025-01-17 22:52:43 +0000719 result = append(result, dep.PrecompiledSrcsZip)
Cole Faust5c503d12023-01-24 11:48:08 -0800720 } else {
Yu Liue98f7062025-01-17 22:52:43 +0000721 result = append(result, dep.SrcsZip)
Cole Faust5c503d12023-01-24 11:48:08 -0800722 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800723 }
Colin Cross6b753602018-06-21 13:03:07 -0700724 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800725 })
Cole Faust4d247e62023-01-23 10:14:58 -0800726 return result
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800727}
728
Brett Brotherton8c7c65e2025-03-12 20:56:07 -0700729func (p *PythonLibraryModule) collectSharedLibDeps(ctx android.ModuleContext) android.Paths {
730 seen := make(map[android.Module]bool)
731
732 var result android.Paths
733
734 ctx.WalkDepsProxy(func(child, _ android.ModuleProxy) bool {
735 // we only collect dependencies tagged as python library deps
736 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
737 return false
738 }
739 if seen[child] {
740 return false
741 }
742 seen[child] = true
743 dep, isLibrary := android.OtherModuleProvider(ctx, child, PythonLibraryInfoProvider)
744 if isLibrary {
745 result = append(result, dep.BundleSharedLibs...)
746 }
747 return true
748 })
749 return result
750}
751
752func (p *PythonLibraryModule) zipSharedLibs(ctx android.ModuleContext, bundleSharedLibs android.Paths) android.Path {
753 // sort the paths to keep the output deterministic
754 sort.Slice(bundleSharedLibs, func(i, j int) bool {
755 return bundleSharedLibs[i].String() < bundleSharedLibs[j].String()
756 })
757
758 parArgs := []string{"-symlinks=false", "-P lib64"}
759 paths := android.Paths{}
760 for _, path := range bundleSharedLibs {
761 // specify relative root of file in following -f arguments
762 parArgs = append(parArgs, `-C `+filepath.Dir(path.String()))
763 parArgs = append(parArgs, `-f `+path.String())
764 paths = append(paths, path)
765 }
766 srcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".sharedlibs.srcszip")
767 ctx.Build(pctx, android.BuildParams{
768 Rule: zip,
769 Description: "bundle shared libraries for python binary",
770 Output: srcsZip,
Spandan Das8ed07762025-03-14 21:52:09 +0000771 Implicits: paths,
Brett Brotherton8c7c65e2025-03-12 20:56:07 -0700772 Args: map[string]string{
773 "args": strings.Join(parArgs, " "),
774 },
775 })
776 return srcsZip
777}
778
Liz Kammerd737d022020-11-16 15:42:51 -0800779// chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which
780// would result in two files being placed in the same location.
781// If there is a duplicate path, an error is thrown and true is returned
782// Otherwise, outputPath: srcPath is added to m and returns false
783func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool {
784 if oldSrcPath, found := m[outputPath]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700785 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800786 " First file: in module %s at path %q."+
787 " Second file: in module %s at path %q.",
Liz Kammerd737d022020-11-16 15:42:51 -0800788 outputPath, curModule, oldSrcPath, otherModule, srcPath)
789 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800790 }
Liz Kammerd737d022020-11-16 15:42:51 -0800791 m[outputPath] = srcPath
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800792
Liz Kammerd737d022020-11-16 15:42:51 -0800793 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800794}
Nan Zhangea568a42017-11-08 21:20:04 -0800795
Liz Kammerd737d022020-11-16 15:42:51 -0800796// InstallInData returns true as Python is not supported in the system partition
Cole Faust4d247e62023-01-23 10:14:58 -0800797func (p *PythonLibraryModule) InstallInData() bool {
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000798 return true
799}
800
Nan Zhangea568a42017-11-08 21:20:04 -0800801var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800802var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800803var String = proptools.String