blob: 7626d094e954c0fda4e377c8f6e09e550ba287ab [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 Zhang1db85402017-12-18 13:20:23 -0800135 // the zip filepath for zipping current module source/data files.
136 srcsZip android.Path
Nan Zhangd4e641b2017-07-12 12:55:28 -0700137
Nan Zhang1db85402017-12-18 13:20:23 -0800138 // dependency modules' zip filepath for zipping current module source/data files.
139 depsSrcsZips android.Paths
Nan Zhangd4e641b2017-07-12 12:55:28 -0700140
141 // (.intermediate) module output path as installation source.
142 installSource android.OptionalPath
143
Nan Zhang5323f8e2017-05-10 13:37:54 -0700144 subAndroidMkOnce map[subAndroidMkProvider]bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800145}
146
Nan Zhangd4e641b2017-07-12 12:55:28 -0700147func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
148 return &Module{
149 hod: hod,
150 multilib: multilib,
151 }
152}
153
154type bootstrapper interface {
155 bootstrapperProps() []interface{}
Nan Zhang1db85402017-12-18 13:20:23 -0800156 bootstrap(ctx android.ModuleContext, ActualVersion string, embeddedLauncher bool,
157 srcsPathMappings []pathMapping, srcsZip android.Path,
158 depsSrcsZips android.Paths) android.OptionalPath
Nan Zhangd4e641b2017-07-12 12:55:28 -0700159}
160
161type installer interface {
162 install(ctx android.ModuleContext, path android.Path)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800163}
164
165type PythonDependency interface {
166 GetSrcsPathMappings() []pathMapping
167 GetDataPathMappings() []pathMapping
Nan Zhang1db85402017-12-18 13:20:23 -0800168 GetSrcsZip() android.Path
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800169}
170
Nan Zhangd4e641b2017-07-12 12:55:28 -0700171func (p *Module) GetSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800172 return p.srcsPathMappings
173}
174
Nan Zhangd4e641b2017-07-12 12:55:28 -0700175func (p *Module) GetDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800176 return p.dataPathMappings
177}
178
Nan Zhang1db85402017-12-18 13:20:23 -0800179func (p *Module) GetSrcsZip() android.Path {
180 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800181}
182
Nan Zhangd4e641b2017-07-12 12:55:28 -0700183var _ PythonDependency = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800184
Nan Zhangd4e641b2017-07-12 12:55:28 -0700185var _ android.AndroidMkDataProvider = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800186
Nan Zhangd4e641b2017-07-12 12:55:28 -0700187func (p *Module) Init() android.Module {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800188
Nan Zhangd4e641b2017-07-12 12:55:28 -0700189 p.AddProperties(&p.properties)
190 if p.bootstrapper != nil {
191 p.AddProperties(p.bootstrapper.bootstrapperProps()...)
192 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800193
Nan Zhangd4e641b2017-07-12 12:55:28 -0700194 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700195 android.InitDefaultableModule(p)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800196
Nan Zhangd4e641b2017-07-12 12:55:28 -0700197 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800198}
199
Nan Zhangd4e641b2017-07-12 12:55:28 -0700200type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800201 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700202 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800203}
204
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800205var (
Nan Zhangd4e641b2017-07-12 12:55:28 -0700206 pythonLibTag = dependencyTag{name: "pythonLib"}
207 launcherTag = dependencyTag{name: "launcher"}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800208 pyIdentifierRegexp = regexp.MustCompile(`^([a-z]|[A-Z]|_)([a-z]|[A-Z]|[0-9]|_)*$`)
209 pyExt = ".py"
210 pyVersion2 = "PY2"
211 pyVersion3 = "PY3"
212 initFileName = "__init__.py"
213 mainFileName = "__main__.py"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700214 entryPointFile = "entry_point.txt"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800215 parFileExt = ".zip"
216 runFiles = "runfiles"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700217 internal = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800218)
219
220// create version variants for modules.
221func versionSplitMutator() func(android.BottomUpMutatorContext) {
222 return func(mctx android.BottomUpMutatorContext) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700223 if base, ok := mctx.Module().(*Module); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800224 versionNames := []string{}
225 if base.properties.Version.Py2.Enabled != nil &&
226 *(base.properties.Version.Py2.Enabled) == true {
227 versionNames = append(versionNames, pyVersion2)
228 }
229 if !(base.properties.Version.Py3.Enabled != nil &&
230 *(base.properties.Version.Py3.Enabled) == false) {
231 versionNames = append(versionNames, pyVersion3)
232 }
233 modules := mctx.CreateVariations(versionNames...)
234 for i, v := range versionNames {
235 // set the actual version for Python module.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700236 modules[i].(*Module).properties.Actual_version = v
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800237 }
238 }
239 }
240}
241
Nan Zhangb8bdacf2017-12-06 15:13:10 -0800242func (p *Module) HostToolPath() android.OptionalPath {
243 if p.installer == nil {
244 // python_library is just meta module, and doesn't have any installer.
245 return android.OptionalPath{}
246 }
247 return android.OptionalPathForPath(p.installer.(*binaryDecorator).path)
248}
249
Nan Zhangd4e641b2017-07-12 12:55:28 -0700250func (p *Module) isEmbeddedLauncherEnabled(actual_version string) bool {
251 switch actual_version {
252 case pyVersion2:
253 return proptools.Bool(p.properties.Version.Py2.Embedded_launcher)
254 case pyVersion3:
255 return proptools.Bool(p.properties.Version.Py3.Embedded_launcher)
256 }
257
258 return false
259}
260
261func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800262 // deps from "data".
263 android.ExtractSourcesDeps(ctx, p.properties.Data)
264 // deps from "srcs".
265 android.ExtractSourcesDeps(ctx, p.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000266 android.ExtractSourcesDeps(ctx, p.properties.Exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800267
Nan Zhangd4e641b2017-07-12 12:55:28 -0700268 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800269 case pyVersion2:
270 // deps from "version.py2.srcs" property.
271 android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000272 android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800273
Nan Zhangd4e641b2017-07-12 12:55:28 -0700274 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800275 uniqueLibs(ctx, p.properties.Libs, "version.py2.libs",
276 p.properties.Version.Py2.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700277
278 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) {
279 ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib")
280 ctx.AddFarVariationDependencies([]blueprint.Variation{
281 {"arch", ctx.Target().String()},
282 }, launcherTag, "py2-launcher")
283 }
284
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800285 case pyVersion3:
286 // deps from "version.py3.srcs" property.
287 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000288 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800289
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.
Nan Zhang1db85402017-12-18 13:20:23 -0800339 embeddedLauncher := false
Nan Zhangd4e641b2017-07-12 12:55:28 -0700340 if p.properties.Actual_version == pyVersion2 {
Nan Zhang1db85402017-12-18 13:20:23 -0800341 embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion2)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700342 }
343 p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
Nan Zhang1db85402017-12-18 13:20:23 -0800344 embeddedLauncher, p.srcsPathMappings, p.srcsZip, p.depsSrcsZips)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700345 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700346
347 if p.installer != nil && p.installSource.Valid() {
348 p.installer.install(ctx, p.installSource.Path())
349 }
350
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800351}
352
Nan Zhangd4e641b2017-07-12 12:55:28 -0700353func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800354 // expand python files from "srcs" property.
355 srcs := p.properties.Srcs
Nan Zhangd4e641b2017-07-12 12:55:28 -0700356 exclude_srcs := p.properties.Exclude_srcs
357 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800358 case pyVersion2:
359 srcs = append(srcs, p.properties.Version.Py2.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700360 exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800361 case pyVersion3:
362 srcs = append(srcs, p.properties.Version.Py3.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700363 exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800364 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700365 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
366 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800367 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700368 expandedSrcs := ctx.ExpandSources(srcs, exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800369 if len(expandedSrcs) == 0 {
370 ctx.ModuleErrorf("doesn't have any source files!")
371 }
372
373 // expand data files from "data" property.
374 expandedData := ctx.ExpandSources(p.properties.Data, nil)
375
376 // sanitize pkg_path.
Nan Zhang1db85402017-12-18 13:20:23 -0800377 pkgPath := String(p.properties.Pkg_path)
378 if pkgPath != "" {
379 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
380 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
381 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700382 ctx.PropertyErrorf("pkg_path",
383 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800384 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700385 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800386 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700387 if p.properties.Is_internal != nil && *p.properties.Is_internal {
388 // pkg_path starts from "internal/" implicitly.
Nan Zhang1db85402017-12-18 13:20:23 -0800389 pkgPath = filepath.Join(internal, pkgPath)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700390 } else {
391 // pkg_path starts from "runfiles/" implicitly.
Nan Zhang1db85402017-12-18 13:20:23 -0800392 pkgPath = filepath.Join(runFiles, pkgPath)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700393 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800394 } else {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700395 if p.properties.Is_internal != nil && *p.properties.Is_internal {
396 // pkg_path starts from "runfiles/" implicitly.
Nan Zhang1db85402017-12-18 13:20:23 -0800397 pkgPath = internal
Nan Zhangd4e641b2017-07-12 12:55:28 -0700398 } else {
399 // pkg_path starts from "runfiles/" implicitly.
Nan Zhang1db85402017-12-18 13:20:23 -0800400 pkgPath = runFiles
Nan Zhangd4e641b2017-07-12 12:55:28 -0700401 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800402 }
403
Nan Zhang1db85402017-12-18 13:20:23 -0800404 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800405
406 p.uniqWholeRunfilesTree(ctx)
Nan Zhang1db85402017-12-18 13:20:23 -0800407
408 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800409}
410
411// generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
412// for python/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800413func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800414 expandedSrcs, expandedData android.Paths) {
415 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
416 // check duplicates.
417 destToPySrcs := make(map[string]string)
418 destToPyData := make(map[string]string)
419
420 for _, s := range expandedSrcs {
421 if s.Ext() != pyExt {
422 ctx.PropertyErrorf("srcs", "found non (.py) file: %q!", s.String())
423 continue
424 }
Nan Zhang1db85402017-12-18 13:20:23 -0800425 runfilesPath := filepath.Join(pkgPath, s.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800426 identifiers := strings.Split(strings.TrimSuffix(runfilesPath, pyExt), "/")
427 for _, token := range identifiers {
428 if !pyIdentifierRegexp.MatchString(token) {
429 ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.",
430 runfilesPath, token)
431 }
432 }
433 if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
434 p.srcsPathMappings = append(p.srcsPathMappings,
435 pathMapping{dest: runfilesPath, src: s})
436 }
437 }
438
439 for _, d := range expandedData {
440 if d.Ext() == pyExt {
441 ctx.PropertyErrorf("data", "found (.py) file: %q!", d.String())
442 continue
443 }
Nan Zhang1db85402017-12-18 13:20:23 -0800444 runfilesPath := filepath.Join(pkgPath, d.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800445 if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
446 p.dataPathMappings = append(p.dataPathMappings,
447 pathMapping{dest: runfilesPath, src: d})
448 }
449 }
450
451}
452
Nan Zhang1db85402017-12-18 13:20:23 -0800453// register build actions to zip current module's sources.
454func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800455 relativeRootMap := make(map[string]android.Paths)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800456 pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
457
458 // "srcs" or "data" properties may have filegroup so it might happen that
459 // the relative root for each source path is different.
460 for _, path := range pathMappings {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700461 var relativeRoot string
462 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800463 if v, found := relativeRootMap[relativeRoot]; found {
464 relativeRootMap[relativeRoot] = append(v, path.src)
465 } else {
466 relativeRootMap[relativeRoot] = android.Paths{path.src}
467 }
468 }
469
470 var keys []string
471
472 // in order to keep stable order of soong_zip params, we sort the keys here.
473 for k := range relativeRootMap {
474 keys = append(keys, k)
475 }
476 sort.Strings(keys)
477
Nan Zhang1db85402017-12-18 13:20:23 -0800478 parArgs := []string{}
479 parArgs = append(parArgs, `-P `+pkgPath)
480 implicits := android.Paths{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800481 for _, k := range keys {
Nan Zhang1db85402017-12-18 13:20:23 -0800482 parArgs = append(parArgs, `-C `+k)
483 for _, path := range relativeRootMap[k] {
484 parArgs = append(parArgs, `-f `+path.String())
485 implicits = append(implicits, path)
486 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800487 }
488
Nan Zhang1db85402017-12-18 13:20:23 -0800489 srcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".zip")
490 ctx.Build(pctx, android.BuildParams{
491 Rule: zip,
492 Description: "python library archive",
493 Output: srcsZip,
494 Implicits: implicits,
495 Args: map[string]string{
496 "args": strings.Join(parArgs, " "),
497 },
498 })
499
500 return srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800501}
502
Nan Zhangd4e641b2017-07-12 12:55:28 -0700503func isPythonLibModule(module blueprint.Module) bool {
504 if m, ok := module.(*Module); ok {
505 // Python library has no bootstrapper or installer.
506 if m.bootstrapper != nil || m.installer != nil {
507 return false
508 }
509 return true
510 }
511 return false
512}
513
514// check Python source/data files duplicates from current module and its whole dependencies.
515func (p *Module) uniqWholeRunfilesTree(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800516 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
517 // check duplicates.
518 destToPySrcs := make(map[string]string)
519 destToPyData := make(map[string]string)
520
521 for _, path := range p.srcsPathMappings {
522 destToPySrcs[path.dest] = path.src.String()
523 }
524 for _, path := range p.dataPathMappings {
525 destToPyData[path.dest] = path.src.String()
526 }
527
528 // visit all its dependencies in depth first.
Colin Crossd11fcda2017-10-23 17:59:01 -0700529 ctx.VisitDepsDepthFirst(func(module android.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700530 if ctx.OtherModuleDependencyTag(module) != pythonLibTag {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800531 return
532 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700533 // Python module cannot depend on modules, except for Python library.
534 if !isPythonLibModule(module) {
535 panic(fmt.Errorf(
536 "the dependency %q of module %q is not Python library!",
537 ctx.ModuleName(), ctx.OtherModuleName(module)))
538 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800539 if dep, ok := module.(PythonDependency); ok {
540 srcs := dep.GetSrcsPathMappings()
541 for _, path := range srcs {
542 if !fillInMap(ctx, destToPySrcs,
543 path.dest, path.src.String(), ctx.ModuleName(),
544 ctx.OtherModuleName(module)) {
545 continue
546 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800547 }
548 data := dep.GetDataPathMappings()
549 for _, path := range data {
550 fillInMap(ctx, destToPyData,
551 path.dest, path.src.String(), ctx.ModuleName(),
552 ctx.OtherModuleName(module))
553 }
Nan Zhang1db85402017-12-18 13:20:23 -0800554 p.depsSrcsZips = append(p.depsSrcsZips, dep.GetSrcsZip())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800555 }
556 })
557}
558
559func fillInMap(ctx android.ModuleContext, m map[string]string,
560 key, value, curModule, otherModule string) bool {
561 if oldValue, found := m[key]; found {
562 ctx.ModuleErrorf("found two files to be placed at the same runfiles location %q."+
563 " First file: in module %s at path %q."+
564 " Second file: in module %s at path %q.",
565 key, curModule, oldValue, otherModule, value)
566 return false
567 } else {
568 m[key] = value
569 }
570
571 return true
572}
Nan Zhangea568a42017-11-08 21:20:04 -0800573
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000574func (p *Module) InstallInData() bool {
575 return true
576}
577
Nan Zhangea568a42017-11-08 21:20:04 -0800578var Bool = proptools.Bool
579var String = proptools.String