规则
- <ph type="x-smartling-placeholder"></ph> cc_binary
 - <ph type="x-smartling-placeholder"></ph> cc_import
 - <ph type="x-smartling-placeholder"></ph> cc_library
 - <ph type="x-smartling-placeholder"></ph> cc_proto_library
 - <ph type="x-smartling-placeholder"></ph> fdo_prefetch_hints
 - <ph type="x-smartling-placeholder"></ph> fdo_profile
 - <ph type="x-smartling-placeholder"></ph> propeller_optimize
 - <ph type="x-smartling-placeholder"></ph> cc_test
 - <ph type="x-smartling-placeholder"></ph> cc_toolchain
 - <ph type="x-smartling-placeholder"></ph> cc_toolchain_suite
 
cc_binary
cc_binary(name, deps, srcs, data, additional_linker_inputs, args, compatible_with, copts, defines, deprecation, distribs, env, exec_compatible_with, exec_properties, features, includes, licenses, linkopts, linkshared, linkstatic, local_defines, malloc, nocopts, output_licenses, restricted_to, stamp, tags, target_compatible_with, testonly, toolchains, visibility, win_def_file)
隐式输出目标
name.stripped(仅在明确请求时构建):剥离 二进制文件版本。对二进制文件运行strip -g以移除调试 符号。您可以使用以下命令在命令行中提供其他剥离选项:--stripopt=-foo。此输出仅在明确请求的情况下构建。name.dwp(仅在明确请求时构建):如果 已启用 Fission:调试 信息包文件。否则: 空白文件。
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          deps
         | 
        
                     
 这些值可以是   | 
      
          srcs
         | 
        
                     
 所有  
 所有  如果规则的名称位于  
            允许的  
 ...以及生成这些文件的任何规则。 不同的扩展表示 。  | 
      
          additional_linker_inputs
         | 
        
                     
 例如,您可以在此处提供经过编译的 Windows .res 文件, 二进制目标。  | 
      
          copts
         | 
        
                     
 
            此属性中的每个字符串都会按照指定顺序添加到  
            如果该软件包声明了 feature,
              | 
      
          defines
         | 
        
                     
 -D,并添加到编译命令行中,
          以及依赖于该规则的每条规则请务必小心,
          产生深远的影响。如有疑问,请将定义值添加到
          local_defines。
         | 
      
          includes
         | 
        
                     
 
          需遵循替换 "Make variable" 的规定。
          每个字符串都带有  必须将标头添加到 srcs 或 hdrs 中,否则它们将无法供从属使用 使用沙盒(默认)编译时应遵循的规则。  | 
      
          linkopts
         | 
        
                     
 LINKOPTS 中,
          链接二进制目标。
          
            此列表中将出现每个不以   | 
      
          linkshared
         | 
        
                     
 linkshared=True。默认情况下
        此选项处于停用状态
        
          此标志的存在表示使用  
          如果您同时指定   | 
      
          linkstatic
         | 
        
                     
 cc_binary和
           cc_test:以静态方式关联二进制文件
           模式。对于 cc_library.linkstatic:请参阅下文。
           
             默认情况下,系统会为  
             如果已启用,并且是二进制文件或测试文件,此选项将指示构建工具加入
             尽可能为用户库使用  关联可执行文件其实有三种不同的方法: 
 
            
           如果为   | 
      
          local_defines
         | 
        
                     
 -D,并添加到此目标的编译命令行中,
          但不向其依赖项传递。
         | 
      
          malloc
         | 
        
                     
 
            默认情况下,C++ 二进制文件链接到   | 
      
          nocopts
         | 
        
                     
 COPTS
          (包括规则的 copts 属性中明确指定的值)将从
          COPTS,以便编译此规则。
          您应该很少需要此属性。
         | 
      
          stamp
         | 
        
                     
 
 除非其依赖项发生变化,否则带时间戳的二进制文件不会被重新构建。  | 
      
          win_def_file
         | 
        
                     
 仅当 Windows 是目标平台时,才应使用此属性。 它可用于 导出符号。  | 
      
cc_import
cc_import(name, data, hdrs, alwayslink, compatible_with, deprecation, distribs, features, interface_library, licenses, restricted_to, shared_library, static_library, system_provided, tags, target_compatible_with, testonly, visibility)
cc_import 规则允许用户导入预编译的 C/C++ 库。
以下是典型的用例:
1.关联静态库
cc_import( name = "mylib", hdrs = ["mylib.h"], static_library = "libmylib.a", # If alwayslink is turned on, # libmylib.a will be forcely linked into any binary that depends on it. # alwayslink = 1, )
cc_import( name = "mylib", hdrs = ["mylib.h"], shared_library = "libmylib.so", )
cc_import( name = "mylib", hdrs = ["mylib.h"], # mylib.lib is a import library for mylib.dll which will be passed to linker interface_library = "mylib.lib", # mylib.dll will be available for runtime shared_library = "mylib.dll", )
system_provided=True 关联 (Windows)
cc_import( name = "mylib", hdrs = ["mylib.h"], # mylib.lib is an import library for mylib.dll which will be passed to linker interface_library = "mylib.lib", # mylib.dll is provided by system environment, for example it can be found in PATH. # This indicates that Bazel is not responsible for making mylib.dll available. system_provided = 1, )
在 Unix 上:
cc_import( name = "mylib", hdrs = ["mylib.h"], static_library = "libmylib.a", shared_library = "libmylib.so", ) # first will link to libmylib.a cc_binary( name = "first", srcs = ["first.cc"], deps = [":mylib"], linkstatic = 1, # default value ) # second will link to libmylib.so cc_binary( name = "second", srcs = ["second.cc"], deps = [":mylib"], linkstatic = 0, )
cc_import( name = "mylib", hdrs = ["mylib.h"], static_library = "libmylib.lib", # A normal static library interface_library = "mylib.lib", # An import library for mylib.dll shared_library = "mylib.dll", ) # first will link to libmylib.lib cc_binary( name = "first", srcs = ["first.cc"], deps = [":mylib"], linkstatic = 1, # default value ) # second will link to mylib.dll through mylib.lib cc_binary( name = "second", srcs = ["second.cc"], deps = [":mylib"], linkstatic = 0, )
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          hdrs
         | 
        
                     
  | 
      
          alwayslink
         | 
        
                     
 如果 alwayslink 无法用于 Windows 上的 VS 2017,原因在于 已知问题, 请将 VS 2017 升级到最新版本。  | 
      
          interface_library
         | 
        
                     
  允许的文件类型:
              | 
      
          shared_library
         | 
        
                     
  允许的文件类型:
              | 
      
          static_library
         | 
        
                     
  允许的文件类型:
              | 
      
          system_provided
         | 
        
                     
 interface_library,
        “shared_library”应该为空。
         | 
      
cc_library
cc_library(name, deps, srcs, data, hdrs, alwayslink, compatible_with, copts, defines, deprecation, distribs, exec_compatible_with, exec_properties, features, implementation_deps, include_prefix, includes, licenses, linkopts, linkstamp, linkstatic, local_defines, nocopts, restricted_to, strip_include_prefix, tags, target_compatible_with, testonly, textual_hdrs, toolchains, visibility, win_def_file)
标头包含性检查
  build 中使用的所有头文件必须在 hdrs 或
  第 srcs 条规则(共 cc_* 条)。这是强制执行的。
  对于 cc_library 规则,hdrs 中的标头包含
  库,并且可以直接包含在 hdrs 和
  库本身的 srcs,以及 hdrs 和
  cc_* 规则中的 srcs 项会在其 deps 中列出此库。
  srcs 中的头文件只能直接从 hdrs 中的文件包含
  以及库本身的 srcs。在决定是否将标题放入
  hdrs 或 srcs,您应询问您是否需要此库的使用方
  以便能够直接添加该文件这与
  编程语言中的 public 和 private 可见性。
  cc_binary 和 cc_test 规则没有导出的接口,因此它们
  也不具有 hdrs 属性。属于二进制文件或测试文件的所有头文件
  srcs 中应直接列出。
为了说明这些规则,请看以下示例。
cc_binary(
    name = "foo",
    srcs = [
        "foo.cc",
        "foo.h",
    ],
    deps = [":bar"],
)
cc_library(
    name = "bar",
    srcs = [
        "bar.cc",
        "bar-impl.h",
    ],
    hdrs = ["bar.h"],
    deps = [":baz"],
)
cc_library(
    name = "baz",
    srcs = [
        "baz.cc",
        "baz-impl.h",
    ],
    hdrs = ["baz.h"],
)
  下表列出了此示例中允许的直接包含项。例如
  foo.cc 可以直接包含 foo.h 和 bar.h,但
  而非 baz.h。
| 包括文件 | 允许的包含项 | 
|---|---|
| foo.h | bar.h | 
| foo.cc | foo.h bar.h | 
| bar.h | bar-impl.h,baz.h | 
| bar-impl.h | bar.h baz.h | 
| bar.cc | bar.h bar-impl.h baz.h | 
| baz.h | baz-impl.h | 
| baz-impl.h | baz.h | 
| baz.cc | baz.h baz-impl.h | 
  包含检查规则仅适用于直接
  包含。在上面的示例中,foo.cc 可以
  包括bar.h,其中可能包括baz.h,
  允许 turn 包含 baz-impl.h。从技术上讲
  编译 .cc 文件可能会以传递方式包含任何头文件
  文件放在 hdrs 或 srcs
  传递 deps 闭包中的任何 cc_library。在
  在这种情况下,编译器可能会读取 baz.h 和 baz-impl.h
  编译 foo.cc 时,但 foo.cc 不得
  包含“#include "baz.h"”。为此
  允许,必须将 baz 添加到 deps
  共 foo 个。
  遗憾的是,Bazel 目前无法区分直接和传递
  因此它无法检测文件非法包含
  标头,它只允许以传递方式包含。例如:
  如果在上面的示例中直接 foo.cc,Bazel 不会抱怨
  包含 baz.h。这是违法行为,因为foo
  不直接依赖于 baz。目前未产生任何错误
  但将来可能会添加此类错误检查。
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          deps
         | 
        
                     
 这些值可以是   | 
      
          srcs
         | 
        
                     
 所有  
 所有  如果规则的名称位于  
            允许的  
 ...以及生成这些文件的任何规则。 不同的扩展表示 。  | 
      
          hdrs
         | 
        
                     
 这是声明
             描述该库的接口这些标头
             以供来源添加到此规则或依赖规则中。
             此库的客户端不应包含的标头应
             在   | 
      
          alwayslink
         | 
        
                     
 srcs,即使其中一些不包含二进制文件引用的符号也是如此。
        如果
        二进制文件。例如,如果您的代码注册以接收一些回调
        提供的服务
        如果 alwayslink 无法用于 Windows 上的 VS 2017,原因在于 已知问题, 请将 VS 2017 升级到最新版本。  | 
      
          copts
         | 
        
                     
 
            此属性中的每个字符串都会按照指定顺序添加到  
            如果该软件包声明了 feature,
              | 
      
          defines
         | 
        
                     
 -D,并添加到编译命令行中,
          以及依赖于该规则的每条规则请务必小心,
          产生深远的影响。如有疑问,请将定义值添加到
          local_defines。
         | 
      
          implementation_deps
         | 
        
                     
 deps,这些库(及其所有库)的头文件和 include 路径
        传递依赖项)仅用于编译此库,而不会用于
        一切都依赖于它使用 implementation_deps 指定的库仍然链接到
        依赖于此库的二进制文件目标
        目前,此用法仅限于 cc_library,并由标志保护
          | 
      
          include_prefix
         | 
        
                     
 设置后,此规则的  先移除   | 
      
          includes
         | 
        
                     
 
          需遵循替换 "Make variable" 的规定。
          每个字符串都带有  必须将标头添加到 srcs 或 hdrs 中,否则它们将无法供从属使用 使用沙盒(默认)编译时应遵循的规则。  | 
      
          linkopts
         | 
        
                     
 LINKOPTS 中,
          链接二进制目标。
          
            此列表中将出现每个不以   | 
      
          linkstamp
         | 
        
                     
 base 软件包。
         | 
      
          linkstatic
         | 
        
                     
 cc_binary和
           cc_test:以静态方式关联二进制文件
           模式。对于 cc_library.linkstatic:请参阅下文。
           
             默认情况下,系统会为  
             如果已启用,并且是二进制文件或测试文件,此选项将指示构建工具加入
             尽可能为用户库使用  关联可执行文件其实有三种不同的方法: 
 
            
           如果为   | 
      
          local_defines
         | 
        
                     
 -D,并添加到此目标的编译命令行中,
          但不向其依赖项传递。
         | 
      
          nocopts
         | 
        
                     
 COPTS
          (包括规则的 copts 属性中明确指定的值)将从
          COPTS,以便编译此规则。
          您应该很少需要此属性。
         | 
      
          strip_include_prefix
         | 
        
                     
 设置后,此规则的  如果是相对路径,则会被当作软件包相对路径。如果它是绝对的, 系统会将它理解为代码库相对路径。 
  | 
      
          textual_hdrs
         | 
        
                     
 此位置用于声明无法自行编译的头文件; 也就是说,它们始终需要由其他源文件以文本形式包含,才能构建有效的 代码。  | 
      
          win_def_file
         | 
        
                     
 仅当 Windows 是目标平台时,才应使用此属性。 它可用于 导出符号。  | 
      
cc_proto_library
cc_proto_library(name, deps, data, compatible_with, deprecation, distribs, exec_compatible_with, exec_properties, features, licenses, restricted_to, tags, target_compatible_with, testonly, visibility)
cc_proto_library 从 .proto 文件生成 C++ 代码。
deps 必须指向 proto_library
 规则。
示例:
cc_library(
    name = "lib",
    deps = [":foo_cc_proto"],
)
cc_proto_library(
    name = "foo_cc_proto",
    deps = [":foo_proto"],
)
proto_library(
    name = "foo_proto",
)
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          deps
         | 
        
                     
 proto_library 的列表
        为其生成 C++ 代码的规则。
         | 
      
fdo_prefetch_hints
fdo_prefetch_hints(name, compatible_with, deprecation, distribs, features, licenses, profile, restricted_to, tags, target_compatible_with, testonly, visibility)
表示工作区中或指定 绝对路径。 示例:
fdo_prefetch_hints(
    name = "hints",
    profile = "//path/to/hints:profile.afdo",
)
fdo_profile(
  name = "hints_abs",
  absolute_path_profile = "/absolute/path/profile.afdo",
)
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          profile
         | 
        
                     
  | 
      
fdo_profile
fdo_profile(name, absolute_path_profile, compatible_with, deprecation, distribs, features, licenses, profile, proto_profile, restricted_to, tags, target_compatible_with, testonly, visibility)
表示工作区中或指定绝对路径中的 FDO 配置文件。 示例:
fdo_profile(
    name = "fdo",
    profile = "//path/to/fdo:profile.zip",
)
fdo_profile(
  name = "fdo_abs",
  absolute_path_profile = "/absolute/path/profile.zip",
)
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          absolute_path_profile
         | 
        
                     
  | 
      
          profile
         | 
        
                     
  | 
      
          proto_profile
         | 
        
                     
  | 
      
propeller_optimize
propeller_optimize(name, compatible_with, deprecation, distribs, features, ld_profile, licenses, restricted_to, tags, target_compatible_with, testonly, visibility)
表示工作区中的 Propeller 优化配置文件。 示例:
propeller_optimize(
    name = "layout",
    cc_profile = "//path:cc_profile.txt",
    ld_profile = "//path:ld_profile.txt"
)
propeller_optimize(
    name = "layout_absolute",
    absolute_cc_profile = "/absolute/cc_profile.txt",
    absolute_ld_profile = "/absolute/ld_profile.txt"
)
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          ld_profile
         | 
        
                     
  | 
      
cc_test
cc_test(name, deps, srcs, data, additional_linker_inputs, args, compatible_with, copts, defines, deprecation, distribs, env, env_inherit, exec_compatible_with, exec_properties, features, flaky, includes, licenses, linkopts, linkstatic, local, local_defines, malloc, nocopts, restricted_to, shard_count, size, stamp, tags, target_compatible_with, testonly, timeout, toolchains, visibility, win_def_file)
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          deps
         | 
        
                     
 这些值可以是   | 
      
          srcs
         | 
        
                     
 所有  
 所有  如果规则的名称位于  
            允许的  
 ...以及生成这些文件的任何规则。 不同的扩展表示 。  | 
      
          additional_linker_inputs
         | 
        
                     
 例如,您可以在此处提供经过编译的 Windows .res 文件, 二进制目标。  | 
      
          copts
         | 
        
                     
 
            此属性中的每个字符串都会按照指定顺序添加到  
            如果该软件包声明了 feature,
              | 
      
          defines
         | 
        
                     
 -D,并添加到编译命令行中,
          以及依赖于该规则的每条规则请务必小心,
          产生深远的影响。如有疑问,请将定义值添加到
          local_defines。
         | 
      
          includes
         | 
        
                     
 
          需遵循替换 "Make variable" 的规定。
          每个字符串都带有  必须将标头添加到 srcs 或 hdrs 中,否则它们将无法供从属使用 使用沙盒(默认)编译时应遵循的规则。  | 
      
          linkopts
         | 
        
                     
 LINKOPTS 中,
          链接二进制目标。
          
            此列表中将出现每个不以   | 
      
          linkstatic
         | 
        
                     
 cc_binary和
           cc_test:以静态方式关联二进制文件
           模式。对于 cc_library.linkstatic:请参阅下文。
           
             默认情况下,系统会为  
             如果已启用,并且是二进制文件或测试文件,此选项将指示构建工具加入
             尽可能为用户库使用  关联可执行文件其实有三种不同的方法: 
 
            
           如果为   | 
      
          local_defines
         | 
        
                     
 -D,并添加到此目标的编译命令行中,
          但不向其依赖项传递。
         | 
      
          malloc
         | 
        
                     
 
            默认情况下,C++ 二进制文件链接到   | 
      
          nocopts
         | 
        
                     
 COPTS
          (包括规则的 copts 属性中明确指定的值)将从
          COPTS,以便编译此规则。
          您应该很少需要此属性。
         | 
      
          stamp
         | 
        
                     
 
 除非其依赖项发生变化,否则带时间戳的二进制文件不会被重新构建。  | 
      
          win_def_file
         | 
        
                     
 仅当 Windows 是目标平台时,才应使用此属性。 它可用于 导出符号。  | 
      
cc_toolchain
cc_toolchain(name, all_files, ar_files, as_files, compatible_with, compiler, compiler_files, compiler_files_without_includes, coverage_files, cpu, deprecation, distribs, dwp_files, dynamic_runtime_lib, exec_transition_for_inputs, features, libc_top, licenses, linker_files, module_map, objcopy_files, restricted_to, static_runtime_lib, strip_files, supports_header_parsing, supports_param_files, tags, target_compatible_with, testonly, toolchain_config, toolchain_identifier, visibility)
表示 C++ 工具链。
此规则负责:
- 
      收集运行 C++ 操作所需的所有工件。这是通过
      属性,例如 
all_files、compiler_files、linker_files或以_files结尾的其他属性)。这些是 最常见的文件组。 - 
      为 C++ 操作生成正确的命令行。为此,您可以使用
      
CcToolchainConfigInfo提供方(详见下文)。 
  使用 toolchain_config 属性配置 C++ 工具链。
  另请参阅此内容
  <ph type="x-smartling-placeholder"></ph>
    信息页
  ,获取详细的 C++ 工具链配置和工具链选择文档。
  使用 tags = ["manual"] 以防止构建和配置工具链
  在调用 bazel build //... 时不必要的
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          all_files
         | 
        
                     
 all_files 是一个超集
        所有其他提供工件的属性(例如,Linktamp 编译需要同时编译
        和链接文件,因此需要 all_files)。
        
        这是   | 
      
          ar_files
         | 
        
                     
 归档操作所需的所有 cc_toolchain 工件的集合。  | 
      
          as_files
         | 
        
                     
 组装操作所需的所有 cc_toolchain 工件的集合。  | 
      
          compiler
         | 
        
                     
 toolchain_identifier 属性。这只是个提示
         晚于
         <ph type="x-smartling-placeholder"></ph>
           CROSSTOOL 迁移到 Starlark
         ,并将于
         #7075。
         设置后,它将用于执行 crosstool_config.toolchain 选择。需要 比 --cpu Bazel 选项优先。  | 
      
          compiler_files
         | 
        
                     
  | 
      
          compiler_files_without_includes
         | 
        
                     
  | 
      
          coverage_files
         | 
        
                     
  | 
      
          cpu
         | 
        
                     
 设置后,它将用于执行 crosstool_config.toolchain 选择。需要 比 --cpu Bazel 选项优先。  | 
      
          dwp_files
         | 
        
                     
  | 
      
          dynamic_runtime_lib
         | 
        
                     
 当“static_link_cpp_runtimes”功能已启用,我们将 动态依赖项。  | 
      
          exec_transition_for_inputs
         | 
        
                     
  | 
      
          libc_top
         | 
        
                     
  | 
      
          linker_files
         | 
        
                     
  | 
      
          module_map
         | 
        
                     
  | 
      
          objcopy_files
         | 
        
                     
  | 
      
          static_runtime_lib
         | 
        
                     
 当“static_link_cpp_runtimes”功能已启用,我们将 依赖项。  | 
      
          strip_files
         | 
        
                     
  | 
      
          supports_header_parsing
         | 
        
                     
  | 
      
          supports_param_files
         | 
        
                     
  | 
      
          toolchain_config
         | 
        
                     
 cc_toolchain_config_info 的规则的标签。
         | 
      
          toolchain_identifier
         | 
        
                     
 
          直到问题 #5380 得到解决
          这是将   | 
      
cc_toolchain_suite
cc_toolchain_suite(name, compatible_with, deprecation, distribs, features, licenses, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)
表示 C++ 工具链的集合。
此规则负责:
- 收集所有相关的 C++ 工具链。
 - 
      根据 
--cpu和--compiler选项选择一个工具链 传递给 Bazel 
另请参阅此内容 <ph type="x-smartling-placeholder"></ph> 信息页 ,获取详细的 C++ 工具链配置和工具链选择文档。
参数
| 属性 | |
|---|---|
name | 
        
           
 此目标的唯一名称。  | 
      
          toolchains
         | 
        
                     
 cc_toolchain 标签。“<cpu>”仅当 --cpu 时使用
        会传递到 Bazel,并且“<cpu>|<compiler>”指定的两个条件
        --cpu 和 --compiler 会传递给 Bazel。示例:
        
 
          cc_toolchain_suite(
            name = "toolchain",
            toolchains = {
              "piii|gcc": ":my_cc_toolchain_for_piii_using_gcc",
              "piii": ":my_cc_toolchain_for_piii_using_default_compiler",
            },
          )
           |