blob: 5a01cf521e7f935a1b98d6c51efb0fe8a566efe7 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 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 common
16
17import (
18 "fmt"
19 "path/filepath"
20
Colin Cross70b40592015-03-23 12:57:34 -070021 "github.com/google/blueprint"
22 "github.com/google/blueprint/bootstrap"
Colin Cross3f40fa42015-01-30 17:27:36 -080023
24 "android/soong/glob"
25)
26
27// This file supports globbing source files in Blueprints files.
28//
29// The build.ninja file needs to be regenerated any time a file matching the glob is added
30// or removed. The naive solution is to have the build.ninja file depend on all the
31// traversed directories, but this will cause the regeneration step to run every time a
32// non-matching file is added to a traversed directory, including backup files created by
33// editors.
34//
35// The solution implemented here optimizes out regenerations when the directory modifications
36// don't match the glob by having the build.ninja file depend on an intermedate file that
37// is only updated when a file matching the glob is added or removed. The intermediate file
38// depends on the traversed directories via a depfile. The depfile is used to avoid build
39// errors if a directory is deleted - a direct dependency on the deleted directory would result
40// in a build failure with a "missing and no known rule to make it" error.
41
42var (
43 globCmd = filepath.Join(bootstrap.BinDir, "soong_glob")
44
45 // globRule rule traverses directories to produce a list of files that match $glob
46 // and writes it to $out if it has changed, and writes the directories to $out.d
47 globRule = pctx.StaticRule("globRule",
48 blueprint.RuleParams{
Colin Cross3e8ec072015-03-31 16:40:23 -070049 Command: fmt.Sprintf(`%s -o $out $excludes "$glob"`, globCmd),
Colin Cross3f40fa42015-01-30 17:27:36 -080050 Description: "glob $glob",
51
Colin Cross6a114ca2015-04-24 15:14:48 -070052 Restat: true,
53 Deps: blueprint.DepsGCC,
54 Depfile: "$out.d",
Colin Cross3f40fa42015-01-30 17:27:36 -080055 },
Colin Cross3e8ec072015-03-31 16:40:23 -070056 "glob", "excludes")
Colin Cross3f40fa42015-01-30 17:27:36 -080057)
58
59func hasGlob(in []string) bool {
60 for _, s := range in {
61 if glob.IsGlob(s) {
62 return true
63 }
64 }
65
66 return false
67}
68
Colin Cross8f101b42015-06-17 15:09:06 -070069// The subset of ModuleContext and SingletonContext needed by Glob
70type globContext interface {
71 Build(pctx *blueprint.PackageContext, params blueprint.BuildParams)
72 AddNinjaFileDeps(deps ...string)
Colin Cross3f40fa42015-01-30 17:27:36 -080073}
74
Colin Cross8f101b42015-06-17 15:09:06 -070075func Glob(ctx globContext, outDir string, globPattern string, excludes []string) ([]string, error) {
76 fileListFile := filepath.Join(outDir, "glob", globToString(globPattern))
Colin Cross3f40fa42015-01-30 17:27:36 -080077 depFile := fileListFile + ".d"
78
79 // Get a globbed file list, and write out fileListFile and depFile
Colin Cross3e8ec072015-03-31 16:40:23 -070080 files, err := glob.GlobWithDepFile(globPattern, fileListFile, depFile, excludes)
Colin Cross3f40fa42015-01-30 17:27:36 -080081 if err != nil {
Colin Cross8f101b42015-06-17 15:09:06 -070082 return nil, err
Colin Cross3f40fa42015-01-30 17:27:36 -080083 }
84
Colin Cross3e8ec072015-03-31 16:40:23 -070085 GlobRule(ctx, globPattern, excludes, fileListFile, depFile)
86
87 // Make build.ninja depend on the fileListFile
88 ctx.AddNinjaFileDeps(fileListFile)
89
Colin Cross8f101b42015-06-17 15:09:06 -070090 return files, nil
Colin Cross3e8ec072015-03-31 16:40:23 -070091}
92
Colin Cross8f101b42015-06-17 15:09:06 -070093func GlobRule(ctx globContext, globPattern string, excludes []string,
Colin Cross3e8ec072015-03-31 16:40:23 -070094 fileListFile, depFile string) {
Colin Cross3e8ec072015-03-31 16:40:23 -070095
Colin Cross3f40fa42015-01-30 17:27:36 -080096 // Create a rule to rebuild fileListFile if a directory in depFile changes. fileListFile
97 // will only be rewritten if it has changed, preventing unnecesary build.ninja regenerations.
98 ctx.Build(pctx, blueprint.BuildParams{
99 Rule: globRule,
100 Outputs: []string{fileListFile},
101 Implicits: []string{globCmd},
102 Args: map[string]string{
Colin Cross3e8ec072015-03-31 16:40:23 -0700103 "glob": globPattern,
Colin Cross9b6826f2015-04-10 15:47:33 -0700104 "excludes": JoinWithPrefixAndQuote(excludes, "-e "),
Colin Cross3f40fa42015-01-30 17:27:36 -0800105 },
106 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800107}
108
109func globToString(glob string) string {
110 ret := ""
111 for _, c := range glob {
112 if c >= 'a' && c <= 'z' ||
113 c >= 'A' && c <= 'Z' ||
114 c >= '0' && c <= '9' ||
115 c == '_' || c == '-' || c == '/' {
116 ret += string(c)
117 }
118 }
119
120 return ret
121}