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 ( |
| 18 | "android/soong/finder" |
Colin Cross | 8d6395c | 2017-12-21 15:46:01 -0800 | [diff] [blame] | 19 | "android/soong/finder/fs" |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 20 | "android/soong/ui/logger" |
| 21 | "bytes" |
| 22 | "io/ioutil" |
| 23 | "os" |
| 24 | "path/filepath" |
| 25 | "strings" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 26 | |
| 27 | "android/soong/ui/metrics" |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | // This file provides an interface to the Finder for use in Soong UI |
| 31 | // This file stores configuration information about which files to find |
| 32 | |
| 33 | // NewSourceFinder returns a new Finder configured to search for source files. |
| 34 | // Callers of NewSourceFinder should call <f.Shutdown()> when done |
| 35 | func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 36 | ctx.BeginTrace(metrics.RunSetupTool, "find modules") |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 37 | defer ctx.EndTrace() |
| 38 | |
| 39 | dir, err := os.Getwd() |
| 40 | if err != nil { |
| 41 | ctx.Fatalf("No working directory for module-finder: %v", err.Error()) |
| 42 | } |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 43 | filesystem := fs.OsFs |
| 44 | |
| 45 | // if the root dir is ignored, then the subsequent error messages are very confusing, |
| 46 | // so check for that upfront |
| 47 | pruneFiles := []string{".out-dir", ".find-ignore"} |
| 48 | for _, name := range pruneFiles { |
| 49 | prunePath := filepath.Join(dir, name) |
| 50 | _, statErr := filesystem.Lstat(prunePath) |
| 51 | if statErr == nil { |
| 52 | ctx.Fatalf("%v must not exist", prunePath) |
| 53 | } |
| 54 | } |
| 55 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 56 | cacheParams := finder.CacheParams{ |
| 57 | WorkingDirectory: dir, |
| 58 | RootDirs: []string{"."}, |
| 59 | ExcludeDirs: []string{".git", ".repo"}, |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 60 | PruneFiles: pruneFiles, |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 61 | IncludeFiles: []string{ |
| 62 | "Android.mk", |
| 63 | "AndroidProducts.mk", |
| 64 | "Android.bp", |
| 65 | "Blueprints", |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame^] | 66 | "BUILD.bazel", |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 67 | "CleanSpec.mk", |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 68 | "OWNERS", |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 69 | "TEST_MAPPING", |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame^] | 70 | "WORKSPACE", |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 71 | }, |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame^] | 72 | IncludeSuffixes: []string{".bzl"}, |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 73 | } |
| 74 | dumpDir := config.FileListDir() |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 75 | f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard), |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 76 | filepath.Join(dumpDir, "files.db")) |
| 77 | if err != nil { |
| 78 | ctx.Fatalf("Could not create module-finder: %v", err) |
| 79 | } |
| 80 | return f |
| 81 | } |
| 82 | |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame^] | 83 | func findBazelFiles(entries finder.DirEntries) (dirNames []string, fileNames []string) { |
| 84 | matches := []string{} |
| 85 | for _, foundName := range entries.FileNames { |
| 86 | if foundName == "BUILD.bazel" || foundName == "WORKSPACE" || strings.HasSuffix(foundName, ".bzl") { |
| 87 | matches = append(matches, foundName) |
| 88 | } |
| 89 | } |
| 90 | return entries.DirNames, matches |
| 91 | } |
| 92 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 93 | // FindSources searches for source files known to <f> and writes them to the filesystem for |
| 94 | // use later. |
| 95 | func FindSources(ctx Context, config Config, f *finder.Finder) { |
| 96 | // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder |
| 97 | // if a caller such as multiproduct_kati wants to share one Finder among several builds |
| 98 | dumpDir := config.FileListDir() |
| 99 | os.MkdirAll(dumpDir, 0777) |
| 100 | |
| 101 | androidMks := f.FindFirstNamedAt(".", "Android.mk") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 102 | err := dumpListToFile(ctx, config, androidMks, filepath.Join(dumpDir, "Android.mk.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 103 | if err != nil { |
| 104 | ctx.Fatalf("Could not export module list: %v", err) |
| 105 | } |
| 106 | |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 107 | androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk") |
| 108 | androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...) |
| 109 | androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 110 | err = dumpListToFile(ctx, config, androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list")) |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 111 | if err != nil { |
| 112 | ctx.Fatalf("Could not export product list: %v", err) |
| 113 | } |
| 114 | |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame^] | 115 | bazelFiles := f.FindMatching(".", findBazelFiles) |
| 116 | err = dumpListToFile(ctx, config, bazelFiles, filepath.Join(dumpDir, "bazel.list")) |
| 117 | if err != nil { |
| 118 | ctx.Fatalf("Could not export bazel BUILD list: %v", err) |
| 119 | } |
| 120 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 121 | cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 122 | err = dumpListToFile(ctx, config, cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 123 | if err != nil { |
| 124 | ctx.Fatalf("Could not export module list: %v", err) |
| 125 | } |
| 126 | |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 127 | owners := f.FindNamedAt(".", "OWNERS") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 128 | err = dumpListToFile(ctx, config, owners, filepath.Join(dumpDir, "OWNERS.list")) |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 129 | if err != nil { |
| 130 | ctx.Fatalf("Could not find OWNERS: %v", err) |
| 131 | } |
| 132 | |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 133 | testMappings := f.FindNamedAt(".", "TEST_MAPPING") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 134 | err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list")) |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 135 | if err != nil { |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 136 | ctx.Fatalf("Could not find TEST_MAPPING: %v", err) |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Jeff Gaston | 29e959d | 2017-12-07 12:38:53 -0800 | [diff] [blame] | 139 | androidBps := f.FindNamedAt(".", "Android.bp") |
| 140 | androidBps = append(androidBps, f.FindNamedAt("build/blueprint", "Blueprints")...) |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 141 | if len(androidBps) == 0 { |
| 142 | ctx.Fatalf("No Android.bp found") |
| 143 | } |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 144 | err = dumpListToFile(ctx, config, androidBps, filepath.Join(dumpDir, "Android.bp.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 145 | if err != nil { |
| 146 | ctx.Fatalf("Could not find modules: %v", err) |
| 147 | } |
Colin Cross | 96e5e41 | 2020-07-06 17:13:43 -0700 | [diff] [blame] | 148 | |
| 149 | if config.Dist() { |
| 150 | f.WaitForDbDump() |
| 151 | distFile(ctx, config, f.DbPath, "module_paths") |
| 152 | } |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 155 | func dumpListToFile(ctx Context, config Config, list []string, filePath string) (err error) { |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 156 | desiredText := strings.Join(list, "\n") |
| 157 | desiredBytes := []byte(desiredText) |
| 158 | actualBytes, readErr := ioutil.ReadFile(filePath) |
| 159 | if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) { |
| 160 | err = ioutil.WriteFile(filePath, desiredBytes, 0777) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 161 | if err != nil { |
| 162 | return err |
| 163 | } |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 164 | } |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 165 | |
| 166 | distFile(ctx, config, filePath, "module_paths") |
| 167 | |
| 168 | return nil |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 169 | } |