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 |
| 78 | // ToolchainInputs is a list of paths or ninja variables pointing to the location of |
| 79 | // toolchain binaries used by the rule. |
| 80 | ToolchainInputs []string |
| 81 | } |
| 82 | |
| 83 | func init() { |
| 84 | pctx.VariableFunc("Wrapper", func(ctx android.PackageVarContext) string { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 85 | return wrapper(ctx.Config()) |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 86 | }) |
| 87 | } |
| 88 | |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 89 | func wrapper(cfg android.Config) string { |
| 90 | if override := cfg.Getenv("RBE_WRAPPER"); override != "" { |
| 91 | return override |
| 92 | } |
| 93 | return DefaultWrapperPath |
| 94 | } |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 95 | |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 96 | // Template generates the remote execution wrapper template to be added as a prefix to the rule's |
| 97 | // command. |
| 98 | func (r *REParams) Template() string { |
| 99 | return "${remoteexec.Wrapper}" + r.wrapperArgs() |
| 100 | } |
| 101 | |
| 102 | // NoVarTemplate generate the remote execution wrapper template without variables, to be used in |
| 103 | // RuleBuilder. |
| 104 | func (r *REParams) NoVarTemplate(cfg android.Config) string { |
| 105 | return wrapper(cfg) + r.wrapperArgs() |
| 106 | } |
| 107 | |
| 108 | func (r *REParams) wrapperArgs() string { |
| 109 | args := "" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 110 | var kvs []string |
| 111 | labels := r.Labels |
| 112 | if len(labels) == 0 { |
| 113 | labels = defaultLabels |
| 114 | } |
| 115 | for k, v := range labels { |
| 116 | kvs = append(kvs, k+"="+v) |
| 117 | } |
| 118 | sort.Strings(kvs) |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 119 | args += " --labels=" + strings.Join(kvs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 120 | |
| 121 | var platform []string |
| 122 | for k, v := range r.Platform { |
| 123 | if v == "" { |
| 124 | continue |
| 125 | } |
| 126 | platform = append(platform, k+"="+v) |
| 127 | } |
| 128 | if _, ok := r.Platform[ContainerImageKey]; !ok { |
| 129 | platform = append(platform, ContainerImageKey+"="+DefaultImage) |
| 130 | } |
| 131 | if platform != nil { |
| 132 | sort.Strings(platform) |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 133 | args += " --platform=\"" + strings.Join(platform, ",") + "\"" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | strategy := r.ExecStrategy |
| 137 | if strategy == "" { |
| 138 | strategy = defaultExecStrategy |
| 139 | } |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 140 | args += " --exec_strategy=" + strategy |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 141 | |
| 142 | if len(r.Inputs) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 143 | args += " --inputs=" + strings.Join(r.Inputs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if r.RSPFile != "" { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 147 | args += " --input_list_paths=" + r.RSPFile |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | if len(r.OutputFiles) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 151 | args += " --output_files=" + strings.Join(r.OutputFiles, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | if len(r.ToolchainInputs) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 155 | args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 158 | return args + " -- " |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // StaticRules returns a pair of rules based on the given RuleParams, where the first rule is a |
| 162 | // locally executable rule and the second rule is a remotely executable rule. |
| 163 | func StaticRules(ctx android.PackageContext, name string, ruleParams blueprint.RuleParams, reParams *REParams, commonArgs []string, reArgs []string) (blueprint.Rule, blueprint.Rule) { |
| 164 | ruleParamsRE := ruleParams |
Ramy Medhat | 31ec942 | 2020-04-17 15:03:58 -0400 | [diff] [blame] | 165 | ruleParams.Command = strings.ReplaceAll(ruleParams.Command, "$reTemplate", "") |
| 166 | ruleParamsRE.Command = strings.ReplaceAll(ruleParamsRE.Command, "$reTemplate", reParams.Template()) |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 167 | |
| 168 | return ctx.AndroidStaticRule(name, ruleParams, commonArgs...), |
| 169 | ctx.AndroidRemoteStaticRule(name+"RE", android.RemoteRuleSupports{RBE: true}, ruleParamsRE, append(commonArgs, reArgs...)...) |
| 170 | } |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 171 | |
| 172 | // EnvOverrideFunc retrieves a variable func that evaluates to the value of the given environment |
| 173 | // variable if set, otherwise the given default. |
| 174 | func EnvOverrideFunc(envVar, defaultVal string) func(ctx android.PackageVarContext) string { |
| 175 | return func(ctx android.PackageVarContext) string { |
| 176 | if override := ctx.Config().Getenv(envVar); override != "" { |
| 177 | return override |
| 178 | } |
| 179 | return defaultVal |
| 180 | } |
| 181 | } |