blob: 9a3b1bbc384cf38283878a5b39c7a332243fc0f9 [file] [log] [blame]
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001// 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
15package python
16
17// This file contains the "Base" module type for building Python program.
18
19import (
20 "fmt"
21 "path/filepath"
22 "regexp"
23 "sort"
24 "strings"
25
26 "github.com/google/blueprint"
Nan Zhangd4e641b2017-07-12 12:55:28 -070027 "github.com/google/blueprint/proptools"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080028
29 "android/soong/android"
30)
31
32func 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 Zhangd4e641b2017-07-12 12:55:28 -070039type VersionProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080040 // true, if the module is required to be built with this version.
Nan Zhangd4e641b2017-07-12 12:55:28 -070041 Enabled *bool `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080042
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 Zhangd4e641b2017-07-12 12:55:28 -070046 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 Zhangdb0b9a32017-02-27 10:12:13 -080051
52 // list of the Python libraries under this Python version.
Nan Zhangd4e641b2017-07-12 12:55:28 -070053 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 Zhangdb0b9a32017-02-27 10:12:13 -080058}
59
60// properties that apply to python libraries and binaries.
Nan Zhangd4e641b2017-07-12 12:55:28 -070061type BaseProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080062 // 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 Zhangea568a42017-11-08 21:20:04 -080068 Pkg_path *string `android:"arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070069
70 // true, if the Python module is used internally, eg, Python std libs.
71 Is_internal *bool `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080072
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 Zhangd4e641b2017-07-12 12:55:28 -070078 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 Zhangdb0b9a32017-02-27 10:12:13 -080083
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 Zhangd4e641b2017-07-12 12:55:28 -070086 Data []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080087
88 // list of the Python libraries compatible both with Python2 and Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -070089 Libs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080090
91 Version struct {
92 // all the "srcs" or Python dependencies that are to be used only for Python2.
Nan Zhangd4e641b2017-07-12 12:55:28 -070093 Py2 VersionProperties `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080094
95 // all the "srcs" or Python dependencies that are to be used only for Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -070096 Py3 VersionProperties `android:"arch_variant"`
97 } `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080098
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 Zhangd4e641b2017-07-12 12:55:28 -0700102 Actual_version string `blueprint:"mutated"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800103}
104
105type pathMapping struct {
106 dest string
107 src android.Path
108}
109
Nan Zhangd4e641b2017-07-12 12:55:28 -0700110type Module struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800111 android.ModuleBase
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700112 android.DefaultableModuleBase
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800113
Nan Zhangd4e641b2017-07-12 12:55:28 -0700114 properties BaseProperties
115
116 // initialize before calling Init
117 hod android.HostOrDeviceSupported
118 multilib android.Multilib
119
120 // the bootstrapper is used to bootstrap .par executable.
121 // bootstrapper might be nil (Python library module).
122 bootstrapper bootstrapper
123
124 // the installer might be nil.
125 installer installer
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800126
127 // the Python files of current module after expanding source dependencies.
128 // pathMapping: <dest: runfile_path, src: source_path>
129 srcsPathMappings []pathMapping
130
131 // the data files of current module after expanding source dependencies.
132 // pathMapping: <dest: runfile_path, src: source_path>
133 dataPathMappings []pathMapping
134
Nan Zhangd4e641b2017-07-12 12:55:28 -0700135 // soong_zip arguments of all its dependencies.
136 depsParSpecs []parSpec
137
138 // Python runfiles paths of all its dependencies.
139 depsPyRunfiles []string
140
141 // (.intermediate) module output path as installation source.
142 installSource android.OptionalPath
143
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800144 // the soong_zip arguments for zipping current module source/data files.
145 parSpec parSpec
Nan Zhang5323f8e2017-05-10 13:37:54 -0700146
Nan Zhang5323f8e2017-05-10 13:37:54 -0700147 subAndroidMkOnce map[subAndroidMkProvider]bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800148}
149
Nan Zhangd4e641b2017-07-12 12:55:28 -0700150func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
151 return &Module{
152 hod: hod,
153 multilib: multilib,
154 }
155}
156
157type bootstrapper interface {
158 bootstrapperProps() []interface{}
159 bootstrap(ctx android.ModuleContext, Actual_version string, embedded_launcher bool,
160 srcsPathMappings []pathMapping, parSpec parSpec,
161 depsPyRunfiles []string, depsParSpecs []parSpec) android.OptionalPath
162}
163
164type installer interface {
165 install(ctx android.ModuleContext, path android.Path)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800166}
167
168type PythonDependency interface {
169 GetSrcsPathMappings() []pathMapping
170 GetDataPathMappings() []pathMapping
171 GetParSpec() parSpec
172}
173
Nan Zhangd4e641b2017-07-12 12:55:28 -0700174func (p *Module) GetSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800175 return p.srcsPathMappings
176}
177
Nan Zhangd4e641b2017-07-12 12:55:28 -0700178func (p *Module) GetDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800179 return p.dataPathMappings
180}
181
Nan Zhangd4e641b2017-07-12 12:55:28 -0700182func (p *Module) GetParSpec() parSpec {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800183 return p.parSpec
184}
185
Nan Zhangd4e641b2017-07-12 12:55:28 -0700186var _ PythonDependency = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800187
Nan Zhangd4e641b2017-07-12 12:55:28 -0700188var _ android.AndroidMkDataProvider = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800189
Nan Zhangd4e641b2017-07-12 12:55:28 -0700190func (p *Module) Init() android.Module {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800191
Nan Zhangd4e641b2017-07-12 12:55:28 -0700192 p.AddProperties(&p.properties)
193 if p.bootstrapper != nil {
194 p.AddProperties(p.bootstrapper.bootstrapperProps()...)
195 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800196
Nan Zhangd4e641b2017-07-12 12:55:28 -0700197 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700198 android.InitDefaultableModule(p)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800199
Nan Zhangd4e641b2017-07-12 12:55:28 -0700200 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800201}
202
Nan Zhangd4e641b2017-07-12 12:55:28 -0700203type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800204 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700205 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800206}
207
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800208var (
Nan Zhangd4e641b2017-07-12 12:55:28 -0700209 pythonLibTag = dependencyTag{name: "pythonLib"}
210 launcherTag = dependencyTag{name: "launcher"}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800211 pyIdentifierRegexp = regexp.MustCompile(`^([a-z]|[A-Z]|_)([a-z]|[A-Z]|[0-9]|_)*$`)
212 pyExt = ".py"
213 pyVersion2 = "PY2"
214 pyVersion3 = "PY3"
215 initFileName = "__init__.py"
216 mainFileName = "__main__.py"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700217 entryPointFile = "entry_point.txt"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800218 parFileExt = ".zip"
219 runFiles = "runfiles"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700220 internal = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800221)
222
223// create version variants for modules.
224func versionSplitMutator() func(android.BottomUpMutatorContext) {
225 return func(mctx android.BottomUpMutatorContext) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700226 if base, ok := mctx.Module().(*Module); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800227 versionNames := []string{}
228 if base.properties.Version.Py2.Enabled != nil &&
229 *(base.properties.Version.Py2.Enabled) == true {
230 versionNames = append(versionNames, pyVersion2)
231 }
232 if !(base.properties.Version.Py3.Enabled != nil &&
233 *(base.properties.Version.Py3.Enabled) == false) {
234 versionNames = append(versionNames, pyVersion3)
235 }
236 modules := mctx.CreateVariations(versionNames...)
237 for i, v := range versionNames {
238 // set the actual version for Python module.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700239 modules[i].(*Module).properties.Actual_version = v
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800240 }
241 }
242 }
243}
244
Nan Zhangb8bdacf2017-12-06 15:13:10 -0800245func (p *Module) HostToolPath() android.OptionalPath {
246 if p.installer == nil {
247 // python_library is just meta module, and doesn't have any installer.
248 return android.OptionalPath{}
249 }
250 return android.OptionalPathForPath(p.installer.(*binaryDecorator).path)
251}
252
Nan Zhangd4e641b2017-07-12 12:55:28 -0700253func (p *Module) isEmbeddedLauncherEnabled(actual_version string) bool {
254 switch actual_version {
255 case pyVersion2:
256 return proptools.Bool(p.properties.Version.Py2.Embedded_launcher)
257 case pyVersion3:
258 return proptools.Bool(p.properties.Version.Py3.Embedded_launcher)
259 }
260
261 return false
262}
263
264func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800265 // deps from "data".
266 android.ExtractSourcesDeps(ctx, p.properties.Data)
267 // deps from "srcs".
268 android.ExtractSourcesDeps(ctx, p.properties.Srcs)
269
Nan Zhangd4e641b2017-07-12 12:55:28 -0700270 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800271 case pyVersion2:
272 // deps from "version.py2.srcs" property.
273 android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Srcs)
274
Nan Zhangd4e641b2017-07-12 12:55:28 -0700275 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800276 uniqueLibs(ctx, p.properties.Libs, "version.py2.libs",
277 p.properties.Version.Py2.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700278
279 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) {
280 ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib")
281 ctx.AddFarVariationDependencies([]blueprint.Variation{
282 {"arch", ctx.Target().String()},
283 }, launcherTag, "py2-launcher")
284 }
285
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800286 case pyVersion3:
287 // deps from "version.py3.srcs" property.
288 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs)
289
Nan Zhangd4e641b2017-07-12 12:55:28 -0700290 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800291 uniqueLibs(ctx, p.properties.Libs, "version.py3.libs",
292 p.properties.Version.Py3.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700293
294 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) {
295 //TODO(nanzhang): Add embedded launcher for Python3.
296 ctx.PropertyErrorf("version.py3.embedded_launcher",
297 "is not supported yet for Python3.")
298 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800299 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700300 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
301 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800302 }
303}
304
305// check "libs" duplicates from current module dependencies.
306func uniqueLibs(ctx android.BottomUpMutatorContext,
307 commonLibs []string, versionProp string, versionLibs []string) []string {
308 set := make(map[string]string)
309 ret := []string{}
310
311 // deps from "libs" property.
312 for _, l := range commonLibs {
313 if _, found := set[l]; found {
314 ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l)
315 } else {
316 set[l] = "libs"
317 ret = append(ret, l)
318 }
319 }
320 // deps from "version.pyX.libs" property.
321 for _, l := range versionLibs {
322 if _, found := set[l]; found {
323 ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l])
324 } else {
325 set[l] = versionProp
326 ret = append(ret, l)
327 }
328 }
329
330 return ret
331}
332
Nan Zhangd4e641b2017-07-12 12:55:28 -0700333func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
334 p.GeneratePythonBuildActions(ctx)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700335
Nan Zhangd4e641b2017-07-12 12:55:28 -0700336 if p.bootstrapper != nil {
337 // TODO(nanzhang): Since embedded launcher is not supported for Python3 for now,
338 // so we initialize "embedded_launcher" to false.
339 embedded_launcher := false
340 if p.properties.Actual_version == pyVersion2 {
341 embedded_launcher = p.isEmbeddedLauncherEnabled(pyVersion2)
342 }
343 p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
344 embedded_launcher, p.srcsPathMappings, p.parSpec, p.depsPyRunfiles,
345 p.depsParSpecs)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700346 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700347
348 if p.installer != nil && p.installSource.Valid() {
349 p.installer.install(ctx, p.installSource.Path())
350 }
351
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800352}
353
Nan Zhangd4e641b2017-07-12 12:55:28 -0700354func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800355 // expand python files from "srcs" property.
356 srcs := p.properties.Srcs
Nan Zhangd4e641b2017-07-12 12:55:28 -0700357 exclude_srcs := p.properties.Exclude_srcs
358 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800359 case pyVersion2:
360 srcs = append(srcs, p.properties.Version.Py2.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700361 exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800362 case pyVersion3:
363 srcs = append(srcs, p.properties.Version.Py3.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700364 exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800365 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700366 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
367 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800368 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700369 expandedSrcs := ctx.ExpandSources(srcs, exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800370 if len(expandedSrcs) == 0 {
371 ctx.ModuleErrorf("doesn't have any source files!")
372 }
373
374 // expand data files from "data" property.
375 expandedData := ctx.ExpandSources(p.properties.Data, nil)
376
377 // sanitize pkg_path.
Nan Zhangea568a42017-11-08 21:20:04 -0800378 pkg_path := String(p.properties.Pkg_path)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800379 if pkg_path != "" {
Nan Zhangea568a42017-11-08 21:20:04 -0800380 pkg_path = filepath.Clean(String(p.properties.Pkg_path))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800381 if pkg_path == ".." || strings.HasPrefix(pkg_path, "../") ||
382 strings.HasPrefix(pkg_path, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700383 ctx.PropertyErrorf("pkg_path",
384 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800385 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700386 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800387 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700388 if p.properties.Is_internal != nil && *p.properties.Is_internal {
389 // pkg_path starts from "internal/" implicitly.
390 pkg_path = filepath.Join(internal, pkg_path)
391 } else {
392 // pkg_path starts from "runfiles/" implicitly.
393 pkg_path = filepath.Join(runFiles, pkg_path)
394 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800395 } else {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700396 if p.properties.Is_internal != nil && *p.properties.Is_internal {
397 // pkg_path starts from "runfiles/" implicitly.
398 pkg_path = internal
399 } else {
400 // pkg_path starts from "runfiles/" implicitly.
401 pkg_path = runFiles
402 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800403 }
404
405 p.genModulePathMappings(ctx, pkg_path, expandedSrcs, expandedData)
406
407 p.parSpec = p.dumpFileList(ctx, pkg_path)
408
409 p.uniqWholeRunfilesTree(ctx)
410}
411
412// generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
413// for python/data files.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700414func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkg_path string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800415 expandedSrcs, expandedData android.Paths) {
416 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
417 // check duplicates.
418 destToPySrcs := make(map[string]string)
419 destToPyData := make(map[string]string)
420
421 for _, s := range expandedSrcs {
422 if s.Ext() != pyExt {
423 ctx.PropertyErrorf("srcs", "found non (.py) file: %q!", s.String())
424 continue
425 }
426 runfilesPath := filepath.Join(pkg_path, s.Rel())
427 identifiers := strings.Split(strings.TrimSuffix(runfilesPath, pyExt), "/")
428 for _, token := range identifiers {
429 if !pyIdentifierRegexp.MatchString(token) {
430 ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.",
431 runfilesPath, token)
432 }
433 }
434 if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
435 p.srcsPathMappings = append(p.srcsPathMappings,
436 pathMapping{dest: runfilesPath, src: s})
437 }
438 }
439
440 for _, d := range expandedData {
441 if d.Ext() == pyExt {
442 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
443 continue
444 }
445 runfilesPath := filepath.Join(pkg_path, d.Rel())
446 if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
447 p.dataPathMappings = append(p.dataPathMappings,
448 pathMapping{dest: runfilesPath, src: d})
449 }
450 }
451
452}
453
454// register build actions to dump filelist to disk.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700455func (p *Module) dumpFileList(ctx android.ModuleContext, pkg_path string) parSpec {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800456 relativeRootMap := make(map[string]android.Paths)
457 // the soong_zip params in order to pack current module's Python/data files.
458 ret := parSpec{rootPrefix: pkg_path}
459
460 pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
461
462 // "srcs" or "data" properties may have filegroup so it might happen that
463 // the relative root for each source path is different.
464 for _, path := range pathMappings {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700465 var relativeRoot string
466 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800467 if v, found := relativeRootMap[relativeRoot]; found {
468 relativeRootMap[relativeRoot] = append(v, path.src)
469 } else {
470 relativeRootMap[relativeRoot] = android.Paths{path.src}
471 }
472 }
473
474 var keys []string
475
476 // in order to keep stable order of soong_zip params, we sort the keys here.
477 for k := range relativeRootMap {
478 keys = append(keys, k)
479 }
480 sort.Strings(keys)
481
482 for _, k := range keys {
483 // use relative root as filelist name.
484 fileListPath := registerBuildActionForModuleFileList(
485 ctx, strings.Replace(k, "/", "_", -1), relativeRootMap[k])
486 ret.fileListSpecs = append(ret.fileListSpecs,
487 fileListSpec{fileList: fileListPath, relativeRoot: k})
488 }
489
490 return ret
491}
492
Nan Zhangd4e641b2017-07-12 12:55:28 -0700493func isPythonLibModule(module blueprint.Module) bool {
494 if m, ok := module.(*Module); ok {
495 // Python library has no bootstrapper or installer.
496 if m.bootstrapper != nil || m.installer != nil {
497 return false
498 }
499 return true
500 }
501 return false
502}
503
504// check Python source/data files duplicates from current module and its whole dependencies.
505func (p *Module) uniqWholeRunfilesTree(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800506 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
507 // check duplicates.
508 destToPySrcs := make(map[string]string)
509 destToPyData := make(map[string]string)
510
511 for _, path := range p.srcsPathMappings {
512 destToPySrcs[path.dest] = path.src.String()
513 }
514 for _, path := range p.dataPathMappings {
515 destToPyData[path.dest] = path.src.String()
516 }
517
518 // visit all its dependencies in depth first.
Colin Crossd11fcda2017-10-23 17:59:01 -0700519 ctx.VisitDepsDepthFirst(func(module android.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700520 if ctx.OtherModuleDependencyTag(module) != pythonLibTag {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800521 return
522 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700523 // Python module cannot depend on modules, except for Python library.
524 if !isPythonLibModule(module) {
525 panic(fmt.Errorf(
526 "the dependency %q of module %q is not Python library!",
527 ctx.ModuleName(), ctx.OtherModuleName(module)))
528 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800529 if dep, ok := module.(PythonDependency); ok {
530 srcs := dep.GetSrcsPathMappings()
531 for _, path := range srcs {
532 if !fillInMap(ctx, destToPySrcs,
533 path.dest, path.src.String(), ctx.ModuleName(),
534 ctx.OtherModuleName(module)) {
535 continue
536 }
537 // binary needs the Python runfiles paths from all its
538 // dependencies to fill __init__.py in each runfiles dir.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700539 p.depsPyRunfiles = append(p.depsPyRunfiles, path.dest)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800540 }
541 data := dep.GetDataPathMappings()
542 for _, path := range data {
543 fillInMap(ctx, destToPyData,
544 path.dest, path.src.String(), ctx.ModuleName(),
545 ctx.OtherModuleName(module))
546 }
547 // binary needs the soong_zip arguments from all its
548 // dependencies to generate executable par file.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700549 p.depsParSpecs = append(p.depsParSpecs, dep.GetParSpec())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800550 }
551 })
552}
553
554func fillInMap(ctx android.ModuleContext, m map[string]string,
555 key, value, curModule, otherModule string) bool {
556 if oldValue, found := m[key]; found {
557 ctx.ModuleErrorf("found two files to be placed at the same runfiles location %q."+
558 " First file: in module %s at path %q."+
559 " Second file: in module %s at path %q.",
560 key, curModule, oldValue, otherModule, value)
561 return false
562 } else {
563 m[key] = value
564 }
565
566 return true
567}
Nan Zhangea568a42017-11-08 21:20:04 -0800568
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000569func (p *Module) InstallInData() bool {
570 return true
571}
572
Nan Zhangea568a42017-11-08 21:20:04 -0800573var Bool = proptools.Bool
574var String = proptools.String