bp2build_product_config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package bp2build
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. )
  8. func CreateProductConfigFiles(
  9. ctx *CodegenContext) ([]BazelFile, error) {
  10. cfg := &ctx.config
  11. targetProduct := "unknown"
  12. if cfg.HasDeviceProduct() {
  13. targetProduct = cfg.DeviceProduct()
  14. }
  15. targetBuildVariant := "user"
  16. if cfg.Eng() {
  17. targetBuildVariant = "eng"
  18. } else if cfg.Debuggable() {
  19. targetBuildVariant = "userdebug"
  20. }
  21. productVariablesFileName := cfg.ProductVariablesFileName
  22. if !strings.HasPrefix(productVariablesFileName, "/") {
  23. productVariablesFileName = filepath.Join(ctx.topDir, productVariablesFileName)
  24. }
  25. bytes, err := os.ReadFile(productVariablesFileName)
  26. if err != nil {
  27. return nil, err
  28. }
  29. // TODO(b/249685973): the name is product_config_platforms because product_config
  30. // was already used for other files. Deduplicate them.
  31. currentProductFolder := fmt.Sprintf("product_config_platforms/products/%s-%s", targetProduct, targetBuildVariant)
  32. productReplacer := strings.NewReplacer(
  33. "{PRODUCT}", targetProduct,
  34. "{VARIANT}", targetBuildVariant,
  35. "{PRODUCT_FOLDER}", currentProductFolder)
  36. result := []BazelFile{
  37. newFile(
  38. currentProductFolder,
  39. "soong.variables.bzl",
  40. `variables = json.decode("""`+strings.ReplaceAll(string(bytes), "\\", "\\\\")+`""")`),
  41. newFile(
  42. currentProductFolder,
  43. "BUILD",
  44. productReplacer.Replace(`
  45. package(default_visibility=[
  46. "@soong_injection//product_config_platforms:__subpackages__",
  47. "@//build/bazel/product_config:__subpackages__",
  48. ])
  49. load(":soong.variables.bzl", _soong_variables = "variables")
  50. load("@//build/bazel/product_config:android_product.bzl", "android_product")
  51. android_product(
  52. name = "{PRODUCT}-{VARIANT}",
  53. soong_variables = _soong_variables,
  54. )
  55. `)),
  56. newFile(
  57. "product_config_platforms",
  58. "BUILD.bazel",
  59. productReplacer.Replace(`
  60. package(default_visibility = [
  61. "@//build/bazel/product_config:__subpackages__",
  62. "@soong_injection//product_config_platforms:__subpackages__",
  63. ])
  64. load("//{PRODUCT_FOLDER}:soong.variables.bzl", _soong_variables = "variables")
  65. load("@//build/bazel/product_config:android_product.bzl", "android_product")
  66. # Bazel will qualify its outputs by the platform name. When switching between products, this
  67. # means that soong-built files that depend on bazel-built files will suddenly get different
  68. # dependency files, because the path changes, and they will be rebuilt. In order to avoid this
  69. # extra rebuilding, make mixed builds always use a single platform so that the bazel artifacts
  70. # are always under the same path.
  71. android_product(
  72. name = "mixed_builds_product-{VARIANT}",
  73. soong_variables = _soong_variables,
  74. )
  75. `)),
  76. newFile(
  77. "product_config_platforms",
  78. "product_labels.bzl",
  79. productReplacer.Replace(`
  80. # This file keeps a list of all the products in the android source tree, because they're
  81. # discovered as part of a preprocessing step before bazel runs.
  82. # TODO: When we start generating the platforms for more than just the
  83. # currently lunched product, they should all be listed here
  84. product_labels = [
  85. "@soong_injection//product_config_platforms:mixed_builds_product-{VARIANT}",
  86. "@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}"
  87. ]
  88. `)),
  89. newFile(
  90. "product_config_platforms",
  91. "common.bazelrc",
  92. productReplacer.Replace(`
  93. build --platforms @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
  94. build:android --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}
  95. build:linux_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
  96. build:linux_bionic_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_bionic_x86_64
  97. build:linux_musl_x86 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86
  98. build:linux_musl_x86_64 --platforms=@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_musl_x86_64
  99. `)),
  100. newFile(
  101. "product_config_platforms",
  102. "linux.bazelrc",
  103. productReplacer.Replace(`
  104. build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_linux_x86_64
  105. `)),
  106. newFile(
  107. "product_config_platforms",
  108. "darwin.bazelrc",
  109. productReplacer.Replace(`
  110. build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_darwin_x86_64
  111. `)),
  112. }
  113. return result, nil
  114. }