blob: e63c26c4cefc70062ddbbe263f30875c88164c93 [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 Zhangd4e641b2017-07-12 12:55:28 -070068 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 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 Zhangd4e641b2017-07-12 12:55:28 -0700245func (p *Module) isEmbeddedLauncherEnabled(actual_version string) bool {
246 switch actual_version {
247 case pyVersion2:
248 return proptools.Bool(p.properties.Version.Py2.Embedded_launcher)
249 case pyVersion3:
250 return proptools.Bool(p.properties.Version.Py3.Embedded_launcher)
251 }
252
253 return false
254}
255
256func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800257 // deps from "data".
258 android.ExtractSourcesDeps(ctx, p.properties.Data)
259 // deps from "srcs".
260 android.ExtractSourcesDeps(ctx, p.properties.Srcs)
261
Nan Zhangd4e641b2017-07-12 12:55:28 -0700262 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800263 case pyVersion2:
264 // deps from "version.py2.srcs" property.
265 android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Srcs)
266
Nan Zhangd4e641b2017-07-12 12:55:28 -0700267 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800268 uniqueLibs(ctx, p.properties.Libs, "version.py2.libs",
269 p.properties.Version.Py2.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700270
271 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) {
272 ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib")
273 ctx.AddFarVariationDependencies([]blueprint.Variation{
274 {"arch", ctx.Target().String()},
275 }, launcherTag, "py2-launcher")
276 }
277
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800278 case pyVersion3:
279 // deps from "version.py3.srcs" property.
280 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs)
281
Nan Zhangd4e641b2017-07-12 12:55:28 -0700282 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800283 uniqueLibs(ctx, p.properties.Libs, "version.py3.libs",
284 p.properties.Version.Py3.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700285
286 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) {
287 //TODO(nanzhang): Add embedded launcher for Python3.
288 ctx.PropertyErrorf("version.py3.embedded_launcher",
289 "is not supported yet for Python3.")
290 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800291 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700292 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
293 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800294 }
295}
296
297// check "libs" duplicates from current module dependencies.
298func uniqueLibs(ctx android.BottomUpMutatorContext,
299 commonLibs []string, versionProp string, versionLibs []string) []string {
300 set := make(map[string]string)
301 ret := []string{}
302
303 // deps from "libs" property.
304 for _, l := range commonLibs {
305 if _, found := set[l]; found {
306 ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l)
307 } else {
308 set[l] = "libs"
309 ret = append(ret, l)
310 }
311 }
312 // deps from "version.pyX.libs" property.
313 for _, l := range versionLibs {
314 if _, found := set[l]; found {
315 ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l])
316 } else {
317 set[l] = versionProp
318 ret = append(ret, l)
319 }
320 }
321
322 return ret
323}
324
Nan Zhangd4e641b2017-07-12 12:55:28 -0700325func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
326 p.GeneratePythonBuildActions(ctx)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700327
Nan Zhangd4e641b2017-07-12 12:55:28 -0700328 if p.bootstrapper != nil {
329 // TODO(nanzhang): Since embedded launcher is not supported for Python3 for now,
330 // so we initialize "embedded_launcher" to false.
331 embedded_launcher := false
332 if p.properties.Actual_version == pyVersion2 {
333 embedded_launcher = p.isEmbeddedLauncherEnabled(pyVersion2)
334 }
335 p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
336 embedded_launcher, p.srcsPathMappings, p.parSpec, p.depsPyRunfiles,
337 p.depsParSpecs)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700338 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700339
340 if p.installer != nil && p.installSource.Valid() {
341 p.installer.install(ctx, p.installSource.Path())
342 }
343
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800344}
345
Nan Zhangd4e641b2017-07-12 12:55:28 -0700346func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800347 // expand python files from "srcs" property.
348 srcs := p.properties.Srcs
Nan Zhangd4e641b2017-07-12 12:55:28 -0700349 exclude_srcs := p.properties.Exclude_srcs
350 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800351 case pyVersion2:
352 srcs = append(srcs, p.properties.Version.Py2.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700353 exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800354 case pyVersion3:
355 srcs = append(srcs, p.properties.Version.Py3.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700356 exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800357 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700358 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
359 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800360 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700361 expandedSrcs := ctx.ExpandSources(srcs, exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800362 if len(expandedSrcs) == 0 {
363 ctx.ModuleErrorf("doesn't have any source files!")
364 }
365
366 // expand data files from "data" property.
367 expandedData := ctx.ExpandSources(p.properties.Data, nil)
368
369 // sanitize pkg_path.
370 pkg_path := p.properties.Pkg_path
371 if pkg_path != "" {
372 pkg_path = filepath.Clean(p.properties.Pkg_path)
373 if pkg_path == ".." || strings.HasPrefix(pkg_path, "../") ||
374 strings.HasPrefix(pkg_path, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700375 ctx.PropertyErrorf("pkg_path",
376 "%q must be a relative path contained in par file.",
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800377 p.properties.Pkg_path)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700378 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800379 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700380 if p.properties.Is_internal != nil && *p.properties.Is_internal {
381 // pkg_path starts from "internal/" implicitly.
382 pkg_path = filepath.Join(internal, pkg_path)
383 } else {
384 // pkg_path starts from "runfiles/" implicitly.
385 pkg_path = filepath.Join(runFiles, pkg_path)
386 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800387 } else {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700388 if p.properties.Is_internal != nil && *p.properties.Is_internal {
389 // pkg_path starts from "runfiles/" implicitly.
390 pkg_path = internal
391 } else {
392 // pkg_path starts from "runfiles/" implicitly.
393 pkg_path = runFiles
394 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800395 }
396
397 p.genModulePathMappings(ctx, pkg_path, expandedSrcs, expandedData)
398
399 p.parSpec = p.dumpFileList(ctx, pkg_path)
400
401 p.uniqWholeRunfilesTree(ctx)
402}
403
404// generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
405// for python/data files.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700406func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkg_path string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800407 expandedSrcs, expandedData android.Paths) {
408 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
409 // check duplicates.
410 destToPySrcs := make(map[string]string)
411 destToPyData := make(map[string]string)
412
413 for _, s := range expandedSrcs {
414 if s.Ext() != pyExt {
415 ctx.PropertyErrorf("srcs", "found non (.py) file: %q!", s.String())
416 continue
417 }
418 runfilesPath := filepath.Join(pkg_path, s.Rel())
419 identifiers := strings.Split(strings.TrimSuffix(runfilesPath, pyExt), "/")
420 for _, token := range identifiers {
421 if !pyIdentifierRegexp.MatchString(token) {
422 ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.",
423 runfilesPath, token)
424 }
425 }
426 if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
427 p.srcsPathMappings = append(p.srcsPathMappings,
428 pathMapping{dest: runfilesPath, src: s})
429 }
430 }
431
432 for _, d := range expandedData {
433 if d.Ext() == pyExt {
434 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
435 continue
436 }
437 runfilesPath := filepath.Join(pkg_path, d.Rel())
438 if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
439 p.dataPathMappings = append(p.dataPathMappings,
440 pathMapping{dest: runfilesPath, src: d})
441 }
442 }
443
444}
445
446// register build actions to dump filelist to disk.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700447func (p *Module) dumpFileList(ctx android.ModuleContext, pkg_path string) parSpec {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800448 relativeRootMap := make(map[string]android.Paths)
449 // the soong_zip params in order to pack current module's Python/data files.
450 ret := parSpec{rootPrefix: pkg_path}
451
452 pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
453
454 // "srcs" or "data" properties may have filegroup so it might happen that
455 // the relative root for each source path is different.
456 for _, path := range pathMappings {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700457 var relativeRoot string
458 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800459 if v, found := relativeRootMap[relativeRoot]; found {
460 relativeRootMap[relativeRoot] = append(v, path.src)
461 } else {
462 relativeRootMap[relativeRoot] = android.Paths{path.src}
463 }
464 }
465
466 var keys []string
467
468 // in order to keep stable order of soong_zip params, we sort the keys here.
469 for k := range relativeRootMap {
470 keys = append(keys, k)
471 }
472 sort.Strings(keys)
473
474 for _, k := range keys {
475 // use relative root as filelist name.
476 fileListPath := registerBuildActionForModuleFileList(
477 ctx, strings.Replace(k, "/", "_", -1), relativeRootMap[k])
478 ret.fileListSpecs = append(ret.fileListSpecs,
479 fileListSpec{fileList: fileListPath, relativeRoot: k})
480 }
481
482 return ret
483}
484
Nan Zhangd4e641b2017-07-12 12:55:28 -0700485func isPythonLibModule(module blueprint.Module) bool {
486 if m, ok := module.(*Module); ok {
487 // Python library has no bootstrapper or installer.
488 if m.bootstrapper != nil || m.installer != nil {
489 return false
490 }
491 return true
492 }
493 return false
494}
495
496// check Python source/data files duplicates from current module and its whole dependencies.
497func (p *Module) uniqWholeRunfilesTree(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800498 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
499 // check duplicates.
500 destToPySrcs := make(map[string]string)
501 destToPyData := make(map[string]string)
502
503 for _, path := range p.srcsPathMappings {
504 destToPySrcs[path.dest] = path.src.String()
505 }
506 for _, path := range p.dataPathMappings {
507 destToPyData[path.dest] = path.src.String()
508 }
509
510 // visit all its dependencies in depth first.
511 ctx.VisitDepsDepthFirst(func(module blueprint.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700512 if ctx.OtherModuleDependencyTag(module) != pythonLibTag {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800513 return
514 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700515 // Python module cannot depend on modules, except for Python library.
516 if !isPythonLibModule(module) {
517 panic(fmt.Errorf(
518 "the dependency %q of module %q is not Python library!",
519 ctx.ModuleName(), ctx.OtherModuleName(module)))
520 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800521 if dep, ok := module.(PythonDependency); ok {
522 srcs := dep.GetSrcsPathMappings()
523 for _, path := range srcs {
524 if !fillInMap(ctx, destToPySrcs,
525 path.dest, path.src.String(), ctx.ModuleName(),
526 ctx.OtherModuleName(module)) {
527 continue
528 }
529 // binary needs the Python runfiles paths from all its
530 // dependencies to fill __init__.py in each runfiles dir.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700531 p.depsPyRunfiles = append(p.depsPyRunfiles, path.dest)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800532 }
533 data := dep.GetDataPathMappings()
534 for _, path := range data {
535 fillInMap(ctx, destToPyData,
536 path.dest, path.src.String(), ctx.ModuleName(),
537 ctx.OtherModuleName(module))
538 }
539 // binary needs the soong_zip arguments from all its
540 // dependencies to generate executable par file.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700541 p.depsParSpecs = append(p.depsParSpecs, dep.GetParSpec())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800542 }
543 })
544}
545
546func fillInMap(ctx android.ModuleContext, m map[string]string,
547 key, value, curModule, otherModule string) bool {
548 if oldValue, found := m[key]; found {
549 ctx.ModuleErrorf("found two files to be placed at the same runfiles location %q."+
550 " First file: in module %s at path %q."+
551 " Second file: in module %s at path %q.",
552 key, curModule, oldValue, otherModule, value)
553 return false
554 } else {
555 m[key] = value
556 }
557
558 return true
559}