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