blob: 8294c3fa36596ff3c1852e7005af469d5f279f08 [file] [log] [blame]
Ramy Medhat9a90fe52020-04-13 13:21:23 -04001// 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
15package remoteexec
16
17import (
Anas Sulaiman9c493642023-10-18 16:34:37 +000018 "fmt"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040019 "sort"
20 "strings"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040021)
22
23const (
24 // ContainerImageKey is the key identifying the container image in the platform spec.
25 ContainerImageKey = "container-image"
26
27 // PoolKey is the key identifying the pool to use for remote execution.
28 PoolKey = "Pool"
29
30 // DefaultImage is the default container image used for Android remote execution. The
31 // image was built with the Dockerfile at
32 // https://android.googlesource.com/platform/prebuilts/remoteexecution-client/+/refs/heads/master/docker/Dockerfile
Anas Sulaiman3f465a42023-11-15 20:26:34 +000033 DefaultImage = "docker://gcr.io/androidbuild-re-dockerimage/android-build-remoteexec-image@sha256:1eb7f64b9e17102b970bd7a1af7daaebdb01c3fb777715899ef462d6c6d01a45"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040034
35 // DefaultWrapperPath is the default path to the remote execution wrapper.
36 DefaultWrapperPath = "prebuilts/remoteexecution-client/live/rewrapper"
37
38 // DefaultPool is the name of the pool to use for remote execution when none is specified.
39 DefaultPool = "default"
40
41 // LocalExecStrategy is the exec strategy to indicate that the action should be run locally.
42 LocalExecStrategy = "local"
43
44 // RemoteExecStrategy is the exec strategy to indicate that the action should be run
45 // remotely.
46 RemoteExecStrategy = "remote"
47
48 // RemoteLocalFallbackExecStrategy is the exec strategy to indicate that the action should
49 // be run remotely and fallback to local execution if remote fails.
50 RemoteLocalFallbackExecStrategy = "remote_local_fallback"
51)
52
53var (
Colin Cross95fad7a2021-06-09 12:48:53 -070054 defaultLabels = map[string]string{"type": "tool"}
55 defaultExecStrategy = LocalExecStrategy
56 defaultEnvironmentVariables = []string{
57 // This is a subset of the allowlist in ui/build/ninja.go that makes sense remotely.
58 "LANG",
59 "LC_MESSAGES",
60 "PYTHONDONTWRITEBYTECODE",
61 }
Ramy Medhat9a90fe52020-04-13 13:21:23 -040062)
63
64// REParams holds information pertinent to the remote execution of a rule.
65type REParams struct {
66 // Platform is the key value pair used for remotely executing the action.
67 Platform map[string]string
68 // Labels is a map of labels that identify the rule.
69 Labels map[string]string
70 // ExecStrategy is the remote execution strategy: remote, local, or remote_local_fallback.
71 ExecStrategy string
72 // Inputs is a list of input paths or ninja variables.
73 Inputs []string
Colin Crossa4eafdd2021-03-24 14:09:28 -070074 // RSPFiles is the name of the files used by the rule as a placeholder for an rsp input.
75 RSPFiles []string
Ramy Medhat9a90fe52020-04-13 13:21:23 -040076 // OutputFiles is a list of output file paths or ninja variables as placeholders for rule
77 // outputs.
78 OutputFiles []string
Ramy Medhatc7965cd2020-04-30 03:08:37 -040079 // OutputDirectories is a list of output directories or ninja variables as placeholders for
80 // rule output directories.
Kousik Kumar1372c1b2020-05-20 07:55:56 -070081 OutputDirectories []string
Ramy Medhat9a90fe52020-04-13 13:21:23 -040082 // ToolchainInputs is a list of paths or ninja variables pointing to the location of
83 // toolchain binaries used by the rule.
84 ToolchainInputs []string
Colin Cross31972dc2021-03-04 10:44:12 -080085 // EnvironmentVariables is a list of environment variables whose values should be passed through
86 // to the remote execution.
87 EnvironmentVariables []string
Anas Sulaiman9c493642023-10-18 16:34:37 +000088 // Boolean indicating whether to compare chosen exec strategy with local execution.
89 Compare bool
90 // Number of times the action should be rerun locally.
91 NumLocalRuns int
92 // Number of times the action should be rerun remotely.
93 NumRemoteRuns int
Anas Sulaiman9d7a36d2023-11-21 23:00:07 +000094 // Boolean indicating whether to update remote cache entry. Rewrapper defaults to true, so the name is negated here.
95 NoRemoteUpdateCache bool
Ramy Medhat9a90fe52020-04-13 13:21:23 -040096}
97
98func init() {
Ramy Medhat427683c2020-04-30 03:08:37 -040099}
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400100
Ramy Medhat427683c2020-04-30 03:08:37 -0400101// Template generates the remote execution wrapper template to be added as a prefix to the rule's
102// command.
103func (r *REParams) Template() string {
Colin Cross77cdcfd2021-03-12 11:28:25 -0800104 return "${android.RBEWrapper}" + r.wrapperArgs()
Ramy Medhat427683c2020-04-30 03:08:37 -0400105}
106
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400107// NoVarTemplate generates the remote execution wrapper template without variables, to be used in
Ramy Medhat427683c2020-04-30 03:08:37 -0400108// RuleBuilder.
Colin Cross77cdcfd2021-03-12 11:28:25 -0800109func (r *REParams) NoVarTemplate(wrapper string) string {
110 return wrapper + r.wrapperArgs()
Ramy Medhat427683c2020-04-30 03:08:37 -0400111}
112
113func (r *REParams) wrapperArgs() string {
114 args := ""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400115 var kvs []string
116 labels := r.Labels
117 if len(labels) == 0 {
118 labels = defaultLabels
119 }
120 for k, v := range labels {
121 kvs = append(kvs, k+"="+v)
122 }
123 sort.Strings(kvs)
Ramy Medhat427683c2020-04-30 03:08:37 -0400124 args += " --labels=" + strings.Join(kvs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400125
126 var platform []string
127 for k, v := range r.Platform {
128 if v == "" {
129 continue
130 }
131 platform = append(platform, k+"="+v)
132 }
133 if _, ok := r.Platform[ContainerImageKey]; !ok {
134 platform = append(platform, ContainerImageKey+"="+DefaultImage)
135 }
136 if platform != nil {
137 sort.Strings(platform)
Ramy Medhat427683c2020-04-30 03:08:37 -0400138 args += " --platform=\"" + strings.Join(platform, ",") + "\""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400139 }
140
141 strategy := r.ExecStrategy
142 if strategy == "" {
143 strategy = defaultExecStrategy
144 }
Ramy Medhat427683c2020-04-30 03:08:37 -0400145 args += " --exec_strategy=" + strategy
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400146
Anas Sulaiman9c493642023-10-18 16:34:37 +0000147 if r.Compare && r.NumLocalRuns >= 0 && r.NumRemoteRuns >= 0 {
148 args += fmt.Sprintf(" --compare=true --num_local_reruns=%d --num_remote_reruns=%d", r.NumLocalRuns, r.NumRemoteRuns)
149 }
150
Anas Sulaiman9d7a36d2023-11-21 23:00:07 +0000151 if r.NoRemoteUpdateCache {
152 args += " --remote_update_cache=false"
153 }
154
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400155 if len(r.Inputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400156 args += " --inputs=" + strings.Join(r.Inputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400157 }
158
Colin Crossa4eafdd2021-03-24 14:09:28 -0700159 if len(r.RSPFiles) > 0 {
160 args += " --input_list_paths=" + strings.Join(r.RSPFiles, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400161 }
162
163 if len(r.OutputFiles) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400164 args += " --output_files=" + strings.Join(r.OutputFiles, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400165 }
166
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700167 if len(r.OutputDirectories) > 0 {
168 args += " --output_directories=" + strings.Join(r.OutputDirectories, ",")
169 }
170
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400171 if len(r.ToolchainInputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400172 args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400173 }
174
Colin Cross95fad7a2021-06-09 12:48:53 -0700175 envVarAllowlist := append(r.EnvironmentVariables, defaultEnvironmentVariables...)
176
177 if len(envVarAllowlist) > 0 {
178 args += " --env_var_allowlist=" + strings.Join(envVarAllowlist, ",")
Colin Cross31972dc2021-03-04 10:44:12 -0800179 }
180
Ramy Medhat427683c2020-04-30 03:08:37 -0400181 return args + " -- "
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400182}