blob: 4b9111f3bf62f590983958373cb0a8486c1859e1 [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.
Nan Zhangbea09752018-05-31 12:49:33 -070066 // if left unspecified, all the source/data files path is unchanged within zip file.
Nan Zhangea568a42017-11-08 21:20:04 -080067 Pkg_path *string `android:"arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070068
69 // true, if the Python module is used internally, eg, Python std libs.
70 Is_internal *bool `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080071
72 // list of source (.py) files compatible both with Python2 and Python3 used to compile the
73 // Python module.
74 // srcs may reference the outputs of other modules that produce source files like genrule
75 // or filegroup using the syntax ":module".
76 // Srcs has to be non-empty.
Nan Zhangd4e641b2017-07-12 12:55:28 -070077 Srcs []string `android:"arch_variant"`
78
79 // list of source files that should not be used to build the C/C++ module.
80 // This is most useful in the arch/multilib variants to remove non-common files
81 Exclude_srcs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080082
83 // list of files or filegroup modules that provide data that should be installed alongside
84 // the test. the file extension can be arbitrary except for (.py).
Nan Zhangd4e641b2017-07-12 12:55:28 -070085 Data []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080086
87 // list of the Python libraries compatible both with Python2 and Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -070088 Libs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080089
90 Version struct {
91 // all the "srcs" or Python dependencies that are to be used only for Python2.
Nan Zhangd4e641b2017-07-12 12:55:28 -070092 Py2 VersionProperties `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080093
94 // all the "srcs" or Python dependencies that are to be used only for Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -070095 Py3 VersionProperties `android:"arch_variant"`
96 } `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080097
98 // the actual version each module uses after variations created.
99 // this property name is hidden from users' perspectives, and soong will populate it during
100 // runtime.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700101 Actual_version string `blueprint:"mutated"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800102}
103
104type pathMapping struct {
105 dest string
106 src android.Path
107}
108
Nan Zhangd4e641b2017-07-12 12:55:28 -0700109type Module struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800110 android.ModuleBase
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700111 android.DefaultableModuleBase
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800112
Nan Zhangb8fa1972017-12-22 16:12:00 -0800113 properties BaseProperties
114 protoProperties android.ProtoProperties
Nan Zhangd4e641b2017-07-12 12:55:28 -0700115
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 Zhangb8fa1972017-12-22 16:12:00 -0800189 p.AddProperties(&p.properties, &p.protoProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700190 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 Zhang1a479b62018-05-15 17:13:38 -0700208 pyIdentifierRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800209 pyExt = ".py"
Nan Zhangb8fa1972017-12-22 16:12:00 -0800210 protoExt = ".proto"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800211 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"
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:
Colin Crossff3ae9d2018-04-10 16:15:18 -0700253 return Bool(p.properties.Version.Py2.Embedded_launcher)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700254 case pyVersion3:
Colin Crossff3ae9d2018-04-10 16:15:18 -0700255 return Bool(p.properties.Version.Py3.Embedded_launcher)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700256 }
257
258 return false
259}
260
Nan Zhangb8fa1972017-12-22 16:12:00 -0800261func hasSrcExt(srcs []string, ext string) bool {
262 for _, src := range srcs {
263 if filepath.Ext(src) == ext {
264 return true
265 }
266 }
267
268 return false
269}
270
271func (p *Module) hasSrcExt(ctx android.BottomUpMutatorContext, ext string) bool {
272 if hasSrcExt(p.properties.Srcs, protoExt) {
273 return true
274 }
275 switch p.properties.Actual_version {
276 case pyVersion2:
277 return hasSrcExt(p.properties.Version.Py2.Srcs, protoExt)
278 case pyVersion3:
279 return hasSrcExt(p.properties.Version.Py3.Srcs, protoExt)
280 default:
281 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
282 p.properties.Actual_version, ctx.ModuleName()))
283 }
284}
285
Nan Zhangd4e641b2017-07-12 12:55:28 -0700286func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800287 // deps from "data".
288 android.ExtractSourcesDeps(ctx, p.properties.Data)
289 // deps from "srcs".
290 android.ExtractSourcesDeps(ctx, p.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000291 android.ExtractSourcesDeps(ctx, p.properties.Exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800292
Nan Zhangb8fa1972017-12-22 16:12:00 -0800293 if p.hasSrcExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
294 ctx.AddVariationDependencies(nil, pythonLibTag, "libprotobuf-python")
295 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700296 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800297 case pyVersion2:
298 // deps from "version.py2.srcs" property.
299 android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000300 android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800301
Nan Zhangd4e641b2017-07-12 12:55:28 -0700302 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800303 uniqueLibs(ctx, p.properties.Libs, "version.py2.libs",
304 p.properties.Version.Py2.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700305
306 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) {
307 ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib")
308 ctx.AddFarVariationDependencies([]blueprint.Variation{
309 {"arch", ctx.Target().String()},
310 }, launcherTag, "py2-launcher")
311 }
312
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800313 case pyVersion3:
314 // deps from "version.py3.srcs" property.
315 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000316 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800317
Nan Zhangd4e641b2017-07-12 12:55:28 -0700318 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800319 uniqueLibs(ctx, p.properties.Libs, "version.py3.libs",
320 p.properties.Version.Py3.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700321
322 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) {
323 //TODO(nanzhang): Add embedded launcher for Python3.
324 ctx.PropertyErrorf("version.py3.embedded_launcher",
325 "is not supported yet for Python3.")
326 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800327 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700328 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
329 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800330 }
331}
332
333// check "libs" duplicates from current module dependencies.
334func uniqueLibs(ctx android.BottomUpMutatorContext,
335 commonLibs []string, versionProp string, versionLibs []string) []string {
336 set := make(map[string]string)
337 ret := []string{}
338
339 // deps from "libs" property.
340 for _, l := range commonLibs {
341 if _, found := set[l]; found {
342 ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l)
343 } else {
344 set[l] = "libs"
345 ret = append(ret, l)
346 }
347 }
348 // deps from "version.pyX.libs" property.
349 for _, l := range versionLibs {
350 if _, found := set[l]; found {
351 ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l])
352 } else {
353 set[l] = versionProp
354 ret = append(ret, l)
355 }
356 }
357
358 return ret
359}
360
Nan Zhangd4e641b2017-07-12 12:55:28 -0700361func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
362 p.GeneratePythonBuildActions(ctx)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700363
Nan Zhangb8fa1972017-12-22 16:12:00 -0800364 // Only Python binaries and test has non-empty bootstrapper.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700365 if p.bootstrapper != nil {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800366 p.walkTransitiveDeps(ctx)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700367 // TODO(nanzhang): Since embedded launcher is not supported for Python3 for now,
368 // so we initialize "embedded_launcher" to false.
Nan Zhang1db85402017-12-18 13:20:23 -0800369 embeddedLauncher := false
Nan Zhangd4e641b2017-07-12 12:55:28 -0700370 if p.properties.Actual_version == pyVersion2 {
Nan Zhang1db85402017-12-18 13:20:23 -0800371 embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion2)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700372 }
373 p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
Nan Zhang1db85402017-12-18 13:20:23 -0800374 embeddedLauncher, p.srcsPathMappings, p.srcsZip, p.depsSrcsZips)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700375 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700376
377 if p.installer != nil && p.installSource.Valid() {
378 p.installer.install(ctx, p.installSource.Path())
379 }
380
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800381}
382
Nan Zhangd4e641b2017-07-12 12:55:28 -0700383func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800384 // expand python files from "srcs" property.
385 srcs := p.properties.Srcs
Nan Zhangd4e641b2017-07-12 12:55:28 -0700386 exclude_srcs := p.properties.Exclude_srcs
387 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800388 case pyVersion2:
389 srcs = append(srcs, p.properties.Version.Py2.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700390 exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800391 case pyVersion3:
392 srcs = append(srcs, p.properties.Version.Py3.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700393 exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800394 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700395 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
396 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800397 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700398 expandedSrcs := ctx.ExpandSources(srcs, exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800399 if len(expandedSrcs) == 0 {
400 ctx.ModuleErrorf("doesn't have any source files!")
401 }
402
403 // expand data files from "data" property.
404 expandedData := ctx.ExpandSources(p.properties.Data, nil)
405
406 // sanitize pkg_path.
Nan Zhang1db85402017-12-18 13:20:23 -0800407 pkgPath := String(p.properties.Pkg_path)
408 if pkgPath != "" {
409 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
410 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
411 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700412 ctx.PropertyErrorf("pkg_path",
413 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800414 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700415 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800416 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700417 if p.properties.Is_internal != nil && *p.properties.Is_internal {
Nan Zhang1db85402017-12-18 13:20:23 -0800418 pkgPath = filepath.Join(internal, pkgPath)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700419 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800420 } else {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700421 if p.properties.Is_internal != nil && *p.properties.Is_internal {
Nan Zhang1db85402017-12-18 13:20:23 -0800422 pkgPath = internal
Nan Zhangd4e641b2017-07-12 12:55:28 -0700423 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800424 }
425
Nan Zhang1db85402017-12-18 13:20:23 -0800426 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800427
Nan Zhang1db85402017-12-18 13:20:23 -0800428 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800429}
430
431// generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
432// for python/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800433func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800434 expandedSrcs, expandedData android.Paths) {
435 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800436 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800437 destToPySrcs := make(map[string]string)
438 destToPyData := make(map[string]string)
439
440 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800441 if s.Ext() != pyExt && s.Ext() != protoExt {
442 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800443 continue
444 }
Nan Zhang1db85402017-12-18 13:20:23 -0800445 runfilesPath := filepath.Join(pkgPath, s.Rel())
Nan Zhangb8fa1972017-12-22 16:12:00 -0800446 identifiers := strings.Split(strings.TrimSuffix(runfilesPath,
447 filepath.Ext(runfilesPath)), "/")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800448 for _, token := range identifiers {
449 if !pyIdentifierRegexp.MatchString(token) {
450 ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.",
451 runfilesPath, token)
452 }
453 }
454 if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
455 p.srcsPathMappings = append(p.srcsPathMappings,
456 pathMapping{dest: runfilesPath, src: s})
457 }
458 }
459
460 for _, d := range expandedData {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800461 if d.Ext() == pyExt || d.Ext() == protoExt {
462 ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800463 continue
464 }
Nan Zhang1db85402017-12-18 13:20:23 -0800465 runfilesPath := filepath.Join(pkgPath, d.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800466 if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
467 p.dataPathMappings = append(p.dataPathMappings,
468 pathMapping{dest: runfilesPath, src: d})
469 }
470 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800471}
472
Nan Zhang1db85402017-12-18 13:20:23 -0800473// register build actions to zip current module's sources.
474func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800475 relativeRootMap := make(map[string]android.Paths)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800476 pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
477
Nan Zhangb8fa1972017-12-22 16:12:00 -0800478 var protoSrcs android.Paths
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800479 // "srcs" or "data" properties may have filegroup so it might happen that
480 // the relative root for each source path is different.
481 for _, path := range pathMappings {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800482 if path.src.Ext() == protoExt {
483 protoSrcs = append(protoSrcs, path.src)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800484 } else {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800485 var relativeRoot string
486 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
487 if v, found := relativeRootMap[relativeRoot]; found {
488 relativeRootMap[relativeRoot] = append(v, path.src)
489 } else {
490 relativeRootMap[relativeRoot] = android.Paths{path.src}
491 }
492 }
493 }
494 var zips android.Paths
495 if len(protoSrcs) > 0 {
496 for _, srcFile := range protoSrcs {
497 zip := genProto(ctx, &p.protoProperties, srcFile,
498 android.ProtoFlags(ctx, &p.protoProperties), pkgPath)
499 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800500 }
501 }
502
Nan Zhangb8fa1972017-12-22 16:12:00 -0800503 if len(relativeRootMap) > 0 {
504 var keys []string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800505
Nan Zhangb8fa1972017-12-22 16:12:00 -0800506 // in order to keep stable order of soong_zip params, we sort the keys here.
507 for k := range relativeRootMap {
508 keys = append(keys, k)
Nan Zhang1db85402017-12-18 13:20:23 -0800509 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800510 sort.Strings(keys)
511
512 parArgs := []string{}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700513 if pkgPath != "" {
514 parArgs = append(parArgs, `-P `+pkgPath)
515 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800516 implicits := android.Paths{}
517 for _, k := range keys {
518 parArgs = append(parArgs, `-C `+k)
519 for _, path := range relativeRootMap[k] {
520 parArgs = append(parArgs, `-f `+path.String())
521 implicits = append(implicits, path)
522 }
523 }
524
525 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
526 ctx.Build(pctx, android.BuildParams{
527 Rule: zip,
528 Description: "python library archive",
529 Output: origSrcsZip,
530 Implicits: implicits,
531 Args: map[string]string{
532 "args": strings.Join(parArgs, " "),
533 },
534 })
535 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800536 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800537 if len(zips) == 1 {
538 return zips[0]
539 } else {
540 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
541 ctx.Build(pctx, android.BuildParams{
542 Rule: combineZip,
543 Description: "combine python library archive",
544 Output: combinedSrcsZip,
545 Inputs: zips,
546 })
547 return combinedSrcsZip
548 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800549}
550
Nan Zhangd4e641b2017-07-12 12:55:28 -0700551func isPythonLibModule(module blueprint.Module) bool {
552 if m, ok := module.(*Module); ok {
553 // Python library has no bootstrapper or installer.
554 if m.bootstrapper != nil || m.installer != nil {
555 return false
556 }
557 return true
558 }
559 return false
560}
561
Nan Zhangb8fa1972017-12-22 16:12:00 -0800562// check Python source/data files duplicates for whole runfiles tree since Python binary/test
563// need collect and zip all srcs of whole transitive dependencies to a final par file.
564func (p *Module) walkTransitiveDeps(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800565 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
566 // check duplicates.
567 destToPySrcs := make(map[string]string)
568 destToPyData := make(map[string]string)
569
570 for _, path := range p.srcsPathMappings {
571 destToPySrcs[path.dest] = path.src.String()
572 }
573 for _, path := range p.dataPathMappings {
574 destToPyData[path.dest] = path.src.String()
575 }
576
577 // visit all its dependencies in depth first.
Colin Crossd11fcda2017-10-23 17:59:01 -0700578 ctx.VisitDepsDepthFirst(func(module android.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700579 if ctx.OtherModuleDependencyTag(module) != pythonLibTag {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800580 return
581 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800582 // Python modules only can depend on Python libraries.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700583 if !isPythonLibModule(module) {
584 panic(fmt.Errorf(
585 "the dependency %q of module %q is not Python library!",
586 ctx.ModuleName(), ctx.OtherModuleName(module)))
587 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800588 if dep, ok := module.(PythonDependency); ok {
589 srcs := dep.GetSrcsPathMappings()
590 for _, path := range srcs {
591 if !fillInMap(ctx, destToPySrcs,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800592 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(module)) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800593 continue
594 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800595 }
596 data := dep.GetDataPathMappings()
597 for _, path := range data {
598 fillInMap(ctx, destToPyData,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800599 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(module))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800600 }
Nan Zhang1db85402017-12-18 13:20:23 -0800601 p.depsSrcsZips = append(p.depsSrcsZips, dep.GetSrcsZip())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800602 }
603 })
604}
605
606func fillInMap(ctx android.ModuleContext, m map[string]string,
607 key, value, curModule, otherModule string) bool {
608 if oldValue, found := m[key]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700609 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800610 " First file: in module %s at path %q."+
611 " Second file: in module %s at path %q.",
612 key, curModule, oldValue, otherModule, value)
613 return false
614 } else {
615 m[key] = value
616 }
617
618 return true
619}
Nan Zhangea568a42017-11-08 21:20:04 -0800620
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000621func (p *Module) InstallInData() bool {
622 return true
623}
624
Nan Zhangea568a42017-11-08 21:20:04 -0800625var Bool = proptools.Bool
626var String = proptools.String