gpt4 book ai didi

c - 构建 cc_library 时如何在 bazel 中移动文件?

转载 作者:行者123 更新时间:2023-11-30 16:14:43 24 4
gpt4 key购买 nike

在 bazel 中从源代码构建项目时,头文件并不总是以正确的相对路径结束。

例如,我想构建 expat来自源头。
其他软件将使用的主要 header 是 <expat.h> .
然而,该项目的源代码在 expat/lib/expat.h

我可以通过以下方式成功构建项目:

cc_library(
name = "expat",
srcs = [
"lib/xmlparse.c",
"lib/xmlrole.c",
"lib/xmltok.c",
],
hdrs = [
"expat_config.h",
"lib/ascii.h",
"lib/asciitab.h",
"lib/expat.h",
"lib/expat_external.h",
"lib/iasciitab.h",
"lib/internal.h",
"lib/latin1tab.h",
"lib/nametab.h",
"lib/siphash.h",
"lib/utf8tab.h",
"lib/xmlrole.h",
"lib/xmltok.h",
"lib/xmltok_impl.c",
"lib/xmltok_impl.h",
"lib/xmltok_ns.c",
],
includes = [
".",
"lib",
],
visibility = ["//visibility:public"],
)

但现在其他项目尝试与 @expat//:expat 链接将无法找到<expat.h> 。正确的包含应该是 <expat/lib/expat.h>这是在其他软件中无法更改的。

按原样构建项目但仍确保头文件位于使用它们的其他项目的正确位置的最佳方法是什么?我想知道在调用 cc_library 之前是否需要先在 lark 中执行 cp ,或者我之后是否可以移动标题。我不想更改项目的实际内容(因为我可能不托管它或者我希望镜像保持一致)。我不确定如何编写规则来执行此操作。

最佳答案

我通过单独的规则单独移动每个文件解决了这个问题。

首先,我对 mv 制定了一条规则:

def mv_file(name, file_from, file_to):

native.genrule(
name = name,
srcs = [file_from],
outs = [file_to],
output_to_bindir = 1,
cmd = "mv $< $@",
)

然后在依赖项的 BUILD 文件中,我按如下方式使用它:

mv_file("xmlparse_c","expat/lib/xmlparse.c","xmlparse.c")
mv_file("xmlrole_c","expat/lib/xmlrole.c","xmlrole.c")
mv_file("xmltok_c","expat/lib/xmltok.c","xmltok.c")
mv_file("ascii_h","expat/lib/ascii.h","ascii.h")
mv_file("asciitab_h","expat/lib/asciitab.h","asciitab.h")
mv_file("expat_h","expat/lib/expat.h","expat.h")
mv_file("expat_external_h","expat/lib/expat_external.h","expat_external.h")
mv_file("iasciitab_h","expat/lib/iasciitab.h","iasciitab.h")
mv_file("internal_h","expat/lib/internal.h","internal.h")
mv_file("latin1tab_h","expat/lib/latin1tab.h","latin1tab.h")
mv_file("nametab_h","expat/lib/nametab.h","nametab.h")
mv_file("siphash_h","expat/lib/siphash.h","siphash.h")
mv_file("utf8tab_h","expat/lib/utf8tab.h","utf8tab.h")
mv_file("xmlrole_h","expat/lib/xmlrole.h","xmlrole.h")
mv_file("xmltok_h","expat/lib/xmltok.h","xmltok.h")
mv_file("xmltok_impl_c","expat/lib/xmltok_impl.c","xmltok_impl.c")
mv_file("xmltok_impl_h","expat/lib/xmltok_impl.h","xmltok_impl.h")
mv_file("xmltok_ns_c","expat/lib/xmltok_ns.c","xmltok_ns.c")

cc_library(
name = "expat",
srcs = [
"xmlparse.c",
"xmlrole.c",
"xmltok.c",
],
hdrs = [
"expat/expat_config.h",
"ascii.h",
"asciitab.h",
"expat.h",
"expat_external.h",
"iasciitab.h",
"internal.h",
"latin1tab.h",
"nametab.h",
"siphash.h",
"utf8tab.h",
"xmlrole.h",
"xmltok.h",
"xmltok_impl.c",
"xmltok_impl.h",
"xmltok_ns.c",
],
includes = [
".",
],
deps = [
],
visibility = ["//visibility:public"],
)

令人困惑的是,我不需要在 expat 的 cc_library 中指定移动的目标名称。我不确定 bazel 如何知道在构建 expat 目标之前首先运行所有 mv_file 目标,但它似乎有效。我无法将 mv_file 目标名称设置为 cc_library 的依赖项,但也许有一种解决方法可以确保根据其依赖项正确指定操作。

关于c - 构建 cc_library 时如何在 bazel 中移动文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57465529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com