blob: 09d53cc1e77cf5d22603631b5554c71b97eee113 [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
Jingwen Chen096a3bf2020-11-17 04:53:22 -050030// This file provides an interface to the Finder type for soong_ui. Finder is
31// used to recursively traverse the source tree to gather paths of files, such
32// as Android.bp or Android.mk, and store the lists/database of paths in files
33// under `$OUT_DIR/.module_paths`. This directory can also be dist'd.
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070034
35// NewSourceFinder returns a new Finder configured to search for source files.
36// Callers of NewSourceFinder should call <f.Shutdown()> when done
37func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) {
Nan Zhang17f27672018-12-12 16:01:49 -080038 ctx.BeginTrace(metrics.RunSetupTool, "find modules")
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070039 defer ctx.EndTrace()
40
Jingwen Chen096a3bf2020-11-17 04:53:22 -050041 // Set up the working directory for the Finder.
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070042 dir, err := os.Getwd()
43 if err != nil {
44 ctx.Fatalf("No working directory for module-finder: %v", err.Error())
45 }
Jeff Gaston02ae4de2017-12-06 17:48:39 -080046 filesystem := fs.OsFs
47
Jingwen Chen096a3bf2020-11-17 04:53:22 -050048 // .out-dir and .find-ignore are markers for Finder to ignore siblings and
49 // subdirectories of the directory Finder finds them in, hence stopping the
50 // search recursively down those branches. It's possible that these files
51 // are in the root directory, and if they are, then the subsequent error
52 // messages are very confusing, so check for that here.
Jeff Gaston02ae4de2017-12-06 17:48:39 -080053 pruneFiles := []string{".out-dir", ".find-ignore"}
54 for _, name := range pruneFiles {
55 prunePath := filepath.Join(dir, name)
56 _, statErr := filesystem.Lstat(prunePath)
57 if statErr == nil {
58 ctx.Fatalf("%v must not exist", prunePath)
59 }
60 }
61
Jingwen Chen096a3bf2020-11-17 04:53:22 -050062 // Set up configuration parameters for the Finder cache.
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070063 cacheParams := finder.CacheParams{
64 WorkingDirectory: dir,
65 RootDirs: []string{"."},
66 ExcludeDirs: []string{".git", ".repo"},
Jeff Gaston02ae4de2017-12-06 17:48:39 -080067 PruneFiles: pruneFiles,
Dan Willemsen567851c2018-05-01 22:43:52 -070068 IncludeFiles: []string{
Jingwen Chen096a3bf2020-11-17 04:53:22 -050069 // Kati build definitions.
Dan Willemsen567851c2018-05-01 22:43:52 -070070 "Android.mk",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050071 // Product configuration files.
Dan Willemsen567851c2018-05-01 22:43:52 -070072 "AndroidProducts.mk",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050073 // General Soong build definitions, using the Blueprint syntax.
Dan Willemsen567851c2018-05-01 22:43:52 -070074 "Android.bp",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050075 // build/blueprint build definitions, using the Blueprint syntax.
Dan Willemsen567851c2018-05-01 22:43:52 -070076 "Blueprints",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050077 // Bazel build definitions.
Chris Parsonsa798d962020-10-12 23:44:08 -040078 "BUILD.bazel",
Rupert Shuttleworthc29cace2021-05-18 07:39:37 -040079 // Bazel build definitions.
80 "BUILD",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050081 // Kati clean definitions.
Dan Willemsen567851c2018-05-01 22:43:52 -070082 "CleanSpec.mk",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050083 // Ownership definition.
Simran Basidf98cd72018-09-06 12:23:28 -070084 "OWNERS",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050085 // Test configuration for modules in directories that contain this
86 // file.
Dan Willemsen567851c2018-05-01 22:43:52 -070087 "TEST_MAPPING",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050088 // Bazel top-level file to mark a directory as a Bazel workspace.
Chris Parsonsa798d962020-10-12 23:44:08 -040089 "WORKSPACE",
Dan Willemsen567851c2018-05-01 22:43:52 -070090 },
Jingwen Chen096a3bf2020-11-17 04:53:22 -050091 // Bazel Starlark configuration files.
Chris Parsonsa798d962020-10-12 23:44:08 -040092 IncludeSuffixes: []string{".bzl"},
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070093 }
94 dumpDir := config.FileListDir()
Jeff Gaston02ae4de2017-12-06 17:48:39 -080095 f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard),
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070096 filepath.Join(dumpDir, "files.db"))
97 if err != nil {
98 ctx.Fatalf("Could not create module-finder: %v", err)
99 }
100 return f
101}
102
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500103// Finds the list of Bazel-related files (BUILD, WORKSPACE and Starlark) in the tree.
Chris Parsonsa798d962020-10-12 23:44:08 -0400104func findBazelFiles(entries finder.DirEntries) (dirNames []string, fileNames []string) {
105 matches := []string{}
106 for _, foundName := range entries.FileNames {
Rupert Shuttleworthc29cace2021-05-18 07:39:37 -0400107 if foundName == "BUILD.bazel" || foundName == "BUILD" || foundName == "WORKSPACE" || strings.HasSuffix(foundName, ".bzl") {
Chris Parsonsa798d962020-10-12 23:44:08 -0400108 matches = append(matches, foundName)
109 }
110 }
111 return entries.DirNames, matches
112}
113
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700114// FindSources searches for source files known to <f> and writes them to the filesystem for
115// use later.
116func FindSources(ctx Context, config Config, f *finder.Finder) {
117 // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder
118 // if a caller such as multiproduct_kati wants to share one Finder among several builds
119 dumpDir := config.FileListDir()
120 os.MkdirAll(dumpDir, 0777)
121
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500122 // Stop searching a subdirectory recursively after finding an Android.mk.
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700123 androidMks := f.FindFirstNamedAt(".", "Android.mk")
Colin Cross8ba7d472020-06-25 11:27:52 -0700124 err := dumpListToFile(ctx, config, androidMks, filepath.Join(dumpDir, "Android.mk.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700125 if err != nil {
126 ctx.Fatalf("Could not export module list: %v", err)
127 }
128
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500129 // Stop searching a subdirectory recursively after finding a CleanSpec.mk.
130 cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk")
131 err = dumpListToFile(ctx, config, cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list"))
132 if err != nil {
133 ctx.Fatalf("Could not export module list: %v", err)
134 }
135
136 // Only consider AndroidProducts.mk in device/, vendor/ and product/, recursively in these directories.
Dan Willemsen567851c2018-05-01 22:43:52 -0700137 androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk")
138 androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...)
139 androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...)
Colin Cross8ba7d472020-06-25 11:27:52 -0700140 err = dumpListToFile(ctx, config, androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list"))
Dan Willemsen567851c2018-05-01 22:43:52 -0700141 if err != nil {
142 ctx.Fatalf("Could not export product list: %v", err)
143 }
144
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500145 // Recursively look for all Bazel related files.
Chris Parsonsa798d962020-10-12 23:44:08 -0400146 bazelFiles := f.FindMatching(".", findBazelFiles)
147 err = dumpListToFile(ctx, config, bazelFiles, filepath.Join(dumpDir, "bazel.list"))
148 if err != nil {
149 ctx.Fatalf("Could not export bazel BUILD list: %v", err)
150 }
151
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500152 // Recursively look for all OWNERS files.
Simran Basidf98cd72018-09-06 12:23:28 -0700153 owners := f.FindNamedAt(".", "OWNERS")
Colin Cross8ba7d472020-06-25 11:27:52 -0700154 err = dumpListToFile(ctx, config, owners, filepath.Join(dumpDir, "OWNERS.list"))
Simran Basidf98cd72018-09-06 12:23:28 -0700155 if err != nil {
156 ctx.Fatalf("Could not find OWNERS: %v", err)
157 }
158
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500159 // Recursively look for all TEST_MAPPING files.
Dan Shicc3d9b32017-11-22 15:35:09 -0800160 testMappings := f.FindNamedAt(".", "TEST_MAPPING")
Colin Cross8ba7d472020-06-25 11:27:52 -0700161 err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list"))
Dan Shicc3d9b32017-11-22 15:35:09 -0800162 if err != nil {
Simran Basidf98cd72018-09-06 12:23:28 -0700163 ctx.Fatalf("Could not find TEST_MAPPING: %v", err)
Dan Shicc3d9b32017-11-22 15:35:09 -0800164 }
165
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500166 // Recursively look for all Android.bp files
Jeff Gaston29e959d2017-12-07 12:38:53 -0800167 androidBps := f.FindNamedAt(".", "Android.bp")
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500168 // The files are named "Blueprints" only in the build/blueprint directory.
Jeff Gaston29e959d2017-12-07 12:38:53 -0800169 androidBps = append(androidBps, f.FindNamedAt("build/blueprint", "Blueprints")...)
Jeff Gaston02ae4de2017-12-06 17:48:39 -0800170 if len(androidBps) == 0 {
171 ctx.Fatalf("No Android.bp found")
172 }
Colin Cross8ba7d472020-06-25 11:27:52 -0700173 err = dumpListToFile(ctx, config, androidBps, filepath.Join(dumpDir, "Android.bp.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700174 if err != nil {
175 ctx.Fatalf("Could not find modules: %v", err)
176 }
Colin Cross96e5e412020-07-06 17:13:43 -0700177
178 if config.Dist() {
179 f.WaitForDbDump()
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500180 // Dist the files.db plain text database.
Colin Cross96e5e412020-07-06 17:13:43 -0700181 distFile(ctx, config, f.DbPath, "module_paths")
182 }
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700183}
184
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500185// Write the .list files to disk.
Colin Cross8ba7d472020-06-25 11:27:52 -0700186func dumpListToFile(ctx Context, config Config, list []string, filePath string) (err error) {
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700187 desiredText := strings.Join(list, "\n")
188 desiredBytes := []byte(desiredText)
189 actualBytes, readErr := ioutil.ReadFile(filePath)
190 if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) {
191 err = ioutil.WriteFile(filePath, desiredBytes, 0777)
Colin Cross8ba7d472020-06-25 11:27:52 -0700192 if err != nil {
193 return err
194 }
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700195 }
Colin Cross8ba7d472020-06-25 11:27:52 -0700196
197 distFile(ctx, config, filePath, "module_paths")
198
199 return nil
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700200}