2016-10-04 83 views
2

我目前正在嘗試使用bazel爲Android編譯TensorFlow的目標//tensorflow:libtensorflow_cc.so。我需要這個庫才能獲得適用於Android的TensorFlow的javacpp-presets。Tensorflow:如何爲Android編譯libtensorflow_cc.so

我嘗試了以下聲明:

bazel build -c opt //tensorflow:libtensorflow_cc.so --crosstool_top=//external:android/crosstool --cpu=armeabi-v7a [email protected]_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。

+0

這giflib的版本似乎已經解決了這一問題: https://android.googlesource.com/platform/external/giflib /+/814d1938f091d311c709bc714c2d31032d43d7bc/egif_lib.c#61 注意的評論,他們已經進行了更改:/ * Android的改變:改變「S_IREAD | S_IWRITE」到「S_IRUSR | S_IWUSR」 */ –

+0

我更新的問題,丹艾伯特的答案解決了原來的問題,但是卻導致了下一個問題。 – andy

+0

應該問一個新的問題,因爲這個問題是不相關的,會有一個完全不同的答案。第二部分看起來只是TF相關而不是NDK相關的。 –

回答

2

從谷歌搜索,我發現S_IWRITE標誌已被棄用,因此從未在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頭文件已經過時了。這是https://github.com/android-ndk/ndk/issues/120

我們將在NDK r14中解決這個問題(我剛剛提交了https://github.com/android-ndk/ndk/issues/211來修復舊的頭文件,以防120號文件在那時沒有修復)。

在此之前,您可以將這些定義添加到您的cflags中。貌似做方式這巴澤勒是:

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

+0

謝謝!這解決了這個問題。但是,現在我有下一個問題。你知道如何解決這個問題嗎? – andy