• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
20
21var (
22	pctx = NewPackageContext("android/soong/android")
23
24	cpPreserveSymlinks = pctx.VariableConfigMethod("cpPreserveSymlinks",
25		Config.CpPreserveSymlinksFlags)
26
27	// A phony rule that is not the built-in Ninja phony rule.  The built-in
28	// phony rule has special behavior that is sometimes not desired.  See the
29	// Ninja docs for more details.
30	Phony = pctx.AndroidStaticRule("Phony",
31		blueprint.RuleParams{
32			Command:     "# phony $out",
33			Description: "phony $out",
34		})
35
36	// GeneratedFile is a rule for indicating that a given file was generated
37	// while running soong.  This allows the file to be cleaned up if it ever
38	// stops being generated by soong.
39	GeneratedFile = pctx.AndroidStaticRule("GeneratedFile",
40		blueprint.RuleParams{
41			Command:     "# generated $out",
42			Description: "generated $out",
43			Generator:   true,
44		})
45
46	// A copy rule.
47	Cp = pctx.AndroidStaticRule("Cp",
48		blueprint.RuleParams{
49			Command:     "rm -f $out && cp $cpPreserveSymlinks $cpFlags $in $out$extraCmds",
50			Description: "cp $out",
51		},
52		"cpFlags", "extraCmds")
53
54	// A copy rule wrapped with bash.
55	CpWithBash = pctx.AndroidStaticRule("CpWithBash",
56		blueprint.RuleParams{
57			Command:     "/bin/bash -c \"rm -f $out && cp $cpFlags $cpPreserveSymlinks $in $out$extraCmds\"",
58			Description: "cp $out",
59		},
60		"cpFlags", "extraCmds")
61
62	// A copy rule that doesn't preserve symlinks.
63	CpNoPreserveSymlink = pctx.AndroidStaticRule("CpNoPreserveSymlink",
64		blueprint.RuleParams{
65			Command:     "rm -f $out && cp $cpFlags $in $out$extraCmds",
66			Description: "cp $out",
67		},
68		"cpFlags", "extraCmds")
69
70	// A copy rule that only updates the output if it changed.
71	CpIfChanged = pctx.AndroidStaticRule("CpIfChanged",
72		blueprint.RuleParams{
73			Command:     "if ! cmp -s $in $out; then cp $in $out; fi",
74			Description: "cp if changed $out",
75			Restat:      true,
76		})
77
78	CpExecutable = pctx.AndroidStaticRule("CpExecutable",
79		blueprint.RuleParams{
80			Command:     "rm -f $out && cp $cpFlags $in $out && chmod +x $out$extraCmds",
81			Description: "cp $out",
82		},
83		"cpFlags", "extraCmds")
84
85	// A copy executable rule wrapped with bash
86	CpExecutableWithBash = pctx.AndroidStaticRule("CpExecutableWithBash",
87		blueprint.RuleParams{
88			Command:     "/bin/bash -c \"(rm -f $out && cp $cpFlags $cpPreserveSymlinks $in $out ) && (chmod +x $out$extraCmds )\"",
89			Description: "cp $out",
90		},
91		"cpFlags", "extraCmds")
92
93	// A timestamp touch rule.
94	Touch = pctx.AndroidStaticRule("Touch",
95		blueprint.RuleParams{
96			Command:     "touch $out",
97			Description: "touch $out",
98		})
99
100	// A symlink rule.
101	Symlink = pctx.AndroidStaticRule("Symlink",
102		blueprint.RuleParams{
103			Command:     "rm -f $out && ln -f -s $fromPath $out",
104			Description: "symlink $out",
105		},
106		"fromPath")
107
108	// A symlink rule wrapped with bash
109	SymlinkWithBash = pctx.AndroidStaticRule("SymlinkWithBash",
110		blueprint.RuleParams{
111			Command:     "/bin/bash -c \"rm -f $out && ln -sfn $fromPath $out\"",
112			Description: "symlink $out",
113		},
114		"fromPath")
115
116	ErrorRule = pctx.AndroidStaticRule("Error",
117		blueprint.RuleParams{
118			Command:     `echo "$error" && false`,
119			Description: "error building $out",
120		},
121		"error")
122
123	Cat = pctx.AndroidStaticRule("Cat",
124		blueprint.RuleParams{
125			Command:     "rm -f $out && cat $in > $out",
126			Description: "concatenate files to $out",
127		})
128
129	// Used only when USE_GOMA=true is set, to restrict non-goma jobs to the local parallelism value
130	localPool = blueprint.NewBuiltinPool("local_pool")
131
132	// Used only by RuleBuilder to identify remoteable rules. Does not actually get created in ninja.
133	remotePool = blueprint.NewBuiltinPool("remote_pool")
134
135	// Used for processes that need significant RAM to ensure there are not too many running in parallel.
136	highmemPool = blueprint.NewBuiltinPool("highmem_pool")
137)
138
139func init() {
140	pctx.Import("github.com/google/blueprint/bootstrap")
141
142	pctx.VariableFunc("RBEWrapper", func(ctx PackageVarContext) string {
143		return ctx.Config().RBEWrapper()
144	})
145}
146
147// CopyFileRule creates a ninja rule to copy path to outPath.
148func CopyFileRule(ctx ModuleContext, path Path, outPath OutputPath) {
149	ctx.Build(pctx, BuildParams{
150		Rule:        Cp,
151		Input:       path,
152		Output:      outPath,
153		Description: "copy " + outPath.Base(),
154	})
155}
156