blob: 166f68c545990714f4a7a347be5999a5717eb5fb [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 (
53 defaultLabels = map[string]string{"type": "tool"}
54 defaultExecStrategy = LocalExecStrategy
Ramy Medhat9a90fe52020-04-13 13:21:23 -040055)
56
57// REParams holds information pertinent to the remote execution of a rule.
58type REParams struct {
59 // Platform is the key value pair used for remotely executing the action.
60 Platform map[string]string
61 // Labels is a map of labels that identify the rule.
62 Labels map[string]string
63 // ExecStrategy is the remote execution strategy: remote, local, or remote_local_fallback.
64 ExecStrategy string
65 // Inputs is a list of input paths or ninja variables.
66 Inputs []string
67 // RSPFile is the name of the ninja variable used by the rule as a placeholder for an rsp
68 // input.
69 RSPFile string
70 // OutputFiles is a list of output file paths or ninja variables as placeholders for rule
71 // outputs.
72 OutputFiles []string
Ramy Medhatc7965cd2020-04-30 03:08:37 -040073 // OutputDirectories is a list of output directories or ninja variables as placeholders for
74 // rule output directories.
Kousik Kumar1372c1b2020-05-20 07:55:56 -070075 OutputDirectories []string
Ramy Medhat9a90fe52020-04-13 13:21:23 -040076 // ToolchainInputs is a list of paths or ninja variables pointing to the location of
77 // toolchain binaries used by the rule.
78 ToolchainInputs []string
Colin Cross31972dc2021-03-04 10:44:12 -080079 // EnvironmentVariables is a list of environment variables whose values should be passed through
80 // to the remote execution.
81 EnvironmentVariables []string
Ramy Medhat9a90fe52020-04-13 13:21:23 -040082}
83
84func init() {
Ramy Medhat427683c2020-04-30 03:08:37 -040085}
Ramy Medhat9a90fe52020-04-13 13:21:23 -040086
Ramy Medhat427683c2020-04-30 03:08:37 -040087// Template generates the remote execution wrapper template to be added as a prefix to the rule's
88// command.
89func (r *REParams) Template() string {
Colin Cross77cdcfd2021-03-12 11:28:25 -080090 return "${android.RBEWrapper}" + r.wrapperArgs()
Ramy Medhat427683c2020-04-30 03:08:37 -040091}
92
Ramy Medhatc7965cd2020-04-30 03:08:37 -040093// NoVarTemplate generates the remote execution wrapper template without variables, to be used in
Ramy Medhat427683c2020-04-30 03:08:37 -040094// RuleBuilder.
Colin Cross77cdcfd2021-03-12 11:28:25 -080095func (r *REParams) NoVarTemplate(wrapper string) string {
96 return wrapper + r.wrapperArgs()
Ramy Medhat427683c2020-04-30 03:08:37 -040097}
98
99func (r *REParams) wrapperArgs() string {
100 args := ""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400101 var kvs []string
102 labels := r.Labels
103 if len(labels) == 0 {
104 labels = defaultLabels
105 }
106 for k, v := range labels {
107 kvs = append(kvs, k+"="+v)
108 }
109 sort.Strings(kvs)
Ramy Medhat427683c2020-04-30 03:08:37 -0400110 args += " --labels=" + strings.Join(kvs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400111
112 var platform []string
113 for k, v := range r.Platform {
114 if v == "" {
115 continue
116 }
117 platform = append(platform, k+"="+v)
118 }
119 if _, ok := r.Platform[ContainerImageKey]; !ok {
120 platform = append(platform, ContainerImageKey+"="+DefaultImage)
121 }
122 if platform != nil {
123 sort.Strings(platform)
Ramy Medhat427683c2020-04-30 03:08:37 -0400124 args += " --platform=\"" + strings.Join(platform, ",") + "\""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400125 }
126
127 strategy := r.ExecStrategy
128 if strategy == "" {
129 strategy = defaultExecStrategy
130 }
Ramy Medhat427683c2020-04-30 03:08:37 -0400131 args += " --exec_strategy=" + strategy
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400132
133 if len(r.Inputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400134 args += " --inputs=" + strings.Join(r.Inputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400135 }
136
137 if r.RSPFile != "" {
Ramy Medhat427683c2020-04-30 03:08:37 -0400138 args += " --input_list_paths=" + r.RSPFile
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400139 }
140
141 if len(r.OutputFiles) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400142 args += " --output_files=" + strings.Join(r.OutputFiles, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400143 }
144
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700145 if len(r.OutputDirectories) > 0 {
146 args += " --output_directories=" + strings.Join(r.OutputDirectories, ",")
147 }
148
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400149 if len(r.ToolchainInputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400150 args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400151 }
152
Colin Cross31972dc2021-03-04 10:44:12 -0800153 if len(r.EnvironmentVariables) > 0 {
154 args += " --env_var_allowlist=" + strings.Join(r.EnvironmentVariables, ",")
155 }
156
Ramy Medhat427683c2020-04-30 03:08:37 -0400157 return args + " -- "
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400158}