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
15package status
16
17import (
18	"bufio"
19	"fmt"
20	"io"
21	"os"
22	"syscall"
23	"time"
24
25	"github.com/golang/protobuf/proto"
26
27	"android/soong/ui/logger"
28	"android/soong/ui/status/ninja_frontend"
29)
30
31// NewNinjaReader reads the protobuf frontend format from ninja and translates it
32// into calls on the ToolStatus API.
33func NewNinjaReader(ctx logger.Logger, status ToolStatus, fifo string) *NinjaReader {
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
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
51}
52
53type NinjaReader struct {
54	status ToolStatus
55	fifo   string
56	done   chan bool
57	cancel chan bool
58}
59
60const NINJA_READER_CLOSE_TIMEOUT = 5 * time.Second
61
62// Close waits for NinjaReader to finish reading from the fifo, or 5 seconds.
63func (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()))
74	}
75
76	return
77}
78
79func (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
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 {
114				n.status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
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 {
123				n.status.Print(fmt.Sprintf("Missing message of size %d from ninja\n", size))
124			} else {
125				n.status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
126			}
127			return
128		}
129
130		msg := &ninja_frontend.Status{}
131		err = proto.Unmarshal(buf, msg)
132		if err != nil {
133			n.status.Print(fmt.Sprintf("Error reading message from ninja: %v", err))
134			continue
135		}
136
137		// Ignore msg.BuildStarted
138		if msg.TotalEdges != nil {
139			n.status.SetTotalActions(int(msg.TotalEdges.GetTotalEdges()))
140		}
141		if msg.EdgeStarted != nil {
142			action := &Action{
143				Description: msg.EdgeStarted.GetDesc(),
144				Outputs:     msg.EdgeStarted.Outputs,
145				Inputs:      msg.EdgeStarted.Inputs,
146				Command:     msg.EdgeStarted.GetCommand(),
147			}
148			n.status.StartAction(action)
149			running[msg.EdgeStarted.GetId()] = action
150		}
151		if msg.EdgeFinished != nil {
152			if started, ok := running[msg.EdgeFinished.GetId()]; ok {
153				delete(running, msg.EdgeFinished.GetId())
154
155				var err error
156				exitCode := int(msg.EdgeFinished.GetStatus())
157				if exitCode != 0 {
158					err = fmt.Errorf("exited with code: %d", exitCode)
159				}
160
161				n.status.FinishAction(ActionResult{
162					Action: started,
163					Output: msg.EdgeFinished.GetOutput(),
164					Error:  err,
165				})
166			}
167		}
168		if msg.Message != nil {
169			message := "ninja: " + msg.Message.GetMessage()
170			switch msg.Message.GetLevel() {
171			case ninja_frontend.Status_Message_INFO:
172				n.status.Status(message)
173			case ninja_frontend.Status_Message_WARNING:
174				n.status.Print("warning: " + message)
175			case ninja_frontend.Status_Message_ERROR:
176				n.status.Error(message)
177			case ninja_frontend.Status_Message_DEBUG:
178				n.status.Verbose(message)
179			default:
180				n.status.Print(message)
181			}
182		}
183		if msg.BuildFinished != nil {
184			n.status.Finish()
185		}
186	}
187}
188
189func readVarInt(r *bufio.Reader) (int, error) {
190	ret := 0
191	shift := uint(0)
192
193	for {
194		b, err := r.ReadByte()
195		if err != nil {
196			return 0, err
197		}
198
199		ret += int(b&0x7f) << (shift * 7)
200		if b&0x80 == 0 {
201			break
202		}
203		shift += 1
204		if shift > 4 {
205			return 0, fmt.Errorf("Expected varint32 length-delimited message")
206		}
207	}
208
209	return ret, nil
210}
211