Add python_test_host module.

bug: 31676493
Test: created py_test modules in real folder. and ran 'mma'.
Change-Id: I22aa2fad74b11e4a31ea7a4c4a4f0ea64cd3fc94
This commit is contained in:
Nan Zhang
2017-05-10 13:37:54 -07:00
parent 2c13abc95e
commit 5323f8e32f
8 changed files with 227 additions and 61 deletions

View File

@@ -219,10 +219,13 @@ bootstrap_go_package {
"soong-android", "soong-android",
], ],
srcs: [ srcs: [
"python/androidmk.go",
"python/binary.go", "python/binary.go",
"python/builder.go", "python/builder.go",
"python/installer.go",
"python/library.go", "python/library.go",
"python/python.go", "python/python.go",
"python/test.go",
], ],
testSrcs: [ testSrcs: [
"python/python_test.go", "python/python_test.go",

74
python/androidmk.go Normal file
View File

@@ -0,0 +1,74 @@
// 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
import (
"android/soong/android"
"fmt"
"io"
"path/filepath"
"strings"
)
type subAndroidMkProvider interface {
AndroidMk(*pythonBaseModule, *android.AndroidMkData)
}
func (p *pythonBaseModule) subAndroidMk(data *android.AndroidMkData, obj interface{}) {
if p.subAndroidMkOnce == nil {
p.subAndroidMkOnce = make(map[subAndroidMkProvider]bool)
}
if androidmk, ok := obj.(subAndroidMkProvider); ok {
if !p.subAndroidMkOnce[androidmk] {
p.subAndroidMkOnce[androidmk] = true
androidmk.AndroidMk(p, data)
}
}
}
func (p *pythonBaseModule) AndroidMk() (ret android.AndroidMkData, err error) {
p.subAndroidMk(&ret, p.installer)
return ret, nil
}
func (p *pythonBinaryHostDecorator) AndroidMk(base *pythonBaseModule, ret *android.AndroidMkData) {
ret.Class = "EXECUTABLES"
base.subAndroidMk(ret, p.pythonDecorator.baseInstaller)
}
func (p *pythonTestHostDecorator) AndroidMk(base *pythonBaseModule, ret *android.AndroidMkData) {
ret.Class = "NATIVE_TESTS"
base.subAndroidMk(ret, p.pythonDecorator.baseInstaller)
}
func (installer *pythonInstaller) AndroidMk(base *pythonBaseModule, ret *android.AndroidMkData) {
// Soong installation is only supported for host modules. Have Make
// installation trigger Soong installation.
if base.Target().Os.Class == android.Host {
ret.OutputFile = android.OptionalPathForPath(installer.path)
}
ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
path := installer.path.RelPathString()
dir, file := filepath.Split(path)
stem := strings.TrimSuffix(file, filepath.Ext(file))
fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+filepath.Ext(file))
fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
return nil
})
}

View File

@@ -18,7 +18,6 @@ package python
import ( import (
"fmt" "fmt"
"io"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -31,7 +30,7 @@ func init() {
android.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) android.RegisterModuleType("python_binary_host", PythonBinaryHostFactory)
} }
type PythonBinaryProperties struct { type PythonBinaryBaseProperties struct {
// the name of the source file that is the main entry point of the program. // the name of the source file that is the main entry point of the program.
// this file must also be listed in srcs. // this file must also be listed in srcs.
// If left unspecified, module name is used instead. // If left unspecified, module name is used instead.
@@ -45,40 +44,53 @@ type PythonBinaryProperties struct {
Suffix string Suffix string
} }
type PythonBinary struct { type pythonBinaryBase struct {
pythonBaseModule pythonBaseModule
binaryProperties PythonBinaryProperties binaryProperties PythonBinaryBaseProperties
// soong_zip arguments from all its dependencies. // soong_zip arguments from all its dependencies.
depsParSpecs []parSpec depsParSpecs []parSpec
// Python runfiles paths from all its dependencies. // Python runfiles paths from all its dependencies.
depsPyRunfiles []string depsPyRunfiles []string
// the installation path for Python binary.
installPath android.OutputPath
} }
var _ PythonSubModule = (*PythonBinary)(nil) type PythonBinaryHost struct {
pythonBinaryBase
}
var _ PythonSubModule = (*PythonBinaryHost)(nil)
type pythonBinaryHostDecorator struct {
pythonDecorator
}
func (p *pythonBinaryHostDecorator) install(ctx android.ModuleContext, file android.Path) {
p.pythonDecorator.baseInstaller.install(ctx, file)
}
var ( var (
stubTemplateHost = "build/soong/python/scripts/stub_template_host.txt" stubTemplateHost = "build/soong/python/scripts/stub_template_host.txt"
) )
func PythonBinaryHostFactory() (blueprint.Module, []interface{}) { func PythonBinaryHostFactory() (blueprint.Module, []interface{}) {
module := &PythonBinary{} decorator := &pythonBinaryHostDecorator{
pythonDecorator: pythonDecorator{baseInstaller: NewPythonInstaller("bin")}}
return InitPythonBaseModule(&module.pythonBaseModule, module, android.HostSupportedNoCross, module := &PythonBinaryHost{}
&module.binaryProperties) module.pythonBaseModule.installer = decorator
return InitPythonBaseModule(&module.pythonBinaryBase.pythonBaseModule,
&module.pythonBinaryBase, android.HostSupportedNoCross, &module.binaryProperties)
} }
func (p *PythonBinary) GeneratePythonBuildActions(ctx android.ModuleContext) { func (p *pythonBinaryBase) GeneratePythonBuildActions(ctx android.ModuleContext) android.OptionalPath {
p.pythonBaseModule.GeneratePythonBuildActions(ctx) p.pythonBaseModule.GeneratePythonBuildActions(ctx)
// no Python source file for compiling par file. // no Python source file for compiling par file.
if len(p.pythonBaseModule.srcsPathMappings) == 0 && len(p.depsPyRunfiles) == 0 { if len(p.pythonBaseModule.srcsPathMappings) == 0 && len(p.depsPyRunfiles) == 0 {
return return android.OptionalPath{}
} }
// the runfiles packages needs to be populated with "__init__.py". // the runfiles packages needs to be populated with "__init__.py".
@@ -121,11 +133,11 @@ func (p *PythonBinary) GeneratePythonBuildActions(ctx android.ModuleContext) {
main := p.getPyMainFile(ctx) main := p.getPyMainFile(ctx)
if main == "" { if main == "" {
return return android.OptionalPath{}
} }
interp := p.getInterpreter(ctx) interp := p.getInterpreter(ctx)
if interp == "" { if interp == "" {
return return android.OptionalPath{}
} }
// we need remove "runfiles/" suffix since stub script starts // we need remove "runfiles/" suffix since stub script starts
@@ -134,13 +146,11 @@ func (p *PythonBinary) GeneratePythonBuildActions(ctx android.ModuleContext) {
strings.TrimPrefix(main, runFiles+"/"), p.getStem(ctx), strings.TrimPrefix(main, runFiles+"/"), p.getStem(ctx),
newPyPkgs, append(p.depsParSpecs, p.pythonBaseModule.parSpec)) newPyPkgs, append(p.depsParSpecs, p.pythonBaseModule.parSpec))
// install par file. return android.OptionalPathForPath(binFile)
p.installPath = ctx.InstallFile(
android.PathForModuleInstall(ctx, "bin"), binFile)
} }
// get interpreter path. // get interpreter path.
func (p *PythonBinary) getInterpreter(ctx android.ModuleContext) string { func (p *pythonBinaryBase) getInterpreter(ctx android.ModuleContext) string {
var interp string var interp string
switch p.pythonBaseModule.properties.ActualVersion { switch p.pythonBaseModule.properties.ActualVersion {
case pyVersion2: case pyVersion2:
@@ -156,7 +166,7 @@ func (p *PythonBinary) getInterpreter(ctx android.ModuleContext) string {
} }
// find main program path within runfiles tree. // find main program path within runfiles tree.
func (p *PythonBinary) getPyMainFile(ctx android.ModuleContext) string { func (p *pythonBinaryBase) getPyMainFile(ctx android.ModuleContext) string {
var main string var main string
if p.binaryProperties.Main == "" { if p.binaryProperties.Main == "" {
main = p.BaseModuleName() + pyExt main = p.BaseModuleName() + pyExt
@@ -174,7 +184,7 @@ func (p *PythonBinary) getPyMainFile(ctx android.ModuleContext) string {
return "" return ""
} }
func (p *PythonBinary) getStem(ctx android.ModuleContext) string { func (p *pythonBinaryBase) getStem(ctx android.ModuleContext) string {
stem := ctx.ModuleName() stem := ctx.ModuleName()
if p.binaryProperties.Stem != "" { if p.binaryProperties.Stem != "" {
stem = p.binaryProperties.Stem stem = p.binaryProperties.Stem
@@ -209,27 +219,3 @@ func PathBeforeLastSlash(path string) string {
} }
return "" return ""
} }
func (p *PythonBinary) GeneratePythonAndroidMk() (ret android.AndroidMkData, err error) {
// Soong installation is only supported for host modules. Have Make
// installation trigger Soong installation.
if p.pythonBaseModule.Target().Os.Class == android.Host {
ret.OutputFile = android.OptionalPathForPath(p.installPath)
}
ret.Class = "EXECUTABLES"
ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
path := p.installPath.RelPathString()
dir, file := filepath.Split(path)
stem := strings.TrimSuffix(file, filepath.Ext(file))
fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+filepath.Ext(file))
fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
return nil
})
return
}

39
python/installer.go Normal file
View File

@@ -0,0 +1,39 @@
// 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
import (
"android/soong/android"
)
// This file handles installing python executables into their final location
type pythonInstaller struct {
dir string
path android.OutputPath
}
func NewPythonInstaller(dir string) *pythonInstaller {
return &pythonInstaller{
dir: dir,
}
}
var _ installer = (*pythonInstaller)(nil)
func (installer *pythonInstaller) install(ctx android.ModuleContext, file android.Path) {
installer.path = ctx.InstallFile(android.PathForModuleInstall(ctx, installer.dir), file)
}

View File

@@ -37,7 +37,3 @@ func PythonLibraryHostFactory() (blueprint.Module, []interface{}) {
return InitPythonBaseModule(&module.pythonBaseModule, module, android.HostSupportedNoCross) return InitPythonBaseModule(&module.pythonBaseModule, module, android.HostSupportedNoCross)
} }
func (p *PythonLibrary) GeneratePythonAndroidMk() (ret android.AndroidMkData, err error) {
return
}

View File

@@ -110,11 +110,15 @@ type pythonBaseModule struct {
// the soong_zip arguments for zipping current module source/data files. // the soong_zip arguments for zipping current module source/data files.
parSpec parSpec parSpec parSpec
// the installer might be nil.
installer installer
subAndroidMkOnce map[subAndroidMkProvider]bool
} }
type PythonSubModule interface { type PythonSubModule interface {
GeneratePythonBuildActions(ctx android.ModuleContext) GeneratePythonBuildActions(ctx android.ModuleContext) android.OptionalPath
GeneratePythonAndroidMk() (ret android.AndroidMkData, err error)
} }
type PythonDependency interface { type PythonDependency interface {
@@ -123,6 +127,14 @@ type PythonDependency interface {
GetParSpec() parSpec GetParSpec() parSpec
} }
type pythonDecorator struct {
baseInstaller *pythonInstaller
}
type installer interface {
install(ctx android.ModuleContext, path android.Path)
}
func (p *pythonBaseModule) GetSrcsPathMappings() []pathMapping { func (p *pythonBaseModule) GetSrcsPathMappings() []pathMapping {
return p.srcsPathMappings return p.srcsPathMappings
} }
@@ -246,10 +258,14 @@ func uniqueLibs(ctx android.BottomUpMutatorContext,
} }
func (p *pythonBaseModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { func (p *pythonBaseModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
p.subModule.GeneratePythonBuildActions(ctx) installSource := p.subModule.GeneratePythonBuildActions(ctx)
if p.installer != nil && installSource.Valid() {
p.installer.install(ctx, installSource.Path())
}
} }
func (p *pythonBaseModule) GeneratePythonBuildActions(ctx android.ModuleContext) { func (p *pythonBaseModule) GeneratePythonBuildActions(ctx android.ModuleContext) android.OptionalPath {
// expand python files from "srcs" property. // expand python files from "srcs" property.
srcs := p.properties.Srcs srcs := p.properties.Srcs
switch p.properties.ActualVersion { switch p.properties.ActualVersion {
@@ -277,7 +293,7 @@ func (p *pythonBaseModule) GeneratePythonBuildActions(ctx android.ModuleContext)
strings.HasPrefix(pkg_path, "/") { strings.HasPrefix(pkg_path, "/") {
ctx.PropertyErrorf("pkg_path", "%q is not a valid format.", ctx.PropertyErrorf("pkg_path", "%q is not a valid format.",
p.properties.Pkg_path) p.properties.Pkg_path)
return return android.OptionalPath{}
} }
// pkg_path starts from "runfiles/" implicitly. // pkg_path starts from "runfiles/" implicitly.
pkg_path = filepath.Join(runFiles, pkg_path) pkg_path = filepath.Join(runFiles, pkg_path)
@@ -291,6 +307,8 @@ func (p *pythonBaseModule) GeneratePythonBuildActions(ctx android.ModuleContext)
p.parSpec = p.dumpFileList(ctx, pkg_path) p.parSpec = p.dumpFileList(ctx, pkg_path)
p.uniqWholeRunfilesTree(ctx) p.uniqWholeRunfilesTree(ctx)
return android.OptionalPath{}
} }
// generate current module unique pathMappings: <dest: runfiles_path, src: source_path> // generate current module unique pathMappings: <dest: runfiles_path, src: source_path>
@@ -409,7 +427,7 @@ func (p *pythonBaseModule) uniqWholeRunfilesTree(ctx android.ModuleContext) {
} }
// binary needs the Python runfiles paths from all its // binary needs the Python runfiles paths from all its
// dependencies to fill __init__.py in each runfiles dir. // dependencies to fill __init__.py in each runfiles dir.
if sub, ok := p.subModule.(*PythonBinary); ok { if sub, ok := p.subModule.(*pythonBinaryBase); ok {
sub.depsPyRunfiles = append(sub.depsPyRunfiles, path.dest) sub.depsPyRunfiles = append(sub.depsPyRunfiles, path.dest)
} }
} }
@@ -421,7 +439,7 @@ func (p *pythonBaseModule) uniqWholeRunfilesTree(ctx android.ModuleContext) {
} }
// binary needs the soong_zip arguments from all its // binary needs the soong_zip arguments from all its
// dependencies to generate executable par file. // dependencies to generate executable par file.
if sub, ok := p.subModule.(*PythonBinary); ok { if sub, ok := p.subModule.(*pythonBinaryBase); ok {
sub.depsParSpecs = append(sub.depsParSpecs, dep.GetParSpec()) sub.depsParSpecs = append(sub.depsParSpecs, dep.GetParSpec())
} }
} }
@@ -442,7 +460,3 @@ func fillInMap(ctx android.ModuleContext, m map[string]string,
return true return true
} }
func (p *pythonBaseModule) AndroidMk() (ret android.AndroidMkData, err error) {
return p.subModule.GeneratePythonAndroidMk()
}

View File

@@ -370,7 +370,7 @@ func expectModule(t *testing.T, ctx *blueprint.Context, buildDir, name, variant
if !baseOk { if !baseOk {
t.Fatalf("%s is not Python module!", name) t.Fatalf("%s is not Python module!", name)
} }
sub, subOk := base.subModule.(*PythonBinary) sub, subOk := base.subModule.(*pythonBinaryBase)
if !subOk { if !subOk {
t.Fatalf("%s is not Python binary!", name) t.Fatalf("%s is not Python binary!", name)
} }

54
python/test.go Normal file
View File

@@ -0,0 +1,54 @@
// 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
import (
"android/soong/android"
"path/filepath"
"github.com/google/blueprint"
)
// This file contains the module types for building Python test.
func init() {
android.RegisterModuleType("python_test_host", PythonTestHostFactory)
}
type PythonTestHost struct {
pythonBinaryBase
}
var _ PythonSubModule = (*PythonTestHost)(nil)
type pythonTestHostDecorator struct {
pythonDecorator
}
func (p *pythonTestHostDecorator) install(ctx android.ModuleContext, file android.Path) {
p.pythonDecorator.baseInstaller.dir = filepath.Join("nativetest", ctx.ModuleName())
p.pythonDecorator.baseInstaller.install(ctx, file)
}
func PythonTestHostFactory() (blueprint.Module, []interface{}) {
decorator := &pythonTestHostDecorator{
pythonDecorator: pythonDecorator{baseInstaller: NewPythonInstaller("nativetest")}}
module := &PythonBinaryHost{}
module.pythonBaseModule.installer = decorator
return InitPythonBaseModule(&module.pythonBinaryBase.pythonBaseModule,
&module.pythonBinaryBase, android.HostSupportedNoCross, &module.binaryProperties)
}