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", |
| 66 | "CleanSpec.mk", |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 67 | "OWNERS", |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 68 | "TEST_MAPPING", |
| 69 | }, |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 70 | } |
| 71 | dumpDir := config.FileListDir() |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 72 | f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard), |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 73 | filepath.Join(dumpDir, "files.db")) |
| 74 | if err != nil { |
| 75 | ctx.Fatalf("Could not create module-finder: %v", err) |
| 76 | } |
| 77 | return f |
| 78 | } |
| 79 | |
| 80 | // FindSources searches for source files known to <f> and writes them to the filesystem for |
| 81 | // use later. |
| 82 | func FindSources(ctx Context, config Config, f *finder.Finder) { |
| 83 | // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder |
| 84 | // if a caller such as multiproduct_kati wants to share one Finder among several builds |
| 85 | dumpDir := config.FileListDir() |
| 86 | os.MkdirAll(dumpDir, 0777) |
| 87 | |
| 88 | androidMks := f.FindFirstNamedAt(".", "Android.mk") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame^] | 89 | err := dumpListToFile(ctx, config, androidMks, filepath.Join(dumpDir, "Android.mk.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 90 | if err != nil { |
| 91 | ctx.Fatalf("Could not export module list: %v", err) |
| 92 | } |
| 93 | |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 94 | androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk") |
| 95 | androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...) |
| 96 | androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame^] | 97 | err = dumpListToFile(ctx, config, androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list")) |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame] | 98 | if err != nil { |
| 99 | ctx.Fatalf("Could not export product list: %v", err) |
| 100 | } |
| 101 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 102 | cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame^] | 103 | err = dumpListToFile(ctx, config, cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 104 | if err != nil { |
| 105 | ctx.Fatalf("Could not export module list: %v", err) |
| 106 | } |
| 107 | |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 108 | owners := f.FindNamedAt(".", "OWNERS") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame^] | 109 | err = dumpListToFile(ctx, config, owners, filepath.Join(dumpDir, "OWNERS.list")) |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 110 | if err != nil { |
| 111 | ctx.Fatalf("Could not find OWNERS: %v", err) |
| 112 | } |
| 113 | |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 114 | testMappings := f.FindNamedAt(".", "TEST_MAPPING") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame^] | 115 | err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list")) |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 116 | if err != nil { |
Simran Basi | df98cd7 | 2018-09-06 12:23:28 -0700 | [diff] [blame] | 117 | ctx.Fatalf("Could not find TEST_MAPPING: %v", err) |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Jeff Gaston | 29e959d | 2017-12-07 12:38:53 -0800 | [diff] [blame] | 120 | androidBps := f.FindNamedAt(".", "Android.bp") |
| 121 | androidBps = append(androidBps, f.FindNamedAt("build/blueprint", "Blueprints")...) |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 122 | if len(androidBps) == 0 { |
| 123 | ctx.Fatalf("No Android.bp found") |
| 124 | } |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame^] | 125 | err = dumpListToFile(ctx, config, androidBps, filepath.Join(dumpDir, "Android.bp.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 126 | if err != nil { |
| 127 | ctx.Fatalf("Could not find modules: %v", err) |
| 128 | } |
| 129 | } |
| 130 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame^] | 131 | func dumpListToFile(ctx Context, config Config, list []string, filePath string) (err error) { |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 132 | desiredText := strings.Join(list, "\n") |
| 133 | desiredBytes := []byte(desiredText) |
| 134 | actualBytes, readErr := ioutil.ReadFile(filePath) |
| 135 | if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) { |
| 136 | err = ioutil.WriteFile(filePath, desiredBytes, 0777) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame^] | 137 | if err != nil { |
| 138 | return err |
| 139 | } |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 140 | } |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame^] | 141 | |
| 142 | distFile(ctx, config, filePath, "module_paths") |
| 143 | |
| 144 | return nil |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 145 | } |