blob: 4f17fecc6e4e402a822f1a15acc50d61dfc9e631 [file] [log] [blame]
Chris Parsonsf3c96ef2020-09-29 02:23:17 -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 android
16
17import (
18 "bytes"
19 "errors"
20 "fmt"
Chris Parsonsa798d962020-10-12 23:44:08 -040021 "io/ioutil"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040022 "os"
23 "os/exec"
Chris Parsonsa798d962020-10-12 23:44:08 -040024 "path/filepath"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040025 "runtime"
26 "strings"
27 "sync"
Chris Parsonsa798d962020-10-12 23:44:08 -040028
29 "github.com/google/blueprint/bootstrap"
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040030)
31
32// Map key to describe bazel cquery requests.
33type cqueryKey struct {
34 label string
35 starlarkExpr string
36}
37
38type BazelContext interface {
39 // The below methods involve queuing cquery requests to be later invoked
40 // by bazel. If any of these methods return (_, false), then the request
41 // has been queued to be run later.
42
43 // Returns result files built by building the given bazel target label.
44 GetAllFiles(label string) ([]string, bool)
45
46 // TODO(cparsons): Other cquery-related methods should be added here.
47 // ** End cquery methods
48
49 // Issues commands to Bazel to receive results for all cquery requests
50 // queued in the BazelContext.
51 InvokeBazel() error
52
53 // Returns true if bazel is enabled for the given configuration.
54 BazelEnabled() bool
55}
56
57// A context object which tracks queued requests that need to be made to Bazel,
58// and their results after the requests have been made.
59type bazelContext struct {
60 homeDir string
61 bazelPath string
62 outputBase string
63 workspaceDir string
64
65 requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
66 requestMutex sync.Mutex // requests can be written in parallel
67
68 results map[cqueryKey]string // Results of cquery requests after Bazel invocations
69}
70
71var _ BazelContext = &bazelContext{}
72
73// A bazel context to use when Bazel is disabled.
74type noopBazelContext struct{}
75
76var _ BazelContext = noopBazelContext{}
77
78// A bazel context to use for tests.
79type MockBazelContext struct {
80 AllFiles map[string][]string
81}
82
83func (m MockBazelContext) GetAllFiles(label string) ([]string, bool) {
84 result, ok := m.AllFiles[label]
85 return result, ok
86}
87
88func (m MockBazelContext) InvokeBazel() error {
89 panic("unimplemented")
90}
91
92func (m MockBazelContext) BazelEnabled() bool {
93 return true
94}
95
96var _ BazelContext = MockBazelContext{}
97
98func (bazelCtx *bazelContext) GetAllFiles(label string) ([]string, bool) {
99 starlarkExpr := "', '.join([f.path for f in target.files.to_list()])"
100 result, ok := bazelCtx.cquery(label, starlarkExpr)
101 if ok {
102 bazelOutput := strings.TrimSpace(result)
103 return strings.Split(bazelOutput, ", "), true
104 } else {
105 return nil, false
106 }
107}
108
109func (n noopBazelContext) GetAllFiles(label string) ([]string, bool) {
110 panic("unimplemented")
111}
112
113func (n noopBazelContext) InvokeBazel() error {
114 panic("unimplemented")
115}
116
117func (n noopBazelContext) BazelEnabled() bool {
118 return false
119}
120
121func NewBazelContext(c *config) (BazelContext, error) {
122 if c.Getenv("USE_BAZEL") != "1" {
123 return noopBazelContext{}, nil
124 }
125
126 bazelCtx := bazelContext{requests: make(map[cqueryKey]bool)}
127 missingEnvVars := []string{}
128 if len(c.Getenv("BAZEL_HOME")) > 1 {
129 bazelCtx.homeDir = c.Getenv("BAZEL_HOME")
130 } else {
131 missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
132 }
133 if len(c.Getenv("BAZEL_PATH")) > 1 {
134 bazelCtx.bazelPath = c.Getenv("BAZEL_PATH")
135 } else {
136 missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
137 }
138 if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
139 bazelCtx.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
140 } else {
141 missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
142 }
143 if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
144 bazelCtx.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
145 } else {
146 missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
147 }
148 if len(missingEnvVars) > 0 {
149 return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
150 } else {
151 return &bazelCtx, nil
152 }
153}
154
155func (context *bazelContext) BazelEnabled() bool {
156 return true
157}
158
159// Adds a cquery request to the Bazel request queue, to be later invoked, or
160// returns the result of the given request if the request was already made.
161// If the given request was already made (and the results are available), then
162// returns (result, true). If the request is queued but no results are available,
163// then returns ("", false).
164func (context *bazelContext) cquery(label string, starlarkExpr string) (string, bool) {
165 key := cqueryKey{label, starlarkExpr}
166 if result, ok := context.results[key]; ok {
167 return result, true
168 } else {
169 context.requestMutex.Lock()
170 defer context.requestMutex.Unlock()
171 context.requests[key] = true
172 return "", false
173 }
174}
175
176func pwdPrefix() string {
177 // Darwin doesn't have /proc
178 if runtime.GOOS != "darwin" {
179 return "PWD=/proc/self/cwd"
180 }
181 return ""
182}
183
184func (context *bazelContext) issueBazelCommand(command string, labels []string,
185 extraFlags ...string) (string, error) {
186
187 cmdFlags := []string{"--output_base=" + context.outputBase, command}
188 cmdFlags = append(cmdFlags, labels...)
189 cmdFlags = append(cmdFlags, extraFlags...)
190
191 bazelCmd := exec.Command(context.bazelPath, cmdFlags...)
192 bazelCmd.Dir = context.workspaceDir
193 bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix())
194
195 var stderr bytes.Buffer
196 bazelCmd.Stderr = &stderr
197
198 if output, err := bazelCmd.Output(); err != nil {
199 return "", fmt.Errorf("bazel command failed. command: [%s], error [%s]", bazelCmd, stderr)
200 } else {
201 return string(output), nil
202 }
203}
204
205// Issues commands to Bazel to receive results for all cquery requests
206// queued in the BazelContext.
207func (context *bazelContext) InvokeBazel() error {
208 context.results = make(map[cqueryKey]string)
209
210 var labels []string
211 var cqueryOutput string
212 var err error
213 for val, _ := range context.requests {
214 labels = append(labels, val.label)
215
216 // TODO(cparsons): Combine requests into a batch cquery request.
217 // TODO(cparsons): Use --query_file to avoid command line limits.
218 cqueryOutput, err = context.issueBazelCommand("cquery", []string{val.label},
219 "--output=starlark",
220 "--starlark:expr="+val.starlarkExpr)
221
222 if err != nil {
223 return err
224 } else {
225 context.results[val] = string(cqueryOutput)
226 }
227 }
228
229 // Issue a build command.
230 // TODO(cparsons): Invoking bazel execution during soong_build should be avoided;
231 // bazel actions should either be added to the Ninja file and executed later,
232 // or bazel should handle execution.
233 // TODO(cparsons): Use --target_pattern_file to avoid command line limits.
234 _, err = context.issueBazelCommand("build", labels)
235
236 if err != nil {
237 return err
238 }
239
240 // Clear requests.
241 context.requests = map[cqueryKey]bool{}
242 return nil
243}
Chris Parsonsa798d962020-10-12 23:44:08 -0400244
245// Singleton used for registering BUILD file ninja dependencies (needed
246// for correctness of builds which use Bazel.
247func BazelSingleton() Singleton {
248 return &bazelSingleton{}
249}
250
251type bazelSingleton struct{}
252
253func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
254 if ctx.Config().BazelContext.BazelEnabled() {
255 bazelBuildList := absolutePath(filepath.Join(
256 filepath.Dir(bootstrap.ModuleListFile), "bazel.list"))
257 ctx.AddNinjaFileDeps(bazelBuildList)
258
259 data, err := ioutil.ReadFile(bazelBuildList)
260 if err != nil {
261 ctx.Errorf(err.Error())
262 }
263 files := strings.Split(strings.TrimSpace(string(data)), "\n")
264 for _, file := range files {
265 ctx.AddNinjaFileDeps(file)
266 }
267 }
268}