blob: 8f74969fbe956c4d3391b31592c256a6b1003894 [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,
66 RootDirs: []string{"."},
67 ExcludeDirs: []string{".git", ".repo"},
Jeff Gaston02ae4de2017-12-06 17:48:39 -080068 PruneFiles: pruneFiles,
Dan Willemsen567851c2018-05-01 22:43:52 -070069 IncludeFiles: []string{
Jingwen Chen096a3bf2020-11-17 04:53:22 -050070 // Kati build definitions.
Dan Willemsen567851c2018-05-01 22:43:52 -070071 "Android.mk",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050072 // Product configuration files.
Dan Willemsen567851c2018-05-01 22:43:52 -070073 "AndroidProducts.mk",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050074 // General Soong build definitions, using the Blueprint syntax.
Dan Willemsen567851c2018-05-01 22:43:52 -070075 "Android.bp",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050076 // Bazel build definitions.
Chris Parsonsa798d962020-10-12 23:44:08 -040077 "BUILD.bazel",
Rupert Shuttleworthc29cace2021-05-18 07:39:37 -040078 // Bazel build definitions.
79 "BUILD",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050080 // Kati clean definitions.
Dan Willemsen567851c2018-05-01 22:43:52 -070081 "CleanSpec.mk",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050082 // Ownership definition.
Simran Basidf98cd72018-09-06 12:23:28 -070083 "OWNERS",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050084 // Test configuration for modules in directories that contain this
85 // file.
Dan Willemsen567851c2018-05-01 22:43:52 -070086 "TEST_MAPPING",
Jingwen Chen096a3bf2020-11-17 04:53:22 -050087 // Bazel top-level file to mark a directory as a Bazel workspace.
Chris Parsonsa798d962020-10-12 23:44:08 -040088 "WORKSPACE",
Dan Willemsen567851c2018-05-01 22:43:52 -070089 },
Jingwen Chen096a3bf2020-11-17 04:53:22 -050090 // Bazel Starlark configuration files.
Chris Parsonsa798d962020-10-12 23:44:08 -040091 IncludeSuffixes: []string{".bzl"},
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070092 }
93 dumpDir := config.FileListDir()
Jeff Gaston02ae4de2017-12-06 17:48:39 -080094 f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard),
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070095 filepath.Join(dumpDir, "files.db"))
96 if err != nil {
97 ctx.Fatalf("Could not create module-finder: %v", err)
98 }
99 return f
100}
101
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500102// Finds the list of Bazel-related files (BUILD, WORKSPACE and Starlark) in the tree.
Chris Parsonsa798d962020-10-12 23:44:08 -0400103func findBazelFiles(entries finder.DirEntries) (dirNames []string, fileNames []string) {
104 matches := []string{}
105 for _, foundName := range entries.FileNames {
Rupert Shuttleworthc29cace2021-05-18 07:39:37 -0400106 if foundName == "BUILD.bazel" || foundName == "BUILD" || foundName == "WORKSPACE" || strings.HasSuffix(foundName, ".bzl") {
Chris Parsonsa798d962020-10-12 23:44:08 -0400107 matches = append(matches, foundName)
108 }
109 }
110 return entries.DirNames, matches
111}
112
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700113// FindSources searches for source files known to <f> and writes them to the filesystem for
114// use later.
115func FindSources(ctx Context, config Config, f *finder.Finder) {
116 // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder
117 // if a caller such as multiproduct_kati wants to share one Finder among several builds
118 dumpDir := config.FileListDir()
119 os.MkdirAll(dumpDir, 0777)
120
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500121 // Stop searching a subdirectory recursively after finding an Android.mk.
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700122 androidMks := f.FindFirstNamedAt(".", "Android.mk")
Colin Cross8ba7d472020-06-25 11:27:52 -0700123 err := dumpListToFile(ctx, config, androidMks, filepath.Join(dumpDir, "Android.mk.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700124 if err != nil {
125 ctx.Fatalf("Could not export module list: %v", err)
126 }
127
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500128 // Stop searching a subdirectory recursively after finding a CleanSpec.mk.
129 cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk")
130 err = dumpListToFile(ctx, config, cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list"))
131 if err != nil {
132 ctx.Fatalf("Could not export module list: %v", err)
133 }
134
135 // Only consider AndroidProducts.mk in device/, vendor/ and product/, recursively in these directories.
Dan Willemsen567851c2018-05-01 22:43:52 -0700136 androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk")
137 androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...)
138 androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...)
Colin Cross8ba7d472020-06-25 11:27:52 -0700139 err = dumpListToFile(ctx, config, androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list"))
Dan Willemsen567851c2018-05-01 22:43:52 -0700140 if err != nil {
141 ctx.Fatalf("Could not export product list: %v", err)
142 }
143
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500144 // Recursively look for all Bazel related files.
Chris Parsonsa798d962020-10-12 23:44:08 -0400145 bazelFiles := f.FindMatching(".", findBazelFiles)
146 err = dumpListToFile(ctx, config, bazelFiles, filepath.Join(dumpDir, "bazel.list"))
147 if err != nil {
148 ctx.Fatalf("Could not export bazel BUILD list: %v", err)
149 }
150
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500151 // Recursively look for all OWNERS files.
Simran Basidf98cd72018-09-06 12:23:28 -0700152 owners := f.FindNamedAt(".", "OWNERS")
Colin Cross8ba7d472020-06-25 11:27:52 -0700153 err = dumpListToFile(ctx, config, owners, filepath.Join(dumpDir, "OWNERS.list"))
Simran Basidf98cd72018-09-06 12:23:28 -0700154 if err != nil {
155 ctx.Fatalf("Could not find OWNERS: %v", err)
156 }
157
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500158 // Recursively look for all TEST_MAPPING files.
Dan Shicc3d9b32017-11-22 15:35:09 -0800159 testMappings := f.FindNamedAt(".", "TEST_MAPPING")
Colin Cross8ba7d472020-06-25 11:27:52 -0700160 err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list"))
Dan Shicc3d9b32017-11-22 15:35:09 -0800161 if err != nil {
Simran Basidf98cd72018-09-06 12:23:28 -0700162 ctx.Fatalf("Could not find TEST_MAPPING: %v", err)
Dan Shicc3d9b32017-11-22 15:35:09 -0800163 }
164
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500165 // Recursively look for all Android.bp files
Jeff Gaston29e959d2017-12-07 12:38:53 -0800166 androidBps := f.FindNamedAt(".", "Android.bp")
Jeff Gaston02ae4de2017-12-06 17:48:39 -0800167 if len(androidBps) == 0 {
168 ctx.Fatalf("No Android.bp found")
169 }
Colin Cross8ba7d472020-06-25 11:27:52 -0700170 err = dumpListToFile(ctx, config, androidBps, filepath.Join(dumpDir, "Android.bp.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700171 if err != nil {
172 ctx.Fatalf("Could not find modules: %v", err)
173 }
Colin Cross96e5e412020-07-06 17:13:43 -0700174
175 if config.Dist() {
176 f.WaitForDbDump()
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500177 // Dist the files.db plain text database.
Colin Cross96e5e412020-07-06 17:13:43 -0700178 distFile(ctx, config, f.DbPath, "module_paths")
179 }
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700180}
181
Jingwen Chen096a3bf2020-11-17 04:53:22 -0500182// Write the .list files to disk.
Colin Cross8ba7d472020-06-25 11:27:52 -0700183func dumpListToFile(ctx Context, config Config, list []string, filePath string) (err error) {
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700184 desiredText := strings.Join(list, "\n")
185 desiredBytes := []byte(desiredText)
186 actualBytes, readErr := ioutil.ReadFile(filePath)
187 if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) {
188 err = ioutil.WriteFile(filePath, desiredBytes, 0777)
Colin Cross8ba7d472020-06-25 11:27:52 -0700189 if err != nil {
190 return err
191 }
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700192 }
Colin Cross8ba7d472020-06-25 11:27:52 -0700193
194 distFile(ctx, config, filePath, "module_paths")
195
196 return nil
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700197}