【发布时间】:2022-01-03 15:11:16
【问题描述】:
我正在尝试从 flatbuffer 模式生成 python 源代码并在另一个应用程序中使用生成的代码。但这似乎不起作用。
我创建了一个简单的可重现代码,突出了我面临的问题。以下是目录结构:
-dummy/
- BUILD
- main.py
- sample.fbs
- WORKSPACE
我在这里尝试做的是:
- 在
sample.fbs中定义一个结构 - 生成与定义的flatbuffer schema对应的python源代码
- 在
main.py中使用生成的源代码
上述每个文件的内容如下:
BUILD
load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_library_public")
package(default_visibility = ["//visibility:public"])
python_export_classes_list = [
"__init__",
"Foo",
]
flatbuffer_library_public(
name = "schema_py",
srcs = ["sample.fbs"],
outs = ["py/%s.py" % f for f in python_export_classes_list],
language_flag = "-p",
out_prefix = "py/",
)
py_library(
name = "schema_lib",
srcs = [":schema_py"],
imports = ["py/"],
)
py_binary(
name = "main",
srcs = ["main.py"],
deps = ["//:schema_lib"]
)
main.py
from foo import Foo
sample.fbs
namespace foo;
struct Foo {
bar: int;
}
WORKSPACE
workspace(name = "dummy")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
maybe(
git_repository,
name = "com_github_google_flatbuffers",
remote = "https://github.com/google/flatbuffers",
commit = "a9a295fecf3fbd5a4f571f53b01f63202a3e2113", # flatbuffers 2.0.0
)
我面临的问题是这样的:
每当我尝试使用命令bazel run //:main 运行目标//:main 时,都会收到以下错误:
DEBUG: Rule 'com_github_google_flatbuffers' indicated that a canonical reproducible form can be obtained by modifying arguments shallow_since = "1620672316 -0700"
DEBUG: Repository com_github_google_flatbuffers instantiated at:
/home/abc/dummy/WORKSPACE:7:6: in <toplevel>
/home/abc/.cache/bazel/_bazel_abc/8ad323a425180f6fe9a223b33e8e7665/external/bazel_tools/tools/build_defs/repo/utils.bzl:201:18: in maybe
Repository rule git_repository defined at:
/home/abc/.cache/bazel/_bazel_abc/8ad323a425180f6fe9a223b33e8e7665/external/bazel_tools/tools/build_defs/repo/git.bzl:199:33: in <toplevel>
INFO: Analyzed target //:main (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /home/abc/dummy/BUILD:10:26: declared output 'py/__init__.py' was not created by genrule. This is probably because the genrule actually didn't create this output, or because the output was a directory and the genrule was run remotely (note that only the contents of declared file outputs are copied from genrules run remotely)
ERROR: /home/abc/dummy/BUILD:10:26: declared output 'py/Foo.py' was not created by genrule. This is probably because the genrule actually didn't create this output, or because the output was a directory and the genrule was run remotely (note that only the contents of declared file outputs are copied from genrules run remotely)
ERROR: /home/abc/dummy/BUILD:10:26: Generating flatbuffer files for schema_py: //:schema_py failed: not all outputs were created or valid
Target //:main failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /home/abc/dummy/BUILD:24:10 Middleman _middlemen/main-runfiles failed: not all outputs were created or valid
INFO: Elapsed time: 0.097s, Critical Path: 0.01s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully
我只是不明白为什么没有生成架构中的 python 源代码文件。
任何帮助将不胜感激。
我的 bazel 版本:
Build label: 4.2.1
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Mon Aug 30 15:17:47 2021 (1630336667)
Build timestamp: 1630336667
Build timestamp as int: 1630336667
操作系统:Linux Ubuntu 20.04.3 LTS
【问题讨论】:
-
对事物的 bazel 方面不太熟悉,抱歉.. 您可能会更幸运地在 FlatBuffers 回购中发布问题
-
您确定在“out”和“out_prefix”中都需要目录“py”?
标签: python bazel flatbuffers bazel-rules