| |
| <!-- ================================================================= |
| package() |
| ================================================================= |
| --> |
| |
| <h3 id="package">package</h3> |
| |
| <p><code>package(default_deprecation, |
| |
| default_testonly, |
| default_visibility, |
| features)</code> |
| </p> |
| |
| <p>This function declares metadata that applies to every subsequent rule in the |
| package. It is used at most once within a package (BUILD file).</p> |
| |
| <p>It is recommended that the package function is called at the top of the |
| file, before any rule.</p> |
| |
| <h4 id="package_args">Arguments</h4> |
| |
| <ul> |
| <li id="package.default_visibility"><code>default_visibility</code>: |
| The default visibility of the rules in this package. |
| <i>(List of <a href="build-ref.html#labels">labels</a>; optional)</i><br/> |
| |
| <p>Every rule in this package has the visibility specified in this |
| attribute, unless otherwise specified in the <code>visibility</code> |
| attribute of the rule. For detailed information about the syntax of this |
| attribute, see the documentation of the <a href="#common.visibility">visibility</a> |
| attribute. The package default visibility does not apply to |
| |
| <a href="#exports_files">exports_files</a>, which is |
| public by default. |
| </li> |
| |
| <li id="package.default_deprecation"><code>default_deprecation</code>: |
| Sets the default <a href="#common.deprecation"><code>deprecation</code></a> message |
| for all rules in this package. <i>(String; optional)</i> |
| </li> |
| |
| <li id="package.default_testonly"><code>default_testonly</code>: |
| Sets the default <a href="#common.testonly"><code>testonly</code></a> property |
| for all rules in this package. <i>(Boolean; optional; default is 0 except as noted)</i> |
| |
| <p>In packages under <code>javatests</code> the default value is 1.</p> |
| </li> |
| |
| <li id="package.features"><code>features</code>: |
| Sets various flags that affect the semantics of this BUILD file. |
| <i>(List strings; optional)</i> |
| |
| <p>This feature is mainly used by the people working on the build system to |
| tag packages that need some kind of special handling. Do not use this unless |
| explicitly requested by someone working on the build system.</p> |
| </li> |
| |
| </ul> |
| |
| <h4 id="package_example">Examples</h4> |
| |
| The declaration below declares that the rules in this package are |
| visible only to members of package |
| group <code>//foo:target</code>. Individual visibility declarations |
| on a rule, if present, override this specification. |
| |
| <pre class="code"> |
| package(default_visibility = ["//foo:target"]) |
| </pre> |
| |
| <!-- ================================================================= |
| package_group() |
| ================================================================= |
| --> |
| |
| <h3 id="package_group">package_group</h3> |
| |
| <p><code>package_group(name, packages, includes)</code></p> |
| |
| <p>This function defines a set of packages and assigns a label to the |
| group. The label can be referenced in <code>visibility</code> attributes.</p> |
| |
| <p>Package groups are used for visibility control. You can grant access to a rule |
| to one or more package groups, every rule in the entire source tree, or only to rules declared |
| in the same package. For more detailed description of the visibility system, see |
| the <a href="#common.visibility">visibility</a> attribute. |
| |
| <h4 id="package_group_args">Arguments</h4> |
| |
| <ul> |
| <li id="package_group.name"><code>name</code>: |
| A unique name for this rule. |
| <i>(<a href="build-ref.html#name">Name</a>; required)</i> |
| </li> |
| |
| <li id="package_group.packages"><code>packages</code>: |
| A complete enumeration of packages in this group. |
| <i>(List of <a href="build-ref.html#s4">Package</a>; optional)</i><br/> |
| |
| <p>Packages should be referred to using their full names, |
| starting with a double slash. For |
| example, <code>//foo/bar/main</code> is a valid element |
| of this list.</p> |
| |
| <p>You can also specify wildcards: the specification |
| <code>//foo/...</code> specifies every package under |
| <code>//foo</code>, including <code>//foo</code> itself. |
| |
| <p>If this attribute is missing, the package group itself will contain |
| no packages (but it can still include other package groups).</p> |
| </li> |
| |
| <li id="package_group.includes"><code>includes</code>: |
| Other package groups that are included in this one. |
| <i>(List of <a href="build-ref.html#labels">labels</a>; optional)</i><br/> |
| |
| <p>The labels in this attribute must refer to other package |
| groups. Packages in referenced package groups are taken to be part |
| of this package group. This is transitive, that is, if package |
| group <code>a</code> contains package group <code>b</code>, |
| and <code>b</code> contains package group <code>c</code>, every |
| package in <code>c</code> will also be a member of <code>a</code>.</p> |
| </li> |
| </ul> |
| |
| |
| <h4 id="package_group_example">Examples</h4> |
| |
| The following <code>package_group</code> declaration specifies a |
| package group called "tropical" that contains tropical fruits. |
| |
| <pre class="code"> |
| package_group( |
| name = "tropical", |
| packages = [ |
| "//fruits/mango", |
| "//fruits/orange", |
| "//fruits/papaya/...", |
| ], |
| ) |
| </pre> |
| |
| The following declarations specify the package groups of a fictional |
| application: |
| |
| <pre class="code"> |
| package_group( |
| name = "fooapp", |
| includes = [ |
| ":controller", |
| ":model", |
| ":view", |
| ], |
| ) |
| |
| package_group( |
| name = "model", |
| packages = ["//fooapp/database"], |
| ) |
| |
| package_group( |
| name = "view", |
| packages = [ |
| "//fooapp/swingui", |
| "//fooapp/webui", |
| ], |
| ) |
| |
| package_group( |
| name = "controller", |
| packages = ["//fooapp/algorithm"], |
| ) |
| </pre> |
| |
| |
| <!-- ================================================================= |
| exports_files([label, ...]) |
| ================================================================= |
| --> |
| |
| <h3 id="exports_files">exports_files</h3> |
| |
| <p><code>exports_files([<i>label</i>, ...], visibility, licenses)</code></p> |
| |
| <p> |
| <code>exports_files()</code> specifies a list of files belonging to |
| this package that are exported to other packages but not otherwise |
| mentioned in the BUILD file. |
| </p> |
| |
| <p> |
| The BUILD file for a package may only refer to files belonging to another |
| package if they are mentioned somewhere in the other packages's BUILD file, |
| whether as an input to a rule or an explicit or implicit output from a rule. |
| The remaining files are not associated with a specific rule but are just "data", |
| and for these, <code>exports_files</code> ensures that they may be referenced by |
| other packages. (One kind of data for which this is particularly useful are |
| shell scripts.) |
| </p> |
| |
| <h4 id="exports_files_args">Arguments</h4> |
| |
| <p> |
| The argument is a list of names of files within the current package. A |
| visibility declaration can also be specified; in this case, the files will be |
| visible to the targets specified. If no visibility is specified, the files |
| will be visible to every package, even if a package default visibility was |
| specified in the <code><a href="#package">package</a></code> function. The |
| <a href="#common.licenses">licenses</a> can also be specified. |
| </p> |
| |
| <h4 id="exports_files_example">Example</h4> |
| |
| <p> |
| The following example exports <code>golden.txt</code>, a |
| text file from the <code>test_data</code> package, so that other |
| packages may use it, for example, in the <code>data</code> attribute |
| of tests. |
| </p> |
| |
| <pre class="code"> |
| # from //test_data/BUILD |
| |
| exports_files(["golden.txt"]) |
| </pre> |
| |
| <!-- ================================================================= |
| glob() |
| ================================================================= |
| --> |
| |
| <h3 id="glob">glob</h3> |
| |
| <p><code>glob(include, exclude=[], exclude_directories=1)</code> |
| </p> |
| |
| <p> |
| Glob is a helper function that can be used anywhere a list of filenames |
| is expected. It takes one or two lists of filename patterns containing |
| the <code>*</code> wildcard: as per the Unix shell, this wildcard |
| matches any string excluding the directory separator <code>/</code>. |
| In addition filename patterns can contain the recursive <code>**</code> |
| wildcard. This wildcard will match zero or more complete |
| path segments separated by the directory separator <code>/</code>. |
| This wildcard can only be used as a complete path segment. For example, |
| <code>"x/**/*.java"</code> is legal, but <code>"test**/testdata.xml"</code> |
| and <code>"**.java"</code> are both illegal. No other wildcards are supported. |
| </p> |
| <p> |
| Glob returns a list of every file in the current package that: |
| </p> |
| <ul> |
| <li>Matches at least one pattern in <code>include</code>. </li> |
| <li>Does not match any of the patterns in <code>exclude</code> (default []).</li> |
| </ul> |
| <p> |
| If the <code>exclude_directories</code> argument is enabled (set to 1), files of |
| type directory will be omitted from the results (default 1). |
| </p> |
| <p> |
| There are several important limitations and caveats: |
| </p> |
| |
| <ol> |
| <li> |
| Globs only match files in your source tree, never |
| generated files. If you are building a target that requires both |
| source and generated files, create an explicit list of generated |
| files, and use <code>+</code> to add it to the result of the |
| <code>glob()</code> call. |
| </li> |
| |
| <li> |
| Globs may match files in subdirectories. And subdirectory names |
| may be wildcarded. However... |
| </li> |
| |
| <li> |
| Labels are not allowed to cross the package boundary and glob does |
| not match files in subpackages. |
| |
| For example, the glob expression <code>**/*.cc</code> in package |
| <code>x</code> does not include <code>x/y/z.cc</code> if |
| <code>x/y</code> exists as a package (either as |
| <code>x/y/BUILD</code>, or somewhere else on the package-path). This |
| means that the result of the glob expression actually depends on the |
| existence of BUILD files — that is, the same glob expression would |
| include <code>x/y/z.cc</code> if there was no package called |
| <code>x/y</code> or it was marked as deleted using the |
| <a href="bazel-user-manual.html#flag--deleted_packages">--deleted_packages</a> |
| flag. |
| |
| </li> |
| |
| <li> |
| The restriction above applies to all glob expressions, |
| no matter which wildcards they use. |
| </li> |
| <li> |
| A hidden file with filename starting with <code>.</code> is matched by |
| both the <code>**</code> and the <code>*</code> wildcards. |
| </li> |
| <li> |
| If a rule and a source file with the same name both exist in the package, the glob will |
| return the outputs of the rule instead of the source file. |
| </li> |
| </ol> |
| |
| <p> |
| In general, you should <b>try to provide an appropriate extension (e.g. *.html) |
| instead of using a bare '*'</b> for a glob pattern. The more explicit name |
| is both self documenting and ensures that you don't accidentally match backup |
| files, or emacs/vi/... auto-save files. |
| </p> |
| |
| <p> |
| When writing build rules you can enumerate the elements of the glob. This |
| enables generating individual rules for every input, for example. See the |
| <a href="#expanded_glob_example">expanded glob example</a> section below. |
| </p> |
| |
| <h4 id="glob_example">Glob Examples</h4> |
| |
| <p> Create a Java library built from all java files in this directory, |
| and all files generated by the <code>:gen_java_srcs</code> rule.</p> |
| <pre class="code"> |
| java_library( |
| name = "mylib", |
| srcs = glob(["*.java"]) + [":localize_strings"], |
| deps = "...", |
| ) |
| |
| genrule( |
| name = "gen_java_srcs", |
| outs = [ |
| "Foo.java", |
| "Bar.java", |
| ], |
| ... |
| ) |
| </pre> |
| |
| <p>Include all txt files in directory testdata except experimental.txt. |
| Note that files in subdirectories of testdata will not be included. If |
| you want those files to be included, use a recursive glob (**).</p> |
| <pre class="code"> |
| sh_test( |
| name = "mytest", |
| srcs = ["mytest.sh"], |
| data = glob( |
| ["testdata/*.txt"], |
| exclude = ["testdata/experimental.txt"], |
| ), |
| ) |
| </pre> |
| |
| <h4 id="recursive_glob_example">Recursive Glob Examples</h4> |
| |
| <p>Create a library built from all java files in this directory and all |
| subdirectories except those whose path includes a directory named testing. |
| Subdirectories containing a BUILD file are ignored. |
| <b>This should be a very common pattern.</b> |
| </p> |
| <pre class="code"> |
| java_library( |
| name = "mylib", |
| srcs = glob( |
| ["**/*.java"], |
| exclude = ["**/testing/**"], |
| ), |
| ) |
| </pre> |
| |
| <p>Make the test depend on all txt files in the testdata directory, |
| its subdirectories</p> |
| <pre class="code"> |
| sh_test( |
| name = "mytest", |
| srcs = ["mytest.sh"], |
| data = glob(["testdata/**/*.txt"]), |
| ) |
| </pre> |
| |
| <h4 id="expanded_glob_example">Expanded Glob Examples</h4> |
| |
| <p> |
| Create an individual genrule for *_test.cc in the current directory |
| that counts the number of lines in the file. |
| </p> |
| |
| <pre class="code"> |
| # Conveniently, the build language supports list comprehensions. |
| [genrule( |
| name = "count_lines_" + f[:-3], # strip ".cc" |
| srcs = [f], |
| outs = ["%s-linecount.txt" % f[:-3]], |
| cmd = "wc -l $< >$@", |
| ) for f in glob(["*_test.cc"])] |
| </pre> |
| |
| <p> |
| If the BUILD file above is in package //foo and the package contains three |
| matching files, a_test.cc, b_test.cc and c_test.cc then running |
| <code>bazel query '//foo:all'</code> will list all rules that were generated: |
| |
| <pre> |
| $ bazel query '//foo:all' | sort |
| //foo:count_lines_a_test |
| //foo:count_lines_b_test |
| //foo:count_lines_c_test |
| </pre> |
| |
| |
| <!-- ================================================================= |
| licenses() |
| ================================================================= |
| --> |
| |
| <h3 id="licenses">licenses</h3> |
| |
| <p><code>licenses(license_types)</code></p> |
| |
| <p><code>licenses()</code> specifies the default license type (or types) |
| of the build rules in a <code>BUILD</code> file. |
| |
| The <code>licenses()</code> directive should appear close to the |
| beginning of the <code>BUILD</code> file, before any build rules, as it |
| sets the <code>BUILD</code>-file scope default for build rule license |
| types.</p> |
| |
| <h4 id="licenses_args">Arguments</h4> |
| |
| <p>The argument, <code id="licenses.licence_types">license_types</code>, |
| is a list of license-type strings. |
| <!-- TODO(bazel-team): specify some examples --> |
| |
| </p> |
| |
| <h4 id="licenses_example">Examples</h4> |
| |
| <p>The following example specifies a single license type of |
| <code>"notice"</code>. Third-party software licenses that do not require |
| publishing of source code changes but require some sort of copyright or |
| other public notice are included in this license type. |
| </p> |
| |
| <pre class="code"> |
| |
| licenses(["notice"]) # MIT license |
| |
| exports_files(["jquery-2.1.1.js"]) |
| </pre> |
| |
| <!-- ================================================================= |
| load() |
| ================================================================= |
| --> |
| |
| <h3 id="load">load</h3> |
| |
| <p><code>load(path, symbols...)</code></p> |
| |
| <p><code>load()</code> is a statement that imports definitions from a |
| Skylark module (<code>.bzl</code> file). For example: |
| </p> |
| |
| <pre class="code"> |
| load("/tools/build_rules/build_test", "build_test") |
| |
| build_test(name = ...) |
| </pre> |
| |
| <p>This will execute the |
| |
| Skylark |
| module <code>tools/build_rules/build_test.bzl</code> and import the symbol |
| <code>build_test</code> in the local environment. This is useful for |
| using macros or for sharing values between multiple BUILD files. By |
| using more arguments, you can load more symbols at once.</p> |
| |
| <p>Relative paths are allowed only to load a file from the same directory |
| (not a subdirectory), e.g.</p> |
| |
| <pre class="code"> |
| load("build_defs", "COMPILER_FLAGS", "LINKER_FLAGS") |
| </pre> |
| |
| |
| <!-- ================================================================= |
| select() |
| ================================================================= |
| --> |
| |
| <h3 id="select">select</h3> |
| |
| <p><code>select({conditionA: valuesA, conditionB: valuesB, ...})</code> |
| </p> |
| |
| <p><code>select()</code> is the helper function that makes an |
| attribute for a |
| rule <a href="#configurable-attributes">configurable</a>. It can |
| replace the right-hand side of |
| |
| <i>almost</i> |
| any attribute assignment so that the attribute's value depends on the |
| Bazel flags passed in at the command line. This can be used, for |
| example, to define platform-specific dependencies or to embed |
| different resources depending on whether a rule is built in |
| "developer" vs. "release" mode. |
| </p> |
| |
| <p>Basic usage is as follows:</p> |
| |
| <pre class="code"> |
| sh_binary( |
| name = "myrule", |
| srcs = select({ |
| ":conditionA": ["myrule_a.sh"], |
| ":conditionB": ["myrule_b.sh"], |
| "//conditions:default": ["myrule_default.sh"] |
| }) |
| ) |
| </pre> |
| |
| <p>This makes the <code>srcs</code> attribute of |
| a <code>sh_binary</code> configurable by replacing its normal label |
| list assignment with a <code>select</code> call that maps |
| configuration conditions to matching values. Each condition is a label |
| reference to |
| a <code><a href="#config_setting">config_setting</a></code> instance, |
| which "matches" if the rule's configuration matches an expected set of |
| values. The value of <code>myrule#srcs</code> then becomes whichever |
| label list matches the current invocation. |
| </p> |
| |
| <p>Further notes:</p> |
| |
| <ul> |
| <li>Exactly one condition is selected on any invocation. |
| </li> |
| <li>If two conditions match and one is a specialization of the other |
| (e.g. it matches on the same flags as the other plus additional flags), |
| the specialization takes precedence. |
| </li> |
| <li>If two conditions match and neither is a specialization of the other, |
| Bazel fails with an error. |
| </li> |
| <li>The special pseudo-label <code>//conditions:default</code> is |
| considered to match if no other condition matches. If this condition |
| is left out, some other rule must match to avoid an error. |
| </li> |
| <li><code>select</code> can be embedded <i>inside</i> a larger |
| attribute assignment. So <code>srcs = ["common.sh"] |
| + select({ ":conditionA": ["myrule_a.sh"], ...})</code> and <code> |
| srcs = select({ ":conditionA": ["a.sh"]}) + select({ ":conditionB": |
| ["b.sh"]})</code> are valid expressions. |
| </li> |
| <li><code>select</code> works with most, but not all, attributes. |
| |
| </li> |
| </ul> |
| |
| <!-- ================================================================= |
| workspace() |
| ================================================================= |
| --> |
| |
| <h3 id="workspace">workspace</h3> |
| |
| <p><code>workspace(name = "something")</code></p> |
| |
| <p>This can only be used in the WORKSPACE file.</p> |
| |
| <p>The sets an optional name for the repository.</p> |
| |
| <p>This name is used for the directory that runfiles are stored in. For |
| example, if there is a runfile foo/bar and the WORKSPACE file contains |
| <code>workspace(name = "baz")</code>, then the runfile will be output under |
| mytarget.runfiles/baz/foo/bar. If no workspace name is specified, then the |
| runfile will be symlinked to bar.runfiles/foo/bar. |
| </p> |
| |
| <p>A workspace name can contain slashes if a deeper directory listing is |
| desired (<code>workspace(name = "baz/qux")</code> will place the runfile at |
| bar.runfiles/baz/qux/foo/bar). |
| </p> |
| |
| </div> |
| </div> |
| </body> |
| </html> |