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" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | const ( |
| 23 | // ContainerImageKey is the key identifying the container image in the platform spec. |
| 24 | ContainerImageKey = "container-image" |
| 25 | |
| 26 | // PoolKey is the key identifying the pool to use for remote execution. |
| 27 | PoolKey = "Pool" |
| 28 | |
| 29 | // DefaultImage is the default container image used for Android remote execution. The |
| 30 | // image was built with the Dockerfile at |
| 31 | // https://android.googlesource.com/platform/prebuilts/remoteexecution-client/+/refs/heads/master/docker/Dockerfile |
| 32 | DefaultImage = "docker://gcr.io/androidbuild-re-dockerimage/android-build-remoteexec-image@sha256:582efb38f0c229ea39952fff9e132ccbe183e14869b39888010dacf56b360d62" |
| 33 | |
| 34 | // DefaultWrapperPath is the default path to the remote execution wrapper. |
| 35 | DefaultWrapperPath = "prebuilts/remoteexecution-client/live/rewrapper" |
| 36 | |
| 37 | // DefaultPool is the name of the pool to use for remote execution when none is specified. |
| 38 | DefaultPool = "default" |
| 39 | |
| 40 | // LocalExecStrategy is the exec strategy to indicate that the action should be run locally. |
| 41 | LocalExecStrategy = "local" |
| 42 | |
| 43 | // RemoteExecStrategy is the exec strategy to indicate that the action should be run |
| 44 | // remotely. |
| 45 | RemoteExecStrategy = "remote" |
| 46 | |
| 47 | // RemoteLocalFallbackExecStrategy is the exec strategy to indicate that the action should |
| 48 | // be run remotely and fallback to local execution if remote fails. |
| 49 | RemoteLocalFallbackExecStrategy = "remote_local_fallback" |
| 50 | ) |
| 51 | |
| 52 | var ( |
Colin Cross | 95fad7a | 2021-06-09 12:48:53 -0700 | [diff] [blame] | 53 | defaultLabels = map[string]string{"type": "tool"} |
| 54 | defaultExecStrategy = LocalExecStrategy |
| 55 | defaultEnvironmentVariables = []string{ |
| 56 | // This is a subset of the allowlist in ui/build/ninja.go that makes sense remotely. |
| 57 | "LANG", |
| 58 | "LC_MESSAGES", |
| 59 | "PYTHONDONTWRITEBYTECODE", |
| 60 | } |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 61 | ) |
| 62 | |
| 63 | // REParams holds information pertinent to the remote execution of a rule. |
| 64 | type REParams struct { |
| 65 | // Platform is the key value pair used for remotely executing the action. |
| 66 | Platform map[string]string |
| 67 | // Labels is a map of labels that identify the rule. |
| 68 | Labels map[string]string |
| 69 | // ExecStrategy is the remote execution strategy: remote, local, or remote_local_fallback. |
| 70 | ExecStrategy string |
| 71 | // Inputs is a list of input paths or ninja variables. |
| 72 | Inputs []string |
Colin Cross | a4eafdd | 2021-03-24 14:09:28 -0700 | [diff] [blame] | 73 | // RSPFiles is the name of the files used by the rule as a placeholder for an rsp input. |
| 74 | RSPFiles []string |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 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 |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 84 | // EnvironmentVariables is a list of environment variables whose values should be passed through |
| 85 | // to the remote execution. |
| 86 | EnvironmentVariables []string |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | func init() { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 90 | } |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 91 | |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 92 | // Template generates the remote execution wrapper template to be added as a prefix to the rule's |
| 93 | // command. |
| 94 | func (r *REParams) Template() string { |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 95 | return "${android.RBEWrapper}" + r.wrapperArgs() |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Ramy Medhat | c7965cd | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 98 | // 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] | 99 | // RuleBuilder. |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 100 | func (r *REParams) NoVarTemplate(wrapper string) string { |
| 101 | return wrapper + r.wrapperArgs() |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | func (r *REParams) wrapperArgs() string { |
| 105 | args := "" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 106 | var kvs []string |
| 107 | labels := r.Labels |
| 108 | if len(labels) == 0 { |
| 109 | labels = defaultLabels |
| 110 | } |
| 111 | for k, v := range labels { |
| 112 | kvs = append(kvs, k+"="+v) |
| 113 | } |
| 114 | sort.Strings(kvs) |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 115 | args += " --labels=" + strings.Join(kvs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 116 | |
| 117 | var platform []string |
| 118 | for k, v := range r.Platform { |
| 119 | if v == "" { |
| 120 | continue |
| 121 | } |
| 122 | platform = append(platform, k+"="+v) |
| 123 | } |
| 124 | if _, ok := r.Platform[ContainerImageKey]; !ok { |
| 125 | platform = append(platform, ContainerImageKey+"="+DefaultImage) |
| 126 | } |
| 127 | if platform != nil { |
| 128 | sort.Strings(platform) |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 129 | args += " --platform=\"" + strings.Join(platform, ",") + "\"" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | strategy := r.ExecStrategy |
| 133 | if strategy == "" { |
| 134 | strategy = defaultExecStrategy |
| 135 | } |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 136 | args += " --exec_strategy=" + strategy |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 137 | |
| 138 | if len(r.Inputs) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 139 | args += " --inputs=" + strings.Join(r.Inputs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Colin Cross | a4eafdd | 2021-03-24 14:09:28 -0700 | [diff] [blame] | 142 | if len(r.RSPFiles) > 0 { |
| 143 | args += " --input_list_paths=" + strings.Join(r.RSPFiles, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if len(r.OutputFiles) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 147 | args += " --output_files=" + strings.Join(r.OutputFiles, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 150 | if len(r.OutputDirectories) > 0 { |
| 151 | args += " --output_directories=" + strings.Join(r.OutputDirectories, ",") |
| 152 | } |
| 153 | |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 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 | |
Colin Cross | 95fad7a | 2021-06-09 12:48:53 -0700 | [diff] [blame] | 158 | envVarAllowlist := append(r.EnvironmentVariables, defaultEnvironmentVariables...) |
| 159 | |
| 160 | if len(envVarAllowlist) > 0 { |
| 161 | args += " --env_var_allowlist=" + strings.Join(envVarAllowlist, ",") |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 164 | return args + " -- " |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 165 | } |