blob: f511922632386ef7f89c04a83a701bc6fe687153 [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
78 // ToolchainInputs is a list of paths or ninja variables pointing to the location of
79 // toolchain binaries used by the rule.
80 ToolchainInputs []string
81}
82
83func init() {
84 pctx.VariableFunc("Wrapper", func(ctx android.PackageVarContext) string {
85 if override := ctx.Config().Getenv("RBE_WRAPPER"); override != "" {
86 return override
87 }
88 return DefaultWrapperPath
89 })
90}
91
92// Generate the remote execution wrapper template to be added as a prefix to the rule's command.
93func (r *REParams) Template() string {
94 template := "${remoteexec.Wrapper}"
95
96 var kvs []string
97 labels := r.Labels
98 if len(labels) == 0 {
99 labels = defaultLabels
100 }
101 for k, v := range labels {
102 kvs = append(kvs, k+"="+v)
103 }
104 sort.Strings(kvs)
105 template += " --labels=" + strings.Join(kvs, ",")
106
107 var platform []string
108 for k, v := range r.Platform {
109 if v == "" {
110 continue
111 }
112 platform = append(platform, k+"="+v)
113 }
114 if _, ok := r.Platform[ContainerImageKey]; !ok {
115 platform = append(platform, ContainerImageKey+"="+DefaultImage)
116 }
117 if platform != nil {
118 sort.Strings(platform)
119 template += " --platform=\"" + strings.Join(platform, ",") + "\""
120 }
121
122 strategy := r.ExecStrategy
123 if strategy == "" {
124 strategy = defaultExecStrategy
125 }
126 template += " --exec_strategy=" + strategy
127
128 if len(r.Inputs) > 0 {
129 template += " --inputs=" + strings.Join(r.Inputs, ",")
130 }
131
132 if r.RSPFile != "" {
133 template += " --input_list_paths=" + r.RSPFile
134 }
135
136 if len(r.OutputFiles) > 0 {
137 template += " --output_files=" + strings.Join(r.OutputFiles, ",")
138 }
139
140 if len(r.ToolchainInputs) > 0 {
141 template += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",")
142 }
143
144 return template + " -- "
145}
146
147// StaticRules returns a pair of rules based on the given RuleParams, where the first rule is a
148// locally executable rule and the second rule is a remotely executable rule.
149func StaticRules(ctx android.PackageContext, name string, ruleParams blueprint.RuleParams, reParams *REParams, commonArgs []string, reArgs []string) (blueprint.Rule, blueprint.Rule) {
150 ruleParamsRE := ruleParams
Ramy Medhat31ec9422020-04-17 15:03:58 -0400151 ruleParams.Command = strings.ReplaceAll(ruleParams.Command, "$reTemplate", "")
152 ruleParamsRE.Command = strings.ReplaceAll(ruleParamsRE.Command, "$reTemplate", reParams.Template())
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400153
154 return ctx.AndroidStaticRule(name, ruleParams, commonArgs...),
155 ctx.AndroidRemoteStaticRule(name+"RE", android.RemoteRuleSupports{RBE: true}, ruleParamsRE, append(commonArgs, reArgs...)...)
156}