finder.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2017 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 build
  15. import (
  16. "bytes"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "strings"
  21. "android/soong/finder"
  22. "android/soong/finder/fs"
  23. "android/soong/ui/logger"
  24. "android/soong/ui/metrics"
  25. )
  26. // This file provides an interface to the Finder type for soong_ui. Finder is
  27. // used to recursively traverse the source tree to gather paths of files, such
  28. // as Android.bp or Android.mk, and store the lists/database of paths in files
  29. // under `$OUT_DIR/.module_paths`. This directory can also be dist'd.
  30. // NewSourceFinder returns a new Finder configured to search for source files.
  31. // Callers of NewSourceFinder should call <f.Shutdown()> when done
  32. func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) {
  33. ctx.BeginTrace(metrics.RunSetupTool, "find modules")
  34. defer ctx.EndTrace()
  35. // Set up the working directory for the Finder.
  36. dir, err := os.Getwd()
  37. if err != nil {
  38. ctx.Fatalf("No working directory for module-finder: %v", err.Error())
  39. }
  40. filesystem := fs.OsFs
  41. // .out-dir and .find-ignore are markers for Finder to ignore siblings and
  42. // subdirectories of the directory Finder finds them in, hence stopping the
  43. // search recursively down those branches. It's possible that these files
  44. // are in the root directory, and if they are, then the subsequent error
  45. // messages are very confusing, so check for that here.
  46. pruneFiles := []string{".out-dir", ".find-ignore"}
  47. for _, name := range pruneFiles {
  48. prunePath := filepath.Join(dir, name)
  49. _, statErr := filesystem.Lstat(prunePath)
  50. if statErr == nil {
  51. ctx.Fatalf("%v must not exist", prunePath)
  52. }
  53. }
  54. // Set up configuration parameters for the Finder cache.
  55. cacheParams := finder.CacheParams{
  56. WorkingDirectory: dir,
  57. RootDirs: []string{"."},
  58. ExcludeDirs: []string{".git", ".repo"},
  59. PruneFiles: pruneFiles,
  60. IncludeFiles: []string{
  61. // Kati build definitions.
  62. "Android.mk",
  63. // Product configuration files.
  64. "AndroidProducts.mk",
  65. // General Soong build definitions, using the Blueprint syntax.
  66. "Android.bp",
  67. // Bazel build definitions.
  68. "BUILD.bazel",
  69. // Bazel build definitions.
  70. "BUILD",
  71. // Kati clean definitions.
  72. "CleanSpec.mk",
  73. // Ownership definition.
  74. "OWNERS",
  75. // Test configuration for modules in directories that contain this
  76. // file.
  77. "TEST_MAPPING",
  78. // Bazel top-level file to mark a directory as a Bazel workspace.
  79. "WORKSPACE",
  80. },
  81. // Bazel Starlark configuration files.
  82. IncludeSuffixes: []string{".bzl"},
  83. }
  84. dumpDir := config.FileListDir()
  85. f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard),
  86. filepath.Join(dumpDir, "files.db"))
  87. if err != nil {
  88. ctx.Fatalf("Could not create module-finder: %v", err)
  89. }
  90. return f
  91. }
  92. // Finds the list of Bazel-related files (BUILD, WORKSPACE and Starlark) in the tree.
  93. func findBazelFiles(entries finder.DirEntries) (dirNames []string, fileNames []string) {
  94. matches := []string{}
  95. for _, foundName := range entries.FileNames {
  96. if foundName == "BUILD.bazel" || foundName == "BUILD" || foundName == "WORKSPACE" || strings.HasSuffix(foundName, ".bzl") {
  97. matches = append(matches, foundName)
  98. }
  99. }
  100. return entries.DirNames, matches
  101. }
  102. // FindSources searches for source files known to <f> and writes them to the filesystem for
  103. // use later.
  104. func FindSources(ctx Context, config Config, f *finder.Finder) {
  105. // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder
  106. // if a caller such as multiproduct_kati wants to share one Finder among several builds
  107. dumpDir := config.FileListDir()
  108. os.MkdirAll(dumpDir, 0777)
  109. // Stop searching a subdirectory recursively after finding an Android.mk.
  110. androidMks := f.FindFirstNamedAt(".", "Android.mk")
  111. err := dumpListToFile(ctx, config, androidMks, filepath.Join(dumpDir, "Android.mk.list"))
  112. if err != nil {
  113. ctx.Fatalf("Could not export module list: %v", err)
  114. }
  115. // Stop searching a subdirectory recursively after finding a CleanSpec.mk.
  116. cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk")
  117. err = dumpListToFile(ctx, config, cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list"))
  118. if err != nil {
  119. ctx.Fatalf("Could not export module list: %v", err)
  120. }
  121. // Only consider AndroidProducts.mk in device/, vendor/ and product/, recursively in these directories.
  122. androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk")
  123. androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...)
  124. androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...)
  125. err = dumpListToFile(ctx, config, androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list"))
  126. if err != nil {
  127. ctx.Fatalf("Could not export product list: %v", err)
  128. }
  129. // Recursively look for all Bazel related files.
  130. bazelFiles := f.FindMatching(".", findBazelFiles)
  131. err = dumpListToFile(ctx, config, bazelFiles, filepath.Join(dumpDir, "bazel.list"))
  132. if err != nil {
  133. ctx.Fatalf("Could not export bazel BUILD list: %v", err)
  134. }
  135. // Recursively look for all OWNERS files.
  136. owners := f.FindNamedAt(".", "OWNERS")
  137. err = dumpListToFile(ctx, config, owners, filepath.Join(dumpDir, "OWNERS.list"))
  138. if err != nil {
  139. ctx.Fatalf("Could not find OWNERS: %v", err)
  140. }
  141. // Recursively look for all TEST_MAPPING files.
  142. testMappings := f.FindNamedAt(".", "TEST_MAPPING")
  143. err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list"))
  144. if err != nil {
  145. ctx.Fatalf("Could not find TEST_MAPPING: %v", err)
  146. }
  147. // Recursively look for all Android.bp files
  148. androidBps := f.FindNamedAt(".", "Android.bp")
  149. if len(androidBps) == 0 {
  150. ctx.Fatalf("No Android.bp found")
  151. }
  152. err = dumpListToFile(ctx, config, androidBps, filepath.Join(dumpDir, "Android.bp.list"))
  153. if err != nil {
  154. ctx.Fatalf("Could not find modules: %v", err)
  155. }
  156. if config.Dist() {
  157. f.WaitForDbDump()
  158. // Dist the files.db plain text database.
  159. distFile(ctx, config, f.DbPath, "module_paths")
  160. }
  161. }
  162. // Write the .list files to disk.
  163. func dumpListToFile(ctx Context, config Config, list []string, filePath string) (err error) {
  164. desiredText := strings.Join(list, "\n")
  165. desiredBytes := []byte(desiredText)
  166. actualBytes, readErr := ioutil.ReadFile(filePath)
  167. if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) {
  168. err = ioutil.WriteFile(filePath, desiredBytes, 0777)
  169. if err != nil {
  170. return err
  171. }
  172. }
  173. distFile(ctx, config, filePath, "module_paths")
  174. return nil
  175. }