如何自定义Bazel中ccstrip的默认strip参数?
自定义Bazel中ccstrip的默认参数
方法1:自定义CC Toolchain覆盖strip参数
Bazel的ccstrip默认参数由CC Toolchain配置控制,你可以通过自定义toolchain替换默认的strip flags:
- 创建
cc_toolchain_config.bzl文件,实现自定义toolchain配置,重点修改strip_args字段:
def _impl(ctx): # 保留其他必要的toolchain配置项 strip_args = [ "--strip-debug", # 替换默认的-S、-p等参数 "--keep-file-symbols" # 按需添加你的自定义参数 ] return cc_common.create_cc_toolchain_config_info( ctx = ctx, # 补充其他必填配置(如target_cpu、compiler等) strip_args = strip_args, ) cc_toolchain_config = rule( implementation = _impl, attrs = {}, provides = [CcToolchainConfigInfo], )
- 在BUILD文件中声明自定义toolchain:
load(":cc_toolchain_config.bzl", "cc_toolchain_config") cc_toolchain_config(name = "my_cc_toolchain_config") cc_toolchain( name = "my_cc_toolchain", toolchain_config = ":my_cc_toolchain_config", toolchain_identifier = "my-custom-toolchain", )
- 构建时指定使用该toolchain:
bazel build --crosstool_top=//:my_cc_toolchain //main:hello-world
方法2:临时禁用默认strip后手动处理
如果只是临时调整strip参数,无需修改toolchain,可以先禁用Bazel的默认strip行为,再手动执行自定义strip命令:
# 禁用默认strip构建目标 bazel build --strip=never //main:hello-world # 执行自定义strip命令 /usr/bin/strip --strip-debug -p -o bazel-out/k8-fastbuild/bin/main/hello-world.stripped bazel-out/k8-fastbuild/bin/main/hello-world
方法3:通过bazelrc全局应用自定义配置
如果想让项目默认使用自定义strip参数,可在项目根目录的.bazelrc中添加以下配置,避免每次构建手动指定toolchain:
build --crosstool_top=//:my_cc_toolchain
内容的提问来源于stack exchange,提问作者Maneesh Singh
相关产品推荐
相关产品推荐

