blob: d7b1bbad4da04c7d78af3cf20a353817dd82e262 [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) {
Colin Crosse20113d2020-11-22 19:37:44 -080037 ctx.BottomUp("python_version", 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
Colin Cross1bc63932020-11-22 20:12:45 -080089 // list of java modules that provide data that should be installed alongside the test.
90 Java_data []string
91
Nan Zhangdb0b9a32017-02-27 10:12:13 -080092 // list of the Python libraries compatible both with Python2 and Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -070093 Libs []string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080094
95 Version struct {
96 // all the "srcs" or Python dependencies that are to be used only for Python2.
Nan Zhangd4e641b2017-07-12 12:55:28 -070097 Py2 VersionProperties `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080098
99 // all the "srcs" or Python dependencies that are to be used only for Python3.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700100 Py3 VersionProperties `android:"arch_variant"`
101 } `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800102
103 // the actual version each module uses after variations created.
104 // this property name is hidden from users' perspectives, and soong will populate it during
105 // runtime.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700106 Actual_version string `blueprint:"mutated"`
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700107
108 // true, if the module is required to be built with actual_version.
109 Enabled *bool `blueprint:"mutated"`
110
111 // true, if the binary is required to be built with embedded launcher.
112 // TODO(nanzhang): Remove this flag when embedded Python3 is supported later.
113 Embedded_launcher *bool `blueprint:"mutated"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800114}
115
116type pathMapping struct {
117 dest string
118 src android.Path
119}
120
Nan Zhangd4e641b2017-07-12 12:55:28 -0700121type Module struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800122 android.ModuleBase
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700123 android.DefaultableModuleBase
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800124
Nan Zhangb8fa1972017-12-22 16:12:00 -0800125 properties BaseProperties
126 protoProperties android.ProtoProperties
Nan Zhangd4e641b2017-07-12 12:55:28 -0700127
128 // initialize before calling Init
129 hod android.HostOrDeviceSupported
130 multilib android.Multilib
131
132 // the bootstrapper is used to bootstrap .par executable.
133 // bootstrapper might be nil (Python library module).
134 bootstrapper bootstrapper
135
136 // the installer might be nil.
137 installer installer
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800138
139 // the Python files of current module after expanding source dependencies.
140 // pathMapping: <dest: runfile_path, src: source_path>
141 srcsPathMappings []pathMapping
142
143 // the data files of current module after expanding source dependencies.
144 // pathMapping: <dest: runfile_path, src: source_path>
145 dataPathMappings []pathMapping
146
Nan Zhang1db85402017-12-18 13:20:23 -0800147 // the zip filepath for zipping current module source/data files.
148 srcsZip android.Path
Nan Zhangd4e641b2017-07-12 12:55:28 -0700149
Nan Zhang1db85402017-12-18 13:20:23 -0800150 // dependency modules' zip filepath for zipping current module source/data files.
151 depsSrcsZips android.Paths
Nan Zhangd4e641b2017-07-12 12:55:28 -0700152
153 // (.intermediate) module output path as installation source.
154 installSource android.OptionalPath
155
Nan Zhang5323f8e2017-05-10 13:37:54 -0700156 subAndroidMkOnce map[subAndroidMkProvider]bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800157}
158
Nan Zhangd4e641b2017-07-12 12:55:28 -0700159func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
160 return &Module{
161 hod: hod,
162 multilib: multilib,
163 }
164}
165
166type bootstrapper interface {
167 bootstrapperProps() []interface{}
Nan Zhang1db85402017-12-18 13:20:23 -0800168 bootstrap(ctx android.ModuleContext, ActualVersion string, embeddedLauncher bool,
169 srcsPathMappings []pathMapping, srcsZip android.Path,
170 depsSrcsZips android.Paths) android.OptionalPath
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800171
172 autorun() bool
Nan Zhangd4e641b2017-07-12 12:55:28 -0700173}
174
175type installer interface {
176 install(ctx android.ModuleContext, path android.Path)
Logan Chien02880e42018-11-06 17:30:35 +0800177 setAndroidMkSharedLibs(sharedLibs []string)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800178}
179
180type PythonDependency interface {
181 GetSrcsPathMappings() []pathMapping
182 GetDataPathMappings() []pathMapping
Nan Zhang1db85402017-12-18 13:20:23 -0800183 GetSrcsZip() android.Path
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800184}
185
Nan Zhangd4e641b2017-07-12 12:55:28 -0700186func (p *Module) GetSrcsPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800187 return p.srcsPathMappings
188}
189
Nan Zhangd4e641b2017-07-12 12:55:28 -0700190func (p *Module) GetDataPathMappings() []pathMapping {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800191 return p.dataPathMappings
192}
193
Nan Zhang1db85402017-12-18 13:20:23 -0800194func (p *Module) GetSrcsZip() android.Path {
195 return p.srcsZip
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800196}
197
Nan Zhangd4e641b2017-07-12 12:55:28 -0700198var _ PythonDependency = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800199
Nan Zhangd4e641b2017-07-12 12:55:28 -0700200var _ android.AndroidMkDataProvider = (*Module)(nil)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800201
Nan Zhangd4e641b2017-07-12 12:55:28 -0700202func (p *Module) Init() android.Module {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800203
Nan Zhangb8fa1972017-12-22 16:12:00 -0800204 p.AddProperties(&p.properties, &p.protoProperties)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700205 if p.bootstrapper != nil {
206 p.AddProperties(p.bootstrapper.bootstrapperProps()...)
207 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800208
Nan Zhangd4e641b2017-07-12 12:55:28 -0700209 android.InitAndroidArchModule(p, p.hod, p.multilib)
Nan Zhanga3fc4ba2017-07-20 17:43:37 -0700210 android.InitDefaultableModule(p)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800211
Nan Zhangd4e641b2017-07-12 12:55:28 -0700212 return p
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800213}
214
Nan Zhangd4e641b2017-07-12 12:55:28 -0700215type dependencyTag struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800216 blueprint.BaseDependencyTag
Nan Zhangd4e641b2017-07-12 12:55:28 -0700217 name string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800218}
219
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800220var (
Logan Chien02880e42018-11-06 17:30:35 +0800221 pythonLibTag = dependencyTag{name: "pythonLib"}
Colin Cross1bc63932020-11-22 20:12:45 -0800222 javaDataTag = dependencyTag{name: "javaData"}
Logan Chien02880e42018-11-06 17:30:35 +0800223 launcherTag = dependencyTag{name: "launcher"}
224 launcherSharedLibTag = dependencyTag{name: "launcherSharedLib"}
225 pyIdentifierRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
226 pyExt = ".py"
227 protoExt = ".proto"
228 pyVersion2 = "PY2"
229 pyVersion3 = "PY3"
230 initFileName = "__init__.py"
231 mainFileName = "__main__.py"
232 entryPointFile = "entry_point.txt"
233 parFileExt = ".zip"
234 internal = "internal"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800235)
236
237// create version variants for modules.
238func versionSplitMutator() func(android.BottomUpMutatorContext) {
239 return func(mctx android.BottomUpMutatorContext) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700240 if base, ok := mctx.Module().(*Module); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800241 versionNames := []string{}
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700242 versionProps := []VersionProperties{}
Liz Kammerdd849a82020-06-12 16:38:45 -0700243 // PY3 is first so that we alias the PY3 variant rather than PY2 if both
244 // are available
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800245 if !(base.properties.Version.Py3.Enabled != nil &&
246 *(base.properties.Version.Py3.Enabled) == false) {
247 versionNames = append(versionNames, pyVersion3)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700248 versionProps = append(versionProps, base.properties.Version.Py3)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800249 }
Liz Kammerdd849a82020-06-12 16:38:45 -0700250 if base.properties.Version.Py2.Enabled != nil &&
251 *(base.properties.Version.Py2.Enabled) == true {
252 versionNames = append(versionNames, pyVersion2)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700253 versionProps = append(versionProps, base.properties.Version.Py2)
Liz Kammerdd849a82020-06-12 16:38:45 -0700254 }
Colin Crosse20113d2020-11-22 19:37:44 -0800255 modules := mctx.CreateLocalVariations(versionNames...)
Liz Kammerdd849a82020-06-12 16:38:45 -0700256 if len(versionNames) > 0 {
257 mctx.AliasVariation(versionNames[0])
258 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800259 for i, v := range versionNames {
260 // set the actual version for Python module.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700261 modules[i].(*Module).properties.Actual_version = v
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700262 // append versioned properties for the Python module
263 err := proptools.AppendMatchingProperties([]interface{}{&modules[i].(*Module).properties}, &versionProps[i], nil)
264 if err != nil {
265 panic(err)
266 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800267 }
268 }
269 }
270}
271
Nan Zhangb8bdacf2017-12-06 15:13:10 -0800272func (p *Module) HostToolPath() android.OptionalPath {
273 if p.installer == nil {
274 // python_library is just meta module, and doesn't have any installer.
275 return android.OptionalPath{}
276 }
277 return android.OptionalPathForPath(p.installer.(*binaryDecorator).path)
278}
279
Liz Kammere0070ee2020-06-22 11:52:59 -0700280func (p *Module) OutputFiles(tag string) (android.Paths, error) {
281 switch tag {
282 case "":
283 if outputFile := p.installSource; outputFile.Valid() {
284 return android.Paths{outputFile.Path()}, nil
285 }
286 return android.Paths{}, nil
287 default:
288 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
289 }
290}
291
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700292func (p *Module) isEmbeddedLauncherEnabled() bool {
293 return Bool(p.properties.Embedded_launcher)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700294}
295
Nan Zhangb8fa1972017-12-22 16:12:00 -0800296func hasSrcExt(srcs []string, ext string) bool {
297 for _, src := range srcs {
298 if filepath.Ext(src) == ext {
299 return true
300 }
301 }
302
303 return false
304}
305
306func (p *Module) hasSrcExt(ctx android.BottomUpMutatorContext, ext string) bool {
Liz Kammer3dfd3ce2020-11-16 11:30:55 -0800307 return hasSrcExt(p.properties.Srcs, ext)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800308}
309
Nan Zhangd4e641b2017-07-12 12:55:28 -0700310func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfe17f6f2019-03-28 19:30:56 -0700311 android.ProtoDeps(ctx, &p.protoProperties)
312
Colin Crosse20113d2020-11-22 19:37:44 -0800313 versionVariation := []blueprint.Variation{
314 {"python_version", p.properties.Actual_version},
Nan Zhangb8fa1972017-12-22 16:12:00 -0800315 }
Colin Crosse20113d2020-11-22 19:37:44 -0800316
317 if p.hasSrcExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
318 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python")
319 }
320 ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...)
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700321
Nan Zhangd4e641b2017-07-12 12:55:28 -0700322 switch p.properties.Actual_version {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800323 case pyVersion2:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700324
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700325 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled() {
Colin Crosse20113d2020-11-22 19:37:44 -0800326 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "py2-stdlib")
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800327
328 launcherModule := "py2-launcher"
329 if p.bootstrapper.autorun() {
330 launcherModule = "py2-launcher-autorun"
331 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700332 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule)
Logan Chien02880e42018-11-06 17:30:35 +0800333
334 // Add py2-launcher shared lib dependencies. Ideally, these should be
335 // derived from the `shared_libs` property of "py2-launcher". However, we
336 // cannot read the property at this stage and it will be too late to add
337 // dependencies later.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700338 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, "libsqlite")
Logan Chien02880e42018-11-06 17:30:35 +0800339
340 if ctx.Target().Os.Bionic() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700341 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag,
342 "libc", "libdl", "libm")
Logan Chien02880e42018-11-06 17:30:35 +0800343 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700344 }
345
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800346 case pyVersion3:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700347
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700348 if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled() {
Colin Crosse20113d2020-11-22 19:37:44 -0800349 ctx.AddVariationDependencies(versionVariation, pythonLibTag, "py3-stdlib")
Dan Willemsen8d4d7be2019-11-04 19:21:04 -0800350
351 launcherModule := "py3-launcher"
352 if p.bootstrapper.autorun() {
353 launcherModule = "py3-launcher-autorun"
354 }
355 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule)
356
357 // Add py3-launcher shared lib dependencies. Ideally, these should be
358 // derived from the `shared_libs` property of "py3-launcher". However, we
359 // cannot read the property at this stage and it will be too late to add
360 // dependencies later.
361 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, "libsqlite")
362
Dan Willemsend7a1dee2020-01-20 22:08:20 -0800363 if ctx.Device() {
364 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag,
365 "liblog")
366 }
367
Dan Willemsen8d4d7be2019-11-04 19:21:04 -0800368 if ctx.Target().Os.Bionic() {
369 ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag,
370 "libc", "libdl", "libm")
371 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700372 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800373 default:
Nan Zhangd4e641b2017-07-12 12:55:28 -0700374 panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
375 p.properties.Actual_version, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800376 }
Colin Cross1bc63932020-11-22 20:12:45 -0800377
378 // Emulate the data property for java_data but with the arch variation overridden to "common"
379 // so that it can point to java modules.
380 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
381 ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800382}
383
Nan Zhangd4e641b2017-07-12 12:55:28 -0700384func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
385 p.GeneratePythonBuildActions(ctx)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700386
Nan Zhangb8fa1972017-12-22 16:12:00 -0800387 // Only Python binaries and test has non-empty bootstrapper.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700388 if p.bootstrapper != nil {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800389 p.walkTransitiveDeps(ctx)
Nan Zhang1db85402017-12-18 13:20:23 -0800390 embeddedLauncher := false
Liz Kammer7e93e5b2020-10-30 15:44:09 -0700391 embeddedLauncher = p.isEmbeddedLauncherEnabled()
Nan Zhangd4e641b2017-07-12 12:55:28 -0700392 p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
Nan Zhang1db85402017-12-18 13:20:23 -0800393 embeddedLauncher, p.srcsPathMappings, p.srcsZip, p.depsSrcsZips)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700394 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700395
Logan Chien02880e42018-11-06 17:30:35 +0800396 if p.installer != nil {
397 var sharedLibs []string
398 ctx.VisitDirectDeps(func(dep android.Module) {
399 if ctx.OtherModuleDependencyTag(dep) == launcherSharedLibTag {
400 sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep))
401 }
402 })
403 p.installer.setAndroidMkSharedLibs(sharedLibs)
404
405 if p.installSource.Valid() {
406 p.installer.install(ctx, p.installSource.Path())
407 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700408 }
409
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800410}
411
Nan Zhangd4e641b2017-07-12 12:55:28 -0700412func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800413 // expand python files from "srcs" property.
414 srcs := p.properties.Srcs
Nan Zhangd4e641b2017-07-12 12:55:28 -0700415 exclude_srcs := p.properties.Exclude_srcs
Colin Cross8a497952019-03-05 22:25:09 -0800416 expandedSrcs := android.PathsForModuleSrcExcludes(ctx, srcs, exclude_srcs)
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800417 requiresSrcs := true
418 if p.bootstrapper != nil && !p.bootstrapper.autorun() {
419 requiresSrcs = false
420 }
421 if len(expandedSrcs) == 0 && requiresSrcs {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800422 ctx.ModuleErrorf("doesn't have any source files!")
423 }
424
425 // expand data files from "data" property.
Colin Cross8a497952019-03-05 22:25:09 -0800426 expandedData := android.PathsForModuleSrc(ctx, p.properties.Data)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800427
Colin Cross1bc63932020-11-22 20:12:45 -0800428 // Emulate the data property for java_data dependencies.
429 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
430 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
431 }
432
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800433 // sanitize pkg_path.
Nan Zhang1db85402017-12-18 13:20:23 -0800434 pkgPath := String(p.properties.Pkg_path)
435 if pkgPath != "" {
436 pkgPath = filepath.Clean(String(p.properties.Pkg_path))
437 if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
438 strings.HasPrefix(pkgPath, "/") {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700439 ctx.PropertyErrorf("pkg_path",
440 "%q must be a relative path contained in par file.",
Nan Zhangea568a42017-11-08 21:20:04 -0800441 String(p.properties.Pkg_path))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700442 return
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800443 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700444 if p.properties.Is_internal != nil && *p.properties.Is_internal {
Nan Zhang1db85402017-12-18 13:20:23 -0800445 pkgPath = filepath.Join(internal, pkgPath)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700446 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800447 } else {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700448 if p.properties.Is_internal != nil && *p.properties.Is_internal {
Nan Zhang1db85402017-12-18 13:20:23 -0800449 pkgPath = internal
Nan Zhangd4e641b2017-07-12 12:55:28 -0700450 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800451 }
452
Nan Zhang1db85402017-12-18 13:20:23 -0800453 p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800454
Nan Zhang1db85402017-12-18 13:20:23 -0800455 p.srcsZip = p.createSrcsZip(ctx, pkgPath)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800456}
457
458// generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
459// for python/data files.
Nan Zhang1db85402017-12-18 13:20:23 -0800460func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800461 expandedSrcs, expandedData android.Paths) {
462 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
Nan Zhangb8fa1972017-12-22 16:12:00 -0800463 // check current module duplicates.
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800464 destToPySrcs := make(map[string]string)
465 destToPyData := make(map[string]string)
466
467 for _, s := range expandedSrcs {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800468 if s.Ext() != pyExt && s.Ext() != protoExt {
469 ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800470 continue
471 }
Nan Zhang1db85402017-12-18 13:20:23 -0800472 runfilesPath := filepath.Join(pkgPath, s.Rel())
Nan Zhangb8fa1972017-12-22 16:12:00 -0800473 identifiers := strings.Split(strings.TrimSuffix(runfilesPath,
474 filepath.Ext(runfilesPath)), "/")
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800475 for _, token := range identifiers {
476 if !pyIdentifierRegexp.MatchString(token) {
477 ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.",
478 runfilesPath, token)
479 }
480 }
481 if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
482 p.srcsPathMappings = append(p.srcsPathMappings,
483 pathMapping{dest: runfilesPath, src: s})
484 }
485 }
486
487 for _, d := range expandedData {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800488 if d.Ext() == pyExt || d.Ext() == protoExt {
489 ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800490 continue
491 }
Nan Zhang1db85402017-12-18 13:20:23 -0800492 runfilesPath := filepath.Join(pkgPath, d.Rel())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800493 if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
494 p.dataPathMappings = append(p.dataPathMappings,
495 pathMapping{dest: runfilesPath, src: d})
496 }
497 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800498}
499
Nan Zhang1db85402017-12-18 13:20:23 -0800500// register build actions to zip current module's sources.
501func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800502 relativeRootMap := make(map[string]android.Paths)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800503 pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
504
Nan Zhangb8fa1972017-12-22 16:12:00 -0800505 var protoSrcs android.Paths
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800506 // "srcs" or "data" properties may have filegroup so it might happen that
507 // the relative root for each source path is different.
508 for _, path := range pathMappings {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800509 if path.src.Ext() == protoExt {
510 protoSrcs = append(protoSrcs, path.src)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800511 } else {
Nan Zhangb8fa1972017-12-22 16:12:00 -0800512 var relativeRoot string
513 relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
514 if v, found := relativeRootMap[relativeRoot]; found {
515 relativeRootMap[relativeRoot] = append(v, path.src)
516 } else {
517 relativeRootMap[relativeRoot] = android.Paths{path.src}
518 }
519 }
520 }
521 var zips android.Paths
522 if len(protoSrcs) > 0 {
Colin Cross19878da2019-03-28 14:45:07 -0700523 protoFlags := android.GetProtoFlags(ctx, &p.protoProperties)
524 protoFlags.OutTypeFlag = "--python_out"
525
Nan Zhangb8fa1972017-12-22 16:12:00 -0800526 for _, srcFile := range protoSrcs {
Colin Cross19878da2019-03-28 14:45:07 -0700527 zip := genProto(ctx, srcFile, protoFlags, pkgPath)
Nan Zhangb8fa1972017-12-22 16:12:00 -0800528 zips = append(zips, zip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800529 }
530 }
531
Nan Zhangb8fa1972017-12-22 16:12:00 -0800532 if len(relativeRootMap) > 0 {
533 var keys []string
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800534
Nan Zhangb8fa1972017-12-22 16:12:00 -0800535 // in order to keep stable order of soong_zip params, we sort the keys here.
536 for k := range relativeRootMap {
537 keys = append(keys, k)
Nan Zhang1db85402017-12-18 13:20:23 -0800538 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800539 sort.Strings(keys)
540
541 parArgs := []string{}
Nan Zhangf0c4e432018-05-22 14:50:18 -0700542 if pkgPath != "" {
543 parArgs = append(parArgs, `-P `+pkgPath)
544 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800545 implicits := android.Paths{}
546 for _, k := range keys {
547 parArgs = append(parArgs, `-C `+k)
548 for _, path := range relativeRootMap[k] {
549 parArgs = append(parArgs, `-f `+path.String())
550 implicits = append(implicits, path)
551 }
552 }
553
554 origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
555 ctx.Build(pctx, android.BuildParams{
556 Rule: zip,
557 Description: "python library archive",
558 Output: origSrcsZip,
559 Implicits: implicits,
560 Args: map[string]string{
561 "args": strings.Join(parArgs, " "),
562 },
563 })
564 zips = append(zips, origSrcsZip)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800565 }
Nan Zhangb8fa1972017-12-22 16:12:00 -0800566 if len(zips) == 1 {
567 return zips[0]
568 } else {
569 combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
570 ctx.Build(pctx, android.BuildParams{
571 Rule: combineZip,
572 Description: "combine python library archive",
573 Output: combinedSrcsZip,
574 Inputs: zips,
575 })
576 return combinedSrcsZip
577 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800578}
579
Nan Zhangd4e641b2017-07-12 12:55:28 -0700580func isPythonLibModule(module blueprint.Module) bool {
581 if m, ok := module.(*Module); ok {
582 // Python library has no bootstrapper or installer.
583 if m.bootstrapper != nil || m.installer != nil {
584 return false
585 }
586 return true
587 }
588 return false
589}
590
Nan Zhangb8fa1972017-12-22 16:12:00 -0800591// check Python source/data files duplicates for whole runfiles tree since Python binary/test
592// need collect and zip all srcs of whole transitive dependencies to a final par file.
593func (p *Module) walkTransitiveDeps(ctx android.ModuleContext) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800594 // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
595 // check duplicates.
596 destToPySrcs := make(map[string]string)
597 destToPyData := make(map[string]string)
598
599 for _, path := range p.srcsPathMappings {
600 destToPySrcs[path.dest] = path.src.String()
601 }
602 for _, path := range p.dataPathMappings {
603 destToPyData[path.dest] = path.src.String()
604 }
605
Colin Cross6b753602018-06-21 13:03:07 -0700606 seen := make(map[android.Module]bool)
607
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800608 // visit all its dependencies in depth first.
Colin Cross6b753602018-06-21 13:03:07 -0700609 ctx.WalkDeps(func(child, parent android.Module) bool {
610 if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
611 return false
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800612 }
Colin Cross6b753602018-06-21 13:03:07 -0700613 if seen[child] {
614 return false
615 }
616 seen[child] = true
Nan Zhangb8fa1972017-12-22 16:12:00 -0800617 // Python modules only can depend on Python libraries.
Colin Cross6b753602018-06-21 13:03:07 -0700618 if !isPythonLibModule(child) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700619 panic(fmt.Errorf(
620 "the dependency %q of module %q is not Python library!",
Colin Cross6b753602018-06-21 13:03:07 -0700621 ctx.ModuleName(), ctx.OtherModuleName(child)))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700622 }
Colin Cross6b753602018-06-21 13:03:07 -0700623 if dep, ok := child.(PythonDependency); ok {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800624 srcs := dep.GetSrcsPathMappings()
625 for _, path := range srcs {
626 if !fillInMap(ctx, destToPySrcs,
Colin Cross6b753602018-06-21 13:03:07 -0700627 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800628 continue
629 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800630 }
631 data := dep.GetDataPathMappings()
632 for _, path := range data {
633 fillInMap(ctx, destToPyData,
Colin Cross6b753602018-06-21 13:03:07 -0700634 path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800635 }
Nan Zhang1db85402017-12-18 13:20:23 -0800636 p.depsSrcsZips = append(p.depsSrcsZips, dep.GetSrcsZip())
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800637 }
Colin Cross6b753602018-06-21 13:03:07 -0700638 return true
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800639 })
640}
641
642func fillInMap(ctx android.ModuleContext, m map[string]string,
643 key, value, curModule, otherModule string) bool {
644 if oldValue, found := m[key]; found {
Nan Zhangbea09752018-05-31 12:49:33 -0700645 ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800646 " First file: in module %s at path %q."+
647 " Second file: in module %s at path %q.",
648 key, curModule, oldValue, otherModule, value)
649 return false
650 } else {
651 m[key] = value
652 }
653
654 return true
655}
Nan Zhangea568a42017-11-08 21:20:04 -0800656
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000657func (p *Module) InstallInData() bool {
658 return true
659}
660
Nan Zhangea568a42017-11-08 21:20:04 -0800661var Bool = proptools.Bool
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800662var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -0800663var String = proptools.String