Home | History | Annotate | Download | only in cmake
      1 #
      2 # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      3 #
      4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #      http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 #
     16 cmake_minimum_required(VERSION 3.4.1)
     17 include(ExternalProject)
     18 
     19 # TENSORFLOW_ROOT_DIR:
     20 #    root directory of tensorflow repo
     21 #    used for shared source files and pre-built libs
     22 get_filename_component(TENSORFLOW_ROOT_DIR ../../../.. ABSOLUTE)
     23 set(PREBUILT_DIR ${TENSORFLOW_ROOT_DIR}/tensorflow/contrib/makefile/gen)
     24 
     25 add_library(lib_proto STATIC IMPORTED )
     26 set_target_properties(lib_proto PROPERTIES IMPORTED_LOCATION
     27     ${PREBUILT_DIR}/protobuf/lib/libprotobuf.a)
     28 
     29 add_library(lib_nsync STATIC IMPORTED )
     30 set_target_properties(lib_nsync PROPERTIES IMPORTED_LOCATION
     31     ${TARGET_NSYNC_LIB}/lib/libnsync.a)
     32 
     33 add_library(lib_tf STATIC IMPORTED )
     34 set_target_properties(lib_tf PROPERTIES IMPORTED_LOCATION
     35         ${PREBUILT_DIR}/lib/libtensorflow-core.a)
     36 # Change to compile flags should be replicated into bazel build file
     37 # TODO: Consider options other than -O2 for binary size.
     38 #       e.g. -Os for gcc, and -Oz for clang.
     39 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIS_SLIM_BUILD \
     40                      -std=c++11 -fno-rtti -fno-exceptions \
     41                      -O2 -Wno-narrowing -fomit-frame-pointer \
     42                      -mfpu=neon -mfloat-abi=softfp -fPIE -fPIC \
     43                      -ftemplate-depth=900 \
     44                      -DGOOGLE_PROTOBUF_NO_RTTI \
     45                      -DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER")
     46 
     47 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} \
     48                               -Wl,--allow-multiple-definition \
     49                               -Wl,--whole-archive \
     50                               -fPIE -pie -v")
     51 file(GLOB tensorflow_inference_sources
     52      ${CMAKE_CURRENT_SOURCE_DIR}/../jni/*.cc)
     53 file(GLOB java_api_native_sources
     54      ${TENSORFLOW_ROOT_DIR}/tensorflow/java/src/main/native/*.cc)
     55 
     56 add_library(tensorflow_inference SHARED
     57             ${tensorflow_inference_sources}
     58             ${TENSORFLOW_ROOT_DIR}/tensorflow/c/tf_status_helper.cc
     59             ${TENSORFLOW_ROOT_DIR}/tensorflow/c/checkpoint_reader.cc
     60             ${TENSORFLOW_ROOT_DIR}/tensorflow/c/test_op.cc
     61             ${TENSORFLOW_ROOT_DIR}/tensorflow/c/c_api.cc
     62             ${java_api_native_sources})
     63 
     64 # Include libraries needed for hello-jni lib
     65 target_link_libraries(tensorflow_inference
     66                       android
     67                       dl
     68                       log
     69                       m
     70                       z
     71                       lib_tf
     72                       lib_proto
     73                       lib_nsync)
     74 
     75 include_directories(
     76     ${PREBUILT_DIR}/proto
     77     ${PREBUILT_DIR}/protobuf/include
     78     ${TENSORFLOW_ROOT_DIR}/tensorflow/contrib/makefile/downloads/eigen
     79     ${TENSORFLOW_ROOT_DIR}
     80     ${CMAKE_CURRENT_SOURCE_DIR}/..)
     81