bp2build.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // 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. package python
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "strings"
  19. "github.com/google/blueprint/proptools"
  20. "android/soong/android"
  21. "android/soong/bazel"
  22. )
  23. type bazelPythonLibraryAttributes struct {
  24. Srcs bazel.LabelListAttribute
  25. Deps bazel.LabelListAttribute
  26. Imports bazel.StringListAttribute
  27. Srcs_version *string
  28. }
  29. type bazelPythonProtoLibraryAttributes struct {
  30. Deps bazel.LabelListAttribute
  31. }
  32. type baseAttributes struct {
  33. // TODO(b/200311466): Probably not translate b/c Bazel has no good equiv
  34. //Pkg_path bazel.StringAttribute
  35. // TODO: Related to Pkg_bath and similarLy gated
  36. //Is_internal bazel.BoolAttribute
  37. // Combines Srcs and Exclude_srcs
  38. Srcs bazel.LabelListAttribute
  39. Deps bazel.LabelListAttribute
  40. // Combines Data and Java_data (invariant)
  41. Data bazel.LabelListAttribute
  42. Imports bazel.StringListAttribute
  43. }
  44. func (m *PythonLibraryModule) makeArchVariantBaseAttributes(ctx android.TopDownMutatorContext) baseAttributes {
  45. var attrs baseAttributes
  46. archVariantBaseProps := m.GetArchVariantProperties(ctx, &BaseProperties{})
  47. for axis, configToProps := range archVariantBaseProps {
  48. for config, props := range configToProps {
  49. if baseProps, ok := props.(*BaseProperties); ok {
  50. attrs.Srcs.SetSelectValue(axis, config,
  51. android.BazelLabelForModuleSrcExcludes(ctx, baseProps.Srcs, baseProps.Exclude_srcs))
  52. attrs.Deps.SetSelectValue(axis, config,
  53. android.BazelLabelForModuleDeps(ctx, baseProps.Libs))
  54. data := android.BazelLabelForModuleSrc(ctx, baseProps.Data)
  55. data.Append(android.BazelLabelForModuleSrc(ctx, baseProps.Java_data))
  56. attrs.Data.SetSelectValue(axis, config, data)
  57. }
  58. }
  59. }
  60. partitionedSrcs := bazel.PartitionLabelListAttribute(ctx, &attrs.Srcs, bazel.LabelPartitions{
  61. "proto": android.ProtoSrcLabelPartition,
  62. "py": bazel.LabelPartition{Keep_remainder: true},
  63. })
  64. attrs.Srcs = partitionedSrcs["py"]
  65. if !partitionedSrcs["proto"].IsEmpty() {
  66. protoInfo, _ := android.Bp2buildProtoProperties(ctx, &m.ModuleBase, partitionedSrcs["proto"])
  67. protoLabel := bazel.Label{Label: ":" + protoInfo.Name}
  68. pyProtoLibraryName := m.Name() + "_py_proto"
  69. ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{
  70. Rule_class: "py_proto_library",
  71. Bzl_load_location: "//build/bazel/rules/python:py_proto.bzl",
  72. }, android.CommonAttributes{
  73. Name: pyProtoLibraryName,
  74. }, &bazelPythonProtoLibraryAttributes{
  75. Deps: bazel.MakeSingleLabelListAttribute(protoLabel),
  76. })
  77. attrs.Deps.Add(bazel.MakeLabelAttribute(":" + pyProtoLibraryName))
  78. }
  79. // Bazel normally requires `import path.from.top.of.tree` statements in
  80. // python code, but with soong you can directly import modules from libraries.
  81. // Add "imports" attributes to the bazel library so it matches soong's behavior.
  82. imports := "."
  83. if m.properties.Pkg_path != nil {
  84. // TODO(b/215119317) This is a hack to handle the fact that we don't convert
  85. // pkg_path properly right now. If the folder structure that contains this
  86. // Android.bp file matches pkg_path, we can set imports to an appropriate
  87. // number of ../..s to emulate moving the files under a pkg_path folder.
  88. pkg_path := filepath.Clean(*m.properties.Pkg_path)
  89. if strings.HasPrefix(pkg_path, "/") {
  90. ctx.ModuleErrorf("pkg_path cannot start with a /: %s", pkg_path)
  91. }
  92. if !strings.HasSuffix(ctx.ModuleDir(), "/"+pkg_path) && ctx.ModuleDir() != pkg_path {
  93. 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())
  94. }
  95. numFolders := strings.Count(pkg_path, "/") + 1
  96. dots := make([]string, numFolders)
  97. for i := 0; i < numFolders; i++ {
  98. dots[i] = ".."
  99. }
  100. imports = strings.Join(dots, "/")
  101. }
  102. attrs.Imports = bazel.MakeStringListAttribute([]string{imports})
  103. return attrs
  104. }
  105. func pythonLibBp2Build(ctx android.TopDownMutatorContext, m *PythonLibraryModule) {
  106. // TODO(b/182306917): this doesn't fully handle all nested props versioned
  107. // by the python version, which would have been handled by the version split
  108. // mutator. This is sufficient for very simple python_library modules under
  109. // Bionic.
  110. py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true)
  111. py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
  112. var python_version *string
  113. if py2Enabled && !py3Enabled {
  114. python_version = &pyVersion2
  115. } else if !py2Enabled && py3Enabled {
  116. python_version = &pyVersion3
  117. } else if !py2Enabled && !py3Enabled {
  118. ctx.ModuleErrorf("bp2build converter doesn't understand having neither py2 nor py3 enabled")
  119. } else {
  120. // do nothing, since python_version defaults to PY2ANDPY3
  121. }
  122. baseAttrs := m.makeArchVariantBaseAttributes(ctx)
  123. attrs := &bazelPythonLibraryAttributes{
  124. Srcs: baseAttrs.Srcs,
  125. Deps: baseAttrs.Deps,
  126. Srcs_version: python_version,
  127. Imports: baseAttrs.Imports,
  128. }
  129. props := bazel.BazelTargetModuleProperties{
  130. // Use the native py_library rule.
  131. Rule_class: "py_library",
  132. }
  133. ctx.CreateBazelTargetModule(props, android.CommonAttributes{
  134. Name: m.Name(),
  135. Data: baseAttrs.Data,
  136. }, attrs)
  137. }
  138. type bazelPythonBinaryAttributes struct {
  139. Main *bazel.Label
  140. Srcs bazel.LabelListAttribute
  141. Deps bazel.LabelListAttribute
  142. Python_version *string
  143. Imports bazel.StringListAttribute
  144. }
  145. func pythonBinaryBp2Build(ctx android.TopDownMutatorContext, m *PythonBinaryModule) {
  146. // TODO(b/182306917): this doesn't fully handle all nested props versioned
  147. // by the python version, which would have been handled by the version split
  148. // mutator. This is sufficient for very simple python_binary_host modules
  149. // under Bionic.
  150. py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, false)
  151. py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
  152. var python_version *string
  153. if py3Enabled && py2Enabled {
  154. panic(fmt.Errorf(
  155. "error for '%s' module: bp2build's python_binary_host converter does not support "+
  156. "converting a module that is enabled for both Python 2 and 3 at the same time.", m.Name()))
  157. } else if py2Enabled {
  158. python_version = &pyVersion2
  159. } else {
  160. // do nothing, since python_version defaults to PY3.
  161. }
  162. baseAttrs := m.makeArchVariantBaseAttributes(ctx)
  163. attrs := &bazelPythonBinaryAttributes{
  164. Main: nil,
  165. Srcs: baseAttrs.Srcs,
  166. Deps: baseAttrs.Deps,
  167. Python_version: python_version,
  168. Imports: baseAttrs.Imports,
  169. }
  170. for _, propIntf := range m.GetProperties() {
  171. if props, ok := propIntf.(*BinaryProperties); ok {
  172. // main is optional.
  173. if props.Main != nil {
  174. main := android.BazelLabelForModuleSrcSingle(ctx, *props.Main)
  175. attrs.Main = &main
  176. break
  177. }
  178. }
  179. }
  180. props := bazel.BazelTargetModuleProperties{
  181. // Use the native py_binary rule.
  182. Rule_class: "py_binary",
  183. }
  184. ctx.CreateBazelTargetModule(props, android.CommonAttributes{
  185. Name: m.Name(),
  186. Data: baseAttrs.Data,
  187. }, attrs)
  188. }
  189. func (p *PythonLibraryModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
  190. pythonLibBp2Build(ctx, p)
  191. }
  192. func (p *PythonBinaryModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
  193. pythonBinaryBp2Build(ctx, p)
  194. }
  195. func (p *PythonTestModule) ConvertWithBp2build(_ android.TopDownMutatorContext) {
  196. // Tests are currently unsupported
  197. }