blob: 4ceb5efc580053895f9984f8a380957fb20b8742 [file] [log] [blame]
Dan Willemsenb82471a2018-05-17 16:37:09 -07001// 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
15package status
16
17import (
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.
32func 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
Dan Willemsen96111992018-07-12 18:29:05 -070040 go ninjaReader(status, fifo)
Dan Willemsenb82471a2018-05-17 16:37:09 -070041}
42
Dan Willemsen96111992018-07-12 18:29:05 -070043func ninjaReader(status ToolStatus, fifo string) {
Dan Willemsenb82471a2018-05-17 16:37:09 -070044 f, err := os.Open(fifo)
45 if err != nil {
Dan Willemsen59339a22018-07-22 21:18:45 -070046 status.Error(fmt.Sprintf("Failed to open fifo: %v", err))
Dan Willemsenb82471a2018-05-17 16:37:09 -070047 }
48 defer f.Close()
49
50 r := bufio.NewReader(f)
51
52 running := map[uint32]*Action{}
53
54 for {
55 size, err := readVarInt(r)
56 if err != nil {
57 if err != io.EOF {
Dan Willemsen96111992018-07-12 18:29:05 -070058 status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
Dan Willemsenb82471a2018-05-17 16:37:09 -070059 }
60 return
61 }
62
63 buf := make([]byte, size)
64 _, err = io.ReadFull(r, buf)
65 if err != nil {
66 if err == io.EOF {
Dan Willemsen96111992018-07-12 18:29:05 -070067 status.Print(fmt.Sprintf("Missing message of size %d from ninja\n", size))
Dan Willemsenb82471a2018-05-17 16:37:09 -070068 } else {
Dan Willemsen96111992018-07-12 18:29:05 -070069 status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
Dan Willemsenb82471a2018-05-17 16:37:09 -070070 }
71 return
72 }
73
74 msg := &ninja_frontend.Status{}
75 err = proto.Unmarshal(buf, msg)
76 if err != nil {
Dan Willemsen96111992018-07-12 18:29:05 -070077 status.Print(fmt.Sprintf("Error reading message from ninja: %v", err))
Dan Willemsenb82471a2018-05-17 16:37:09 -070078 continue
79 }
80
81 // Ignore msg.BuildStarted
82 if msg.TotalEdges != nil {
83 status.SetTotalActions(int(msg.TotalEdges.GetTotalEdges()))
84 }
85 if msg.EdgeStarted != nil {
86 action := &Action{
87 Description: msg.EdgeStarted.GetDesc(),
88 Outputs: msg.EdgeStarted.Outputs,
89 Command: msg.EdgeStarted.GetCommand(),
90 }
91 status.StartAction(action)
92 running[msg.EdgeStarted.GetId()] = action
93 }
94 if msg.EdgeFinished != nil {
95 if started, ok := running[msg.EdgeFinished.GetId()]; ok {
96 delete(running, msg.EdgeFinished.GetId())
97
98 var err error
99 exitCode := int(msg.EdgeFinished.GetStatus())
100 if exitCode != 0 {
101 err = fmt.Errorf("exited with code: %d", exitCode)
102 }
103
104 status.FinishAction(ActionResult{
105 Action: started,
106 Output: msg.EdgeFinished.GetOutput(),
107 Error: err,
108 })
109 }
110 }
111 if msg.Message != nil {
112 message := "ninja: " + msg.Message.GetMessage()
113 switch msg.Message.GetLevel() {
114 case ninja_frontend.Status_Message_INFO:
115 status.Status(message)
116 case ninja_frontend.Status_Message_WARNING:
117 status.Print("warning: " + message)
118 case ninja_frontend.Status_Message_ERROR:
119 status.Error(message)
120 default:
121 status.Print(message)
122 }
123 }
124 if msg.BuildFinished != nil {
125 status.Finish()
126 }
127 }
128}
129
130func readVarInt(r *bufio.Reader) (int, error) {
131 ret := 0
132 shift := uint(0)
133
134 for {
135 b, err := r.ReadByte()
136 if err != nil {
137 return 0, err
138 }
139
140 ret += int(b&0x7f) << (shift * 7)
141 if b&0x80 == 0 {
142 break
143 }
144 shift += 1
145 if shift > 4 {
146 return 0, fmt.Errorf("Expected varint32 length-delimited message")
147 }
148 }
149
150 return ret, nil
151}