| 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" | 
|  | 23 | "sort" | 
|  | 24 | "strings" | 
|  | 25 |  | 
|  | 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() { | 
|  | 33 | android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { | 
|  | 34 | ctx.BottomUp("version_split", versionSplitMutator()).Parallel() | 
|  | 35 | }) | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | // the version properties that apply to python libraries and binaries. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 39 | type VersionProperties struct { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 40 | // true, if the module is required to be built with this version. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 41 | Enabled *bool `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 42 |  | 
|  | 43 | // non-empty list of .py files under this strict Python version. | 
|  | 44 | // srcs may reference the outputs of other modules that produce source files like genrule | 
|  | 45 | // or filegroup using the syntax ":module". | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 46 | Srcs []string `android:"arch_variant"` | 
|  | 47 |  | 
|  | 48 | // list of source files that should not be used to build the Python module. | 
|  | 49 | // This is most useful in the arch/multilib variants to remove non-common files | 
|  | 50 | Exclude_srcs []string `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 51 |  | 
|  | 52 | // list of the Python libraries under this Python version. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 53 | Libs []string `android:"arch_variant"` | 
|  | 54 |  | 
|  | 55 | // true, if the binary is required to be built with embedded launcher. | 
|  | 56 | // TODO(nanzhang): Remove this flag when embedded Python3 is supported later. | 
|  | 57 | Embedded_launcher *bool `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
|  | 60 | // properties that apply to python libraries and binaries. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 61 | type BaseProperties struct { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 62 | // the package path prefix within the output artifact at which to place the source/data | 
|  | 63 | // files of the current module. | 
|  | 64 | // eg. Pkg_path = "a/b/c"; Other packages can reference this module by using | 
|  | 65 | // (from a.b.c import ...) statement. | 
| Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 66 | // 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] | 67 | Pkg_path *string `android:"arch_variant"` | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 68 |  | 
|  | 69 | // true, if the Python module is used internally, eg, Python std libs. | 
|  | 70 | Is_internal *bool `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 71 |  | 
|  | 72 | // list of source (.py) files compatible both with Python2 and Python3 used to compile the | 
|  | 73 | // Python module. | 
|  | 74 | // srcs may reference the outputs of other modules that produce source files like genrule | 
|  | 75 | // or filegroup using the syntax ":module". | 
|  | 76 | // Srcs has to be non-empty. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 77 | Srcs []string `android:"arch_variant"` | 
|  | 78 |  | 
|  | 79 | // list of source files that should not be used to build the C/C++ module. | 
|  | 80 | // This is most useful in the arch/multilib variants to remove non-common files | 
|  | 81 | Exclude_srcs []string `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 82 |  | 
|  | 83 | // list of files or filegroup modules that provide data that should be installed alongside | 
|  | 84 | // the test. the file extension can be arbitrary except for (.py). | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 85 | Data []string `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 86 |  | 
|  | 87 | // list of the Python libraries compatible both with Python2 and Python3. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 88 | Libs []string `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 89 |  | 
|  | 90 | Version struct { | 
|  | 91 | // all the "srcs" or Python dependencies that are to be used only for Python2. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 92 | Py2 VersionProperties `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 93 |  | 
|  | 94 | // all the "srcs" or Python dependencies that are to be used only for Python3. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 95 | Py3 VersionProperties `android:"arch_variant"` | 
|  | 96 | } `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 97 |  | 
|  | 98 | // the actual version each module uses after variations created. | 
|  | 99 | // this property name is hidden from users' perspectives, and soong will populate it during | 
|  | 100 | // runtime. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 101 | Actual_version string `blueprint:"mutated"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
|  | 104 | type pathMapping struct { | 
|  | 105 | dest string | 
|  | 106 | src  android.Path | 
|  | 107 | } | 
|  | 108 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 109 | type Module struct { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 110 | android.ModuleBase | 
| Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 111 | android.DefaultableModuleBase | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 112 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 113 | properties      BaseProperties | 
|  | 114 | protoProperties android.ProtoProperties | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | // initialize before calling Init | 
|  | 117 | hod      android.HostOrDeviceSupported | 
|  | 118 | multilib android.Multilib | 
|  | 119 |  | 
|  | 120 | // the bootstrapper is used to bootstrap .par executable. | 
|  | 121 | // bootstrapper might be nil (Python library module). | 
|  | 122 | bootstrapper bootstrapper | 
|  | 123 |  | 
|  | 124 | // the installer might be nil. | 
|  | 125 | installer installer | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 126 |  | 
|  | 127 | // the Python files of current module after expanding source dependencies. | 
|  | 128 | // pathMapping: <dest: runfile_path, src: source_path> | 
|  | 129 | srcsPathMappings []pathMapping | 
|  | 130 |  | 
|  | 131 | // the data files of current module after expanding source dependencies. | 
|  | 132 | // pathMapping: <dest: runfile_path, src: source_path> | 
|  | 133 | dataPathMappings []pathMapping | 
|  | 134 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 135 | // the zip filepath for zipping current module source/data files. | 
|  | 136 | srcsZip android.Path | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 137 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 138 | // dependency modules' zip filepath for zipping current module source/data files. | 
|  | 139 | depsSrcsZips android.Paths | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 140 |  | 
|  | 141 | // (.intermediate) module output path as installation source. | 
|  | 142 | installSource android.OptionalPath | 
|  | 143 |  | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 144 | subAndroidMkOnce map[subAndroidMkProvider]bool | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 147 | func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module { | 
|  | 148 | return &Module{ | 
|  | 149 | hod:      hod, | 
|  | 150 | multilib: multilib, | 
|  | 151 | } | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | type bootstrapper interface { | 
|  | 155 | bootstrapperProps() []interface{} | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 156 | bootstrap(ctx android.ModuleContext, ActualVersion string, embeddedLauncher bool, | 
|  | 157 | srcsPathMappings []pathMapping, srcsZip android.Path, | 
|  | 158 | depsSrcsZips android.Paths) android.OptionalPath | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
|  | 161 | type installer interface { | 
|  | 162 | install(ctx android.ModuleContext, path android.Path) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
|  | 165 | type PythonDependency interface { | 
|  | 166 | GetSrcsPathMappings() []pathMapping | 
|  | 167 | GetDataPathMappings() []pathMapping | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 168 | GetSrcsZip() android.Path | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 169 | } | 
|  | 170 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 171 | func (p *Module) GetSrcsPathMappings() []pathMapping { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 172 | return p.srcsPathMappings | 
|  | 173 | } | 
|  | 174 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 175 | func (p *Module) GetDataPathMappings() []pathMapping { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 176 | return p.dataPathMappings | 
|  | 177 | } | 
|  | 178 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 179 | func (p *Module) GetSrcsZip() android.Path { | 
|  | 180 | return p.srcsZip | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 183 | var _ PythonDependency = (*Module)(nil) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 184 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 185 | var _ android.AndroidMkDataProvider = (*Module)(nil) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 186 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 187 | func (p *Module) Init() android.Module { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 188 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 189 | p.AddProperties(&p.properties, &p.protoProperties) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 190 | if p.bootstrapper != nil { | 
|  | 191 | p.AddProperties(p.bootstrapper.bootstrapperProps()...) | 
|  | 192 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 193 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 194 | android.InitAndroidArchModule(p, p.hod, p.multilib) | 
| Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 195 | android.InitDefaultableModule(p) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 196 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 197 | return p | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 198 | } | 
|  | 199 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 200 | type dependencyTag struct { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 201 | blueprint.BaseDependencyTag | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 202 | name string | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 203 | } | 
|  | 204 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 205 | var ( | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 206 | pythonLibTag       = dependencyTag{name: "pythonLib"} | 
|  | 207 | launcherTag        = dependencyTag{name: "launcher"} | 
| Nan Zhang | 1a479b6 | 2018-05-15 17:13:38 -0700 | [diff] [blame] | 208 | pyIdentifierRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 209 | pyExt              = ".py" | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 210 | protoExt           = ".proto" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 211 | pyVersion2         = "PY2" | 
|  | 212 | pyVersion3         = "PY3" | 
|  | 213 | initFileName       = "__init__.py" | 
|  | 214 | mainFileName       = "__main__.py" | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 215 | entryPointFile     = "entry_point.txt" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 216 | parFileExt         = ".zip" | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 217 | internal           = "internal" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 218 | ) | 
|  | 219 |  | 
|  | 220 | // create version variants for modules. | 
|  | 221 | func versionSplitMutator() func(android.BottomUpMutatorContext) { | 
|  | 222 | return func(mctx android.BottomUpMutatorContext) { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 223 | if base, ok := mctx.Module().(*Module); ok { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 224 | versionNames := []string{} | 
|  | 225 | if base.properties.Version.Py2.Enabled != nil && | 
|  | 226 | *(base.properties.Version.Py2.Enabled) == true { | 
|  | 227 | versionNames = append(versionNames, pyVersion2) | 
|  | 228 | } | 
|  | 229 | if !(base.properties.Version.Py3.Enabled != nil && | 
|  | 230 | *(base.properties.Version.Py3.Enabled) == false) { | 
|  | 231 | versionNames = append(versionNames, pyVersion3) | 
|  | 232 | } | 
|  | 233 | modules := mctx.CreateVariations(versionNames...) | 
|  | 234 | for i, v := range versionNames { | 
|  | 235 | // set the actual version for Python module. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 236 | modules[i].(*Module).properties.Actual_version = v | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 237 | } | 
|  | 238 | } | 
|  | 239 | } | 
|  | 240 | } | 
|  | 241 |  | 
| Nan Zhang | b8bdacf | 2017-12-06 15:13:10 -0800 | [diff] [blame] | 242 | func (p *Module) HostToolPath() android.OptionalPath { | 
|  | 243 | if p.installer == nil { | 
|  | 244 | // python_library is just meta module, and doesn't have any installer. | 
|  | 245 | return android.OptionalPath{} | 
|  | 246 | } | 
|  | 247 | return android.OptionalPathForPath(p.installer.(*binaryDecorator).path) | 
|  | 248 | } | 
|  | 249 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 250 | func (p *Module) isEmbeddedLauncherEnabled(actual_version string) bool { | 
|  | 251 | switch actual_version { | 
|  | 252 | case pyVersion2: | 
| Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 253 | return Bool(p.properties.Version.Py2.Embedded_launcher) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 254 | case pyVersion3: | 
| Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 255 | return Bool(p.properties.Version.Py3.Embedded_launcher) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 256 | } | 
|  | 257 |  | 
|  | 258 | return false | 
|  | 259 | } | 
|  | 260 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 261 | func hasSrcExt(srcs []string, ext string) bool { | 
|  | 262 | for _, src := range srcs { | 
|  | 263 | if filepath.Ext(src) == ext { | 
|  | 264 | return true | 
|  | 265 | } | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | return false | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | func (p *Module) hasSrcExt(ctx android.BottomUpMutatorContext, ext string) bool { | 
|  | 272 | if hasSrcExt(p.properties.Srcs, protoExt) { | 
|  | 273 | return true | 
|  | 274 | } | 
|  | 275 | switch p.properties.Actual_version { | 
|  | 276 | case pyVersion2: | 
|  | 277 | return hasSrcExt(p.properties.Version.Py2.Srcs, protoExt) | 
|  | 278 | case pyVersion3: | 
|  | 279 | return hasSrcExt(p.properties.Version.Py3.Srcs, protoExt) | 
|  | 280 | default: | 
|  | 281 | panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.", | 
|  | 282 | p.properties.Actual_version, ctx.ModuleName())) | 
|  | 283 | } | 
|  | 284 | } | 
|  | 285 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 286 | func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 287 | // deps from "data". | 
|  | 288 | android.ExtractSourcesDeps(ctx, p.properties.Data) | 
|  | 289 | // deps from "srcs". | 
|  | 290 | android.ExtractSourcesDeps(ctx, p.properties.Srcs) | 
| Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 291 | android.ExtractSourcesDeps(ctx, p.properties.Exclude_srcs) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 292 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 293 | if p.hasSrcExt(ctx, protoExt) && p.Name() != "libprotobuf-python" { | 
|  | 294 | ctx.AddVariationDependencies(nil, pythonLibTag, "libprotobuf-python") | 
|  | 295 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 296 | switch p.properties.Actual_version { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 297 | case pyVersion2: | 
|  | 298 | // deps from "version.py2.srcs" property. | 
|  | 299 | android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Srcs) | 
| Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 300 | android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Exclude_srcs) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 301 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 302 | ctx.AddVariationDependencies(nil, pythonLibTag, | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 303 | uniqueLibs(ctx, p.properties.Libs, "version.py2.libs", | 
|  | 304 | p.properties.Version.Py2.Libs)...) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 305 |  | 
|  | 306 | if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) { | 
|  | 307 | ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib") | 
|  | 308 | ctx.AddFarVariationDependencies([]blueprint.Variation{ | 
| Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 309 | {Mutator: "arch", Variation: ctx.Target().String()}, | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 310 | }, launcherTag, "py2-launcher") | 
|  | 311 | } | 
|  | 312 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 313 | case pyVersion3: | 
|  | 314 | // deps from "version.py3.srcs" property. | 
|  | 315 | android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs) | 
| Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 316 | android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Exclude_srcs) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 317 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 318 | ctx.AddVariationDependencies(nil, pythonLibTag, | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 319 | uniqueLibs(ctx, p.properties.Libs, "version.py3.libs", | 
|  | 320 | p.properties.Version.Py3.Libs)...) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 321 |  | 
|  | 322 | if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) { | 
|  | 323 | //TODO(nanzhang): Add embedded launcher for Python3. | 
|  | 324 | ctx.PropertyErrorf("version.py3.embedded_launcher", | 
|  | 325 | "is not supported yet for Python3.") | 
|  | 326 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 327 | default: | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 328 | panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.", | 
|  | 329 | p.properties.Actual_version, ctx.ModuleName())) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 330 | } | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | // check "libs" duplicates from current module dependencies. | 
|  | 334 | func uniqueLibs(ctx android.BottomUpMutatorContext, | 
|  | 335 | commonLibs []string, versionProp string, versionLibs []string) []string { | 
|  | 336 | set := make(map[string]string) | 
|  | 337 | ret := []string{} | 
|  | 338 |  | 
|  | 339 | // deps from "libs" property. | 
|  | 340 | for _, l := range commonLibs { | 
|  | 341 | if _, found := set[l]; found { | 
|  | 342 | ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l) | 
|  | 343 | } else { | 
|  | 344 | set[l] = "libs" | 
|  | 345 | ret = append(ret, l) | 
|  | 346 | } | 
|  | 347 | } | 
|  | 348 | // deps from "version.pyX.libs" property. | 
|  | 349 | for _, l := range versionLibs { | 
|  | 350 | if _, found := set[l]; found { | 
|  | 351 | ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l]) | 
|  | 352 | } else { | 
|  | 353 | set[l] = versionProp | 
|  | 354 | ret = append(ret, l) | 
|  | 355 | } | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | return ret | 
|  | 359 | } | 
|  | 360 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 361 | func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 362 | p.GeneratePythonBuildActions(ctx) | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 363 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 364 | // Only Python binaries and test has non-empty bootstrapper. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 365 | if p.bootstrapper != nil { | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 366 | p.walkTransitiveDeps(ctx) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 367 | // TODO(nanzhang): Since embedded launcher is not supported for Python3 for now, | 
|  | 368 | // so we initialize "embedded_launcher" to false. | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 369 | embeddedLauncher := false | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 370 | if p.properties.Actual_version == pyVersion2 { | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 371 | embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion2) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 372 | } | 
|  | 373 | p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version, | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 374 | embeddedLauncher, p.srcsPathMappings, p.srcsZip, p.depsSrcsZips) | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 375 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 376 |  | 
|  | 377 | if p.installer != nil && p.installSource.Valid() { | 
|  | 378 | p.installer.install(ctx, p.installSource.Path()) | 
|  | 379 | } | 
|  | 380 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 381 | } | 
|  | 382 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 383 | func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 384 | // expand python files from "srcs" property. | 
|  | 385 | srcs := p.properties.Srcs | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 386 | exclude_srcs := p.properties.Exclude_srcs | 
|  | 387 | switch p.properties.Actual_version { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 388 | case pyVersion2: | 
|  | 389 | srcs = append(srcs, p.properties.Version.Py2.Srcs...) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 390 | exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 391 | case pyVersion3: | 
|  | 392 | srcs = append(srcs, p.properties.Version.Py3.Srcs...) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 393 | exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 394 | default: | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 395 | panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.", | 
|  | 396 | p.properties.Actual_version, ctx.ModuleName())) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 397 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 398 | expandedSrcs := ctx.ExpandSources(srcs, exclude_srcs) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 399 | if len(expandedSrcs) == 0 { | 
|  | 400 | ctx.ModuleErrorf("doesn't have any source files!") | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | // expand data files from "data" property. | 
|  | 404 | expandedData := ctx.ExpandSources(p.properties.Data, nil) | 
|  | 405 |  | 
|  | 406 | // sanitize pkg_path. | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 407 | pkgPath := String(p.properties.Pkg_path) | 
|  | 408 | if pkgPath != "" { | 
|  | 409 | pkgPath = filepath.Clean(String(p.properties.Pkg_path)) | 
|  | 410 | if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") || | 
|  | 411 | strings.HasPrefix(pkgPath, "/") { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 412 | ctx.PropertyErrorf("pkg_path", | 
|  | 413 | "%q must be a relative path contained in par file.", | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 414 | String(p.properties.Pkg_path)) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 415 | return | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 416 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 417 | if p.properties.Is_internal != nil && *p.properties.Is_internal { | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 418 | pkgPath = filepath.Join(internal, pkgPath) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 419 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 420 | } else { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 421 | if p.properties.Is_internal != nil && *p.properties.Is_internal { | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 422 | pkgPath = internal | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 423 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 424 | } | 
|  | 425 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 426 | p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 427 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 428 | p.srcsZip = p.createSrcsZip(ctx, pkgPath) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 429 | } | 
|  | 430 |  | 
|  | 431 | // generate current module unique pathMappings: <dest: runfiles_path, src: source_path> | 
|  | 432 | // for python/data files. | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 433 | func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string, | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 434 | expandedSrcs, expandedData android.Paths) { | 
|  | 435 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 436 | // check current module duplicates. | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 437 | destToPySrcs := make(map[string]string) | 
|  | 438 | destToPyData := make(map[string]string) | 
|  | 439 |  | 
|  | 440 | for _, s := range expandedSrcs { | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 441 | if s.Ext() != pyExt && s.Ext() != protoExt { | 
|  | 442 | ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String()) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 443 | continue | 
|  | 444 | } | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 445 | runfilesPath := filepath.Join(pkgPath, s.Rel()) | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 446 | identifiers := strings.Split(strings.TrimSuffix(runfilesPath, | 
|  | 447 | filepath.Ext(runfilesPath)), "/") | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 448 | for _, token := range identifiers { | 
|  | 449 | if !pyIdentifierRegexp.MatchString(token) { | 
|  | 450 | ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.", | 
|  | 451 | runfilesPath, token) | 
|  | 452 | } | 
|  | 453 | } | 
|  | 454 | if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) { | 
|  | 455 | p.srcsPathMappings = append(p.srcsPathMappings, | 
|  | 456 | pathMapping{dest: runfilesPath, src: s}) | 
|  | 457 | } | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | for _, d := range expandedData { | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 461 | if d.Ext() == pyExt || d.Ext() == protoExt { | 
|  | 462 | ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String()) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 463 | continue | 
|  | 464 | } | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 465 | runfilesPath := filepath.Join(pkgPath, d.Rel()) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 466 | if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) { | 
|  | 467 | p.dataPathMappings = append(p.dataPathMappings, | 
|  | 468 | pathMapping{dest: runfilesPath, src: d}) | 
|  | 469 | } | 
|  | 470 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 471 | } | 
|  | 472 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 473 | // register build actions to zip current module's sources. | 
|  | 474 | func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 475 | relativeRootMap := make(map[string]android.Paths) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 476 | pathMappings := append(p.srcsPathMappings, p.dataPathMappings...) | 
|  | 477 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 478 | var protoSrcs android.Paths | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 479 | // "srcs" or "data" properties may have filegroup so it might happen that | 
|  | 480 | // the relative root for each source path is different. | 
|  | 481 | for _, path := range pathMappings { | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 482 | if path.src.Ext() == protoExt { | 
|  | 483 | protoSrcs = append(protoSrcs, path.src) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 484 | } else { | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 485 | var relativeRoot string | 
|  | 486 | relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel()) | 
|  | 487 | if v, found := relativeRootMap[relativeRoot]; found { | 
|  | 488 | relativeRootMap[relativeRoot] = append(v, path.src) | 
|  | 489 | } else { | 
|  | 490 | relativeRootMap[relativeRoot] = android.Paths{path.src} | 
|  | 491 | } | 
|  | 492 | } | 
|  | 493 | } | 
|  | 494 | var zips android.Paths | 
|  | 495 | if len(protoSrcs) > 0 { | 
|  | 496 | for _, srcFile := range protoSrcs { | 
|  | 497 | zip := genProto(ctx, &p.protoProperties, srcFile, | 
|  | 498 | android.ProtoFlags(ctx, &p.protoProperties), pkgPath) | 
|  | 499 | zips = append(zips, zip) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 500 | } | 
|  | 501 | } | 
|  | 502 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 503 | if len(relativeRootMap) > 0 { | 
|  | 504 | var keys []string | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 505 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 506 | // in order to keep stable order of soong_zip params, we sort the keys here. | 
|  | 507 | for k := range relativeRootMap { | 
|  | 508 | keys = append(keys, k) | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 509 | } | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 510 | sort.Strings(keys) | 
|  | 511 |  | 
|  | 512 | parArgs := []string{} | 
| Nan Zhang | f0c4e43 | 2018-05-22 14:50:18 -0700 | [diff] [blame] | 513 | if pkgPath != "" { | 
|  | 514 | parArgs = append(parArgs, `-P `+pkgPath) | 
|  | 515 | } | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 516 | implicits := android.Paths{} | 
|  | 517 | for _, k := range keys { | 
|  | 518 | parArgs = append(parArgs, `-C `+k) | 
|  | 519 | for _, path := range relativeRootMap[k] { | 
|  | 520 | parArgs = append(parArgs, `-f `+path.String()) | 
|  | 521 | implicits = append(implicits, path) | 
|  | 522 | } | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip") | 
|  | 526 | ctx.Build(pctx, android.BuildParams{ | 
|  | 527 | Rule:        zip, | 
|  | 528 | Description: "python library archive", | 
|  | 529 | Output:      origSrcsZip, | 
|  | 530 | Implicits:   implicits, | 
|  | 531 | Args: map[string]string{ | 
|  | 532 | "args": strings.Join(parArgs, " "), | 
|  | 533 | }, | 
|  | 534 | }) | 
|  | 535 | zips = append(zips, origSrcsZip) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 536 | } | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 537 | if len(zips) == 1 { | 
|  | 538 | return zips[0] | 
|  | 539 | } else { | 
|  | 540 | combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip") | 
|  | 541 | ctx.Build(pctx, android.BuildParams{ | 
|  | 542 | Rule:        combineZip, | 
|  | 543 | Description: "combine python library archive", | 
|  | 544 | Output:      combinedSrcsZip, | 
|  | 545 | Inputs:      zips, | 
|  | 546 | }) | 
|  | 547 | return combinedSrcsZip | 
|  | 548 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 549 | } | 
|  | 550 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 551 | func isPythonLibModule(module blueprint.Module) bool { | 
|  | 552 | if m, ok := module.(*Module); ok { | 
|  | 553 | // Python library has no bootstrapper or installer. | 
|  | 554 | if m.bootstrapper != nil || m.installer != nil { | 
|  | 555 | return false | 
|  | 556 | } | 
|  | 557 | return true | 
|  | 558 | } | 
|  | 559 | return false | 
|  | 560 | } | 
|  | 561 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 562 | // check Python source/data files duplicates for whole runfiles tree since Python binary/test | 
|  | 563 | // need collect and zip all srcs of whole transitive dependencies to a final par file. | 
|  | 564 | func (p *Module) walkTransitiveDeps(ctx android.ModuleContext) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 565 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to | 
|  | 566 | // check duplicates. | 
|  | 567 | destToPySrcs := make(map[string]string) | 
|  | 568 | destToPyData := make(map[string]string) | 
|  | 569 |  | 
|  | 570 | for _, path := range p.srcsPathMappings { | 
|  | 571 | destToPySrcs[path.dest] = path.src.String() | 
|  | 572 | } | 
|  | 573 | for _, path := range p.dataPathMappings { | 
|  | 574 | destToPyData[path.dest] = path.src.String() | 
|  | 575 | } | 
|  | 576 |  | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 577 | seen := make(map[android.Module]bool) | 
|  | 578 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 579 | // visit all its dependencies in depth first. | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 580 | ctx.WalkDeps(func(child, parent android.Module) bool { | 
|  | 581 | if ctx.OtherModuleDependencyTag(child) != pythonLibTag { | 
|  | 582 | return false | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 583 | } | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 584 | if seen[child] { | 
|  | 585 | return false | 
|  | 586 | } | 
|  | 587 | seen[child] = true | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 588 | // Python modules only can depend on Python libraries. | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 589 | if !isPythonLibModule(child) { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 590 | panic(fmt.Errorf( | 
|  | 591 | "the dependency %q of module %q is not Python library!", | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 592 | ctx.ModuleName(), ctx.OtherModuleName(child))) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 593 | } | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 594 | if dep, ok := child.(PythonDependency); ok { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 595 | srcs := dep.GetSrcsPathMappings() | 
|  | 596 | for _, path := range srcs { | 
|  | 597 | if !fillInMap(ctx, destToPySrcs, | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 598 | path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 599 | continue | 
|  | 600 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 601 | } | 
|  | 602 | data := dep.GetDataPathMappings() | 
|  | 603 | for _, path := range data { | 
|  | 604 | fillInMap(ctx, destToPyData, | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 605 | path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 606 | } | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 607 | p.depsSrcsZips = append(p.depsSrcsZips, dep.GetSrcsZip()) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 608 | } | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 609 | return true | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 610 | }) | 
|  | 611 | } | 
|  | 612 |  | 
|  | 613 | func fillInMap(ctx android.ModuleContext, m map[string]string, | 
|  | 614 | key, value, curModule, otherModule string) bool { | 
|  | 615 | if oldValue, found := m[key]; found { | 
| Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 616 | 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] | 617 | " First file: in module %s at path %q."+ | 
|  | 618 | " Second file: in module %s at path %q.", | 
|  | 619 | key, curModule, oldValue, otherModule, value) | 
|  | 620 | return false | 
|  | 621 | } else { | 
|  | 622 | m[key] = value | 
|  | 623 | } | 
|  | 624 |  | 
|  | 625 | return true | 
|  | 626 | } | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 627 |  | 
| Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 628 | func (p *Module) InstallInData() bool { | 
|  | 629 | return true | 
|  | 630 | } | 
|  | 631 |  | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 632 | var Bool = proptools.Bool | 
|  | 633 | var String = proptools.String |