从bazel中的源代码构建项目时,头文件并不总是以正确的相对路径结尾。
例如,我想从源代码构建expat。
其他软件将使用的主要标头是<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
之前先在云雀中执行cp,或者之后是否可以移动标头。我不想更改项目的实际内容(因为我可能没有托管它,或者我希望镜像保持一致)。我不确定如何编写规则来做到这一点。
答案 0 :(得分:0)
我通过按照单独的规则分别移动每个文件来解决了这个问题。
首先,我对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如何在构建mv_file
目标之前先运行所有expat
目标,但似乎可以正常工作。而且我无法将mv_file
目标名称设置为cc_library
的deps,但是也许有一种方法可以确保根据动作的依赖关系正确指定动作。