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
15package remoteexec
16
17import (
18	"fmt"
19	"sort"
20	"strings"
21)
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:1eb7f64b9e17102b970bd7a1af7daaebdb01c3fb777715899ef462d6c6d01a45"
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 (
54	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	}
62)
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
74	// RSPFiles is the name of the files used by the rule as a placeholder for an rsp input.
75	RSPFiles []string
76	// OutputFiles is a list of output file paths or ninja variables as placeholders for rule
77	// outputs.
78	OutputFiles []string
79	// OutputDirectories is a list of output directories or ninja variables as placeholders for
80	// rule output directories.
81	OutputDirectories []string
82	// ToolchainInputs is a list of paths or ninja variables pointing to the location of
83	// toolchain binaries used by the rule.
84	ToolchainInputs []string
85	// EnvironmentVariables is a list of environment variables whose values should be passed through
86	// to the remote execution.
87	EnvironmentVariables []string
88	// 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
94	// Boolean indicating whether to update remote cache entry. Rewrapper defaults to true, so the name is negated here.
95	NoRemoteUpdateCache bool
96}
97
98func init() {
99}
100
101// Template generates the remote execution wrapper template to be added as a prefix to the rule's
102// command.
103func (r *REParams) Template() string {
104	return "${android.RBEWrapper}" + r.wrapperArgs()
105}
106
107// NoVarTemplate generates the remote execution wrapper template without variables, to be used in
108// RuleBuilder.
109func (r *REParams) NoVarTemplate(wrapper string) string {
110	return wrapper + r.wrapperArgs()
111}
112
113func (r *REParams) wrapperArgs() string {
114	args := ""
115	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)
124	args += " --labels=" + strings.Join(kvs, ",")
125
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)
138		args += " --platform=\"" + strings.Join(platform, ",") + "\""
139	}
140
141	strategy := r.ExecStrategy
142	if strategy == "" {
143		strategy = defaultExecStrategy
144	}
145	args += " --exec_strategy=" + strategy
146
147	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
151	if r.NoRemoteUpdateCache {
152		args += " --remote_update_cache=false"
153	}
154
155	if len(r.Inputs) > 0 {
156		args += " --inputs=" + strings.Join(r.Inputs, ",")
157	}
158
159	if len(r.RSPFiles) > 0 {
160		args += " --input_list_paths=" + strings.Join(r.RSPFiles, ",")
161	}
162
163	if len(r.OutputFiles) > 0 {
164		args += " --output_files=" + strings.Join(r.OutputFiles, ",")
165	}
166
167	if len(r.OutputDirectories) > 0 {
168		args += " --output_directories=" + strings.Join(r.OutputDirectories, ",")
169	}
170
171	if len(r.ToolchainInputs) > 0 {
172		args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",")
173	}
174
175	envVarAllowlist := append(r.EnvironmentVariables, defaultEnvironmentVariables...)
176
177	if len(envVarAllowlist) > 0 {
178		args += " --env_var_allowlist=" + strings.Join(envVarAllowlist, ",")
179	}
180
181	return args + " -- "
182}
183