blob: 6dc35a0bfa273aaec687a80ca8460fcfcb2a0203 [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"
26)
27
28// This file provides an interface to the Finder for use in Soong UI
29// This file stores configuration information about which files to find
30
31// NewSourceFinder returns a new Finder configured to search for source files.
32// Callers of NewSourceFinder should call <f.Shutdown()> when done
33func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) {
34 ctx.BeginTrace("find modules")
35 defer ctx.EndTrace()
36
37 dir, err := os.Getwd()
38 if err != nil {
39 ctx.Fatalf("No working directory for module-finder: %v", err.Error())
40 }
Jeff Gaston02ae4de2017-12-06 17:48:39 -080041 filesystem := fs.OsFs
42
43 // if the root dir is ignored, then the subsequent error messages are very confusing,
44 // so check for that upfront
45 pruneFiles := []string{".out-dir", ".find-ignore"}
46 for _, name := range pruneFiles {
47 prunePath := filepath.Join(dir, name)
48 _, statErr := filesystem.Lstat(prunePath)
49 if statErr == nil {
50 ctx.Fatalf("%v must not exist", prunePath)
51 }
52 }
53
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070054 cacheParams := finder.CacheParams{
55 WorkingDirectory: dir,
56 RootDirs: []string{"."},
57 ExcludeDirs: []string{".git", ".repo"},
Jeff Gaston02ae4de2017-12-06 17:48:39 -080058 PruneFiles: pruneFiles,
Dan Willemsen567851c2018-05-01 22:43:52 -070059 IncludeFiles: []string{
60 "Android.mk",
61 "AndroidProducts.mk",
62 "Android.bp",
63 "Blueprints",
64 "CleanSpec.mk",
65 "TEST_MAPPING",
66 },
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070067 }
68 dumpDir := config.FileListDir()
Jeff Gaston02ae4de2017-12-06 17:48:39 -080069 f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard),
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070070 filepath.Join(dumpDir, "files.db"))
71 if err != nil {
72 ctx.Fatalf("Could not create module-finder: %v", err)
73 }
74 return f
75}
76
77// FindSources searches for source files known to <f> and writes them to the filesystem for
78// use later.
79func FindSources(ctx Context, config Config, f *finder.Finder) {
80 // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder
81 // if a caller such as multiproduct_kati wants to share one Finder among several builds
82 dumpDir := config.FileListDir()
83 os.MkdirAll(dumpDir, 0777)
84
85 androidMks := f.FindFirstNamedAt(".", "Android.mk")
86 err := dumpListToFile(androidMks, filepath.Join(dumpDir, "Android.mk.list"))
87 if err != nil {
88 ctx.Fatalf("Could not export module list: %v", err)
89 }
90
Dan Willemsen567851c2018-05-01 22:43:52 -070091 androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk")
92 androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...)
93 androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...)
94 err = dumpListToFile(androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list"))
95 if err != nil {
96 ctx.Fatalf("Could not export product list: %v", err)
97 }
98
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070099 cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk")
Dan Willemsen567851c2018-05-01 22:43:52 -0700100 err = dumpListToFile(cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list"))
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700101 if err != nil {
102 ctx.Fatalf("Could not export module list: %v", err)
103 }
104
Dan Shicc3d9b32017-11-22 15:35:09 -0800105 testMappings := f.FindNamedAt(".", "TEST_MAPPING")
106 err = dumpListToFile(testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list"))
107 if err != nil {
108 ctx.Fatalf("Could not find modules: %v", err)
109 }
110
Jeff Gaston29e959d2017-12-07 12:38:53 -0800111 androidBps := f.FindNamedAt(".", "Android.bp")
112 androidBps = append(androidBps, f.FindNamedAt("build/blueprint", "Blueprints")...)
Jeff Gaston02ae4de2017-12-06 17:48:39 -0800113 if len(androidBps) == 0 {
114 ctx.Fatalf("No Android.bp found")
115 }
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700116 err = dumpListToFile(androidBps, filepath.Join(dumpDir, "Android.bp.list"))
117 if err != nil {
118 ctx.Fatalf("Could not find modules: %v", err)
119 }
120}
121
122func dumpListToFile(list []string, filePath string) (err error) {
123 desiredText := strings.Join(list, "\n")
124 desiredBytes := []byte(desiredText)
125 actualBytes, readErr := ioutil.ReadFile(filePath)
126 if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) {
127 err = ioutil.WriteFile(filePath, desiredBytes, 0777)
128 }
129 return err
130}