blob: 9e7a0f1e30c1bc759a39d2918b81edaa3e11eb7f [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 (
18 "sort"
19 "strings"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040020)
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 (
Colin Cross95fad7a2021-06-09 12:48:53 -070053 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 Medhat9a90fe52020-04-13 13:21:23 -040061)
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
Colin Crossa4eafdd2021-03-24 14:09:28 -070073 // RSPFiles is the name of the files used by the rule as a placeholder for an rsp input.
74 RSPFiles []string
Ramy Medhat9a90fe52020-04-13 13:21:23 -040075 // OutputFiles is a list of output file paths or ninja variables as placeholders for rule
76 // outputs.
77 OutputFiles []string
Ramy Medhatc7965cd2020-04-30 03:08:37 -040078 // OutputDirectories is a list of output directories or ninja variables as placeholders for
79 // rule output directories.
Kousik Kumar1372c1b2020-05-20 07:55:56 -070080 OutputDirectories []string
Ramy Medhat9a90fe52020-04-13 13:21:23 -040081 // 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 Cross31972dc2021-03-04 10:44:12 -080084 // EnvironmentVariables is a list of environment variables whose values should be passed through
85 // to the remote execution.
86 EnvironmentVariables []string
Ramy Medhat9a90fe52020-04-13 13:21:23 -040087}
88
89func init() {
Ramy Medhat427683c2020-04-30 03:08:37 -040090}
Ramy Medhat9a90fe52020-04-13 13:21:23 -040091
Ramy Medhat427683c2020-04-30 03:08:37 -040092// Template generates the remote execution wrapper template to be added as a prefix to the rule's
93// command.
94func (r *REParams) Template() string {
Colin Cross77cdcfd2021-03-12 11:28:25 -080095 return "${android.RBEWrapper}" + r.wrapperArgs()
Ramy Medhat427683c2020-04-30 03:08:37 -040096}
97
Ramy Medhatc7965cd2020-04-30 03:08:37 -040098// NoVarTemplate generates the remote execution wrapper template without variables, to be used in
Ramy Medhat427683c2020-04-30 03:08:37 -040099// RuleBuilder.
Colin Cross77cdcfd2021-03-12 11:28:25 -0800100func (r *REParams) NoVarTemplate(wrapper string) string {
101 return wrapper + r.wrapperArgs()
Ramy Medhat427683c2020-04-30 03:08:37 -0400102}
103
104func (r *REParams) wrapperArgs() string {
105 args := ""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400106 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 Medhat427683c2020-04-30 03:08:37 -0400115 args += " --labels=" + strings.Join(kvs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400116
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 Medhat427683c2020-04-30 03:08:37 -0400129 args += " --platform=\"" + strings.Join(platform, ",") + "\""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400130 }
131
132 strategy := r.ExecStrategy
133 if strategy == "" {
134 strategy = defaultExecStrategy
135 }
Ramy Medhat427683c2020-04-30 03:08:37 -0400136 args += " --exec_strategy=" + strategy
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400137
138 if len(r.Inputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400139 args += " --inputs=" + strings.Join(r.Inputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400140 }
141
Colin Crossa4eafdd2021-03-24 14:09:28 -0700142 if len(r.RSPFiles) > 0 {
143 args += " --input_list_paths=" + strings.Join(r.RSPFiles, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400144 }
145
146 if len(r.OutputFiles) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400147 args += " --output_files=" + strings.Join(r.OutputFiles, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400148 }
149
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700150 if len(r.OutputDirectories) > 0 {
151 args += " --output_directories=" + strings.Join(r.OutputDirectories, ",")
152 }
153
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400154 if len(r.ToolchainInputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400155 args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400156 }
157
Colin Cross95fad7a2021-06-09 12:48:53 -0700158 envVarAllowlist := append(r.EnvironmentVariables, defaultEnvironmentVariables...)
159
160 if len(envVarAllowlist) > 0 {
161 args += " --env_var_allowlist=" + strings.Join(envVarAllowlist, ",")
Colin Cross31972dc2021-03-04 10:44:12 -0800162 }
163
Ramy Medhat427683c2020-04-30 03:08:37 -0400164 return args + " -- "
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400165}