| 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". | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 46 | Srcs []string `android:"path,arch_variant"` | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 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 | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 50 | Exclude_srcs []string `android:"path,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. | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 77 | Srcs []string `android:"path,arch_variant"` | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 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 | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 81 | Exclude_srcs []string `android:"path,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). | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 85 | Data []string `android:"path,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 | 
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 159 |  | 
|  | 160 | autorun() bool | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
|  | 163 | type installer interface { | 
|  | 164 | install(ctx android.ModuleContext, path android.Path) | 
| Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 165 | setAndroidMkSharedLibs(sharedLibs []string) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
|  | 168 | type PythonDependency interface { | 
|  | 169 | GetSrcsPathMappings() []pathMapping | 
|  | 170 | GetDataPathMappings() []pathMapping | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 171 | GetSrcsZip() android.Path | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 174 | func (p *Module) GetSrcsPathMappings() []pathMapping { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 175 | return p.srcsPathMappings | 
|  | 176 | } | 
|  | 177 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 178 | func (p *Module) GetDataPathMappings() []pathMapping { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 179 | return p.dataPathMappings | 
|  | 180 | } | 
|  | 181 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 182 | func (p *Module) GetSrcsZip() android.Path { | 
|  | 183 | return p.srcsZip | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 186 | var _ PythonDependency = (*Module)(nil) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 187 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 188 | var _ android.AndroidMkDataProvider = (*Module)(nil) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 189 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 190 | func (p *Module) Init() android.Module { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 191 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 192 | p.AddProperties(&p.properties, &p.protoProperties) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 193 | if p.bootstrapper != nil { | 
|  | 194 | p.AddProperties(p.bootstrapper.bootstrapperProps()...) | 
|  | 195 | } | 
| 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 | android.InitAndroidArchModule(p, p.hod, p.multilib) | 
| Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 198 | android.InitDefaultableModule(p) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 199 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 200 | return p | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 201 | } | 
|  | 202 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 203 | type dependencyTag struct { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 204 | blueprint.BaseDependencyTag | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 205 | name string | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 206 | } | 
|  | 207 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 208 | var ( | 
| Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 209 | pythonLibTag         = dependencyTag{name: "pythonLib"} | 
|  | 210 | launcherTag          = dependencyTag{name: "launcher"} | 
|  | 211 | launcherSharedLibTag = dependencyTag{name: "launcherSharedLib"} | 
|  | 212 | pyIdentifierRegexp   = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`) | 
|  | 213 | pyExt                = ".py" | 
|  | 214 | protoExt             = ".proto" | 
|  | 215 | pyVersion2           = "PY2" | 
|  | 216 | pyVersion3           = "PY3" | 
|  | 217 | initFileName         = "__init__.py" | 
|  | 218 | mainFileName         = "__main__.py" | 
|  | 219 | entryPointFile       = "entry_point.txt" | 
|  | 220 | parFileExt           = ".zip" | 
|  | 221 | internal             = "internal" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 222 | ) | 
|  | 223 |  | 
|  | 224 | // create version variants for modules. | 
|  | 225 | func versionSplitMutator() func(android.BottomUpMutatorContext) { | 
|  | 226 | return func(mctx android.BottomUpMutatorContext) { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 227 | if base, ok := mctx.Module().(*Module); ok { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 228 | versionNames := []string{} | 
|  | 229 | if base.properties.Version.Py2.Enabled != nil && | 
|  | 230 | *(base.properties.Version.Py2.Enabled) == true { | 
|  | 231 | versionNames = append(versionNames, pyVersion2) | 
|  | 232 | } | 
|  | 233 | if !(base.properties.Version.Py3.Enabled != nil && | 
|  | 234 | *(base.properties.Version.Py3.Enabled) == false) { | 
|  | 235 | versionNames = append(versionNames, pyVersion3) | 
|  | 236 | } | 
|  | 237 | modules := mctx.CreateVariations(versionNames...) | 
|  | 238 | for i, v := range versionNames { | 
|  | 239 | // set the actual version for Python module. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 240 | modules[i].(*Module).properties.Actual_version = v | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 241 | } | 
|  | 242 | } | 
|  | 243 | } | 
|  | 244 | } | 
|  | 245 |  | 
| Nan Zhang | b8bdacf | 2017-12-06 15:13:10 -0800 | [diff] [blame] | 246 | func (p *Module) HostToolPath() android.OptionalPath { | 
|  | 247 | if p.installer == nil { | 
|  | 248 | // python_library is just meta module, and doesn't have any installer. | 
|  | 249 | return android.OptionalPath{} | 
|  | 250 | } | 
|  | 251 | return android.OptionalPathForPath(p.installer.(*binaryDecorator).path) | 
|  | 252 | } | 
|  | 253 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 254 | func (p *Module) isEmbeddedLauncherEnabled(actual_version string) bool { | 
|  | 255 | switch actual_version { | 
|  | 256 | case pyVersion2: | 
| Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 257 | return Bool(p.properties.Version.Py2.Embedded_launcher) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 258 | case pyVersion3: | 
| Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 259 | return Bool(p.properties.Version.Py3.Embedded_launcher) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 260 | } | 
|  | 261 |  | 
|  | 262 | return false | 
|  | 263 | } | 
|  | 264 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 265 | func hasSrcExt(srcs []string, ext string) bool { | 
|  | 266 | for _, src := range srcs { | 
|  | 267 | if filepath.Ext(src) == ext { | 
|  | 268 | return true | 
|  | 269 | } | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | return false | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | func (p *Module) hasSrcExt(ctx android.BottomUpMutatorContext, ext string) bool { | 
|  | 276 | if hasSrcExt(p.properties.Srcs, protoExt) { | 
|  | 277 | return true | 
|  | 278 | } | 
|  | 279 | switch p.properties.Actual_version { | 
|  | 280 | case pyVersion2: | 
|  | 281 | return hasSrcExt(p.properties.Version.Py2.Srcs, protoExt) | 
|  | 282 | case pyVersion3: | 
|  | 283 | return hasSrcExt(p.properties.Version.Py3.Srcs, protoExt) | 
|  | 284 | default: | 
|  | 285 | panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.", | 
|  | 286 | p.properties.Actual_version, ctx.ModuleName())) | 
|  | 287 | } | 
|  | 288 | } | 
|  | 289 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 290 | func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 291 | android.ProtoDeps(ctx, &p.protoProperties) | 
|  | 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: | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 298 | ctx.AddVariationDependencies(nil, pythonLibTag, | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 299 | uniqueLibs(ctx, p.properties.Libs, "version.py2.libs", | 
|  | 300 | p.properties.Version.Py2.Libs)...) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 301 |  | 
|  | 302 | if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) { | 
|  | 303 | ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib") | 
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 304 |  | 
|  | 305 | launcherModule := "py2-launcher" | 
|  | 306 | if p.bootstrapper.autorun() { | 
|  | 307 | launcherModule = "py2-launcher-autorun" | 
|  | 308 | } | 
| Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 309 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule) | 
| Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 310 |  | 
|  | 311 | // Add py2-launcher shared lib dependencies. Ideally, these should be | 
|  | 312 | // derived from the `shared_libs` property of "py2-launcher". However, we | 
|  | 313 | // cannot read the property at this stage and it will be too late to add | 
|  | 314 | // dependencies later. | 
| Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 315 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, "libsqlite") | 
| Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 316 |  | 
|  | 317 | if ctx.Target().Os.Bionic() { | 
| Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 318 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, | 
|  | 319 | "libc", "libdl", "libm") | 
| Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 320 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 321 | } | 
|  | 322 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 323 | case pyVersion3: | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 324 | ctx.AddVariationDependencies(nil, pythonLibTag, | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 325 | uniqueLibs(ctx, p.properties.Libs, "version.py3.libs", | 
|  | 326 | p.properties.Version.Py3.Libs)...) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 327 |  | 
|  | 328 | if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) { | 
| Dan Willemsen | 8d4d7be | 2019-11-04 19:21:04 -0800 | [diff] [blame] | 329 | ctx.AddVariationDependencies(nil, pythonLibTag, "py3-stdlib") | 
|  | 330 |  | 
|  | 331 | launcherModule := "py3-launcher" | 
|  | 332 | if p.bootstrapper.autorun() { | 
|  | 333 | launcherModule = "py3-launcher-autorun" | 
|  | 334 | } | 
|  | 335 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule) | 
|  | 336 |  | 
|  | 337 | // Add py3-launcher shared lib dependencies. Ideally, these should be | 
|  | 338 | // derived from the `shared_libs` property of "py3-launcher". However, we | 
|  | 339 | // cannot read the property at this stage and it will be too late to add | 
|  | 340 | // dependencies later. | 
|  | 341 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, "libsqlite") | 
|  | 342 |  | 
| Dan Willemsen | d7a1dee | 2020-01-20 22:08:20 -0800 | [diff] [blame] | 343 | if ctx.Device() { | 
|  | 344 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, | 
|  | 345 | "liblog") | 
|  | 346 | } | 
|  | 347 |  | 
| Dan Willemsen | 8d4d7be | 2019-11-04 19:21:04 -0800 | [diff] [blame] | 348 | if ctx.Target().Os.Bionic() { | 
|  | 349 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, | 
|  | 350 | "libc", "libdl", "libm") | 
|  | 351 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 352 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 353 | default: | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 354 | panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.", | 
|  | 355 | p.properties.Actual_version, ctx.ModuleName())) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 356 | } | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | // check "libs" duplicates from current module dependencies. | 
|  | 360 | func uniqueLibs(ctx android.BottomUpMutatorContext, | 
|  | 361 | commonLibs []string, versionProp string, versionLibs []string) []string { | 
|  | 362 | set := make(map[string]string) | 
|  | 363 | ret := []string{} | 
|  | 364 |  | 
|  | 365 | // deps from "libs" property. | 
|  | 366 | for _, l := range commonLibs { | 
|  | 367 | if _, found := set[l]; found { | 
|  | 368 | ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l) | 
|  | 369 | } else { | 
|  | 370 | set[l] = "libs" | 
|  | 371 | ret = append(ret, l) | 
|  | 372 | } | 
|  | 373 | } | 
|  | 374 | // deps from "version.pyX.libs" property. | 
|  | 375 | for _, l := range versionLibs { | 
|  | 376 | if _, found := set[l]; found { | 
|  | 377 | ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l]) | 
|  | 378 | } else { | 
|  | 379 | set[l] = versionProp | 
|  | 380 | ret = append(ret, l) | 
|  | 381 | } | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | return ret | 
|  | 385 | } | 
|  | 386 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 387 | func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 388 | p.GeneratePythonBuildActions(ctx) | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 389 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 390 | // Only Python binaries and test has non-empty bootstrapper. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 391 | if p.bootstrapper != nil { | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 392 | p.walkTransitiveDeps(ctx) | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 393 | embeddedLauncher := false | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 394 | if p.properties.Actual_version == pyVersion2 { | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 395 | embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion2) | 
| Dan Willemsen | 8d4d7be | 2019-11-04 19:21:04 -0800 | [diff] [blame] | 396 | } else { | 
|  | 397 | embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion3) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 398 | } | 
|  | 399 | p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version, | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 400 | embeddedLauncher, p.srcsPathMappings, p.srcsZip, p.depsSrcsZips) | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 401 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 402 |  | 
| Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 403 | if p.installer != nil { | 
|  | 404 | var sharedLibs []string | 
|  | 405 | ctx.VisitDirectDeps(func(dep android.Module) { | 
|  | 406 | if ctx.OtherModuleDependencyTag(dep) == launcherSharedLibTag { | 
|  | 407 | sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep)) | 
|  | 408 | } | 
|  | 409 | }) | 
|  | 410 | p.installer.setAndroidMkSharedLibs(sharedLibs) | 
|  | 411 |  | 
|  | 412 | if p.installSource.Valid() { | 
|  | 413 | p.installer.install(ctx, p.installSource.Path()) | 
|  | 414 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 415 | } | 
|  | 416 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 417 | } | 
|  | 418 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 419 | func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 420 | // expand python files from "srcs" property. | 
|  | 421 | srcs := p.properties.Srcs | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 422 | exclude_srcs := p.properties.Exclude_srcs | 
|  | 423 | switch p.properties.Actual_version { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 424 | case pyVersion2: | 
|  | 425 | srcs = append(srcs, p.properties.Version.Py2.Srcs...) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 426 | exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 427 | case pyVersion3: | 
|  | 428 | srcs = append(srcs, p.properties.Version.Py3.Srcs...) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 429 | exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 430 | default: | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 431 | panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.", | 
|  | 432 | p.properties.Actual_version, ctx.ModuleName())) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 433 | } | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 434 | expandedSrcs := android.PathsForModuleSrcExcludes(ctx, srcs, exclude_srcs) | 
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 435 | requiresSrcs := true | 
|  | 436 | if p.bootstrapper != nil && !p.bootstrapper.autorun() { | 
|  | 437 | requiresSrcs = false | 
|  | 438 | } | 
|  | 439 | if len(expandedSrcs) == 0 && requiresSrcs { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 440 | ctx.ModuleErrorf("doesn't have any source files!") | 
|  | 441 | } | 
|  | 442 |  | 
|  | 443 | // expand data files from "data" property. | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 444 | expandedData := android.PathsForModuleSrc(ctx, p.properties.Data) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 445 |  | 
|  | 446 | // sanitize pkg_path. | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 447 | pkgPath := String(p.properties.Pkg_path) | 
|  | 448 | if pkgPath != "" { | 
|  | 449 | pkgPath = filepath.Clean(String(p.properties.Pkg_path)) | 
|  | 450 | if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") || | 
|  | 451 | strings.HasPrefix(pkgPath, "/") { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 452 | ctx.PropertyErrorf("pkg_path", | 
|  | 453 | "%q must be a relative path contained in par file.", | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 454 | String(p.properties.Pkg_path)) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 455 | return | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 456 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 457 | if p.properties.Is_internal != nil && *p.properties.Is_internal { | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 458 | pkgPath = filepath.Join(internal, pkgPath) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 459 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 460 | } else { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 461 | if p.properties.Is_internal != nil && *p.properties.Is_internal { | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 462 | pkgPath = internal | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 463 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 464 | } | 
|  | 465 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 466 | p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 467 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 468 | p.srcsZip = p.createSrcsZip(ctx, pkgPath) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 469 | } | 
|  | 470 |  | 
|  | 471 | // generate current module unique pathMappings: <dest: runfiles_path, src: source_path> | 
|  | 472 | // for python/data files. | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 473 | func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string, | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 474 | expandedSrcs, expandedData android.Paths) { | 
|  | 475 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 476 | // check current module duplicates. | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 477 | destToPySrcs := make(map[string]string) | 
|  | 478 | destToPyData := make(map[string]string) | 
|  | 479 |  | 
|  | 480 | for _, s := range expandedSrcs { | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 481 | if s.Ext() != pyExt && s.Ext() != protoExt { | 
|  | 482 | ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String()) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 483 | continue | 
|  | 484 | } | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 485 | runfilesPath := filepath.Join(pkgPath, s.Rel()) | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 486 | identifiers := strings.Split(strings.TrimSuffix(runfilesPath, | 
|  | 487 | filepath.Ext(runfilesPath)), "/") | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 488 | for _, token := range identifiers { | 
|  | 489 | if !pyIdentifierRegexp.MatchString(token) { | 
|  | 490 | ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.", | 
|  | 491 | runfilesPath, token) | 
|  | 492 | } | 
|  | 493 | } | 
|  | 494 | if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) { | 
|  | 495 | p.srcsPathMappings = append(p.srcsPathMappings, | 
|  | 496 | pathMapping{dest: runfilesPath, src: s}) | 
|  | 497 | } | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 | for _, d := range expandedData { | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 501 | if d.Ext() == pyExt || d.Ext() == protoExt { | 
|  | 502 | ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String()) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 503 | continue | 
|  | 504 | } | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 505 | runfilesPath := filepath.Join(pkgPath, d.Rel()) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 506 | if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) { | 
|  | 507 | p.dataPathMappings = append(p.dataPathMappings, | 
|  | 508 | pathMapping{dest: runfilesPath, src: d}) | 
|  | 509 | } | 
|  | 510 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 511 | } | 
|  | 512 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 513 | // register build actions to zip current module's sources. | 
|  | 514 | func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 515 | relativeRootMap := make(map[string]android.Paths) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 516 | pathMappings := append(p.srcsPathMappings, p.dataPathMappings...) | 
|  | 517 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 518 | var protoSrcs android.Paths | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 519 | // "srcs" or "data" properties may have filegroup so it might happen that | 
|  | 520 | // the relative root for each source path is different. | 
|  | 521 | for _, path := range pathMappings { | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 522 | if path.src.Ext() == protoExt { | 
|  | 523 | protoSrcs = append(protoSrcs, path.src) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 524 | } else { | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 525 | var relativeRoot string | 
|  | 526 | relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel()) | 
|  | 527 | if v, found := relativeRootMap[relativeRoot]; found { | 
|  | 528 | relativeRootMap[relativeRoot] = append(v, path.src) | 
|  | 529 | } else { | 
|  | 530 | relativeRootMap[relativeRoot] = android.Paths{path.src} | 
|  | 531 | } | 
|  | 532 | } | 
|  | 533 | } | 
|  | 534 | var zips android.Paths | 
|  | 535 | if len(protoSrcs) > 0 { | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 536 | protoFlags := android.GetProtoFlags(ctx, &p.protoProperties) | 
|  | 537 | protoFlags.OutTypeFlag = "--python_out" | 
|  | 538 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 539 | for _, srcFile := range protoSrcs { | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 540 | zip := genProto(ctx, srcFile, protoFlags, pkgPath) | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 541 | zips = append(zips, zip) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 542 | } | 
|  | 543 | } | 
|  | 544 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 545 | if len(relativeRootMap) > 0 { | 
|  | 546 | var keys []string | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 547 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 548 | // in order to keep stable order of soong_zip params, we sort the keys here. | 
|  | 549 | for k := range relativeRootMap { | 
|  | 550 | keys = append(keys, k) | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 551 | } | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 552 | sort.Strings(keys) | 
|  | 553 |  | 
|  | 554 | parArgs := []string{} | 
| Nan Zhang | f0c4e43 | 2018-05-22 14:50:18 -0700 | [diff] [blame] | 555 | if pkgPath != "" { | 
|  | 556 | parArgs = append(parArgs, `-P `+pkgPath) | 
|  | 557 | } | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 558 | implicits := android.Paths{} | 
|  | 559 | for _, k := range keys { | 
|  | 560 | parArgs = append(parArgs, `-C `+k) | 
|  | 561 | for _, path := range relativeRootMap[k] { | 
|  | 562 | parArgs = append(parArgs, `-f `+path.String()) | 
|  | 563 | implicits = append(implicits, path) | 
|  | 564 | } | 
|  | 565 | } | 
|  | 566 |  | 
|  | 567 | origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip") | 
|  | 568 | ctx.Build(pctx, android.BuildParams{ | 
|  | 569 | Rule:        zip, | 
|  | 570 | Description: "python library archive", | 
|  | 571 | Output:      origSrcsZip, | 
|  | 572 | Implicits:   implicits, | 
|  | 573 | Args: map[string]string{ | 
|  | 574 | "args": strings.Join(parArgs, " "), | 
|  | 575 | }, | 
|  | 576 | }) | 
|  | 577 | zips = append(zips, origSrcsZip) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 578 | } | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 579 | if len(zips) == 1 { | 
|  | 580 | return zips[0] | 
|  | 581 | } else { | 
|  | 582 | combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip") | 
|  | 583 | ctx.Build(pctx, android.BuildParams{ | 
|  | 584 | Rule:        combineZip, | 
|  | 585 | Description: "combine python library archive", | 
|  | 586 | Output:      combinedSrcsZip, | 
|  | 587 | Inputs:      zips, | 
|  | 588 | }) | 
|  | 589 | return combinedSrcsZip | 
|  | 590 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 591 | } | 
|  | 592 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 593 | func isPythonLibModule(module blueprint.Module) bool { | 
|  | 594 | if m, ok := module.(*Module); ok { | 
|  | 595 | // Python library has no bootstrapper or installer. | 
|  | 596 | if m.bootstrapper != nil || m.installer != nil { | 
|  | 597 | return false | 
|  | 598 | } | 
|  | 599 | return true | 
|  | 600 | } | 
|  | 601 | return false | 
|  | 602 | } | 
|  | 603 |  | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 604 | // check Python source/data files duplicates for whole runfiles tree since Python binary/test | 
|  | 605 | // need collect and zip all srcs of whole transitive dependencies to a final par file. | 
|  | 606 | func (p *Module) walkTransitiveDeps(ctx android.ModuleContext) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 607 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to | 
|  | 608 | // check duplicates. | 
|  | 609 | destToPySrcs := make(map[string]string) | 
|  | 610 | destToPyData := make(map[string]string) | 
|  | 611 |  | 
|  | 612 | for _, path := range p.srcsPathMappings { | 
|  | 613 | destToPySrcs[path.dest] = path.src.String() | 
|  | 614 | } | 
|  | 615 | for _, path := range p.dataPathMappings { | 
|  | 616 | destToPyData[path.dest] = path.src.String() | 
|  | 617 | } | 
|  | 618 |  | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 619 | seen := make(map[android.Module]bool) | 
|  | 620 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 621 | // visit all its dependencies in depth first. | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 622 | ctx.WalkDeps(func(child, parent android.Module) bool { | 
|  | 623 | if ctx.OtherModuleDependencyTag(child) != pythonLibTag { | 
|  | 624 | return false | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 625 | } | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 626 | if seen[child] { | 
|  | 627 | return false | 
|  | 628 | } | 
|  | 629 | seen[child] = true | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 630 | // Python modules only can depend on Python libraries. | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 631 | if !isPythonLibModule(child) { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 632 | panic(fmt.Errorf( | 
|  | 633 | "the dependency %q of module %q is not Python library!", | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 634 | ctx.ModuleName(), ctx.OtherModuleName(child))) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 635 | } | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 636 | if dep, ok := child.(PythonDependency); ok { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 637 | srcs := dep.GetSrcsPathMappings() | 
|  | 638 | for _, path := range srcs { | 
|  | 639 | if !fillInMap(ctx, destToPySrcs, | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 640 | path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 641 | continue | 
|  | 642 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 643 | } | 
|  | 644 | data := dep.GetDataPathMappings() | 
|  | 645 | for _, path := range data { | 
|  | 646 | fillInMap(ctx, destToPyData, | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 647 | path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 648 | } | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 649 | p.depsSrcsZips = append(p.depsSrcsZips, dep.GetSrcsZip()) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 650 | } | 
| Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 651 | return true | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 652 | }) | 
|  | 653 | } | 
|  | 654 |  | 
|  | 655 | func fillInMap(ctx android.ModuleContext, m map[string]string, | 
|  | 656 | key, value, curModule, otherModule string) bool { | 
|  | 657 | if oldValue, found := m[key]; found { | 
| Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 658 | 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] | 659 | " First file: in module %s at path %q."+ | 
|  | 660 | " Second file: in module %s at path %q.", | 
|  | 661 | key, curModule, oldValue, otherModule, value) | 
|  | 662 | return false | 
|  | 663 | } else { | 
|  | 664 | m[key] = value | 
|  | 665 | } | 
|  | 666 |  | 
|  | 667 | return true | 
|  | 668 | } | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 669 |  | 
| Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 670 | func (p *Module) InstallInData() bool { | 
|  | 671 | return true | 
|  | 672 | } | 
|  | 673 |  | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 674 | var Bool = proptools.Bool | 
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 675 | var BoolDefault = proptools.BoolDefault | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 676 | var String = proptools.String |