Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package python |
| 16 | |
| 17 | // This file contains the "Base" module type for building Python program. |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "path/filepath" |
| 22 | "regexp" |
| 23 | "sort" |
| 24 | "strings" |
| 25 | |
| 26 | "github.com/google/blueprint" |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 27 | "github.com/google/blueprint/proptools" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 28 | |
| 29 | "android/soong/android" |
| 30 | ) |
| 31 | |
| 32 | func init() { |
| 33 | android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 34 | ctx.BottomUp("version_split", versionSplitMutator()).Parallel() |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | // the version properties that apply to python libraries and binaries. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 39 | type VersionProperties struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 40 | // true, if the module is required to be built with this version. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 41 | Enabled *bool `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 42 | |
| 43 | // non-empty list of .py files under this strict Python version. |
| 44 | // srcs may reference the outputs of other modules that produce source files like genrule |
| 45 | // or filegroup using the syntax ":module". |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 46 | Srcs []string `android:"arch_variant"` |
| 47 | |
| 48 | // list of source files that should not be used to build the Python module. |
| 49 | // This is most useful in the arch/multilib variants to remove non-common files |
| 50 | Exclude_srcs []string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 51 | |
| 52 | // list of the Python libraries under this Python version. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 53 | Libs []string `android:"arch_variant"` |
| 54 | |
| 55 | // true, if the binary is required to be built with embedded launcher. |
| 56 | // TODO(nanzhang): Remove this flag when embedded Python3 is supported later. |
| 57 | Embedded_launcher *bool `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // properties that apply to python libraries and binaries. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 61 | type BaseProperties struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 62 | // the package path prefix within the output artifact at which to place the source/data |
| 63 | // files of the current module. |
| 64 | // eg. Pkg_path = "a/b/c"; Other packages can reference this module by using |
| 65 | // (from a.b.c import ...) statement. |
| 66 | // if left unspecified, all the source/data files of current module are copied to |
| 67 | // "runfiles/" tree directory directly. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 68 | Pkg_path string `android:"arch_variant"` |
| 69 | |
| 70 | // true, if the Python module is used internally, eg, Python std libs. |
| 71 | Is_internal *bool `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 72 | |
| 73 | // list of source (.py) files compatible both with Python2 and Python3 used to compile the |
| 74 | // Python module. |
| 75 | // srcs may reference the outputs of other modules that produce source files like genrule |
| 76 | // or filegroup using the syntax ":module". |
| 77 | // Srcs has to be non-empty. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 78 | Srcs []string `android:"arch_variant"` |
| 79 | |
| 80 | // list of source files that should not be used to build the C/C++ module. |
| 81 | // This is most useful in the arch/multilib variants to remove non-common files |
| 82 | Exclude_srcs []string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 83 | |
| 84 | // list of files or filegroup modules that provide data that should be installed alongside |
| 85 | // the test. the file extension can be arbitrary except for (.py). |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 86 | Data []string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 87 | |
| 88 | // list of the Python libraries compatible both with Python2 and Python3. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 89 | Libs []string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 90 | |
| 91 | Version struct { |
| 92 | // 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^] | 93 | Py2 VersionProperties `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 94 | |
| 95 | // 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^] | 96 | Py3 VersionProperties `android:"arch_variant"` |
| 97 | } `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 98 | |
| 99 | // the actual version each module uses after variations created. |
| 100 | // this property name is hidden from users' perspectives, and soong will populate it during |
| 101 | // runtime. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 102 | Actual_version string `blueprint:"mutated"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | type pathMapping struct { |
| 106 | dest string |
| 107 | src android.Path |
| 108 | } |
| 109 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 110 | type Module struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 111 | android.ModuleBase |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 112 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 113 | properties BaseProperties |
| 114 | |
| 115 | // initialize before calling Init |
| 116 | hod android.HostOrDeviceSupported |
| 117 | multilib android.Multilib |
| 118 | |
| 119 | // the bootstrapper is used to bootstrap .par executable. |
| 120 | // bootstrapper might be nil (Python library module). |
| 121 | bootstrapper bootstrapper |
| 122 | |
| 123 | // the installer might be nil. |
| 124 | installer installer |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 125 | |
| 126 | // the Python files of current module after expanding source dependencies. |
| 127 | // pathMapping: <dest: runfile_path, src: source_path> |
| 128 | srcsPathMappings []pathMapping |
| 129 | |
| 130 | // the data files of current module after expanding source dependencies. |
| 131 | // pathMapping: <dest: runfile_path, src: source_path> |
| 132 | dataPathMappings []pathMapping |
| 133 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 134 | // soong_zip arguments of all its dependencies. |
| 135 | depsParSpecs []parSpec |
| 136 | |
| 137 | // Python runfiles paths of all its dependencies. |
| 138 | depsPyRunfiles []string |
| 139 | |
| 140 | // (.intermediate) module output path as installation source. |
| 141 | installSource android.OptionalPath |
| 142 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 143 | // the soong_zip arguments for zipping current module source/data files. |
| 144 | parSpec parSpec |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 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{} |
| 158 | bootstrap(ctx android.ModuleContext, Actual_version string, embedded_launcher bool, |
| 159 | srcsPathMappings []pathMapping, parSpec parSpec, |
| 160 | depsPyRunfiles []string, depsParSpecs []parSpec) android.OptionalPath |
| 161 | } |
| 162 | |
| 163 | type installer interface { |
| 164 | install(ctx android.ModuleContext, path android.Path) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | type PythonDependency interface { |
| 168 | GetSrcsPathMappings() []pathMapping |
| 169 | GetDataPathMappings() []pathMapping |
| 170 | GetParSpec() parSpec |
| 171 | } |
| 172 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 173 | func (p *Module) GetSrcsPathMappings() []pathMapping { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 174 | return p.srcsPathMappings |
| 175 | } |
| 176 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 177 | func (p *Module) GetDataPathMappings() []pathMapping { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 178 | return p.dataPathMappings |
| 179 | } |
| 180 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 181 | func (p *Module) GetParSpec() parSpec { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 182 | return p.parSpec |
| 183 | } |
| 184 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 185 | var _ PythonDependency = (*Module)(nil) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 186 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 187 | var _ android.AndroidMkDataProvider = (*Module)(nil) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 188 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 189 | func (p *Module) Init() android.Module { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 190 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 191 | p.AddProperties(&p.properties) |
| 192 | if p.bootstrapper != nil { |
| 193 | p.AddProperties(p.bootstrapper.bootstrapperProps()...) |
| 194 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 195 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 196 | android.InitAndroidArchModule(p, p.hod, p.multilib) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 197 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 198 | return p |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 201 | type dependencyTag struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 202 | blueprint.BaseDependencyTag |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 203 | name string |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 206 | var ( |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 207 | pythonLibTag = dependencyTag{name: "pythonLib"} |
| 208 | launcherTag = dependencyTag{name: "launcher"} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 209 | pyIdentifierRegexp = regexp.MustCompile(`^([a-z]|[A-Z]|_)([a-z]|[A-Z]|[0-9]|_)*$`) |
| 210 | pyExt = ".py" |
| 211 | pyVersion2 = "PY2" |
| 212 | pyVersion3 = "PY3" |
| 213 | initFileName = "__init__.py" |
| 214 | mainFileName = "__main__.py" |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 215 | entryPointFile = "entry_point.txt" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 216 | parFileExt = ".zip" |
| 217 | runFiles = "runfiles" |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 218 | internal = "internal" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 219 | ) |
| 220 | |
| 221 | // create version variants for modules. |
| 222 | func versionSplitMutator() func(android.BottomUpMutatorContext) { |
| 223 | return func(mctx android.BottomUpMutatorContext) { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 224 | if base, ok := mctx.Module().(*Module); ok { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 225 | versionNames := []string{} |
| 226 | if base.properties.Version.Py2.Enabled != nil && |
| 227 | *(base.properties.Version.Py2.Enabled) == true { |
| 228 | versionNames = append(versionNames, pyVersion2) |
| 229 | } |
| 230 | if !(base.properties.Version.Py3.Enabled != nil && |
| 231 | *(base.properties.Version.Py3.Enabled) == false) { |
| 232 | versionNames = append(versionNames, pyVersion3) |
| 233 | } |
| 234 | modules := mctx.CreateVariations(versionNames...) |
| 235 | for i, v := range versionNames { |
| 236 | // set the actual version for Python module. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 237 | modules[i].(*Module).properties.Actual_version = v |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 243 | func (p *Module) isEmbeddedLauncherEnabled(actual_version string) bool { |
| 244 | switch actual_version { |
| 245 | case pyVersion2: |
| 246 | return proptools.Bool(p.properties.Version.Py2.Embedded_launcher) |
| 247 | case pyVersion3: |
| 248 | return proptools.Bool(p.properties.Version.Py3.Embedded_launcher) |
| 249 | } |
| 250 | |
| 251 | return false |
| 252 | } |
| 253 | |
| 254 | func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 255 | // deps from "data". |
| 256 | android.ExtractSourcesDeps(ctx, p.properties.Data) |
| 257 | // deps from "srcs". |
| 258 | android.ExtractSourcesDeps(ctx, p.properties.Srcs) |
| 259 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 260 | switch p.properties.Actual_version { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 261 | case pyVersion2: |
| 262 | // deps from "version.py2.srcs" property. |
| 263 | android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Srcs) |
| 264 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 265 | ctx.AddVariationDependencies(nil, pythonLibTag, |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 266 | uniqueLibs(ctx, p.properties.Libs, "version.py2.libs", |
| 267 | p.properties.Version.Py2.Libs)...) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 268 | |
| 269 | if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) { |
| 270 | ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib") |
| 271 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 272 | {"arch", ctx.Target().String()}, |
| 273 | }, launcherTag, "py2-launcher") |
| 274 | } |
| 275 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 276 | case pyVersion3: |
| 277 | // deps from "version.py3.srcs" property. |
| 278 | android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs) |
| 279 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 280 | ctx.AddVariationDependencies(nil, pythonLibTag, |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 281 | uniqueLibs(ctx, p.properties.Libs, "version.py3.libs", |
| 282 | p.properties.Version.Py3.Libs)...) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 283 | |
| 284 | if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) { |
| 285 | //TODO(nanzhang): Add embedded launcher for Python3. |
| 286 | ctx.PropertyErrorf("version.py3.embedded_launcher", |
| 287 | "is not supported yet for Python3.") |
| 288 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 289 | default: |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 290 | panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.", |
| 291 | p.properties.Actual_version, ctx.ModuleName())) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | // check "libs" duplicates from current module dependencies. |
| 296 | func uniqueLibs(ctx android.BottomUpMutatorContext, |
| 297 | commonLibs []string, versionProp string, versionLibs []string) []string { |
| 298 | set := make(map[string]string) |
| 299 | ret := []string{} |
| 300 | |
| 301 | // deps from "libs" property. |
| 302 | for _, l := range commonLibs { |
| 303 | if _, found := set[l]; found { |
| 304 | ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l) |
| 305 | } else { |
| 306 | set[l] = "libs" |
| 307 | ret = append(ret, l) |
| 308 | } |
| 309 | } |
| 310 | // deps from "version.pyX.libs" property. |
| 311 | for _, l := range versionLibs { |
| 312 | if _, found := set[l]; found { |
| 313 | ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l]) |
| 314 | } else { |
| 315 | set[l] = versionProp |
| 316 | ret = append(ret, l) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return ret |
| 321 | } |
| 322 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 323 | func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 324 | p.GeneratePythonBuildActions(ctx) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 325 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 326 | if p.bootstrapper != nil { |
| 327 | // TODO(nanzhang): Since embedded launcher is not supported for Python3 for now, |
| 328 | // so we initialize "embedded_launcher" to false. |
| 329 | embedded_launcher := false |
| 330 | if p.properties.Actual_version == pyVersion2 { |
| 331 | embedded_launcher = p.isEmbeddedLauncherEnabled(pyVersion2) |
| 332 | } |
| 333 | p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version, |
| 334 | embedded_launcher, p.srcsPathMappings, p.parSpec, p.depsPyRunfiles, |
| 335 | p.depsParSpecs) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 336 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 337 | |
| 338 | if p.installer != nil && p.installSource.Valid() { |
| 339 | p.installer.install(ctx, p.installSource.Path()) |
| 340 | } |
| 341 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 344 | func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 345 | // expand python files from "srcs" property. |
| 346 | srcs := p.properties.Srcs |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 347 | exclude_srcs := p.properties.Exclude_srcs |
| 348 | switch p.properties.Actual_version { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 349 | case pyVersion2: |
| 350 | srcs = append(srcs, p.properties.Version.Py2.Srcs...) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 351 | exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 352 | case pyVersion3: |
| 353 | srcs = append(srcs, p.properties.Version.Py3.Srcs...) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 354 | exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 355 | default: |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 356 | panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.", |
| 357 | p.properties.Actual_version, ctx.ModuleName())) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 358 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 359 | expandedSrcs := ctx.ExpandSources(srcs, exclude_srcs) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 360 | if len(expandedSrcs) == 0 { |
| 361 | ctx.ModuleErrorf("doesn't have any source files!") |
| 362 | } |
| 363 | |
| 364 | // expand data files from "data" property. |
| 365 | expandedData := ctx.ExpandSources(p.properties.Data, nil) |
| 366 | |
| 367 | // sanitize pkg_path. |
| 368 | pkg_path := p.properties.Pkg_path |
| 369 | if pkg_path != "" { |
| 370 | pkg_path = filepath.Clean(p.properties.Pkg_path) |
| 371 | if pkg_path == ".." || strings.HasPrefix(pkg_path, "../") || |
| 372 | strings.HasPrefix(pkg_path, "/") { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 373 | ctx.PropertyErrorf("pkg_path", |
| 374 | "%q must be a relative path contained in par file.", |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 375 | p.properties.Pkg_path) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 376 | return |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 377 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 378 | if p.properties.Is_internal != nil && *p.properties.Is_internal { |
| 379 | // pkg_path starts from "internal/" implicitly. |
| 380 | pkg_path = filepath.Join(internal, pkg_path) |
| 381 | } else { |
| 382 | // pkg_path starts from "runfiles/" implicitly. |
| 383 | pkg_path = filepath.Join(runFiles, pkg_path) |
| 384 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 385 | } else { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 386 | if p.properties.Is_internal != nil && *p.properties.Is_internal { |
| 387 | // pkg_path starts from "runfiles/" implicitly. |
| 388 | pkg_path = internal |
| 389 | } else { |
| 390 | // pkg_path starts from "runfiles/" implicitly. |
| 391 | pkg_path = runFiles |
| 392 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | p.genModulePathMappings(ctx, pkg_path, expandedSrcs, expandedData) |
| 396 | |
| 397 | p.parSpec = p.dumpFileList(ctx, pkg_path) |
| 398 | |
| 399 | p.uniqWholeRunfilesTree(ctx) |
| 400 | } |
| 401 | |
| 402 | // generate current module unique pathMappings: <dest: runfiles_path, src: source_path> |
| 403 | // for python/data files. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 404 | func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkg_path string, |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 405 | expandedSrcs, expandedData android.Paths) { |
| 406 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to |
| 407 | // check duplicates. |
| 408 | destToPySrcs := make(map[string]string) |
| 409 | destToPyData := make(map[string]string) |
| 410 | |
| 411 | for _, s := range expandedSrcs { |
| 412 | if s.Ext() != pyExt { |
| 413 | ctx.PropertyErrorf("srcs", "found non (.py) file: %q!", s.String()) |
| 414 | continue |
| 415 | } |
| 416 | runfilesPath := filepath.Join(pkg_path, s.Rel()) |
| 417 | identifiers := strings.Split(strings.TrimSuffix(runfilesPath, pyExt), "/") |
| 418 | for _, token := range identifiers { |
| 419 | if !pyIdentifierRegexp.MatchString(token) { |
| 420 | ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.", |
| 421 | runfilesPath, token) |
| 422 | } |
| 423 | } |
| 424 | if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) { |
| 425 | p.srcsPathMappings = append(p.srcsPathMappings, |
| 426 | pathMapping{dest: runfilesPath, src: s}) |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | for _, d := range expandedData { |
| 431 | if d.Ext() == pyExt { |
| 432 | ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String()) |
| 433 | continue |
| 434 | } |
| 435 | runfilesPath := filepath.Join(pkg_path, d.Rel()) |
| 436 | if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) { |
| 437 | p.dataPathMappings = append(p.dataPathMappings, |
| 438 | pathMapping{dest: runfilesPath, src: d}) |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | } |
| 443 | |
| 444 | // register build actions to dump filelist to disk. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 445 | func (p *Module) dumpFileList(ctx android.ModuleContext, pkg_path string) parSpec { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 446 | relativeRootMap := make(map[string]android.Paths) |
| 447 | // the soong_zip params in order to pack current module's Python/data files. |
| 448 | ret := parSpec{rootPrefix: pkg_path} |
| 449 | |
| 450 | pathMappings := append(p.srcsPathMappings, p.dataPathMappings...) |
| 451 | |
| 452 | // "srcs" or "data" properties may have filegroup so it might happen that |
| 453 | // the relative root for each source path is different. |
| 454 | for _, path := range pathMappings { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 455 | var relativeRoot string |
| 456 | relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 457 | if v, found := relativeRootMap[relativeRoot]; found { |
| 458 | relativeRootMap[relativeRoot] = append(v, path.src) |
| 459 | } else { |
| 460 | relativeRootMap[relativeRoot] = android.Paths{path.src} |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | var keys []string |
| 465 | |
| 466 | // in order to keep stable order of soong_zip params, we sort the keys here. |
| 467 | for k := range relativeRootMap { |
| 468 | keys = append(keys, k) |
| 469 | } |
| 470 | sort.Strings(keys) |
| 471 | |
| 472 | for _, k := range keys { |
| 473 | // use relative root as filelist name. |
| 474 | fileListPath := registerBuildActionForModuleFileList( |
| 475 | ctx, strings.Replace(k, "/", "_", -1), relativeRootMap[k]) |
| 476 | ret.fileListSpecs = append(ret.fileListSpecs, |
| 477 | fileListSpec{fileList: fileListPath, relativeRoot: k}) |
| 478 | } |
| 479 | |
| 480 | return ret |
| 481 | } |
| 482 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 483 | func isPythonLibModule(module blueprint.Module) bool { |
| 484 | if m, ok := module.(*Module); ok { |
| 485 | // Python library has no bootstrapper or installer. |
| 486 | if m.bootstrapper != nil || m.installer != nil { |
| 487 | return false |
| 488 | } |
| 489 | return true |
| 490 | } |
| 491 | return false |
| 492 | } |
| 493 | |
| 494 | // check Python source/data files duplicates from current module and its whole dependencies. |
| 495 | func (p *Module) uniqWholeRunfilesTree(ctx android.ModuleContext) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 496 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to |
| 497 | // check duplicates. |
| 498 | destToPySrcs := make(map[string]string) |
| 499 | destToPyData := make(map[string]string) |
| 500 | |
| 501 | for _, path := range p.srcsPathMappings { |
| 502 | destToPySrcs[path.dest] = path.src.String() |
| 503 | } |
| 504 | for _, path := range p.dataPathMappings { |
| 505 | destToPyData[path.dest] = path.src.String() |
| 506 | } |
| 507 | |
| 508 | // visit all its dependencies in depth first. |
| 509 | ctx.VisitDepsDepthFirst(func(module blueprint.Module) { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 510 | if ctx.OtherModuleDependencyTag(module) != pythonLibTag { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 511 | return |
| 512 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 513 | // Python module cannot depend on modules, except for Python library. |
| 514 | if !isPythonLibModule(module) { |
| 515 | panic(fmt.Errorf( |
| 516 | "the dependency %q of module %q is not Python library!", |
| 517 | ctx.ModuleName(), ctx.OtherModuleName(module))) |
| 518 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 519 | if dep, ok := module.(PythonDependency); ok { |
| 520 | srcs := dep.GetSrcsPathMappings() |
| 521 | for _, path := range srcs { |
| 522 | if !fillInMap(ctx, destToPySrcs, |
| 523 | path.dest, path.src.String(), ctx.ModuleName(), |
| 524 | ctx.OtherModuleName(module)) { |
| 525 | continue |
| 526 | } |
| 527 | // binary needs the Python runfiles paths from all its |
| 528 | // dependencies to fill __init__.py in each runfiles dir. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 529 | p.depsPyRunfiles = append(p.depsPyRunfiles, path.dest) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 530 | } |
| 531 | data := dep.GetDataPathMappings() |
| 532 | for _, path := range data { |
| 533 | fillInMap(ctx, destToPyData, |
| 534 | path.dest, path.src.String(), ctx.ModuleName(), |
| 535 | ctx.OtherModuleName(module)) |
| 536 | } |
| 537 | // binary needs the soong_zip arguments from all its |
| 538 | // dependencies to generate executable par file. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame^] | 539 | p.depsParSpecs = append(p.depsParSpecs, dep.GetParSpec()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 540 | } |
| 541 | }) |
| 542 | } |
| 543 | |
| 544 | func fillInMap(ctx android.ModuleContext, m map[string]string, |
| 545 | key, value, curModule, otherModule string) bool { |
| 546 | if oldValue, found := m[key]; found { |
| 547 | ctx.ModuleErrorf("found two files to be placed at the same runfiles location %q."+ |
| 548 | " First file: in module %s at path %q."+ |
| 549 | " Second file: in module %s at path %q.", |
| 550 | key, curModule, oldValue, otherModule, value) |
| 551 | return false |
| 552 | } else { |
| 553 | m[key] = value |
| 554 | } |
| 555 | |
| 556 | return true |
| 557 | } |