| 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 ( | 
| Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 28 | pctx         = NewPackageContext("android/soong/android") | 
|  | 29 | exportedVars = NewExportedVariables(pctx) | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 30 |  | 
|  | 31 | cpPreserveSymlinks = pctx.VariableConfigMethod("cpPreserveSymlinks", | 
|  | 32 | Config.CpPreserveSymlinksFlags) | 
|  | 33 |  | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 34 | // 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 Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 37 | Phony = pctx.AndroidStaticRule("Phony", | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 38 | 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 Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 46 | GeneratedFile = pctx.AndroidStaticRule("GeneratedFile", | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 47 | blueprint.RuleParams{ | 
|  | 48 | Command:     "# generated $out", | 
|  | 49 | Description: "generated $out", | 
|  | 50 | Generator:   true, | 
|  | 51 | }) | 
|  | 52 |  | 
|  | 53 | // A copy rule. | 
| Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 54 | Cp = pctx.AndroidStaticRule("Cp", | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 55 | blueprint.RuleParams{ | 
| Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 56 | Command:     "rm -f $out && cp $cpPreserveSymlinks $cpFlags $in $out$extraCmds", | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 57 | Description: "cp $out", | 
|  | 58 | }, | 
| Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 59 | "cpFlags", "extraCmds") | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 60 |  | 
| Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 61 | // A copy rule that doesn't preserve symlinks. | 
|  | 62 | CpNoPreserveSymlink = pctx.AndroidStaticRule("CpNoPreserveSymlink", | 
|  | 63 | blueprint.RuleParams{ | 
|  | 64 | Command:     "rm -f $out && cp $cpFlags $in $out$extraCmds", | 
|  | 65 | Description: "cp $out", | 
|  | 66 | }, | 
|  | 67 | "cpFlags", "extraCmds") | 
|  | 68 |  | 
| Colin Cross | 00d93b1 | 2021-03-04 10:00:09 -0800 | [diff] [blame] | 69 | // A copy rule that only updates the output if it changed. | 
|  | 70 | CpIfChanged = pctx.AndroidStaticRule("CpIfChanged", | 
|  | 71 | blueprint.RuleParams{ | 
|  | 72 | Command:     "if ! cmp -s $in $out; then cp $in $out; fi", | 
|  | 73 | Description: "cp if changed $out", | 
|  | 74 | Restat:      true, | 
|  | 75 | }, | 
|  | 76 | "cpFlags") | 
|  | 77 |  | 
| Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 78 | CpExecutable = pctx.AndroidStaticRule("CpExecutable", | 
|  | 79 | blueprint.RuleParams{ | 
| Chih-Hung Hsieh | 1048a73 | 2022-08-10 20:51:37 -0700 | [diff] [blame] | 80 | Command:     "rm -f $out && cp $cpFlags $in $out && chmod +x $out$extraCmds", | 
| Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 81 | Description: "cp $out", | 
|  | 82 | }, | 
| Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 83 | "cpFlags", "extraCmds") | 
| Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 84 |  | 
| Dan Albert | 5d723ab | 2016-07-18 22:29:52 -0700 | [diff] [blame] | 85 | // A timestamp touch rule. | 
| Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 86 | Touch = pctx.AndroidStaticRule("Touch", | 
| Dan Albert | 5d723ab | 2016-07-18 22:29:52 -0700 | [diff] [blame] | 87 | blueprint.RuleParams{ | 
|  | 88 | Command:     "touch $out", | 
|  | 89 | Description: "touch $out", | 
|  | 90 | }) | 
|  | 91 |  | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 92 | // A symlink rule. | 
| Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 93 | Symlink = pctx.AndroidStaticRule("Symlink", | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 94 | blueprint.RuleParams{ | 
| Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 95 | Command:        "rm -f $out && ln -f -s $fromPath $out", | 
|  | 96 | Description:    "symlink $out", | 
|  | 97 | SymlinkOutputs: []string{"$out"}, | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 98 | }, | 
|  | 99 | "fromPath") | 
| Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 100 |  | 
| Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 101 | ErrorRule = pctx.AndroidStaticRule("Error", | 
| Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 102 | blueprint.RuleParams{ | 
|  | 103 | Command:     `echo "$error" && false`, | 
|  | 104 | Description: "error building $out", | 
|  | 105 | }, | 
|  | 106 | "error") | 
| Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 107 |  | 
| Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 108 | Cat = pctx.AndroidStaticRule("Cat", | 
|  | 109 | blueprint.RuleParams{ | 
| Cole Faust | 20f2030 | 2023-08-31 11:00:25 -0700 | [diff] [blame] | 110 | Command:     "rm -f $out && cat $in > $out", | 
|  | 111 | Description: "concatenate files to $out", | 
| Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 112 | }) | 
|  | 113 |  | 
| Nan Zhang | 2700511 | 2017-05-12 14:02:13 -0700 | [diff] [blame] | 114 | // ubuntu 14.04 offcially use dash for /bin/sh, and its builtin echo command | 
|  | 115 | // doesn't support -e option. Therefore we force to use /bin/bash when writing out | 
|  | 116 | // content to file. | 
| Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 117 | writeFile = pctx.AndroidStaticRule("writeFile", | 
| Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 118 | blueprint.RuleParams{ | 
| Cole Faust | 20f2030 | 2023-08-31 11:00:25 -0700 | [diff] [blame] | 119 | Command:     `rm -f $out && /bin/bash -c 'echo -e -n "$$0" > $out' $content`, | 
| Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 120 | Description: "writing file $out", | 
|  | 121 | }, | 
|  | 122 | "content") | 
|  | 123 |  | 
| Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 124 | // Used only when USE_GOMA=true is set, to restrict non-goma jobs to the local parallelism value | 
|  | 125 | localPool = blueprint.NewBuiltinPool("local_pool") | 
| Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 126 |  | 
| Ramy Medhat | 944839a | 2020-03-31 22:14:52 -0400 | [diff] [blame] | 127 | // Used only by RuleBuilder to identify remoteable rules. Does not actually get created in ninja. | 
|  | 128 | remotePool = blueprint.NewBuiltinPool("remote_pool") | 
|  | 129 |  | 
| Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 130 | // Used for processes that need significant RAM to ensure there are not too many running in parallel. | 
|  | 131 | highmemPool = blueprint.NewBuiltinPool("highmem_pool") | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 132 | ) | 
| Dan Willemsen | 24f2f8d | 2015-07-15 14:34:02 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | func init() { | 
|  | 135 | pctx.Import("github.com/google/blueprint/bootstrap") | 
| Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 136 |  | 
|  | 137 | pctx.VariableFunc("RBEWrapper", func(ctx PackageVarContext) string { | 
|  | 138 | return ctx.Config().RBEWrapper() | 
|  | 139 | }) | 
| Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 140 |  | 
|  | 141 | exportedVars.ExportStringList("NeverAllowNotInIncludeDir", neverallowNotInIncludeDir) | 
|  | 142 | exportedVars.ExportStringList("NeverAllowNoUseIncludeDir", neverallowNoUseIncludeDir) | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | func BazelCcToolchainVars(config Config) string { | 
|  | 146 | return BazelToolchainVars(config, exportedVars) | 
| Dan Willemsen | 24f2f8d | 2015-07-15 14:34:02 -0700 | [diff] [blame] | 147 | } | 
| Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 148 |  | 
|  | 149 | var ( | 
|  | 150 | // echoEscaper escapes a string such that passing it to "echo -e" will produce the input value. | 
|  | 151 | echoEscaper = strings.NewReplacer( | 
|  | 152 | `\`, `\\`, // First escape existing backslashes so they aren't interpreted by `echo -e`. | 
|  | 153 | "\n", `\n`, // Then replace newlines with \n | 
|  | 154 | ) | 
|  | 155 |  | 
|  | 156 | // echoEscaper reverses echoEscaper. | 
|  | 157 | echoUnescaper = strings.NewReplacer( | 
|  | 158 | `\n`, "\n", | 
|  | 159 | `\\`, `\`, | 
|  | 160 | ) | 
|  | 161 |  | 
|  | 162 | // shellUnescaper reverses the replacer in proptools.ShellEscape | 
|  | 163 | shellUnescaper = strings.NewReplacer(`'\''`, `'`) | 
|  | 164 | ) | 
|  | 165 |  | 
| Hans Månsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 166 | func buildWriteFileRule(ctx BuilderContext, outputFile WritablePath, content string) { | 
| Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 167 | content = echoEscaper.Replace(content) | 
| Colin Cross | 1c217fd | 2021-03-12 17:24:18 -0800 | [diff] [blame] | 168 | content = proptools.NinjaEscape(proptools.ShellEscapeIncludingSpaces(content)) | 
| Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 169 | if content == "" { | 
|  | 170 | content = "''" | 
|  | 171 | } | 
|  | 172 | ctx.Build(pctx, BuildParams{ | 
|  | 173 | Rule:        writeFile, | 
|  | 174 | Output:      outputFile, | 
|  | 175 | Description: "write " + outputFile.Base(), | 
|  | 176 | Args: map[string]string{ | 
|  | 177 | "content": content, | 
|  | 178 | }, | 
|  | 179 | }) | 
|  | 180 | } | 
|  | 181 |  | 
| Hans Månsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 182 | // WriteFileRule creates a ninja rule to write contents to a file.  The contents will be escaped | 
|  | 183 | // so that the file contains exactly the contents passed to the function, plus a trailing newline. | 
|  | 184 | func WriteFileRule(ctx BuilderContext, outputFile WritablePath, content string) { | 
| Cole Faust | a734749 | 2022-12-16 10:56:24 -0800 | [diff] [blame] | 185 | WriteFileRuleVerbatim(ctx, outputFile, content+"\n") | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | // WriteFileRuleVerbatim creates a ninja rule to write contents to a file.  The contents will be | 
|  | 189 | // escaped so that the file contains exactly the contents passed to the function. | 
|  | 190 | func WriteFileRuleVerbatim(ctx BuilderContext, outputFile WritablePath, content string) { | 
| Hans Månsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 191 | // This is MAX_ARG_STRLEN subtracted with some safety to account for shell escapes | 
|  | 192 | const SHARD_SIZE = 131072 - 10000 | 
|  | 193 |  | 
| Hans Månsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 194 | if len(content) > SHARD_SIZE { | 
|  | 195 | var chunks WritablePaths | 
|  | 196 | for i, c := range ShardString(content, SHARD_SIZE) { | 
|  | 197 | tempPath := outputFile.ReplaceExtension(ctx, fmt.Sprintf("%s.%d", outputFile.Ext(), i)) | 
|  | 198 | buildWriteFileRule(ctx, tempPath, c) | 
|  | 199 | chunks = append(chunks, tempPath) | 
|  | 200 | } | 
|  | 201 | ctx.Build(pctx, BuildParams{ | 
|  | 202 | Rule:        Cat, | 
|  | 203 | Inputs:      chunks.Paths(), | 
|  | 204 | Output:      outputFile, | 
|  | 205 | Description: "Merging to " + outputFile.Base(), | 
|  | 206 | }) | 
|  | 207 | return | 
|  | 208 | } | 
|  | 209 | buildWriteFileRule(ctx, outputFile, content) | 
|  | 210 | } | 
|  | 211 |  | 
| Cole Faust | 39b614a | 2023-08-23 16:11:26 -0700 | [diff] [blame] | 212 | // WriteExecutableFileRuleVerbatim is the same as WriteFileRuleVerbatim, but runs chmod +x on the result | 
|  | 213 | func WriteExecutableFileRuleVerbatim(ctx BuilderContext, outputFile WritablePath, content string) { | 
|  | 214 | intermediate := PathForIntermediates(ctx, "write_executable_file_intermediates").Join(ctx, outputFile.String()) | 
|  | 215 | WriteFileRuleVerbatim(ctx, intermediate, content) | 
| Rob Seymour | 925aa09 | 2021-08-10 20:42:03 +0000 | [diff] [blame] | 216 | ctx.Build(pctx, BuildParams{ | 
| Cole Faust | 39b614a | 2023-08-23 16:11:26 -0700 | [diff] [blame] | 217 | Rule:   CpExecutable, | 
|  | 218 | Output: outputFile, | 
|  | 219 | Input:  intermediate, | 
| Rob Seymour | 925aa09 | 2021-08-10 20:42:03 +0000 | [diff] [blame] | 220 | }) | 
|  | 221 | } | 
|  | 222 |  | 
| Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 223 | // shellUnescape reverses proptools.ShellEscape | 
|  | 224 | func shellUnescape(s string) string { | 
|  | 225 | // Remove leading and trailing quotes if present | 
|  | 226 | if len(s) >= 2 && s[0] == '\'' { | 
|  | 227 | s = s[1 : len(s)-1] | 
|  | 228 | } | 
|  | 229 | s = shellUnescaper.Replace(s) | 
|  | 230 | return s | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | // ContentFromFileRuleForTests returns the content that was passed to a WriteFileRule for use | 
|  | 234 | // in tests. | 
|  | 235 | func ContentFromFileRuleForTests(t *testing.T, params TestingBuildParams) string { | 
|  | 236 | t.Helper() | 
|  | 237 | if g, w := params.Rule, writeFile; g != w { | 
|  | 238 | t.Errorf("expected params.Rule to be %q, was %q", w, g) | 
|  | 239 | return "" | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | content := params.Args["content"] | 
|  | 243 | content = shellUnescape(content) | 
|  | 244 | content = echoUnescaper.Replace(content) | 
|  | 245 |  | 
|  | 246 | return content | 
|  | 247 | } | 
| Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 248 |  | 
|  | 249 | // GlobToListFileRule creates a rule that writes a list of files matching a pattern to a file. | 
|  | 250 | func GlobToListFileRule(ctx ModuleContext, pattern string, excludes []string, file WritablePath) { | 
|  | 251 | bootstrap.GlobFile(ctx.blueprintModuleContext(), pattern, excludes, file.String()) | 
|  | 252 | } |