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 ( |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 18 | "github.com/google/blueprint" |
Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 19 | "github.com/google/blueprint/bootstrap" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | var ( |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 23 | pctx = NewPackageContext("android/soong/android") |
| 24 | exportedVars = NewExportedVariables(pctx) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 25 | |
| 26 | cpPreserveSymlinks = pctx.VariableConfigMethod("cpPreserveSymlinks", |
| 27 | Config.CpPreserveSymlinksFlags) |
| 28 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | // A phony rule that is not the built-in Ninja phony rule. The built-in |
| 30 | // phony rule has special behavior that is sometimes not desired. See the |
| 31 | // Ninja docs for more details. |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 32 | Phony = pctx.AndroidStaticRule("Phony", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 33 | blueprint.RuleParams{ |
| 34 | Command: "# phony $out", |
| 35 | Description: "phony $out", |
| 36 | }) |
| 37 | |
| 38 | // GeneratedFile is a rule for indicating that a given file was generated |
| 39 | // while running soong. This allows the file to be cleaned up if it ever |
| 40 | // stops being generated by soong. |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 41 | GeneratedFile = pctx.AndroidStaticRule("GeneratedFile", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 42 | blueprint.RuleParams{ |
| 43 | Command: "# generated $out", |
| 44 | Description: "generated $out", |
| 45 | Generator: true, |
| 46 | }) |
| 47 | |
| 48 | // A copy rule. |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 49 | Cp = pctx.AndroidStaticRule("Cp", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 50 | blueprint.RuleParams{ |
Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 51 | Command: "rm -f $out && cp $cpPreserveSymlinks $cpFlags $in $out$extraCmds", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 52 | Description: "cp $out", |
| 53 | }, |
Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 54 | "cpFlags", "extraCmds") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 55 | |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 56 | // A copy rule that doesn't preserve symlinks. |
| 57 | CpNoPreserveSymlink = pctx.AndroidStaticRule("CpNoPreserveSymlink", |
| 58 | blueprint.RuleParams{ |
| 59 | Command: "rm -f $out && cp $cpFlags $in $out$extraCmds", |
| 60 | Description: "cp $out", |
| 61 | }, |
| 62 | "cpFlags", "extraCmds") |
| 63 | |
Colin Cross | 00d93b1 | 2021-03-04 10:00:09 -0800 | [diff] [blame] | 64 | // A copy rule that only updates the output if it changed. |
| 65 | CpIfChanged = pctx.AndroidStaticRule("CpIfChanged", |
| 66 | blueprint.RuleParams{ |
| 67 | Command: "if ! cmp -s $in $out; then cp $in $out; fi", |
| 68 | Description: "cp if changed $out", |
| 69 | Restat: true, |
Colin Cross | 31a6745 | 2023-11-02 16:57:08 -0700 | [diff] [blame] | 70 | }) |
Colin Cross | 00d93b1 | 2021-03-04 10:00:09 -0800 | [diff] [blame] | 71 | |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 72 | CpExecutable = pctx.AndroidStaticRule("CpExecutable", |
| 73 | blueprint.RuleParams{ |
Chih-Hung Hsieh | 1048a73 | 2022-08-10 20:51:37 -0700 | [diff] [blame] | 74 | Command: "rm -f $out && cp $cpFlags $in $out && chmod +x $out$extraCmds", |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 75 | Description: "cp $out", |
| 76 | }, |
Colin Cross | 50ed1f9 | 2021-11-12 17:41:02 -0800 | [diff] [blame] | 77 | "cpFlags", "extraCmds") |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 78 | |
Dan Albert | 5d723ab | 2016-07-18 22:29:52 -0700 | [diff] [blame] | 79 | // A timestamp touch rule. |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 80 | Touch = pctx.AndroidStaticRule("Touch", |
Dan Albert | 5d723ab | 2016-07-18 22:29:52 -0700 | [diff] [blame] | 81 | blueprint.RuleParams{ |
| 82 | Command: "touch $out", |
| 83 | Description: "touch $out", |
| 84 | }) |
| 85 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 86 | // A symlink rule. |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 87 | Symlink = pctx.AndroidStaticRule("Symlink", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 88 | blueprint.RuleParams{ |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame] | 89 | Command: "rm -f $out && ln -f -s $fromPath $out", |
| 90 | Description: "symlink $out", |
| 91 | SymlinkOutputs: []string{"$out"}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 92 | }, |
| 93 | "fromPath") |
Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 94 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 95 | ErrorRule = pctx.AndroidStaticRule("Error", |
Colin Cross | 6ff5138 | 2015-12-17 16:39:19 -0800 | [diff] [blame] | 96 | blueprint.RuleParams{ |
| 97 | Command: `echo "$error" && false`, |
| 98 | Description: "error building $out", |
| 99 | }, |
| 100 | "error") |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 101 | |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 102 | Cat = pctx.AndroidStaticRule("Cat", |
| 103 | blueprint.RuleParams{ |
Cole Faust | 20f2030 | 2023-08-31 11:00:25 -0700 | [diff] [blame] | 104 | Command: "rm -f $out && cat $in > $out", |
| 105 | Description: "concatenate files to $out", |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 106 | }) |
| 107 | |
Nan Zhang | 2700511 | 2017-05-12 14:02:13 -0700 | [diff] [blame] | 108 | // ubuntu 14.04 offcially use dash for /bin/sh, and its builtin echo command |
| 109 | // doesn't support -e option. Therefore we force to use /bin/bash when writing out |
| 110 | // content to file. |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 111 | writeFile = pctx.AndroidStaticRule("writeFile", |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 112 | blueprint.RuleParams{ |
Cole Faust | 20f2030 | 2023-08-31 11:00:25 -0700 | [diff] [blame] | 113 | 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] | 114 | Description: "writing file $out", |
| 115 | }, |
| 116 | "content") |
| 117 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 118 | // Used only when USE_GOMA=true is set, to restrict non-goma jobs to the local parallelism value |
| 119 | localPool = blueprint.NewBuiltinPool("local_pool") |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 120 | |
Ramy Medhat | 944839a | 2020-03-31 22:14:52 -0400 | [diff] [blame] | 121 | // Used only by RuleBuilder to identify remoteable rules. Does not actually get created in ninja. |
| 122 | remotePool = blueprint.NewBuiltinPool("remote_pool") |
| 123 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 124 | // Used for processes that need significant RAM to ensure there are not too many running in parallel. |
| 125 | highmemPool = blueprint.NewBuiltinPool("highmem_pool") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 126 | ) |
Dan Willemsen | 24f2f8d | 2015-07-15 14:34:02 -0700 | [diff] [blame] | 127 | |
| 128 | func init() { |
| 129 | pctx.Import("github.com/google/blueprint/bootstrap") |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 130 | |
| 131 | pctx.VariableFunc("RBEWrapper", func(ctx PackageVarContext) string { |
| 132 | return ctx.Config().RBEWrapper() |
| 133 | }) |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 134 | |
| 135 | exportedVars.ExportStringList("NeverAllowNotInIncludeDir", neverallowNotInIncludeDir) |
| 136 | exportedVars.ExportStringList("NeverAllowNoUseIncludeDir", neverallowNoUseIncludeDir) |
| 137 | } |
| 138 | |
Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 139 | // GlobToListFileRule creates a rule that writes a list of files matching a pattern to a file. |
| 140 | func GlobToListFileRule(ctx ModuleContext, pattern string, excludes []string, file WritablePath) { |
| 141 | bootstrap.GlobFile(ctx.blueprintModuleContext(), pattern, excludes, file.String()) |
| 142 | } |