conversion.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. files = append(files, newFile("allowlists", "env.bzl", android.EnvironmentVarsFile(cfg)))
  61. // TODO(b/262781701): Create an alternate soong_build entrypoint for writing out these files only when requested
  62. files = append(files, newFile("allowlists", "mixed_build_prod_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelProdMode), "\n")+"\n"))
  63. files = append(files, newFile("allowlists", "mixed_build_staging_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelStagingMode), "\n")+"\n"))
  64. return files, nil
  65. }
  66. func platformVersionContents(cfg android.Config) string {
  67. // Despite these coming from cfg.productVariables, they are actually hardcoded in global
  68. // makefiles, not set in individual product config makesfiles, so they're safe to just export
  69. // and load() directly.
  70. platformVersionActiveCodenames := make([]string, 0, len(cfg.PlatformVersionActiveCodenames()))
  71. for _, codename := range cfg.PlatformVersionActiveCodenames() {
  72. platformVersionActiveCodenames = append(platformVersionActiveCodenames, fmt.Sprintf("%q", codename))
  73. }
  74. platformSdkVersion := "None"
  75. if cfg.RawPlatformSdkVersion() != nil {
  76. platformSdkVersion = strconv.Itoa(*cfg.RawPlatformSdkVersion())
  77. }
  78. return fmt.Sprintf(`
  79. platform_versions = struct(
  80. platform_sdk_final = %s,
  81. platform_sdk_version = %s,
  82. platform_sdk_codename = %q,
  83. platform_version_active_codenames = [%s],
  84. )
  85. `, starlark_fmt.PrintBool(cfg.PlatformSdkFinal()), platformSdkVersion, cfg.PlatformSdkCodename(), strings.Join(platformVersionActiveCodenames, ", "))
  86. }
  87. func CreateBazelFiles(
  88. cfg android.Config,
  89. ruleShims map[string]RuleShim,
  90. buildToTargets map[string]BazelTargets,
  91. mode CodegenMode) []BazelFile {
  92. var files []BazelFile
  93. if mode == QueryView {
  94. // Write top level WORKSPACE.
  95. files = append(files, newFile("", "WORKSPACE", ""))
  96. // Used to denote that the top level directory is a package.
  97. files = append(files, newFile("", GeneratedBuildFileName, ""))
  98. files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
  99. // These files are only used for queryview.
  100. files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
  101. for bzlFileName, ruleShim := range ruleShims {
  102. files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
  103. }
  104. files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
  105. }
  106. files = append(files, createBuildFiles(buildToTargets, mode)...)
  107. return files
  108. }
  109. func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
  110. files := make([]BazelFile, 0, len(buildToTargets))
  111. for _, dir := range android.SortedKeys(buildToTargets) {
  112. targets := buildToTargets[dir]
  113. targets.sort()
  114. var content string
  115. if mode == Bp2Build || mode == ApiBp2build {
  116. content = `# READ THIS FIRST:
  117. # This file was automatically generated by bp2build for the Bazel migration project.
  118. # Feel free to edit or test it, but do *not* check it into your version control system.
  119. `
  120. content += targets.LoadStatements()
  121. content += "\n\n"
  122. // Get package rule from the handcrafted BUILD file, otherwise emit the default one.
  123. prText := "package(default_visibility = [\"//visibility:public\"])\n"
  124. if pr := targets.packageRule(); pr != nil {
  125. prText = pr.content
  126. }
  127. content += prText
  128. } else if mode == QueryView {
  129. content = soongModuleLoad
  130. }
  131. if content != "" {
  132. // If there are load statements, add a couple of newlines.
  133. content += "\n\n"
  134. }
  135. content += targets.String()
  136. files = append(files, newFile(dir, GeneratedBuildFileName, content))
  137. }
  138. return files
  139. }
  140. func newFile(dir, basename, content string) BazelFile {
  141. return BazelFile{
  142. Dir: dir,
  143. Basename: basename,
  144. Contents: content,
  145. }
  146. }
  147. const (
  148. bazelRulesSubDir = "build/bazel/queryview_rules"
  149. // additional files:
  150. // * workspace file
  151. // * base BUILD file
  152. // * rules BUILD file
  153. // * rules providers.bzl file
  154. // * rules soong_module.bzl file
  155. numAdditionalFiles = 5
  156. )
  157. var (
  158. // Certain module property names are blocklisted/ignored here, for the reasons commented.
  159. ignoredPropNames = map[string]bool{
  160. "name": true, // redundant, since this is explicitly generated for every target
  161. "from": true, // reserved keyword
  162. "in": true, // reserved keyword
  163. "size": true, // reserved for tests
  164. "arch": true, // interface prop type is not supported yet.
  165. "multilib": true, // interface prop type is not supported yet.
  166. "target": true, // interface prop type is not supported yet.
  167. "visibility": true, // Bazel has native visibility semantics. Handle later.
  168. "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
  169. "for": true, // reserved keyword, b/233579439
  170. "versions_with_info": true, // TODO(b/245730552) struct properties not fully supported
  171. }
  172. )
  173. func shouldGenerateAttribute(prop string) bool {
  174. return !ignoredPropNames[prop]
  175. }
  176. func shouldSkipStructField(field reflect.StructField) bool {
  177. if field.PkgPath != "" && !field.Anonymous {
  178. // Skip unexported fields. Some properties are
  179. // internal to Soong only, and these fields do not have PkgPath.
  180. return true
  181. }
  182. // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc.
  183. // but cannot be set in a .bp file
  184. if proptools.HasTag(field, "blueprint", "mutated") {
  185. return true
  186. }
  187. return false
  188. }
  189. // FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
  190. // testonly = True, forcing other rules that depend on _test rules to also be
  191. // marked as testonly = True. This semantic constraint is not present in Soong.
  192. // To work around, rename "*_test" rules to "*_test_".
  193. func canonicalizeModuleType(moduleName string) string {
  194. if strings.HasSuffix(moduleName, "_test") {
  195. return moduleName + "_"
  196. }
  197. return moduleName
  198. }