blob: ddc3f1f9375c8a6e8128921d0c96c3ac87d83089 [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)
Logan Chien02880e42018-11-06 17:30:35 +0800163 setAndroidMkSharedLibs(sharedLibs []string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800164}
165
166type PythonDependency interface {
167 GetSrcsPathMappings() []pathMapping
168 GetDataPathMappings() []pathMapping
Nan Zhang1db85402017-12-18 13:20:23 -0800169 GetSrcsZip() android.Path
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800170}
171
Nan Zhangd4e641b2017-07-12 12:55:28 -0700172func (p *Module) GetSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800173 return p.srcsPathMappings
174}
175
Nan Zhangd4e641b2017-07-12 12:55:28 -0700176func (p *Module) GetDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800177 return p.dataPathMappings
178}
179
Nan Zhang1db85402017-12-18 13:20:23 -0800180func (p *Module) GetSrcsZip() android.Path {
181 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800182}
183
Nan Zhangd4e641b2017-07-12 12:55:28 -0700184var _ PythonDependency = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800185
Nan Zhangd4e641b2017-07-12 12:55:28 -0700186var _ android.AndroidMkDataProvider = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800187
Nan Zhangd4e641b2017-07-12 12:55:28 -0700188func (p *Module) Init() android.Module {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800189
Nan Zhangb8fa1972017-12-22 16:12:00 -0800190 p.AddProperties(&p.properties, &p.protoProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700191 if p.bootstrapper != nil {
192 p.AddProperties(p.bootstrapper.bootstrapperProps()...)
193 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800194
Nan Zhangd4e641b2017-07-12 12:55:28 -0700195 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700196 android.InitDefaultableModule(p)
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 (
Logan Chien02880e42018-11-06 17:30:35 +0800207 pythonLibTag = dependencyTag{name: "pythonLib"}
208 launcherTag = dependencyTag{name: "launcher"}
209 launcherSharedLibTag = dependencyTag{name: "launcherSharedLib"}
210 pyIdentifierRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
211 pyExt = ".py"
212 protoExt = ".proto"
213 pyVersion2 = "PY2"
214 pyVersion3 = "PY3"
215 initFileName = "__init__.py"
216 mainFileName = "__main__.py"
217 entryPointFile = "entry_point.txt"
218 parFileExt = ".zip"
219 internal = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800220)
221
222// create version variants for modules.
223func versionSplitMutator() func(android.BottomUpMutatorContext) {
224 return func(mctx android.BottomUpMutatorContext) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700225 if base, ok := mctx.Module().(*Module); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800226 versionNames := []string{}
227 if base.properties.Version.Py2.Enabled != nil &&
228 *(base.properties.Version.Py2.Enabled) == true {
229 versionNames = append(versionNames, pyVersion2)
230 }
231 if !(base.properties.Version.Py3.Enabled != nil &&
232 *(base.properties.Version.Py3.Enabled) == false) {
233 versionNames = append(versionNames, pyVersion3)
234 }
235 modules := mctx.CreateVariations(versionNames...)
236 for i, v := range versionNames {
237 // set the actual version for Python module.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700238 modules[i].(*Module).properties.Actual_version = v
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800239 }
240 }
241 }
242}
243
Nan Zhangb8bdacf2017-12-06 15:13:10 -0800244func (p *Module) HostToolPath() android.OptionalPath {
245 if p.installer == nil {
246 // python_library is just meta module, and doesn't have any installer.
247 return android.OptionalPath{}
248 }
249 return android.OptionalPathForPath(p.installer.(*binaryDecorator).path)
250}
251
Nan Zhangd4e641b2017-07-12 12:55:28 -0700252func (p *Module) isEmbeddedLauncherEnabled(actual_version string) bool {
253 switch actual_version {
254 case pyVersion2:
Colin Crossff3ae9d2018-04-10 16:15:18 -0700255 return Bool(p.properties.Version.Py2.Embedded_launcher)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700256 case pyVersion3:
Colin Crossff3ae9d2018-04-10 16:15:18 -0700257 return Bool(p.properties.Version.Py3.Embedded_launcher)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700258 }
259
260 return false
261}
262
Nan Zhangb8fa1972017-12-22 16:12:00 -0800263func hasSrcExt(srcs []string, ext string) bool {
264 for _, src := range srcs {
265 if filepath.Ext(src) == ext {
266 return true
267 }
268 }
269
270 return false
271}
272
273func (p *Module) hasSrcExt(ctx android.BottomUpMutatorContext, ext string) bool {
274 if hasSrcExt(p.properties.Srcs, protoExt) {
275 return true
276 }
277 switch p.properties.Actual_version {
278 case pyVersion2:
279 return hasSrcExt(p.properties.Version.Py2.Srcs, protoExt)
280 case pyVersion3:
281 return hasSrcExt(p.properties.Version.Py3.Srcs, protoExt)
282 default:
283 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
284 p.properties.Actual_version, ctx.ModuleName()))
285 }
286}
287
Nan Zhangd4e641b2017-07-12 12:55:28 -0700288func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800289 // deps from "data".
290 android.ExtractSourcesDeps(ctx, p.properties.Data)
291 // deps from "srcs".
292 android.ExtractSourcesDeps(ctx, p.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000293 android.ExtractSourcesDeps(ctx, p.properties.Exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800294
Nan Zhangb8fa1972017-12-22 16:12:00 -0800295 if p.hasSrcExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
296 ctx.AddVariationDependencies(nil, pythonLibTag, "libprotobuf-python")
297 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700298 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800299 case pyVersion2:
300 // deps from "version.py2.srcs" property.
301 android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000302 android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800303
Nan Zhangd4e641b2017-07-12 12:55:28 -0700304 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800305 uniqueLibs(ctx, p.properties.Libs, "version.py2.libs",
306 p.properties.Version.Py2.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700307
308 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) {
309 ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib")
310 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700311 {Mutator: "arch", Variation: ctx.Target().String()},
Nan Zhangd4e641b2017-07-12 12:55:28 -0700312 }, launcherTag, "py2-launcher")
Logan Chien02880e42018-11-06 17:30:35 +0800313
314 // Add py2-launcher shared lib dependencies. Ideally, these should be
315 // derived from the `shared_libs` property of "py2-launcher". However, we
316 // cannot read the property at this stage and it will be too late to add
317 // dependencies later.
318 ctx.AddFarVariationDependencies([]blueprint.Variation{
319 {Mutator: "arch", Variation: ctx.Target().String()},
320 }, launcherSharedLibTag, "libsqlite")
321
322 if ctx.Target().Os.Bionic() {
323 ctx.AddFarVariationDependencies([]blueprint.Variation{
324 {Mutator: "arch", Variation: ctx.Target().String()},
325 }, launcherSharedLibTag, "libc", "libdl", "libm")
326 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700327 }
328
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800329 case pyVersion3:
330 // deps from "version.py3.srcs" property.
331 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000332 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800333
Nan Zhangd4e641b2017-07-12 12:55:28 -0700334 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800335 uniqueLibs(ctx, p.properties.Libs, "version.py3.libs",
336 p.properties.Version.Py3.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700337
338 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) {
339 //TODO(nanzhang): Add embedded launcher for Python3.
340 ctx.PropertyErrorf("version.py3.embedded_launcher",
341 "is not supported yet for Python3.")
342 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800343 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700344 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
345 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800346 }
347}
348
349// check "libs" duplicates from current module dependencies.
350func uniqueLibs(ctx android.BottomUpMutatorContext,
351 commonLibs []string, versionProp string, versionLibs []string) []string {
352 set := make(map[string]string)
353 ret := []string{}
354
355 // deps from "libs" property.
356 for _, l := range commonLibs {
357 if _, found := set[l]; found {
358 ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l)
359 } else {
360 set[l] = "libs"
361 ret = append(ret, l)
362 }
363 }
364 // deps from "version.pyX.libs" property.
365 for _, l := range versionLibs {
366 if _, found := set[l]; found {
367 ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l])
368 } else {
369 set[l] = versionProp
370 ret = append(ret, l)
371 }
372 }
373
374 return ret
375}
376
Nan Zhangd4e641b2017-07-12 12:55:28 -0700377func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
378 p.GeneratePythonBuildActions(ctx)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700379
Nan Zhangb8fa1972017-12-22 16:12:00 -0800380 // Only Python binaries and test has non-empty bootstrapper.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700381 if p.bootstrapper != nil {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800382 p.walkTransitiveDeps(ctx)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700383 // TODO(nanzhang): Since embedded launcher is not supported for Python3 for now,
384 // so we initialize "embedded_launcher" to false.
Nan Zhang1db85402017-12-18 13:20:23 -0800385 embeddedLauncher := false
Nan Zhangd4e641b2017-07-12 12:55:28 -0700386 if p.properties.Actual_version == pyVersion2 {
Nan Zhang1db85402017-12-18 13:20:23 -0800387 embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion2)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700388 }
389 p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
Nan Zhang1db85402017-12-18 13:20:23 -0800390 embeddedLauncher, p.srcsPathMappings, p.srcsZip, p.depsSrcsZips)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700391 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700392
Logan Chien02880e42018-11-06 17:30:35 +0800393 if p.installer != nil {
394 var sharedLibs []string
395 ctx.VisitDirectDeps(func(dep android.Module) {
396 if ctx.OtherModuleDependencyTag(dep) == launcherSharedLibTag {
397 sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep))
398 }
399 })
400 p.installer.setAndroidMkSharedLibs(sharedLibs)
401
402 if p.installSource.Valid() {
403 p.installer.install(ctx, p.installSource.Path())
404 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700405 }
406
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800407}
408
Nan Zhangd4e641b2017-07-12 12:55:28 -0700409func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800410 // expand python files from "srcs" property.
411 srcs := p.properties.Srcs
Nan Zhangd4e641b2017-07-12 12:55:28 -0700412 exclude_srcs := p.properties.Exclude_srcs
413 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800414 case pyVersion2:
415 srcs = append(srcs, p.properties.Version.Py2.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700416 exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800417 case pyVersion3:
418 srcs = append(srcs, p.properties.Version.Py3.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700419 exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800420 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700421 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
422 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800423 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700424 expandedSrcs := ctx.ExpandSources(srcs, exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800425 if len(expandedSrcs) == 0 {
426 ctx.ModuleErrorf("doesn't have any source files!")
427 }
428
429 // expand data files from "data" property.
430 expandedData := ctx.ExpandSources(p.properties.Data, nil)
431
432 // sanitize pkg_path.
Nan Zhang1db85402017-12-18 13:20:23 -0800433 pkgPath := String(p.properties.Pkg_path)
434 if pkgPath != "" {
435 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
436 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
437 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700438 ctx.PropertyErrorf("pkg_path",
439 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800440 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700441 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800442 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700443 if p.properties.Is_internal != nil && *p.properties.Is_internal {
Nan Zhang1db85402017-12-18 13:20:23 -0800444 pkgPath = filepath.Join(internal, pkgPath)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700445 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800446 } else {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700447 if p.properties.Is_internal != nil && *p.properties.Is_internal {
Nan Zhang1db85402017-12-18 13:20:23 -0800448 pkgPath = internal
Nan Zhangd4e641b2017-07-12 12:55:28 -0700449 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800450 }
451
Nan Zhang1db85402017-12-18 13:20:23 -0800452 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800453
Nan Zhang1db85402017-12-18 13:20:23 -0800454 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800455}
456
457// generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
458// for python/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800459func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800460 expandedSrcs, expandedData android.Paths) {
461 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800462 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800463 destToPySrcs := make(map[string]string)
464 destToPyData := make(map[string]string)
465
466 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800467 if s.Ext() != pyExt && s.Ext() != protoExt {
468 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800469 continue
470 }
Nan Zhang1db85402017-12-18 13:20:23 -0800471 runfilesPath := filepath.Join(pkgPath, s.Rel())
Nan Zhangb8fa1972017-12-22 16:12:00 -0800472 identifiers := strings.Split(strings.TrimSuffix(runfilesPath,
473 filepath.Ext(runfilesPath)), "/")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800474 for _, token := range identifiers {
475 if !pyIdentifierRegexp.MatchString(token) {
476 ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.",
477 runfilesPath, token)
478 }
479 }
480 if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
481 p.srcsPathMappings = append(p.srcsPathMappings,
482 pathMapping{dest: runfilesPath, src: s})
483 }
484 }
485
486 for _, d := range expandedData {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800487 if d.Ext() == pyExt || d.Ext() == protoExt {
488 ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800489 continue
490 }
Nan Zhang1db85402017-12-18 13:20:23 -0800491 runfilesPath := filepath.Join(pkgPath, d.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800492 if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
493 p.dataPathMappings = append(p.dataPathMappings,
494 pathMapping{dest: runfilesPath, src: d})
495 }
496 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800497}
498
Nan Zhang1db85402017-12-18 13:20:23 -0800499// register build actions to zip current module's sources.
500func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800501 relativeRootMap := make(map[string]android.Paths)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800502 pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
503
Nan Zhangb8fa1972017-12-22 16:12:00 -0800504 var protoSrcs android.Paths
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800505 // "srcs" or "data" properties may have filegroup so it might happen that
506 // the relative root for each source path is different.
507 for _, path := range pathMappings {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800508 if path.src.Ext() == protoExt {
509 protoSrcs = append(protoSrcs, path.src)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800510 } else {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800511 var relativeRoot string
512 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
513 if v, found := relativeRootMap[relativeRoot]; found {
514 relativeRootMap[relativeRoot] = append(v, path.src)
515 } else {
516 relativeRootMap[relativeRoot] = android.Paths{path.src}
517 }
518 }
519 }
520 var zips android.Paths
521 if len(protoSrcs) > 0 {
522 for _, srcFile := range protoSrcs {
523 zip := genProto(ctx, &p.protoProperties, srcFile,
524 android.ProtoFlags(ctx, &p.protoProperties), pkgPath)
525 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800526 }
527 }
528
Nan Zhangb8fa1972017-12-22 16:12:00 -0800529 if len(relativeRootMap) > 0 {
530 var keys []string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800531
Nan Zhangb8fa1972017-12-22 16:12:00 -0800532 // in order to keep stable order of soong_zip params, we sort the keys here.
533 for k := range relativeRootMap {
534 keys = append(keys, k)
Nan Zhang1db85402017-12-18 13:20:23 -0800535 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800536 sort.Strings(keys)
537
538 parArgs := []string{}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700539 if pkgPath != "" {
540 parArgs = append(parArgs, `-P `+pkgPath)
541 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800542 implicits := android.Paths{}
543 for _, k := range keys {
544 parArgs = append(parArgs, `-C `+k)
545 for _, path := range relativeRootMap[k] {
546 parArgs = append(parArgs, `-f `+path.String())
547 implicits = append(implicits, path)
548 }
549 }
550
551 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
552 ctx.Build(pctx, android.BuildParams{
553 Rule: zip,
554 Description: "python library archive",
555 Output: origSrcsZip,
556 Implicits: implicits,
557 Args: map[string]string{
558 "args": strings.Join(parArgs, " "),
559 },
560 })
561 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800562 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800563 if len(zips) == 1 {
564 return zips[0]
565 } else {
566 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
567 ctx.Build(pctx, android.BuildParams{
568 Rule: combineZip,
569 Description: "combine python library archive",
570 Output: combinedSrcsZip,
571 Inputs: zips,
572 })
573 return combinedSrcsZip
574 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800575}
576
Nan Zhangd4e641b2017-07-12 12:55:28 -0700577func isPythonLibModule(module blueprint.Module) bool {
578 if m, ok := module.(*Module); ok {
579 // Python library has no bootstrapper or installer.
580 if m.bootstrapper != nil || m.installer != nil {
581 return false
582 }
583 return true
584 }
585 return false
586}
587
Nan Zhangb8fa1972017-12-22 16:12:00 -0800588// check Python source/data files duplicates for whole runfiles tree since Python binary/test
589// need collect and zip all srcs of whole transitive dependencies to a final par file.
590func (p *Module) walkTransitiveDeps(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800591 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
592 // check duplicates.
593 destToPySrcs := make(map[string]string)
594 destToPyData := make(map[string]string)
595
596 for _, path := range p.srcsPathMappings {
597 destToPySrcs[path.dest] = path.src.String()
598 }
599 for _, path := range p.dataPathMappings {
600 destToPyData[path.dest] = path.src.String()
601 }
602
Colin Cross6b753602018-06-21 13:03:07 -0700603 seen := make(map[android.Module]bool)
604
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800605 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700606 ctx.WalkDeps(func(child, parent android.Module) bool {
607 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
608 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800609 }
Colin Cross6b753602018-06-21 13:03:07 -0700610 if seen[child] {
611 return false
612 }
613 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800614 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700615 if !isPythonLibModule(child) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700616 panic(fmt.Errorf(
617 "the dependency %q of module %q is not Python library!",
Colin Cross6b753602018-06-21 13:03:07 -0700618 ctx.ModuleName(), ctx.OtherModuleName(child)))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700619 }
Colin Cross6b753602018-06-21 13:03:07 -0700620 if dep, ok := child.(PythonDependency); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800621 srcs := dep.GetSrcsPathMappings()
622 for _, path := range srcs {
623 if !fillInMap(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700624 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800625 continue
626 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800627 }
628 data := dep.GetDataPathMappings()
629 for _, path := range data {
630 fillInMap(ctx, destToPyData,
Colin Cross6b753602018-06-21 13:03:07 -0700631 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800632 }
Nan Zhang1db85402017-12-18 13:20:23 -0800633 p.depsSrcsZips = append(p.depsSrcsZips, dep.GetSrcsZip())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800634 }
Colin Cross6b753602018-06-21 13:03:07 -0700635 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800636 })
637}
638
639func fillInMap(ctx android.ModuleContext, m map[string]string,
640 key, value, curModule, otherModule string) bool {
641 if oldValue, found := m[key]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700642 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800643 " First file: in module %s at path %q."+
644 " Second file: in module %s at path %q.",
645 key, curModule, oldValue, otherModule, value)
646 return false
647 } else {
648 m[key] = value
649 }
650
651 return true
652}
Nan Zhangea568a42017-11-08 21:20:04 -0800653
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000654func (p *Module) InstallInData() bool {
655 return true
656}
657
Nan Zhangea568a42017-11-08 21:20:04 -0800658var Bool = proptools.Bool
659var String = proptools.String