如果目标未在工作空间的根目录声明,则无法找到jni.h

时间:2018-04-13 20:19:59

标签: java java-native-interface bazel

我有一个相对简单的BUILD文件:

#include <jni.h>

有一个更简单的源文件:

$ bazel build //:target
INFO: Analysed target //:target (1 packages loaded).
INFO: Found 1 target...
Target //:target up-to-date:
  bazel-bin/libtarget.a
INFO: Elapsed time: 0.348s, Critical Path: 0.00s
INFO: Build completed successfully, 1 total action

如果两个文件都位于工作区根目录下,那么一切正常:

$ bazel build //nested:target
WARNING: /path/to/workspace/nested/BUILD:3:10: in srcs attribute of cc_library rule //nested:target: please do not import '@local_jdk//:include/jni.h' directly. You should either move the file to this package or depend on an appropriate rule there
WARNING: /path/to/workspace/nested/BUILD:3:10: in srcs attribute of cc_library rule //nested:target: please do not import '@local_jdk//:include/linux/jni_md.h' directly. You should either move the file to this package or depend on an appropriate rule there
INFO: Analysed target //nested:target (0 packages loaded).
INFO: Found 1 target...
ERROR: /path/to/workspace/nested/BUILD:1:1: C++ compilation of rule '//nested:target' failed (Exit 1)
nested/hello.cc:1:10: fatal error: jni.h: No such file or directory
 #include <jni.h>
      ^~~~~~~
compilation terminated.
Target //nested:target failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.424s, Critical Path: 0.09s
FAILED: Build did NOT complete successfully

但是,如果我将它们嵌套在目录中,则构建将失败:

$ bazel version
Build label: 0.12.0
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Aug 4 01:22:27 +50246 (1523462001747)
Build timestamp: 1523462001747
Build timestamp as int: 1523462001747

我说错了吗?

{{1}}

1 个答案:

答案 0 :(得分:1)

不是修复,而是解决方法......

在顶层的BUILD文件中:

cc_library(
  name = "jni_headers",
  srcs = [
    "@local_jdk//:jni_header",
    "@local_jdk//:jni_md_header-linux",
  ],
  includes = [
    "external/local_jdk/include",
    "external/local_jdk/include/linux",
  ],
  visibility = [
    "//visibility:public",
  ],
)

在嵌套目录中:

cc_library(
  name = "target",
  srcs = [
    "hello.cc",
  ],
  deps = [
    "//:jni_headers",
  ]
  linkstatic = 1
)