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