blob: 479c7291dac15e2f67a9f3300c06652b4f6a7673 [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() {
Liz Kammerdd849a82020-06-12 16:38:45 -070033 android.PreDepsMutators(RegisterPythonPreDepsMutators)
34}
35
36func RegisterPythonPreDepsMutators(ctx android.RegisterMutatorsContext) {
37 ctx.BottomUp("version_split", versionSplitMutator()).Parallel()
Nan Zhangdb0b9a32017-02-27 10:12:13 -080038}
39
40// the version properties that apply to python libraries and binaries.
Nan Zhangd4e641b2017-07-12 12:55:28 -070041type VersionProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080042 // true, if the module is required to be built with this version.
Nan Zhangd4e641b2017-07-12 12:55:28 -070043 Enabled *bool `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080044
45 // non-empty list of .py files under this strict Python version.
46 // srcs may reference the outputs of other modules that produce source files like genrule
47 // or filegroup using the syntax ":module".
Colin Cross27b922f2019-03-04 22:35:41 -080048 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070049
50 // list of source files that should not be used to build the Python module.
51 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080052 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080053
54 // list of the Python libraries under this Python version.
Nan Zhangd4e641b2017-07-12 12:55:28 -070055 Libs []string `android:"arch_variant"`
56
57 // true, if the binary is required to be built with embedded launcher.
58 // TODO(nanzhang): Remove this flag when embedded Python3 is supported later.
59 Embedded_launcher *bool `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080060}
61
62// properties that apply to python libraries and binaries.
Nan Zhangd4e641b2017-07-12 12:55:28 -070063type BaseProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080064 // the package path prefix within the output artifact at which to place the source/data
65 // files of the current module.
66 // eg. Pkg_path = "a/b/c"; Other packages can reference this module by using
67 // (from a.b.c import ...) statement.
Nan Zhangbea09752018-05-31 12:49:33 -070068 // if left unspecified, all the source/data files path is unchanged within zip file.
Nan Zhangea568a42017-11-08 21:20:04 -080069 Pkg_path *string `android:"arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070070
71 // true, if the Python module is used internally, eg, Python std libs.
72 Is_internal *bool `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080073
74 // list of source (.py) files compatible both with Python2 and Python3 used to compile the
75 // Python module.
76 // srcs may reference the outputs of other modules that produce source files like genrule
77 // or filegroup using the syntax ":module".
78 // Srcs has to be non-empty.
Colin Cross27b922f2019-03-04 22:35:41 -080079 Srcs []string `android:"path,arch_variant"`
Nan Zhangd4e641b2017-07-12 12:55:28 -070080
81 // list of source files that should not be used to build the C/C++ module.
82 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080083 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080084
85 // list of files or filegroup modules that provide data that should be installed alongside
86 // the test. the file extension can be arbitrary except for (.py).
Colin Cross27b922f2019-03-04 22:35:41 -080087 Data []string `android:"path,arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080088
89 // list of the Python libraries compatible both with Python2 and Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -070090 Libs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080091
92 Version struct {
93 // all the "srcs" or Python dependencies that are to be used only for Python2.
Nan Zhangd4e641b2017-07-12 12:55:28 -070094 Py2 VersionProperties `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080095
96 // all the "srcs" or Python dependencies that are to be used only for Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -070097 Py3 VersionProperties `android:"arch_variant"`
98 } `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080099
100 // the actual version each module uses after variations created.
101 // this property name is hidden from users' perspectives, and soong will populate it during
102 // runtime.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700103 Actual_version string `blueprint:"mutated"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800104}
105
106type pathMapping struct {
107 dest string
108 src android.Path
109}
110
Nan Zhangd4e641b2017-07-12 12:55:28 -0700111type Module struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800112 android.ModuleBase
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700113 android.DefaultableModuleBase
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800114
Nan Zhangb8fa1972017-12-22 16:12:00 -0800115 properties BaseProperties
116 protoProperties android.ProtoProperties
Nan Zhangd4e641b2017-07-12 12:55:28 -0700117
118 // initialize before calling Init
119 hod android.HostOrDeviceSupported
120 multilib android.Multilib
121
122 // the bootstrapper is used to bootstrap .par executable.
123 // bootstrapper might be nil (Python library module).
124 bootstrapper bootstrapper
125
126 // the installer might be nil.
127 installer installer
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800128
129 // the Python files of current module after expanding source dependencies.
130 // pathMapping: <dest: runfile_path, src: source_path>
131 srcsPathMappings []pathMapping
132
133 // the data files of current module after expanding source dependencies.
134 // pathMapping: <dest: runfile_path, src: source_path>
135 dataPathMappings []pathMapping
136
Nan Zhang1db85402017-12-18 13:20:23 -0800137 // the zip filepath for zipping current module source/data files.
138 srcsZip android.Path
Nan Zhangd4e641b2017-07-12 12:55:28 -0700139
Nan Zhang1db85402017-12-18 13:20:23 -0800140 // dependency modules' zip filepath for zipping current module source/data files.
141 depsSrcsZips android.Paths
Nan Zhangd4e641b2017-07-12 12:55:28 -0700142
143 // (.intermediate) module output path as installation source.
144 installSource android.OptionalPath
145
Nan Zhang5323f8e2017-05-10 13:37:54 -0700146 subAndroidMkOnce map[subAndroidMkProvider]bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800147}
148
Nan Zhangd4e641b2017-07-12 12:55:28 -0700149func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
150 return &Module{
151 hod: hod,
152 multilib: multilib,
153 }
154}
155
156type bootstrapper interface {
157 bootstrapperProps() []interface{}
Nan Zhang1db85402017-12-18 13:20:23 -0800158 bootstrap(ctx android.ModuleContext, ActualVersion string, embeddedLauncher bool,
159 srcsPathMappings []pathMapping, srcsZip android.Path,
160 depsSrcsZips android.Paths) android.OptionalPath
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800161
162 autorun() bool
Nan Zhangd4e641b2017-07-12 12:55:28 -0700163}
164
165type installer interface {
166 install(ctx android.ModuleContext, path android.Path)
Logan Chien02880e42018-11-06 17:30:35 +0800167 setAndroidMkSharedLibs(sharedLibs []string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800168}
169
170type PythonDependency interface {
171 GetSrcsPathMappings() []pathMapping
172 GetDataPathMappings() []pathMapping
Nan Zhang1db85402017-12-18 13:20:23 -0800173 GetSrcsZip() android.Path
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800174}
175
Nan Zhangd4e641b2017-07-12 12:55:28 -0700176func (p *Module) GetSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800177 return p.srcsPathMappings
178}
179
Nan Zhangd4e641b2017-07-12 12:55:28 -0700180func (p *Module) GetDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800181 return p.dataPathMappings
182}
183
Nan Zhang1db85402017-12-18 13:20:23 -0800184func (p *Module) GetSrcsZip() android.Path {
185 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800186}
187
Nan Zhangd4e641b2017-07-12 12:55:28 -0700188var _ PythonDependency = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800189
Nan Zhangd4e641b2017-07-12 12:55:28 -0700190var _ android.AndroidMkDataProvider = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800191
Nan Zhangd4e641b2017-07-12 12:55:28 -0700192func (p *Module) Init() android.Module {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800193
Nan Zhangb8fa1972017-12-22 16:12:00 -0800194 p.AddProperties(&p.properties, &p.protoProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700195 if p.bootstrapper != nil {
196 p.AddProperties(p.bootstrapper.bootstrapperProps()...)
197 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800198
Nan Zhangd4e641b2017-07-12 12:55:28 -0700199 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700200 android.InitDefaultableModule(p)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800201
Nan Zhangd4e641b2017-07-12 12:55:28 -0700202 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800203}
204
Nan Zhangd4e641b2017-07-12 12:55:28 -0700205type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800206 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700207 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800208}
209
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800210var (
Logan Chien02880e42018-11-06 17:30:35 +0800211 pythonLibTag = dependencyTag{name: "pythonLib"}
212 launcherTag = dependencyTag{name: "launcher"}
213 launcherSharedLibTag = dependencyTag{name: "launcherSharedLib"}
214 pyIdentifierRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
215 pyExt = ".py"
216 protoExt = ".proto"
217 pyVersion2 = "PY2"
218 pyVersion3 = "PY3"
219 initFileName = "__init__.py"
220 mainFileName = "__main__.py"
221 entryPointFile = "entry_point.txt"
222 parFileExt = ".zip"
223 internal = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800224)
225
226// create version variants for modules.
227func versionSplitMutator() func(android.BottomUpMutatorContext) {
228 return func(mctx android.BottomUpMutatorContext) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700229 if base, ok := mctx.Module().(*Module); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800230 versionNames := []string{}
Liz Kammerdd849a82020-06-12 16:38:45 -0700231 // PY3 is first so that we alias the PY3 variant rather than PY2 if both
232 // are available
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800233 if !(base.properties.Version.Py3.Enabled != nil &&
234 *(base.properties.Version.Py3.Enabled) == false) {
235 versionNames = append(versionNames, pyVersion3)
236 }
Liz Kammerdd849a82020-06-12 16:38:45 -0700237 if base.properties.Version.Py2.Enabled != nil &&
238 *(base.properties.Version.Py2.Enabled) == true {
239 versionNames = append(versionNames, pyVersion2)
240 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800241 modules := mctx.CreateVariations(versionNames...)
Liz Kammerdd849a82020-06-12 16:38:45 -0700242 if len(versionNames) > 0 {
243 mctx.AliasVariation(versionNames[0])
244 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800245 for i, v := range versionNames {
246 // set the actual version for Python module.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700247 modules[i].(*Module).properties.Actual_version = v
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800248 }
249 }
250 }
251}
252
Nan Zhangb8bdacf2017-12-06 15:13:10 -0800253func (p *Module) HostToolPath() android.OptionalPath {
254 if p.installer == nil {
255 // python_library is just meta module, and doesn't have any installer.
256 return android.OptionalPath{}
257 }
258 return android.OptionalPathForPath(p.installer.(*binaryDecorator).path)
259}
260
Liz Kammere0070ee2020-06-22 11:52:59 -0700261func (p *Module) OutputFiles(tag string) (android.Paths, error) {
262 switch tag {
263 case "":
264 if outputFile := p.installSource; outputFile.Valid() {
265 return android.Paths{outputFile.Path()}, nil
266 }
267 return android.Paths{}, nil
268 default:
269 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
270 }
271}
272
Nan Zhangd4e641b2017-07-12 12:55:28 -0700273func (p *Module) isEmbeddedLauncherEnabled(actual_version string) bool {
274 switch actual_version {
275 case pyVersion2:
Colin Crossff3ae9d2018-04-10 16:15:18 -0700276 return Bool(p.properties.Version.Py2.Embedded_launcher)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700277 case pyVersion3:
Colin Crossff3ae9d2018-04-10 16:15:18 -0700278 return Bool(p.properties.Version.Py3.Embedded_launcher)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700279 }
280
281 return false
282}
283
Nan Zhangb8fa1972017-12-22 16:12:00 -0800284func hasSrcExt(srcs []string, ext string) bool {
285 for _, src := range srcs {
286 if filepath.Ext(src) == ext {
287 return true
288 }
289 }
290
291 return false
292}
293
294func (p *Module) hasSrcExt(ctx android.BottomUpMutatorContext, ext string) bool {
295 if hasSrcExt(p.properties.Srcs, protoExt) {
296 return true
297 }
298 switch p.properties.Actual_version {
299 case pyVersion2:
300 return hasSrcExt(p.properties.Version.Py2.Srcs, protoExt)
301 case pyVersion3:
302 return hasSrcExt(p.properties.Version.Py3.Srcs, protoExt)
303 default:
304 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
305 p.properties.Actual_version, ctx.ModuleName()))
306 }
307}
308
Nan Zhangd4e641b2017-07-12 12:55:28 -0700309func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700310 android.ProtoDeps(ctx, &p.protoProperties)
311
Nan Zhangb8fa1972017-12-22 16:12:00 -0800312 if p.hasSrcExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
313 ctx.AddVariationDependencies(nil, pythonLibTag, "libprotobuf-python")
314 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700315 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800316 case pyVersion2:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700317 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800318 uniqueLibs(ctx, p.properties.Libs, "version.py2.libs",
319 p.properties.Version.Py2.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700320
321 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) {
322 ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib")
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800323
324 launcherModule := "py2-launcher"
325 if p.bootstrapper.autorun() {
326 launcherModule = "py2-launcher-autorun"
327 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700328 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule)
Logan Chien02880e42018-11-06 17:30:35 +0800329
330 // Add py2-launcher shared lib dependencies. Ideally, these should be
331 // derived from the `shared_libs` property of "py2-launcher". However, we
332 // cannot read the property at this stage and it will be too late to add
333 // dependencies later.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700334 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, "libsqlite")
Logan Chien02880e42018-11-06 17:30:35 +0800335
336 if ctx.Target().Os.Bionic() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700337 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag,
338 "libc", "libdl", "libm")
Logan Chien02880e42018-11-06 17:30:35 +0800339 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700340 }
341
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800342 case pyVersion3:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700343 ctx.AddVariationDependencies(nil, pythonLibTag,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800344 uniqueLibs(ctx, p.properties.Libs, "version.py3.libs",
345 p.properties.Version.Py3.Libs)...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700346
347 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) {
Dan Willemsen8d4d7be2019-11-04 19:21:04 -0800348 ctx.AddVariationDependencies(nil, pythonLibTag, "py3-stdlib")
349
350 launcherModule := "py3-launcher"
351 if p.bootstrapper.autorun() {
352 launcherModule = "py3-launcher-autorun"
353 }
354 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule)
355
356 // Add py3-launcher shared lib dependencies. Ideally, these should be
357 // derived from the `shared_libs` property of "py3-launcher". However, we
358 // cannot read the property at this stage and it will be too late to add
359 // dependencies later.
360 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, "libsqlite")
361
Dan Willemsend7a1dee2020-01-20 22:08:20 -0800362 if ctx.Device() {
363 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag,
364 "liblog")
365 }
366
Dan Willemsen8d4d7be2019-11-04 19:21:04 -0800367 if ctx.Target().Os.Bionic() {
368 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag,
369 "libc", "libdl", "libm")
370 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700371 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800372 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700373 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
374 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800375 }
376}
377
378// check "libs" duplicates from current module dependencies.
379func uniqueLibs(ctx android.BottomUpMutatorContext,
380 commonLibs []string, versionProp string, versionLibs []string) []string {
381 set := make(map[string]string)
382 ret := []string{}
383
384 // deps from "libs" property.
385 for _, l := range commonLibs {
386 if _, found := set[l]; found {
387 ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l)
388 } else {
389 set[l] = "libs"
390 ret = append(ret, l)
391 }
392 }
393 // deps from "version.pyX.libs" property.
394 for _, l := range versionLibs {
395 if _, found := set[l]; found {
396 ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l])
397 } else {
398 set[l] = versionProp
399 ret = append(ret, l)
400 }
401 }
402
403 return ret
404}
405
Nan Zhangd4e641b2017-07-12 12:55:28 -0700406func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
407 p.GeneratePythonBuildActions(ctx)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700408
Nan Zhangb8fa1972017-12-22 16:12:00 -0800409 // Only Python binaries and test has non-empty bootstrapper.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700410 if p.bootstrapper != nil {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800411 p.walkTransitiveDeps(ctx)
Nan Zhang1db85402017-12-18 13:20:23 -0800412 embeddedLauncher := false
Nan Zhangd4e641b2017-07-12 12:55:28 -0700413 if p.properties.Actual_version == pyVersion2 {
Nan Zhang1db85402017-12-18 13:20:23 -0800414 embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion2)
Dan Willemsen8d4d7be2019-11-04 19:21:04 -0800415 } else {
416 embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion3)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700417 }
418 p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
Nan Zhang1db85402017-12-18 13:20:23 -0800419 embeddedLauncher, p.srcsPathMappings, p.srcsZip, p.depsSrcsZips)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700420 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700421
Logan Chien02880e42018-11-06 17:30:35 +0800422 if p.installer != nil {
423 var sharedLibs []string
424 ctx.VisitDirectDeps(func(dep android.Module) {
425 if ctx.OtherModuleDependencyTag(dep) == launcherSharedLibTag {
426 sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep))
427 }
428 })
429 p.installer.setAndroidMkSharedLibs(sharedLibs)
430
431 if p.installSource.Valid() {
432 p.installer.install(ctx, p.installSource.Path())
433 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700434 }
435
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800436}
437
Nan Zhangd4e641b2017-07-12 12:55:28 -0700438func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800439 // expand python files from "srcs" property.
440 srcs := p.properties.Srcs
Nan Zhangd4e641b2017-07-12 12:55:28 -0700441 exclude_srcs := p.properties.Exclude_srcs
442 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800443 case pyVersion2:
444 srcs = append(srcs, p.properties.Version.Py2.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700445 exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800446 case pyVersion3:
447 srcs = append(srcs, p.properties.Version.Py3.Srcs...)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700448 exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800449 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700450 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
451 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800452 }
Colin Cross8a497952019-03-05 22:25:09 -0800453 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, srcs, exclude_srcs)
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800454 requiresSrcs := true
455 if p.bootstrapper != nil && !p.bootstrapper.autorun() {
456 requiresSrcs = false
457 }
458 if len(expandedSrcs) == 0 && requiresSrcs {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800459 ctx.ModuleErrorf("doesn't have any source files!")
460 }
461
462 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800463 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800464
465 // sanitize pkg_path.
Nan Zhang1db85402017-12-18 13:20:23 -0800466 pkgPath := String(p.properties.Pkg_path)
467 if pkgPath != "" {
468 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
469 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
470 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700471 ctx.PropertyErrorf("pkg_path",
472 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800473 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700474 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800475 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700476 if p.properties.Is_internal != nil && *p.properties.Is_internal {
Nan Zhang1db85402017-12-18 13:20:23 -0800477 pkgPath = filepath.Join(internal, pkgPath)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700478 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800479 } else {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700480 if p.properties.Is_internal != nil && *p.properties.Is_internal {
Nan Zhang1db85402017-12-18 13:20:23 -0800481 pkgPath = internal
Nan Zhangd4e641b2017-07-12 12:55:28 -0700482 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800483 }
484
Nan Zhang1db85402017-12-18 13:20:23 -0800485 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800486
Nan Zhang1db85402017-12-18 13:20:23 -0800487 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800488}
489
490// generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
491// for python/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800492func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800493 expandedSrcs, expandedData android.Paths) {
494 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800495 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800496 destToPySrcs := make(map[string]string)
497 destToPyData := make(map[string]string)
498
499 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800500 if s.Ext() != pyExt && s.Ext() != protoExt {
501 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800502 continue
503 }
Nan Zhang1db85402017-12-18 13:20:23 -0800504 runfilesPath := filepath.Join(pkgPath, s.Rel())
Nan Zhangb8fa1972017-12-22 16:12:00 -0800505 identifiers := strings.Split(strings.TrimSuffix(runfilesPath,
506 filepath.Ext(runfilesPath)), "/")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800507 for _, token := range identifiers {
508 if !pyIdentifierRegexp.MatchString(token) {
509 ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.",
510 runfilesPath, token)
511 }
512 }
513 if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
514 p.srcsPathMappings = append(p.srcsPathMappings,
515 pathMapping{dest: runfilesPath, src: s})
516 }
517 }
518
519 for _, d := range expandedData {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800520 if d.Ext() == pyExt || d.Ext() == protoExt {
521 ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800522 continue
523 }
Nan Zhang1db85402017-12-18 13:20:23 -0800524 runfilesPath := filepath.Join(pkgPath, d.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800525 if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
526 p.dataPathMappings = append(p.dataPathMappings,
527 pathMapping{dest: runfilesPath, src: d})
528 }
529 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800530}
531
Nan Zhang1db85402017-12-18 13:20:23 -0800532// register build actions to zip current module's sources.
533func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800534 relativeRootMap := make(map[string]android.Paths)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800535 pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
536
Nan Zhangb8fa1972017-12-22 16:12:00 -0800537 var protoSrcs android.Paths
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800538 // "srcs" or "data" properties may have filegroup so it might happen that
539 // the relative root for each source path is different.
540 for _, path := range pathMappings {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800541 if path.src.Ext() == protoExt {
542 protoSrcs = append(protoSrcs, path.src)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800543 } else {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800544 var relativeRoot string
545 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
546 if v, found := relativeRootMap[relativeRoot]; found {
547 relativeRootMap[relativeRoot] = append(v, path.src)
548 } else {
549 relativeRootMap[relativeRoot] = android.Paths{path.src}
550 }
551 }
552 }
553 var zips android.Paths
554 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700555 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
556 protoFlags.OutTypeFlag = "--python_out"
557
Nan Zhangb8fa1972017-12-22 16:12:00 -0800558 for _, srcFile := range protoSrcs {
Colin Cross19878da2019-03-28 14:45:07 -0700559 zip := genProto(ctx, srcFile, protoFlags, pkgPath)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800560 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800561 }
562 }
563
Nan Zhangb8fa1972017-12-22 16:12:00 -0800564 if len(relativeRootMap) > 0 {
565 var keys []string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800566
Nan Zhangb8fa1972017-12-22 16:12:00 -0800567 // in order to keep stable order of soong_zip params, we sort the keys here.
568 for k := range relativeRootMap {
569 keys = append(keys, k)
Nan Zhang1db85402017-12-18 13:20:23 -0800570 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800571 sort.Strings(keys)
572
573 parArgs := []string{}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700574 if pkgPath != "" {
575 parArgs = append(parArgs, `-P `+pkgPath)
576 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800577 implicits := android.Paths{}
578 for _, k := range keys {
579 parArgs = append(parArgs, `-C `+k)
580 for _, path := range relativeRootMap[k] {
581 parArgs = append(parArgs, `-f `+path.String())
582 implicits = append(implicits, path)
583 }
584 }
585
586 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
587 ctx.Build(pctx, android.BuildParams{
588 Rule: zip,
589 Description: "python library archive",
590 Output: origSrcsZip,
591 Implicits: implicits,
592 Args: map[string]string{
593 "args": strings.Join(parArgs, " "),
594 },
595 })
596 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800597 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800598 if len(zips) == 1 {
599 return zips[0]
600 } else {
601 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
602 ctx.Build(pctx, android.BuildParams{
603 Rule: combineZip,
604 Description: "combine python library archive",
605 Output: combinedSrcsZip,
606 Inputs: zips,
607 })
608 return combinedSrcsZip
609 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800610}
611
Nan Zhangd4e641b2017-07-12 12:55:28 -0700612func isPythonLibModule(module blueprint.Module) bool {
613 if m, ok := module.(*Module); ok {
614 // Python library has no bootstrapper or installer.
615 if m.bootstrapper != nil || m.installer != nil {
616 return false
617 }
618 return true
619 }
620 return false
621}
622
Nan Zhangb8fa1972017-12-22 16:12:00 -0800623// check Python source/data files duplicates for whole runfiles tree since Python binary/test
624// need collect and zip all srcs of whole transitive dependencies to a final par file.
625func (p *Module) walkTransitiveDeps(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800626 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
627 // check duplicates.
628 destToPySrcs := make(map[string]string)
629 destToPyData := make(map[string]string)
630
631 for _, path := range p.srcsPathMappings {
632 destToPySrcs[path.dest] = path.src.String()
633 }
634 for _, path := range p.dataPathMappings {
635 destToPyData[path.dest] = path.src.String()
636 }
637
Colin Cross6b753602018-06-21 13:03:07 -0700638 seen := make(map[android.Module]bool)
639
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800640 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700641 ctx.WalkDeps(func(child, parent android.Module) bool {
642 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
643 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800644 }
Colin Cross6b753602018-06-21 13:03:07 -0700645 if seen[child] {
646 return false
647 }
648 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800649 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700650 if !isPythonLibModule(child) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700651 panic(fmt.Errorf(
652 "the dependency %q of module %q is not Python library!",
Colin Cross6b753602018-06-21 13:03:07 -0700653 ctx.ModuleName(), ctx.OtherModuleName(child)))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700654 }
Colin Cross6b753602018-06-21 13:03:07 -0700655 if dep, ok := child.(PythonDependency); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800656 srcs := dep.GetSrcsPathMappings()
657 for _, path := range srcs {
658 if !fillInMap(ctx, destToPySrcs,
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 continue
661 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800662 }
663 data := dep.GetDataPathMappings()
664 for _, path := range data {
665 fillInMap(ctx, destToPyData,
Colin Cross6b753602018-06-21 13:03:07 -0700666 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800667 }
Nan Zhang1db85402017-12-18 13:20:23 -0800668 p.depsSrcsZips = append(p.depsSrcsZips, dep.GetSrcsZip())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800669 }
Colin Cross6b753602018-06-21 13:03:07 -0700670 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800671 })
672}
673
674func fillInMap(ctx android.ModuleContext, m map[string]string,
675 key, value, curModule, otherModule string) bool {
676 if oldValue, found := m[key]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700677 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800678 " First file: in module %s at path %q."+
679 " Second file: in module %s at path %q.",
680 key, curModule, oldValue, otherModule, value)
681 return false
682 } else {
683 m[key] = value
684 }
685
686 return true
687}
Nan Zhangea568a42017-11-08 21:20:04 -0800688
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000689func (p *Module) InstallInData() bool {
690 return true
691}
692
Nan Zhangea568a42017-11-08 21:20:04 -0800693var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800694var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800695var String = proptools.String