Skip to content

Instantly share code, notes, and snippets.

@kujyp
Last active November 9, 2019 08:18
Show Gist options
  • Save kujyp/9c4c8c63cfd12f797639874d151b1707 to your computer and use it in GitHub Desktop.
Save kujyp/9c4c8c63cfd12f797639874d151b1707 to your computer and use it in GitHub Desktop.
CMakeLists.txt with tensorflow 1.4 for Android
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Ref: https://github.com/tensorflow/tensorflow/blob/v1.4.0/tensorflow/contrib/android/cmake/CMakeLists.txt#L37-L48
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIS_SLIM_BUILD \
-std=c++11 -fno-rtti -fexceptions \
-O2 -Wno-narrowing -fomit-frame-pointer \
-fPIE \
-ftemplate-depth=900 \
-DGOOGLE_PROTOBUF_NO_RTTI \
-DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} \
-Wl,--allow-multiple-definition \
-fPIE -pie -v")
# Ref: https://stackoverflow.com/a/27093163
# Ref2: https://android.googlesource.com/platform/ndk/+/master/docs/user/common_problems.md#target-api-set-higher-than-device-api
set(ANDROID_PLATFORM android-14)
file(GLOB offline_nmt_interface_sources
${CMAKE_CURRENT_SOURCE_DIR}/offline_nmt_interface/*.c*)
add_library(offline_nmt_translator SHARED
${offline_nmt_interface_sources}
offline_nmt_translator.cpp)
# Ref: https://github.com/tensorflow/tensorflow/blob/v1.4.0/tensorflow/contrib/android/cmake/CMakeLists.txt#L63-L68
# Ref2: -Wl,--whole-archive - https://oss.navercorp.com/Papago-MT/offline_nmt_interface/issues/62
target_link_libraries(offline_nmt_translator
android
dl
log
m
z
-Wl,--whole-archive ${CMAKE_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libtensorflow-core.a
-Wl,--no-whole-archive ${CMAKE_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libprotobuf.a
-Wl,--no-whole-archive ${CMAKE_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libnsync.a)
include_directories(
${CMAKE_SOURCE_DIR}/jniLibs/include/${ANDROID_ABI}/protobuf
${CMAKE_SOURCE_DIR}/jniLibs/include/eigen
${CMAKE_SOURCE_DIR}/jniLibs/include/nsync
${CMAKE_SOURCE_DIR}/jniLibs/include/proto
${CMAKE_SOURCE_DIR}/jniLibs/include/tensorflow
${CMAKE_SOURCE_DIR}/jniLibs/include/third_party)
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Ref: https://github.com/tensorflow/tensorflow/blob/v1.4.0/tensorflow/contrib/android/cmake/CMakeLists.txt#L37-L48
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIS_SLIM_BUILD \
-std=c++11 -fno-rtti -fexceptions \
-O2 -Wno-narrowing -fomit-frame-pointer \
-fPIE \
-ftemplate-depth=900 \
-DGOOGLE_PROTOBUF_NO_RTTI \
-DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} \
-Wl,--allow-multiple-definition \
-Wl,--whole-archive \
-fPIE -pie -v")
# Ref: https://stackoverflow.com/a/27093163
# Ref2: https://android.googlesource.com/platform/ndk/+/master/docs/user/common_problems.md#target-api-set-higher-than-device-api
set(ANDROID_PLATFORM android-14)
file(GLOB offline_nmt_interface_sources
${CMAKE_CURRENT_SOURCE_DIR}/offline_nmt_interface/*.c*)
add_library(offline_nmt_translator SHARED
${offline_nmt_interface_sources}
offline_nmt_translator.cpp)
# Ref: https://github.com/tensorflow/tensorflow/blob/v1.4.0/tensorflow/contrib/android/cmake/CMakeLists.txt#L63-L68
target_link_libraries(offline_nmt_translator
android
dl
log
m
z
${CMAKE_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libnsync.a
${CMAKE_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libprotobuf.a
${CMAKE_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libtensorflow-core.a)
include_directories(
${CMAKE_SOURCE_DIR}/jniLibs/include/${ANDROID_ABI}/protobuf
${CMAKE_SOURCE_DIR}/jniLibs/include/eigen
${CMAKE_SOURCE_DIR}/jniLibs/include/nsync
${CMAKE_SOURCE_DIR}/jniLibs/include/proto
${CMAKE_SOURCE_DIR}/jniLibs/include/tensorflow
${CMAKE_SOURCE_DIR}/jniLibs/include/third_party)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment