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" |
| 23 | |
| 24 | "github.com/golang/protobuf/proto" |
| 25 | |
| 26 | "android/soong/ui/logger" |
| 27 | "android/soong/ui/status/ninja_frontend" |
| 28 | ) |
| 29 | |
| 30 | // NinjaReader reads the protobuf frontend format from ninja and translates it |
| 31 | // into calls on the ToolStatus API. |
| 32 | func NinjaReader(ctx logger.Logger, status ToolStatus, fifo string) { |
| 33 | os.Remove(fifo) |
| 34 | |
| 35 | err := syscall.Mkfifo(fifo, 0666) |
| 36 | if err != nil { |
| 37 | ctx.Fatalf("Failed to mkfifo(%q): %v", fifo, err) |
| 38 | } |
| 39 | |
| 40 | go ninjaReader(ctx, status, fifo) |
| 41 | } |
| 42 | |
| 43 | func ninjaReader(ctx logger.Logger, status ToolStatus, fifo string) { |
| 44 | defer os.Remove(fifo) |
| 45 | |
| 46 | f, err := os.Open(fifo) |
| 47 | if err != nil { |
| 48 | ctx.Fatal("Failed to open fifo:", err) |
| 49 | } |
| 50 | defer f.Close() |
| 51 | |
| 52 | r := bufio.NewReader(f) |
| 53 | |
| 54 | running := map[uint32]*Action{} |
| 55 | |
| 56 | for { |
| 57 | size, err := readVarInt(r) |
| 58 | if err != nil { |
| 59 | if err != io.EOF { |
| 60 | ctx.Println("Got error reading from ninja:", err) |
| 61 | } |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | buf := make([]byte, size) |
| 66 | _, err = io.ReadFull(r, buf) |
| 67 | if err != nil { |
| 68 | if err == io.EOF { |
| 69 | ctx.Printf("Missing message of size %d from ninja\n", size) |
| 70 | } else { |
| 71 | ctx.Fatal("Got error reading from ninja:", err) |
| 72 | } |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | msg := &ninja_frontend.Status{} |
| 77 | err = proto.Unmarshal(buf, msg) |
| 78 | if err != nil { |
| 79 | ctx.Printf("Error reading message from ninja: %v\n", err) |
| 80 | continue |
| 81 | } |
| 82 | |
| 83 | // Ignore msg.BuildStarted |
| 84 | if msg.TotalEdges != nil { |
| 85 | status.SetTotalActions(int(msg.TotalEdges.GetTotalEdges())) |
| 86 | } |
| 87 | if msg.EdgeStarted != nil { |
| 88 | action := &Action{ |
| 89 | Description: msg.EdgeStarted.GetDesc(), |
| 90 | Outputs: msg.EdgeStarted.Outputs, |
| 91 | Command: msg.EdgeStarted.GetCommand(), |
| 92 | } |
| 93 | status.StartAction(action) |
| 94 | running[msg.EdgeStarted.GetId()] = action |
| 95 | } |
| 96 | if msg.EdgeFinished != nil { |
| 97 | if started, ok := running[msg.EdgeFinished.GetId()]; ok { |
| 98 | delete(running, msg.EdgeFinished.GetId()) |
| 99 | |
| 100 | var err error |
| 101 | exitCode := int(msg.EdgeFinished.GetStatus()) |
| 102 | if exitCode != 0 { |
| 103 | err = fmt.Errorf("exited with code: %d", exitCode) |
| 104 | } |
| 105 | |
| 106 | status.FinishAction(ActionResult{ |
| 107 | Action: started, |
| 108 | Output: msg.EdgeFinished.GetOutput(), |
| 109 | Error: err, |
| 110 | }) |
| 111 | } |
| 112 | } |
| 113 | if msg.Message != nil { |
| 114 | message := "ninja: " + msg.Message.GetMessage() |
| 115 | switch msg.Message.GetLevel() { |
| 116 | case ninja_frontend.Status_Message_INFO: |
| 117 | status.Status(message) |
| 118 | case ninja_frontend.Status_Message_WARNING: |
| 119 | status.Print("warning: " + message) |
| 120 | case ninja_frontend.Status_Message_ERROR: |
| 121 | status.Error(message) |
| 122 | default: |
| 123 | status.Print(message) |
| 124 | } |
| 125 | } |
| 126 | if msg.BuildFinished != nil { |
| 127 | status.Finish() |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func readVarInt(r *bufio.Reader) (int, error) { |
| 133 | ret := 0 |
| 134 | shift := uint(0) |
| 135 | |
| 136 | for { |
| 137 | b, err := r.ReadByte() |
| 138 | if err != nil { |
| 139 | return 0, err |
| 140 | } |
| 141 | |
| 142 | ret += int(b&0x7f) << (shift * 7) |
| 143 | if b&0x80 == 0 { |
| 144 | break |
| 145 | } |
| 146 | shift += 1 |
| 147 | if shift > 4 { |
| 148 | return 0, fmt.Errorf("Expected varint32 length-delimited message") |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return ret, nil |
| 153 | } |