diff options
Diffstat (limited to 'python/binary.go')
-rw-r--r-- | python/binary.go | 99 |
1 files changed, 95 insertions, 4 deletions
diff --git a/python/binary.go b/python/binary.go index 695fa123b..e955492a6 100644 --- a/python/binary.go +++ b/python/binary.go @@ -20,10 +20,99 @@ import ( "fmt" "android/soong/android" + "android/soong/bazel" + + "github.com/google/blueprint/proptools" ) func init() { - android.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) + registerPythonBinaryComponents(android.InitRegistrationContext) + android.RegisterBp2BuildMutator("python_binary_host", PythonBinaryBp2Build) +} + +func registerPythonBinaryComponents(ctx android.RegistrationContext) { + ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) +} + +type bazelPythonBinaryAttributes struct { + Main string + Srcs bazel.LabelListAttribute + Data bazel.LabelListAttribute + Python_version string +} + +type bazelPythonBinary struct { + android.BazelTargetModuleBase + bazelPythonBinaryAttributes +} + +func BazelPythonBinaryFactory() android.Module { + module := &bazelPythonBinary{} + module.AddProperties(&module.bazelPythonBinaryAttributes) + android.InitBazelTargetModule(module) + return module +} + +func (m *bazelPythonBinary) Name() string { + return m.BaseModuleName() +} + +func (m *bazelPythonBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {} + +func PythonBinaryBp2Build(ctx android.TopDownMutatorContext) { + m, ok := ctx.Module().(*Module) + if !ok || !m.ConvertWithBp2build(ctx) { + return + } + + // a Module can be something other than a python_binary_host + if ctx.ModuleType() != "python_binary_host" { + return + } + + var main string + for _, propIntf := range m.GetProperties() { + if props, ok := propIntf.(*BinaryProperties); ok { + // main is optional. + if props.Main != nil { + main = *props.Main + break + } + } + } + // TODO(b/182306917): this doesn't fully handle all nested props versioned + // by the python version, which would have been handled by the version split + // mutator. This is sufficient for very simple python_binary_host modules + // under Bionic. + py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, false) + py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false) + var python_version string + if py3Enabled && py2Enabled { + panic(fmt.Errorf( + "error for '%s' module: bp2build's python_binary_host converter does not support "+ + "converting a module that is enabled for both Python 2 and 3 at the same time.", m.Name())) + } else if py2Enabled { + python_version = "PY2" + } else { + // do nothing, since python_version defaults to PY3. + } + + srcs := android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs) + data := android.BazelLabelForModuleSrc(ctx, m.properties.Data) + + attrs := &bazelPythonBinaryAttributes{ + Main: main, + Srcs: bazel.MakeLabelListAttribute(srcs), + Data: bazel.MakeLabelListAttribute(data), + Python_version: python_version, + } + + props := bazel.BazelTargetModuleProperties{ + // Use the native py_binary rule. + Rule_class: "py_binary", + } + + ctx.CreateBazelTargetModule(BazelPythonBinaryFactory, m.Name(), props, attrs) } type BinaryProperties struct { @@ -65,7 +154,7 @@ type IntermPathProvider interface { } var ( - stubTemplateHost = "build/soong/python/scripts/stub_template_host.txt" + StubTemplateHost = "build/soong/python/scripts/stub_template_host.txt" ) func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { @@ -79,9 +168,11 @@ func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { } func PythonBinaryHostFactory() android.Module { - module, _ := NewBinary(android.HostSupportedNoCross) + module, _ := NewBinary(android.HostSupported) + + android.InitBazelModule(module) - return module.Init() + return module.init() } func (binary *binaryDecorator) autorun() bool { |