blob: d0bcf40600ef196459392fc7e60857008967cfc0 [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 (
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070018 "bytes"
19 "io/ioutil"
20 "os"
21 "path/filepath"
22 "strings"
Nan Zhang17f27672018-12-12 16:01:49 -080023
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020024 "android/soong/finder"
25 "android/soong/finder/fs"
26 "android/soong/ui/logger"
27
Nan Zhang17f27672018-12-12 16:01:49 -080028 "android/soong/ui/metrics"
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070029)
30
Jingwen Chen096a3bf2020-11-17 04:53:22 -050031// 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 Gastonb64fc1c2017-08-04 12:30:12 -070035
36// NewSourceFinder returns a new Finder configured to search for source files.
37// Callers of NewSourceFinder should call <f.Shutdown()> when done
38func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) {
Nan Zhang17f27672018-12-12 16:01:49 -080039 ctx.BeginTrace(metrics.RunSetupTool, "find modules")
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070040 defer ctx.EndTrace()
41
Jingwen Chen096a3bf2020-11-17 04:53:22 -050042 // Set up the working directory for the Finder.
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070043 dir, err := os.Getwd()
44 if err != nil {
45 ctx.Fatalf("No working directory for module-finder: %v", err.Error())
46 }
Jeff Gaston02ae4de2017-12-06 17:48:39 -080047 filesystem := fs.OsFs
48
Jingwen Chen096a3bf2020-11-17 04:53:22 -050049 // .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 Gaston02ae4de2017-12-06 17:48:39 -080054 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 Chen096a3bf2020-11-17 04:53:22 -050063 // Set up configuration parameters for the Finder cache.
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070064 cacheParams := finder.CacheParams{
65 WorkingDirectory: dir,
Spandan Das394aa322022-11-03 17:02:10 +000066 RootDirs: androidBpSearchDirs(config),
Lukacs T. Berkie3487c82022-05-02 10:13:19 +020067 FollowSymlinks: config.environ.IsEnvTrue("ALLOW_BP_UNDER_SYMLINKS"),
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070068 ExcludeDirs: []string{".git", ".repo"},
Jeff Gaston02ae4de2017-12-06 17:48:39 -080069 PruneFiles: pruneFiles,
Dan Willemsen567851c2018-05-01 22:43:52 -070070 IncludeFiles: []string{
Jingwen Chen096a3bf2020-11-17 04:53:22 -050071 // Kati build definitions.
Dan Willemsen567851c2018-05-01 22:43:52 -070072 "Android.mk",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050073 // Product configuration files.
Dan Willemsen567851c2018-05-01 22:43:52 -070074 "AndroidProducts.mk",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050075 // General Soong build definitions, using the Blueprint syntax.
Dan Willemsen567851c2018-05-01 22:43:52 -070076 "Android.bp",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050077 // Kati clean definitions.
Dan Willemsen567851c2018-05-01 22:43:52 -070078 "CleanSpec.mk",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050079 // Ownership definition.
Simran Basidf98cd72018-09-06 12:23:28 -070080 "OWNERS",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050081 // Test configuration for modules in directories that contain this
82 // file.
Dan Willemsen567851c2018-05-01 22:43:52 -070083 "TEST_MAPPING",
Wei Li2c9e8d62023-05-05 01:07:15 -070084 // METADATA file of packages
85 "METADATA",
Dan Willemsen567851c2018-05-01 22:43:52 -070086 },
Colin Cross8d411ff2023-12-07 10:31:24 -080087 // .mk files for product/board configuration.
88 IncludeSuffixes: []string{".mk"},
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070089 }
90 dumpDir := config.FileListDir()
Jeff Gaston02ae4de2017-12-06 17:48:39 -080091 f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard),
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070092 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 Das394aa322022-11-03 17:02:10 +000099func 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 Faust8d47c482022-01-26 14:27:44 -0800108func 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 Gastonb64fc1c2017-08-04 12:30:12 -0700121// FindSources searches for source files known to <f> and writes them to the filesystem for
122// use later.
123func 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 Chen096a3bf2020-11-17 04:53:22 -0500129 // Stop searching a subdirectory recursively after finding an Android.mk.
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700130 androidMks := f.FindFirstNamedAt(".", "Android.mk")
Colin Cross8ba7d472020-06-25 11:27:52 -0700131 err := dumpListToFile(ctx, config, androidMks, filepath.Join(dumpDir, "Android.mk.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700132 if err != nil {
133 ctx.Fatalf("Could not export module list: %v", err)
134 }
135
Chris Parsons53f68ae2022-03-03 12:01:40 -0500136 // Gate collecting/reporting mk metrics on builds that specifically request
137 // it, as identifying the total number of mk files adds 4-5ms onto null
138 // builds.
139 if config.reportMkMetrics {
140 androidMksTotal := f.FindNamedAt(".", "Android.mk")
141
142 ctx.Metrics.SetToplevelMakefiles(len(androidMks))
143 ctx.Metrics.SetTotalMakefiles(len(androidMksTotal))
144 ctx.Metrics.DumpMkMetrics(config.MkMetrics())
145 }
146
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500147 // Stop searching a subdirectory recursively after finding a CleanSpec.mk.
148 cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk")
149 err = dumpListToFile(ctx, config, cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list"))
150 if err != nil {
151 ctx.Fatalf("Could not export module list: %v", err)
152 }
153
154 // Only consider AndroidProducts.mk in device/, vendor/ and product/, recursively in these directories.
Dan Willemsen567851c2018-05-01 22:43:52 -0700155 androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk")
156 androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...)
157 androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...)
Colin Cross8ba7d472020-06-25 11:27:52 -0700158 err = dumpListToFile(ctx, config, androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list"))
Dan Willemsen567851c2018-05-01 22:43:52 -0700159 if err != nil {
160 ctx.Fatalf("Could not export product list: %v", err)
161 }
162
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500163 // Recursively look for all OWNERS files.
Simran Basidf98cd72018-09-06 12:23:28 -0700164 owners := f.FindNamedAt(".", "OWNERS")
Colin Cross8ba7d472020-06-25 11:27:52 -0700165 err = dumpListToFile(ctx, config, owners, filepath.Join(dumpDir, "OWNERS.list"))
Simran Basidf98cd72018-09-06 12:23:28 -0700166 if err != nil {
167 ctx.Fatalf("Could not find OWNERS: %v", err)
168 }
169
Wei Li2c9e8d62023-05-05 01:07:15 -0700170 // Recursively look for all METADATA files.
171 metadataFiles := f.FindNamedAt(".", "METADATA")
172 err = dumpListToFile(ctx, config, metadataFiles, filepath.Join(dumpDir, "METADATA.list"))
173 if err != nil {
174 ctx.Fatalf("Could not find METADATA: %v", err)
175 }
176
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500177 // Recursively look for all TEST_MAPPING files.
Dan Shicc3d9b32017-11-22 15:35:09 -0800178 testMappings := f.FindNamedAt(".", "TEST_MAPPING")
Colin Cross8ba7d472020-06-25 11:27:52 -0700179 err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list"))
Dan Shicc3d9b32017-11-22 15:35:09 -0800180 if err != nil {
Simran Basidf98cd72018-09-06 12:23:28 -0700181 ctx.Fatalf("Could not find TEST_MAPPING: %v", err)
Dan Shicc3d9b32017-11-22 15:35:09 -0800182 }
183
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500184 // Recursively look for all Android.bp files
Jeff Gaston29e959d2017-12-07 12:38:53 -0800185 androidBps := f.FindNamedAt(".", "Android.bp")
Jeff Gaston02ae4de2017-12-06 17:48:39 -0800186 if len(androidBps) == 0 {
187 ctx.Fatalf("No Android.bp found")
188 }
Colin Cross8ba7d472020-06-25 11:27:52 -0700189 err = dumpListToFile(ctx, config, androidBps, filepath.Join(dumpDir, "Android.bp.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700190 if err != nil {
191 ctx.Fatalf("Could not find modules: %v", err)
192 }
Colin Cross96e5e412020-07-06 17:13:43 -0700193
Cole Faust8d47c482022-01-26 14:27:44 -0800194 // Recursively look for all product/board config files.
195 configurationFiles := f.FindMatching(".", findProductAndBoardConfigFiles)
196 err = dumpListToFile(ctx, config, configurationFiles, filepath.Join(dumpDir, "configuration.list"))
197 if err != nil {
198 ctx.Fatalf("Could not export product/board configuration list: %v", err)
199 }
200
Colin Cross96e5e412020-07-06 17:13:43 -0700201 if config.Dist() {
202 f.WaitForDbDump()
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500203 // Dist the files.db plain text database.
Colin Cross96e5e412020-07-06 17:13:43 -0700204 distFile(ctx, config, f.DbPath, "module_paths")
205 }
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700206}
207
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500208// Write the .list files to disk.
Colin Cross8ba7d472020-06-25 11:27:52 -0700209func dumpListToFile(ctx Context, config Config, list []string, filePath string) (err error) {
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700210 desiredText := strings.Join(list, "\n")
211 desiredBytes := []byte(desiredText)
212 actualBytes, readErr := ioutil.ReadFile(filePath)
213 if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) {
214 err = ioutil.WriteFile(filePath, desiredBytes, 0777)
Colin Cross8ba7d472020-06-25 11:27:52 -0700215 if err != nil {
216 return err
217 }
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700218 }
Colin Cross8ba7d472020-06-25 11:27:52 -0700219
220 distFile(ctx, config, filePath, "module_paths")
221
222 return nil
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700223}