2017-02-27 19:12:13 +01:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package python
|
|
|
|
|
|
|
|
// This file contains the "Base" module type for building Python program.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2017-07-12 21:55:28 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
|
|
|
|
ctx.BottomUp("version_split", versionSplitMutator()).Parallel()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// the version properties that apply to python libraries and binaries.
|
2017-07-12 21:55:28 +02:00
|
|
|
type VersionProperties struct {
|
2017-02-27 19:12:13 +01:00
|
|
|
// true, if the module is required to be built with this version.
|
2017-07-12 21:55:28 +02:00
|
|
|
Enabled *bool `android:"arch_variant"`
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
// non-empty list of .py files under this strict Python version.
|
|
|
|
// srcs may reference the outputs of other modules that produce source files like genrule
|
|
|
|
// or filegroup using the syntax ":module".
|
2017-07-12 21:55:28 +02:00
|
|
|
Srcs []string `android:"arch_variant"`
|
|
|
|
|
|
|
|
// list of source files that should not be used to build the Python module.
|
|
|
|
// This is most useful in the arch/multilib variants to remove non-common files
|
|
|
|
Exclude_srcs []string `android:"arch_variant"`
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
// list of the Python libraries under this Python version.
|
2017-07-12 21:55:28 +02:00
|
|
|
Libs []string `android:"arch_variant"`
|
|
|
|
|
|
|
|
// true, if the binary is required to be built with embedded launcher.
|
|
|
|
// TODO(nanzhang): Remove this flag when embedded Python3 is supported later.
|
|
|
|
Embedded_launcher *bool `android:"arch_variant"`
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// properties that apply to python libraries and binaries.
|
2017-07-12 21:55:28 +02:00
|
|
|
type BaseProperties struct {
|
2017-02-27 19:12:13 +01:00
|
|
|
// the package path prefix within the output artifact at which to place the source/data
|
|
|
|
// files of the current module.
|
|
|
|
// eg. Pkg_path = "a/b/c"; Other packages can reference this module by using
|
|
|
|
// (from a.b.c import ...) statement.
|
2018-05-31 21:49:33 +02:00
|
|
|
// if left unspecified, all the source/data files path is unchanged within zip file.
|
2017-11-09 06:20:04 +01:00
|
|
|
Pkg_path *string `android:"arch_variant"`
|
2017-07-12 21:55:28 +02:00
|
|
|
|
|
|
|
// true, if the Python module is used internally, eg, Python std libs.
|
|
|
|
Is_internal *bool `android:"arch_variant"`
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
// list of source (.py) files compatible both with Python2 and Python3 used to compile the
|
|
|
|
// Python module.
|
|
|
|
// srcs may reference the outputs of other modules that produce source files like genrule
|
|
|
|
// or filegroup using the syntax ":module".
|
|
|
|
// Srcs has to be non-empty.
|
2017-07-12 21:55:28 +02:00
|
|
|
Srcs []string `android:"arch_variant"`
|
|
|
|
|
|
|
|
// list of source files that should not be used to build the C/C++ module.
|
|
|
|
// This is most useful in the arch/multilib variants to remove non-common files
|
|
|
|
Exclude_srcs []string `android:"arch_variant"`
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
// list of files or filegroup modules that provide data that should be installed alongside
|
|
|
|
// the test. the file extension can be arbitrary except for (.py).
|
2017-07-12 21:55:28 +02:00
|
|
|
Data []string `android:"arch_variant"`
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
// list of the Python libraries compatible both with Python2 and Python3.
|
2017-07-12 21:55:28 +02:00
|
|
|
Libs []string `android:"arch_variant"`
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
Version struct {
|
|
|
|
// all the "srcs" or Python dependencies that are to be used only for Python2.
|
2017-07-12 21:55:28 +02:00
|
|
|
Py2 VersionProperties `android:"arch_variant"`
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
// all the "srcs" or Python dependencies that are to be used only for Python3.
|
2017-07-12 21:55:28 +02:00
|
|
|
Py3 VersionProperties `android:"arch_variant"`
|
|
|
|
} `android:"arch_variant"`
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
// the actual version each module uses after variations created.
|
|
|
|
// this property name is hidden from users' perspectives, and soong will populate it during
|
|
|
|
// runtime.
|
2017-07-12 21:55:28 +02:00
|
|
|
Actual_version string `blueprint:"mutated"`
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type pathMapping struct {
|
|
|
|
dest string
|
|
|
|
src android.Path
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
type Module struct {
|
2017-02-27 19:12:13 +01:00
|
|
|
android.ModuleBase
|
2017-07-21 02:43:37 +02:00
|
|
|
android.DefaultableModuleBase
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-12-23 01:12:00 +01:00
|
|
|
properties BaseProperties
|
|
|
|
protoProperties android.ProtoProperties
|
2017-07-12 21:55:28 +02:00
|
|
|
|
|
|
|
// initialize before calling Init
|
|
|
|
hod android.HostOrDeviceSupported
|
|
|
|
multilib android.Multilib
|
|
|
|
|
|
|
|
// the bootstrapper is used to bootstrap .par executable.
|
|
|
|
// bootstrapper might be nil (Python library module).
|
|
|
|
bootstrapper bootstrapper
|
|
|
|
|
|
|
|
// the installer might be nil.
|
|
|
|
installer installer
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
// the Python files of current module after expanding source dependencies.
|
|
|
|
// pathMapping: <dest: runfile_path, src: source_path>
|
|
|
|
srcsPathMappings []pathMapping
|
|
|
|
|
|
|
|
// the data files of current module after expanding source dependencies.
|
|
|
|
// pathMapping: <dest: runfile_path, src: source_path>
|
|
|
|
dataPathMappings []pathMapping
|
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
// the zip filepath for zipping current module source/data files.
|
|
|
|
srcsZip android.Path
|
2017-07-12 21:55:28 +02:00
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
// dependency modules' zip filepath for zipping current module source/data files.
|
|
|
|
depsSrcsZips android.Paths
|
2017-07-12 21:55:28 +02:00
|
|
|
|
|
|
|
// (.intermediate) module output path as installation source.
|
|
|
|
installSource android.OptionalPath
|
|
|
|
|
2017-05-10 22:37:54 +02:00
|
|
|
subAndroidMkOnce map[subAndroidMkProvider]bool
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
|
|
|
|
return &Module{
|
|
|
|
hod: hod,
|
|
|
|
multilib: multilib,
|
|
|
|
}
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
type bootstrapper interface {
|
|
|
|
bootstrapperProps() []interface{}
|
2017-12-18 22:20:23 +01:00
|
|
|
bootstrap(ctx android.ModuleContext, ActualVersion string, embeddedLauncher bool,
|
|
|
|
srcsPathMappings []pathMapping, srcsZip android.Path,
|
|
|
|
depsSrcsZips android.Paths) android.OptionalPath
|
2017-05-10 22:37:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type installer interface {
|
|
|
|
install(ctx android.ModuleContext, path android.Path)
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
type PythonDependency interface {
|
|
|
|
GetSrcsPathMappings() []pathMapping
|
|
|
|
GetDataPathMappings() []pathMapping
|
2017-12-18 22:20:23 +01:00
|
|
|
GetSrcsZip() android.Path
|
2017-07-12 21:55:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Module) GetSrcsPathMappings() []pathMapping {
|
2017-02-27 19:12:13 +01:00
|
|
|
return p.srcsPathMappings
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
func (p *Module) GetDataPathMappings() []pathMapping {
|
2017-02-27 19:12:13 +01:00
|
|
|
return p.dataPathMappings
|
|
|
|
}
|
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
func (p *Module) GetSrcsZip() android.Path {
|
|
|
|
return p.srcsZip
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
var _ PythonDependency = (*Module)(nil)
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
var _ android.AndroidMkDataProvider = (*Module)(nil)
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
func (p *Module) Init() android.Module {
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-12-23 01:12:00 +01:00
|
|
|
p.AddProperties(&p.properties, &p.protoProperties)
|
2017-07-12 21:55:28 +02:00
|
|
|
if p.bootstrapper != nil {
|
|
|
|
p.AddProperties(p.bootstrapper.bootstrapperProps()...)
|
|
|
|
}
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
android.InitAndroidArchModule(p, p.hod, p.multilib)
|
2017-07-21 02:43:37 +02:00
|
|
|
android.InitDefaultableModule(p)
|
2017-06-24 00:06:31 +02:00
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
return p
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
type dependencyTag struct {
|
2017-02-27 19:12:13 +01:00
|
|
|
blueprint.BaseDependencyTag
|
2017-07-12 21:55:28 +02:00
|
|
|
name string
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2017-07-12 21:55:28 +02:00
|
|
|
pythonLibTag = dependencyTag{name: "pythonLib"}
|
|
|
|
launcherTag = dependencyTag{name: "launcher"}
|
2018-05-16 02:13:38 +02:00
|
|
|
pyIdentifierRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
|
2017-02-27 19:12:13 +01:00
|
|
|
pyExt = ".py"
|
2017-12-23 01:12:00 +01:00
|
|
|
protoExt = ".proto"
|
2017-02-27 19:12:13 +01:00
|
|
|
pyVersion2 = "PY2"
|
|
|
|
pyVersion3 = "PY3"
|
|
|
|
initFileName = "__init__.py"
|
|
|
|
mainFileName = "__main__.py"
|
2017-07-12 21:55:28 +02:00
|
|
|
entryPointFile = "entry_point.txt"
|
2017-02-27 19:12:13 +01:00
|
|
|
parFileExt = ".zip"
|
2017-07-12 21:55:28 +02:00
|
|
|
internal = "internal"
|
2017-02-27 19:12:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// create version variants for modules.
|
|
|
|
func versionSplitMutator() func(android.BottomUpMutatorContext) {
|
|
|
|
return func(mctx android.BottomUpMutatorContext) {
|
2017-07-12 21:55:28 +02:00
|
|
|
if base, ok := mctx.Module().(*Module); ok {
|
2017-02-27 19:12:13 +01:00
|
|
|
versionNames := []string{}
|
|
|
|
if base.properties.Version.Py2.Enabled != nil &&
|
|
|
|
*(base.properties.Version.Py2.Enabled) == true {
|
|
|
|
versionNames = append(versionNames, pyVersion2)
|
|
|
|
}
|
|
|
|
if !(base.properties.Version.Py3.Enabled != nil &&
|
|
|
|
*(base.properties.Version.Py3.Enabled) == false) {
|
|
|
|
versionNames = append(versionNames, pyVersion3)
|
|
|
|
}
|
|
|
|
modules := mctx.CreateVariations(versionNames...)
|
|
|
|
for i, v := range versionNames {
|
|
|
|
// set the actual version for Python module.
|
2017-07-12 21:55:28 +02:00
|
|
|
modules[i].(*Module).properties.Actual_version = v
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 00:13:10 +01:00
|
|
|
func (p *Module) HostToolPath() android.OptionalPath {
|
|
|
|
if p.installer == nil {
|
|
|
|
// python_library is just meta module, and doesn't have any installer.
|
|
|
|
return android.OptionalPath{}
|
|
|
|
}
|
|
|
|
return android.OptionalPathForPath(p.installer.(*binaryDecorator).path)
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
func (p *Module) isEmbeddedLauncherEnabled(actual_version string) bool {
|
|
|
|
switch actual_version {
|
|
|
|
case pyVersion2:
|
2018-04-11 01:15:18 +02:00
|
|
|
return Bool(p.properties.Version.Py2.Embedded_launcher)
|
2017-07-12 21:55:28 +02:00
|
|
|
case pyVersion3:
|
2018-04-11 01:15:18 +02:00
|
|
|
return Bool(p.properties.Version.Py3.Embedded_launcher)
|
2017-07-12 21:55:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-12-23 01:12:00 +01:00
|
|
|
func hasSrcExt(srcs []string, ext string) bool {
|
|
|
|
for _, src := range srcs {
|
|
|
|
if filepath.Ext(src) == ext {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Module) hasSrcExt(ctx android.BottomUpMutatorContext, ext string) bool {
|
|
|
|
if hasSrcExt(p.properties.Srcs, protoExt) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch p.properties.Actual_version {
|
|
|
|
case pyVersion2:
|
|
|
|
return hasSrcExt(p.properties.Version.Py2.Srcs, protoExt)
|
|
|
|
case pyVersion3:
|
|
|
|
return hasSrcExt(p.properties.Version.Py3.Srcs, protoExt)
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
|
|
|
|
p.properties.Actual_version, ctx.ModuleName()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
|
2017-02-27 19:12:13 +01:00
|
|
|
// deps from "data".
|
|
|
|
android.ExtractSourcesDeps(ctx, p.properties.Data)
|
|
|
|
// deps from "srcs".
|
|
|
|
android.ExtractSourcesDeps(ctx, p.properties.Srcs)
|
2018-02-09 22:03:53 +01:00
|
|
|
android.ExtractSourcesDeps(ctx, p.properties.Exclude_srcs)
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-12-23 01:12:00 +01:00
|
|
|
if p.hasSrcExt(ctx, protoExt) && p.Name() != "libprotobuf-python" {
|
|
|
|
ctx.AddVariationDependencies(nil, pythonLibTag, "libprotobuf-python")
|
|
|
|
}
|
2017-07-12 21:55:28 +02:00
|
|
|
switch p.properties.Actual_version {
|
2017-02-27 19:12:13 +01:00
|
|
|
case pyVersion2:
|
|
|
|
// deps from "version.py2.srcs" property.
|
|
|
|
android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Srcs)
|
2018-02-09 22:03:53 +01:00
|
|
|
android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Exclude_srcs)
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
ctx.AddVariationDependencies(nil, pythonLibTag,
|
2017-02-27 19:12:13 +01:00
|
|
|
uniqueLibs(ctx, p.properties.Libs, "version.py2.libs",
|
|
|
|
p.properties.Version.Py2.Libs)...)
|
2017-07-12 21:55:28 +02:00
|
|
|
|
|
|
|
if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion2) {
|
|
|
|
ctx.AddVariationDependencies(nil, pythonLibTag, "py2-stdlib")
|
|
|
|
ctx.AddFarVariationDependencies([]blueprint.Variation{
|
2018-07-23 06:18:45 +02:00
|
|
|
{Mutator: "arch", Variation: ctx.Target().String()},
|
2017-07-12 21:55:28 +02:00
|
|
|
}, launcherTag, "py2-launcher")
|
|
|
|
}
|
|
|
|
|
2017-02-27 19:12:13 +01:00
|
|
|
case pyVersion3:
|
|
|
|
// deps from "version.py3.srcs" property.
|
|
|
|
android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs)
|
2018-02-09 22:03:53 +01:00
|
|
|
android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Exclude_srcs)
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
ctx.AddVariationDependencies(nil, pythonLibTag,
|
2017-02-27 19:12:13 +01:00
|
|
|
uniqueLibs(ctx, p.properties.Libs, "version.py3.libs",
|
|
|
|
p.properties.Version.Py3.Libs)...)
|
2017-07-12 21:55:28 +02:00
|
|
|
|
|
|
|
if p.bootstrapper != nil && p.isEmbeddedLauncherEnabled(pyVersion3) {
|
|
|
|
//TODO(nanzhang): Add embedded launcher for Python3.
|
|
|
|
ctx.PropertyErrorf("version.py3.embedded_launcher",
|
|
|
|
"is not supported yet for Python3.")
|
|
|
|
}
|
2017-02-27 19:12:13 +01:00
|
|
|
default:
|
2017-07-12 21:55:28 +02:00
|
|
|
panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
|
|
|
|
p.properties.Actual_version, ctx.ModuleName()))
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check "libs" duplicates from current module dependencies.
|
|
|
|
func uniqueLibs(ctx android.BottomUpMutatorContext,
|
|
|
|
commonLibs []string, versionProp string, versionLibs []string) []string {
|
|
|
|
set := make(map[string]string)
|
|
|
|
ret := []string{}
|
|
|
|
|
|
|
|
// deps from "libs" property.
|
|
|
|
for _, l := range commonLibs {
|
|
|
|
if _, found := set[l]; found {
|
|
|
|
ctx.PropertyErrorf("libs", "%q has duplicates within libs.", l)
|
|
|
|
} else {
|
|
|
|
set[l] = "libs"
|
|
|
|
ret = append(ret, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// deps from "version.pyX.libs" property.
|
|
|
|
for _, l := range versionLibs {
|
|
|
|
if _, found := set[l]; found {
|
|
|
|
ctx.PropertyErrorf(versionProp, "%q has duplicates within %q.", set[l])
|
|
|
|
} else {
|
|
|
|
set[l] = versionProp
|
|
|
|
ret = append(ret, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|
|
|
p.GeneratePythonBuildActions(ctx)
|
2017-05-10 22:37:54 +02:00
|
|
|
|
2017-12-23 01:12:00 +01:00
|
|
|
// Only Python binaries and test has non-empty bootstrapper.
|
2017-07-12 21:55:28 +02:00
|
|
|
if p.bootstrapper != nil {
|
2017-12-23 01:12:00 +01:00
|
|
|
p.walkTransitiveDeps(ctx)
|
2017-07-12 21:55:28 +02:00
|
|
|
// TODO(nanzhang): Since embedded launcher is not supported for Python3 for now,
|
|
|
|
// so we initialize "embedded_launcher" to false.
|
2017-12-18 22:20:23 +01:00
|
|
|
embeddedLauncher := false
|
2017-07-12 21:55:28 +02:00
|
|
|
if p.properties.Actual_version == pyVersion2 {
|
2017-12-18 22:20:23 +01:00
|
|
|
embeddedLauncher = p.isEmbeddedLauncherEnabled(pyVersion2)
|
2017-07-12 21:55:28 +02:00
|
|
|
}
|
|
|
|
p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version,
|
2017-12-18 22:20:23 +01:00
|
|
|
embeddedLauncher, p.srcsPathMappings, p.srcsZip, p.depsSrcsZips)
|
2017-07-12 21:55:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.installer != nil && p.installSource.Valid() {
|
|
|
|
p.installer.install(ctx, p.installSource.Path())
|
2017-05-10 22:37:54 +02:00
|
|
|
}
|
2017-07-12 21:55:28 +02:00
|
|
|
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
func (p *Module) GeneratePythonBuildActions(ctx android.ModuleContext) {
|
2017-02-27 19:12:13 +01:00
|
|
|
// expand python files from "srcs" property.
|
|
|
|
srcs := p.properties.Srcs
|
2017-07-12 21:55:28 +02:00
|
|
|
exclude_srcs := p.properties.Exclude_srcs
|
|
|
|
switch p.properties.Actual_version {
|
2017-02-27 19:12:13 +01:00
|
|
|
case pyVersion2:
|
|
|
|
srcs = append(srcs, p.properties.Version.Py2.Srcs...)
|
2017-07-12 21:55:28 +02:00
|
|
|
exclude_srcs = append(exclude_srcs, p.properties.Version.Py2.Exclude_srcs...)
|
2017-02-27 19:12:13 +01:00
|
|
|
case pyVersion3:
|
|
|
|
srcs = append(srcs, p.properties.Version.Py3.Srcs...)
|
2017-07-12 21:55:28 +02:00
|
|
|
exclude_srcs = append(exclude_srcs, p.properties.Version.Py3.Exclude_srcs...)
|
2017-02-27 19:12:13 +01:00
|
|
|
default:
|
2017-07-12 21:55:28 +02:00
|
|
|
panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
|
|
|
|
p.properties.Actual_version, ctx.ModuleName()))
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
2017-07-12 21:55:28 +02:00
|
|
|
expandedSrcs := ctx.ExpandSources(srcs, exclude_srcs)
|
2017-02-27 19:12:13 +01:00
|
|
|
if len(expandedSrcs) == 0 {
|
|
|
|
ctx.ModuleErrorf("doesn't have any source files!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// expand data files from "data" property.
|
|
|
|
expandedData := ctx.ExpandSources(p.properties.Data, nil)
|
|
|
|
|
|
|
|
// sanitize pkg_path.
|
2017-12-18 22:20:23 +01:00
|
|
|
pkgPath := String(p.properties.Pkg_path)
|
|
|
|
if pkgPath != "" {
|
|
|
|
pkgPath = filepath.Clean(String(p.properties.Pkg_path))
|
|
|
|
if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") ||
|
|
|
|
strings.HasPrefix(pkgPath, "/") {
|
2017-07-12 21:55:28 +02:00
|
|
|
ctx.PropertyErrorf("pkg_path",
|
|
|
|
"%q must be a relative path contained in par file.",
|
2017-11-09 06:20:04 +01:00
|
|
|
String(p.properties.Pkg_path))
|
2017-07-12 21:55:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.properties.Is_internal != nil && *p.properties.Is_internal {
|
2017-12-18 22:20:23 +01:00
|
|
|
pkgPath = filepath.Join(internal, pkgPath)
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-07-12 21:55:28 +02:00
|
|
|
if p.properties.Is_internal != nil && *p.properties.Is_internal {
|
2017-12-18 22:20:23 +01:00
|
|
|
pkgPath = internal
|
2017-07-12 21:55:28 +02:00
|
|
|
}
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData)
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
p.srcsZip = p.createSrcsZip(ctx, pkgPath)
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
|
|
|
|
// for python/data files.
|
2017-12-18 22:20:23 +01:00
|
|
|
func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string,
|
2017-02-27 19:12:13 +01:00
|
|
|
expandedSrcs, expandedData android.Paths) {
|
|
|
|
// fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
|
2017-12-23 01:12:00 +01:00
|
|
|
// check current module duplicates.
|
2017-02-27 19:12:13 +01:00
|
|
|
destToPySrcs := make(map[string]string)
|
|
|
|
destToPyData := make(map[string]string)
|
|
|
|
|
|
|
|
for _, s := range expandedSrcs {
|
2017-12-23 01:12:00 +01:00
|
|
|
if s.Ext() != pyExt && s.Ext() != protoExt {
|
|
|
|
ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String())
|
2017-02-27 19:12:13 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-12-18 22:20:23 +01:00
|
|
|
runfilesPath := filepath.Join(pkgPath, s.Rel())
|
2017-12-23 01:12:00 +01:00
|
|
|
identifiers := strings.Split(strings.TrimSuffix(runfilesPath,
|
|
|
|
filepath.Ext(runfilesPath)), "/")
|
2017-02-27 19:12:13 +01:00
|
|
|
for _, token := range identifiers {
|
|
|
|
if !pyIdentifierRegexp.MatchString(token) {
|
|
|
|
ctx.PropertyErrorf("srcs", "the path %q contains invalid token %q.",
|
|
|
|
runfilesPath, token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fillInMap(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) {
|
|
|
|
p.srcsPathMappings = append(p.srcsPathMappings,
|
|
|
|
pathMapping{dest: runfilesPath, src: s})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range expandedData {
|
2017-12-23 01:12:00 +01:00
|
|
|
if d.Ext() == pyExt || d.Ext() == protoExt {
|
|
|
|
ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String())
|
2017-02-27 19:12:13 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-12-18 22:20:23 +01:00
|
|
|
runfilesPath := filepath.Join(pkgPath, d.Rel())
|
2017-02-27 19:12:13 +01:00
|
|
|
if fillInMap(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) {
|
|
|
|
p.dataPathMappings = append(p.dataPathMappings,
|
|
|
|
pathMapping{dest: runfilesPath, src: d})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
// register build actions to zip current module's sources.
|
|
|
|
func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path {
|
2017-02-27 19:12:13 +01:00
|
|
|
relativeRootMap := make(map[string]android.Paths)
|
|
|
|
pathMappings := append(p.srcsPathMappings, p.dataPathMappings...)
|
|
|
|
|
2017-12-23 01:12:00 +01:00
|
|
|
var protoSrcs android.Paths
|
2017-02-27 19:12:13 +01:00
|
|
|
// "srcs" or "data" properties may have filegroup so it might happen that
|
|
|
|
// the relative root for each source path is different.
|
|
|
|
for _, path := range pathMappings {
|
2017-12-23 01:12:00 +01:00
|
|
|
if path.src.Ext() == protoExt {
|
|
|
|
protoSrcs = append(protoSrcs, path.src)
|
2017-02-27 19:12:13 +01:00
|
|
|
} else {
|
2017-12-23 01:12:00 +01:00
|
|
|
var relativeRoot string
|
|
|
|
relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel())
|
|
|
|
if v, found := relativeRootMap[relativeRoot]; found {
|
|
|
|
relativeRootMap[relativeRoot] = append(v, path.src)
|
|
|
|
} else {
|
|
|
|
relativeRootMap[relativeRoot] = android.Paths{path.src}
|
|
|
|
}
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-23 01:12:00 +01:00
|
|
|
var zips android.Paths
|
|
|
|
if len(protoSrcs) > 0 {
|
|
|
|
for _, srcFile := range protoSrcs {
|
|
|
|
zip := genProto(ctx, &p.protoProperties, srcFile,
|
|
|
|
android.ProtoFlags(ctx, &p.protoProperties), pkgPath)
|
|
|
|
zips = append(zips, zip)
|
2017-12-18 22:20:23 +01:00
|
|
|
}
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
2017-12-23 01:12:00 +01:00
|
|
|
if len(relativeRootMap) > 0 {
|
|
|
|
var keys []string
|
|
|
|
|
|
|
|
// in order to keep stable order of soong_zip params, we sort the keys here.
|
|
|
|
for k := range relativeRootMap {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
parArgs := []string{}
|
2018-05-22 23:50:18 +02:00
|
|
|
if pkgPath != "" {
|
|
|
|
parArgs = append(parArgs, `-P `+pkgPath)
|
|
|
|
}
|
2017-12-23 01:12:00 +01:00
|
|
|
implicits := android.Paths{}
|
|
|
|
for _, k := range keys {
|
|
|
|
parArgs = append(parArgs, `-C `+k)
|
|
|
|
for _, path := range relativeRootMap[k] {
|
|
|
|
parArgs = append(parArgs, `-f `+path.String())
|
|
|
|
implicits = append(implicits, path)
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 22:20:23 +01:00
|
|
|
|
2017-12-23 01:12:00 +01:00
|
|
|
origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip")
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: zip,
|
|
|
|
Description: "python library archive",
|
|
|
|
Output: origSrcsZip,
|
|
|
|
Implicits: implicits,
|
|
|
|
Args: map[string]string{
|
|
|
|
"args": strings.Join(parArgs, " "),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
zips = append(zips, origSrcsZip)
|
|
|
|
}
|
|
|
|
if len(zips) == 1 {
|
|
|
|
return zips[0]
|
|
|
|
} else {
|
|
|
|
combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip")
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: combineZip,
|
|
|
|
Description: "combine python library archive",
|
|
|
|
Output: combinedSrcsZip,
|
|
|
|
Inputs: zips,
|
|
|
|
})
|
|
|
|
return combinedSrcsZip
|
|
|
|
}
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
func isPythonLibModule(module blueprint.Module) bool {
|
|
|
|
if m, ok := module.(*Module); ok {
|
|
|
|
// Python library has no bootstrapper or installer.
|
|
|
|
if m.bootstrapper != nil || m.installer != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-12-23 01:12:00 +01:00
|
|
|
// check Python source/data files duplicates for whole runfiles tree since Python binary/test
|
|
|
|
// need collect and zip all srcs of whole transitive dependencies to a final par file.
|
|
|
|
func (p *Module) walkTransitiveDeps(ctx android.ModuleContext) {
|
2017-02-27 19:12:13 +01:00
|
|
|
// fetch <runfiles_path, source_path> pairs from "src" and "data" properties to
|
|
|
|
// check duplicates.
|
|
|
|
destToPySrcs := make(map[string]string)
|
|
|
|
destToPyData := make(map[string]string)
|
|
|
|
|
|
|
|
for _, path := range p.srcsPathMappings {
|
|
|
|
destToPySrcs[path.dest] = path.src.String()
|
|
|
|
}
|
|
|
|
for _, path := range p.dataPathMappings {
|
|
|
|
destToPyData[path.dest] = path.src.String()
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:03:07 +02:00
|
|
|
seen := make(map[android.Module]bool)
|
|
|
|
|
2017-02-27 19:12:13 +01:00
|
|
|
// visit all its dependencies in depth first.
|
2018-06-21 22:03:07 +02:00
|
|
|
ctx.WalkDeps(func(child, parent android.Module) bool {
|
|
|
|
if ctx.OtherModuleDependencyTag(child) != pythonLibTag {
|
|
|
|
return false
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
2018-06-21 22:03:07 +02:00
|
|
|
if seen[child] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
seen[child] = true
|
2017-12-23 01:12:00 +01:00
|
|
|
// Python modules only can depend on Python libraries.
|
2018-06-21 22:03:07 +02:00
|
|
|
if !isPythonLibModule(child) {
|
2017-07-12 21:55:28 +02:00
|
|
|
panic(fmt.Errorf(
|
|
|
|
"the dependency %q of module %q is not Python library!",
|
2018-06-21 22:03:07 +02:00
|
|
|
ctx.ModuleName(), ctx.OtherModuleName(child)))
|
2017-07-12 21:55:28 +02:00
|
|
|
}
|
2018-06-21 22:03:07 +02:00
|
|
|
if dep, ok := child.(PythonDependency); ok {
|
2017-02-27 19:12:13 +01:00
|
|
|
srcs := dep.GetSrcsPathMappings()
|
|
|
|
for _, path := range srcs {
|
|
|
|
if !fillInMap(ctx, destToPySrcs,
|
2018-06-21 22:03:07 +02:00
|
|
|
path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) {
|
2017-02-27 19:12:13 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := dep.GetDataPathMappings()
|
|
|
|
for _, path := range data {
|
|
|
|
fillInMap(ctx, destToPyData,
|
2018-06-21 22:03:07 +02:00
|
|
|
path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child))
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
2017-12-18 22:20:23 +01:00
|
|
|
p.depsSrcsZips = append(p.depsSrcsZips, dep.GetSrcsZip())
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
2018-06-21 22:03:07 +02:00
|
|
|
return true
|
2017-02-27 19:12:13 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func fillInMap(ctx android.ModuleContext, m map[string]string,
|
|
|
|
key, value, curModule, otherModule string) bool {
|
|
|
|
if oldValue, found := m[key]; found {
|
2018-05-31 21:49:33 +02:00
|
|
|
ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+
|
2017-02-27 19:12:13 +01:00
|
|
|
" First file: in module %s at path %q."+
|
|
|
|
" Second file: in module %s at path %q.",
|
|
|
|
key, curModule, oldValue, otherModule, value)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
m[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2017-11-09 06:20:04 +01:00
|
|
|
|
2017-12-01 21:00:31 +01:00
|
|
|
func (p *Module) InstallInData() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-09 06:20:04 +01:00
|
|
|
var Bool = proptools.Bool
|
|
|
|
var String = proptools.String
|