blob: d462af124e88cbd253002bbfcebeb428e4e8dd9b [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 Zhangdb0b9a32017-02-27 10:12:13 -0800112
Nan Zhangd4e641b2017-07-12 12:55:28 -0700113 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 Zhangdb0b9a32017-02-27 10:12:13 -0800125
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 Zhangd4e641b2017-07-12 12:55:28 -0700134 // 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 Zhangdb0b9a32017-02-27 10:12:13 -0800143 // the soong_zip arguments for zipping current module source/data files.
144 parSpec parSpec
Nan Zhang5323f8e2017-05-10 13:37:54 -0700145
Nan Zhang5323f8e2017-05-10 13:37:54 -0700146 subAndroidMkOnce map[subAndroidMkProvider]bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800147}
148
Nan Zhangd4e641b2017-07-12 12:55:28 -0700149func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
150 return &Module{
151 hod: hod,
152 multilib: multilib,
153 }
154}
155
156type 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
163type installer interface {
164 install(ctx android.ModuleContext, path android.Path)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800165}
166
167type PythonDependency interface {
168 GetSrcsPathMappings() []pathMapping
169 GetDataPathMappings() []pathMapping
170 GetParSpec() parSpec
171}
172
Nan Zhangd4e641b2017-07-12 12:55:28 -0700173func (p *Module) GetSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800174 return p.srcsPathMappings
175}
176
Nan Zhangd4e641b2017-07-12 12:55:28 -0700177func (p *Module) GetDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800178 return p.dataPathMappings
179}
180
Nan Zhangd4e641b2017-07-12 12:55:28 -0700181func (p *Module) GetParSpec() parSpec {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800182 return p.parSpec
183}
184
Nan Zhangd4e641b2017-07-12 12:55:28 -0700185var _ PythonDependency = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800186
Nan Zhangd4e641b2017-07-12 12:55:28 -0700187var _ android.AndroidMkDataProvider = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800188
Nan Zhangd4e641b2017-07-12 12:55:28 -0700189func (p *Module) Init() android.Module {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800190
Nan Zhangd4e641b2017-07-12 12:55:28 -0700191 p.AddProperties(&p.properties)
192 if p.bootstrapper != nil {
193 p.AddProperties(p.bootstrapper.bootstrapperProps()...)
194 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800195
Nan Zhangd4e641b2017-07-12 12:55:28 -0700196 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800197
Nan Zhangd4e641b2017-07-12 12:55:28 -0700198 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800199}
200
Nan Zhangd4e641b2017-07-12 12:55:28 -0700201type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800202 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700203 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800204}
205
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800206var (
Nan Zhangd4e641b2017-07-12 12:55:28 -0700207 pythonLibTag = dependencyTag{name: "pythonLib"}
208 launcherTag = dependencyTag{name: "launcher"}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800209 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 Zhangd4e641b2017-07-12 12:55:28 -0700215 entryPointFile = "entry_point.txt"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800216 parFileExt = ".zip"
217 runFiles = "runfiles"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700218 internal = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800219)
220
221// create version variants for modules.
222func versionSplitMutator() func(android.BottomUpMutatorContext) {
223 return func(mctx android.BottomUpMutatorContext) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700224 if base, ok := mctx.Module().(*Module); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800225 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 Zhangd4e641b2017-07-12 12:55:28 -0700237 modules[i].(*Module).properties.Actual_version = v
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800238 }
239 }
240 }
241}
242
Nan Zhangd4e641b2017-07-12 12:55:28 -0700243func (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
254func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800255 // deps from "data".
256 android.ExtractSourcesDeps(ctx, p.properties.Data)
257 // deps from "srcs".
258 android.ExtractSourcesDeps(ctx, p.properties.Srcs)
259
Nan Zhangd4e641b2017-07-12 12:55:28 -0700260 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800261 case pyVersion2:
262 // deps from "version.py2.srcs" property.
263 android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Srcs)
264
Nan Zhangd4e641b2017-07-12 12:55:28 -0700265 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800266 uniqueLibs(ctx, p.properties.Libs, "version.py2.libs",
267 p.properties.Version.Py2.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700268
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 Zhangdb0b9a32017-02-27 10:12:13 -0800276 case pyVersion3:
277 // deps from "version.py3.srcs" property.
278 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs)
279
Nan Zhangd4e641b2017-07-12 12:55:28 -0700280 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800281 uniqueLibs(ctx, p.properties.Libs, "version.py3.libs",
282 p.properties.Version.Py3.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700283
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 Zhangdb0b9a32017-02-27 10:12:13 -0800289 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700290 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
291 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800292 }
293}
294
295// check "libs" duplicates from current module dependencies.
296func 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 Zhangd4e641b2017-07-12 12:55:28 -0700323func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
324 p.GeneratePythonBuildActions(ctx)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700325
Nan Zhangd4e641b2017-07-12 12:55:28 -0700326 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 Zhang5323f8e2017-05-10 13:37:54 -0700336 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700337
338 if p.installer != nil && p.installSource.Valid() {
339 p.installer.install(ctx, p.installSource.Path())
340 }
341
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800342}
343
Nan Zhangd4e641b2017-07-12 12:55:28 -0700344func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800345 // expand python files from "srcs" property.
346 srcs := p.properties.Srcs
Nan Zhangd4e641b2017-07-12 12:55:28 -0700347 exclude_srcs := p.properties.Exclude_srcs
348 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800349 case pyVersion2:
350 srcs = append(srcs, p.properties.Version.Py2.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700351 exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800352 case pyVersion3:
353 srcs = append(srcs, p.properties.Version.Py3.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700354 exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800355 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700356 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
357 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800358 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700359 expandedSrcs := ctx.ExpandSources(srcs, exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800360 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 Zhangd4e641b2017-07-12 12:55:28 -0700373 ctx.PropertyErrorf("pkg_path",
374 "%q must be a relative path contained in par file.",
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800375 p.properties.Pkg_path)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700376 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800377 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700378 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 Zhangdb0b9a32017-02-27 10:12:13 -0800385 } else {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700386 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 Zhangdb0b9a32017-02-27 10:12:13 -0800393 }
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 Zhangd4e641b2017-07-12 12:55:28 -0700404func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkg_path string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800405 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 Zhangd4e641b2017-07-12 12:55:28 -0700445func (p *Module) dumpFileList(ctx android.ModuleContext, pkg_path string) parSpec {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800446 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 Zhangd4e641b2017-07-12 12:55:28 -0700455 var relativeRoot string
456 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800457 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 Zhangd4e641b2017-07-12 12:55:28 -0700483func 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.
495func (p *Module) uniqWholeRunfilesTree(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800496 // 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 Zhangd4e641b2017-07-12 12:55:28 -0700510 if ctx.OtherModuleDependencyTag(module) != pythonLibTag {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800511 return
512 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700513 // 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 Zhangdb0b9a32017-02-27 10:12:13 -0800519 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 Zhangd4e641b2017-07-12 12:55:28 -0700529 p.depsPyRunfiles = append(p.depsPyRunfiles, path.dest)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800530 }
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 Zhangd4e641b2017-07-12 12:55:28 -0700539 p.depsParSpecs = append(p.depsParSpecs, dep.GetParSpec())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800540 }
541 })
542}
543
544func 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}