Tensorflow:如何为 Android 编译 libtensorflow_cc.so

Tensorflow: How to compile libtensorflow_cc.so for Android

我目前正在尝试使用 bazel 为 Android 编译 TensorFlow 的目标//tensorflow:libtensorflow_cc.so。我需要这个库才能获得用于 TensorFlow 的 javacpp-presets Android.

我尝试了以下语句:

bazel build -c opt //tensorflow:libtensorflow_cc.so --crosstool_top=//external:android/crosstool --cpu=armeabi-v7a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --verbose_failures

但是会导致无法找到 S_IREAD、S_IWRITE 的错误:

external/gif_archive/giflib-5.1.4/lib/egif_lib.c:62:6: error: 'S_IREAD' undeclared (first use in this function)
  S_IREAD | S_IWRITE);
  ^
external/gif_archive/giflib-5.1.4/lib/egif_lib.c:62:6: note: each undeclared identifier is reported only once for each function it appears in
external/gif_archive/giflib-5.1.4/lib/egif_lib.c:62:16: error: 'S_IWRITE' undeclared (first use in this function)
      S_IREAD | S_IWRITE);
                ^
Target //tensorflow:libtensorflow_cc.so failed to build

Android Demo build中Android的启发,我也尝试将cc_binary的定义改成下面的代码,还是报同样的错误

cc_binary(
    name = "libtensorflow_cc.so",
    copts = tf_copts(),
    linkopts = [
        "-landroid",
        "-ljnigraphics",
        "-llog",
        "-lm",
        "-z defs",
        "-s",
        "-Wl,--icf=all",  # Identical Code Folding
    ],
    linkshared = 1,
    linkstatic = 1,
    deps = [
        "//tensorflow/c:c_api",
        "//tensorflow/cc:cc_ops",
        "//tensorflow/core:tensorflow",
    ],
)

通过谷歌搜索,我发现 S_IWRITE 标志已被弃用,因此从未在 Android 中实现。但是,我不知道如何解决这个问题。

总结一下:您知道我如何为 Android 构建 libtensorflow_cc.so 目标吗? Android 示例中的库构建对我来说还不够,因为我还需要包含 cc_ops。

From googling, I found out that the S_IWRITE flags are deprecated and therefore have never been implemented in Android.

为了兼容性,我们似乎改变了主意:https://android.googlesource.com/platform/bionic/+/1f1a51aecd7c825418bfedcb66772e92de790149%5E%21/#F2

#if defined(__USE_BSD) || defined(__USE_GNU)
#define S_IREAD S_IRUSR
#define S_IWRITE S_IWUSR
#define S_IEXEC S_IXUSR
#endif

这是系统的sys/stat.h;它尚未在 NDK 中发布。不幸的是,大多数 NDK headers 都已经过时了。这是https://github.com/android-ndk/ndk/issues/120

我们将在 NDK r14 中修复此问题(我刚刚提交 https://github.com/android-ndk/ndk/issues/211 以修复旧的 headers,以防 #120 届时无法修复)。

在那之前,您可以将这些定义添加到您的 cflags 中。看起来在 bazel 中执行此操作的方法是:

cc_binary(
    name = "libtensorflow_cc.so",
    defines = [
        "S_IREAD=S_IRUSR",
        "S_IWRITE=S_IWUSR",
        "S_IEXEC=S_IXUSR",
    ],
    ...
)

https://www.bazel.io/versions/master/docs/be/c-cpp.html#cc_binary.defines