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