Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 1 | // Copyright 2020 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 | |
| 15 | package remoteexec |
| 16 | |
| 17 | import ( |
| 18 | "sort" |
| 19 | "strings" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | ) |
| 25 | |
| 26 | const ( |
| 27 | // ContainerImageKey is the key identifying the container image in the platform spec. |
| 28 | ContainerImageKey = "container-image" |
| 29 | |
| 30 | // PoolKey is the key identifying the pool to use for remote execution. |
| 31 | PoolKey = "Pool" |
| 32 | |
| 33 | // DefaultImage is the default container image used for Android remote execution. The |
| 34 | // image was built with the Dockerfile at |
| 35 | // https://android.googlesource.com/platform/prebuilts/remoteexecution-client/+/refs/heads/master/docker/Dockerfile |
| 36 | DefaultImage = "docker://gcr.io/androidbuild-re-dockerimage/android-build-remoteexec-image@sha256:582efb38f0c229ea39952fff9e132ccbe183e14869b39888010dacf56b360d62" |
| 37 | |
| 38 | // DefaultWrapperPath is the default path to the remote execution wrapper. |
| 39 | DefaultWrapperPath = "prebuilts/remoteexecution-client/live/rewrapper" |
| 40 | |
| 41 | // DefaultPool is the name of the pool to use for remote execution when none is specified. |
| 42 | DefaultPool = "default" |
| 43 | |
| 44 | // LocalExecStrategy is the exec strategy to indicate that the action should be run locally. |
| 45 | LocalExecStrategy = "local" |
| 46 | |
| 47 | // RemoteExecStrategy is the exec strategy to indicate that the action should be run |
| 48 | // remotely. |
| 49 | RemoteExecStrategy = "remote" |
| 50 | |
| 51 | // RemoteLocalFallbackExecStrategy is the exec strategy to indicate that the action should |
| 52 | // be run remotely and fallback to local execution if remote fails. |
| 53 | RemoteLocalFallbackExecStrategy = "remote_local_fallback" |
| 54 | ) |
| 55 | |
| 56 | var ( |
| 57 | defaultLabels = map[string]string{"type": "tool"} |
| 58 | defaultExecStrategy = LocalExecStrategy |
| 59 | pctx = android.NewPackageContext("android/soong/remoteexec") |
| 60 | ) |
| 61 | |
| 62 | // REParams holds information pertinent to the remote execution of a rule. |
| 63 | type REParams struct { |
| 64 | // Platform is the key value pair used for remotely executing the action. |
| 65 | Platform map[string]string |
| 66 | // Labels is a map of labels that identify the rule. |
| 67 | Labels map[string]string |
| 68 | // ExecStrategy is the remote execution strategy: remote, local, or remote_local_fallback. |
| 69 | ExecStrategy string |
| 70 | // Inputs is a list of input paths or ninja variables. |
| 71 | Inputs []string |
| 72 | // RSPFile is the name of the ninja variable used by the rule as a placeholder for an rsp |
| 73 | // input. |
| 74 | RSPFile string |
| 75 | // OutputFiles is a list of output file paths or ninja variables as placeholders for rule |
| 76 | // outputs. |
| 77 | OutputFiles []string |
Ramy Medhat | c7965cd | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 78 | // OutputDirectories is a list of output directories or ninja variables as placeholders for |
| 79 | // rule output directories. |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 80 | OutputDirectories []string |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 81 | // ToolchainInputs is a list of paths or ninja variables pointing to the location of |
| 82 | // toolchain binaries used by the rule. |
| 83 | ToolchainInputs []string |
| 84 | } |
| 85 | |
| 86 | func init() { |
| 87 | pctx.VariableFunc("Wrapper", func(ctx android.PackageVarContext) string { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 88 | return wrapper(ctx.Config()) |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 89 | }) |
| 90 | } |
| 91 | |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 92 | func wrapper(cfg android.Config) string { |
| 93 | if override := cfg.Getenv("RBE_WRAPPER"); override != "" { |
| 94 | return override |
| 95 | } |
| 96 | return DefaultWrapperPath |
| 97 | } |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 98 | |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 99 | // Template generates the remote execution wrapper template to be added as a prefix to the rule's |
| 100 | // command. |
| 101 | func (r *REParams) Template() string { |
| 102 | return "${remoteexec.Wrapper}" + r.wrapperArgs() |
| 103 | } |
| 104 | |
Ramy Medhat | c7965cd | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 105 | // NoVarTemplate generates the remote execution wrapper template without variables, to be used in |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 106 | // RuleBuilder. |
| 107 | func (r *REParams) NoVarTemplate(cfg android.Config) string { |
| 108 | return wrapper(cfg) + r.wrapperArgs() |
| 109 | } |
| 110 | |
| 111 | func (r *REParams) wrapperArgs() string { |
| 112 | args := "" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 113 | var kvs []string |
| 114 | labels := r.Labels |
| 115 | if len(labels) == 0 { |
| 116 | labels = defaultLabels |
| 117 | } |
| 118 | for k, v := range labels { |
| 119 | kvs = append(kvs, k+"="+v) |
| 120 | } |
| 121 | sort.Strings(kvs) |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 122 | args += " --labels=" + strings.Join(kvs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 123 | |
| 124 | var platform []string |
| 125 | for k, v := range r.Platform { |
| 126 | if v == "" { |
| 127 | continue |
| 128 | } |
| 129 | platform = append(platform, k+"="+v) |
| 130 | } |
| 131 | if _, ok := r.Platform[ContainerImageKey]; !ok { |
| 132 | platform = append(platform, ContainerImageKey+"="+DefaultImage) |
| 133 | } |
| 134 | if platform != nil { |
| 135 | sort.Strings(platform) |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 136 | args += " --platform=\"" + strings.Join(platform, ",") + "\"" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | strategy := r.ExecStrategy |
| 140 | if strategy == "" { |
| 141 | strategy = defaultExecStrategy |
| 142 | } |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 143 | args += " --exec_strategy=" + strategy |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 144 | |
| 145 | if len(r.Inputs) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 146 | args += " --inputs=" + strings.Join(r.Inputs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if r.RSPFile != "" { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 150 | args += " --input_list_paths=" + r.RSPFile |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if len(r.OutputFiles) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 154 | args += " --output_files=" + strings.Join(r.OutputFiles, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 157 | if len(r.OutputDirectories) > 0 { |
| 158 | args += " --output_directories=" + strings.Join(r.OutputDirectories, ",") |
| 159 | } |
| 160 | |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 161 | if len(r.ToolchainInputs) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 162 | args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 165 | return args + " -- " |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // StaticRules returns a pair of rules based on the given RuleParams, where the first rule is a |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 169 | // locally executable rule and the second rule is a remotely executable rule. commonArgs are args |
| 170 | // used for both the local and remotely executable rules. reArgs are used only for remote |
| 171 | // execution. |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 172 | func StaticRules(ctx android.PackageContext, name string, ruleParams blueprint.RuleParams, reParams *REParams, commonArgs []string, reArgs []string) (blueprint.Rule, blueprint.Rule) { |
| 173 | ruleParamsRE := ruleParams |
Ramy Medhat | 31ec942 | 2020-04-17 15:03:58 -0400 | [diff] [blame] | 174 | ruleParams.Command = strings.ReplaceAll(ruleParams.Command, "$reTemplate", "") |
| 175 | ruleParamsRE.Command = strings.ReplaceAll(ruleParamsRE.Command, "$reTemplate", reParams.Template()) |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 176 | |
| 177 | return ctx.AndroidStaticRule(name, ruleParams, commonArgs...), |
| 178 | ctx.AndroidRemoteStaticRule(name+"RE", android.RemoteRuleSupports{RBE: true}, ruleParamsRE, append(commonArgs, reArgs...)...) |
| 179 | } |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 180 | |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 181 | // MultiCommandStaticRules returns a pair of rules based on the given RuleParams, where the first |
| 182 | // rule is a locally executable rule and the second rule is a remotely executable rule. This |
| 183 | // function supports multiple remote execution wrappers placed in the template when commands are |
| 184 | // chained together with &&. commonArgs are args used for both the local and remotely executable |
| 185 | // rules. reArgs are args used only for remote execution. |
| 186 | func MultiCommandStaticRules(ctx android.PackageContext, name string, ruleParams blueprint.RuleParams, reParams map[string]*REParams, commonArgs []string, reArgs []string) (blueprint.Rule, blueprint.Rule) { |
| 187 | ruleParamsRE := ruleParams |
| 188 | for k, v := range reParams { |
| 189 | ruleParams.Command = strings.ReplaceAll(ruleParams.Command, k, "") |
| 190 | ruleParamsRE.Command = strings.ReplaceAll(ruleParamsRE.Command, k, v.Template()) |
| 191 | } |
| 192 | |
| 193 | return ctx.AndroidStaticRule(name, ruleParams, commonArgs...), |
| 194 | ctx.AndroidRemoteStaticRule(name+"RE", android.RemoteRuleSupports{RBE: true}, ruleParamsRE, append(commonArgs, reArgs...)...) |
| 195 | } |
| 196 | |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 197 | // EnvOverrideFunc retrieves a variable func that evaluates to the value of the given environment |
| 198 | // variable if set, otherwise the given default. |
| 199 | func EnvOverrideFunc(envVar, defaultVal string) func(ctx android.PackageVarContext) string { |
| 200 | return func(ctx android.PackageVarContext) string { |
| 201 | if override := ctx.Config().Getenv(envVar); override != "" { |
| 202 | return override |
| 203 | } |
| 204 | return defaultVal |
| 205 | } |
| 206 | } |