blob: 7a856577ef50105533cfc49706eb18e4e702d5a1 [file] [log] [blame]
Jeff Gastonb64fc1c2017-08-04 12:30:12 -07001// 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
15package build
16
17import (
18 "android/soong/finder"
Colin Cross8d6395c2017-12-21 15:46:01 -080019 "android/soong/finder/fs"
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070020 "android/soong/ui/logger"
21 "bytes"
22 "io/ioutil"
23 "os"
24 "path/filepath"
25 "strings"
Nan Zhang17f27672018-12-12 16:01:49 -080026
27 "android/soong/ui/metrics"
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070028)
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
35func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) {
Nan Zhang17f27672018-12-12 16:01:49 -080036 ctx.BeginTrace(metrics.RunSetupTool, "find modules")
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070037 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 Gaston02ae4de2017-12-06 17:48:39 -080043 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 Gastonb64fc1c2017-08-04 12:30:12 -070056 cacheParams := finder.CacheParams{
57 WorkingDirectory: dir,
58 RootDirs: []string{"."},
59 ExcludeDirs: []string{".git", ".repo"},
Jeff Gaston02ae4de2017-12-06 17:48:39 -080060 PruneFiles: pruneFiles,
Dan Willemsen567851c2018-05-01 22:43:52 -070061 IncludeFiles: []string{
62 "Android.mk",
63 "AndroidProducts.mk",
64 "Android.bp",
65 "Blueprints",
Chris Parsonsa798d962020-10-12 23:44:08 -040066 "BUILD.bazel",
Dan Willemsen567851c2018-05-01 22:43:52 -070067 "CleanSpec.mk",
Simran Basidf98cd72018-09-06 12:23:28 -070068 "OWNERS",
Dan Willemsen567851c2018-05-01 22:43:52 -070069 "TEST_MAPPING",
Chris Parsonsa798d962020-10-12 23:44:08 -040070 "WORKSPACE",
Dan Willemsen567851c2018-05-01 22:43:52 -070071 },
Chris Parsonsa798d962020-10-12 23:44:08 -040072 IncludeSuffixes: []string{".bzl"},
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070073 }
74 dumpDir := config.FileListDir()
Jeff Gaston02ae4de2017-12-06 17:48:39 -080075 f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard),
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070076 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 Parsonsa798d962020-10-12 23:44:08 -040083func 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 Gastonb64fc1c2017-08-04 12:30:12 -070093// FindSources searches for source files known to <f> and writes them to the filesystem for
94// use later.
95func 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 Cross8ba7d472020-06-25 11:27:52 -0700102 err := dumpListToFile(ctx, config, androidMks, filepath.Join(dumpDir, "Android.mk.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700103 if err != nil {
104 ctx.Fatalf("Could not export module list: %v", err)
105 }
106
Dan Willemsen567851c2018-05-01 22:43:52 -0700107 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 Cross8ba7d472020-06-25 11:27:52 -0700110 err = dumpListToFile(ctx, config, androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list"))
Dan Willemsen567851c2018-05-01 22:43:52 -0700111 if err != nil {
112 ctx.Fatalf("Could not export product list: %v", err)
113 }
114
Chris Parsonsa798d962020-10-12 23:44:08 -0400115 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 Gastonb64fc1c2017-08-04 12:30:12 -0700121 cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk")
Colin Cross8ba7d472020-06-25 11:27:52 -0700122 err = dumpListToFile(ctx, config, cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700123 if err != nil {
124 ctx.Fatalf("Could not export module list: %v", err)
125 }
126
Simran Basidf98cd72018-09-06 12:23:28 -0700127 owners := f.FindNamedAt(".", "OWNERS")
Colin Cross8ba7d472020-06-25 11:27:52 -0700128 err = dumpListToFile(ctx, config, owners, filepath.Join(dumpDir, "OWNERS.list"))
Simran Basidf98cd72018-09-06 12:23:28 -0700129 if err != nil {
130 ctx.Fatalf("Could not find OWNERS: %v", err)
131 }
132
Dan Shicc3d9b32017-11-22 15:35:09 -0800133 testMappings := f.FindNamedAt(".", "TEST_MAPPING")
Colin Cross8ba7d472020-06-25 11:27:52 -0700134 err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list"))
Dan Shicc3d9b32017-11-22 15:35:09 -0800135 if err != nil {
Simran Basidf98cd72018-09-06 12:23:28 -0700136 ctx.Fatalf("Could not find TEST_MAPPING: %v", err)
Dan Shicc3d9b32017-11-22 15:35:09 -0800137 }
138
Jeff Gaston29e959d2017-12-07 12:38:53 -0800139 androidBps := f.FindNamedAt(".", "Android.bp")
140 androidBps = append(androidBps, f.FindNamedAt("build/blueprint", "Blueprints")...)
Jeff Gaston02ae4de2017-12-06 17:48:39 -0800141 if len(androidBps) == 0 {
142 ctx.Fatalf("No Android.bp found")
143 }
Colin Cross8ba7d472020-06-25 11:27:52 -0700144 err = dumpListToFile(ctx, config, androidBps, filepath.Join(dumpDir, "Android.bp.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700145 if err != nil {
146 ctx.Fatalf("Could not find modules: %v", err)
147 }
Colin Cross96e5e412020-07-06 17:13:43 -0700148
149 if config.Dist() {
150 f.WaitForDbDump()
151 distFile(ctx, config, f.DbPath, "module_paths")
152 }
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700153}
154
Colin Cross8ba7d472020-06-25 11:27:52 -0700155func dumpListToFile(ctx Context, config Config, list []string, filePath string) (err error) {
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700156 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 Cross8ba7d472020-06-25 11:27:52 -0700161 if err != nil {
162 return err
163 }
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700164 }
Colin Cross8ba7d472020-06-25 11:27:52 -0700165
166 distFile(ctx, config, filePath, "module_paths")
167
168 return nil
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700169}