blob: 8f92269a589967442cdd1cc890908f5bdd22c81f [file] [log] [blame]
Dan Willemsen269a8c72017-05-03 17:15:47 -07001// Copyright 2017 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 build
16
17import (
Colin Cross7b97ecd2019-06-19 13:17:59 -070018 "bufio"
19 "io"
Dan Willemsen269a8c72017-05-03 17:15:47 -070020 "os/exec"
Colin Cross7b97ecd2019-06-19 13:17:59 -070021 "strings"
Dan Willemsenacafc672020-04-19 12:04:55 -070022 "syscall"
23 "time"
Dan Willemsen269a8c72017-05-03 17:15:47 -070024)
25
26// Cmd is a wrapper of os/exec.Cmd that integrates with the build context for
27// logging, the config's Environment for simpler environment modification, and
28// implements hooks for sandboxing
29type Cmd struct {
30 *exec.Cmd
31
32 Environment *Environment
33 Sandbox Sandbox
34
35 ctx Context
36 config Config
37 name string
Dan Willemsenacafc672020-04-19 12:04:55 -070038
39 started time.Time
Dan Willemsen269a8c72017-05-03 17:15:47 -070040}
41
42func Command(ctx Context, config Config, name string, executable string, args ...string) *Cmd {
43 ret := &Cmd{
44 Cmd: exec.CommandContext(ctx.Context, executable, args...),
45 Environment: config.Environment().Copy(),
46 Sandbox: noSandbox,
47
Jeff Gastona6697e82017-06-13 12:51:50 -070048 ctx: ctx,
49 config: config,
50 name: name,
Dan Willemsen269a8c72017-05-03 17:15:47 -070051 }
52
53 return ret
54}
55
56func (c *Cmd) prepare() {
57 if c.Env == nil {
58 c.Env = c.Environment.Environ()
59 }
60 if c.sandboxSupported() {
61 c.wrapSandbox()
62 }
63
Dan Willemsenacafc672020-04-19 12:04:55 -070064 c.ctx.Verbosef("%q executing %q %v\n", c.name, c.Path, c.Args)
65 c.started = time.Now()
66}
67
68func (c *Cmd) report() {
Patrice Arrudaca221f32020-10-19 11:38:54 -070069 if state := c.Cmd.ProcessState; state != nil {
70 if c.ctx.Metrics != nil {
71 c.ctx.Metrics.EventTracer.AddProcResInfo(c.name, state)
72 }
73 rusage := state.SysUsage().(*syscall.Rusage)
Dan Willemsenacafc672020-04-19 12:04:55 -070074 c.ctx.Verbosef("%q finished with exit code %d (%s real, %s user, %s system, %dMB maxrss)",
75 c.name, c.Cmd.ProcessState.ExitCode(),
76 time.Since(c.started).Round(time.Millisecond),
77 c.Cmd.ProcessState.UserTime().Round(time.Millisecond),
78 c.Cmd.ProcessState.SystemTime().Round(time.Millisecond),
79 rusage.Maxrss/1024)
80 }
Dan Willemsen269a8c72017-05-03 17:15:47 -070081}
82
83func (c *Cmd) Start() error {
84 c.prepare()
85 return c.Cmd.Start()
86}
87
88func (c *Cmd) Run() error {
89 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070090 err := c.Cmd.Run()
Dan Willemsenacafc672020-04-19 12:04:55 -070091 c.report()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070092 return err
Dan Willemsen269a8c72017-05-03 17:15:47 -070093}
94
95func (c *Cmd) Output() ([]byte, error) {
96 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070097 bytes, err := c.Cmd.Output()
Dan Willemsenacafc672020-04-19 12:04:55 -070098 c.report()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070099 return bytes, err
Dan Willemsen269a8c72017-05-03 17:15:47 -0700100}
101
102func (c *Cmd) CombinedOutput() ([]byte, error) {
103 c.prepare()
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700104 bytes, err := c.Cmd.CombinedOutput()
Dan Willemsenacafc672020-04-19 12:04:55 -0700105 c.report()
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700106 return bytes, err
Dan Willemsen269a8c72017-05-03 17:15:47 -0700107}
108
Dan Willemsenacafc672020-04-19 12:04:55 -0700109func (c *Cmd) Wait() error {
110 err := c.Cmd.Wait()
111 c.report()
112 return err
113}
114
Dan Willemsen269a8c72017-05-03 17:15:47 -0700115// StartOrFatal is equivalent to Start, but handles the error with a call to ctx.Fatal
116func (c *Cmd) StartOrFatal() {
117 if err := c.Start(); err != nil {
118 c.ctx.Fatalf("Failed to run %s: %v", c.name, err)
119 }
120}
121
Dan Willemsendb8457c2017-05-12 16:38:17 -0700122func (c *Cmd) reportError(err error) {
123 if err == nil {
124 return
125 }
126 if e, ok := err.(*exec.ExitError); ok {
127 c.ctx.Fatalf("%s failed with: %v", c.name, e.ProcessState.String())
128 } else {
129 c.ctx.Fatalf("Failed to run %s: %v", c.name, err)
130 }
131}
132
Dan Willemsen269a8c72017-05-03 17:15:47 -0700133// RunOrFatal is equivalent to Run, but handles the error with a call to ctx.Fatal
134func (c *Cmd) RunOrFatal() {
Dan Willemsendb8457c2017-05-12 16:38:17 -0700135 c.reportError(c.Run())
Dan Willemsen269a8c72017-05-03 17:15:47 -0700136}
137
138// WaitOrFatal is equivalent to Wait, but handles the error with a call to ctx.Fatal
139func (c *Cmd) WaitOrFatal() {
Dan Willemsendb8457c2017-05-12 16:38:17 -0700140 c.reportError(c.Wait())
141}
142
143// OutputOrFatal is equivalent to Output, but handles the error with a call to ctx.Fatal
144func (c *Cmd) OutputOrFatal() []byte {
145 ret, err := c.Output()
146 c.reportError(err)
147 return ret
148}
149
150// CombinedOutputOrFatal is equivalent to CombinedOutput, but handles the error with
151// a call to ctx.Fatal
152func (c *Cmd) CombinedOutputOrFatal() []byte {
153 ret, err := c.CombinedOutput()
154 c.reportError(err)
155 return ret
Dan Willemsen269a8c72017-05-03 17:15:47 -0700156}
Dan Willemsenb82471a2018-05-17 16:37:09 -0700157
158// RunAndPrintOrFatal will run the command, then after finishing
159// print any output, then handling any errors with a call to
160// ctx.Fatal
161func (c *Cmd) RunAndPrintOrFatal() {
162 ret, err := c.CombinedOutput()
163 st := c.ctx.Status.StartTool()
164 if len(ret) > 0 {
165 if err != nil {
166 st.Error(string(ret))
167 } else {
168 st.Print(string(ret))
169 }
170 }
171 st.Finish()
172 c.reportError(err)
173}
Colin Cross7b97ecd2019-06-19 13:17:59 -0700174
175// RunAndStreamOrFatal will run the command, while running print
176// any output, then handle any errors with a call to ctx.Fatal
177func (c *Cmd) RunAndStreamOrFatal() {
178 out, err := c.StdoutPipe()
179 if err != nil {
180 c.ctx.Fatal(err)
181 }
182 c.Stderr = c.Stdout
183
184 st := c.ctx.Status.StartTool()
185
186 c.StartOrFatal()
187
188 buf := bufio.NewReaderSize(out, 2*1024*1024)
189 for {
190 // Attempt to read whole lines, but write partial lines that are too long to fit in the buffer or hit EOF
191 line, err := buf.ReadString('\n')
192 if line != "" {
193 st.Print(strings.TrimSuffix(line, "\n"))
194 } else if err == io.EOF {
195 break
196 } else if err != nil {
197 c.ctx.Fatal(err)
198 }
199 }
200
201 err = c.Wait()
202 st.Finish()
203 c.reportError(err)
204}