【发布时间】:2025-12-15 00:10:01
【问题描述】:
假设我有一个这样的 Bazel 项目:
tree .
.
├── foo
│ ├── BUILD.bazel
│ └── foo.txt
└── WORKSPACE
1 directory, 3 files
foo/BUILD.bazel:
genrule(
name = "bar",
srcs = [
"foo.txt",
],
cmd = "cp foo.txt $@",
outs = [
"bar.txt",
],
)
我无法构建bazel build //foo:bar:
bazel build //foo:bar
...
cp: cannot stat 'foo.txt': No such file or directory
看来cmd 中的路径必须相对于WORKSPACE 根,而不是BUILD 根。
这行得通:
genrule(
name = "bar",
srcs = [
"foo.txt",
],
# cmd = "cp foo.txt $@",
cmd = "cp foo/foo.txt $@",
outs = [
"bar.txt",
],
)
必须指定完整路径很不方便,尤其是当BUILD 文件可能被移动时。
很高兴能够编写脚本就好像它们从它们在源树中的位置运行(当然它们实际上在沙箱中运行! )
是否有一个 Make 变量替换可以让我更清楚地指定它?
例如:
genrule(
name = "bar",
srcs = [
"foo.txt",
],
cmd = "cd $(SRCDIR) && cp foo.txt $@",
outs = [
"bar.txt",
],
)
这里的$(SRCDIR) 可以扩展为./foo。
请注意,这是一个人为的示例。我不能使用$(SRCS),因为我需要以不同的方式使用输入文件。我也不能使用$<,因为我不止一次使用srcs。
【问题讨论】:
标签: bazel