Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 18 | "bytes" |
| 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "strings" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 23 | |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 24 | "android/soong/finder" |
| 25 | "android/soong/finder/fs" |
| 26 | "android/soong/ui/logger" |
| 27 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 28 | "android/soong/ui/metrics" |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 31 | // This file provides an interface to the Finder type for soong_ui. Finder is |
| 32 | // used to recursively traverse the source tree to gather paths of files, such |
| 33 | // as Android.bp or Android.mk, and store the lists/database of paths in files |
| 34 | // under `$OUT_DIR/.module_paths`. This directory can also be dist'd. |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 35 | |
| 36 | // NewSourceFinder returns a new Finder configured to search for source files. |
| 37 | // Callers of NewSourceFinder should call <f.Shutdown()> when done |
| 38 | func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 39 | ctx.BeginTrace(metrics.RunSetupTool, "find modules") |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 40 | defer ctx.EndTrace() |
| 41 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 42 | // Set up the working directory for the Finder. |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 43 | dir, err := os.Getwd() |
| 44 | if err != nil { |
| 45 | ctx.Fatalf("No working directory for module-finder: %v", err.Error()) |
| 46 | } |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 47 | filesystem := fs.OsFs |
| 48 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 49 | // .out-dir and .find-ignore are markers for Finder to ignore siblings and |
| 50 | // subdirectories of the directory Finder finds them in, hence stopping the |
| 51 | // search recursively down those branches. It's possible that these files |
| 52 | // are in the root directory, and if they are, then the subsequent error |
| 53 | // messages are very confusing, so check for that here. |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 54 | pruneFiles := []string{".out-dir", ".find-ignore"} |
| 55 | for _, name := range pruneFiles { |
| 56 | prunePath := filepath.Join(dir, name) |
| 57 | _, statErr := filesystem.Lstat(prunePath) |
| 58 | if statErr == nil { |
| 59 | ctx.Fatalf("%v must not exist", prunePath) |
| 60 | } |
| 61 | } |
| 62 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 63 | // Set up configuration parameters for the Finder cache. |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 64 | cacheParams := finder.CacheParams{ |
| 65 | WorkingDirectory: dir, |
| 66 | RootDirs: []string{"."}, |
| 67 | ExcludeDirs: []string{".git", ".repo"}, |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 68 | PruneFiles: pruneFiles, |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 69 | IncludeFiles: []string{ |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 70 | // Kati build definitions. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 71 | "Android.mk", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 72 | // Product configuration files. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 73 | "AndroidProducts.mk", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 74 | // General Soong build definitions, using the Blueprint syntax. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 75 | "Android.bp", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 76 | // Bazel build definitions. |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 77 | "BUILD.bazel", |
Rupert Shuttleworth | c29cace | 2021-05-18 07:39:37 -0400 | [diff] [blame] | 78 | // Bazel build definitions. |
| 79 | "BUILD", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 80 | // Kati clean definitions. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 81 | "CleanSpec.mk", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 82 | // Ownership definition. |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 83 | "OWNERS", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 84 | // Test configuration for modules in directories that contain this |
| 85 | // file. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 86 | "TEST_MAPPING", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 87 | // Bazel top-level file to mark a directory as a Bazel workspace. |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 88 | "WORKSPACE", |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 89 | }, |
Cole Faust | 8d47c48 | 2022-01-26 14:27:44 -0800 | [diff] [blame] | 90 | // Bazel Starlark configuration files and all .mk files for product/board configuration. |
| 91 | IncludeSuffixes: []string{".bzl", ".mk"}, |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 92 | } |
| 93 | dumpDir := config.FileListDir() |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 94 | f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard), |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 95 | filepath.Join(dumpDir, "files.db")) |
| 96 | if err != nil { |
| 97 | ctx.Fatalf("Could not create module-finder: %v", err) |
| 98 | } |
| 99 | return f |
| 100 | } |
| 101 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 102 | // Finds the list of Bazel-related files (BUILD, WORKSPACE and Starlark) in the tree. |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 103 | func findBazelFiles(entries finder.DirEntries) (dirNames []string, fileNames []string) { |
| 104 | matches := []string{} |
| 105 | for _, foundName := range entries.FileNames { |
Rupert Shuttleworth | c29cace | 2021-05-18 07:39:37 -0400 | [diff] [blame] | 106 | if foundName == "BUILD.bazel" || foundName == "BUILD" || foundName == "WORKSPACE" || strings.HasSuffix(foundName, ".bzl") { |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 107 | matches = append(matches, foundName) |
| 108 | } |
| 109 | } |
| 110 | return entries.DirNames, matches |
| 111 | } |
| 112 | |
Cole Faust | 8d47c48 | 2022-01-26 14:27:44 -0800 | [diff] [blame] | 113 | func findProductAndBoardConfigFiles(entries finder.DirEntries) (dirNames []string, fileNames []string) { |
| 114 | matches := []string{} |
| 115 | for _, foundName := range entries.FileNames { |
| 116 | if foundName != "Android.mk" && |
| 117 | foundName != "AndroidProducts.mk" && |
| 118 | foundName != "CleanSpec.mk" && |
| 119 | strings.HasSuffix(foundName, ".mk") { |
| 120 | matches = append(matches, foundName) |
| 121 | } |
| 122 | } |
| 123 | return entries.DirNames, matches |
| 124 | } |
| 125 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 126 | // FindSources searches for source files known to <f> and writes them to the filesystem for |
| 127 | // use later. |
| 128 | func FindSources(ctx Context, config Config, f *finder.Finder) { |
| 129 | // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder |
| 130 | // if a caller such as multiproduct_kati wants to share one Finder among several builds |
| 131 | dumpDir := config.FileListDir() |
| 132 | os.MkdirAll(dumpDir, 0777) |
| 133 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 134 | // Stop searching a subdirectory recursively after finding an Android.mk. |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 135 | androidMks := f.FindFirstNamedAt(".", "Android.mk") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 136 | err := dumpListToFile(ctx, config, androidMks, filepath.Join(dumpDir, "Android.mk.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 137 | if err != nil { |
| 138 | ctx.Fatalf("Could not export module list: %v", err) |
| 139 | } |
| 140 | |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame^] | 141 | // Gate collecting/reporting mk metrics on builds that specifically request |
| 142 | // it, as identifying the total number of mk files adds 4-5ms onto null |
| 143 | // builds. |
| 144 | if config.reportMkMetrics { |
| 145 | androidMksTotal := f.FindNamedAt(".", "Android.mk") |
| 146 | |
| 147 | ctx.Metrics.SetToplevelMakefiles(len(androidMks)) |
| 148 | ctx.Metrics.SetTotalMakefiles(len(androidMksTotal)) |
| 149 | ctx.Metrics.DumpMkMetrics(config.MkMetrics()) |
| 150 | } |
| 151 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 152 | // Stop searching a subdirectory recursively after finding a CleanSpec.mk. |
| 153 | cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk") |
| 154 | err = dumpListToFile(ctx, config, cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list")) |
| 155 | if err != nil { |
| 156 | ctx.Fatalf("Could not export module list: %v", err) |
| 157 | } |
| 158 | |
| 159 | // Only consider AndroidProducts.mk in device/, vendor/ and product/, recursively in these directories. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 160 | androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk") |
| 161 | androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...) |
| 162 | androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 163 | err = dumpListToFile(ctx, config, androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list")) |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 164 | if err != nil { |
| 165 | ctx.Fatalf("Could not export product list: %v", err) |
| 166 | } |
| 167 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 168 | // Recursively look for all Bazel related files. |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 169 | bazelFiles := f.FindMatching(".", findBazelFiles) |
| 170 | err = dumpListToFile(ctx, config, bazelFiles, filepath.Join(dumpDir, "bazel.list")) |
| 171 | if err != nil { |
| 172 | ctx.Fatalf("Could not export bazel BUILD list: %v", err) |
| 173 | } |
| 174 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 175 | // Recursively look for all OWNERS files. |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 176 | owners := f.FindNamedAt(".", "OWNERS") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 177 | err = dumpListToFile(ctx, config, owners, filepath.Join(dumpDir, "OWNERS.list")) |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 178 | if err != nil { |
| 179 | ctx.Fatalf("Could not find OWNERS: %v", err) |
| 180 | } |
| 181 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 182 | // Recursively look for all TEST_MAPPING files. |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 183 | testMappings := f.FindNamedAt(".", "TEST_MAPPING") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 184 | err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list")) |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 185 | if err != nil { |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 186 | ctx.Fatalf("Could not find TEST_MAPPING: %v", err) |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 189 | // Recursively look for all Android.bp files |
Jeff Gaston | 29e959d | 2017-12-07 12:38:53 -0800 | [diff] [blame] | 190 | androidBps := f.FindNamedAt(".", "Android.bp") |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 191 | if len(androidBps) == 0 { |
| 192 | ctx.Fatalf("No Android.bp found") |
| 193 | } |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 194 | err = dumpListToFile(ctx, config, androidBps, filepath.Join(dumpDir, "Android.bp.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 195 | if err != nil { |
| 196 | ctx.Fatalf("Could not find modules: %v", err) |
| 197 | } |
Colin Cross | 96e5e41 | 2020-07-06 17:13:43 -0700 | [diff] [blame] | 198 | |
Cole Faust | 8d47c48 | 2022-01-26 14:27:44 -0800 | [diff] [blame] | 199 | // Recursively look for all product/board config files. |
| 200 | configurationFiles := f.FindMatching(".", findProductAndBoardConfigFiles) |
| 201 | err = dumpListToFile(ctx, config, configurationFiles, filepath.Join(dumpDir, "configuration.list")) |
| 202 | if err != nil { |
| 203 | ctx.Fatalf("Could not export product/board configuration list: %v", err) |
| 204 | } |
| 205 | |
Colin Cross | 96e5e41 | 2020-07-06 17:13:43 -0700 | [diff] [blame] | 206 | if config.Dist() { |
| 207 | f.WaitForDbDump() |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 208 | // Dist the files.db plain text database. |
Colin Cross | 96e5e41 | 2020-07-06 17:13:43 -0700 | [diff] [blame] | 209 | distFile(ctx, config, f.DbPath, "module_paths") |
| 210 | } |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 213 | // Write the .list files to disk. |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 214 | func dumpListToFile(ctx Context, config Config, list []string, filePath string) (err error) { |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 215 | desiredText := strings.Join(list, "\n") |
| 216 | desiredBytes := []byte(desiredText) |
| 217 | actualBytes, readErr := ioutil.ReadFile(filePath) |
| 218 | if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) { |
| 219 | err = ioutil.WriteFile(filePath, desiredBytes, 0777) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 220 | if err != nil { |
| 221 | return err |
| 222 | } |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 223 | } |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 224 | |
| 225 | distFile(ctx, config, filePath, "module_paths") |
| 226 | |
| 227 | return nil |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 228 | } |