1// Copyright 2015 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 kati
16
17import (
18	"fmt"
19	"io"
20)
21
22func showDeps(w io.Writer, n *DepNode, indent int, seen map[string]int) {
23	id, present := seen[n.Output]
24	if !present {
25		id = len(seen)
26		seen[n.Output] = id
27	}
28	fmt.Fprintf(w, "%*c%s (%d)\n", indent, ' ', n.Output, id)
29	if present {
30		return
31	}
32	for _, d := range n.Deps {
33		showDeps(w, d, indent+1, seen)
34	}
35	if len(n.OrderOnlys) > 0 {
36		fmt.Fprintf(w, "%*corder_onlys:\n", indent, ' ')
37		for _, d := range n.OrderOnlys {
38			showDeps(w, d, indent+1, seen)
39		}
40	}
41}
42
43func showNode(w io.Writer, n *DepNode) {
44	fmt.Fprintf(w, "%s:", n.Output)
45	for _, i := range n.ActualInputs {
46		fmt.Fprintf(w, " %s", i)
47	}
48	fmt.Fprintf(w, "\n")
49	for _, c := range n.Cmds {
50		fmt.Fprintf(w, "\t%s\n", c)
51	}
52	for k, v := range n.TargetSpecificVars {
53		fmt.Fprintf(w, "%s: %s=%s\n", n.Output, k, v.String())
54	}
55
56	fmt.Fprintf(w, "\n")
57	fmt.Fprintf(w, "location: %s:%d\n", n.Filename, n.Lineno)
58	if n.IsPhony {
59		fmt.Fprintf(w, "phony: true\n")
60	}
61
62	seen := make(map[string]int)
63	fmt.Fprintf(w, "dependencies:\n")
64	showDeps(w, n, 1, seen)
65}
66
67func handleNodeQuery(w io.Writer, q string, nodes []*DepNode) {
68	for _, n := range nodes {
69		if n.Output == q {
70			showNode(w, n)
71			break
72		}
73	}
74}
75
76// Query queries q in g.
77func Query(w io.Writer, q string, g *DepGraph) {
78	if q == "$MAKEFILE_LIST" {
79		for _, mk := range g.accessedMks {
80			fmt.Fprintf(w, "%s: state=%d\n", mk.Filename, mk.State)
81		}
82		return
83	}
84
85	if q == "$*" {
86		for k, v := range g.vars {
87			fmt.Fprintf(w, "%s=%s\n", k, v.String())
88		}
89		return
90	}
91
92	if q == "*" {
93		for _, n := range g.nodes {
94			fmt.Fprintf(w, "%s\n", n.Output)
95		}
96		return
97	}
98	handleNodeQuery(w, q, g.nodes)
99}
100