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