找不到生成的标头

时间:2018-06-25 13:37:36

标签: c++ bazel flatbuffers

我正在尝试使用Bazel构建使用Flatbuffers的cpp项目。 但是找不到用map_schema_generated.h生成的flatc

我的树:

|
|_ data
|  |_ maps
|     |_ BUILD
|     |_ map_schema.fbs
|
|_ src
|  |_ map
|     |_ BUILD
|     |_ map.hpp
|     |_ map.cpp
|
|_ tools
|  |_ BUILD
|  |_ generate_fbs.bzl
|
|_ WORKSPACE

tools/generate_fbs.bzl

def _impl(ctx):
  output = ctx.outputs.out
  input = ctx.files.srcs
  print("generating", output.basename)
  ctx.action(
      use_default_shell_env = True,
      outputs = [output],
      inputs = input,
      progress_message="Generating %s with %s" % (output.path, input[0].path),
      command="flatc -o %s --cpp %s" % (output.dirname, input[0].path)
  )

generate_fbs = rule(
  implementation=_impl,
  output_to_genfiles = True,
  attrs={
       "srcs": attr.label_list(allow_files=True, allow_empty=False),
       "out": attr.output()
       },
)

data/maps/BUILD

load("//tools:generate_fbs.bzl", "generate_fbs")

generate_fbs(
  name = "schema",
  srcs = ["map_schema.fbs"],
  out = "map_schema_generated.h",
  visibility = ["//visibility:public"]
)

src/map/BUILD

cc_library(
  name = "map",
  srcs = [
    "//data/maps:map_schema_generated.h",
    "map.hpp",
    "map.cpp"
  ]
)

src/map/map.cc#include "map_schema_generated.h"

我用来构建的命令行:bazel build //src/map

如果我在findbazel-*,我会得到:

bazel-genfiles/data/maps/map_schema_generated.h
bazel-out/k8-fastbuild/genfiles/data/maps/map_schema_generated.h
bazel-my-workspace-name/bazel-out/k8-fastbuild/genfiles/data/maps/map_schema_generated.h

如果我cat这些文件,我可以看到它们生成的很好。

我发现的所有信息都是关于Tensorflow的,实际上并没有帮助。

最诚挚的问候,

2 个答案:

答案 0 :(得分:2)

我可以写成“ #include“ data / maps / map_schema_g​​enic.h”,而不是#include "map_schema_generated.h"中的src/map/map.cpp

我认为这是使其工作最干净的方法。

答案 1 :(得分:1)

问题是您的varsss = self.optimistic_restore(tf.train.latest_checkpoint(my_ckpt_dir)) b = tf.global_variables() tf.train.Saver(var_list=b[:len(varsss)]) 实际上并没有真正识别出生成的标头需要任何特殊的操作(例如为其所在的位置添加cc_library标志)。它会生成并驻留在构建树中,但不会在编译器(预处理器)会在-I上查找它的任何地方。 (使用map.cpp运行构建可以更深入地了解发生的情况和发生的情况。)

现在关于如何解决此问题,也许有更好的方法,但这似乎行得通。我猜该功能也可以纳入-s规则中。

generate_fbs中,我添加了“仅标头”库,如下所示:

data/maps/BUILD

然后在cc_library( name = "map_schema_hdr", hdrs = [":map_schema_generated.h"], include_prefix = ".", # to manipulate -I of dependenices visibility = ["//visibility:public"] ) 中,我将只使用此标头的库用作src/map/BUILD的依赖项:

map

为了更加方便使用单个规则(宏)的想法,我进行了以下更改:

cc_library( name = "map", srcs = [ "map.cpp" "map.hpp" ], deps = [ "//data/maps:map_schema_hdr", ] ) 现在显示为:

tools/generate_fbs.bzl

这样,我可以拥有def _impl(ctx): output = ctx.outputs.out input = ctx.files.srcs print("generating", output.basename) ctx.action( use_default_shell_env = True, outputs = [output], inputs = input, progress_message="Generating %s with %s" % (output.path, input[0].path), command="/bin/cp %s %s" % (input[0].path, output.path) ) _generate_fbs = rule( implementation=_impl, output_to_genfiles = True, attrs={ "srcs": attr.label_list(allow_files=True, allow_empty=False), "out": attr.output() }, ) def generate_fbs(name, srcs, out): _generate_fbs( name = "_%s" % name, srcs = srcs, out = out ) native.cc_library( name = name, hdrs = [out], include_prefix = ".", visibility = ["//visibility:public"], )

data/maps/BUILD

load("//tools:generate_fbs.bzl", "generate_fbs") generate_fbs( name = "schema", srcs = ["map_schema.fbs"], out = "map_schema_generated.h", ) 包含:

src/map/BUILD

然后cc_library( name = "map", srcs = [ "map.cpp", "map.hpp", ], deps = [ "//data/maps:schema", ] ) 构建bazel build //src/mapbazel-bin/src/map/libmap.a