Tensorflow的Bazel编程(三)

里面的一些规则简单了解一下:

BUILD的一般规则:

Attribute

 Description
features

List of features. Default is the empty list.

Features on a rule modify the features currently enabled on thepackage level via the features attribute.
For example, if the features ['a', 'b'] are enabled on the package level,and a rulefeatures attribute contains ['-a', 'c'], the featuresenabled for the rule will be 'b' and 'c'.

licenses

List of strings; optional

A list of license-type strings to be used for this particular build rule.Overrides theBUILD-file scope defaults defined by thelicenses() directive.

data

List of labels; optional

The list of files needed by this rule at runtime.

Targets named in the data attribute will appear inthe *.runfiles area of this rule, if it has one. Thismay include data files needed by a binary or library, or otherprograms needed by it. See thedata dependencies section for moreinformation about how to depend on and use data files.

Almost all rules permit a data attribute, but wherethis attribute is not allowed, this fact is documented under thespecific rule.

visibility

List of labels; optional;default default_visibility frompackage if specified, else private

The visibility attribute on a rule controls whetherthe rule can be used by other packages. Rules are always visible toother rules declared in the same package.

There are five forms (and one temporary form) a visibility label can take:

  • ["//visibility:public"]: Anyone can use this rule.
  • ["//visibility:private"]: Only rules in this packagecan use this rule. Rules injavatests/foo/barcan always use rules in java/foo/bar.
  • ["//some/package:__pkg__", "//other/package:__pkg__"]:Only rules insome/package and other/package(defined in some/package/BUILD andother/package/BUILD) have access to this rule. Note thatsub-packages do not have access to the rule; for example,//some/package/foo:bar or//other/package/testing:bla wouldn't have access.__pkg__ is a special target and must be used verbatim.It represents all of the rules in the package.
  • ["//project:__subpackages__", "//other:__subpackages__"]:Only rules in packagesproject or other orin one of their sub-packages have access to this rule. For example,//project:rule,//project/library:lib or//other/testing/internal:munge are allowed to depend onthis rule (but not//independent:evil)
  • ["//some/package:my_package_group"]:A package group isa named set of package names. Package groups can also grant access rightsto entire subtrees, e.g.//myproj/....

The visibility specifications of//visibility:public and //visibility:privatecan not be combined with any other visibility specifications.A visibility specification may contain a combination of package labels(i.e.//foo:__pkg__) and package_groups.

If a rule does not specify the visibility attribute,the default_visibilityattribute of thepackagestatement in the BUILD file containing the rule is used(exceptexports_files).

If the default visibility for the package is not specified,the rule is private.

Example:

File //frobber/bin/BUILD:

# This rule is visible to everyone
cc_binary(
    name = "executable",
    visibility = ["//visibility:public"],
    deps = [":library"],
)

# This rule is visible only to rules declared in the same package
cc_library(
    name = "library",
    visibility = ["//visibility:private"],
)

# This rule is visible to rules in package //object and //noun
cc_library(
    name = "subject",
    visibility = [
        "//noun:__pkg__",
        "//object:__pkg__",
    ],
)

# See package group "//frobber:friends" (below) for who can
# access this rule.
cc_library(
    name = thingy,
    visibility = ["//frobber:friends"],
)

File //frobber/BUILD:

# This is the package group declaration to which rule
# //frobber/bin:thingy refers.
#
# Our friends are packages //frobber, //fribber and any
# subpackage of //fribber.
package_group(
    name = "friends",
    packages = [
        "//fribber/...",
        "//frobber",
    ],
)
compatible_with

List of labels; optional

The list of environments this rule can be built for, in addition todefault-supported environments.

This is part of Bazel's soft-launched constraint system, which lets usersdeclare which rules can and cannot depend on each other. For example,externally deployable binaries shouldn't depend on libraries withcompany-secret code. SeeConstraintSemantics for details.

distribs

List of strings; optional

A list of distribution-method strings to be used for this particular build rule.Overrides theBUILD-file scope defaults defined by thedistribs() directive.

deps

List of labels; optional

A list of dependencies of this rule.

The precise semantics of what it means for this rule to depend onanother usingdeps are specific to the kind of this rule,and the rule-specific documentation below goes into more detail.At a minimum, though, the targets named viadeps willappear in the *.runfiles area of this rule, if it hasone.

Most often, a deps dependency is used to allow onemodule to use symbols defined in another module written in thesame programming language and separately compiled. Cross-languagedependencies are also permitted in many cases: for example,ajava_library rule may depend on C++ code ina cc_library rule, by declaring the latter inthedeps attribute. See the definitionof dependencies for moreinformation.

Almost all rules permit a deps attribute, but wherethis attribute is not allowed, this fact is documented under thespecific rule.

deprecation

String; optional

An explanatory warning message associated with this rule.Typically this is used to notify users that a rule has become obsolete,or has become superseded by another rule, is private to a package, or isperhaps considered harmful for some reason. It is a good idea to includesome reference (like a webpage, a bug number or example migration CLs) sothat one can easily find out what changes are required to avoid the message.If there is a new target that can be used as a drop in replacement, it is agood idea to just migrate all users of the old target.

This attribute has no effect on the way things are built, but itmay affect a build tool's diagnostic output. The build tool issues awarning when a rule with adeprecation attribute isdepended upon by another rule.

Intra-package dependencies are exempt from this warning, so that,for example, building the tests of a deprecated rule does notencounter a warning.

If a deprecated rule depends on another deprecated rule, no warningmessage is issued.

Once people have stopped using it, the package can be removed.

restricted_to

List of labels; optional

The list of environments this rule can be built for, instead ofdefault-supported environments.

This is part of Bazel's soft-launched constraint system. Seecompatible_withfor details.

tags

List of arbitrary text tags. Tags may be any valid string; default is the empty list.

Tags can be used on any rule. Tags on test and test_suite rules are useful for categorizing the tests.Tags on non-test rules are used to control sandboxed execution of genrules andSkylark actions, and for parsing by humans and/or external tools.

Bazel modifies the behavior of its sandboxing code if it finds the following keywords in thetags attribute of any test rule or genrule, or the keys ofexecution_requirements for any Skylark action.

  • local keyword results in the action or test never being run remotely or inside thesandbox. For genrules and tests, marking the rule with the local = 1 attribute has the same effect.
  • block-network keyword blocks access to the external network from inside the sandbox. In this case, only communication with localhost is allowed.

Tags on tests are generally used to annotate a test's role in your debug and release process. Typically, tags are most useful for C++ and Python tests, which lack any runtime annotation ability. The use of tags and size elements gives flexibility in assembling suites of tests based around codebase check-in policy.

Bazel modifies test running behavior if it finds the following keywords in thetags attribute of the test rule:

  • exclusive keyword will force the test to be run in the "exclusive" mode, ensuring that no other tests are running at the same time. Such tests will be executed in serial fashion after all build activity and non-exclusive tests have been completed. They will also always run locally and thus without sandboxing.
  • manual keyword will force the test target to not be included in target pattern wildcards (...,:*, :all, etc); the test target will be neither built nor run. It will also be ignored by thetest_suite rules that do not mention this test explicitly. The only way to build or run such a test is to specify it via an explicit target pattern on the command line.
  • external keyword will force test to be unconditionally executed (regardless of--cache_test_results value).
testonly

Boolean; optional; default 0 except as noted

If 1, only testonly targets (such as tests) can depend on this target.

Equivalently, a rule that is not testonly is not allowed todepend on any rule that istestonly.

Tests (*_test rules)and test suites (test_suite rules)aretestonly by default.

This attribute is intended to mean that the target should not becontained in binaries that are released to production.

Because testonly is enforced at build time, not run time, and propagatesvirally through the dependency tree, it should be applied judiciously. Forexample, stubs and fakes thatare useful for unit tests may also be useful for integration testsinvolving the same binaries that will be released to production, andtherefore should probably not be marked testonly. Conversely, rules thatare dangerous to even link in, perhaps because they unconditionallyoverride normal behavior, should definitely be marked testonly.

visibility是一个重要参数。

有几个函数:


例如:
1、load("//tools/build_rules:build_test.bzl", "build_test")

build_test(name = ...)

2、package(default_deprecation, default_testonly, default_visibility, features)
package(default_visibility = ["//foo:target"])
3、package_group(name, packages, includes)
package_group(
    name = "tropical",
    packages = [
        "//fruits/mango",
        "//fruits/orange",
        "//fruits/papaya/...",
    ],
)

The following declarations specify the package groups of a fictionalapplication:

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"],
)
4、licenses(license_types)

restricted
Requires mandatory source distribution.
reciprocal
Allows usage of software freely inunmodified form. Any modifications must be made freely available.
notice
Original or modified third-party software may beshipped without danger nor encumbering other sources. All of the licenses in thiscategory do, however, have an \"original Copyright notice\" or\"advertising clause\", wherein any external distributions must include the noticeor clause specified in the license.
permissive
Code that is under a license but does notrequire a notice.
unencumbered
Public domain, free for any use.
5、exports_files([label, ...], visibility, licenses)

exports_files(["golden.txt"])
6、glob(include, exclude=[], exclude_directories=1)

java_library(
    name = "mylib",
    srcs = glob(["*.java"]) + [":gen_java_srcs"],
    deps = "...",
)

genrule(
    name = "gen_java_srcs",
    outs = [
        "Foo.java",
        "Bar.java",
    ],
    ...
)
sh_test(
    name = "mytest",
    srcs = ["mytest.sh"],
    data = glob(
        ["testdata/*.txt"],
        exclude = ["testdata/experimental.txt"],
    ),
)
java_library(
    name = "mylib",
    srcs = glob(
        ["**/*.java"],
        exclude = ["**/testing/**"],
    ),
)
sh_test(
    name = "mytest",
    srcs = ["mytest.sh"],
    data = glob(["testdata/**/*.txt"]),
)
# 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"])]
$ bazel query '//foo:all' | sort
//foo:count_lines_a_test
//foo:count_lines_b_test
//foo:count_lines_c_test
7、
select(
    {conditionA: valuesA, conditionB: valuesB, ...},
    no_match_error = "custom message"
)
sh_binary(
    name = "myrule",
    srcs = select({
        ":conditionA": ["myrule_a.sh"],
        ":conditionB": ["myrule_b.sh"],
        "//conditions:default": ["myrule_default.sh"]
    })
)
config_setting(
    name = "windows",
    values = {
        "crosstool_top": "//crosstools/windows",
    },
)
cc_binary(
    name = "multiplatform_app",
    ...
    linkopts = select({
        ":windows": [
            "-Wl,windows_support1.lib",
            "-Wl,windows_support2.lib",
        ],
        "//conditions:default": [],
    ...
)
8、workspace(name = "com_example_project")

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值