blob: d9190911202e45638ad0485f094b378c01052b5b [file] [log] [blame]
David Chen8fe82a32016-08-24 10:55:41 +00001---
2layout: documentation
3title: Query Language
4---
5<h1>The Bazel Query Reference</h1>
6
7<p>
8 When you use <code>bazel query</code> to analyze build
9 dependencies, you use a little language, the <em>Bazel Query
10 Language</em>. This document is the reference manual for that
11 language. This document also describes the output
12 formats <code>bazel query</code> supports.
13</p>
14
15<h2>Examples</h2>
16
17<p>
18 How do people use <code>bazel query</code>? Here are typical examples:
19</p>
20
21<p>
22 Why does the <code>//foo</code> tree depend on <code>//bar/baz</code>?
23 Show a path:</p>
24 <pre>somepath(foo/..., //bar/baz:all)</pre>
25
26
27<p>
28 What C++ libraries do all the <code>foo</code> tests depend on that
29 the <code>foo_bin</code> target does not?</p>
30 <pre>kind("cc_library", deps(kind(".*test rule", foo/...)) except deps(//foo:foo_bin))</pre>
31
32
33<h2>Tokens: the lexical syntax</h2>
34
35<p>
36 Expressions in the query language are composed of the following
37 tokens:</p>
38 <ul>
39 <li>
40 <p>
Googler020ef072017-07-18 10:47:01 +020041 <b>Keywords</b>, such as <code>let</code>. Keywords are the reserved words of the
David Chen8fe82a32016-08-24 10:55:41 +000042 language, and each of them is described below. The complete set
43 of keywords is:
44 </p>
45
46<code><!-- keep this alphabetically sorted -->
David Chen8fe82a32016-08-24 10:55:41 +000047<a href="#set-operations">except</a><br/>
David Chen8fe82a32016-08-24 10:55:41 +000048<a href="#variables">in</a><br/>
49<a href="#set-operations">intersect</a><br/>
David Chen8fe82a32016-08-24 10:55:41 +000050<a href="#variables">let</a><br/>
David Chen8fe82a32016-08-24 10:55:41 +000051<a href="#set">set</a><br/>
David Chen8fe82a32016-08-24 10:55:41 +000052<a href="#set-operations">union</a><br/>
53</code>
54 </li>
55
56 <li>
57 <p>
58 <b>Words</b>, such as <code>foo/...</code> or
59 <code>".*test rule"</code> or
60 <code>//bar/baz:all</code>.
61 If a character sequence is "quoted" (begins and ends with a
62 single-quote <code>'</code>, or begins and ends with a
63 double-quote <code>&quot;</code>), it is a word.
64 If a character sequence is not quoted, it may still be parsed as a word.
65 Unquoted words are sequences of characters drawn from
66 the set of alphabet characters, numerals, slash <code>/</code>,
67 hyphen <code>-</code>, underscore <code>_</code>, star <code>*</code>, and
68 period <code>.</code>. Unquoted words may not start with a
69 hyphen or period.
70 </p>
71
72 <p>We chose this syntax so that quote marks aren't needed in most cases.
73 The (unusual) <code>".*test rule"</code> example needs quotes: it
74 starts with a period and contains a space.
75 Quoting <code>"cc_library"</code> is unnecessary but harmless.
76 </p>
77
78 <p>
79 Quoting <em>is</em> necessary when writing scripts that
80 construct Bazel query expressions from user-supplied values.
81
82 </p>
83 <pre>
84 //foo:bar+wiz # WRONG: scanned as //foo:bar + wiz.
85 //foo:bar=wiz # WRONG: scanned as //foo:bar = wiz.
86 "//foo:bar+wiz" # ok.
87 "//foo:bar=wiz" # ok.
88 </pre>
89 <p>
90 Note that this quoting is in addition to any quoting that may
91 be required by your shell. e.g.
92 </p>
laszlocsomor79251002017-04-03 13:09:47 +000093 <pre>bazel query ' "//foo:bar=wiz" ' # single-quotes for shell, double-quotes for Bazel.</pre>
David Chen8fe82a32016-08-24 10:55:41 +000094
95 <p>
96 Keywords, when quoted, are treated as ordinary words, thus
97 <code>some</code> is a keyword but <code>"some"</code> is a word.
98 Both <code>foo</code> and <code>"foo"</code> are words.
99 </p>
100
101 <li><b>Punctuation</b>, such as parens <code>()</code>, period
102 <code>.</code> and comma <code>,</code>, etc. Words containing
103 punctuation (other than the exceptions listed above) must be quoted.
104 </ul>
105
106<p>
107 Whitespace characters outside of a quoted word are ignored.
108</p>
109
110<h2 id='concepts'>Bazel Query Language Concepts</h2>
111<p>
112 The Bazel query language is a language of expressions. Every
113 expression evaluates to a <b>partially-ordered set</b> of targets,
114 or equivalently, a <b>graph</b> (DAG) of targets. This is the only
115 datatype.
116</p>
117<p>
118 In some expressions, the partial order of the graph is
119 not interesting; In this case, we call the values
120 "sets". In cases where the partial order of elements
121 is significant, we call values "graphs". Note
122 that both terms refer to the same datatype, but merely emphasize
123 different aspects of it.
124</p>
125
126<h3>Cycles in the dependency graph</h3>
127<p>
128 Build dependency graphs should be acyclic.
129
130 The algorithms used by the query language are intended for use in
131 acyclic graphs, but are robust against cycles. The details of how
132 cycles are treated are not specified and should not be relied upon.
133</p>
134
135<h3 id='implicit_deps'>Implicit dependencies</h3>
136
137<p>
138 In addition to build dependencies that are defined explicitly in BUILD files,
139 Bazel adds additional <em>implicit</em> dependencies to rules. For example
140 every Java rule implicitly depends on the JavaBuilder. Implicit dependencies
141 are established using attributes that start with <code>$</code> and they
142 cannot be overridden in BUILD files.
143
144</p>
145
146<p>
147 Per default <code>bazel query</code> takes implicit dependencies into account
148 when computing the query result. This behavior can be changed with
149 the <code>--[no]implicit_deps</code> option.
150</p>
151
152<h3 id='soundness'>Soundness</h3>
153
154<p>
155 Bazel query language expressions operate over the build
156 dependency graph, which is the graph implicitly defined by all
157 rule declarations in all BUILD files. It is important to understand
158 that this graph is somewhat abstract, and does not constitute a
159 complete description of how to perform all the steps of a build. In
160 order to perform a build, a <em>configuration</em> is required too;
161 see the <a href='bazel-user-manual.html#configurations'>configurations</a>
162 section of the User's Guide for more detail.
163</p>
164
165<p>
166 The result of evaluating an expression in the Bazel query language
167 is true <em>for all configurations</em>, which means that it may be
168 a conservative over-approximation, and not exactly precise. If you
169 use the query tool to compute the set of all source files needed
170 during a build, it may report more than are actually necessary
171 because, for example, the query tool will include all the files
172 needed to support message translation, even though you don't intend
173 to use that feature in your build.
174</p>
175
176<h3 id='graph-order'>On the preservation of graph order</h3>
177
178<p>
179 Operations preserve any ordering
180 constraints inherited from their subexpressions. You can think of
181 this as "the law of conservation of partial order". Consider an
182 example: if you issue a query to determine the transitive closure of
183 dependencies of a particular target, the resulting set is ordered
184 according to the dependency graph. If you filter that set to
185 include only the targets of <code>file</code> kind, the same
186 <em>transitive</em> partial ordering relation holds between every
187 pair of targets in the resulting subset&mdash;even though none of
188 these pairs is actually directly connected in the original graph.
189 (There are no file&ndash;file edges in the build dependency graph).
190</p>
191
192<p>
193 However, while all operators <em>preserve</em> order, some
194 operations, such as the <a href='#set-operations'>set operations</a>
195 don't <em>introduce</em> any ordering constraints of their own.
196 Consider this expression:
197</p>
198
199<pre>deps(x) union y</pre>
200
201<p>
202 The order of the final result set is guaranteed to preserve all the
203 ordering constraints of its subexpressions, namely, that all the
204 transitive dependencies of <code>x</code> are correctly ordered with
205 respect to each other. However, the query guarantees nothing about
206 the ordering of the targets in <code>y</code>, nor about the
207 ordering of the targets in <code>deps(x)</code> relative to those in
208 <code>y</code> (except for those targets in
209 <code>y</code> that also happen to be in <code>deps(x)</code>).
210</p>
211
212<p>
213 Operators that introduce ordering constraints include:
214 <code>allpaths</code>,
215 <code>deps</code>,
216 <code>rdeps</code>,
217 <code>somepath</code>,
218 and the target pattern wildcards
219 <code>package:*</code>,
220 <code>dir/...</code>, etc.
221</p>
222
223<h2>Expressions: syntax and semantics of the grammar</h2>
224
225<p>
226 This is the grammar of the Bazel query language, expressed in EBNF
227 notation:
228</p>
229
230
231<pre>expr ::= <var>word</var>
232 | let <var>name</var> = <var>expr</var> in <var>expr</var>
233 | (<var>expr</var>)
234 | <var>expr</var> intersect <var>expr</var>
235 | <var>expr</var> ^ <var>expr</var>
236 | <var>expr</var> union <var>expr</var>
237 | <var>expr</var> + <var>expr</var>
238 | <var>expr</var> except <var>expr</var>
239 | <var>expr</var> - <var>expr</var>
David Chen8fe82a32016-08-24 10:55:41 +0000240 | set(<var>word</var> *)
lberkiaf70ce52017-06-12 18:15:29 +0200241 | <var>word</var> '(' <var>int</var> | <var>word</var> | <var>expr</var> ... ')'
David Chen8fe82a32016-08-24 10:55:41 +0000242</pre>
243
244<p>
245 We will examine each of the productions of this grammar in order.
246</p>
247
248<h3 id="target-patterns">Target patterns</h3>
249<pre>expr ::= <var>word</var></pre>
250<p>
251 Syntactically, a <em>target pattern</em> is just a word. It
252 is interpreted as an (unordered) set of targets. The simplest
253 target pattern is a label,
254 which identifies a single target (file or rule). For example, the
255 target pattern <code>//foo:bar</code> evaluates to a set
256 containing one element, the target, the <code>bar</code>
257 rule.
258</p>
259
260<p>
261 Target patterns generalize labels to include wildcards over packages
262 and targets. For example, <code>foo/...:all</code> (or
263 just <code>foo/...</code>) is a target pattern that evaluates to a
264 set containing all <em>rules</em> in every package recursively
265 beneath the <code>foo</code> directory;
266 <code>bar/baz:all</code> is a target pattern that
267 evaluates to a set containing all the rules in the
268 <code>bar/baz</code> package, but not its subpackages.
269</p>
270
271<p>
272 Similarly, <code>foo/...:*</code> is a target pattern that evaluates
273 to a set containing all <em>targets</em> (rules <em>and</em> files) in
274 every package recursively beneath the <code>foo</code> directory;
275 <code>bar/baz:*</code> evaluates to a set containing
276 all the targets in the
277 <code>bar/baz</code> package, but not its subpackages.
278</p>
279
280<p>
281 Because the <code>:*</code> wildcard matches files as well as rules,
282 it is often more useful than <code>:all</code> for queries.
283 Conversely, the <code>:all</code> wildcard (implicit in target
284 patterns like <code>foo/...</code>) is typically more useful for
285 builds.
286</p>
287
288<p>
289 <code>bazel query</code> target patterns work the same as
290 <code>bazel build</code> build targets do;
291 refer to <a href='bazel-user-manual.html#target-patterns'>Target Patterns</a>
292 in the Bazel User Manual for further details, or type <code>bazel
293 help target-syntax</code>.
294
295</p>
296
297<p>
298 Target patterns may evaluate to a singleton set (in the case of a
299 label), to a set containing many elements (as in the case of
300 <code>foo/...</code>, which has thousands of elements) or to the
301 empty set, if the target pattern matches no targets.
302</p>
303
304<p>
305 All nodes in the result of a target pattern expression are correctly
306 ordered relative to each other according to the dependency relation.
307 So, the result of <code>foo:*</code> is not just the set of targets
308 in package <code>foo</code>, it is also the <em>graph</em> over
309 those targets. (No guarantees are made about the relative ordering
310 of the result nodes against other nodes.) See the section
311 on <a href='#graph-order'>graph order</a> for more details.
312</p>
313
314<h3 id="variables">Variables</h3>
315<pre>expr ::= let <var>name</var> = <var>expr</var><sub>1</sub> in <var>expr</var><sub>2</sub>
316 | <var>$name</var></pre>
317<p>
318 The Bazel query language allows definitions of and references to
319 variables. The
320 result of evaluation of a <code>let</code> expression is the same as
321 that of <var>expr</var><sub>2</sub>, with all free occurrences of
322 variable <var>name</var> replaced by the value of
323 <var>expr</var><sub>1</sub>.
324</p>
325
326<p>
327 For example, <code>let v = foo/... in allpaths($v, //common)
328 intersect $v</code> is equivalent to the <code>allpaths(foo/...,
329 //common) intersect foo/...</code>.
330</p>
331
332<p>
333 An occurrence of a variable reference <code>name</code> other than in
334 an enclosing <code>let <var>name</var> = ...</code> expression is an
335 error. In other words, toplevel query expressions cannot have free
336 variables.
337</p>
338
339<p>
340 In the above grammar productions, <code>name</code> is like
341 <em>word</em>, but with the additional constraint that it be a legal
342 identifier in the C programming language. References to the variable
343 must be prepended with the "$" character.
344</p>
345
346<p>
347 Each <code>let</code> expression defines only a single variable,
348 but you can nest them.
349</p>
350
351<p>
352 (Both <a
353 href='#target-patterns'>target patterns</a> and variable references
354 consist of just a single token, a word, creating a syntactic
355 ambiguity. However, there is no semantic ambiguity, because the
356 subset of words that are legal variable names is disjoint from the
357 subset of words that are legal target patterns.)
358</p>
359
360<p>
361 (Technically speaking, <code>let</code> expressions do not increase
362 the expressiveness of the query language: any query expressible in
363 the language can also be expressed without them. However, they
364 improve the conciseness of many queries, and may also lead to more
365 efficient query evaluation.)
366</p>
367
368<h3 id="parentheses">Parenthesized expressions</h3>
369<pre>expr ::= (<var>expr</var>)</pre>
370
371<p>
372 Parentheses associate subexpressions to force an
373 order of evaluation.
374 A parenthesized expression evaluates
375 to the value of its argument.
376</p>
377
378<h3 id="set-operations">Algebraic set operations:
379 intersection, union, set difference</h3>
380
381<pre>expr ::= <var>expr</var> intersect <var>expr</var>
382 | <var>expr</var> ^ <var>expr</var>
383 | <var>expr</var> union <var>expr</var>
384 | <var>expr</var> + <var>expr</var>
385 | <var>expr</var> except <var>expr</var>
386 | <var>expr</var> - <var>expr</var>
387</pre>
388
389<p>
390 These three operators compute the usual set operations over their
391 arguments. Each operator has two forms, a nominal form such
392 as <code>intersect</code> and a symbolic form such
393 as <code>^</code>. Both forms are equivalent;
394 the symbolic forms are quicker to type. (For clarity, the rest of
395 this manual uses the nominal forms.) For example,
396</p>
397
398<pre>foo/... except foo/bar/...</pre>
399
400 evaluates to the set of targets that match
401 <code>foo/...</code> but not
402 <code>foo/bar/...</code>&nbsp;. Equivalently:
403
404<pre>foo/... - foo/bar/...</pre>
405
406 The <code>intersect</code> (<code>^</code>)
407 and <code>union</code> (<code>+</code>) operations are commutative
408 (symmetric); <code>except</code> (<code>-</code>) is
409 asymmetric. The parser treats all three operators as
410 left-associative and of equal precedence, so you might want parentheses.
411 For example, the first two of these expressions are
412 equivalent, but the third is not:
413
414<pre>x intersect y union z
415(x intersect y) union z
416x intersect (y union z)</pre>
417
418<p>
419 (We strongly recommend that you use parentheses where there is
420 any danger of ambiguity in reading a query expression.)
421</p>
422
423<h3 id="set">Read targets from an external source: set</h3>
424<pre>expr ::= set(<var>word</var> *) </pre>
425<p>
426 The <code>set(<var>a</var> <var>b</var> <var>c</var> ...)</code>
427 operator computes the union of a set of zero or
428 more <a href='#target-patterns'>target patterns</a>, separated by
429 whitespace (no commas).
430</p>
431
432<p>
433 In conjunction with the Bourne shell's <code>$(...)</code>
434 feature, <code>set()</code> provides a means of saving the results
435 of one query in a regular text file, manipulating that text file
436 using other programs (e.g. standard UNIX shell tools), and then
437 introducing the result back into the query tool as a value for
438 further processing. For example:
439</p>
440<pre>
laszlocsomor79251002017-04-03 13:09:47 +0000441 bazel query deps(//my:target) --output=label | grep ... | sed ... | awk ... &gt; foo
442 bazel query "kind(cc_binary, set($(&lt;foo)))"
David Chen8fe82a32016-08-24 10:55:41 +0000443</pre>
444<p>
445 In the next example, <code>kind(cc_library,
446 deps(//some_dir/foo:main, 5))</code> is effectively computed
447 by filtering on the <code>maxrank</code> values using
448 an <code>awk</code> program.
449</p>
450<pre>
laszlocsomor79251002017-04-03 13:09:47 +0000451 bazel query 'deps(//some_dir/foo:main)' --output maxrank |
David Chen8fe82a32016-08-24 10:55:41 +0000452 awk '($1 &lt; 5) { print $2;} ' &gt; foo
laszlocsomor79251002017-04-03 13:09:47 +0000453 bazel query "kind(cc_library, set($(&lt;foo)))"
David Chen8fe82a32016-08-24 10:55:41 +0000454</pre>
455<p>
456 In these examples, <code>$(&lt;foo)</code> is a shorthand
457 for <code>$(cat foo)</code>, but shell commands other
458 than <code>cat</code> may be used too&mdash;such as
459 the previous <code>awk</code> command.
460</p>
461
462<p>
463 Note, <code>set()</code> introduces no graph ordering constraints,
464 so path information may be lost when saving and reloading sets of
465 nodes using it. See the <a href='#graph-order'>graph order</a>
466 section below for more detail.
467</p>
468
lberkiaf70ce52017-06-12 18:15:29 +0200469<h3 id="functions">Functions</h3>
470<pre>expr ::= <var>word</var> '(' <var>int</var> | <var>word</var> | <var>expr</var> ... ')'</pre>
471<p>
472 The query language defines several functions. The name of the function
473 determines the number and type of arguments it requires. The following
474 functions are available:
475</p>
476
477<code><!-- keep this alphabetically sorted -->
478<a href="#path-operators">allpaths</a><br/>
479<a href="#attr">attr</a><br/>
480
481<a href="#buildfiles">buildfiles</a><br/>
482
483<a href="#deps">deps</a><br/>
484<a href="#filter">filter</a><br/>
485<a href="#kind">kind</a><br/>
486<a href="#labels">labels</a><br/>
487<a href="#loadfiles">loadfiles</a><br/>
488<a href="#rdeps">rdeps</a><br/>
489<a href="#some">some</a><br/>
490<a href="#path-operators">somepath</a><br/>
491<a href="#tests">tests</a><br/>
492</code>
493
David Chen8fe82a32016-08-24 10:55:41 +0000494<h3 id="deps">Transitive closure of dependencies: deps</h3>
495<pre>expr ::= deps(<var>expr</var>)
496 | deps(<var>expr</var>, <var>depth</var>)</pre>
497<p>
498 The <code>deps(<var>x</var>)</code> operator evaluates to the graph
499 formed by the transitive closure of dependencies of its argument set
500 <var>x</var>. For example, the value of <code>deps(//foo)</code> is
501 the dependency graph rooted at the single node <code>foo</code>,
502 including all its dependencies. The value of
503 <code>deps(foo/...)</code> is the dependency graphs whose roots are
504 all rules in every package beneath the <code>foo</code> directory.
505 Please note that 'dependencies' means only rule and file targets
506 in this context, therefore the BUILD,
507
508 and Skylark files needed to
509 create these targets are not included here. For that you should use the
510 <a href="#buildfiles"><code>buildfiles</code></a> operator.
511</p>
512
513<p>
514 The resulting graph is ordered according to the dependency relation.
515 See the section on <a href='#graph-order'>graph order</a> for more
516 details.
517</p>
518
519<p>
520 The <code>deps</code> operator accepts an optional second argument,
521 which is an integer literal specifying an upper bound on the depth
522 of the search. So <code>deps(foo:*, 1)</code> evaluates to all the
523 direct prerequisites of any target in the <code>foo</code> package,
524 and <code>deps(foo:*, 2)</code> further includes the nodes directly
525 reachable from the nodes in <code>deps(foo:*, 1)</code>, and so on.
526 (These numbers correspond to the ranks shown in
527 the <a href='#output-ranked'><code>minrank</code></a> output
528 format.) If the <var>depth</var> parameter is omitted, the search
529 is unbounded, i.e. it computes the reflexive transitive closure of
530 prerequsites.
531</p>
532
533<h3 id="rdeps">Transitive closure of reverse dependencies: rdeps</h3>
534<pre>expr ::= rdeps(<var>expr</var>, <var>expr</var>)
535 | rdeps(<var>expr</var>, <var>expr</var>, <var>depth</var>)</pre>
536<p>
537 The <code>rdeps(<var>u</var>, <var>x</var>)</code> operator evaluates
538 to the reverse dependencies of the argument set <var>x</var> within the
539 transitive closure of the universe set <var>u</var>.
540</p>
541
542<p>
543 The resulting graph is ordered according to the dependency relation. See the
544 section on <a href='#graph-order'>graph order</a> for more details.
545</p>
546
547<p>
548 The <code>rdeps</code> operator accepts an optional third argument,
549 which is an integer literal specifying an upper bound on the depth of the
550 search. The resulting graph will only include nodes within a distance of the
551 specified depth from any node in the argument set. So
552 <code>rdeps(//foo, //common, 1)</code> evaluates to all nodes in the
553 transitive closure of <code>//foo</code> that directly depend on
554 <code>//common</code>. (These numbers correspond to the ranks shown in the
555 <a href='#output-ranked'><code>minrank</code></a> output format.) If the
556 <var>depth</var> parameter is omitted, the search is unbounded.
557</p>
558
559<h3 id="some">Arbitrary choice: some</h3>
560<pre>expr ::= some(<var>expr</var>)</pre>
561<p>
562 The <code>some(<var>x</var>)</code> operator selects one target
563 arbitrarily from its argument set <var>x</var>, and evaluates to a
564 singleton set containing only that target. For example, the
565 expression <code>some(//foo:main union //bar:baz)</code>
566 evaluates to a set containing either <code>//foo:main</code> or
567 <code>//bar:baz</code>&mdash;though which one is not defined.
568</p>
569
570<p>
571 If the argument is a singleton, then <code>some</code>
572 computes the identity function: <code>some(//foo:main)</code> is
573 equivalent to <code>//foo:main</code>.
574
575 It is an error if the specified argument set is empty, as in the
576 expression <code>some(//foo:main intersect //bar:baz)</code>.
577</p>
578
579<h3 id="path-operators">Path operators: somepath, allpaths</h3>
580<pre>expr ::= somepath(<var>expr</var>, <var>expr</var>)
581 | allpaths(<var>expr</var>, <var>expr</var>)</pre>
582<p>
583 The <code>somepath(<var>S</var>, <var>E</var>)</code> and
584 <code>allpaths(<var>S</var>, <var>E</var>)</code> operators compute
585 paths between two sets of targets. Both queries accept two
586 arguments, a set <var>S</var> of starting points and a set
587 <var>E</var> of ending points. <code>somepath</code> returns the
588 graph of nodes on <em>some</em> arbitrary path from a target in
589 <var>S</var> to a target in <var>E</var>; <code>allpaths</code>
590 returns the graph of nodes on <em>all</em> paths from any target in
591 <var>S</var> to any target in <var>E</var>.
592</p>
593
594<p>
595 The resulting graphs are ordered according to the dependency relation.
596 See the section on <a href='#graph-order'>graph order</a> for more
597 details.
598</p>
599
600<table style='margin: auto'><tr>
601<td style='text-align: center'>
Kristina Chodorowfc9750c2017-02-09 21:13:13 +0000602<div class='graphviz dot'><!--
603digraph somepath1 {
604 graph [size="4,4"]
605 node [label="",shape=circle];
606 n1;
607 n2 [fillcolor="pink",style=filled];
608 n3 [fillcolor="pink",style=filled];
609 n4 [fillcolor="pink",style=filled,label="E"];
610 n5; n6;
611 n7 [fillcolor="pink",style=filled,label="S1"];
612 n8 [label="S2"];
613 n9;
614 n10 [fillcolor="pink",style=filled];
615
616 n1 -> n2;
617 n2 -> n3;
618 n7 -> n5;
619 n7 -> n2;
620 n5 -> n6;
621 n6 -> n4;
622 n8 -> n6;
623 n6 -> n9;
624 n2 -> n10;
625 n3 -> n10;
626 n10 -> n4;
627 n10 -> n11;
628}
629--></div>
David Chen8fe82a32016-08-24 10:55:41 +0000630<p><code>somepath(S1 + S2, E)</code>,<br/>one possible result.</p>
631</td>
632<td style='padding: 40px; text-align: center'>
Kristina Chodorowfc9750c2017-02-09 21:13:13 +0000633<div class='graphviz dot'><!--
634digraph somepath2 {
635 graph [size="4,4"]
636 node [label="",shape=circle];
637
638 n1; n2; n3;
639 n4 [fillcolor="pink",style=filled,label="E"];
640 n5;
641 n6 [fillcolor="pink",style=filled];
642 n7 [label="S1"];
643 n8 [fillcolor="pink",style=filled,label="S2"];
644 n9; n10;
645
646 n1 -> n2;
647 n2 -> n3;
648 n7 -> n5;
649 n7 -> n2;
650 n5 -> n6;
651 n6 -> n4;
652 n8 -> n6;
653 n6 -> n9;
654 n2 -> n10;
655 n3 -> n10;
656 n10 -> n4;
657 n10 -> n11;
658}
659--></div>
David Chen8fe82a32016-08-24 10:55:41 +0000660<p><code>somepath(S1 + S2, E)</code>,<br/>another possible result.</p>
661</td>
662<td style='text-align: center'>
Kristina Chodorowfc9750c2017-02-09 21:13:13 +0000663<div class='graphviz dot'><!--
664digraph allpaths {
665 graph [size="4,4"]
666 node [label="",shape=circle];
667 n1;
668 n2 [fillcolor="pink",style=filled];
669 n3 [fillcolor="pink",style=filled];
670 n4 [fillcolor="pink",style=filled,label="E"];
671 n5 [fillcolor="pink",style=filled];
672 n6 [fillcolor="pink",style=filled];
673 n7 [fillcolor="pink",style=filled, label="S1"];
674 n8 [fillcolor="pink",style=filled, label="S2"];
675 n9;
676 n10 [fillcolor="pink",style=filled];
677
678 n1 -> n2;
679 n2 -> n3;
680 n7 -> n5;
681 n7 -> n2;
682 n5 -> n6;
683 n6 -> n4;
684 n8 -> n6;
685 n6 -> n9;
686 n2 -> n10;
687 n3 -> n10;
688 n10 -> n4;
689 n10 -> n11;
690}
691--></div>
David Chen8fe82a32016-08-24 10:55:41 +0000692<p><code>allpaths(S1 + S2, E)</code>.</p>
693</td>
694</tr></table>
695
696<h3 id="kind">Target kind filtering: kind</h3>
697<pre>expr ::= kind(<var>word</var>, <var>expr</var>) </pre>
698<p>
699 The <code>kind(<var>pattern</var>, <var>input</var>)</code> operator
700 applies a filter to a set of targets, and discards those targets
701 that are not of the expected kind. The <var>pattern</var> parameter specifies
702 what kind of target to match.
703</p>
704<ul>
705<li><b>file</b> patterns can be one of:
706 <ul>
707 <li><code>source file</code>
708 <li><code>generated file</code>
709 </ul>
710<li><b>rule</b> patterns can be one of:
711 <ul>
712 <li><code><var>ruletype</var> rule</code>
713 <li><code><var>ruletype</var></code><br>
714 Where <var>ruletype</var> is a build rule. The difference between these
715 forms is that including "rule" causes the regular expression match for
716 <var>ruletype</var> to be anchored.
717 </ul>
718<li><b>package group</b> patterns should simply be:
719 <ul>
720 <li><code>package group</code>
721 </ul>
722</ul>
723<p>
724 For example, the kinds for the four targets defined by the BUILD file
725 (for package <code>p</code>) shown below are illustrated in the
726 table:
727</p>
728
729<table style='margin: auto'><tr><td style='padding-right:10px'>
730<pre style='margin-left: 0em;'>
731genrule(
732 name = "a",
733 srcs = ["a.in"],
734 outs = ["a.out"],
735 cmd = "...",
736)
737</pre>
738</td><td>
739 <table class="grid">
740 <tr><th>Target</th><th>Kind</th></tr>
741 <tr class='tt'><td>//p:a</td><td>genrule rule</td></tr>
742 <tr class='tt'><td>//p:a.in</td><td>source file</td></tr>
743 <tr class='tt'><td>//p:a.out</td><td>generated file</td></tr>
744 <tr class='tt'><td>//p:BUILD</td><td>source file</td></tr>
745 </table>
746</td></tr></table>
747
748<p>
749 Thus, <code>kind("cc_.* rule", foo/...)</code> evaluates to the set
750 of all <code>cc_library</code>, <code>cc_binary</code>, etc,
751 rule targets beneath
752 <code>foo</code>, and <code>kind("source file", deps(//foo))</code>
753 evaluates to the set of all source files in the transitive closure
754 of dependencies of the <code>//foo</code> target.
755</p>
756
757<p>
758 Quotation of the <var>pattern</var> argument is often required
759 because without it, many regular expressions, such as <code>source
760 file</code> and <code>.*_test</code>, are not considered words by
761 the parser.
762</p>
763
764<p>
765 When matching for <code>package group</code>, targets ending in
766 <code>:all</code> may not yield any results.
767 Use <code>:all-targets</code> instead.
768</p>
769
770<h3 id="filter">Target name filtering: filter</h3>
771<pre>expr ::= filter(<var>word</var>, <var>expr</var>) </pre>
772<p>
773 The <code>filter(<var>pattern</var>, <var>input</var>)</code> operator
774 applies a filter to a set of targets, and discards targets whose
775 labels (in absolute form) do not match the pattern; it
776 evaluates to a subset of its input.
777</p>
778
779<p>
780 The first argument, <var>pattern</var> is a word containing a
781 regular expression over target names. A <code>filter</code> expression
782 evaluates to the set containing all targets <var>x</var> such that
783 <var>x</var> is a member of the set <var>input</var> and the
784 label (in absolute form, e.g. <code>//foo:bar</code>)
785 of <var>x</var> contains an (unanchored) match
786 for the regular expression <var>pattern</var>. Since all
787 target names start with <code>//</code>, it may be used as an alternative
788 to the <code>^</code> regular expression anchor.
789</p>
790
791<p>
792 This operator often provides a much faster and more robust alternative to the
793 <code>intersect</code> operator. For example, in order to see all
794 <code>bar</code> dependencies of the <code>//foo:foo</code> target, one could
795 evaluate
796</p>
797<pre>deps(//foo) intersect //bar/...</pre>
798<p>
799 This statement, however, will require parsing of all BUILD files in the
800 <code>bar</code> tree, which will be slow and prone to errors in
801 irrelevant BUILD files. An alternative would be:
802</p>
803<pre>filter(//bar, deps(//foo))</pre>
804<p>
805 which would first calculate the set of <code>//foo</code> dependencies and
806 then would filter only targets matching the provided pattern&mdash;in other
807 words, targets with names containing <code>//bar</code> as a
808 substring.
809</p>
810
811<p>
812 Another common use of the <code>filter(<var>pattern</var>,
813 <var>expr</var>)</code> operator is to filter specific files by their
814 name or extension. For example,
815</p>
816<pre>filter("\.cc$", deps(//foo))</pre>
817<p>
818 will provide a list of all <code>.cc</code> files used to build
819 <code>//foo</code>.
820</p>
821
822<h3 id="attr">Rule attribute filtering: attr</h3>
823<pre>expr ::= attr(<var>word</var>, <var>word</var>, <var>expr</var>) </pre>
824<p>
825 The <code>attr(<var>name</var>, <var>pattern</var>, <var>input</var>)</code>
826 operator applies a filter to a set of targets, and discards targets that
827 are not rules, rule targets that do not have attribute <var>name</var>
828 defined or rule targets where the attribute value does not match the provided
829 regular expression <var>pattern</var>; it evaluates to a subset of its input.
830</p>
831
832<p>
833 The first argument, <var>name</var> is the name of the rule attribute that
834 should be matched against the provided regular expression pattern. The second
835 argument, <var>pattern</var> is a regular expression over the attribute
836 values. An <code>attr</code> expression evaluates to the set containing all
837 targets <var>x</var> such that <var>x</var> is a member of the set
838 <var>input</var>, is a rule with the defined attribute <var>name</var> and
839 the attribute value contains an (unanchored) match for the regular expression
840 <var>pattern</var>. Please note, that if <var>name</var> is an optional
841 attribute and rule does not specify it explicitly then default attribute
842 value will be used for comparison. For example,
843</p>
844<pre>attr(linkshared, 0, deps(//foo))</pre>
845<p>
846 will select all <code>//foo</code> dependencies that are allowed to have a
847 linkshared attribute (e.g., <code>cc_binary</code> rule) and have it either
848 explicitly set to 0 or do not set it at all but default value is 0 (e.g. for
849 <code>cc_binary</code> rules).
850</p>
851
852<p>
853 List-type attributes (such as <code>srcs</code>, <code>data</code>, etc) are
854 converted to strings of the form <code>[value<sub>1</sub>, ..., value<sub>n</sub>]</code>,
855 starting with a <code>[</code> bracket, ending with a <code>]</code> bracket
856 and using "<code>, </code>" (comma, space) to delimit multiple values.
857 Labels are converted to strings by using the absolute form of the
858 label. For example, an attribute <code>deps=[":foo",
859 "//otherpkg:bar", "wiz"]</code> would be converted to the
860 string <code>[//thispkg:foo, //otherpkg:bar, //thispkg:wiz]</code>.
861 Brackets
862 are always present, so the empty list would use string value <code>[]</code>
863 for matching purposes. For example,
864</p>
865<pre>attr("srcs", "\[\]", deps(//foo))</pre>
866<p>
867 will select all rules among <code>//foo</code> dependencies that have an
868 empty <code>srcs</code> attribute, while
869</p>
870<pre>attr("data", ".{3,}", deps(//foo))</pre>
871<p>
872 will select all rules among <code>//foo</code> dependencies that specify at
873 least one value in the <code>data</code> attribute (every label is at least
874 3 characters long due to the <code>//</code> and <code>:</code>).
875</p>
876
877<h3 id="visible">Rule visibility filtering: visible</h3>
878<pre>expr ::= visible(<var>expr</var>, <var>expr</var>) </pre>
879<p>
880 The <code>visible(<var>predicate</var>, <var>input</var>)</code> operator
881 applies a filter to a set of targets, and discards targets without the
882 required visibility.
883</p>
884
885<p>
886 The first argument, <var>predicate</var>, is a set of targets that all targets
887 in the output must be visible to. A <var>visible</var> expression
888 evaluates to the set containing all targets <var>x</var> such that <var>x</var>
889 is a member of the set <var>input</var>, and for all targets <var>y</var> in
890 <var>predicate</var> <var>x</var> is visible to <var>y</var>. For example:
891</p>
892<pre>visible(//foo, //bar:*)</pre>
893<p>
894 will select all targets in the package <code>//bar</code> that <code>//foo</code>
895 can depend on without violating visibility restrictions.
896</p>
897
898<h3 id="labels">Evaluation of rule attributes of type label: labels</h3>
899<pre>expr ::= labels(<var>word</var>, <var>expr</var>) </pre>
900<p>
901 The <code>labels(<var>attr_name</var>, <var>inputs</var>)</code>
902 operator returns the set of targets specified in the
903 attribute <var>attr_name</var> of type "label" or "list of label" in
904 some rule in set <var>inputs</var>.
905</p>
906
907<p>
908 For example, <code>labels(srcs, //foo)</code> returns the set of
909 targets appearing in the <code>srcs</code> attribute of
910 the <code>//foo</code> rule. If there are multiple rules
911 with <code>srcs</code> attributes in the <var>inputs</var> set, the
912 union of their <code>srcs</code> is returned.
913</p>
914
David Chen8fe82a32016-08-24 10:55:41 +0000915<h3 id="tests">Expand and filter test_suites: tests</h3>
916<pre>expr ::= tests(<var>expr</var>)</pre>
917<p>
918 The <code>tests(<var>x</var>)</code> operator returns the set of all test
919 rules in set <var>x</var>, expanding any <code>test_suite</code> rules into
920 the set of individual tests that they refer to, and applying filtering by
921 <code>tag</code> and <code>size</code>.
922
923 By default, query evaluation
924 ignores any non-test targets in all <code>test_suite</code> rules. This can be
925 changed to errors with the <code>--strict_test_suite</code> option.
926</p>
927
928<p>
929 For example, the query <code>kind(test, foo:*)</code> lists all
930 the <code>*_test</code> and <code>test_suite</code> rules
931 in the <code>foo</code> package. All the results are (by
932 definition) members of the <code>foo</code> package. In contrast,
933 the query <code>tests(foo:*)</code> will return all of the
934 individual tests that would be executed by <code>bazel test
935 foo:*</code>: this may include tests belonging to other packages,
936 that are referenced directly or indirectly
937 via <code>test_suite</code> rules.
938</p>
939
940
941<h3 id="buildfiles">Package definition files: buildfiles</h3>
942<pre>expr ::= buildfiles(<var>expr</var>)</pre>
943<p>
944 The <code>buildfiles(<var>x</var>)</code> operator returns the set
945 of files that define the packages of each target in
946 set <var>x</var>; in other words, for each package, its BUILD file,
947 plus any files it references
948
949 via <code>load</code>. Note that this also returns the BUILD files of the
950 packages containing these <code>load</code>ed files.
951</p>
952
953<p>
954 This operator is typically used when determining what files or
955 packages are required to build a specified target, often in conjunction with
956 the <a href='#output-package'><code>--output package</code></a>
957 option, below). For example,
958</p>
959<pre>bazel query 'buildfiles(deps(//foo))' --output package</pre>
960<p>
961 returns the set of all packages on which <code>//foo</code> transitively
962 depends.
963</p>
964
965<p>
966 (Note: a naive attempt at the above query would omit
967 the <code>buildfiles</code> operator and use only <code>deps</code>,
968 but this yields an incorrect result: while the result contains the
969 majority of needed packages, those packages that contain only files
970 that are <code>load()</code>'ed
971
972 will be missing.
973</p>
974
975<h3 id="loadfiles">Package definition files: loadfiles</h3>
976<pre>expr ::= loadfiles(<var>expr</var>)</pre>
977<p>
978 The <code>loadfiles(<var>x</var>)</code> operator returns the set of
979 Skylark files that are needed to load the packages of each target in
980 set <var>x</var>. In other words, for each package, it returns the
981 .bzl files that are referenced from its BUILD files.
982</p>
983
984<h2>Output formats</h2>
985
986<p>
987 <code>bazel query</code> generates a graph.
988 You specify the content, format, and ordering by which
989 <code>bazel query</code> presents this graph
990 by means of the <code>--output</code>
991 command-line option.
992 </p>
993
994<p>
995 Some of the output formats accept additional options. The name of
996 each output option is prefixed with the output format to which it
997 applies, so <code>--graph:factored</code> applies only
998 when <code>--output=graph</code> is being used; it has no effect if
999 an output format other than <code>graph</code> is used. Similarly,
1000 <code>--xml:line_numbers</code> applies only when <code>--output=xml</code>
1001 is being used.
1002</p>
1003
1004<h3 id='result-order'>On the ordering of results</h3>
1005
1006<p>
1007 Although query expressions always follow the "<a href='#graph-order'>law of
1008 conservation of graph order</a>", <i>presenting</i> the results may be done
1009 in either a dependency-ordered or unordered manner. This does <b>not</b>
1010 influence the targets in the result set or how the query is computed. It only
1011 affects how the results are printed to stdout. Moreover, nodes that are
1012 equivalent in the dependency order may or may not be ordered alphabetically.
1013 The <code>--order_output</code> flag can be used to control this behavior.
1014 (The <code>--[no]order_results</code> flag has a subset of the functionality
1015 of the <code>--order_output</code> flag and is deprecated.)
1016</p>
1017<p>
1018 The default value of this flag is <code>auto</code>, which is equivalent to
1019 <code>full</code> for every output format except for <code>proto</code>,
1020 <code>graph</code>, <code>minrank</code>, and <code>maxrank</code>, for which
1021 it is equivalent to <code>deps</code>.
1022</p>
1023<p>
1024 When this flag is <code>no</code> and <code>--output</code> is one of
1025 <code>build</code>, <code>label</code>, <code>label_kind</code>,
1026 <code>location</code>, <code>package</code>, <code>proto</code>,
1027 <code>record</code> or <code>xml</code>, the outputs will be printed in
1028 arbitrary order. <b>This is generally the fastest option</b>. It is not
1029 supported though when <code>--output</code> is one of <code>graph</code>,
1030 <code>min_rank</code> or <code>max_rank</code>: with these formats, bazel will
1031 always print results ordered by the dependency order or rank.
1032</p>
1033<p>
1034 When this flag is <code>deps</code>, bazel will print results ordered by the
1035 dependency order. However, nodes that are unordered by the dependency order
1036 (because there is no path from either one to the other) may be printed in any
1037 order.
1038</p>
1039<p>
1040 When this flag is <code>full</code>, bazel will print results ordered by the
1041 dependency order, with unordered nodes ordered alphabetically or reverse
1042 alphabetically, depending on the output format. This may be slower than the
1043 other options, and so should only be used when deterministic results are
1044 important &mdash; it is guaranteed with this option that running the same query
1045 multiple times will always produce the same output.
1046</p>
1047
1048<h3 id="output-build">Print the source form of targets as they would appear in BUILD</h3>
1049<pre>--output build</pre>
1050<p>
1051 With this option, the representation of each target is as if it were
1052 hand-written in the BUILD language. All variables and function calls
1053 (e.g. glob, macros) are expanded, which is useful for seeing the effect
1054 of Skylark macros. Additionally, each effective rule is annotated with
1055 the name of the macro (if any, see <code>generator_name</code> and
1056 <code>generator_function</code>) that produced it.
1057</p>
1058<p>
1059 Although the output uses the same syntax as BUILD files, it is not
1060 guaranteed to produce a valid BUILD file.
1061</p>
1062
1063<h3 id="output-label">Print the label of each target</h3>
1064<pre>--output label</pre>
1065<p>
1066 With this option, the set of names (or <em>labels</em>) of each target
1067 in the resulting graph is printed, one label per line, in
1068 topological order (unless <code>--noorder_results</code> is specified, see
1069 <a href='#result-order'>notes on the ordering of results</a>).
1070 (A topological ordering is one in which a graph
1071 node appears earlier than all of its successors.) Of course there
1072 are many possible topological orderings of a graph (<em>reverse
1073 postorder</em> is just one); which one is chosen is not specified.
1074
1075 When printing the output of a <code>somepath</code> query, the order
1076 in which the nodes are printed is the order of the path.
1077</p>
1078
1079<p>
1080 Caveat: in some corner cases, there may be two distinct targets with
1081 the same label; for example, a <code>sh_binary</code> rule and its
1082 sole (implicit) <code>srcs</code> file may both be called
1083 <code>foo.sh</code>. If the result of a query contains both of
1084 these targets, the output (in <code>label</code> format) will appear
1085 to contain a duplicate. When using the <code>label_kind</code> (see
1086 below) format, the distinction becomes clear: the two targets have
1087 the same name, but one has kind <code>sh_binary rule</code> and the
1088 other kind <code>source file</code>.
1089</p>
1090
1091<h3 id="output-label_kind">Print the label and kind of each target</h3>
1092<pre>--output label_kind</pre>
1093<p>
1094 Like <code>label</code>, this output format prints the labels of
1095 each target in the resulting graph, in topological order, but it
1096 additionally precedes the label by
1097 the <a href='#kind'><em>kind</em></a> of the target.
1098</p>
1099
1100<h3 id="output-ranked">Print the label of each target, in rank order</h3>
1101<pre>--output minrank
1102--output maxrank</pre>
1103<p>
1104 Like <code>label</code>, the <code>minrank</code>
1105 and <code>maxrank</code> output formats print the labels of each
1106 target in the resulting graph, but instead of appearing in
1107 topological order, they appear in rank order, preceded by their
1108 rank number. These are unaffected by the result ordering
1109 <code>--[no]order_results</code> flag (see <a href='#result-order'>notes on
1110 the ordering of results</a>).
1111</p>
1112
1113<p>
1114 There are two variants of this format: <code>minrank</code> ranks
1115 each node by the length of the shortest path from a root node to it.
1116 "Root" nodes (those which have no incoming edges) are of rank 0,
1117 their successors are of rank 1, etc. (As always, edges point from a
1118 target to its prerequisites: the targets it depends upon.)
1119</p>
1120
1121<p>
1122 <code>maxrank</code> ranks each node by the length of the longest
1123 path from a root node to it. Again, "roots" have rank 0, all other
1124 nodes have a rank which is one greater than the maximum rank of all
1125 their predecessors.
1126</p>
1127
1128<p>
1129 All nodes in a cycle are considered of equal rank. (Most graphs are
1130 acyclic, but cycles do occur
1131 simply because BUILD files contain erroneous cycles.)
1132</p>
1133
1134<p>
1135 These output formats are useful for discovering how deep a graph is.
1136 If used for the result of a <code>deps(x)</code>, <code>rdeps(x)</code>,
1137 or <code>allpaths</code> query, then the rank number is equal to the
1138 length of the shortest (with <code>minrank</code>) or longest
1139 (with <code>maxrank</code>) path from <code>x</code> to a node in
1140 that rank. <code>maxrank</code> can be used to determine the
1141 longest sequence of build steps required to build a target.
1142</p>
1143
1144<p>
1145 Please note, the ranked output of a <code>somepath</code> query is
1146 basically meaningless because <code>somepath</code> doesn't
1147 guarantee to return either a shortest or a longest path, and it may
1148 include "transitive" edges from one path node to another that are
1149 not direct edges in original graph.
1150</p>
1151
1152<p>
1153 For example, the graph on the left yields the outputs on the right
1154 when <code>--output minrank</code> and <code>--output maxrank</code>
1155 are specified, respectively.
1156</p>
1157
1158<table style='margin: auto'><tr><td>
Kristina Chodorowfc9750c2017-02-09 21:13:13 +00001159<div class='graphviz dot'><!--
1160digraph mygraph {
1161 node [shape=box];
1162"//a:a" -> "//a:a.cc"
1163"//b:b" -> "//a:a"
1164"//b:b" -> "//b:b.cc"
1165"//c:c" -> "//b:b"
1166"//c:c" -> "//a:a"
1167}
1168--></div>
David Chen8fe82a32016-08-24 10:55:41 +00001169</td><td>
1170<pre>
1171minrank
1172
11730 //c:c
11741 //b:b
11751 //a:a
11762 //b:b.cc
11772 //a:a.cc
1178</pre>
1179</td><td>
1180<pre>
1181maxrank
1182
11830 //c:c
11841 //b:b
11852 //a:a
11862 //b:b.cc
11873 //a:a.cc
1188</pre>
1189</td></tr></table>
1190
1191<h3 id="output-location">Print the location of each target</h3>
1192<pre>--output location</pre>
1193<p>
1194 Like <code>label_kind</code>, this option prints out, for each
1195 target in the result, the target's kind and label, but it is
1196 prefixed by a string describing the location of that target, as a
1197 filename and line number. The format resembles the output of
1198 <code>grep</code>. Thus, tools that can parse the latter (such as Emacs
1199 or vi) can also use the query output to step through a series of
1200 matches, allowing the Bazel query tool to be used as a
1201 dependency-graph-aware "grep for BUILD files".
1202</p>
1203
1204<p>
1205 The location information varies by target kind (see the <a
1206 href='#kind'>kind</a> operator). For rules, the
1207 location of the rule's declaration within the BUILD file is printed.
1208 For source files, the location of line 1 of the actual file is
1209 printed. For a generated file, the location of the rule that
1210 generates it is printed. (The query tool does not have sufficient
1211 information to find the actual location of the generated file, and
1212 in any case, it might not exist if a build has not yet been
1213 performed.)
1214</p>
1215
1216<h3 id="output-package">Print the set of packages</h3>
1217<pre>--output package</pre>
1218<p>
1219 This option prints the name of all packages to which
1220 some target in the result set belongs. The names are printed in
1221 lexicographical order; duplicates are excluded. Formally, this
1222 is a <em>projection</em> from the set of labels (package, target) onto
1223 packages.
1224</p>
1225
1226<p>
mschaller3c566c62017-06-22 00:11:44 +02001227 Packages in external repositories are formatted as
1228 <code>@repo//foo/bar</code> while packages in the main repository are
1229 formatted as <code>foo/bar</code>.
1230</p>
1231<p>
ajmichael0cce52e2017-06-08 18:40:00 +02001232 In conjunction with the <code>deps(...)</code> query, this output
1233 option can be used to find the set of packages that must be checked
1234 out in order to build a given set of targets.
1235</p>
1236
1237<h3 id="output-graph">Display a graph of the result</h3>
1238<pre>--output graph</pre>
1239<p>
1240 This option causes the query result to be printed as a directed
1241 graph in the popular AT&amp;T GraphViz format. Typically the
1242 result is saved to a file, such as <code>.png</code> or <code>.svg</code>.
1243 (If the <code>dot</code> program is not installed on your workstation, you
1244 can install it using the command <code>sudo apt-get install graphviz</code>.)
1245 See the example section below for a sample invocation.
1246</p>
1247
1248<p>
1249 This output format is particularly useful for <code>allpath</code>,
1250 <code>deps</code>, or <code>rdeps</code> queries, where the result
1251 includes a <em>set of paths</em> that cannot be easily visualized when
1252 rendered in a linear form, such as with <code>--output label</code>.
1253</p>
1254
1255<p>
1256 By default, the graph is rendered in a <em>factored</em> form. That is,
1257 topologically-equivalent nodes are merged together into a single
1258 node with multiple labels. This makes the graph more compact
1259 and readable, because typical result graphs contain highly
1260 repetitive patterns. For example, a <code>java_library</code> rule
1261 may depend on hundreds of Java source files all generated by the
1262 same <code>genrule</code>; in the factored graph, all these files
1263 are represented by a single node. This behavior may be disabled
1264 with the <code>--nograph:factored</code> option.
1265</p>
1266
1267<h4><code>--graph:node_limit <var>n</var></code></h4>
1268<p>
1269 The option specifies the maximum length of the label string for a
1270 graph node in the output. Longer labels will be truncated; -1
1271 disables truncation. Due to the factored form in which graphs are
1272 usually printed, the node labels may be very long. GraphViz cannot
1273 handle labels exceeding 1024 characters, which is the default value
1274 of this option. This option has no effect unless
1275 <code>--output=graph</code> is being used.
1276</p>
1277
1278<h4><code>--[no]graph:factored</code></h4>
1279<p>
1280 By default, graphs are displayed in factored form, as explained
1281 <a href='#output-graph'>above</a>.
1282 When <code>--nograph:factored</code> is specified, graphs are
1283 printed without factoring. This makes visualization using GraphViz
1284 impractical, but the simpler format may ease processing by other
1285 tools (e.g. grep). This option has no effect
1286 unless <code>--output=graph</code> is being used.
1287</p>
1288
David Chen8fe82a32016-08-24 10:55:41 +00001289<h3 id="output-xml">XML</h3>
1290<pre>--output xml</pre>
1291<p>
1292 This option causes the resulting targets to be printed in an XML
1293 form. The output starts with an XML header such as this
1294</p>
1295<pre>
1296 &lt;?xml version="1.0" encoding="UTF-8"?&gt;
1297 &lt;query version="2"&gt;
1298</pre>
1299<!-- The docs should continue to document version 2 into perpetuity,
1300 even if we add new formats, to handle clients synced to old CLs. -->
1301<p>
1302 and then continues with an XML element for each target
1303 in the result graph, in topological order (unless
1304 <a href='#result-order'>unordered results</a> are requested),
1305 and then finishes with a terminating
1306</p>
1307<pre>
1308&lt;/query&gt;
1309</pre>
1310<p>
1311 Simple entries are emitted for targets of <code>file</code>
1312 kind:
1313</p>
1314<pre>
1315 &lt;source-file name='//foo:foo_main.cc' .../&gt;
1316 &lt;generated-file name='//foo:libfoo.so' .../&gt;
1317</pre>
1318<p>
1319 But for rules, the XML is structured and contains definitions of all
1320 the attributes of the rule, including those whose value was not
1321 explicitly specified in the rule's BUILD file.
1322</p>
1323<p>
1324 Additionally, the result includes <code>rule-input</code> and
1325 <code>rule-output</code> elements so that the topology of the
1326 dependency graph can be reconstructed without having to know that,
1327 for example, the elements of the <code>srcs</code> attribute are
1328 forward dependencies (prerequisites) and the contents of the
1329 <code>outs</code> attribute are backward dependencies (consumers).
1330
1331 <code>rule-input</code> elements for <a
1332 href='#implicit_deps'>implicit dependencies</a> are suppressed if
1333 <code>--noimplicit_deps</code> is specified.
1334</p>
1335<pre>
1336 &lt;rule class='cc_binary rule' name='//foo:foo' ...&gt;
1337 &lt;list name='srcs'&gt;
1338 &lt;label value='//foo:foo_main.cc'/&gt;
1339 &lt;label value='//foo:bar.cc'/&gt;
1340 ...
1341 &lt;/list&gt;
1342 &lt;list name='deps'&gt;
1343 &lt;label value='//common:common'/&gt;
1344 &lt;label value='//collections:collections'/&gt;
1345 ...
1346 &lt;/list&gt;
1347 &lt;list name='data'&gt;
1348 ...
1349 &lt;/list&gt;
1350 &lt;int name='linkstatic' value='0'/&gt;
1351 &lt;int name='linkshared' value='0'/&gt;
1352 &lt;list name='licenses'/&gt;
1353 &lt;list name='distribs'&gt;
1354 &lt;distribution value="INTERNAL" /&gt;
1355 &lt;/list&gt;
1356 &lt;rule-input name="//common:common" /&gt;
1357 &lt;rule-input name="//collections:collections" /&gt;
1358 &lt;rule-input name="//foo:foo_main.cc" /&gt;
1359 &lt;rule-input name="//foo:bar.cc" /&gt;
1360 ...
1361 &lt;/rule&gt;
1362</pre>
1363
1364<p>
1365 Every XML element for a target contains a <code>name</code>
1366 attribute, whose value is the target's label, and
1367 a <code>location</code> attribute, whose value is the target's
1368 location as printed by the <a href='output-location'><code>--output
1369 location</code></a>.
1370</p>
1371
1372<h4><code>--[no]xml:line_numbers</code></h4>
1373<p>
1374 By default, the locations displayed in the XML output contain line numbers.
1375 When <code>--noxml:line_numbers</code> is specified, line numbers are not
1376 printed.
1377</p>
1378
1379<h4><code>--[no]xml:default_values</code></h4>
1380<p>
1381 By default, XML output does not include rule attribute whose value
1382 is the default value for that kind of attribute (e.g. because it
1383 were not specified in the BUILD file, or the default value was
1384 provided explicitly). This option causes such attribute values to
1385 be included in the XML output.
1386</p>
1387
1388
1389<h3 id="external-repos">Querying with external repositories</h3>
1390
1391<p>
1392 If the build depends on rules from external repositories (defined in the
1393 WORKSPACE file) then query results will include these dependencies. For
1394 example, if <code>//foo:bar</code> depends on <code>//external:some-lib</code>
1395 and <code>//external:some-lib</code> is bound to
1396 <code>@other-repo//baz:lib</code>, then
1397 <code>bazel query 'deps(//foo:bar)'</code>
1398 will list both <code>@other-repo//baz:lib</code> and
1399 <code>//external:some-lib</code> as dependencies.
1400</p>
1401
1402<p>
1403 External repositories themselves are not dependencies of a build. That is, in
1404 the example above, <code>//external:other-repo</code> is not a dependency. It
1405 can be queried for as a member of the <code>//external</code> package, though,
1406 for example:
1407<p>
1408
1409<pre>
laszlocsomor79251002017-04-03 13:09:47 +00001410 # Querying over all members of //external returns the repository.
1411 bazel query 'kind(maven_jar, //external:*)'
David Chen8fe82a32016-08-24 10:55:41 +00001412 //external:other-repo
1413
laszlocsomor79251002017-04-03 13:09:47 +00001414 # ...but the repository is not a dependency.
1415 bazel query 'kind(maven_jar, deps(//foo:bar))'
David Chen8fe82a32016-08-24 10:55:41 +00001416 INFO: Empty results
1417</pre>