1// 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 android
16
17import (
18	"github.com/google/blueprint"
19	"github.com/google/blueprint/bootstrap"
20)
21
22var (
23	pctx = NewPackageContext("android/soong/android")
24
25	cpPreserveSymlinks = pctx.VariableConfigMethod("cpPreserveSymlinks",
26		Config.CpPreserveSymlinksFlags)
27
28	// A phony rule that is not the built-in Ninja phony rule.  The built-in
29	// phony rule has special behavior that is sometimes not desired.  See the
30	// Ninja docs for more details.
31	Phony = pctx.AndroidStaticRule("Phony",
32		blueprint.RuleParams{
33			Command:     "# phony $out",
34			Description: "phony $out",
35		})
36
37	// GeneratedFile is a rule for indicating that a given file was generated
38	// while running soong.  This allows the file to be cleaned up if it ever
39	// stops being generated by soong.
40	GeneratedFile = pctx.AndroidStaticRule("GeneratedFile",
41		blueprint.RuleParams{
42			Command:     "# generated $out",
43			Description: "generated $out",
44			Generator:   true,
45		})
46
47	// A copy rule.
48	Cp = pctx.AndroidStaticRule("Cp",
49		blueprint.RuleParams{
50			Command:     "rm -f $out && cp $cpPreserveSymlinks $cpFlags $in $out$extraCmds",
51			Description: "cp $out",
52		},
53		"cpFlags", "extraCmds")
54
55	// A copy rule that doesn't preserve symlinks.
56	CpNoPreserveSymlink = pctx.AndroidStaticRule("CpNoPreserveSymlink",
57		blueprint.RuleParams{
58			Command:     "rm -f $out && cp $cpFlags $in $out$extraCmds",
59			Description: "cp $out",
60		},
61		"cpFlags", "extraCmds")
62
63	// A copy rule that only updates the output if it changed.
64	CpIfChanged = pctx.AndroidStaticRule("CpIfChanged",
65		blueprint.RuleParams{
66			Command:     "if ! cmp -s $in $out; then cp $in $out; fi",
67			Description: "cp if changed $out",
68			Restat:      true,
69		})
70
71	CpExecutable = pctx.AndroidStaticRule("CpExecutable",
72		blueprint.RuleParams{
73			Command:     "rm -f $out && cp $cpFlags $in $out && chmod +x $out$extraCmds",
74			Description: "cp $out",
75		},
76		"cpFlags", "extraCmds")
77
78	// A timestamp touch rule.
79	Touch = pctx.AndroidStaticRule("Touch",
80		blueprint.RuleParams{
81			Command:     "touch $out",
82			Description: "touch $out",
83		})
84
85	// A symlink rule.
86	Symlink = pctx.AndroidStaticRule("Symlink",
87		blueprint.RuleParams{
88			Command:     "rm -f $out && ln -f -s $fromPath $out",
89			Description: "symlink $out",
90		},
91		"fromPath")
92
93	ErrorRule = pctx.AndroidStaticRule("Error",
94		blueprint.RuleParams{
95			Command:     `echo "$error" && false`,
96			Description: "error building $out",
97		},
98		"error")
99
100	Cat = pctx.AndroidStaticRule("Cat",
101		blueprint.RuleParams{
102			Command:     "rm -f $out && cat $in > $out",
103			Description: "concatenate files to $out",
104		})
105
106	// Used only when USE_GOMA=true is set, to restrict non-goma jobs to the local parallelism value
107	localPool = blueprint.NewBuiltinPool("local_pool")
108
109	// Used only by RuleBuilder to identify remoteable rules. Does not actually get created in ninja.
110	remotePool = blueprint.NewBuiltinPool("remote_pool")
111
112	// Used for processes that need significant RAM to ensure there are not too many running in parallel.
113	highmemPool = blueprint.NewBuiltinPool("highmem_pool")
114)
115
116func init() {
117	pctx.Import("github.com/google/blueprint/bootstrap")
118
119	pctx.VariableFunc("RBEWrapper", func(ctx PackageVarContext) string {
120		return ctx.Config().RBEWrapper()
121	})
122}
123
124// GlobToListFileRule creates a rule that writes a list of files matching a pattern to a file.
125func GlobToListFileRule(ctx ModuleContext, pattern string, excludes []string, file WritablePath) {
126	bootstrap.GlobFile(ctx.blueprintModuleContext(), pattern, excludes, file.String())
127}
128