• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	"sort"
19	"strings"
20)
21
22const (
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
52var (
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	}
61)
62
63// REParams holds information pertinent to the remote execution of a rule.
64type 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
73	// RSPFiles is the name of the files used by the rule as a placeholder for an rsp input.
74	RSPFiles []string
75	// OutputFiles is a list of output file paths or ninja variables as placeholders for rule
76	// outputs.
77	OutputFiles []string
78	// OutputDirectories is a list of output directories or ninja variables as placeholders for
79	// rule output directories.
80	OutputDirectories []string
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
84	// EnvironmentVariables is a list of environment variables whose values should be passed through
85	// to the remote execution.
86	EnvironmentVariables []string
87}
88
89func init() {
90}
91
92// Template generates the remote execution wrapper template to be added as a prefix to the rule's
93// command.
94func (r *REParams) Template() string {
95	return "${android.RBEWrapper}" + r.wrapperArgs()
96}
97
98// NoVarTemplate generates the remote execution wrapper template without variables, to be used in
99// RuleBuilder.
100func (r *REParams) NoVarTemplate(wrapper string) string {
101	return wrapper + r.wrapperArgs()
102}
103
104func (r *REParams) wrapperArgs() string {
105	args := ""
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)
115	args += " --labels=" + strings.Join(kvs, ",")
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)
129		args += " --platform=\"" + strings.Join(platform, ",") + "\""
130	}
131
132	strategy := r.ExecStrategy
133	if strategy == "" {
134		strategy = defaultExecStrategy
135	}
136	args += " --exec_strategy=" + strategy
137
138	if len(r.Inputs) > 0 {
139		args += " --inputs=" + strings.Join(r.Inputs, ",")
140	}
141
142	if len(r.RSPFiles) > 0 {
143		args += " --input_list_paths=" + strings.Join(r.RSPFiles, ",")
144	}
145
146	if len(r.OutputFiles) > 0 {
147		args += " --output_files=" + strings.Join(r.OutputFiles, ",")
148	}
149
150	if len(r.OutputDirectories) > 0 {
151		args += " --output_directories=" + strings.Join(r.OutputDirectories, ",")
152	}
153
154	if len(r.ToolchainInputs) > 0 {
155		args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",")
156	}
157
158	envVarAllowlist := append(r.EnvironmentVariables, defaultEnvironmentVariables...)
159
160	if len(envVarAllowlist) > 0 {
161		args += " --env_var_allowlist=" + strings.Join(envVarAllowlist, ",")
162	}
163
164	return args + " -- "
165}
166