blob: 6786337be1c376c34a6c1d4a07f61393ab92c038 [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",
66 "CleanSpec.mk",
Simran Basidf98cd72018-09-06 12:23:28 -070067 "OWNERS",
Dan Willemsen567851c2018-05-01 22:43:52 -070068 "TEST_MAPPING",
69 },
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070070 }
71 dumpDir := config.FileListDir()
Jeff Gaston02ae4de2017-12-06 17:48:39 -080072 f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard),
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070073 filepath.Join(dumpDir, "files.db"))
74 if err != nil {
75 ctx.Fatalf("Could not create module-finder: %v", err)
76 }
77 return f
78}
79
80// FindSources searches for source files known to <f> and writes them to the filesystem for
81// use later.
82func FindSources(ctx Context, config Config, f *finder.Finder) {
83 // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder
84 // if a caller such as multiproduct_kati wants to share one Finder among several builds
85 dumpDir := config.FileListDir()
86 os.MkdirAll(dumpDir, 0777)
87
88 androidMks := f.FindFirstNamedAt(".", "Android.mk")
Colin Cross8ba7d472020-06-25 11:27:52 -070089 err := dumpListToFile(ctx, config, androidMks, filepath.Join(dumpDir, "Android.mk.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070090 if err != nil {
91 ctx.Fatalf("Could not export module list: %v", err)
92 }
93
Dan Willemsen567851c2018-05-01 22:43:52 -070094 androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk")
95 androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...)
96 androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...)
Colin Cross8ba7d472020-06-25 11:27:52 -070097 err = dumpListToFile(ctx, config, androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list"))
Dan Willemsen567851c2018-05-01 22:43:52 -070098 if err != nil {
99 ctx.Fatalf("Could not export product list: %v", err)
100 }
101
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700102 cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk")
Colin Cross8ba7d472020-06-25 11:27:52 -0700103 err = dumpListToFile(ctx, config, cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700104 if err != nil {
105 ctx.Fatalf("Could not export module list: %v", err)
106 }
107
Simran Basidf98cd72018-09-06 12:23:28 -0700108 owners := f.FindNamedAt(".", "OWNERS")
Colin Cross8ba7d472020-06-25 11:27:52 -0700109 err = dumpListToFile(ctx, config, owners, filepath.Join(dumpDir, "OWNERS.list"))
Simran Basidf98cd72018-09-06 12:23:28 -0700110 if err != nil {
111 ctx.Fatalf("Could not find OWNERS: %v", err)
112 }
113
Dan Shicc3d9b32017-11-22 15:35:09 -0800114 testMappings := f.FindNamedAt(".", "TEST_MAPPING")
Colin Cross8ba7d472020-06-25 11:27:52 -0700115 err = dumpListToFile(ctx, config, testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list"))
Dan Shicc3d9b32017-11-22 15:35:09 -0800116 if err != nil {
Simran Basidf98cd72018-09-06 12:23:28 -0700117 ctx.Fatalf("Could not find TEST_MAPPING: %v", err)
Dan Shicc3d9b32017-11-22 15:35:09 -0800118 }
119
Jeff Gaston29e959d2017-12-07 12:38:53 -0800120 androidBps := f.FindNamedAt(".", "Android.bp")
121 androidBps = append(androidBps, f.FindNamedAt("build/blueprint", "Blueprints")...)
Jeff Gaston02ae4de2017-12-06 17:48:39 -0800122 if len(androidBps) == 0 {
123 ctx.Fatalf("No Android.bp found")
124 }
Colin Cross8ba7d472020-06-25 11:27:52 -0700125 err = dumpListToFile(ctx, config, androidBps, filepath.Join(dumpDir, "Android.bp.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700126 if err != nil {
127 ctx.Fatalf("Could not find modules: %v", err)
128 }
129}
130
Colin Cross8ba7d472020-06-25 11:27:52 -0700131func dumpListToFile(ctx Context, config Config, list []string, filePath string) (err error) {
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700132 desiredText := strings.Join(list, "\n")
133 desiredBytes := []byte(desiredText)
134 actualBytes, readErr := ioutil.ReadFile(filePath)
135 if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) {
136 err = ioutil.WriteFile(filePath, desiredBytes, 0777)
Colin Cross8ba7d472020-06-25 11:27:52 -0700137 if err != nil {
138 return err
139 }
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700140 }
Colin Cross8ba7d472020-06-25 11:27:52 -0700141
142 distFile(ctx, config, filePath, "module_paths")
143
144 return nil
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700145}