blob: 41e9f497450433e24bf2918253967a3af23a9b96 [file] [log] [blame]
Cole Faust4d247e62023-01-23 10:14:58 -08001// Copyright 2023 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
17import (
Cole Faust4d247e62023-01-23 10:14:58 -080018 "path/filepath"
19 "strings"
20
21 "github.com/google/blueprint/proptools"
22
23 "android/soong/android"
24 "android/soong/bazel"
25)
26
27type bazelPythonLibraryAttributes struct {
28 Srcs bazel.LabelListAttribute
29 Deps bazel.LabelListAttribute
30 Imports bazel.StringListAttribute
31 Srcs_version *string
32}
33
34type bazelPythonProtoLibraryAttributes struct {
35 Deps bazel.LabelListAttribute
36}
37
38type baseAttributes struct {
39 // TODO(b/200311466): Probably not translate b/c Bazel has no good equiv
40 //Pkg_path bazel.StringAttribute
41 // TODO: Related to Pkg_bath and similarLy gated
42 //Is_internal bazel.BoolAttribute
43 // Combines Srcs and Exclude_srcs
44 Srcs bazel.LabelListAttribute
45 Deps bazel.LabelListAttribute
46 // Combines Data and Java_data (invariant)
47 Data bazel.LabelListAttribute
48 Imports bazel.StringListAttribute
49}
50
51func (m *PythonLibraryModule) makeArchVariantBaseAttributes(ctx android.TopDownMutatorContext) baseAttributes {
52 var attrs baseAttributes
53 archVariantBaseProps := m.GetArchVariantProperties(ctx, &BaseProperties{})
54 for axis, configToProps := range archVariantBaseProps {
55 for config, props := range configToProps {
56 if baseProps, ok := props.(*BaseProperties); ok {
57 attrs.Srcs.SetSelectValue(axis, config,
58 android.BazelLabelForModuleSrcExcludes(ctx, baseProps.Srcs, baseProps.Exclude_srcs))
59 attrs.Deps.SetSelectValue(axis, config,
60 android.BazelLabelForModuleDeps(ctx, baseProps.Libs))
61 data := android.BazelLabelForModuleSrc(ctx, baseProps.Data)
62 data.Append(android.BazelLabelForModuleSrc(ctx, baseProps.Java_data))
63 attrs.Data.SetSelectValue(axis, config, data)
64 }
65 }
66 }
67
68 partitionedSrcs := bazel.PartitionLabelListAttribute(ctx, &attrs.Srcs, bazel.LabelPartitions{
69 "proto": android.ProtoSrcLabelPartition,
70 "py": bazel.LabelPartition{Keep_remainder: true},
71 })
72 attrs.Srcs = partitionedSrcs["py"]
73
74 if !partitionedSrcs["proto"].IsEmpty() {
75 protoInfo, _ := android.Bp2buildProtoProperties(ctx, &m.ModuleBase, partitionedSrcs["proto"])
Cole Faust4d247e62023-01-23 10:14:58 -080076
77 pyProtoLibraryName := m.Name() + "_py_proto"
78 ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{
79 Rule_class: "py_proto_library",
80 Bzl_load_location: "//build/bazel/rules/python:py_proto.bzl",
81 }, android.CommonAttributes{
82 Name: pyProtoLibraryName,
83 }, &bazelPythonProtoLibraryAttributes{
Spandan Dasc53767e2023-08-03 23:02:26 +000084 Deps: bazel.MakeLabelListAttribute(protoInfo.Proto_libs),
Cole Faust4d247e62023-01-23 10:14:58 -080085 })
86
87 attrs.Deps.Add(bazel.MakeLabelAttribute(":" + pyProtoLibraryName))
88 }
89
90 // Bazel normally requires `import path.from.top.of.tree` statements in
91 // python code, but with soong you can directly import modules from libraries.
92 // Add "imports" attributes to the bazel library so it matches soong's behavior.
93 imports := "."
94 if m.properties.Pkg_path != nil {
95 // TODO(b/215119317) This is a hack to handle the fact that we don't convert
96 // pkg_path properly right now. If the folder structure that contains this
97 // Android.bp file matches pkg_path, we can set imports to an appropriate
98 // number of ../..s to emulate moving the files under a pkg_path folder.
99 pkg_path := filepath.Clean(*m.properties.Pkg_path)
100 if strings.HasPrefix(pkg_path, "/") {
101 ctx.ModuleErrorf("pkg_path cannot start with a /: %s", pkg_path)
102 }
103
104 if !strings.HasSuffix(ctx.ModuleDir(), "/"+pkg_path) && ctx.ModuleDir() != pkg_path {
105 ctx.ModuleErrorf("Currently, bp2build only supports pkg_paths that are the same as the folders the Android.bp file is in. pkg_path: %s, module directory: %s", pkg_path, ctx.ModuleDir())
106 }
107 numFolders := strings.Count(pkg_path, "/") + 1
108 dots := make([]string, numFolders)
109 for i := 0; i < numFolders; i++ {
110 dots[i] = ".."
111 }
112 imports = strings.Join(dots, "/")
113 }
114 attrs.Imports = bazel.MakeStringListAttribute([]string{imports})
115
116 return attrs
117}
118
Cole Faustd82f0362023-04-12 17:32:19 -0700119func (m *PythonLibraryModule) bp2buildPythonVersion(ctx android.TopDownMutatorContext) *string {
Cole Faust4d247e62023-01-23 10:14:58 -0800120 py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true)
121 py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
Cole Faust4d247e62023-01-23 10:14:58 -0800122 if py2Enabled && !py3Enabled {
Cole Faustd82f0362023-04-12 17:32:19 -0700123 return &pyVersion2
Cole Faust4d247e62023-01-23 10:14:58 -0800124 } else if !py2Enabled && py3Enabled {
Cole Faustd82f0362023-04-12 17:32:19 -0700125 return &pyVersion3
Cole Faust4d247e62023-01-23 10:14:58 -0800126 } else if !py2Enabled && !py3Enabled {
127 ctx.ModuleErrorf("bp2build converter doesn't understand having neither py2 nor py3 enabled")
Cole Faustd82f0362023-04-12 17:32:19 -0700128 return &pyVersion3
Cole Faust4d247e62023-01-23 10:14:58 -0800129 } else {
Cole Faustd82f0362023-04-12 17:32:19 -0700130 return &pyVersion2And3
Cole Faust4d247e62023-01-23 10:14:58 -0800131 }
Cole Faust4d247e62023-01-23 10:14:58 -0800132}
133
134type bazelPythonBinaryAttributes struct {
135 Main *bazel.Label
136 Srcs bazel.LabelListAttribute
137 Deps bazel.LabelListAttribute
138 Python_version *string
139 Imports bazel.StringListAttribute
140}
141
Cole Faustd82f0362023-04-12 17:32:19 -0700142func (p *PythonLibraryModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
143 // TODO(b/182306917): this doesn't fully handle all nested props versioned
144 // by the python version, which would have been handled by the version split
145 // mutator. This is sufficient for very simple python_library modules under
146 // Bionic.
147 baseAttrs := p.makeArchVariantBaseAttributes(ctx)
148 pyVersion := p.bp2buildPythonVersion(ctx)
149 if *pyVersion == pyVersion2And3 {
150 // Libraries default to python 2 and 3
151 pyVersion = nil
152 }
153
154 attrs := &bazelPythonLibraryAttributes{
155 Srcs: baseAttrs.Srcs,
156 Deps: baseAttrs.Deps,
157 Srcs_version: pyVersion,
158 Imports: baseAttrs.Imports,
159 }
160
161 props := bazel.BazelTargetModuleProperties{
162 // Use the native py_library rule.
163 Rule_class: "py_library",
164 }
165
166 ctx.CreateBazelTargetModule(props, android.CommonAttributes{
167 Name: p.Name(),
168 Data: baseAttrs.Data,
169 }, attrs)
170}
171
172func (p *PythonBinaryModule) bp2buildBinaryProperties(ctx android.TopDownMutatorContext) (*bazelPythonBinaryAttributes, bazel.LabelListAttribute) {
Cole Faust4d247e62023-01-23 10:14:58 -0800173 // TODO(b/182306917): this doesn't fully handle all nested props versioned
174 // by the python version, which would have been handled by the version split
175 // mutator. This is sufficient for very simple python_binary_host modules
176 // under Bionic.
Cole Faustd82f0362023-04-12 17:32:19 -0700177
178 baseAttrs := p.makeArchVariantBaseAttributes(ctx)
179 pyVersion := p.bp2buildPythonVersion(ctx)
180 if *pyVersion == pyVersion3 {
181 // Binaries default to python 3
182 pyVersion = nil
183 } else if *pyVersion == pyVersion2And3 {
184 ctx.ModuleErrorf("error for '%s' module: bp2build's python_binary_host converter "+
185 "does not support converting a module that is enabled for both Python 2 and 3 at the "+
186 "same time.", p.Name())
Cole Faust4d247e62023-01-23 10:14:58 -0800187 }
188
Cole Faust4d247e62023-01-23 10:14:58 -0800189 attrs := &bazelPythonBinaryAttributes{
190 Main: nil,
191 Srcs: baseAttrs.Srcs,
192 Deps: baseAttrs.Deps,
Cole Faustd82f0362023-04-12 17:32:19 -0700193 Python_version: pyVersion,
Cole Faust4d247e62023-01-23 10:14:58 -0800194 Imports: baseAttrs.Imports,
195 }
196
Cole Faustd82f0362023-04-12 17:32:19 -0700197 // main is optional.
198 if p.binaryProperties.Main != nil {
199 main := android.BazelLabelForModuleSrcSingle(ctx, *p.binaryProperties.Main)
200 attrs.Main = &main
Cole Faust4d247e62023-01-23 10:14:58 -0800201 }
Cole Faustd82f0362023-04-12 17:32:19 -0700202 return attrs, baseAttrs.Data
203}
204
205func (p *PythonBinaryModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
206 attrs, data := p.bp2buildBinaryProperties(ctx)
Cole Faust4d247e62023-01-23 10:14:58 -0800207
208 props := bazel.BazelTargetModuleProperties{
209 // Use the native py_binary rule.
210 Rule_class: "py_binary",
211 }
212
213 ctx.CreateBazelTargetModule(props, android.CommonAttributes{
Cole Faustd82f0362023-04-12 17:32:19 -0700214 Name: p.Name(),
215 Data: data,
Cole Faust4d247e62023-01-23 10:14:58 -0800216 }, attrs)
217}
218
Cole Faustd82f0362023-04-12 17:32:19 -0700219func (p *PythonTestModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
220 // Python tests are currently exactly the same as binaries, but with a different module type
221 attrs, data := p.bp2buildBinaryProperties(ctx)
Cole Faust4d247e62023-01-23 10:14:58 -0800222
Cole Faustd82f0362023-04-12 17:32:19 -0700223 props := bazel.BazelTargetModuleProperties{
224 // Use the native py_binary rule.
yike1aaeea32023-07-26 22:19:09 +0000225 Rule_class: "py_test",
226 Bzl_load_location: "//build/bazel/rules/python:py_test.bzl",
Cole Faustd82f0362023-04-12 17:32:19 -0700227 }
Cole Faust4d247e62023-01-23 10:14:58 -0800228
Cole Faustd82f0362023-04-12 17:32:19 -0700229 ctx.CreateBazelTargetModule(props, android.CommonAttributes{
230 Name: p.Name(),
231 Data: data,
232 }, attrs)
Cole Faust4d247e62023-01-23 10:14:58 -0800233}