blob: 150d62c04656d753a59c5dc3c309808ce35095e3 [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
33 DefaultImage = "docker://gcr.io/androidbuild-re-dockerimage/android-build-remoteexec-image@sha256:582efb38f0c229ea39952fff9e132ccbe183e14869b39888010dacf56b360d62"
34
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
Ramy Medhat9a90fe52020-04-13 13:21:23 -040094}
95
96func init() {
Ramy Medhat427683c2020-04-30 03:08:37 -040097}
Ramy Medhat9a90fe52020-04-13 13:21:23 -040098
Ramy Medhat427683c2020-04-30 03:08:37 -040099// Template generates the remote execution wrapper template to be added as a prefix to the rule's
100// command.
101func (r *REParams) Template() string {
Colin Cross77cdcfd2021-03-12 11:28:25 -0800102 return "${android.RBEWrapper}" + r.wrapperArgs()
Ramy Medhat427683c2020-04-30 03:08:37 -0400103}
104
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400105// NoVarTemplate generates the remote execution wrapper template without variables, to be used in
Ramy Medhat427683c2020-04-30 03:08:37 -0400106// RuleBuilder.
Colin Cross77cdcfd2021-03-12 11:28:25 -0800107func (r *REParams) NoVarTemplate(wrapper string) string {
108 return wrapper + r.wrapperArgs()
Ramy Medhat427683c2020-04-30 03:08:37 -0400109}
110
111func (r *REParams) wrapperArgs() string {
112 args := ""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400113 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 Medhat427683c2020-04-30 03:08:37 -0400122 args += " --labels=" + strings.Join(kvs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400123
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 Medhat427683c2020-04-30 03:08:37 -0400136 args += " --platform=\"" + strings.Join(platform, ",") + "\""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400137 }
138
139 strategy := r.ExecStrategy
140 if strategy == "" {
141 strategy = defaultExecStrategy
142 }
Ramy Medhat427683c2020-04-30 03:08:37 -0400143 args += " --exec_strategy=" + strategy
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400144
Anas Sulaiman9c493642023-10-18 16:34:37 +0000145 if r.Compare && r.NumLocalRuns >= 0 && r.NumRemoteRuns >= 0 {
146 args += fmt.Sprintf(" --compare=true --num_local_reruns=%d --num_remote_reruns=%d", r.NumLocalRuns, r.NumRemoteRuns)
147 }
148
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400149 if len(r.Inputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400150 args += " --inputs=" + strings.Join(r.Inputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400151 }
152
Colin Crossa4eafdd2021-03-24 14:09:28 -0700153 if len(r.RSPFiles) > 0 {
154 args += " --input_list_paths=" + strings.Join(r.RSPFiles, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400155 }
156
157 if len(r.OutputFiles) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400158 args += " --output_files=" + strings.Join(r.OutputFiles, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400159 }
160
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700161 if len(r.OutputDirectories) > 0 {
162 args += " --output_directories=" + strings.Join(r.OutputDirectories, ",")
163 }
164
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400165 if len(r.ToolchainInputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400166 args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400167 }
168
Colin Cross95fad7a2021-06-09 12:48:53 -0700169 envVarAllowlist := append(r.EnvironmentVariables, defaultEnvironmentVariables...)
170
171 if len(envVarAllowlist) > 0 {
172 args += " --env_var_allowlist=" + strings.Join(envVarAllowlist, ",")
Colin Cross31972dc2021-03-04 10:44:12 -0800173 }
174
Ramy Medhat427683c2020-04-30 03:08:37 -0400175 return args + " -- "
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400176}