Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package python |
| 16 | |
| 17 | // This file contains the "Base" module type for building Python program. |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "path/filepath" |
| 22 | "regexp" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 23 | "strings" |
| 24 | |
| 25 | "github.com/google/blueprint" |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 26 | "github.com/google/blueprint/proptools" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 27 | |
| 28 | "android/soong/android" |
| 29 | ) |
| 30 | |
| 31 | func init() { |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 32 | registerPythonMutators(android.InitRegistrationContext) |
| 33 | } |
| 34 | |
| 35 | func registerPythonMutators(ctx android.RegistrationContext) { |
| 36 | ctx.PreDepsMutators(RegisterPythonPreDepsMutators) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 39 | // Exported to support other packages using Python modules in tests. |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 40 | func RegisterPythonPreDepsMutators(ctx android.RegisterMutatorsContext) { |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 41 | ctx.BottomUp("python_version", versionSplitMutator()).Parallel() |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 44 | // the version-specific properties that apply to python modules. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 45 | type VersionProperties struct { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 46 | // whether the module is required to be built with this version. |
| 47 | // Defaults to true for Python 3, and false otherwise. |
Liz Kammer | 59c0eae | 2021-09-17 17:48:05 -0400 | [diff] [blame] | 48 | Enabled *bool |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 49 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 50 | // 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 Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 53 | Srcs []string `android:"path,arch_variant"` |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 54 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 55 | // 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 Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 57 | Exclude_srcs []string `android:"path,arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 58 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 59 | // list of the Python libraries used only for this Python version. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 60 | Libs []string `android:"arch_variant"` |
| 61 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 62 | // whether the binary is required to be built with embedded launcher for this version, defaults to false. |
Liz Kammer | 59c0eae | 2021-09-17 17:48:05 -0400 | [diff] [blame] | 63 | Embedded_launcher *bool // TODO(b/174041232): Remove this property |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 66 | // properties that apply to all python modules |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 67 | type BaseProperties struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 68 | // 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 Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 72 | // if left unspecified, all the source/data files path is unchanged within zip file. |
Liz Kammer | 59c0eae | 2021-09-17 17:48:05 -0400 | [diff] [blame] | 73 | Pkg_path *string |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 74 | |
| 75 | // true, if the Python module is used internally, eg, Python std libs. |
Liz Kammer | 59c0eae | 2021-09-17 17:48:05 -0400 | [diff] [blame] | 76 | Is_internal *bool |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 77 | |
| 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 Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 83 | Srcs []string `android:"path,arch_variant"` |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 84 | |
| 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 Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 87 | Exclude_srcs []string `android:"path,arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 88 | |
| 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 Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 91 | Data []string `android:"path,arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 92 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 93 | // list of java modules that provide data that should be installed alongside the test. |
| 94 | Java_data []string |
| 95 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 96 | // list of the Python libraries compatible both with Python2 and Python3. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 97 | Libs []string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 98 | |
| 99 | Version struct { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 100 | // Python2-specific properties, including whether Python2 is supported for this module |
| 101 | // and version-specific sources, exclusions and dependencies. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 102 | Py2 VersionProperties `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 103 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 104 | // Python3-specific properties, including whether Python3 is supported for this module |
| 105 | // and version-specific sources, exclusions and dependencies. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 106 | Py3 VersionProperties `android:"arch_variant"` |
| 107 | } `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 108 | |
| 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 Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 112 | Actual_version string `blueprint:"mutated"` |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 113 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 114 | // 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 Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 116 | Enabled *bool `blueprint:"mutated"` |
| 117 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 118 | // 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 Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 120 | Embedded_launcher *bool `blueprint:"mutated"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 123 | // Used to store files of current module after expanding dependencies |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 124 | type pathMapping struct { |
| 125 | dest string |
| 126 | src android.Path |
| 127 | } |
| 128 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 129 | type PythonLibraryModule struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 130 | android.ModuleBase |
Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 131 | android.DefaultableModuleBase |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 132 | android.BazelModuleBase |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 133 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 134 | properties BaseProperties |
| 135 | protoProperties android.ProtoProperties |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 136 | |
| 137 | // initialize before calling Init |
| 138 | hod android.HostOrDeviceSupported |
| 139 | multilib android.Multilib |
| 140 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 141 | // the Python files of current module after expanding source dependencies. |
| 142 | // pathMapping: <dest: runfile_path, src: source_path> |
| 143 | srcsPathMappings []pathMapping |
| 144 | |
| 145 | // the data files of current module after expanding source dependencies. |
| 146 | // pathMapping: <dest: runfile_path, src: source_path> |
| 147 | dataPathMappings []pathMapping |
| 148 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 149 | // the zip filepath for zipping current module source/data files. |
| 150 | srcsZip android.Path |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 153 | // newModule generates new Python base module |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 154 | func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *PythonLibraryModule { |
| 155 | return &PythonLibraryModule{ |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 156 | hod: hod, |
| 157 | multilib: multilib, |
| 158 | } |
| 159 | } |
| 160 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 161 | // interface implemented by Python modules to provide source and data mappings and zip to python |
| 162 | // modules that depend on it |
| 163 | type pythonDependency interface { |
| 164 | getSrcsPathMappings() []pathMapping |
| 165 | getDataPathMappings() []pathMapping |
| 166 | getSrcsZip() android.Path |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 169 | // getSrcsPathMappings gets this module's path mapping of src source path : runfiles destination |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 170 | func (p *PythonLibraryModule) getSrcsPathMappings() []pathMapping { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 171 | return p.srcsPathMappings |
| 172 | } |
| 173 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 174 | // getSrcsPathMappings gets this module's path mapping of data source path : runfiles destination |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 175 | func (p *PythonLibraryModule) getDataPathMappings() []pathMapping { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 176 | return p.dataPathMappings |
| 177 | } |
| 178 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 179 | // getSrcsZip returns the filepath where the current module's source/data files are zipped. |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 180 | func (p *PythonLibraryModule) getSrcsZip() android.Path { |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 181 | return p.srcsZip |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 184 | func (p *PythonLibraryModule) getBaseProperties() *BaseProperties { |
| 185 | return &p.properties |
| 186 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 187 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 188 | var _ pythonDependency = (*PythonLibraryModule)(nil) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 189 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 190 | func (p *PythonLibraryModule) init() android.Module { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 191 | p.AddProperties(&p.properties, &p.protoProperties) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 192 | android.InitAndroidArchModule(p, p.hod, p.multilib) |
Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 193 | android.InitDefaultableModule(p) |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 194 | android.InitBazelModule(p) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 195 | return p |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 198 | // Python-specific tag to transfer information on the purpose of a dependency. |
| 199 | // This is used when adding a dependency on a module, which can later be accessed when visiting |
| 200 | // dependencies. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 201 | type dependencyTag struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 202 | blueprint.BaseDependencyTag |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 203 | name string |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 206 | // Python-specific tag that indicates that installed files of this module should depend on installed |
| 207 | // files of the dependency |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 208 | type installDependencyTag struct { |
| 209 | blueprint.BaseDependencyTag |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 210 | // embedding this struct provides the installation dependency requirement |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 211 | android.InstallAlwaysNeededDependencyTag |
| 212 | name string |
| 213 | } |
| 214 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 215 | var ( |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 216 | pythonLibTag = dependencyTag{name: "pythonLib"} |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 217 | javaDataTag = dependencyTag{name: "javaData"} |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 218 | launcherTag = dependencyTag{name: "launcher"} |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 219 | launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"} |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 220 | pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`) |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 221 | pyExt = ".py" |
| 222 | protoExt = ".proto" |
| 223 | pyVersion2 = "PY2" |
| 224 | pyVersion3 = "PY3" |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 225 | internalPath = "internal" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 226 | ) |
| 227 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 228 | type basePropertiesProvider interface { |
| 229 | getBaseProperties() *BaseProperties |
| 230 | } |
| 231 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 232 | // versionSplitMutator creates version variants for modules and appends the version-specific |
| 233 | // properties for a given variant to the properties in the variant module |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 234 | func versionSplitMutator() func(android.BottomUpMutatorContext) { |
| 235 | return func(mctx android.BottomUpMutatorContext) { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 236 | if base, ok := mctx.Module().(basePropertiesProvider); ok { |
| 237 | props := base.getBaseProperties() |
| 238 | var versionNames []string |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 239 | // collect version specific properties, so that we can merge version-specific properties |
| 240 | // into the module's overall properties |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 241 | var versionProps []VersionProperties |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 242 | // PY3 is first so that we alias the PY3 variant rather than PY2 if both |
| 243 | // are available |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 244 | if proptools.BoolDefault(props.Version.Py3.Enabled, true) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 245 | versionNames = append(versionNames, pyVersion3) |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 246 | versionProps = append(versionProps, props.Version.Py3) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 247 | } |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 248 | if proptools.BoolDefault(props.Version.Py2.Enabled, false) { |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 249 | versionNames = append(versionNames, pyVersion2) |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 250 | versionProps = append(versionProps, props.Version.Py2) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 251 | } |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 252 | modules := mctx.CreateLocalVariations(versionNames...) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 253 | // Alias module to the first variant |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 254 | if len(versionNames) > 0 { |
| 255 | mctx.AliasVariation(versionNames[0]) |
| 256 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 257 | for i, v := range versionNames { |
| 258 | // set the actual version for Python module. |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 259 | newProps := modules[i].(basePropertiesProvider).getBaseProperties() |
| 260 | newProps.Actual_version = v |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 261 | // append versioned properties for the Python module to the overall properties |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 262 | err := proptools.AppendMatchingProperties([]interface{}{newProps}, &versionProps[i], nil) |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 263 | if err != nil { |
| 264 | panic(err) |
| 265 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 271 | func anyHasExt(paths []string, ext string) bool { |
| 272 | for _, p := range paths { |
| 273 | if filepath.Ext(p) == ext { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 274 | return true |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return false |
| 279 | } |
| 280 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 281 | func (p *PythonLibraryModule) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 282 | return anyHasExt(p.properties.Srcs, ext) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 285 | // DepsMutator mutates dependencies for this module: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 286 | // - handles proto dependencies, |
| 287 | // - if required, specifies launcher and adds launcher dependencies, |
| 288 | // - applies python version mutations to Python dependencies |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 289 | func (p *PythonLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 290 | android.ProtoDeps(ctx, &p.protoProperties) |
| 291 | |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 292 | versionVariation := []blueprint.Variation{ |
| 293 | {"python_version", p.properties.Actual_version}, |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 294 | } |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 295 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 296 | // If sources contain a proto file, add dependency on libprotobuf-python |
| 297 | if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" { |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 298 | ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python") |
| 299 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 300 | |
| 301 | // Add python library dependencies for this python version variation |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 302 | ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...) |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 303 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 304 | // Emulate the data property for java_data but with the arch variation overridden to "common" |
| 305 | // so that it can point to java modules. |
| 306 | javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}} |
| 307 | ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 310 | // GenerateAndroidBuildActions performs build actions common to all Python modules |
| 311 | func (p *PythonLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 312 | expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 313 | |
| 314 | // expand data files from "data" property. |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 315 | expandedData := android.PathsForModuleSrc(ctx, p.properties.Data) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 316 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 317 | // Emulate the data property for java_data dependencies. |
| 318 | for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) { |
| 319 | expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...) |
| 320 | } |
| 321 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 322 | // Validate pkg_path property |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 323 | pkgPath := String(p.properties.Pkg_path) |
| 324 | if pkgPath != "" { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 325 | // TODO: export validation from android/paths.go handling to replace this duplicated functionality |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 326 | pkgPath = filepath.Clean(String(p.properties.Pkg_path)) |
| 327 | if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") || |
| 328 | strings.HasPrefix(pkgPath, "/") { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 329 | ctx.PropertyErrorf("pkg_path", |
| 330 | "%q must be a relative path contained in par file.", |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 331 | String(p.properties.Pkg_path)) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 332 | return |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 333 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 334 | } |
| 335 | // If property Is_internal is set, prepend pkgPath with internalPath |
| 336 | if proptools.BoolDefault(p.properties.Is_internal, false) { |
| 337 | pkgPath = filepath.Join(internalPath, pkgPath) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 340 | // generate src:destination path mappings for this module |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 341 | p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 342 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 343 | // generate the zipfile of all source and data files |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 344 | p.srcsZip = p.createSrcsZip(ctx, pkgPath) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 347 | func isValidPythonPath(path string) error { |
| 348 | identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/") |
| 349 | for _, token := range identifiers { |
| 350 | if !pathComponentRegexp.MatchString(token) { |
| 351 | return fmt.Errorf("the path %q contains invalid subpath %q. "+ |
| 352 | "Subpaths must be at least one character long. "+ |
| 353 | "The first character must an underscore or letter. "+ |
| 354 | "Following characters may be any of: letter, digit, underscore, hyphen.", |
| 355 | path, token) |
| 356 | } |
| 357 | } |
| 358 | return nil |
| 359 | } |
| 360 | |
| 361 | // For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path> |
| 362 | // for python/data files expanded from properties. |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 363 | func (p *PythonLibraryModule) genModulePathMappings(ctx android.ModuleContext, pkgPath string, |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 364 | expandedSrcs, expandedData android.Paths) { |
| 365 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 366 | // check current module duplicates. |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 367 | destToPySrcs := make(map[string]string) |
| 368 | destToPyData := make(map[string]string) |
| 369 | |
| 370 | for _, s := range expandedSrcs { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 371 | if s.Ext() != pyExt && s.Ext() != protoExt { |
| 372 | ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 373 | continue |
| 374 | } |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 375 | runfilesPath := filepath.Join(pkgPath, s.Rel()) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 376 | if err := isValidPythonPath(runfilesPath); err != nil { |
| 377 | ctx.PropertyErrorf("srcs", err.Error()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 378 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 379 | if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) { |
| 380 | p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s}) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | for _, d := range expandedData { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 385 | if d.Ext() == pyExt || d.Ext() == protoExt { |
| 386 | ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 387 | continue |
| 388 | } |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 389 | runfilesPath := filepath.Join(pkgPath, d.Rel()) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 390 | if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 391 | p.dataPathMappings = append(p.dataPathMappings, |
| 392 | pathMapping{dest: runfilesPath, src: d}) |
| 393 | } |
| 394 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 397 | // createSrcsZip registers build actions to zip current module's sources and data. |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 398 | func (p *PythonLibraryModule) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 399 | relativeRootMap := make(map[string]android.Paths) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 400 | pathMappings := append(p.srcsPathMappings, p.dataPathMappings...) |
| 401 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 402 | var protoSrcs android.Paths |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 403 | // "srcs" or "data" properties may contain filegroup so it might happen that |
| 404 | // the root directory for each source path is different. |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 405 | for _, path := range pathMappings { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 406 | // handle proto sources separately |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 407 | if path.src.Ext() == protoExt { |
| 408 | protoSrcs = append(protoSrcs, path.src) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 409 | } else { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 410 | relativeRoot := strings.TrimSuffix(path.src.String(), path.src.Rel()) |
| 411 | relativeRootMap[relativeRoot] = append(relativeRootMap[relativeRoot], path.src) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | var zips android.Paths |
| 415 | if len(protoSrcs) > 0 { |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 416 | protoFlags := android.GetProtoFlags(ctx, &p.protoProperties) |
| 417 | protoFlags.OutTypeFlag = "--python_out" |
| 418 | |
Cole Faust | caf766b | 2022-10-21 16:07:56 -0700 | [diff] [blame] | 419 | if pkgPath != "" { |
Cole Faust | 43ac21f | 2022-09-19 11:19:52 -0700 | [diff] [blame] | 420 | pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path") |
| 421 | rule := android.NewRuleBuilder(pctx, ctx) |
| 422 | var stagedProtoSrcs android.Paths |
| 423 | for _, srcFile := range protoSrcs { |
| 424 | stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel()) |
| 425 | rule.Command().Text("mkdir -p").Flag(filepath.Base(stagedProtoSrc.String())) |
| 426 | rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc) |
| 427 | stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc) |
| 428 | } |
| 429 | rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path") |
| 430 | protoSrcs = stagedProtoSrcs |
Cole Faust | 43ac21f | 2022-09-19 11:19:52 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 433 | for _, srcFile := range protoSrcs { |
Cole Faust | caf766b | 2022-10-21 16:07:56 -0700 | [diff] [blame] | 434 | zip := genProto(ctx, srcFile, protoFlags) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 435 | zips = append(zips, zip) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 439 | if len(relativeRootMap) > 0 { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 440 | // in order to keep stable order of soong_zip params, we sort the keys here. |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 441 | roots := android.SortedStringKeys(relativeRootMap) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 442 | |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame] | 443 | // Use -symlinks=false so that the symlinks in the bazel output directory are followed |
| 444 | parArgs := []string{"-symlinks=false"} |
Nan Zhang | f0c4e43 | 2018-05-22 14:50:18 -0700 | [diff] [blame] | 445 | if pkgPath != "" { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 446 | // use package path as path prefix |
Nan Zhang | f0c4e43 | 2018-05-22 14:50:18 -0700 | [diff] [blame] | 447 | parArgs = append(parArgs, `-P `+pkgPath) |
| 448 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 449 | paths := android.Paths{} |
| 450 | for _, root := range roots { |
| 451 | // specify relative root of file in following -f arguments |
| 452 | parArgs = append(parArgs, `-C `+root) |
| 453 | for _, path := range relativeRootMap[root] { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 454 | parArgs = append(parArgs, `-f `+path.String()) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 455 | paths = append(paths, path) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip") |
| 460 | ctx.Build(pctx, android.BuildParams{ |
| 461 | Rule: zip, |
| 462 | Description: "python library archive", |
| 463 | Output: origSrcsZip, |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 464 | // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits |
| 465 | Implicits: paths, |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 466 | Args: map[string]string{ |
| 467 | "args": strings.Join(parArgs, " "), |
| 468 | }, |
| 469 | }) |
| 470 | zips = append(zips, origSrcsZip) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 471 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 472 | // we may have multiple zips due to separate handling of proto source files |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 473 | if len(zips) == 1 { |
| 474 | return zips[0] |
| 475 | } else { |
| 476 | combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip") |
| 477 | ctx.Build(pctx, android.BuildParams{ |
| 478 | Rule: combineZip, |
| 479 | Description: "combine python library archive", |
| 480 | Output: combinedSrcsZip, |
| 481 | Inputs: zips, |
| 482 | }) |
| 483 | return combinedSrcsZip |
| 484 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 487 | // isPythonLibModule returns whether the given module is a Python library PythonLibraryModule or not |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 488 | func isPythonLibModule(module blueprint.Module) bool { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 489 | if _, ok := module.(*PythonLibraryModule); ok { |
| 490 | if _, ok := module.(*PythonBinaryModule); !ok { |
| 491 | return true |
| 492 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 493 | } |
| 494 | return false |
| 495 | } |
| 496 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 497 | // collectPathsFromTransitiveDeps checks for source/data files for duplicate paths |
| 498 | // for module and its transitive dependencies and collects list of data/source file |
| 499 | // zips for transitive dependencies. |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 500 | func (p *PythonLibraryModule) collectPathsFromTransitiveDeps(ctx android.ModuleContext) android.Paths { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 501 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to |
| 502 | // check duplicates. |
| 503 | destToPySrcs := make(map[string]string) |
| 504 | destToPyData := make(map[string]string) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 505 | for _, path := range p.srcsPathMappings { |
| 506 | destToPySrcs[path.dest] = path.src.String() |
| 507 | } |
| 508 | for _, path := range p.dataPathMappings { |
| 509 | destToPyData[path.dest] = path.src.String() |
| 510 | } |
| 511 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 512 | seen := make(map[android.Module]bool) |
| 513 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 514 | var result android.Paths |
| 515 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 516 | // visit all its dependencies in depth first. |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 517 | ctx.WalkDeps(func(child, parent android.Module) bool { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 518 | // we only collect dependencies tagged as python library deps |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 519 | if ctx.OtherModuleDependencyTag(child) != pythonLibTag { |
| 520 | return false |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 521 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 522 | if seen[child] { |
| 523 | return false |
| 524 | } |
| 525 | seen[child] = true |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 526 | // Python modules only can depend on Python libraries. |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 527 | if !isPythonLibModule(child) { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 528 | ctx.PropertyErrorf("libs", |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 529 | "the dependency %q of module %q is not Python library!", |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | d75507f | 2021-08-20 21:02:43 +0000 | [diff] [blame] | 530 | ctx.OtherModuleName(child), ctx.ModuleName()) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 531 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 532 | // collect source and data paths, checking that there are no duplicate output file conflicts |
| 533 | if dep, ok := child.(pythonDependency); ok { |
| 534 | srcs := dep.getSrcsPathMappings() |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 535 | for _, path := range srcs { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 536 | checkForDuplicateOutputPath(ctx, destToPySrcs, |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 537 | path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 538 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 539 | data := dep.getDataPathMappings() |
| 540 | for _, path := range data { |
| 541 | checkForDuplicateOutputPath(ctx, destToPyData, |
| 542 | path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) |
| 543 | } |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 544 | result = append(result, dep.getSrcsZip()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 545 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 546 | return true |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 547 | }) |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 548 | return result |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 551 | // chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which |
| 552 | // would result in two files being placed in the same location. |
| 553 | // If there is a duplicate path, an error is thrown and true is returned |
| 554 | // Otherwise, outputPath: srcPath is added to m and returns false |
| 555 | func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool { |
| 556 | if oldSrcPath, found := m[outputPath]; found { |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 557 | ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 558 | " First file: in module %s at path %q."+ |
| 559 | " Second file: in module %s at path %q.", |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 560 | outputPath, curModule, oldSrcPath, otherModule, srcPath) |
| 561 | return true |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 562 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 563 | m[outputPath] = srcPath |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 564 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 565 | return false |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 566 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 567 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 568 | // InstallInData returns true as Python is not supported in the system partition |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 569 | func (p *PythonLibraryModule) InstallInData() bool { |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 570 | return true |
| 571 | } |
| 572 | |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 573 | var Bool = proptools.Bool |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 574 | var BoolDefault = proptools.BoolDefault |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 575 | var String = proptools.String |