blob: 1a767214c1a59f67a04e6e0bcf14e15ed9285a83 [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
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Hans Månssond3f2bd72020-11-27 12:37:28 +010018 "fmt"
Colin Crosscf371cc2020-11-13 11:48:42 -080019 "strings"
20 "testing"
21
Colin Cross70b40592015-03-23 12:57:34 -070022 "github.com/google/blueprint"
Colin Crossc20dc852020-11-10 12:27:45 -080023 "github.com/google/blueprint/bootstrap"
Colin Crosscf371cc2020-11-13 11:48:42 -080024 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080025)
26
27var (
Colin Crosscc0ce802019-04-02 16:14:11 -070028 pctx = NewPackageContext("android/soong/android")
Colin Cross3f40fa42015-01-30 17:27:36 -080029
30 cpPreserveSymlinks = pctx.VariableConfigMethod("cpPreserveSymlinks",
31 Config.CpPreserveSymlinksFlags)
32
Colin Cross3f40fa42015-01-30 17:27:36 -080033 // A phony rule that is not the built-in Ninja phony rule. The built-in
34 // phony rule has special behavior that is sometimes not desired. See the
35 // Ninja docs for more details.
Colin Cross9d45bb72016-08-29 16:14:13 -070036 Phony = pctx.AndroidStaticRule("Phony",
Colin Cross3f40fa42015-01-30 17:27:36 -080037 blueprint.RuleParams{
38 Command: "# phony $out",
39 Description: "phony $out",
40 })
41
42 // GeneratedFile is a rule for indicating that a given file was generated
43 // while running soong. This allows the file to be cleaned up if it ever
44 // stops being generated by soong.
Colin Cross9d45bb72016-08-29 16:14:13 -070045 GeneratedFile = pctx.AndroidStaticRule("GeneratedFile",
Colin Cross3f40fa42015-01-30 17:27:36 -080046 blueprint.RuleParams{
47 Command: "# generated $out",
48 Description: "generated $out",
49 Generator: true,
50 })
51
52 // A copy rule.
Colin Cross9d45bb72016-08-29 16:14:13 -070053 Cp = pctx.AndroidStaticRule("Cp",
Colin Cross3f40fa42015-01-30 17:27:36 -080054 blueprint.RuleParams{
Josh Gaoae152712017-07-26 14:09:50 -070055 Command: "rm -f $out && cp $cpPreserveSymlinks $cpFlags $in $out",
Colin Cross3f40fa42015-01-30 17:27:36 -080056 Description: "cp $out",
57 },
58 "cpFlags")
59
Colin Cross00d93b12021-03-04 10:00:09 -080060 // A copy rule that only updates the output if it changed.
61 CpIfChanged = pctx.AndroidStaticRule("CpIfChanged",
62 blueprint.RuleParams{
63 Command: "if ! cmp -s $in $out; then cp $in $out; fi",
64 Description: "cp if changed $out",
65 Restat: true,
66 },
67 "cpFlags")
68
Colin Cross5c517922017-08-31 12:29:17 -070069 CpExecutable = pctx.AndroidStaticRule("CpExecutable",
70 blueprint.RuleParams{
71 Command: "rm -f $out && cp $cpPreserveSymlinks $cpFlags $in $out && chmod +x $out",
72 Description: "cp $out",
73 },
74 "cpFlags")
75
Dan Albert5d723ab2016-07-18 22:29:52 -070076 // A timestamp touch rule.
Colin Cross9d45bb72016-08-29 16:14:13 -070077 Touch = pctx.AndroidStaticRule("Touch",
Dan Albert5d723ab2016-07-18 22:29:52 -070078 blueprint.RuleParams{
79 Command: "touch $out",
80 Description: "touch $out",
81 })
82
Colin Cross3f40fa42015-01-30 17:27:36 -080083 // A symlink rule.
Colin Cross9d45bb72016-08-29 16:14:13 -070084 Symlink = pctx.AndroidStaticRule("Symlink",
Colin Cross3f40fa42015-01-30 17:27:36 -080085 blueprint.RuleParams{
Jingwen Chence679d22020-09-23 04:30:02 +000086 Command: "rm -f $out && ln -f -s $fromPath $out",
87 Description: "symlink $out",
88 SymlinkOutputs: []string{"$out"},
Colin Cross3f40fa42015-01-30 17:27:36 -080089 },
90 "fromPath")
Colin Cross6ff51382015-12-17 16:39:19 -080091
Colin Cross9d45bb72016-08-29 16:14:13 -070092 ErrorRule = pctx.AndroidStaticRule("Error",
Colin Cross6ff51382015-12-17 16:39:19 -080093 blueprint.RuleParams{
94 Command: `echo "$error" && false`,
95 Description: "error building $out",
96 },
97 "error")
Colin Cross9d45bb72016-08-29 16:14:13 -070098
Dan Albertc6345fb2016-10-20 01:36:11 -070099 Cat = pctx.AndroidStaticRule("Cat",
100 blueprint.RuleParams{
101 Command: "cat $in > $out",
102 Description: "concatenate licenses $out",
103 })
104
Nan Zhang27005112017-05-12 14:02:13 -0700105 // ubuntu 14.04 offcially use dash for /bin/sh, and its builtin echo command
106 // doesn't support -e option. Therefore we force to use /bin/bash when writing out
107 // content to file.
Colin Crosscf371cc2020-11-13 11:48:42 -0800108 writeFile = pctx.AndroidStaticRule("writeFile",
Dan Albert30c9d6e2017-03-28 14:54:55 -0700109 blueprint.RuleParams{
Hans Månssond3f2bd72020-11-27 12:37:28 +0100110 Command: `/bin/bash -c 'echo -e -n "$$0" > $out' $content`,
Dan Albert30c9d6e2017-03-28 14:54:55 -0700111 Description: "writing file $out",
112 },
113 "content")
114
Colin Cross9d45bb72016-08-29 16:14:13 -0700115 // Used only when USE_GOMA=true is set, to restrict non-goma jobs to the local parallelism value
116 localPool = blueprint.NewBuiltinPool("local_pool")
Colin Cross8b8bec32019-11-15 13:18:43 -0800117
Ramy Medhat944839a2020-03-31 22:14:52 -0400118 // Used only by RuleBuilder to identify remoteable rules. Does not actually get created in ninja.
119 remotePool = blueprint.NewBuiltinPool("remote_pool")
120
Colin Cross8b8bec32019-11-15 13:18:43 -0800121 // Used for processes that need significant RAM to ensure there are not too many running in parallel.
122 highmemPool = blueprint.NewBuiltinPool("highmem_pool")
Colin Cross3f40fa42015-01-30 17:27:36 -0800123)
Dan Willemsen24f2f8d2015-07-15 14:34:02 -0700124
125func init() {
126 pctx.Import("github.com/google/blueprint/bootstrap")
127}
Colin Crosscf371cc2020-11-13 11:48:42 -0800128
129var (
130 // echoEscaper escapes a string such that passing it to "echo -e" will produce the input value.
131 echoEscaper = strings.NewReplacer(
132 `\`, `\\`, // First escape existing backslashes so they aren't interpreted by `echo -e`.
133 "\n", `\n`, // Then replace newlines with \n
134 )
135
136 // echoEscaper reverses echoEscaper.
137 echoUnescaper = strings.NewReplacer(
138 `\n`, "\n",
139 `\\`, `\`,
140 )
141
142 // shellUnescaper reverses the replacer in proptools.ShellEscape
143 shellUnescaper = strings.NewReplacer(`'\''`, `'`)
144)
145
Hans Månssond3f2bd72020-11-27 12:37:28 +0100146func buildWriteFileRule(ctx BuilderContext, outputFile WritablePath, content string) {
Colin Crosscf371cc2020-11-13 11:48:42 -0800147 content = echoEscaper.Replace(content)
148 content = proptools.ShellEscape(content)
149 if content == "" {
150 content = "''"
151 }
152 ctx.Build(pctx, BuildParams{
153 Rule: writeFile,
154 Output: outputFile,
155 Description: "write " + outputFile.Base(),
156 Args: map[string]string{
157 "content": content,
158 },
159 })
160}
161
Hans Månssond3f2bd72020-11-27 12:37:28 +0100162// WriteFileRule creates a ninja rule to write contents to a file. The contents will be escaped
163// so that the file contains exactly the contents passed to the function, plus a trailing newline.
164func WriteFileRule(ctx BuilderContext, outputFile WritablePath, content string) {
165 // This is MAX_ARG_STRLEN subtracted with some safety to account for shell escapes
166 const SHARD_SIZE = 131072 - 10000
167
168 content += "\n"
169 if len(content) > SHARD_SIZE {
170 var chunks WritablePaths
171 for i, c := range ShardString(content, SHARD_SIZE) {
172 tempPath := outputFile.ReplaceExtension(ctx, fmt.Sprintf("%s.%d", outputFile.Ext(), i))
173 buildWriteFileRule(ctx, tempPath, c)
174 chunks = append(chunks, tempPath)
175 }
176 ctx.Build(pctx, BuildParams{
177 Rule: Cat,
178 Inputs: chunks.Paths(),
179 Output: outputFile,
180 Description: "Merging to " + outputFile.Base(),
181 })
182 return
183 }
184 buildWriteFileRule(ctx, outputFile, content)
185}
186
Colin Crosscf371cc2020-11-13 11:48:42 -0800187// shellUnescape reverses proptools.ShellEscape
188func shellUnescape(s string) string {
189 // Remove leading and trailing quotes if present
190 if len(s) >= 2 && s[0] == '\'' {
191 s = s[1 : len(s)-1]
192 }
193 s = shellUnescaper.Replace(s)
194 return s
195}
196
197// ContentFromFileRuleForTests returns the content that was passed to a WriteFileRule for use
198// in tests.
199func ContentFromFileRuleForTests(t *testing.T, params TestingBuildParams) string {
200 t.Helper()
201 if g, w := params.Rule, writeFile; g != w {
202 t.Errorf("expected params.Rule to be %q, was %q", w, g)
203 return ""
204 }
205
206 content := params.Args["content"]
207 content = shellUnescape(content)
208 content = echoUnescaper.Replace(content)
209
210 return content
211}
Colin Crossc20dc852020-11-10 12:27:45 -0800212
213// GlobToListFileRule creates a rule that writes a list of files matching a pattern to a file.
214func GlobToListFileRule(ctx ModuleContext, pattern string, excludes []string, file WritablePath) {
215 bootstrap.GlobFile(ctx.blueprintModuleContext(), pattern, excludes, file.String())
216}