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() { |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 32 | android.PreDepsMutators(RegisterPythonPreDepsMutators) |
| 33 | } |
| 34 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 35 | // Exported to support other packages using Python modules in tests. |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 36 | func RegisterPythonPreDepsMutators(ctx android.RegisterMutatorsContext) { |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 37 | ctx.BottomUp("python_version", versionSplitMutator()).Parallel() |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 40 | // the version-specific properties that apply to python modules. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 41 | type VersionProperties struct { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 42 | // whether the module is required to be built with this version. |
| 43 | // Defaults to true for Python 3, and false otherwise. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 44 | Enabled *bool `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 45 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 46 | // list of source files specific to this Python version. |
| 47 | // Using the syntax ":module", srcs may reference the outputs of other modules that produce source files, |
| 48 | // e.g. genrule or filegroup. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 49 | Srcs []string `android:"path,arch_variant"` |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 50 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 51 | // list of source files that should not be used to build the Python module for this version. |
| 52 | // 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] | 53 | Exclude_srcs []string `android:"path,arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 54 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 55 | // list of the Python libraries used only for this Python version. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 56 | Libs []string `android:"arch_variant"` |
| 57 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 58 | // whether the binary is required to be built with embedded launcher for this version, defaults to false. |
| 59 | Embedded_launcher *bool `android:"arch_variant"` // TODO(b/174041232): Remove this property |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 62 | // properties that apply to all python modules |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 63 | type BaseProperties struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 64 | // the package path prefix within the output artifact at which to place the source/data |
| 65 | // files of the current module. |
| 66 | // eg. Pkg_path = "a/b/c"; Other packages can reference this module by using |
| 67 | // (from a.b.c import ...) statement. |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 68 | // if left unspecified, all the source/data files path is unchanged within zip file. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 69 | Pkg_path *string `android:"arch_variant"` |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 70 | |
| 71 | // true, if the Python module is used internally, eg, Python std libs. |
| 72 | Is_internal *bool `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 73 | |
| 74 | // list of source (.py) files compatible both with Python2 and Python3 used to compile the |
| 75 | // Python module. |
| 76 | // srcs may reference the outputs of other modules that produce source files like genrule |
| 77 | // or filegroup using the syntax ":module". |
| 78 | // Srcs has to be non-empty. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 79 | Srcs []string `android:"path,arch_variant"` |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 80 | |
| 81 | // list of source files that should not be used to build the C/C++ module. |
| 82 | // 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] | 83 | Exclude_srcs []string `android:"path,arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 84 | |
| 85 | // list of files or filegroup modules that provide data that should be installed alongside |
| 86 | // the test. the file extension can be arbitrary except for (.py). |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 87 | Data []string `android:"path,arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 88 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 89 | // list of java modules that provide data that should be installed alongside the test. |
| 90 | Java_data []string |
| 91 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 92 | // list of the Python libraries compatible both with Python2 and Python3. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 93 | Libs []string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 94 | |
| 95 | Version struct { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 96 | // Python2-specific properties, including whether Python2 is supported for this module |
| 97 | // and version-specific sources, exclusions and dependencies. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 98 | Py2 VersionProperties `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 99 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 100 | // Python3-specific properties, including whether Python3 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 | Py3 VersionProperties `android:"arch_variant"` |
| 103 | } `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 104 | |
| 105 | // the actual version each module uses after variations created. |
| 106 | // this property name is hidden from users' perspectives, and soong will populate it during |
| 107 | // runtime. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 108 | Actual_version string `blueprint:"mutated"` |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 109 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 110 | // whether the module is required to be built with actual_version. |
| 111 | // 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] | 112 | Enabled *bool `blueprint:"mutated"` |
| 113 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 114 | // whether the binary is required to be built with embedded launcher for this 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 | Embedded_launcher *bool `blueprint:"mutated"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 119 | // Used to store files of current module after expanding dependencies |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 120 | type pathMapping struct { |
| 121 | dest string |
| 122 | src android.Path |
| 123 | } |
| 124 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 125 | type Module struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 126 | android.ModuleBase |
Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 127 | android.DefaultableModuleBase |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 128 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 129 | properties BaseProperties |
| 130 | protoProperties android.ProtoProperties |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 131 | |
| 132 | // initialize before calling Init |
| 133 | hod android.HostOrDeviceSupported |
| 134 | multilib android.Multilib |
| 135 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 136 | // interface used to bootstrap .par executable when embedded_launcher is true |
| 137 | // this should be set by Python modules which are runnable, e.g. binaries and tests |
| 138 | // bootstrapper might be nil (e.g. Python library module). |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 139 | bootstrapper bootstrapper |
| 140 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 141 | // interface that implements functions required for installation |
| 142 | // this should be set by Python modules which are runnable, e.g. binaries and tests |
| 143 | // installer might be nil (e.g. Python library module). |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 144 | installer installer |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 145 | |
| 146 | // the Python files of current module after expanding source dependencies. |
| 147 | // pathMapping: <dest: runfile_path, src: source_path> |
| 148 | srcsPathMappings []pathMapping |
| 149 | |
| 150 | // the data files of current module after expanding source dependencies. |
| 151 | // pathMapping: <dest: runfile_path, src: source_path> |
| 152 | dataPathMappings []pathMapping |
| 153 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 154 | // the zip filepath for zipping current module source/data files. |
| 155 | srcsZip android.Path |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 156 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 157 | // dependency modules' zip filepath for zipping current module source/data files. |
| 158 | depsSrcsZips android.Paths |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 159 | |
| 160 | // (.intermediate) module output path as installation source. |
| 161 | installSource android.OptionalPath |
| 162 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 163 | // Map to ensure sub-part of the AndroidMk for this module is only added once |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 164 | subAndroidMkOnce map[subAndroidMkProvider]bool |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 167 | // newModule generates new Python base module |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 168 | func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module { |
| 169 | return &Module{ |
| 170 | hod: hod, |
| 171 | multilib: multilib, |
| 172 | } |
| 173 | } |
| 174 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 175 | // bootstrapper interface should be implemented for runnable modules, e.g. binary and test |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 176 | type bootstrapper interface { |
| 177 | bootstrapperProps() []interface{} |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 178 | bootstrap(ctx android.ModuleContext, ActualVersion string, embeddedLauncher bool, |
| 179 | srcsPathMappings []pathMapping, srcsZip android.Path, |
| 180 | depsSrcsZips android.Paths) android.OptionalPath |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 181 | |
| 182 | autorun() bool |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 185 | // installer interface should be implemented for installable modules, e.g. binary and test |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 186 | type installer interface { |
| 187 | install(ctx android.ModuleContext, path android.Path) |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 188 | setAndroidMkSharedLibs(sharedLibs []string) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 191 | // interface implemented by Python modules to provide source and data mappings and zip to python |
| 192 | // modules that depend on it |
| 193 | type pythonDependency interface { |
| 194 | getSrcsPathMappings() []pathMapping |
| 195 | getDataPathMappings() []pathMapping |
| 196 | getSrcsZip() android.Path |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 199 | // getSrcsPathMappings gets this module's path mapping of src source path : runfiles destination |
| 200 | func (p *Module) getSrcsPathMappings() []pathMapping { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 201 | return p.srcsPathMappings |
| 202 | } |
| 203 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 204 | // getSrcsPathMappings gets this module's path mapping of data source path : runfiles destination |
| 205 | func (p *Module) getDataPathMappings() []pathMapping { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 206 | return p.dataPathMappings |
| 207 | } |
| 208 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 209 | // getSrcsZip returns the filepath where the current module's source/data files are zipped. |
| 210 | func (p *Module) getSrcsZip() android.Path { |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 211 | return p.srcsZip |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 214 | var _ pythonDependency = (*Module)(nil) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 215 | |
Liz Kammer | d8dceb0 | 2020-11-24 08:36:14 -0800 | [diff] [blame] | 216 | var _ android.AndroidMkEntriesProvider = (*Module)(nil) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 217 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 218 | func (p *Module) init(additionalProps ...interface{}) android.Module { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 219 | p.AddProperties(&p.properties, &p.protoProperties) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 220 | |
| 221 | // Add additional properties for bootstrapping/installation |
| 222 | // This is currently tied to the bootstrapper interface; |
| 223 | // however, these are a combination of properties for the installation and bootstrapping of a module |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 224 | if p.bootstrapper != nil { |
| 225 | p.AddProperties(p.bootstrapper.bootstrapperProps()...) |
| 226 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 227 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 228 | android.InitAndroidArchModule(p, p.hod, p.multilib) |
Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 229 | android.InitDefaultableModule(p) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 230 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 231 | return p |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 234 | // Python-specific tag to transfer information on the purpose of a dependency. |
| 235 | // This is used when adding a dependency on a module, which can later be accessed when visiting |
| 236 | // dependencies. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 237 | type dependencyTag struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 238 | blueprint.BaseDependencyTag |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 239 | name string |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 242 | // Python-specific tag that indicates that installed files of this module should depend on installed |
| 243 | // files of the dependency |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 244 | type installDependencyTag struct { |
| 245 | blueprint.BaseDependencyTag |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 246 | // embedding this struct provides the installation dependency requirement |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 247 | android.InstallAlwaysNeededDependencyTag |
| 248 | name string |
| 249 | } |
| 250 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 251 | var ( |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 252 | pythonLibTag = dependencyTag{name: "pythonLib"} |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 253 | javaDataTag = dependencyTag{name: "javaData"} |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 254 | launcherTag = dependencyTag{name: "launcher"} |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 255 | launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"} |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 256 | pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`) |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 257 | pyExt = ".py" |
| 258 | protoExt = ".proto" |
| 259 | pyVersion2 = "PY2" |
| 260 | pyVersion3 = "PY3" |
| 261 | initFileName = "__init__.py" |
| 262 | mainFileName = "__main__.py" |
| 263 | entryPointFile = "entry_point.txt" |
| 264 | parFileExt = ".zip" |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 265 | internalPath = "internal" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 266 | ) |
| 267 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 268 | // versionSplitMutator creates version variants for modules and appends the version-specific |
| 269 | // properties for a given variant to the properties in the variant module |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 270 | func versionSplitMutator() func(android.BottomUpMutatorContext) { |
| 271 | return func(mctx android.BottomUpMutatorContext) { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 272 | if base, ok := mctx.Module().(*Module); ok { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 273 | versionNames := []string{} |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 274 | // collect version specific properties, so that we can merge version-specific properties |
| 275 | // into the module's overall properties |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 276 | versionProps := []VersionProperties{} |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 277 | // PY3 is first so that we alias the PY3 variant rather than PY2 if both |
| 278 | // are available |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 279 | if proptools.BoolDefault(base.properties.Version.Py3.Enabled, true) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 280 | versionNames = append(versionNames, pyVersion3) |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 281 | versionProps = append(versionProps, base.properties.Version.Py3) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 282 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 283 | if proptools.BoolDefault(base.properties.Version.Py2.Enabled, false) { |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 284 | versionNames = append(versionNames, pyVersion2) |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 285 | versionProps = append(versionProps, base.properties.Version.Py2) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 286 | } |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 287 | modules := mctx.CreateLocalVariations(versionNames...) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 288 | // Alias module to the first variant |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 289 | if len(versionNames) > 0 { |
| 290 | mctx.AliasVariation(versionNames[0]) |
| 291 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 292 | for i, v := range versionNames { |
| 293 | // set the actual version for Python module. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 294 | modules[i].(*Module).properties.Actual_version = v |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 295 | // append versioned properties for the Python module to the overall properties |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 296 | err := proptools.AppendMatchingProperties([]interface{}{&modules[i].(*Module).properties}, &versionProps[i], nil) |
| 297 | if err != nil { |
| 298 | panic(err) |
| 299 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 305 | // HostToolPath returns a path if appropriate such that this module can be used as a host tool, |
| 306 | // fulfilling HostToolProvider interface. |
Nan Zhang | b8bdacf | 2017-12-06 15:13:10 -0800 | [diff] [blame] | 307 | func (p *Module) HostToolPath() android.OptionalPath { |
| 308 | if p.installer == nil { |
| 309 | // python_library is just meta module, and doesn't have any installer. |
| 310 | return android.OptionalPath{} |
| 311 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 312 | // TODO: This should only be set when building host binaries -- tests built for device would be |
| 313 | // setting this incorrectly. |
Nan Zhang | b8bdacf | 2017-12-06 15:13:10 -0800 | [diff] [blame] | 314 | return android.OptionalPathForPath(p.installer.(*binaryDecorator).path) |
| 315 | } |
| 316 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 317 | // OutputFiles returns output files based on given tag, returns an error if tag is unsupported. |
Liz Kammer | e0070ee | 2020-06-22 11:52:59 -0700 | [diff] [blame] | 318 | func (p *Module) OutputFiles(tag string) (android.Paths, error) { |
| 319 | switch tag { |
| 320 | case "": |
| 321 | if outputFile := p.installSource; outputFile.Valid() { |
| 322 | return android.Paths{outputFile.Path()}, nil |
| 323 | } |
| 324 | return android.Paths{}, nil |
| 325 | default: |
| 326 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 327 | } |
| 328 | } |
| 329 | |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 330 | func (p *Module) isEmbeddedLauncherEnabled() bool { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 331 | return p.installer != nil && Bool(p.properties.Embedded_launcher) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 334 | func anyHasExt(paths []string, ext string) bool { |
| 335 | for _, p := range paths { |
| 336 | if filepath.Ext(p) == ext { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 337 | return true |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return false |
| 342 | } |
| 343 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 344 | func (p *Module) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool { |
| 345 | return anyHasExt(p.properties.Srcs, ext) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 348 | // DepsMutator mutates dependencies for this module: |
| 349 | // * handles proto dependencies, |
| 350 | // * if required, specifies launcher and adds launcher dependencies, |
| 351 | // * applies python version mutations to Python dependencies |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 352 | func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 353 | android.ProtoDeps(ctx, &p.protoProperties) |
| 354 | |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 355 | versionVariation := []blueprint.Variation{ |
| 356 | {"python_version", p.properties.Actual_version}, |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 357 | } |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 358 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 359 | // If sources contain a proto file, add dependency on libprotobuf-python |
| 360 | if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" { |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 361 | ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python") |
| 362 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 363 | |
| 364 | // Add python library dependencies for this python version variation |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 365 | ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...) |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 366 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 367 | // If this module will be installed and has an embedded launcher, we need to add dependencies for: |
| 368 | // * standard library |
| 369 | // * launcher |
| 370 | // * shared dependencies of the launcher |
| 371 | if p.installer != nil && p.isEmbeddedLauncherEnabled() { |
| 372 | var stdLib string |
| 373 | var launcherModule string |
| 374 | // Add launcher shared lib dependencies. Ideally, these should be |
| 375 | // derived from the `shared_libs` property of the launcher. However, we |
| 376 | // cannot read the property at this stage and it will be too late to add |
| 377 | // dependencies later. |
| 378 | launcherSharedLibDeps := []string{ |
| 379 | "libsqlite", |
| 380 | } |
| 381 | // Add launcher-specific dependencies for bionic |
| 382 | if ctx.Target().Os.Bionic() { |
| 383 | launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm") |
| 384 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 385 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 386 | switch p.properties.Actual_version { |
| 387 | case pyVersion2: |
| 388 | stdLib = "py2-stdlib" |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 389 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 390 | launcherModule = "py2-launcher" |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 391 | if p.bootstrapper.autorun() { |
| 392 | launcherModule = "py2-launcher-autorun" |
| 393 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 394 | launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++") |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 395 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 396 | case pyVersion3: |
| 397 | stdLib = "py3-stdlib" |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 398 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 399 | launcherModule = "py3-launcher" |
Dan Willemsen | 8d4d7be | 2019-11-04 19:21:04 -0800 | [diff] [blame] | 400 | if p.bootstrapper.autorun() { |
| 401 | launcherModule = "py3-launcher-autorun" |
| 402 | } |
Dan Willemsen | 8d4d7be | 2019-11-04 19:21:04 -0800 | [diff] [blame] | 403 | |
Dan Willemsen | d7a1dee | 2020-01-20 22:08:20 -0800 | [diff] [blame] | 404 | if ctx.Device() { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 405 | launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog") |
Dan Willemsen | d7a1dee | 2020-01-20 22:08:20 -0800 | [diff] [blame] | 406 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 407 | default: |
| 408 | panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.", |
| 409 | p.properties.Actual_version, ctx.ModuleName())) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 410 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 411 | ctx.AddVariationDependencies(versionVariation, pythonLibTag, stdLib) |
| 412 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule) |
| 413 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, launcherSharedLibDeps...) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 414 | } |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 415 | |
| 416 | // Emulate the data property for java_data but with the arch variation overridden to "common" |
| 417 | // so that it can point to java modules. |
| 418 | javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}} |
| 419 | ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 422 | func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 423 | p.generatePythonBuildActions(ctx) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 424 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 425 | // Only Python binary and test modules have non-empty bootstrapper. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 426 | if p.bootstrapper != nil { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 427 | // if the module is being installed, we need to collect all transitive dependencies to embed in |
| 428 | // the final par |
| 429 | p.collectPathsFromTransitiveDeps(ctx) |
| 430 | // bootstrap the module, including resolving main file, getting launcher path, and |
| 431 | // registering actions to build the par file |
| 432 | // bootstrap returns the binary output path |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 433 | p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version, |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 434 | p.isEmbeddedLauncherEnabled(), p.srcsPathMappings, p.srcsZip, p.depsSrcsZips) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 435 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 436 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 437 | // Only Python binary and test modules have non-empty installer. |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 438 | if p.installer != nil { |
| 439 | var sharedLibs []string |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 440 | // if embedded launcher is enabled, we need to collect the shared library depenendencies of the |
| 441 | // launcher |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 442 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 443 | if ctx.OtherModuleDependencyTag(dep) == launcherSharedLibTag { |
| 444 | sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep)) |
| 445 | } |
| 446 | }) |
| 447 | p.installer.setAndroidMkSharedLibs(sharedLibs) |
| 448 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 449 | // Install the par file from installSource |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 450 | if p.installSource.Valid() { |
| 451 | p.installer.install(ctx, p.installSource.Path()) |
| 452 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 453 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 456 | // generatePythonBuildActions performs build actions common to all Python modules |
| 457 | func (p *Module) generatePythonBuildActions(ctx android.ModuleContext) { |
| 458 | expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs) |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 459 | requiresSrcs := true |
| 460 | if p.bootstrapper != nil && !p.bootstrapper.autorun() { |
| 461 | requiresSrcs = false |
| 462 | } |
| 463 | if len(expandedSrcs) == 0 && requiresSrcs { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 464 | ctx.ModuleErrorf("doesn't have any source files!") |
| 465 | } |
| 466 | |
| 467 | // expand data files from "data" property. |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 468 | expandedData := android.PathsForModuleSrc(ctx, p.properties.Data) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 469 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 470 | // Emulate the data property for java_data dependencies. |
| 471 | for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) { |
| 472 | expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...) |
| 473 | } |
| 474 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 475 | // Validate pkg_path property |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 476 | pkgPath := String(p.properties.Pkg_path) |
| 477 | if pkgPath != "" { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 478 | // 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] | 479 | pkgPath = filepath.Clean(String(p.properties.Pkg_path)) |
| 480 | if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") || |
| 481 | strings.HasPrefix(pkgPath, "/") { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 482 | ctx.PropertyErrorf("pkg_path", |
| 483 | "%q must be a relative path contained in par file.", |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 484 | String(p.properties.Pkg_path)) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 485 | return |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 486 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 487 | } |
| 488 | // If property Is_internal is set, prepend pkgPath with internalPath |
| 489 | if proptools.BoolDefault(p.properties.Is_internal, false) { |
| 490 | pkgPath = filepath.Join(internalPath, pkgPath) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 493 | // generate src:destination path mappings for this module |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 494 | p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 495 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 496 | // generate the zipfile of all source and data files |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 497 | p.srcsZip = p.createSrcsZip(ctx, pkgPath) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 500 | func isValidPythonPath(path string) error { |
| 501 | identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/") |
| 502 | for _, token := range identifiers { |
| 503 | if !pathComponentRegexp.MatchString(token) { |
| 504 | return fmt.Errorf("the path %q contains invalid subpath %q. "+ |
| 505 | "Subpaths must be at least one character long. "+ |
| 506 | "The first character must an underscore or letter. "+ |
| 507 | "Following characters may be any of: letter, digit, underscore, hyphen.", |
| 508 | path, token) |
| 509 | } |
| 510 | } |
| 511 | return nil |
| 512 | } |
| 513 | |
| 514 | // For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path> |
| 515 | // for python/data files expanded from properties. |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 516 | func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string, |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 517 | expandedSrcs, expandedData android.Paths) { |
| 518 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 519 | // check current module duplicates. |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 520 | destToPySrcs := make(map[string]string) |
| 521 | destToPyData := make(map[string]string) |
| 522 | |
| 523 | for _, s := range expandedSrcs { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 524 | if s.Ext() != pyExt && s.Ext() != protoExt { |
| 525 | ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 526 | continue |
| 527 | } |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 528 | runfilesPath := filepath.Join(pkgPath, s.Rel()) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 529 | if err := isValidPythonPath(runfilesPath); err != nil { |
| 530 | ctx.PropertyErrorf("srcs", err.Error()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 531 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 532 | if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) { |
| 533 | p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s}) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
| 537 | for _, d := range expandedData { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 538 | if d.Ext() == pyExt || d.Ext() == protoExt { |
| 539 | ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 540 | continue |
| 541 | } |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 542 | runfilesPath := filepath.Join(pkgPath, d.Rel()) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 543 | if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 544 | p.dataPathMappings = append(p.dataPathMappings, |
| 545 | pathMapping{dest: runfilesPath, src: d}) |
| 546 | } |
| 547 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 550 | // createSrcsZip registers build actions to zip current module's sources and data. |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 551 | func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 552 | relativeRootMap := make(map[string]android.Paths) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 553 | pathMappings := append(p.srcsPathMappings, p.dataPathMappings...) |
| 554 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 555 | var protoSrcs android.Paths |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 556 | // "srcs" or "data" properties may contain filegroup so it might happen that |
| 557 | // the root directory for each source path is different. |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 558 | for _, path := range pathMappings { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 559 | // handle proto sources separately |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 560 | if path.src.Ext() == protoExt { |
| 561 | protoSrcs = append(protoSrcs, path.src) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 562 | } else { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 563 | var relativeRoot string |
| 564 | relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel()) |
| 565 | if v, found := relativeRootMap[relativeRoot]; found { |
| 566 | relativeRootMap[relativeRoot] = append(v, path.src) |
| 567 | } else { |
| 568 | relativeRootMap[relativeRoot] = android.Paths{path.src} |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | var zips android.Paths |
| 573 | if len(protoSrcs) > 0 { |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 574 | protoFlags := android.GetProtoFlags(ctx, &p.protoProperties) |
| 575 | protoFlags.OutTypeFlag = "--python_out" |
| 576 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 577 | for _, srcFile := range protoSrcs { |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 578 | zip := genProto(ctx, srcFile, protoFlags, pkgPath) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 579 | zips = append(zips, zip) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 583 | if len(relativeRootMap) > 0 { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 584 | // 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^] | 585 | roots := android.SortedStringKeys(relativeRootMap) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 586 | |
| 587 | parArgs := []string{} |
Nan Zhang | f0c4e43 | 2018-05-22 14:50:18 -0700 | [diff] [blame] | 588 | if pkgPath != "" { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 589 | // use package path as path prefix |
Nan Zhang | f0c4e43 | 2018-05-22 14:50:18 -0700 | [diff] [blame] | 590 | parArgs = append(parArgs, `-P `+pkgPath) |
| 591 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 592 | paths := android.Paths{} |
| 593 | for _, root := range roots { |
| 594 | // specify relative root of file in following -f arguments |
| 595 | parArgs = append(parArgs, `-C `+root) |
| 596 | for _, path := range relativeRootMap[root] { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 597 | parArgs = append(parArgs, `-f `+path.String()) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 598 | paths = append(paths, path) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip") |
| 603 | ctx.Build(pctx, android.BuildParams{ |
| 604 | Rule: zip, |
| 605 | Description: "python library archive", |
| 606 | Output: origSrcsZip, |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 607 | // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits |
| 608 | Implicits: paths, |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 609 | Args: map[string]string{ |
| 610 | "args": strings.Join(parArgs, " "), |
| 611 | }, |
| 612 | }) |
| 613 | zips = append(zips, origSrcsZip) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 614 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 615 | // 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] | 616 | if len(zips) == 1 { |
| 617 | return zips[0] |
| 618 | } else { |
| 619 | combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip") |
| 620 | ctx.Build(pctx, android.BuildParams{ |
| 621 | Rule: combineZip, |
| 622 | Description: "combine python library archive", |
| 623 | Output: combinedSrcsZip, |
| 624 | Inputs: zips, |
| 625 | }) |
| 626 | return combinedSrcsZip |
| 627 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 630 | // isPythonLibModule returns whether the given module is a Python library Module or not |
| 631 | // This is distinguished by the fact that Python libraries are not installable, while other Python |
| 632 | // modules are. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 633 | func isPythonLibModule(module blueprint.Module) bool { |
| 634 | if m, ok := module.(*Module); ok { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 635 | // Python library has no bootstrapper or installer |
| 636 | if m.bootstrapper == nil && m.installer == nil { |
| 637 | return true |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 638 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 639 | } |
| 640 | return false |
| 641 | } |
| 642 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 643 | // collectPathsFromTransitiveDeps checks for source/data files for duplicate paths |
| 644 | // for module and its transitive dependencies and collects list of data/source file |
| 645 | // zips for transitive dependencies. |
| 646 | func (p *Module) collectPathsFromTransitiveDeps(ctx android.ModuleContext) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 647 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to |
| 648 | // check duplicates. |
| 649 | destToPySrcs := make(map[string]string) |
| 650 | destToPyData := make(map[string]string) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 651 | for _, path := range p.srcsPathMappings { |
| 652 | destToPySrcs[path.dest] = path.src.String() |
| 653 | } |
| 654 | for _, path := range p.dataPathMappings { |
| 655 | destToPyData[path.dest] = path.src.String() |
| 656 | } |
| 657 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 658 | seen := make(map[android.Module]bool) |
| 659 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 660 | // visit all its dependencies in depth first. |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 661 | ctx.WalkDeps(func(child, parent android.Module) bool { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 662 | // we only collect dependencies tagged as python library deps |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 663 | if ctx.OtherModuleDependencyTag(child) != pythonLibTag { |
| 664 | return false |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 665 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 666 | if seen[child] { |
| 667 | return false |
| 668 | } |
| 669 | seen[child] = true |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 670 | // Python modules only can depend on Python libraries. |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 671 | if !isPythonLibModule(child) { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 672 | ctx.PropertyErrorf("libs", |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 673 | "the dependency %q of module %q is not Python library!", |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 674 | ctx.ModuleName(), ctx.OtherModuleName(child)) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 675 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 676 | // collect source and data paths, checking that there are no duplicate output file conflicts |
| 677 | if dep, ok := child.(pythonDependency); ok { |
| 678 | srcs := dep.getSrcsPathMappings() |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 679 | for _, path := range srcs { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 680 | checkForDuplicateOutputPath(ctx, destToPySrcs, |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 681 | path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 682 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 683 | data := dep.getDataPathMappings() |
| 684 | for _, path := range data { |
| 685 | checkForDuplicateOutputPath(ctx, destToPyData, |
| 686 | path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) |
| 687 | } |
| 688 | p.depsSrcsZips = append(p.depsSrcsZips, dep.getSrcsZip()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 689 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 690 | return true |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 691 | }) |
| 692 | } |
| 693 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 694 | // chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which |
| 695 | // would result in two files being placed in the same location. |
| 696 | // If there is a duplicate path, an error is thrown and true is returned |
| 697 | // Otherwise, outputPath: srcPath is added to m and returns false |
| 698 | func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool { |
| 699 | if oldSrcPath, found := m[outputPath]; found { |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 700 | 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] | 701 | " First file: in module %s at path %q."+ |
| 702 | " Second file: in module %s at path %q.", |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 703 | outputPath, curModule, oldSrcPath, otherModule, srcPath) |
| 704 | return true |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 705 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 706 | m[outputPath] = srcPath |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 707 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 708 | return false |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 709 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 710 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame^] | 711 | // InstallInData returns true as Python is not supported in the system partition |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 712 | func (p *Module) InstallInData() bool { |
| 713 | return true |
| 714 | } |
| 715 | |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 716 | var Bool = proptools.Bool |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 717 | var BoolDefault = proptools.BoolDefault |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 718 | var String = proptools.String |