Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 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 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 16 | |
| 17 | import ( |
Hans Månsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 18 | "fmt" |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 19 | "strings" |
| 20 | "testing" |
| 21 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 23 | "github.com/google/blueprint/bootstrap" |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var ( |
Colin Cross | cc0ce80 | 2019-04-02 16:14:11 -0700 | [diff] [blame] | 28 | pctx = NewPackageContext("android/soong/android") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | |
| 30 | cpPreserveSymlinks = pctx.VariableConfigMethod("cpPreserveSymlinks", |
| 31 | Config.CpPreserveSymlinksFlags) |
| 32 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 33 | // 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 Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 36 | Phony = pctx.AndroidStaticRule("Phony", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 37 | 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 Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 45 | GeneratedFile = pctx.AndroidStaticRule("GeneratedFile", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 46 | blueprint.RuleParams{ |
| 47 | Command: "# generated $out", |
| 48 | Description: "generated $out", |
| 49 | Generator: true, |
| 50 | }) |
| 51 | |
| 52 | // A copy rule. |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 53 | Cp = pctx.AndroidStaticRule("Cp", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 54 | blueprint.RuleParams{ |
Josh Gao | ae15271 | 2017-07-26 14:09:50 -0700 | [diff] [blame] | 55 | Command: "rm -f $out && cp $cpPreserveSymlinks $cpFlags $in $out", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 56 | Description: "cp $out", |
| 57 | }, |
| 58 | "cpFlags") |
| 59 | |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 60 | CpExecutable = pctx.AndroidStaticRule("CpExecutable", |
| 61 | blueprint.RuleParams{ |
| 62 | Command: "rm -f $out && cp $cpPreserveSymlinks $cpFlags $in $out && chmod +x $out", |
| 63 | Description: "cp $out", |
| 64 | }, |
| 65 | "cpFlags") |
| 66 | |
Dan Albert | 5d723ab | 2016-07-18 22:29:52 -0700 | [diff] [blame] | 67 | // A timestamp touch rule. |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 68 | Touch = pctx.AndroidStaticRule("Touch", |
Dan Albert | 5d723ab | 2016-07-18 22:29:52 -0700 | [diff] [blame] | 69 | blueprint.RuleParams{ |
| 70 | Command: "touch $out", |
| 71 | Description: "touch $out", |
| 72 | }) |
| 73 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 74 | // A symlink rule. |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 75 | Symlink = pctx.AndroidStaticRule("Symlink", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 76 | blueprint.RuleParams{ |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 77 | Command: "rm -f $out && ln -f -s $fromPath $out", |
| 78 | Description: "symlink $out", |
| 79 | SymlinkOutputs: []string{"$out"}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 80 | }, |
| 81 | "fromPath") |
Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 82 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 83 | ErrorRule = pctx.AndroidStaticRule("Error", |
Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 84 | blueprint.RuleParams{ |
| 85 | Command: `echo "$error" && false`, |
| 86 | Description: "error building $out", |
| 87 | }, |
| 88 | "error") |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 89 | |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 90 | Cat = pctx.AndroidStaticRule("Cat", |
| 91 | blueprint.RuleParams{ |
| 92 | Command: "cat $in > $out", |
| 93 | Description: "concatenate licenses $out", |
| 94 | }) |
| 95 | |
Nan Zhang | 2700511 | 2017-05-12 14:02:13 -0700 | [diff] [blame] | 96 | // ubuntu 14.04 offcially use dash for /bin/sh, and its builtin echo command |
| 97 | // doesn't support -e option. Therefore we force to use /bin/bash when writing out |
| 98 | // content to file. |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 99 | writeFile = pctx.AndroidStaticRule("writeFile", |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 100 | blueprint.RuleParams{ |
Hans Månsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 101 | Command: `/bin/bash -c 'echo -e -n "$$0" > $out' $content`, |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 102 | Description: "writing file $out", |
| 103 | }, |
| 104 | "content") |
| 105 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 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") |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 108 | |
Ramy Medhat | 944839a | 2020-03-31 22:14:52 -0400 | [diff] [blame] | 109 | // Used only by RuleBuilder to identify remoteable rules. Does not actually get created in ninja. |
| 110 | remotePool = blueprint.NewBuiltinPool("remote_pool") |
| 111 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 112 | // Used for processes that need significant RAM to ensure there are not too many running in parallel. |
| 113 | highmemPool = blueprint.NewBuiltinPool("highmem_pool") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 114 | ) |
Dan Willemsen | 24f2f8d | 2015-07-15 14:34:02 -0700 | [diff] [blame] | 115 | |
| 116 | func init() { |
| 117 | pctx.Import("github.com/google/blueprint/bootstrap") |
| 118 | } |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 119 | |
| 120 | var ( |
| 121 | // echoEscaper escapes a string such that passing it to "echo -e" will produce the input value. |
| 122 | echoEscaper = strings.NewReplacer( |
| 123 | `\`, `\\`, // First escape existing backslashes so they aren't interpreted by `echo -e`. |
| 124 | "\n", `\n`, // Then replace newlines with \n |
| 125 | ) |
| 126 | |
| 127 | // echoEscaper reverses echoEscaper. |
| 128 | echoUnescaper = strings.NewReplacer( |
| 129 | `\n`, "\n", |
| 130 | `\\`, `\`, |
| 131 | ) |
| 132 | |
| 133 | // shellUnescaper reverses the replacer in proptools.ShellEscape |
| 134 | shellUnescaper = strings.NewReplacer(`'\''`, `'`) |
| 135 | ) |
| 136 | |
Hans Månsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 137 | func buildWriteFileRule(ctx BuilderContext, outputFile WritablePath, content string) { |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 138 | content = echoEscaper.Replace(content) |
| 139 | content = proptools.ShellEscape(content) |
| 140 | if content == "" { |
| 141 | content = "''" |
| 142 | } |
| 143 | ctx.Build(pctx, BuildParams{ |
| 144 | Rule: writeFile, |
| 145 | Output: outputFile, |
| 146 | Description: "write " + outputFile.Base(), |
| 147 | Args: map[string]string{ |
| 148 | "content": content, |
| 149 | }, |
| 150 | }) |
| 151 | } |
| 152 | |
Hans Månsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 153 | // WriteFileRule creates a ninja rule to write contents to a file. The contents will be escaped |
| 154 | // so that the file contains exactly the contents passed to the function, plus a trailing newline. |
| 155 | func WriteFileRule(ctx BuilderContext, outputFile WritablePath, content string) { |
| 156 | // This is MAX_ARG_STRLEN subtracted with some safety to account for shell escapes |
| 157 | const SHARD_SIZE = 131072 - 10000 |
| 158 | |
| 159 | content += "\n" |
| 160 | if len(content) > SHARD_SIZE { |
| 161 | var chunks WritablePaths |
| 162 | for i, c := range ShardString(content, SHARD_SIZE) { |
| 163 | tempPath := outputFile.ReplaceExtension(ctx, fmt.Sprintf("%s.%d", outputFile.Ext(), i)) |
| 164 | buildWriteFileRule(ctx, tempPath, c) |
| 165 | chunks = append(chunks, tempPath) |
| 166 | } |
| 167 | ctx.Build(pctx, BuildParams{ |
| 168 | Rule: Cat, |
| 169 | Inputs: chunks.Paths(), |
| 170 | Output: outputFile, |
| 171 | Description: "Merging to " + outputFile.Base(), |
| 172 | }) |
| 173 | return |
| 174 | } |
| 175 | buildWriteFileRule(ctx, outputFile, content) |
| 176 | } |
| 177 | |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 178 | // shellUnescape reverses proptools.ShellEscape |
| 179 | func shellUnescape(s string) string { |
| 180 | // Remove leading and trailing quotes if present |
| 181 | if len(s) >= 2 && s[0] == '\'' { |
| 182 | s = s[1 : len(s)-1] |
| 183 | } |
| 184 | s = shellUnescaper.Replace(s) |
| 185 | return s |
| 186 | } |
| 187 | |
| 188 | // ContentFromFileRuleForTests returns the content that was passed to a WriteFileRule for use |
| 189 | // in tests. |
| 190 | func ContentFromFileRuleForTests(t *testing.T, params TestingBuildParams) string { |
| 191 | t.Helper() |
| 192 | if g, w := params.Rule, writeFile; g != w { |
| 193 | t.Errorf("expected params.Rule to be %q, was %q", w, g) |
| 194 | return "" |
| 195 | } |
| 196 | |
| 197 | content := params.Args["content"] |
| 198 | content = shellUnescape(content) |
| 199 | content = echoUnescaper.Replace(content) |
| 200 | |
| 201 | return content |
| 202 | } |
Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 203 | |
| 204 | // GlobToListFileRule creates a rule that writes a list of files matching a pattern to a file. |
| 205 | func GlobToListFileRule(ctx ModuleContext, pattern string, excludes []string, file WritablePath) { |
| 206 | bootstrap.GlobFile(ctx.blueprintModuleContext(), pattern, excludes, file.String()) |
| 207 | } |