Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 18 | "bufio" |
| 19 | "io" |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 20 | "os/exec" |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 21 | "strings" |
Dan Willemsen | acafc67 | 2020-04-19 12:04:55 -0700 | [diff] [blame] | 22 | "syscall" |
| 23 | "time" |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 24 | ) |
| 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 |
| 29 | type Cmd struct { |
| 30 | *exec.Cmd |
| 31 | |
| 32 | Environment *Environment |
| 33 | Sandbox Sandbox |
| 34 | |
| 35 | ctx Context |
| 36 | config Config |
| 37 | name string |
Dan Willemsen | acafc67 | 2020-04-19 12:04:55 -0700 | [diff] [blame] | 38 | |
| 39 | started time.Time |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | func 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 Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 48 | ctx: ctx, |
| 49 | config: config, |
| 50 | name: name, |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return ret |
| 54 | } |
| 55 | |
| 56 | func (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 Willemsen | acafc67 | 2020-04-19 12:04:55 -0700 | [diff] [blame] | 64 | c.ctx.Verbosef("%q executing %q %v\n", c.name, c.Path, c.Args) |
| 65 | c.started = time.Now() |
| 66 | } |
| 67 | |
| 68 | func (c *Cmd) report() { |
Patrice Arruda | ca221f3 | 2020-10-19 11:38:54 -0700 | [diff] [blame] | 69 | 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 Willemsen | acafc67 | 2020-04-19 12:04:55 -0700 | [diff] [blame] | 74 | 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 Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | func (c *Cmd) Start() error { |
| 84 | c.prepare() |
| 85 | return c.Cmd.Start() |
| 86 | } |
| 87 | |
| 88 | func (c *Cmd) Run() error { |
| 89 | c.prepare() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 90 | err := c.Cmd.Run() |
Dan Willemsen | acafc67 | 2020-04-19 12:04:55 -0700 | [diff] [blame] | 91 | c.report() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 92 | return err |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func (c *Cmd) Output() ([]byte, error) { |
| 96 | c.prepare() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 97 | bytes, err := c.Cmd.Output() |
Dan Willemsen | acafc67 | 2020-04-19 12:04:55 -0700 | [diff] [blame] | 98 | c.report() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 99 | return bytes, err |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | func (c *Cmd) CombinedOutput() ([]byte, error) { |
| 103 | c.prepare() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 104 | bytes, err := c.Cmd.CombinedOutput() |
Dan Willemsen | acafc67 | 2020-04-19 12:04:55 -0700 | [diff] [blame] | 105 | c.report() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 106 | return bytes, err |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Dan Willemsen | acafc67 | 2020-04-19 12:04:55 -0700 | [diff] [blame] | 109 | func (c *Cmd) Wait() error { |
| 110 | err := c.Cmd.Wait() |
| 111 | c.report() |
| 112 | return err |
| 113 | } |
| 114 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 115 | // StartOrFatal is equivalent to Start, but handles the error with a call to ctx.Fatal |
| 116 | func (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 Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 122 | func (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 Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 133 | // RunOrFatal is equivalent to Run, but handles the error with a call to ctx.Fatal |
| 134 | func (c *Cmd) RunOrFatal() { |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 135 | c.reportError(c.Run()) |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // WaitOrFatal is equivalent to Wait, but handles the error with a call to ctx.Fatal |
| 139 | func (c *Cmd) WaitOrFatal() { |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 140 | c.reportError(c.Wait()) |
| 141 | } |
| 142 | |
| 143 | // OutputOrFatal is equivalent to Output, but handles the error with a call to ctx.Fatal |
| 144 | func (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 |
| 152 | func (c *Cmd) CombinedOutputOrFatal() []byte { |
| 153 | ret, err := c.CombinedOutput() |
| 154 | c.reportError(err) |
| 155 | return ret |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 156 | } |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 157 | |
| 158 | // RunAndPrintOrFatal will run the command, then after finishing |
| 159 | // print any output, then handling any errors with a call to |
| 160 | // ctx.Fatal |
| 161 | func (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 Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 174 | |
| 175 | // RunAndStreamOrFatal will run the command, while running print |
| 176 | // any output, then handle any errors with a call to ctx.Fatal |
| 177 | func (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 | } |