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" |
| 26 | ) |
| 27 | |
| 28 | // This file provides an interface to the Finder for use in Soong UI |
| 29 | // This file stores configuration information about which files to find |
| 30 | |
| 31 | // NewSourceFinder returns a new Finder configured to search for source files. |
| 32 | // Callers of NewSourceFinder should call <f.Shutdown()> when done |
| 33 | func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) { |
| 34 | ctx.BeginTrace("find modules") |
| 35 | defer ctx.EndTrace() |
| 36 | |
| 37 | dir, err := os.Getwd() |
| 38 | if err != nil { |
| 39 | ctx.Fatalf("No working directory for module-finder: %v", err.Error()) |
| 40 | } |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 41 | filesystem := fs.OsFs |
| 42 | |
| 43 | // if the root dir is ignored, then the subsequent error messages are very confusing, |
| 44 | // so check for that upfront |
| 45 | pruneFiles := []string{".out-dir", ".find-ignore"} |
| 46 | for _, name := range pruneFiles { |
| 47 | prunePath := filepath.Join(dir, name) |
| 48 | _, statErr := filesystem.Lstat(prunePath) |
| 49 | if statErr == nil { |
| 50 | ctx.Fatalf("%v must not exist", prunePath) |
| 51 | } |
| 52 | } |
| 53 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 54 | cacheParams := finder.CacheParams{ |
| 55 | WorkingDirectory: dir, |
| 56 | RootDirs: []string{"."}, |
| 57 | ExcludeDirs: []string{".git", ".repo"}, |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 58 | PruneFiles: pruneFiles, |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame^] | 59 | IncludeFiles: []string{ |
| 60 | "Android.mk", |
| 61 | "AndroidProducts.mk", |
| 62 | "Android.bp", |
| 63 | "Blueprints", |
| 64 | "CleanSpec.mk", |
| 65 | "TEST_MAPPING", |
| 66 | }, |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 67 | } |
| 68 | dumpDir := config.FileListDir() |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 69 | f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard), |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 70 | filepath.Join(dumpDir, "files.db")) |
| 71 | if err != nil { |
| 72 | ctx.Fatalf("Could not create module-finder: %v", err) |
| 73 | } |
| 74 | return f |
| 75 | } |
| 76 | |
| 77 | // FindSources searches for source files known to <f> and writes them to the filesystem for |
| 78 | // use later. |
| 79 | func FindSources(ctx Context, config Config, f *finder.Finder) { |
| 80 | // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder |
| 81 | // if a caller such as multiproduct_kati wants to share one Finder among several builds |
| 82 | dumpDir := config.FileListDir() |
| 83 | os.MkdirAll(dumpDir, 0777) |
| 84 | |
| 85 | androidMks := f.FindFirstNamedAt(".", "Android.mk") |
| 86 | err := dumpListToFile(androidMks, filepath.Join(dumpDir, "Android.mk.list")) |
| 87 | if err != nil { |
| 88 | ctx.Fatalf("Could not export module list: %v", err) |
| 89 | } |
| 90 | |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame^] | 91 | androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk") |
| 92 | androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...) |
| 93 | androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...) |
| 94 | err = dumpListToFile(androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list")) |
| 95 | if err != nil { |
| 96 | ctx.Fatalf("Could not export product list: %v", err) |
| 97 | } |
| 98 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 99 | cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk") |
Dan Willemsen | 567851c | 2018-05-01 22:43:52 -0700 | [diff] [blame^] | 100 | err = dumpListToFile(cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 101 | if err != nil { |
| 102 | ctx.Fatalf("Could not export module list: %v", err) |
| 103 | } |
| 104 | |
Dan Shi | cc3d9b3 | 2017-11-22 15:35:09 -0800 | [diff] [blame] | 105 | testMappings := f.FindNamedAt(".", "TEST_MAPPING") |
| 106 | err = dumpListToFile(testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list")) |
| 107 | if err != nil { |
| 108 | ctx.Fatalf("Could not find modules: %v", err) |
| 109 | } |
| 110 | |
Jeff Gaston | 29e959d | 2017-12-07 12:38:53 -0800 | [diff] [blame] | 111 | androidBps := f.FindNamedAt(".", "Android.bp") |
| 112 | androidBps = append(androidBps, f.FindNamedAt("build/blueprint", "Blueprints")...) |
Jeff Gaston | 02ae4de | 2017-12-06 17:48:39 -0800 | [diff] [blame] | 113 | if len(androidBps) == 0 { |
| 114 | ctx.Fatalf("No Android.bp found") |
| 115 | } |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 116 | err = dumpListToFile(androidBps, filepath.Join(dumpDir, "Android.bp.list")) |
| 117 | if err != nil { |
| 118 | ctx.Fatalf("Could not find modules: %v", err) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func dumpListToFile(list []string, filePath string) (err error) { |
| 123 | desiredText := strings.Join(list, "\n") |
| 124 | desiredBytes := []byte(desiredText) |
| 125 | actualBytes, readErr := ioutil.ReadFile(filePath) |
| 126 | if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) { |
| 127 | err = ioutil.WriteFile(filePath, desiredBytes, 0777) |
| 128 | } |
| 129 | return err |
| 130 | } |