如何在Bazel中实现非原生自定义构建配置与变体管理
步骤1:定义变体维度约束
首先在工作区根目录的BUILD文件中定义你需要的三个变体维度对应的约束:
# 定义约束维度 constraint_setting(name = "has_hw_x") constraint_value( name = "hw_x_enabled", constraint_setting = ":has_hw_x", ) constraint_value( name = "hw_x_disabled", constraint_setting = ":has_hw_x", ) constraint_setting(name = "connect_interface") constraint_value( name = "interface_eth", constraint_setting = ":connect_interface", ) constraint_value( name = "interface_usb", constraint_setting = ":connect_interface", ) constraint_value( name = "interface_uart", constraint_setting = ":connect_interface", ) constraint_setting(name = "fail_fast") constraint_value( name = "fail_fast_enabled", constraint_setting = ":fail_fast", ) constraint_value( name = "fail_fast_disabled", constraint_setting = ":fail_fast", )
步骤2:定义预设配置
还是在根目录BUILD文件中,把你需要的4种预设配置对应到上面的约束组合:
# 客户配置1 config_setting( name = "customer1", constraint_values = [ ":hw_x_enabled", ":interface_eth", ":fail_fast_enabled", ], ) # 客户配置2 config_setting( name = "customer2", constraint_values = [ ":hw_x_enabled", ":interface_usb", ":fail_fast_enabled", ], ) # 客户配置3 config_setting( name = "customer3", constraint_values = [ ":hw_x_disabled", ":interface_usb", ":fail_fast_disabled", ], ) # 硬件调试配置 config_setting( name = "debughw", constraint_values = [ ":hw_x_disabled", ":interface_uart", ":fail_fast_enabled", ], )
步骤3:满足各项需求的实现
1. 通用目标默认兼容所有配置,专属组件指定适配
- 普通目标不需要做任何修改,天然支持所有配置构建,新增配置时只需要新增上面的
config_setting定义,不需要修改原有目标。 - 仅适配特定配置的组件,直接在属性中声明即可,比如仅硬件调试配置可用的组件:
cc_library( name = "hw_debug_only_component", srcs = ["debug.c"], target_compatible_with = [":hw_x_disabled", ":interface_uart"], )
2. 按配置自动添加编译标记
在编译属性中通过select()匹配约束值即可,示例:
cc_library( name = "common_lib", srcs = ["common.c"], copts = select({ ":interface_eth": ["-DVARIANT_POINT_CONNECTED_ETH"], ":interface_usb": ["-DVARIANT_POINT_CONNECTED_USB"], ":interface_uart": ["-DVARIANT_POINT_CONNECTED_UART"], "//conditions:default": [], }) + select({ ":hw_x_enabled": ["-DHW_X_ENABLED"], "//conditions:default": [], }), )
3. 简化构建命令
有两种实现方式可选:
方式1:用--config参数(更简单)
在工作区根目录的.bazelrc文件中添加如下配置:
build:customer1 --constraint_value=//:hw_x_enabled --constraint_value=//:interface_eth --constraint_value=//:fail_fast_enabled build:customer2 --constraint_value=//:hw_x_enabled --constraint_value=//:interface_usb --constraint_value=//:fail_fast_enabled build:customer3 --constraint_value=//:hw_x_disabled --constraint_value=//:interface_usb --constraint_value=//:fail_fast_disabled build:debughw --constraint_value=//:hw_x_disabled --constraint_value=//:interface_uart --constraint_value=//:fail_fast_enabled
之后就可以直接用对应命令构建:bazel build //my/target --config=debughw
方式2:自定义全局flag(符合你期望的命令格式)
在根目录BUILD文件中添加自定义flag和配置转换逻辑:
load("@bazel_skylib//rules:common_settings.bzl", "string_flag") string_flag( name = "configuration", build_setting_default = "customer1", values = ["customer1", "customer2", "customer3", "debughw"], ) # 绑定flag值到对应约束 config_setting( name = "use_customer1", flag_values = {":configuration": "customer1"}, ) config_setting( name = "use_customer2", flag_values = {":configuration": "customer2"}, ) config_setting( name = "use_customer3", flag_values = {":configuration": "customer3"}, ) config_setting( name = "use_debughw", flag_values = {":configuration": "debughw"}, )
之后就可以用你期望的命令格式构建:bazel build //my/target --//:configuration=debughw
内容的提问来源于stack exchange,提问作者Andreas
相关产品推荐
相关产品推荐

