blob: 65e3efd486d94e1a4167b98aa974e6b647c1cec2 [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 Zhangb8fa1972017-12-22 16:12:00 -0800114 properties BaseProperties
115 protoProperties android.ProtoProperties
Nan Zhangd4e641b2017-07-12 12:55:28 -0700116
117 // initialize before calling Init
118 hod android.HostOrDeviceSupported
119 multilib android.Multilib
120
121 // the bootstrapper is used to bootstrap .par executable.
122 // bootstrapper might be nil (Python library module).
123 bootstrapper bootstrapper
124
125 // the installer might be nil.
126 installer installer
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800127
128 // the Python files of current module after expanding source dependencies.
129 // pathMapping: <dest: runfile_path, src: source_path>
130 srcsPathMappings []pathMapping
131
132 // the data files of current module after expanding source dependencies.
133 // pathMapping: <dest: runfile_path, src: source_path>
134 dataPathMappings []pathMapping
135
Nan Zhang1db85402017-12-18 13:20:23 -0800136 // the zip filepath for zipping current module source/data files.
137 srcsZip android.Path
Nan Zhangd4e641b2017-07-12 12:55:28 -0700138
Nan Zhang1db85402017-12-18 13:20:23 -0800139 // dependency modules' zip filepath for zipping current module source/data files.
140 depsSrcsZips android.Paths
Nan Zhangd4e641b2017-07-12 12:55:28 -0700141
142 // (.intermediate) module output path as installation source.
143 installSource android.OptionalPath
144
Nan Zhang5323f8e2017-05-10 13:37:54 -0700145 subAndroidMkOnce map[subAndroidMkProvider]bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800146}
147
Nan Zhangd4e641b2017-07-12 12:55:28 -0700148func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
149 return &Module{
150 hod: hod,
151 multilib: multilib,
152 }
153}
154
155type bootstrapper interface {
156 bootstrapperProps() []interface{}
Nan Zhang1db85402017-12-18 13:20:23 -0800157 bootstrap(ctx android.ModuleContext, ActualVersion string, embeddedLauncher bool,
158 srcsPathMappings []pathMapping, srcsZip android.Path,
159 depsSrcsZips android.Paths) android.OptionalPath
Nan Zhangd4e641b2017-07-12 12:55:28 -0700160}
161
162type installer interface {
163 install(ctx android.ModuleContext, path android.Path)
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 (
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"
Nan Zhangb8fa1972017-12-22 16:12:00 -0800211 protoExt = ".proto"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800212 pyVersion2 = "PY2"
213 pyVersion3 = "PY3"
214 initFileName = "__init__.py"
215 mainFileName = "__main__.py"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700216 entryPointFile = "entry_point.txt"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800217 parFileExt = ".zip"
218 runFiles = "runfiles"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700219 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{
311 {"arch", ctx.Target().String()},
312 }, launcherTag, "py2-launcher")
313 }
314
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800315 case pyVersion3:
316 // deps from "version.py3.srcs" property.
317 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000318 android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800319
Nan Zhangd4e641b2017-07-12 12:55:28 -0700320 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800321 uniqueLibs(ctx, p.properties.Libs, "version.py3.libs",
322 p.properties.Version.Py3.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700323
324 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) {
325 //TODO(nanzhang): Add embedded launcher for Python3.
326 ctx.PropertyErrorf("version.py3.embedded_launcher",
327 "is not supported yet for Python3.")
328 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800329 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700330 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
331 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800332 }
333}
334
335// check "libs" duplicates from current module dependencies.
336func uniqueLibs(ctx android.BottomUpMutatorContext,
337 commonLibs []string, versionProp string, versionLibs []string) []string {
338 set := make(map[string]string)
339 ret := []string{}
340
341 // deps from "libs" property.
342 for _, l := range commonLibs {
343 if _, found := set[l]; found {
344 ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l)
345 } else {
346 set[l] = "libs"
347 ret = append(ret, l)
348 }
349 }
350 // deps from "version.pyX.libs" property.
351 for _, l := range versionLibs {
352 if _, found := set[l]; found {
353 ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l])
354 } else {
355 set[l] = versionProp
356 ret = append(ret, l)
357 }
358 }
359
360 return ret
361}
362
Nan Zhangd4e641b2017-07-12 12:55:28 -0700363func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
364 p.GeneratePythonBuildActions(ctx)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700365
Nan Zhangb8fa1972017-12-22 16:12:00 -0800366 // Only Python binaries and test has non-empty bootstrapper.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700367 if p.bootstrapper != nil {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800368 p.walkTransitiveDeps(ctx)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700369 // TODO(nanzhang): Since embedded launcher is not supported for Python3 for now,
370 // so we initialize "embedded_launcher" to false.
Nan Zhang1db85402017-12-18 13:20:23 -0800371 embeddedLauncher := false
Nan Zhangd4e641b2017-07-12 12:55:28 -0700372 if p.properties.Actual_version == pyVersion2 {
Nan Zhang1db85402017-12-18 13:20:23 -0800373 embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion2)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700374 }
375 p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
Nan Zhang1db85402017-12-18 13:20:23 -0800376 embeddedLauncher, p.srcsPathMappings, p.srcsZip, p.depsSrcsZips)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700377 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700378
379 if p.installer != nil && p.installSource.Valid() {
380 p.installer.install(ctx, p.installSource.Path())
381 }
382
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800383}
384
Nan Zhangd4e641b2017-07-12 12:55:28 -0700385func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800386 // expand python files from "srcs" property.
387 srcs := p.properties.Srcs
Nan Zhangd4e641b2017-07-12 12:55:28 -0700388 exclude_srcs := p.properties.Exclude_srcs
389 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800390 case pyVersion2:
391 srcs = append(srcs, p.properties.Version.Py2.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700392 exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800393 case pyVersion3:
394 srcs = append(srcs, p.properties.Version.Py3.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700395 exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800396 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700397 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
398 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800399 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700400 expandedSrcs := ctx.ExpandSources(srcs, exclude_srcs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800401 if len(expandedSrcs) == 0 {
402 ctx.ModuleErrorf("doesn't have any source files!")
403 }
404
405 // expand data files from "data" property.
406 expandedData := ctx.ExpandSources(p.properties.Data, nil)
407
408 // sanitize pkg_path.
Nan Zhang1db85402017-12-18 13:20:23 -0800409 pkgPath := String(p.properties.Pkg_path)
410 if pkgPath != "" {
411 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
412 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
413 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700414 ctx.PropertyErrorf("pkg_path",
415 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800416 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700417 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800418 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700419 if p.properties.Is_internal != nil && *p.properties.Is_internal {
420 // pkg_path starts from "internal/" implicitly.
Nan Zhang1db85402017-12-18 13:20:23 -0800421 pkgPath = filepath.Join(internal, pkgPath)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700422 } else {
Nan Zhangf0c4e432018-05-22 14:50:18 -0700423 if !p.isEmbeddedLauncherEnabled(p.properties.Actual_version) {
424 // pkg_path starts from "runfiles/" implicitly.
425 pkgPath = filepath.Join(runFiles, pkgPath)
426 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700427 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800428 } else {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700429 if p.properties.Is_internal != nil && *p.properties.Is_internal {
430 // pkg_path starts from "runfiles/" implicitly.
Nan Zhang1db85402017-12-18 13:20:23 -0800431 pkgPath = internal
Nan Zhangd4e641b2017-07-12 12:55:28 -0700432 } else {
Nan Zhangf0c4e432018-05-22 14:50:18 -0700433 if !p.isEmbeddedLauncherEnabled(p.properties.Actual_version) {
434 // pkg_path starts from "runfiles/" implicitly.
435 pkgPath = runFiles
436 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700437 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800438 }
439
Nan Zhang1db85402017-12-18 13:20:23 -0800440 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800441
Nan Zhang1db85402017-12-18 13:20:23 -0800442 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800443}
444
445// generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
446// for python/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800447func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800448 expandedSrcs, expandedData android.Paths) {
449 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800450 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800451 destToPySrcs := make(map[string]string)
452 destToPyData := make(map[string]string)
453
454 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800455 if s.Ext() != pyExt && s.Ext() != protoExt {
456 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800457 continue
458 }
Nan Zhang1db85402017-12-18 13:20:23 -0800459 runfilesPath := filepath.Join(pkgPath, s.Rel())
Nan Zhangb8fa1972017-12-22 16:12:00 -0800460 identifiers := strings.Split(strings.TrimSuffix(runfilesPath,
461 filepath.Ext(runfilesPath)), "/")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800462 for _, token := range identifiers {
463 if !pyIdentifierRegexp.MatchString(token) {
464 ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.",
465 runfilesPath, token)
466 }
467 }
468 if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
469 p.srcsPathMappings = append(p.srcsPathMappings,
470 pathMapping{dest: runfilesPath, src: s})
471 }
472 }
473
474 for _, d := range expandedData {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800475 if d.Ext() == pyExt || d.Ext() == protoExt {
476 ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800477 continue
478 }
Nan Zhang1db85402017-12-18 13:20:23 -0800479 runfilesPath := filepath.Join(pkgPath, d.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800480 if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
481 p.dataPathMappings = append(p.dataPathMappings,
482 pathMapping{dest: runfilesPath, src: d})
483 }
484 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800485}
486
Nan Zhang1db85402017-12-18 13:20:23 -0800487// register build actions to zip current module's sources.
488func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800489 relativeRootMap := make(map[string]android.Paths)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800490 pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
491
Nan Zhangb8fa1972017-12-22 16:12:00 -0800492 var protoSrcs android.Paths
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800493 // "srcs" or "data" properties may have filegroup so it might happen that
494 // the relative root for each source path is different.
495 for _, path := range pathMappings {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800496 if path.src.Ext() == protoExt {
497 protoSrcs = append(protoSrcs, path.src)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800498 } else {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800499 var relativeRoot string
500 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
501 if v, found := relativeRootMap[relativeRoot]; found {
502 relativeRootMap[relativeRoot] = append(v, path.src)
503 } else {
504 relativeRootMap[relativeRoot] = android.Paths{path.src}
505 }
506 }
507 }
508 var zips android.Paths
509 if len(protoSrcs) > 0 {
510 for _, srcFile := range protoSrcs {
511 zip := genProto(ctx, &p.protoProperties, srcFile,
512 android.ProtoFlags(ctx, &p.protoProperties), pkgPath)
513 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800514 }
515 }
516
Nan Zhangb8fa1972017-12-22 16:12:00 -0800517 if len(relativeRootMap) > 0 {
518 var keys []string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800519
Nan Zhangb8fa1972017-12-22 16:12:00 -0800520 // in order to keep stable order of soong_zip params, we sort the keys here.
521 for k := range relativeRootMap {
522 keys = append(keys, k)
Nan Zhang1db85402017-12-18 13:20:23 -0800523 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800524 sort.Strings(keys)
525
526 parArgs := []string{}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700527 if pkgPath != "" {
528 parArgs = append(parArgs, `-P `+pkgPath)
529 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800530 implicits := android.Paths{}
531 for _, k := range keys {
532 parArgs = append(parArgs, `-C `+k)
533 for _, path := range relativeRootMap[k] {
534 parArgs = append(parArgs, `-f `+path.String())
535 implicits = append(implicits, path)
536 }
537 }
538
539 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
540 ctx.Build(pctx, android.BuildParams{
541 Rule: zip,
542 Description: "python library archive",
543 Output: origSrcsZip,
544 Implicits: implicits,
545 Args: map[string]string{
546 "args": strings.Join(parArgs, " "),
547 },
548 })
549 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800550 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800551 if len(zips) == 1 {
552 return zips[0]
553 } else {
554 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
555 ctx.Build(pctx, android.BuildParams{
556 Rule: combineZip,
557 Description: "combine python library archive",
558 Output: combinedSrcsZip,
559 Inputs: zips,
560 })
561 return combinedSrcsZip
562 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800563}
564
Nan Zhangd4e641b2017-07-12 12:55:28 -0700565func isPythonLibModule(module blueprint.Module) bool {
566 if m, ok := module.(*Module); ok {
567 // Python library has no bootstrapper or installer.
568 if m.bootstrapper != nil || m.installer != nil {
569 return false
570 }
571 return true
572 }
573 return false
574}
575
Nan Zhangb8fa1972017-12-22 16:12:00 -0800576// check Python source/data files duplicates for whole runfiles tree since Python binary/test
577// need collect and zip all srcs of whole transitive dependencies to a final par file.
578func (p *Module) walkTransitiveDeps(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800579 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
580 // check duplicates.
581 destToPySrcs := make(map[string]string)
582 destToPyData := make(map[string]string)
583
584 for _, path := range p.srcsPathMappings {
585 destToPySrcs[path.dest] = path.src.String()
586 }
587 for _, path := range p.dataPathMappings {
588 destToPyData[path.dest] = path.src.String()
589 }
590
591 // visit all its dependencies in depth first.
Colin Crossd11fcda2017-10-23 17:59:01 -0700592 ctx.VisitDepsDepthFirst(func(module android.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700593 if ctx.OtherModuleDependencyTag(module) != pythonLibTag {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800594 return
595 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800596 // Python modules only can depend on Python libraries.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700597 if !isPythonLibModule(module) {
598 panic(fmt.Errorf(
599 "the dependency %q of module %q is not Python library!",
600 ctx.ModuleName(), ctx.OtherModuleName(module)))
601 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800602 if dep, ok := module.(PythonDependency); ok {
603 srcs := dep.GetSrcsPathMappings()
604 for _, path := range srcs {
605 if !fillInMap(ctx, destToPySrcs,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800606 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(module)) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800607 continue
608 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800609 }
610 data := dep.GetDataPathMappings()
611 for _, path := range data {
612 fillInMap(ctx, destToPyData,
Nan Zhangb8fa1972017-12-22 16:12:00 -0800613 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(module))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800614 }
Nan Zhang1db85402017-12-18 13:20:23 -0800615 p.depsSrcsZips = append(p.depsSrcsZips, dep.GetSrcsZip())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800616 }
617 })
618}
619
620func fillInMap(ctx android.ModuleContext, m map[string]string,
621 key, value, curModule, otherModule string) bool {
622 if oldValue, found := m[key]; found {
623 ctx.ModuleErrorf("found two files to be placed at the same runfiles location %q."+
624 " First file: in module %s at path %q."+
625 " Second file: in module %s at path %q.",
626 key, curModule, oldValue, otherModule, value)
627 return false
628 } else {
629 m[key] = value
630 }
631
632 return true
633}
Nan Zhangea568a42017-11-08 21:20:04 -0800634
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000635func (p *Module) InstallInData() bool {
636 return true
637}
638
Nan Zhangea568a42017-11-08 21:20:04 -0800639var Bool = proptools.Bool
640var String = proptools.String