blob: 9ae360e36e114c44f632c29602e71681904a666f [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 (
Sam Delmerico46d08b42022-11-15 15:51:04 -050028 pctx = NewPackageContext("android/soong/android")
29 exportedVars = NewExportedVariables(pctx)
Colin Cross3f40fa42015-01-30 17:27:36 -080030
31 cpPreserveSymlinks = pctx.VariableConfigMethod("cpPreserveSymlinks",
32 Config.CpPreserveSymlinksFlags)
33
Colin Cross3f40fa42015-01-30 17:27:36 -080034 // A phony rule that is not the built-in Ninja phony rule. The built-in
35 // phony rule has special behavior that is sometimes not desired. See the
36 // Ninja docs for more details.
Colin Cross9d45bb72016-08-29 16:14:13 -070037 Phony = pctx.AndroidStaticRule("Phony",
Colin Cross3f40fa42015-01-30 17:27:36 -080038 blueprint.RuleParams{
39 Command: "# phony $out",
40 Description: "phony $out",
41 })
42
43 // GeneratedFile is a rule for indicating that a given file was generated
44 // while running soong. This allows the file to be cleaned up if it ever
45 // stops being generated by soong.
Colin Cross9d45bb72016-08-29 16:14:13 -070046 GeneratedFile = pctx.AndroidStaticRule("GeneratedFile",
Colin Cross3f40fa42015-01-30 17:27:36 -080047 blueprint.RuleParams{
48 Command: "# generated $out",
49 Description: "generated $out",
50 Generator: true,
51 })
52
53 // A copy rule.
Colin Cross9d45bb72016-08-29 16:14:13 -070054 Cp = pctx.AndroidStaticRule("Cp",
Colin Cross3f40fa42015-01-30 17:27:36 -080055 blueprint.RuleParams{
Colin Cross50ed1f92021-11-12 17:41:02 -080056 Command: "rm -f $out && cp $cpPreserveSymlinks $cpFlags $in $out$extraCmds",
Colin Cross3f40fa42015-01-30 17:27:36 -080057 Description: "cp $out",
58 },
Colin Cross50ed1f92021-11-12 17:41:02 -080059 "cpFlags", "extraCmds")
Colin Cross3f40fa42015-01-30 17:27:36 -080060
Colin Cross00d93b12021-03-04 10:00:09 -080061 // A copy rule that only updates the output if it changed.
62 CpIfChanged = pctx.AndroidStaticRule("CpIfChanged",
63 blueprint.RuleParams{
64 Command: "if ! cmp -s $in $out; then cp $in $out; fi",
65 Description: "cp if changed $out",
66 Restat: true,
67 },
68 "cpFlags")
69
Colin Cross5c517922017-08-31 12:29:17 -070070 CpExecutable = pctx.AndroidStaticRule("CpExecutable",
71 blueprint.RuleParams{
Chih-Hung Hsieh1048a732022-08-10 20:51:37 -070072 Command: "rm -f $out && cp $cpFlags $in $out && chmod +x $out$extraCmds",
Colin Cross5c517922017-08-31 12:29:17 -070073 Description: "cp $out",
74 },
Colin Cross50ed1f92021-11-12 17:41:02 -080075 "cpFlags", "extraCmds")
Colin Cross5c517922017-08-31 12:29:17 -070076
Dan Albert5d723ab2016-07-18 22:29:52 -070077 // A timestamp touch rule.
Colin Cross9d45bb72016-08-29 16:14:13 -070078 Touch = pctx.AndroidStaticRule("Touch",
Dan Albert5d723ab2016-07-18 22:29:52 -070079 blueprint.RuleParams{
80 Command: "touch $out",
81 Description: "touch $out",
82 })
83
Colin Cross3f40fa42015-01-30 17:27:36 -080084 // A symlink rule.
Colin Cross9d45bb72016-08-29 16:14:13 -070085 Symlink = pctx.AndroidStaticRule("Symlink",
Colin Cross3f40fa42015-01-30 17:27:36 -080086 blueprint.RuleParams{
Jingwen Chence679d22020-09-23 04:30:02 +000087 Command: "rm -f $out && ln -f -s $fromPath $out",
88 Description: "symlink $out",
89 SymlinkOutputs: []string{"$out"},
Colin Cross3f40fa42015-01-30 17:27:36 -080090 },
91 "fromPath")
Colin Cross6ff51382015-12-17 16:39:19 -080092
Colin Cross9d45bb72016-08-29 16:14:13 -070093 ErrorRule = pctx.AndroidStaticRule("Error",
Colin Cross6ff51382015-12-17 16:39:19 -080094 blueprint.RuleParams{
95 Command: `echo "$error" && false`,
96 Description: "error building $out",
97 },
98 "error")
Colin Cross9d45bb72016-08-29 16:14:13 -070099
Dan Albertc6345fb2016-10-20 01:36:11 -0700100 Cat = pctx.AndroidStaticRule("Cat",
101 blueprint.RuleParams{
102 Command: "cat $in > $out",
103 Description: "concatenate licenses $out",
104 })
105
Nan Zhang27005112017-05-12 14:02:13 -0700106 // ubuntu 14.04 offcially use dash for /bin/sh, and its builtin echo command
107 // doesn't support -e option. Therefore we force to use /bin/bash when writing out
108 // content to file.
Colin Crosscf371cc2020-11-13 11:48:42 -0800109 writeFile = pctx.AndroidStaticRule("writeFile",
Dan Albert30c9d6e2017-03-28 14:54:55 -0700110 blueprint.RuleParams{
Hans Månssond3f2bd72020-11-27 12:37:28 +0100111 Command: `/bin/bash -c 'echo -e -n "$$0" > $out' $content`,
Dan Albert30c9d6e2017-03-28 14:54:55 -0700112 Description: "writing file $out",
113 },
114 "content")
115
Colin Cross9d45bb72016-08-29 16:14:13 -0700116 // Used only when USE_GOMA=true is set, to restrict non-goma jobs to the local parallelism value
117 localPool = blueprint.NewBuiltinPool("local_pool")
Colin Cross8b8bec32019-11-15 13:18:43 -0800118
Ramy Medhat944839a2020-03-31 22:14:52 -0400119 // Used only by RuleBuilder to identify remoteable rules. Does not actually get created in ninja.
120 remotePool = blueprint.NewBuiltinPool("remote_pool")
121
Colin Cross8b8bec32019-11-15 13:18:43 -0800122 // Used for processes that need significant RAM to ensure there are not too many running in parallel.
123 highmemPool = blueprint.NewBuiltinPool("highmem_pool")
Colin Cross3f40fa42015-01-30 17:27:36 -0800124)
Dan Willemsen24f2f8d2015-07-15 14:34:02 -0700125
126func init() {
127 pctx.Import("github.com/google/blueprint/bootstrap")
Colin Cross77cdcfd2021-03-12 11:28:25 -0800128
129 pctx.VariableFunc("RBEWrapper", func(ctx PackageVarContext) string {
130 return ctx.Config().RBEWrapper()
131 })
Sam Delmerico46d08b42022-11-15 15:51:04 -0500132
133 exportedVars.ExportStringList("NeverAllowNotInIncludeDir", neverallowNotInIncludeDir)
134 exportedVars.ExportStringList("NeverAllowNoUseIncludeDir", neverallowNoUseIncludeDir)
135}
136
137func BazelCcToolchainVars(config Config) string {
138 return BazelToolchainVars(config, exportedVars)
Dan Willemsen24f2f8d2015-07-15 14:34:02 -0700139}
Colin Crosscf371cc2020-11-13 11:48:42 -0800140
141var (
142 // echoEscaper escapes a string such that passing it to "echo -e" will produce the input value.
143 echoEscaper = strings.NewReplacer(
144 `\`, `\\`, // First escape existing backslashes so they aren't interpreted by `echo -e`.
145 "\n", `\n`, // Then replace newlines with \n
146 )
147
148 // echoEscaper reverses echoEscaper.
149 echoUnescaper = strings.NewReplacer(
150 `\n`, "\n",
151 `\\`, `\`,
152 )
153
154 // shellUnescaper reverses the replacer in proptools.ShellEscape
155 shellUnescaper = strings.NewReplacer(`'\''`, `'`)
156)
157
Hans Månssond3f2bd72020-11-27 12:37:28 +0100158func buildWriteFileRule(ctx BuilderContext, outputFile WritablePath, content string) {
Colin Crosscf371cc2020-11-13 11:48:42 -0800159 content = echoEscaper.Replace(content)
Colin Cross1c217fd2021-03-12 17:24:18 -0800160 content = proptools.NinjaEscape(proptools.ShellEscapeIncludingSpaces(content))
Colin Crosscf371cc2020-11-13 11:48:42 -0800161 if content == "" {
162 content = "''"
163 }
164 ctx.Build(pctx, BuildParams{
165 Rule: writeFile,
166 Output: outputFile,
167 Description: "write " + outputFile.Base(),
168 Args: map[string]string{
169 "content": content,
170 },
171 })
172}
173
Hans Månssond3f2bd72020-11-27 12:37:28 +0100174// WriteFileRule creates a ninja rule to write contents to a file. The contents will be escaped
175// so that the file contains exactly the contents passed to the function, plus a trailing newline.
176func WriteFileRule(ctx BuilderContext, outputFile WritablePath, content string) {
177 // This is MAX_ARG_STRLEN subtracted with some safety to account for shell escapes
178 const SHARD_SIZE = 131072 - 10000
179
180 content += "\n"
181 if len(content) > SHARD_SIZE {
182 var chunks WritablePaths
183 for i, c := range ShardString(content, SHARD_SIZE) {
184 tempPath := outputFile.ReplaceExtension(ctx, fmt.Sprintf("%s.%d", outputFile.Ext(), i))
185 buildWriteFileRule(ctx, tempPath, c)
186 chunks = append(chunks, tempPath)
187 }
188 ctx.Build(pctx, BuildParams{
189 Rule: Cat,
190 Inputs: chunks.Paths(),
191 Output: outputFile,
192 Description: "Merging to " + outputFile.Base(),
193 })
194 return
195 }
196 buildWriteFileRule(ctx, outputFile, content)
197}
198
Rob Seymour925aa092021-08-10 20:42:03 +0000199func CatFileRule(ctx BuilderContext, paths Paths, outputFile WritablePath) {
200 ctx.Build(pctx, BuildParams{
201 Rule: Cat,
202 Inputs: paths,
203 Output: outputFile,
204 Description: "combine files to " + outputFile.Base(),
205 })
206}
207
Colin Crosscf371cc2020-11-13 11:48:42 -0800208// shellUnescape reverses proptools.ShellEscape
209func shellUnescape(s string) string {
210 // Remove leading and trailing quotes if present
211 if len(s) >= 2 && s[0] == '\'' {
212 s = s[1 : len(s)-1]
213 }
214 s = shellUnescaper.Replace(s)
215 return s
216}
217
218// ContentFromFileRuleForTests returns the content that was passed to a WriteFileRule for use
219// in tests.
220func ContentFromFileRuleForTests(t *testing.T, params TestingBuildParams) string {
221 t.Helper()
222 if g, w := params.Rule, writeFile; g != w {
223 t.Errorf("expected params.Rule to be %q, was %q", w, g)
224 return ""
225 }
226
227 content := params.Args["content"]
228 content = shellUnescape(content)
229 content = echoUnescaper.Replace(content)
230
231 return content
232}
Colin Crossc20dc852020-11-10 12:27:45 -0800233
234// GlobToListFileRule creates a rule that writes a list of files matching a pattern to a file.
235func GlobToListFileRule(ctx ModuleContext, pattern string, excludes []string, file WritablePath) {
236 bootstrap.GlobFile(ctx.blueprintModuleContext(), pattern, excludes, file.String())
237}