Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 status |
| 16 | |
| 17 | import ( |
| 18 | "bufio" |
| 19 | "fmt" |
| 20 | "io" |
| 21 | "os" |
| 22 | "syscall" |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 23 | "time" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 24 | |
| 25 | "github.com/golang/protobuf/proto" |
| 26 | |
| 27 | "android/soong/ui/logger" |
| 28 | "android/soong/ui/status/ninja_frontend" |
| 29 | ) |
| 30 | |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 31 | // NewNinjaReader reads the protobuf frontend format from ninja and translates it |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 32 | // into calls on the ToolStatus API. |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 33 | func NewNinjaReader(ctx logger.Logger, status ToolStatus, fifo string) *NinjaReader { |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 34 | os.Remove(fifo) |
| 35 | |
| 36 | err := syscall.Mkfifo(fifo, 0666) |
| 37 | if err != nil { |
| 38 | ctx.Fatalf("Failed to mkfifo(%q): %v", fifo, err) |
| 39 | } |
| 40 | |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 41 | n := &NinjaReader{ |
| 42 | status: status, |
| 43 | fifo: fifo, |
| 44 | done: make(chan bool), |
| 45 | cancel: make(chan bool), |
| 46 | } |
| 47 | |
| 48 | go n.run() |
| 49 | |
| 50 | return n |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 53 | type NinjaReader struct { |
| 54 | status ToolStatus |
| 55 | fifo string |
| 56 | done chan bool |
| 57 | cancel chan bool |
| 58 | } |
| 59 | |
| 60 | const NINJA_READER_CLOSE_TIMEOUT = 5 * time.Second |
| 61 | |
| 62 | // Close waits for NinjaReader to finish reading from the fifo, or 5 seconds. |
| 63 | func (n *NinjaReader) Close() { |
| 64 | // Signal the goroutine to stop if it is blocking opening the fifo. |
| 65 | close(n.cancel) |
| 66 | |
| 67 | timeoutCh := time.After(NINJA_READER_CLOSE_TIMEOUT) |
| 68 | |
| 69 | select { |
| 70 | case <-n.done: |
| 71 | // Nothing |
| 72 | case <-timeoutCh: |
| 73 | n.status.Error(fmt.Sprintf("ninja fifo didn't finish after %s", NINJA_READER_CLOSE_TIMEOUT.String())) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 74 | } |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 75 | |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | func (n *NinjaReader) run() { |
| 80 | defer close(n.done) |
| 81 | |
| 82 | // Opening the fifo can block forever if ninja never opens the write end, do it in a goroutine so this |
| 83 | // method can exit on cancel. |
| 84 | fileCh := make(chan *os.File) |
| 85 | go func() { |
| 86 | f, err := os.Open(n.fifo) |
| 87 | if err != nil { |
| 88 | n.status.Error(fmt.Sprintf("Failed to open fifo: %v", err)) |
| 89 | close(fileCh) |
| 90 | return |
| 91 | } |
| 92 | fileCh <- f |
| 93 | }() |
| 94 | |
| 95 | var f *os.File |
| 96 | |
| 97 | select { |
| 98 | case f = <-fileCh: |
| 99 | // Nothing |
| 100 | case <-n.cancel: |
| 101 | return |
| 102 | } |
| 103 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 104 | defer f.Close() |
| 105 | |
| 106 | r := bufio.NewReader(f) |
| 107 | |
| 108 | running := map[uint32]*Action{} |
| 109 | |
| 110 | for { |
| 111 | size, err := readVarInt(r) |
| 112 | if err != nil { |
| 113 | if err != io.EOF { |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 114 | n.status.Error(fmt.Sprintf("Got error reading from ninja: %s", err)) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 115 | } |
| 116 | return |
| 117 | } |
| 118 | |
| 119 | buf := make([]byte, size) |
| 120 | _, err = io.ReadFull(r, buf) |
| 121 | if err != nil { |
| 122 | if err == io.EOF { |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 123 | n.status.Print(fmt.Sprintf("Missing message of size %d from ninja\n", size)) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 124 | } else { |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 125 | n.status.Error(fmt.Sprintf("Got error reading from ninja: %s", err)) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 126 | } |
| 127 | return |
| 128 | } |
| 129 | |
| 130 | msg := &ninja_frontend.Status{} |
| 131 | err = proto.Unmarshal(buf, msg) |
| 132 | if err != nil { |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 133 | n.status.Print(fmt.Sprintf("Error reading message from ninja: %v", err)) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 134 | continue |
| 135 | } |
| 136 | |
| 137 | // Ignore msg.BuildStarted |
| 138 | if msg.TotalEdges != nil { |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 139 | n.status.SetTotalActions(int(msg.TotalEdges.GetTotalEdges())) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 140 | } |
| 141 | if msg.EdgeStarted != nil { |
| 142 | action := &Action{ |
| 143 | Description: msg.EdgeStarted.GetDesc(), |
| 144 | Outputs: msg.EdgeStarted.Outputs, |
| 145 | Command: msg.EdgeStarted.GetCommand(), |
| 146 | } |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 147 | n.status.StartAction(action) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 148 | running[msg.EdgeStarted.GetId()] = action |
| 149 | } |
| 150 | if msg.EdgeFinished != nil { |
| 151 | if started, ok := running[msg.EdgeFinished.GetId()]; ok { |
| 152 | delete(running, msg.EdgeFinished.GetId()) |
| 153 | |
| 154 | var err error |
| 155 | exitCode := int(msg.EdgeFinished.GetStatus()) |
| 156 | if exitCode != 0 { |
| 157 | err = fmt.Errorf("exited with code: %d", exitCode) |
| 158 | } |
| 159 | |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 160 | n.status.FinishAction(ActionResult{ |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 161 | Action: started, |
| 162 | Output: msg.EdgeFinished.GetOutput(), |
| 163 | Error: err, |
| 164 | }) |
| 165 | } |
| 166 | } |
| 167 | if msg.Message != nil { |
| 168 | message := "ninja: " + msg.Message.GetMessage() |
| 169 | switch msg.Message.GetLevel() { |
| 170 | case ninja_frontend.Status_Message_INFO: |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 171 | n.status.Status(message) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 172 | case ninja_frontend.Status_Message_WARNING: |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 173 | n.status.Print("warning: " + message) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 174 | case ninja_frontend.Status_Message_ERROR: |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 175 | n.status.Error(message) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 176 | default: |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 177 | n.status.Print(message) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | if msg.BuildFinished != nil { |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 181 | n.status.Finish() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func readVarInt(r *bufio.Reader) (int, error) { |
| 187 | ret := 0 |
| 188 | shift := uint(0) |
| 189 | |
| 190 | for { |
| 191 | b, err := r.ReadByte() |
| 192 | if err != nil { |
| 193 | return 0, err |
| 194 | } |
| 195 | |
| 196 | ret += int(b&0x7f) << (shift * 7) |
| 197 | if b&0x80 == 0 { |
| 198 | break |
| 199 | } |
| 200 | shift += 1 |
| 201 | if shift > 4 { |
| 202 | return 0, fmt.Errorf("Expected varint32 length-delimited message") |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return ret, nil |
| 207 | } |