blob: 010b3256a4b2d828f076370783a8882257fffa29 [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"
19 "android/soong/fs"
20 "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 }
41 cacheParams := finder.CacheParams{
42 WorkingDirectory: dir,
43 RootDirs: []string{"."},
44 ExcludeDirs: []string{".git", ".repo"},
45 PruneFiles: []string{".out-dir", ".find-ignore"},
Dan Shicc3d9b32017-11-22 15:35:09 -080046 IncludeFiles: []string{"Android.mk", "Android.bp", "Blueprints", "CleanSpec.mk", "TEST_MAPPING"},
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070047 }
48 dumpDir := config.FileListDir()
49 f, err = finder.New(cacheParams, fs.OsFs, logger.New(ioutil.Discard),
50 filepath.Join(dumpDir, "files.db"))
51 if err != nil {
52 ctx.Fatalf("Could not create module-finder: %v", err)
53 }
54 return f
55}
56
57// FindSources searches for source files known to <f> and writes them to the filesystem for
58// use later.
59func FindSources(ctx Context, config Config, f *finder.Finder) {
60 // note that dumpDir in FindSources may be different than dumpDir in NewSourceFinder
61 // if a caller such as multiproduct_kati wants to share one Finder among several builds
62 dumpDir := config.FileListDir()
63 os.MkdirAll(dumpDir, 0777)
64
65 androidMks := f.FindFirstNamedAt(".", "Android.mk")
66 err := dumpListToFile(androidMks, filepath.Join(dumpDir, "Android.mk.list"))
67 if err != nil {
68 ctx.Fatalf("Could not export module list: %v", err)
69 }
70
71 cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk")
72 dumpListToFile(cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list"))
73 if err != nil {
74 ctx.Fatalf("Could not export module list: %v", err)
75 }
76
Dan Shicc3d9b32017-11-22 15:35:09 -080077 testMappings := f.FindNamedAt(".", "TEST_MAPPING")
78 err = dumpListToFile(testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list"))
79 if err != nil {
80 ctx.Fatalf("Could not find modules: %v", err)
81 }
82
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070083 isBlueprintFile := func(dir finder.DirEntries) (dirs []string, files []string) {
84 files = []string{}
85 for _, file := range dir.FileNames {
86 if file == "Android.bp" || file == "Blueprints" {
87 files = append(files, file)
88 }
89 }
90
91 return dir.DirNames, files
92 }
93 androidBps := f.FindMatching(".", isBlueprintFile)
94 err = dumpListToFile(androidBps, filepath.Join(dumpDir, "Android.bp.list"))
95 if err != nil {
96 ctx.Fatalf("Could not find modules: %v", err)
97 }
98}
99
100func dumpListToFile(list []string, filePath string) (err error) {
101 desiredText := strings.Join(list, "\n")
102 desiredBytes := []byte(desiredText)
103 actualBytes, readErr := ioutil.ReadFile(filePath)
104 if readErr != nil || !bytes.Equal(desiredBytes, actualBytes) {
105 err = ioutil.WriteFile(filePath, desiredBytes, 0777)
106 }
107 return err
108}