blob: a6c9e2a074fa430deee58ba84258fe44a728322d [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".
Colin Cross27b922f2019-03-04 22:35:41 -080046 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070047
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
Colin Cross27b922f2019-03-04 22:35:41 -080050 Exclude_srcs []string `android:"path,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.
Colin Cross27b922f2019-03-04 22:35:41 -080077 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070078
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
Colin Cross27b922f2019-03-04 22:35:41 -080081 Exclude_srcs []string `android:"path,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).
Colin Cross27b922f2019-03-04 22:35:41 -080085 Data []string `android:"path,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
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800159
160 autorun() bool
Nan Zhangd4e641b2017-07-12 12:55:28 -0700161}
162
163type installer interface {
164 install(ctx android.ModuleContext, path android.Path)
Logan Chien02880e42018-11-06 17:30:35 +0800165 setAndroidMkSharedLibs(sharedLibs []string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800166}
167
168type PythonDependency interface {
169 GetSrcsPathMappings() []pathMapping
170 GetDataPathMappings() []pathMapping
Nan Zhang1db85402017-12-18 13:20:23 -0800171 GetSrcsZip() android.Path
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800172}
173
Nan Zhangd4e641b2017-07-12 12:55:28 -0700174func (p *Module) GetSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800175 return p.srcsPathMappings
176}
177
Nan Zhangd4e641b2017-07-12 12:55:28 -0700178func (p *Module) GetDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800179 return p.dataPathMappings
180}
181
Nan Zhang1db85402017-12-18 13:20:23 -0800182func (p *Module) GetSrcsZip() android.Path {
183 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800184}
185
Nan Zhangd4e641b2017-07-12 12:55:28 -0700186var _ PythonDependency = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800187
Nan Zhangd4e641b2017-07-12 12:55:28 -0700188var _ android.AndroidMkDataProvider = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800189
Nan Zhangd4e641b2017-07-12 12:55:28 -0700190func (p *Module) Init() android.Module {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800191
Nan Zhangb8fa1972017-12-22 16:12:00 -0800192 p.AddProperties(&p.properties, &p.protoProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700193 if p.bootstrapper != nil {
194 p.AddProperties(p.bootstrapper.bootstrapperProps()...)
195 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800196
Nan Zhangd4e641b2017-07-12 12:55:28 -0700197 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700198 android.InitDefaultableModule(p)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800199
Nan Zhangd4e641b2017-07-12 12:55:28 -0700200 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800201}
202
Nan Zhangd4e641b2017-07-12 12:55:28 -0700203type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800204 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700205 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800206}
207
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800208var (
Logan Chien02880e42018-11-06 17:30:35 +0800209 pythonLibTag = dependencyTag{name: "pythonLib"}
210 launcherTag = dependencyTag{name: "launcher"}
211 launcherSharedLibTag = dependencyTag{name: "launcherSharedLib"}
212 pyIdentifierRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
213 pyExt = ".py"
214 protoExt = ".proto"
215 pyVersion2 = "PY2"
216 pyVersion3 = "PY3"
217 initFileName = "__init__.py"
218 mainFileName = "__main__.py"
219 entryPointFile = "entry_point.txt"
220 parFileExt = ".zip"
221 internal = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800222)
223
224// create version variants for modules.
225func versionSplitMutator() func(android.BottomUpMutatorContext) {
226 return func(mctx android.BottomUpMutatorContext) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700227 if base, ok := mctx.Module().(*Module); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800228 versionNames := []string{}
229 if base.properties.Version.Py2.Enabled != nil &&
230 *(base.properties.Version.Py2.Enabled) == true {
231 versionNames = append(versionNames, pyVersion2)
232 }
233 if !(base.properties.Version.Py3.Enabled != nil &&
234 *(base.properties.Version.Py3.Enabled) == false) {
235 versionNames = append(versionNames, pyVersion3)
236 }
237 modules := mctx.CreateVariations(versionNames...)
238 for i, v := range versionNames {
239 // set the actual version for Python module.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700240 modules[i].(*Module).properties.Actual_version = v
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800241 }
242 }
243 }
244}
245
Nan Zhangb8bdacf2017-12-06 15:13:10 -0800246func (p *Module) HostToolPath() android.OptionalPath {
247 if p.installer == nil {
248 // python_library is just meta module, and doesn't have any installer.
249 return android.OptionalPath{}
250 }
251 return android.OptionalPathForPath(p.installer.(*binaryDecorator).path)
252}
253
Liz Kammere0070ee2020-06-22 11:52:59 -0700254func (p *Module) OutputFiles(tag string) (android.Paths, error) {
255 switch tag {
256 case "":
257 if outputFile := p.installSource; outputFile.Valid() {
258 return android.Paths{outputFile.Path()}, nil
259 }
260 return android.Paths{}, nil
261 default:
262 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
263 }
264}
265
Nan Zhangd4e641b2017-07-12 12:55:28 -0700266func (p *Module) isEmbeddedLauncherEnabled(actual_version string) bool {
267 switch actual_version {
268 case pyVersion2:
Colin Crossff3ae9d2018-04-10 16:15:18 -0700269 return Bool(p.properties.Version.Py2.Embedded_launcher)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700270 case pyVersion3:
Colin Crossff3ae9d2018-04-10 16:15:18 -0700271 return Bool(p.properties.Version.Py3.Embedded_launcher)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700272 }
273
274 return false
275}
276
Nan Zhangb8fa1972017-12-22 16:12:00 -0800277func hasSrcExt(srcs []string, ext string) bool {
278 for _, src := range srcs {
279 if filepath.Ext(src) == ext {
280 return true
281 }
282 }
283
284 return false
285}
286
287func (p *Module) hasSrcExt(ctx android.BottomUpMutatorContext, ext string) bool {
288 if hasSrcExt(p.properties.Srcs, protoExt) {
289 return true
290 }
291 switch p.properties.Actual_version {
292 case pyVersion2:
293 return hasSrcExt(p.properties.Version.Py2.Srcs, protoExt)
294 case pyVersion3:
295 return hasSrcExt(p.properties.Version.Py3.Srcs, protoExt)
296 default:
297 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
298 p.properties.Actual_version, ctx.ModuleName()))
299 }
300}
301
Nan Zhangd4e641b2017-07-12 12:55:28 -0700302func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700303 android.ProtoDeps(ctx, &p.protoProperties)
304
Nan Zhangb8fa1972017-12-22 16:12:00 -0800305 if p.hasSrcExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
306 ctx.AddVariationDependencies(nil, pythonLibTag, "libprotobuf-python")
307 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700308 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800309 case pyVersion2:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700310 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800311 uniqueLibs(ctx, p.properties.Libs, "version.py2.libs",
312 p.properties.Version.Py2.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700313
314 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) {
315 ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib")
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800316
317 launcherModule := "py2-launcher"
318 if p.bootstrapper.autorun() {
319 launcherModule = "py2-launcher-autorun"
320 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700321 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule)
Logan Chien02880e42018-11-06 17:30:35 +0800322
323 // Add py2-launcher shared lib dependencies. Ideally, these should be
324 // derived from the `shared_libs` property of "py2-launcher". However, we
325 // cannot read the property at this stage and it will be too late to add
326 // dependencies later.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700327 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, "libsqlite")
Logan Chien02880e42018-11-06 17:30:35 +0800328
329 if ctx.Target().Os.Bionic() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700330 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag,
331 "libc", "libdl", "libm")
Logan Chien02880e42018-11-06 17:30:35 +0800332 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700333 }
334
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800335 case pyVersion3:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700336 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800337 uniqueLibs(ctx, p.properties.Libs, "version.py3.libs",
338 p.properties.Version.Py3.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700339
340 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) {
Dan Willemsen8d4d7be2019-11-04 19:21:04 -0800341 ctx.AddVariationDependencies(nil, pythonLibTag, "py3-stdlib")
342
343 launcherModule := "py3-launcher"
344 if p.bootstrapper.autorun() {
345 launcherModule = "py3-launcher-autorun"
346 }
347 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule)
348
349 // Add py3-launcher shared lib dependencies. Ideally, these should be
350 // derived from the `shared_libs` property of "py3-launcher". However, we
351 // cannot read the property at this stage and it will be too late to add
352 // dependencies later.
353 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, "libsqlite")
354
Dan Willemsend7a1dee2020-01-20 22:08:20 -0800355 if ctx.Device() {
356 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag,
357 "liblog")
358 }
359
Dan Willemsen8d4d7be2019-11-04 19:21:04 -0800360 if ctx.Target().Os.Bionic() {
361 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag,
362 "libc", "libdl", "libm")
363 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700364 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800365 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700366 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
367 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800368 }
369}
370
371// check "libs" duplicates from current module dependencies.
372func uniqueLibs(ctx android.BottomUpMutatorContext,
373 commonLibs []string, versionProp string, versionLibs []string) []string {
374 set := make(map[string]string)
375 ret := []string{}
376
377 // deps from "libs" property.
378 for _, l := range commonLibs {
379 if _, found := set[l]; found {
380 ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l)
381 } else {
382 set[l] = "libs"
383 ret = append(ret, l)
384 }
385 }
386 // deps from "version.pyX.libs" property.
387 for _, l := range versionLibs {
388 if _, found := set[l]; found {
389 ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l])
390 } else {
391 set[l] = versionProp
392 ret = append(ret, l)
393 }
394 }
395
396 return ret
397}
398
Nan Zhangd4e641b2017-07-12 12:55:28 -0700399func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
400 p.GeneratePythonBuildActions(ctx)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700401
Nan Zhangb8fa1972017-12-22 16:12:00 -0800402 // Only Python binaries and test has non-empty bootstrapper.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700403 if p.bootstrapper != nil {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800404 p.walkTransitiveDeps(ctx)
Nan Zhang1db85402017-12-18 13:20:23 -0800405 embeddedLauncher := false
Nan Zhangd4e641b2017-07-12 12:55:28 -0700406 if p.properties.Actual_version == pyVersion2 {
Nan Zhang1db85402017-12-18 13:20:23 -0800407 embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion2)
Dan Willemsen8d4d7be2019-11-04 19:21:04 -0800408 } else {
409 embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion3)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700410 }
411 p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
Nan Zhang1db85402017-12-18 13:20:23 -0800412 embeddedLauncher, p.srcsPathMappings, p.srcsZip, p.depsSrcsZips)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700413 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700414
Logan Chien02880e42018-11-06 17:30:35 +0800415 if p.installer != nil {
416 var sharedLibs []string
417 ctx.VisitDirectDeps(func(dep android.Module) {
418 if ctx.OtherModuleDependencyTag(dep) == launcherSharedLibTag {
419 sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep))
420 }
421 })
422 p.installer.setAndroidMkSharedLibs(sharedLibs)
423
424 if p.installSource.Valid() {
425 p.installer.install(ctx, p.installSource.Path())
426 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700427 }
428
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800429}
430
Nan Zhangd4e641b2017-07-12 12:55:28 -0700431func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800432 // expand python files from "srcs" property.
433 srcs := p.properties.Srcs
Nan Zhangd4e641b2017-07-12 12:55:28 -0700434 exclude_srcs := p.properties.Exclude_srcs
435 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800436 case pyVersion2:
437 srcs = append(srcs, p.properties.Version.Py2.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700438 exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800439 case pyVersion3:
440 srcs = append(srcs, p.properties.Version.Py3.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700441 exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800442 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700443 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
444 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800445 }
Colin Cross8a497952019-03-05 22:25:09 -0800446 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, srcs, exclude_srcs)
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800447 requiresSrcs := true
448 if p.bootstrapper != nil && !p.bootstrapper.autorun() {
449 requiresSrcs = false
450 }
451 if len(expandedSrcs) == 0 && requiresSrcs {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800452 ctx.ModuleErrorf("doesn't have any source files!")
453 }
454
455 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800456 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800457
458 // sanitize pkg_path.
Nan Zhang1db85402017-12-18 13:20:23 -0800459 pkgPath := String(p.properties.Pkg_path)
460 if pkgPath != "" {
461 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
462 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
463 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700464 ctx.PropertyErrorf("pkg_path",
465 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800466 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700467 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800468 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700469 if p.properties.Is_internal != nil && *p.properties.Is_internal {
Nan Zhang1db85402017-12-18 13:20:23 -0800470 pkgPath = filepath.Join(internal, pkgPath)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700471 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800472 } else {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700473 if p.properties.Is_internal != nil && *p.properties.Is_internal {
Nan Zhang1db85402017-12-18 13:20:23 -0800474 pkgPath = internal
Nan Zhangd4e641b2017-07-12 12:55:28 -0700475 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800476 }
477
Nan Zhang1db85402017-12-18 13:20:23 -0800478 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800479
Nan Zhang1db85402017-12-18 13:20:23 -0800480 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800481}
482
483// generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
484// for python/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800485func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800486 expandedSrcs, expandedData android.Paths) {
487 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800488 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800489 destToPySrcs := make(map[string]string)
490 destToPyData := make(map[string]string)
491
492 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800493 if s.Ext() != pyExt && s.Ext() != protoExt {
494 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800495 continue
496 }
Nan Zhang1db85402017-12-18 13:20:23 -0800497 runfilesPath := filepath.Join(pkgPath, s.Rel())
Nan Zhangb8fa1972017-12-22 16:12:00 -0800498 identifiers := strings.Split(strings.TrimSuffix(runfilesPath,
499 filepath.Ext(runfilesPath)), "/")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800500 for _, token := range identifiers {
501 if !pyIdentifierRegexp.MatchString(token) {
502 ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.",
503 runfilesPath, token)
504 }
505 }
506 if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
507 p.srcsPathMappings = append(p.srcsPathMappings,
508 pathMapping{dest: runfilesPath, src: s})
509 }
510 }
511
512 for _, d := range expandedData {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800513 if d.Ext() == pyExt || d.Ext() == protoExt {
514 ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800515 continue
516 }
Nan Zhang1db85402017-12-18 13:20:23 -0800517 runfilesPath := filepath.Join(pkgPath, d.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800518 if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
519 p.dataPathMappings = append(p.dataPathMappings,
520 pathMapping{dest: runfilesPath, src: d})
521 }
522 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800523}
524
Nan Zhang1db85402017-12-18 13:20:23 -0800525// register build actions to zip current module's sources.
526func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800527 relativeRootMap := make(map[string]android.Paths)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800528 pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
529
Nan Zhangb8fa1972017-12-22 16:12:00 -0800530 var protoSrcs android.Paths
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800531 // "srcs" or "data" properties may have filegroup so it might happen that
532 // the relative root for each source path is different.
533 for _, path := range pathMappings {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800534 if path.src.Ext() == protoExt {
535 protoSrcs = append(protoSrcs, path.src)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800536 } else {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800537 var relativeRoot string
538 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
539 if v, found := relativeRootMap[relativeRoot]; found {
540 relativeRootMap[relativeRoot] = append(v, path.src)
541 } else {
542 relativeRootMap[relativeRoot] = android.Paths{path.src}
543 }
544 }
545 }
546 var zips android.Paths
547 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700548 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
549 protoFlags.OutTypeFlag = "--python_out"
550
Nan Zhangb8fa1972017-12-22 16:12:00 -0800551 for _, srcFile := range protoSrcs {
Colin Cross19878da2019-03-28 14:45:07 -0700552 zip := genProto(ctx, srcFile, protoFlags, pkgPath)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800553 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800554 }
555 }
556
Nan Zhangb8fa1972017-12-22 16:12:00 -0800557 if len(relativeRootMap) > 0 {
558 var keys []string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800559
Nan Zhangb8fa1972017-12-22 16:12:00 -0800560 // in order to keep stable order of soong_zip params, we sort the keys here.
561 for k := range relativeRootMap {
562 keys = append(keys, k)
Nan Zhang1db85402017-12-18 13:20:23 -0800563 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800564 sort.Strings(keys)
565
566 parArgs := []string{}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700567 if pkgPath != "" {
568 parArgs = append(parArgs, `-P `+pkgPath)
569 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800570 implicits := android.Paths{}
571 for _, k := range keys {
572 parArgs = append(parArgs, `-C `+k)
573 for _, path := range relativeRootMap[k] {
574 parArgs = append(parArgs, `-f `+path.String())
575 implicits = append(implicits, path)
576 }
577 }
578
579 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
580 ctx.Build(pctx, android.BuildParams{
581 Rule: zip,
582 Description: "python library archive",
583 Output: origSrcsZip,
584 Implicits: implicits,
585 Args: map[string]string{
586 "args": strings.Join(parArgs, " "),
587 },
588 })
589 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800590 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800591 if len(zips) == 1 {
592 return zips[0]
593 } else {
594 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
595 ctx.Build(pctx, android.BuildParams{
596 Rule: combineZip,
597 Description: "combine python library archive",
598 Output: combinedSrcsZip,
599 Inputs: zips,
600 })
601 return combinedSrcsZip
602 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800603}
604
Nan Zhangd4e641b2017-07-12 12:55:28 -0700605func isPythonLibModule(module blueprint.Module) bool {
606 if m, ok := module.(*Module); ok {
607 // Python library has no bootstrapper or installer.
608 if m.bootstrapper != nil || m.installer != nil {
609 return false
610 }
611 return true
612 }
613 return false
614}
615
Nan Zhangb8fa1972017-12-22 16:12:00 -0800616// check Python source/data files duplicates for whole runfiles tree since Python binary/test
617// need collect and zip all srcs of whole transitive dependencies to a final par file.
618func (p *Module) walkTransitiveDeps(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800619 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
620 // check duplicates.
621 destToPySrcs := make(map[string]string)
622 destToPyData := make(map[string]string)
623
624 for _, path := range p.srcsPathMappings {
625 destToPySrcs[path.dest] = path.src.String()
626 }
627 for _, path := range p.dataPathMappings {
628 destToPyData[path.dest] = path.src.String()
629 }
630
Colin Cross6b753602018-06-21 13:03:07 -0700631 seen := make(map[android.Module]bool)
632
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800633 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700634 ctx.WalkDeps(func(child, parent android.Module) bool {
635 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
636 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800637 }
Colin Cross6b753602018-06-21 13:03:07 -0700638 if seen[child] {
639 return false
640 }
641 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800642 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700643 if !isPythonLibModule(child) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700644 panic(fmt.Errorf(
645 "the dependency %q of module %q is not Python library!",
Colin Cross6b753602018-06-21 13:03:07 -0700646 ctx.ModuleName(), ctx.OtherModuleName(child)))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700647 }
Colin Cross6b753602018-06-21 13:03:07 -0700648 if dep, ok := child.(PythonDependency); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800649 srcs := dep.GetSrcsPathMappings()
650 for _, path := range srcs {
651 if !fillInMap(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700652 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800653 continue
654 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800655 }
656 data := dep.GetDataPathMappings()
657 for _, path := range data {
658 fillInMap(ctx, destToPyData,
Colin Cross6b753602018-06-21 13:03:07 -0700659 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800660 }
Nan Zhang1db85402017-12-18 13:20:23 -0800661 p.depsSrcsZips = append(p.depsSrcsZips, dep.GetSrcsZip())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800662 }
Colin Cross6b753602018-06-21 13:03:07 -0700663 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800664 })
665}
666
667func fillInMap(ctx android.ModuleContext, m map[string]string,
668 key, value, curModule, otherModule string) bool {
669 if oldValue, found := m[key]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700670 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800671 " First file: in module %s at path %q."+
672 " Second file: in module %s at path %q.",
673 key, curModule, oldValue, otherModule, value)
674 return false
675 } else {
676 m[key] = value
677 }
678
679 return true
680}
Nan Zhangea568a42017-11-08 21:20:04 -0800681
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000682func (p *Module) InstallInData() bool {
683 return true
684}
685
Nan Zhangea568a42017-11-08 21:20:04 -0800686var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800687var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800688var String = proptools.String