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 module types for building Python library.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"android/soong/android"
|
2021-08-19 21:21:30 +02:00
|
|
|
"android/soong/bazel"
|
2021-12-01 16:09:34 +01:00
|
|
|
|
2021-08-19 21:21:30 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
2017-02-27 19:12:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-03-17 22:57:08 +01:00
|
|
|
registerPythonLibraryComponents(android.InitRegistrationContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerPythonLibraryComponents(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("python_library_host", PythonLibraryHostFactory)
|
|
|
|
ctx.RegisterModuleType("python_library", PythonLibraryFactory)
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
2017-06-24 00:06:31 +02:00
|
|
|
func PythonLibraryHostFactory() android.Module {
|
2020-09-14 12:43:17 +02:00
|
|
|
module := newModule(android.HostSupported, android.MultilibFirst)
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2021-08-23 22:21:19 +02:00
|
|
|
android.InitBazelModule(module)
|
|
|
|
|
2020-11-17 00:42:51 +01:00
|
|
|
return module.init()
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
2017-12-01 21:00:31 +01:00
|
|
|
|
2021-08-19 21:21:30 +02:00
|
|
|
type bazelPythonLibraryAttributes struct {
|
|
|
|
Srcs bazel.LabelListAttribute
|
2021-08-26 22:13:29 +02:00
|
|
|
Deps bazel.LabelListAttribute
|
2022-05-18 19:57:33 +02:00
|
|
|
Imports bazel.StringListAttribute
|
2021-12-01 16:09:34 +01:00
|
|
|
Srcs_version *string
|
2021-08-19 21:21:30 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 00:37:02 +02:00
|
|
|
type bazelPythonProtoLibraryAttributes struct {
|
|
|
|
Deps bazel.LabelListAttribute
|
|
|
|
}
|
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
func pythonLibBp2Build(ctx android.TopDownMutatorContext, m *Module) {
|
2021-08-19 21:21:30 +02:00
|
|
|
// 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_library modules under
|
|
|
|
// Bionic.
|
|
|
|
py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true)
|
|
|
|
py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
|
2021-12-01 16:09:34 +01:00
|
|
|
var python_version *string
|
2021-08-19 21:21:30 +02:00
|
|
|
if py2Enabled && !py3Enabled {
|
2021-12-01 16:09:34 +01:00
|
|
|
python_version = &pyVersion2
|
2021-08-19 21:21:30 +02:00
|
|
|
} else if !py2Enabled && py3Enabled {
|
2021-12-01 16:09:34 +01:00
|
|
|
python_version = &pyVersion3
|
2021-08-19 21:21:30 +02:00
|
|
|
} else if !py2Enabled && !py3Enabled {
|
2021-11-01 20:32:43 +01:00
|
|
|
ctx.ModuleErrorf("bp2build converter doesn't understand having neither py2 nor py3 enabled")
|
2021-08-19 21:21:30 +02:00
|
|
|
} else {
|
|
|
|
// do nothing, since python_version defaults to PY2ANDPY3
|
|
|
|
}
|
|
|
|
|
2021-09-17 22:30:21 +02:00
|
|
|
baseAttrs := m.makeArchVariantBaseAttributes(ctx)
|
2022-05-13 00:37:02 +02:00
|
|
|
|
2021-08-19 21:21:30 +02:00
|
|
|
attrs := &bazelPythonLibraryAttributes{
|
2021-09-17 22:30:21 +02:00
|
|
|
Srcs: baseAttrs.Srcs,
|
|
|
|
Deps: baseAttrs.Deps,
|
2021-08-19 21:21:30 +02:00
|
|
|
Srcs_version: python_version,
|
2022-06-02 21:11:12 +02:00
|
|
|
Imports: baseAttrs.Imports,
|
2021-08-19 21:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
props := bazel.BazelTargetModuleProperties{
|
2022-05-18 19:57:33 +02:00
|
|
|
// Use the native py_library rule.
|
|
|
|
Rule_class: "py_library",
|
2021-08-19 21:21:30 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 22:30:36 +02:00
|
|
|
ctx.CreateBazelTargetModule(props, android.CommonAttributes{
|
|
|
|
Name: m.Name(),
|
|
|
|
Data: baseAttrs.Data,
|
|
|
|
}, attrs)
|
2021-08-19 21:21:30 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:00:31 +01:00
|
|
|
func PythonLibraryFactory() android.Module {
|
|
|
|
module := newModule(android.HostAndDeviceSupported, android.MultilibBoth)
|
|
|
|
|
2021-08-19 21:21:30 +02:00
|
|
|
android.InitBazelModule(module)
|
|
|
|
|
2020-11-17 00:42:51 +01:00
|
|
|
return module.init()
|
2017-12-01 21:00:31 +01:00
|
|
|
}
|