blob: 5f0426a7f1e30bb076e4bb44d707400656fff92c [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"
20
21 "android/soong/android"
22
23 "github.com/google/blueprint"
24)
25
26const (
27 // ContainerImageKey is the key identifying the container image in the platform spec.
28 ContainerImageKey = "container-image"
29
30 // PoolKey is the key identifying the pool to use for remote execution.
31 PoolKey = "Pool"
32
33 // DefaultImage is the default container image used for Android remote execution. The
34 // image was built with the Dockerfile at
35 // https://android.googlesource.com/platform/prebuilts/remoteexecution-client/+/refs/heads/master/docker/Dockerfile
36 DefaultImage = "docker://gcr.io/androidbuild-re-dockerimage/android-build-remoteexec-image@sha256:582efb38f0c229ea39952fff9e132ccbe183e14869b39888010dacf56b360d62"
37
38 // DefaultWrapperPath is the default path to the remote execution wrapper.
39 DefaultWrapperPath = "prebuilts/remoteexecution-client/live/rewrapper"
40
41 // DefaultPool is the name of the pool to use for remote execution when none is specified.
42 DefaultPool = "default"
43
44 // LocalExecStrategy is the exec strategy to indicate that the action should be run locally.
45 LocalExecStrategy = "local"
46
47 // RemoteExecStrategy is the exec strategy to indicate that the action should be run
48 // remotely.
49 RemoteExecStrategy = "remote"
50
51 // RemoteLocalFallbackExecStrategy is the exec strategy to indicate that the action should
52 // be run remotely and fallback to local execution if remote fails.
53 RemoteLocalFallbackExecStrategy = "remote_local_fallback"
54)
55
56var (
57 defaultLabels = map[string]string{"type": "tool"}
58 defaultExecStrategy = LocalExecStrategy
59 pctx = android.NewPackageContext("android/soong/remoteexec")
60)
61
62// REParams holds information pertinent to the remote execution of a rule.
63type REParams struct {
64 // Platform is the key value pair used for remotely executing the action.
65 Platform map[string]string
66 // Labels is a map of labels that identify the rule.
67 Labels map[string]string
68 // ExecStrategy is the remote execution strategy: remote, local, or remote_local_fallback.
69 ExecStrategy string
70 // Inputs is a list of input paths or ninja variables.
71 Inputs []string
72 // RSPFile is the name of the ninja variable used by the rule as a placeholder for an rsp
73 // input.
74 RSPFile string
75 // 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() {
90 pctx.VariableFunc("Wrapper", func(ctx android.PackageVarContext) string {
Ramy Medhat427683c2020-04-30 03:08:37 -040091 return wrapper(ctx.Config())
Ramy Medhat9a90fe52020-04-13 13:21:23 -040092 })
93}
94
Ramy Medhat427683c2020-04-30 03:08:37 -040095func wrapper(cfg android.Config) string {
96 if override := cfg.Getenv("RBE_WRAPPER"); override != "" {
97 return override
98 }
99 return DefaultWrapperPath
100}
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400101
Ramy Medhat427683c2020-04-30 03:08:37 -0400102// Template generates the remote execution wrapper template to be added as a prefix to the rule's
103// command.
104func (r *REParams) Template() string {
105 return "${remoteexec.Wrapper}" + r.wrapperArgs()
106}
107
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400108// NoVarTemplate generates the remote execution wrapper template without variables, to be used in
Ramy Medhat427683c2020-04-30 03:08:37 -0400109// RuleBuilder.
110func (r *REParams) NoVarTemplate(cfg android.Config) string {
111 return wrapper(cfg) + r.wrapperArgs()
112}
113
114func (r *REParams) wrapperArgs() string {
115 args := ""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400116 var kvs []string
117 labels := r.Labels
118 if len(labels) == 0 {
119 labels = defaultLabels
120 }
121 for k, v := range labels {
122 kvs = append(kvs, k+"="+v)
123 }
124 sort.Strings(kvs)
Ramy Medhat427683c2020-04-30 03:08:37 -0400125 args += " --labels=" + strings.Join(kvs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400126
127 var platform []string
128 for k, v := range r.Platform {
129 if v == "" {
130 continue
131 }
132 platform = append(platform, k+"="+v)
133 }
134 if _, ok := r.Platform[ContainerImageKey]; !ok {
135 platform = append(platform, ContainerImageKey+"="+DefaultImage)
136 }
137 if platform != nil {
138 sort.Strings(platform)
Ramy Medhat427683c2020-04-30 03:08:37 -0400139 args += " --platform=\"" + strings.Join(platform, ",") + "\""
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400140 }
141
142 strategy := r.ExecStrategy
143 if strategy == "" {
144 strategy = defaultExecStrategy
145 }
Ramy Medhat427683c2020-04-30 03:08:37 -0400146 args += " --exec_strategy=" + strategy
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400147
148 if len(r.Inputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400149 args += " --inputs=" + strings.Join(r.Inputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400150 }
151
152 if r.RSPFile != "" {
Ramy Medhat427683c2020-04-30 03:08:37 -0400153 args += " --input_list_paths=" + r.RSPFile
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400154 }
155
156 if len(r.OutputFiles) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400157 args += " --output_files=" + strings.Join(r.OutputFiles, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400158 }
159
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700160 if len(r.OutputDirectories) > 0 {
161 args += " --output_directories=" + strings.Join(r.OutputDirectories, ",")
162 }
163
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400164 if len(r.ToolchainInputs) > 0 {
Ramy Medhat427683c2020-04-30 03:08:37 -0400165 args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400166 }
167
Colin Cross31972dc2021-03-04 10:44:12 -0800168 if len(r.EnvironmentVariables) > 0 {
169 args += " --env_var_allowlist=" + strings.Join(r.EnvironmentVariables, ",")
170 }
171
Ramy Medhat427683c2020-04-30 03:08:37 -0400172 return args + " -- "
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400173}
174
175// StaticRules returns a pair of rules based on the given RuleParams, where the first rule is a
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700176// locally executable rule and the second rule is a remotely executable rule. commonArgs are args
177// used for both the local and remotely executable rules. reArgs are used only for remote
178// execution.
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400179func StaticRules(ctx android.PackageContext, name string, ruleParams blueprint.RuleParams, reParams *REParams, commonArgs []string, reArgs []string) (blueprint.Rule, blueprint.Rule) {
180 ruleParamsRE := ruleParams
Ramy Medhat31ec9422020-04-17 15:03:58 -0400181 ruleParams.Command = strings.ReplaceAll(ruleParams.Command, "$reTemplate", "")
182 ruleParamsRE.Command = strings.ReplaceAll(ruleParamsRE.Command, "$reTemplate", reParams.Template())
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400183
184 return ctx.AndroidStaticRule(name, ruleParams, commonArgs...),
185 ctx.AndroidRemoteStaticRule(name+"RE", android.RemoteRuleSupports{RBE: true}, ruleParamsRE, append(commonArgs, reArgs...)...)
186}
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400187
Kousik Kumar366afc52020-05-20 11:27:16 -0700188// MultiCommandStaticRules returns a pair of rules based on the given RuleParams, where the first
189// rule is a locally executable rule and the second rule is a remotely executable rule. This
190// function supports multiple remote execution wrappers placed in the template when commands are
191// chained together with &&. commonArgs are args used for both the local and remotely executable
192// rules. reArgs are args used only for remote execution.
193func MultiCommandStaticRules(ctx android.PackageContext, name string, ruleParams blueprint.RuleParams, reParams map[string]*REParams, commonArgs []string, reArgs []string) (blueprint.Rule, blueprint.Rule) {
194 ruleParamsRE := ruleParams
195 for k, v := range reParams {
196 ruleParams.Command = strings.ReplaceAll(ruleParams.Command, k, "")
197 ruleParamsRE.Command = strings.ReplaceAll(ruleParamsRE.Command, k, v.Template())
198 }
199
200 return ctx.AndroidStaticRule(name, ruleParams, commonArgs...),
201 ctx.AndroidRemoteStaticRule(name+"RE", android.RemoteRuleSupports{RBE: true}, ruleParamsRE, append(commonArgs, reArgs...)...)
202}
203
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400204// EnvOverrideFunc retrieves a variable func that evaluates to the value of the given environment
205// variable if set, otherwise the given default.
206func EnvOverrideFunc(envVar, defaultVal string) func(ctx android.PackageVarContext) string {
207 return func(ctx android.PackageVarContext) string {
208 if override := ctx.Config().Getenv(envVar); override != "" {
209 return override
210 }
211 return defaultVal
212 }
213}