queryview.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2020 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 main
  15. import (
  16. "android/soong/starlark_import"
  17. "io/fs"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "android/soong/android"
  22. "android/soong/bp2build"
  23. )
  24. // A helper function to generate a Read-only Bazel workspace in outDir
  25. func createBazelWorkspace(ctx *bp2build.CodegenContext, outDir string, generateFilegroups bool) error {
  26. os.RemoveAll(outDir)
  27. ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories())
  28. res, err := bp2build.GenerateBazelTargets(ctx, generateFilegroups)
  29. if err != nil {
  30. panic(err)
  31. }
  32. filesToWrite := bp2build.CreateBazelFiles(ctx.Config(), ruleShims, res.BuildDirToTargets(),
  33. ctx.Mode())
  34. bazelRcFiles, err2 := CopyBazelRcFiles()
  35. if err2 != nil {
  36. return err2
  37. }
  38. filesToWrite = append(filesToWrite, bazelRcFiles...)
  39. for _, f := range filesToWrite {
  40. if err := writeReadOnlyFile(outDir, f); err != nil {
  41. return err
  42. }
  43. }
  44. // Add starlark deps here, so that they apply to both queryview and apibp2build which
  45. // both run this function.
  46. starlarkDeps, err2 := starlark_import.GetNinjaDeps()
  47. if err2 != nil {
  48. return err2
  49. }
  50. ctx.AddNinjaFileDeps(starlarkDeps...)
  51. return nil
  52. }
  53. // CopyBazelRcFiles creates BazelFiles for all the bazelrc files under
  54. // build/bazel. They're needed because the rc files are still read when running
  55. // queryview, so they have to be in the queryview workspace.
  56. func CopyBazelRcFiles() ([]bp2build.BazelFile, error) {
  57. result := make([]bp2build.BazelFile, 0)
  58. err := filepath.WalkDir(filepath.Join(topDir, "build/bazel"), func(path string, info fs.DirEntry, err error) error {
  59. if filepath.Ext(path) == ".bazelrc" {
  60. contents, err := os.ReadFile(path)
  61. if err != nil {
  62. return err
  63. }
  64. path, err = filepath.Rel(topDir, path)
  65. if err != nil {
  66. return err
  67. }
  68. result = append(result, bp2build.BazelFile{
  69. Dir: filepath.Dir(path),
  70. Basename: filepath.Base(path),
  71. Contents: string(contents),
  72. })
  73. }
  74. return nil
  75. })
  76. return result, err
  77. }
  78. // The auto-conversion directory should be read-only, sufficient for bazel query. The files
  79. // are not intended to be edited by end users.
  80. func writeReadOnlyFile(dir string, f bp2build.BazelFile) error {
  81. dir = filepath.Join(dir, f.Dir)
  82. if err := createDirectoryIfNonexistent(dir); err != nil {
  83. return err
  84. }
  85. pathToFile := filepath.Join(dir, f.Basename)
  86. // 0444 is read-only
  87. err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0444)
  88. return err
  89. }
  90. func writeReadWriteFile(dir string, f bp2build.BazelFile) error {
  91. dir = filepath.Join(dir, f.Dir)
  92. if err := createDirectoryIfNonexistent(dir); err != nil {
  93. return err
  94. }
  95. pathToFile := filepath.Join(dir, f.Basename)
  96. // 0644 is read-write
  97. err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0644)
  98. return err
  99. }
  100. func createDirectoryIfNonexistent(dir string) error {
  101. if _, err := os.Stat(dir); os.IsNotExist(err) {
  102. return os.MkdirAll(dir, os.ModePerm)
  103. } else {
  104. return err
  105. }
  106. }