简体   繁体   English

Bazel 文件不是由 genrule 创建的

[英]Bazel files not created by genrule

I am trying to generate python source code from flatbuffer schema and using the generated code in another application.我正在尝试从 flatbuffer 模式生成 python 源代码,并在另一个应用程序中使用生成的代码。 But it just doesn't seem to work.但它似乎不起作用。

I have created a simple reproducible code that highlights the issue that I am facing.我创建了一个简单的可重现代码,突出了我面临的问题。 Following is the directory structure:以下是目录结构:

-dummy/
      - BUILD
      - main.py
      - sample.fbs
      - WORKSPACE

What I am trying to do here is this:我在这里尝试做的是:

  • Define a structure in sample.fbssample.fbs中定义一个结构
  • Generate python source code corresponding to the defined flatbuffer schema生成与定义的flatbuffer schema对应的python源代码
  • Use the resulting generated source code in main.pymain.py中使用生成的源代码

The contents of each of the above files are as follows:上述每个文件的内容如下:

  • 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
    )

The problem I am facing is this:我面临的问题是:

Whenever I try to run the target //:main using the command bazel run //:main , I get the following error:每当我尝试使用命令bazel run //:main运行目标//: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

I am just not understanding why the python source code files from the schema are not getting generated.我只是不明白为什么没有生成架构中的 python 源代码文件。

Any help would be greatly appreciated.任何帮助将不胜感激。

My bazel version:我的 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

OS: Linux Ubuntu 20.04.3 LTS操作系统: Linux Ubuntu 20.04.3 LTS

If I'm reading the flatbuffer python generator correctly, I think it's generating py/foo/__init__.py , but not py/__init__.py .如果我正确读取flatbuffer python 生成器,我认为它正在生成py/foo/__init__.py ,但不是py/__init__.py Try changing outs to include that.尝试改变outs以包括它。 I think you're expected to create/generate py/__init__.py yourself.我认为您应该自己创建/生成py/__init__.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM