blob: ef4672a73011ed7cd9379e66dafeb8f492989807 [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
Colin Crossa4eafdd2021-03-24 14:09:28 -070067 // RSPFiles is the name of the files used by the rule as a placeholder for an rsp input.
68 RSPFiles []string
Ramy Medhat9a90fe52020-04-13 13:21:23 -040069 // OutputFiles is a list of output file paths or ninja variables as placeholders for rule
70 // outputs.
71 OutputFiles []string
Ramy Medhatc7965cd2020-04-30 03:08:37 -040072 // OutputDirectories is a list of output directories or ninja variables as placeholders for
73 // rule output directories.
Kousik Kumar1372c1b2020-05-20 07:55:56 -070074 OutputDirectories []string
Ramy Medhat9a90fe52020-04-13 13:21:23 -040075 // ToolchainInputs is a list of paths or ninja variables pointing to the location of
76 // toolchain binaries used by the rule.
77 ToolchainInputs []string
Colin Cross31972dc2021-03-04 10:44:12 -080078 // EnvironmentVariables is a list of environment variables whose values should be passed through
79 // to the remote execution.
80 EnvironmentVariables []string
Ramy Medhat9a90fe52020-04-13 13:21:23 -040081}
82
83func init() {
Ramy Medhat427683c2020-04-30 03:08:37 -040084}
Ramy Medhat9a90fe52020-04-13 13:21:23 -040085
Ramy Medhat427683c2020-04-30 03:08:37 -040086// Template generates the remote execution wrapper template to be added as a prefix to the rule's
87// command.
88func (r *REParams) Template() string {
Colin Cross77cdcfd2021-03-12 11:28:25 -080089 return "${android.RBEWrapper}" + r.wrapperArgs()
Ramy Medhat427683c2020-04-30 03:08:37 -040090}
91
Ramy Medhatc7965cd2020-04-30 03:08:37 -040092// NoVarTemplate generates the remote execution wrapper template without variables, to be used in
Ramy Medhat427683c2020-04-30 03:08:37 -040093// RuleBuilder.
Colin Cross77cdcfd2021-03-12 11:28:25 -080094func (r *REParams) NoVarTemplate(wrapper string) string {
95 return wrapper + r.wrapperArgs()
Ramy Medhat427683c2020-04-30 03:08:37 -040096}
97
98func (r *REParams) wrapperArgs() string {
99 args := ""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400100 var kvs []string
101 labels := r.Labels
102 if len(labels) == 0 {
103 labels = defaultLabels
104 }
105 for k, v := range labels {
106 kvs = append(kvs, k+"="+v)
107 }
108 sort.Strings(kvs)
Ramy Medhat427683c2020-04-30 03:08:37 -0400109 args += " --labels=" + strings.Join(kvs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400110
111 var platform []string
112 for k, v := range r.Platform {
113 if v == "" {
114 continue
115 }
116 platform = append(platform, k+"="+v)
117 }
118 if _, ok := r.Platform[ContainerImageKey]; !ok {
119 platform = append(platform, ContainerImageKey+"="+DefaultImage)
120 }
121 if platform != nil {
122 sort.Strings(platform)
Ramy Medhat427683c2020-04-30 03:08:37 -0400123 args += " --platform=\"" + strings.Join(platform, ",") + "\""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400124 }
125
126 strategy := r.ExecStrategy
127 if strategy == "" {
128 strategy = defaultExecStrategy
129 }
Ramy Medhat427683c2020-04-30 03:08:37 -0400130 args += " --exec_strategy=" + strategy
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400131
132 if len(r.Inputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400133 args += " --inputs=" + strings.Join(r.Inputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400134 }
135
Colin Crossa4eafdd2021-03-24 14:09:28 -0700136 if len(r.RSPFiles) > 0 {
137 args += " --input_list_paths=" + strings.Join(r.RSPFiles, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400138 }
139
140 if len(r.OutputFiles) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400141 args += " --output_files=" + strings.Join(r.OutputFiles, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400142 }
143
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700144 if len(r.OutputDirectories) > 0 {
145 args += " --output_directories=" + strings.Join(r.OutputDirectories, ",")
146 }
147
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400148 if len(r.ToolchainInputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400149 args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400150 }
151
Colin Cross31972dc2021-03-04 10:44:12 -0800152 if len(r.EnvironmentVariables) > 0 {
153 args += " --env_var_allowlist=" + strings.Join(r.EnvironmentVariables, ",")
154 }
155
Ramy Medhat427683c2020-04-30 03:08:37 -0400156 return args + " -- "
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400157}