conversion.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package bp2build
  2. import (
  3. "android/soong/starlark_fmt"
  4. "encoding/json"
  5. "fmt"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "android/soong/android"
  10. "android/soong/cc"
  11. cc_config "android/soong/cc/config"
  12. java_config "android/soong/java/config"
  13. "android/soong/apex"
  14. "github.com/google/blueprint/proptools"
  15. )
  16. type BazelFile struct {
  17. Dir string
  18. Basename string
  19. Contents string
  20. }
  21. // PRIVATE: Use CreateSoongInjectionDirFiles instead
  22. func soongInjectionFiles(cfg android.Config, metrics CodegenMetrics) ([]BazelFile, error) {
  23. var files []BazelFile
  24. files = append(files, newFile("android", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
  25. files = append(files, newFile("android", "constants.bzl", android.BazelCcToolchainVars(cfg)))
  26. files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
  27. files = append(files, newFile("cc_toolchain", "config_constants.bzl", cc_config.BazelCcToolchainVars(cfg)))
  28. files = append(files, newFile("cc_toolchain", "sanitizer_constants.bzl", cc.BazelCcSanitizerToolchainVars(cfg)))
  29. files = append(files, newFile("java_toolchain", GeneratedBuildFileName, "")) // Creates a //java_toolchain package.
  30. files = append(files, newFile("java_toolchain", "constants.bzl", java_config.BazelJavaToolchainVars(cfg)))
  31. files = append(files, newFile("apex_toolchain", GeneratedBuildFileName, "")) // Creates a //apex_toolchain package.
  32. apexToolchainVars, err := apex.BazelApexToolchainVars()
  33. if err != nil {
  34. return nil, err
  35. }
  36. files = append(files, newFile("apex_toolchain", "constants.bzl", apexToolchainVars))
  37. files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.Serialize().ConvertedModules, "\n")))
  38. convertedModulePathMap, err := json.MarshalIndent(metrics.convertedModulePathMap, "", "\t")
  39. if err != nil {
  40. panic(err)
  41. }
  42. files = append(files, newFile("metrics", GeneratedBuildFileName, "")) // Creates a //metrics package.
  43. files = append(files, newFile("metrics", "converted_modules_path_map.json", string(convertedModulePathMap)))
  44. files = append(files, newFile("metrics", "converted_modules_path_map.bzl", "modules = "+strings.ReplaceAll(string(convertedModulePathMap), "\\", "\\\\")))
  45. files = append(files, newFile("product_config", "soong_config_variables.bzl", cfg.Bp2buildSoongConfigDefinitions.String()))
  46. files = append(files, newFile("product_config", "arch_configuration.bzl", android.StarlarkArchConfigurations()))
  47. apiLevelsMap, err := android.GetApiLevelsMap(cfg)
  48. if err != nil {
  49. return nil, err
  50. }
  51. apiLevelsContent, err := json.Marshal(apiLevelsMap)
  52. if err != nil {
  53. return nil, err
  54. }
  55. files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`))
  56. // TODO(b/269691302) value of apiLevelsContent is product variable dependent and should be avoided for soong injection
  57. files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
  58. files = append(files, newFile("api_levels", "platform_versions.bzl", platformVersionContents(cfg)))
  59. files = append(files, newFile("allowlists", GeneratedBuildFileName, ""))
  60. // TODO(b/262781701): Create an alternate soong_build entrypoint for writing out these files only when requested
  61. files = append(files, newFile("allowlists", "mixed_build_prod_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelProdMode), "\n")+"\n"))
  62. files = append(files, newFile("allowlists", "mixed_build_staging_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelStagingMode), "\n")+"\n"))
  63. return files, nil
  64. }
  65. func platformVersionContents(cfg android.Config) string {
  66. // Despite these coming from cfg.productVariables, they are actually hardcoded in global
  67. // makefiles, not set in individual product config makesfiles, so they're safe to just export
  68. // and load() directly.
  69. platformVersionActiveCodenames := make([]string, 0, len(cfg.PlatformVersionActiveCodenames()))
  70. for _, codename := range cfg.PlatformVersionActiveCodenames() {
  71. platformVersionActiveCodenames = append(platformVersionActiveCodenames, fmt.Sprintf("%q", codename))
  72. }
  73. platformSdkVersion := "None"
  74. if cfg.RawPlatformSdkVersion() != nil {
  75. platformSdkVersion = strconv.Itoa(*cfg.RawPlatformSdkVersion())
  76. }
  77. return fmt.Sprintf(`
  78. platform_versions = struct(
  79. platform_sdk_final = %s,
  80. platform_sdk_version = %s,
  81. platform_sdk_codename = %q,
  82. platform_version_active_codenames = [%s],
  83. )
  84. `, starlark_fmt.PrintBool(cfg.PlatformSdkFinal()), platformSdkVersion, cfg.PlatformSdkCodename(), strings.Join(platformVersionActiveCodenames, ", "))
  85. }
  86. func CreateBazelFiles(
  87. cfg android.Config,
  88. ruleShims map[string]RuleShim,
  89. buildToTargets map[string]BazelTargets,
  90. mode CodegenMode) []BazelFile {
  91. var files []BazelFile
  92. if mode == QueryView {
  93. // Write top level WORKSPACE.
  94. files = append(files, newFile("", "WORKSPACE", ""))
  95. // Used to denote that the top level directory is a package.
  96. files = append(files, newFile("", GeneratedBuildFileName, ""))
  97. files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
  98. // These files are only used for queryview.
  99. files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
  100. for bzlFileName, ruleShim := range ruleShims {
  101. files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
  102. }
  103. files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
  104. }
  105. files = append(files, createBuildFiles(buildToTargets, mode)...)
  106. return files
  107. }
  108. func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
  109. files := make([]BazelFile, 0, len(buildToTargets))
  110. for _, dir := range android.SortedKeys(buildToTargets) {
  111. targets := buildToTargets[dir]
  112. targets.sort()
  113. var content string
  114. if mode == Bp2Build || mode == ApiBp2build {
  115. content = `# READ THIS FIRST:
  116. # This file was automatically generated by bp2build for the Bazel migration project.
  117. # Feel free to edit or test it, but do *not* check it into your version control system.
  118. `
  119. content += targets.LoadStatements()
  120. content += "\n\n"
  121. // Get package rule from the handcrafted BUILD file, otherwise emit the default one.
  122. prText := "package(default_visibility = [\"//visibility:public\"])\n"
  123. if pr := targets.packageRule(); pr != nil {
  124. prText = pr.content
  125. }
  126. content += prText
  127. } else if mode == QueryView {
  128. content = soongModuleLoad
  129. }
  130. if content != "" {
  131. // If there are load statements, add a couple of newlines.
  132. content += "\n\n"
  133. }
  134. content += targets.String()
  135. files = append(files, newFile(dir, GeneratedBuildFileName, content))
  136. }
  137. return files
  138. }
  139. func newFile(dir, basename, content string) BazelFile {
  140. return BazelFile{
  141. Dir: dir,
  142. Basename: basename,
  143. Contents: content,
  144. }
  145. }
  146. const (
  147. bazelRulesSubDir = "build/bazel/queryview_rules"
  148. // additional files:
  149. // * workspace file
  150. // * base BUILD file
  151. // * rules BUILD file
  152. // * rules providers.bzl file
  153. // * rules soong_module.bzl file
  154. numAdditionalFiles = 5
  155. )
  156. var (
  157. // Certain module property names are blocklisted/ignored here, for the reasons commented.
  158. ignoredPropNames = map[string]bool{
  159. "name": true, // redundant, since this is explicitly generated for every target
  160. "from": true, // reserved keyword
  161. "in": true, // reserved keyword
  162. "size": true, // reserved for tests
  163. "arch": true, // interface prop type is not supported yet.
  164. "multilib": true, // interface prop type is not supported yet.
  165. "target": true, // interface prop type is not supported yet.
  166. "visibility": true, // Bazel has native visibility semantics. Handle later.
  167. "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
  168. "for": true, // reserved keyword, b/233579439
  169. "versions_with_info": true, // TODO(b/245730552) struct properties not fully supported
  170. }
  171. )
  172. func shouldGenerateAttribute(prop string) bool {
  173. return !ignoredPropNames[prop]
  174. }
  175. func shouldSkipStructField(field reflect.StructField) bool {
  176. if field.PkgPath != "" && !field.Anonymous {
  177. // Skip unexported fields. Some properties are
  178. // internal to Soong only, and these fields do not have PkgPath.
  179. return true
  180. }
  181. // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc.
  182. // but cannot be set in a .bp file
  183. if proptools.HasTag(field, "blueprint", "mutated") {
  184. return true
  185. }
  186. return false
  187. }
  188. // FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
  189. // testonly = True, forcing other rules that depend on _test rules to also be
  190. // marked as testonly = True. This semantic constraint is not present in Soong.
  191. // To work around, rename "*_test" rules to "*_test_".
  192. func canonicalizeModuleType(moduleName string) string {
  193. if strings.HasSuffix(moduleName, "_test") {
  194. return moduleName + "_"
  195. }
  196. return moduleName
  197. }