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, |
Spandan Das | 394aa32 | 2022-11-03 17:02:10 +0000 | [diff] [blame] | 66 | RootDirs: androidBpSearchDirs(config), |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 67 | FollowSymlinks: config.environ.IsEnvTrue("ALLOW_BP_UNDER_SYMLINKS"), |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 68 | ExcludeDirs: []string{".git", ".repo"}, |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 69 | PruneFiles: pruneFiles, |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 70 | IncludeFiles: []string{ |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 71 | // Kati build definitions. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 72 | "Android.mk", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 73 | // Product configuration files. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 74 | "AndroidProducts.mk", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 75 | // General Soong build definitions, using the Blueprint syntax. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 76 | "Android.bp", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 77 | // Kati clean definitions. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 78 | "CleanSpec.mk", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 79 | // Ownership definition. |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 80 | "OWNERS", |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 81 | // Test configuration for modules in directories that contain this |
| 82 | // file. |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 83 | "TEST_MAPPING", |
Wei Li | 2c9e8d6 | 2023-05-05 01:07:15 -0700 | [diff] [blame] | 84 | // METADATA file of packages |
| 85 | "METADATA", |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 86 | }, |
Colin Cross | 8d411ff | 2023-12-07 10:31:24 -0800 | [diff] [blame] | 87 | // .mk files for product/board configuration. |
| 88 | IncludeSuffixes: []string{".mk"}, |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 89 | } |
| 90 | dumpDir := config.FileListDir() |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 91 | f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard), |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 92 | filepath.Join(dumpDir, "files.db")) |
| 93 | if err != nil { |
| 94 | ctx.Fatalf("Could not create module-finder: %v", err) |
| 95 | } |
| 96 | return f |
| 97 | } |
| 98 | |
Spandan Das | 394aa32 | 2022-11-03 17:02:10 +0000 | [diff] [blame] | 99 | func androidBpSearchDirs(config Config) []string { |
| 100 | dirs := []string{"."} // always search from root of source tree. |
| 101 | if config.searchApiDir { |
| 102 | // Search in out/api_surfaces |
| 103 | dirs = append(dirs, config.ApiSurfacesOutDir()) |
| 104 | } |
| 105 | return dirs |
| 106 | } |
| 107 | |
Cole Faust | 8d47c48 | 2022-01-26 14:27:44 -0800 | [diff] [blame] | 108 | func findProductAndBoardConfigFiles(entries finder.DirEntries) (dirNames []string, fileNames []string) { |
| 109 | matches := []string{} |
| 110 | for _, foundName := range entries.FileNames { |
| 111 | if foundName != "Android.mk" && |
| 112 | foundName != "AndroidProducts.mk" && |
| 113 | foundName != "CleanSpec.mk" && |
| 114 | strings.HasSuffix(foundName, ".mk") { |
| 115 | matches = append(matches, foundName) |
| 116 | } |
| 117 | } |
| 118 | return entries.DirNames, matches |
| 119 | } |
| 120 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 121 | // FindSources searches for source files known to <f> and writes them to the filesystem for |
| 122 | // use later. |
| 123 | func FindSources(ctx Context, config Config, f *finder.Finder) { |
| 124 | // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder |
| 125 | // if a caller such as multiproduct_kati wants to share one Finder among several builds |
| 126 | dumpDir := config.FileListDir() |
| 127 | os.MkdirAll(dumpDir, 0777) |
| 128 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 129 | // Stop searching a subdirectory recursively after finding an Android.mk. |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 130 | androidMks := f.FindFirstNamedAt(".", "Android.mk") |
Wei Li | f6ac275 | 2024-10-15 18:35:53 +0000 | [diff] [blame] | 131 | androidMks = ignoreNdkAndroidMks(androidMks) |
Wei Li | d0a2e32 | 2024-02-06 11:54:55 -0800 | [diff] [blame] | 132 | blockAndroidMks(ctx, androidMks) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 133 | err := dumpListToFile(ctx, config, androidMks, filepath.Join(dumpDir, "Android.mk.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 134 | if err != nil { |
| 135 | ctx.Fatalf("Could not export module list: %v", err) |
| 136 | } |
| 137 | |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame] | 138 | // Gate collecting/reporting mk metrics on builds that specifically request |
| 139 | // it, as identifying the total number of mk files adds 4-5ms onto null |
| 140 | // builds. |
| 141 | if config.reportMkMetrics { |
| 142 | androidMksTotal := f.FindNamedAt(".", "Android.mk") |
| 143 | |
| 144 | ctx.Metrics.SetToplevelMakefiles(len(androidMks)) |
| 145 | ctx.Metrics.SetTotalMakefiles(len(androidMksTotal)) |
| 146 | ctx.Metrics.DumpMkMetrics(config.MkMetrics()) |
| 147 | } |
| 148 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 149 | // Stop searching a subdirectory recursively after finding a CleanSpec.mk. |
| 150 | cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk") |
| 151 | err = dumpListToFile(ctx, config, cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list")) |
| 152 | if err != nil { |
| 153 | ctx.Fatalf("Could not export module list: %v", err) |
| 154 | } |
| 155 | |
| 156 | // 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] | 157 | androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk") |
| 158 | androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...) |
| 159 | androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 160 | err = dumpListToFile(ctx, config, androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list")) |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 161 | if err != nil { |
| 162 | ctx.Fatalf("Could not export product list: %v", err) |
| 163 | } |
| 164 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 165 | // Recursively look for all OWNERS files. |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 166 | owners := f.FindNamedAt(".", "OWNERS") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 167 | err = dumpListToFile(ctx, config, owners, filepath.Join(dumpDir, "OWNERS.list")) |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 168 | if err != nil { |
| 169 | ctx.Fatalf("Could not find OWNERS: %v", err) |
| 170 | } |
| 171 | |
Wei Li | 2c9e8d6 | 2023-05-05 01:07:15 -0700 | [diff] [blame] | 172 | // Recursively look for all METADATA files. |
| 173 | metadataFiles := f.FindNamedAt(".", "METADATA") |
Wei Li | ae3c12b | 2024-10-29 15:26:49 -0700 | [diff] [blame] | 174 | metadataFiles = ignoreNonAndroidMetadataFiles(metadataFiles) |
Wei Li | 2c9e8d6 | 2023-05-05 01:07:15 -0700 | [diff] [blame] | 175 | err = dumpListToFile(ctx, config, metadataFiles, filepath.Join(dumpDir, "METADATA.list")) |
| 176 | if err != nil { |
| 177 | ctx.Fatalf("Could not find METADATA: %v", err) |
| 178 | } |
| 179 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 180 | // Recursively look for all TEST_MAPPING files. |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 181 | testMappings := f.FindNamedAt(".", "TEST_MAPPING") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 182 | err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list")) |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 183 | if err != nil { |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 184 | ctx.Fatalf("Could not find TEST_MAPPING: %v", err) |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 187 | // Recursively look for all Android.bp files |
Jeff Gaston | 29e959d | 2017-12-07 12:38:53 -0800 | [diff] [blame] | 188 | androidBps := f.FindNamedAt(".", "Android.bp") |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 189 | if len(androidBps) == 0 { |
| 190 | ctx.Fatalf("No Android.bp found") |
| 191 | } |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 192 | err = dumpListToFile(ctx, config, androidBps, filepath.Join(dumpDir, "Android.bp.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 193 | if err != nil { |
| 194 | ctx.Fatalf("Could not find modules: %v", err) |
| 195 | } |
Colin Cross | 96e5e41 | 2020-07-06 17:13:43 -0700 | [diff] [blame] | 196 | |
Cole Faust | 8d47c48 | 2022-01-26 14:27:44 -0800 | [diff] [blame] | 197 | // Recursively look for all product/board config files. |
| 198 | configurationFiles := f.FindMatching(".", findProductAndBoardConfigFiles) |
| 199 | err = dumpListToFile(ctx, config, configurationFiles, filepath.Join(dumpDir, "configuration.list")) |
| 200 | if err != nil { |
| 201 | ctx.Fatalf("Could not export product/board configuration list: %v", err) |
| 202 | } |
| 203 | |
Colin Cross | 96e5e41 | 2020-07-06 17:13:43 -0700 | [diff] [blame] | 204 | if config.Dist() { |
| 205 | f.WaitForDbDump() |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 206 | // Dist the files.db plain text database. |
Colin Cross | 96e5e41 | 2020-07-06 17:13:43 -0700 | [diff] [blame] | 207 | distFile(ctx, config, f.DbPath, "module_paths") |
| 208 | } |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Jingwen Chen | 096a3bf | 2020-11-17 04:53:22 -0500 | [diff] [blame] | 211 | // Write the .list files to disk. |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 212 | func dumpListToFile(ctx Context, config Config, list []string, filePath string) (err error) { |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 213 | desiredText := strings.Join(list, "\n") |
| 214 | desiredBytes := []byte(desiredText) |
| 215 | actualBytes, readErr := ioutil.ReadFile(filePath) |
| 216 | if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) { |
| 217 | err = ioutil.WriteFile(filePath, desiredBytes, 0777) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 218 | if err != nil { |
| 219 | return err |
| 220 | } |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 221 | } |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 222 | |
| 223 | distFile(ctx, config, filePath, "module_paths") |
| 224 | |
| 225 | return nil |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 226 | } |
Wei Li | ae3c12b | 2024-10-29 15:26:49 -0700 | [diff] [blame] | 227 | |
| 228 | func ignoreNonAndroidMetadataFiles(metadataFiles []string) []string { |
| 229 | result := make([]string, 0, len(metadataFiles)) |
| 230 | for _, file := range metadataFiles { |
| 231 | // Ignore files like prebuilts/clang/host/linux-x86/clang-r536225/python3/lib/python3.11/site-packages/pip-23.1.2.dist-info/METADATA |
| 232 | // these METADATA files are from upstream and are not the METADATA files used in Android codebase. |
| 233 | if strings.Contains(file, "prebuilts/clang/host/") && strings.Contains(file, "/site-packages/") { |
| 234 | continue |
| 235 | } |
| 236 | result = append(result, file) |
| 237 | } |
| 238 | return result |
| 239 | } |