Quy tắc trong Bazel

Bí danh

alias(<a href="#alias.name">name</a>, <a href="#alias.actual">actual</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.compatible_with">compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.deprecation">deprecation</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.features">features</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.package_metadata">package_metadata</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.restricted_to">restricted_to</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.tags">tags</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.target_compatible_with">target_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.testonly">testonly</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.visibility">visibility</a>)

Quy tắc alias tạo một tên gọi khác để tham chiếu đến một quy tắc đã tồn tại. Điều này đặc biệt hữu ích trong các dự án lớn khi việc đổi tên một mục tiêu có thể đòi hỏi thay đổi ở nhiều tệp.

Tuy nhiên, cần lưu ý rằng alias chỉ hoạt động với các mục tiêu “thông thường”. package_grouptest_suite không thể được gán bí danh. Quy tắc alias có khai báo chế độ hiển thị riêng, nhưng về mọi mặt khác, nó hoạt động giống hệt như quy tắc mà nó tham chiếu đến.

config_setting

config_setting(<a href="#config_setting.name">name</a>, <a href="#config_setting.constraint_values">constraint_values</a>, <a href="#config_setting.define_values">define_values</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.deprecation">deprecation</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.features">features</a>, <a href="#config_setting.flag_values">flag_values</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#typical.licenses">licenses</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.package_metadata">package_metadata</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.tags">tags</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.testonly">testonly</a>, <a href="#config_setting.values">values</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.visibility">visibility</a>)

Quy tắc config_setting cho phép kích hoạt các thuộc tính có thể định cấu hình dựa trên trạng thái cấu hình dự kiến (thông qua cờ bản dựng hoặc quy tắc ràng buộc nền tảng). Quy tắc này thường được sử dụng kết hợp với select.

Ví dụ, để khớp với mọi bản dựng đặt --compilation_mode=opt hoặc -c opt:

config_setting(
    name = "simple",
    values = {"compilation_mode": "opt"}
)

Một ví dụ khác, khớp với mọi bản dựng nhắm đến ARM và áp dụng định nghĩa tùy chỉnh FOO=bar:

config_setting(
    name = "two_conditions",
    values = {
        "cpu": "arm",
        "define": "FOO=bar"
    }
)

Đối với phương pháp chiếu góc thứ nhất thì, config_setting cho phép xác định các cấu hình khác nhau dựa trên các cờ và giá trị ràng buộc. Điều này rất quan trọng để hỗ trợ các môi trường xây dựng đa dạng và các yêu cầu cụ thể của từng nền tảng. Việc sử dụng constraint_values cho phép chỉ định các ràng buộc về nền tảng mục tiêu, trong khi define_valuesflag_values cho phép tùy chỉnh dựa trên các cờ bản dựng.

filegroup

filegroup(<a href="#filegroup.name">name</a>, <a href="#filegroup.srcs">srcs</a>, <a href="#filegroup.data">data</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.compatible_with">compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.deprecation">deprecation</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.features">features</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#typical.licenses">licenses</a>, <a href="#filegroup.output_group">output_group</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.package_metadata">package_metadata</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.restricted_to">restricted_to</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.tags">tags</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.target_compatible_with">target_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.testonly">testonly</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.visibility">visibility</a>)

filegroup được sử dụng để thu thập các mục tiêu khác nhau dưới một nhãn duy nhất. Nó không thay thế cho việc liệt kê các mục tiêu trên dòng lệnh, nhưng hữu ích trong các trường hợp như thuộc tính srcs của genrule hoặc thuộc tính data của *_binary.

Ví dụ, để tạo một filegroup bao gồm hai tệp nguồn:

filegroup(
    name = "mygroup",
    srcs = [
        "a_file.txt",
        "//a/library:target",
        "//a/binary:target",
    ],
)

genquery

genquery(<a href="#genquery.name">name</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#typical.deps">deps</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#typical.data">data</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.compatible_with">compatible_with</a>, <a href="#genquery.compressed_output">compressed_output</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.deprecation">deprecation</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.exec_compatible_with">exec_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.exec_group_compatible_with">exec_group_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.exec_properties">exec_properties</a>, <a href="#genquery.expression">expression</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.features">features</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#typical.licenses">licenses</a>, <a href="#genquery.opts">opts</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.package_metadata">package_metadata</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.restricted_to">restricted_to</a>, <a href="#genquery.scope">scope</a>, <a href="#genquery.strict">strict</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.tags">tags</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.target_compatible_with">target_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.testonly">testonly</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.visibility">visibility</a>)

genquery() thực thi một truy vấn bằng ngôn ngữ truy vấn của Bazel và ghi kết quả vào một tệp. Để đảm bảo tính nhất quán của bản dựng, truy vấn chỉ được phép truy cập vào một tập hợp đóng bắc cầu các mục tiêu được chỉ định trong thuộc tính scope.

Ví dụ:

genquery(
    name = "kiwi-deps",
    expression = "deps(//kiwi:kiwi_lib)",
    scope = ["//kiwi:kiwi_lib"],
)

genrule

genrule(<a href="#genrule.name">name</a>, <a href="#genrule.srcs">srcs</a>, <a href="#genrule.outs">outs</a>, <a href="#genrule.cmd">cmd</a>, <a href="#genrule.cmd_bash">cmd_bash</a>, <a href="#genrule.cmd_bat">cmd_bat</a>, <a href="#genrule.cmd_ps">cmd_ps</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.compatible_with">compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.deprecation">deprecation</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.exec_compatible_with">exec_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.exec_group_compatible_with">exec_group_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.exec_properties">exec_properties</a>, <a href="#genrule.executable">executable</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.features">features</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#typical.licenses">licenses</a>, <a href="#genrule.local">local</a>, <a href="#genrule.message">message</a>, <a href="#genrule.output_licenses">output_licenses</a>, <a href="#genrule.output_to_bindir">output_to_bindir</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.package_metadata">package_metadata</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.restricted_to">restricted_to</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.tags">tags</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.target_compatible_with">target_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.testonly">testonly</a>, <a href="#genrule.toolchains">toolchains</a>, <a href="#genrule.tools">tools</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.visibility">visibility</a>)

genrule tạo ra một hoặc nhiều tệp bằng cách sử dụng một lệnh Bash do người dùng chỉ định. Đây là quy tắc bản dựng chung có thể sử dụng khi không có quy tắc cụ thể nào phù hợp.

Ví dụ:

genrule(
    name = "foo",
    srcs = [],
    outs = ["foo.h"],
    cmd = "./$(location create_foo.pl) > "$@"",
    tools = ["create_foo.pl"],
)

starlark_doc_extract

starlark_doc_extract(<a href="#starlark_doc_extract.name">name</a>, <a href="#starlark_doc_extract.deps">deps</a>, <a href="#starlark_doc_extract.src">src</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#typical.data">data</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.compatible_with">compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.deprecation">deprecation</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.exec_compatible_with">exec_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.exec_group_compatible_with">exec_group_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.exec_properties">exec_properties</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.features">features</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#typical.licenses">licenses</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.package_metadata">package_metadata</a>, <a href="#starlark_doc_extract.render_main_repo_name">render_main_repo_name</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.restricted_to">restricted_to</a>, <a href="#starlark_doc_extract.symbol_names">symbol_names</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.tags">tags</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.target_compatible_with">target_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.testonly">testonly</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.visibility">visibility</a>)

starlark_doc_extract() trích xuất tài liệu cho các quy tắc, hàm, khía cạnh và nhà cung cấp được định nghĩa trong một tệp .bzl hoặc .scl. Kết quả là một tệp proto nhị phân ModuleInfo.

test_suite

test_suite(<a href="#test_suite.name">name</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.compatible_with">compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.deprecation">deprecation</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.features">features</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#typical.licenses">licenses</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.package_metadata">package_metadata</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.restricted_to">restricted_to</a>, <a href="#test_suite.tags">tags</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.target_compatible_with">target_compatible_with</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.testonly">testonly</a>, <a href="#test_suite.tests">tests</a>, <a href="https://bazel.build/reference/be/common-definitions?hl=vi#common.visibility">visibility</a>)

test_suite định nghĩa một tập hợp các kiểm thử “hữu ích” cho con người. Nó cho phép xác định các bộ kiểm thử, ví dụ như “các kiểm thử cần chạy trước khi kiểm tra”, “các kiểm thử quan trọng”, hoặc “tất cả các kiểm thử nhỏ”.

Ví dụ, một bộ kiểm thử để chạy tất cả các kiểm thử nhỏ trong gói hiện tại:


test_suite(
    name = "small_tests",
    tags = ["small"],
)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *