Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / tf_call_ie_layer / build.sh
1 # Copyright (c) 2018 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 #!/bin/bash
16
17 get_tf_version()
18 {
19     tf_dir=$1
20     TF_VERSION=$(cd $tf_dir 2>/dev/null; git describe --abbrev=0 --tags 2>/dev/null)
21     if [ $? != 0 ]; then
22         echo "ERROR: failed to determine TensorFlow version."
23         echo "Please, set environment variable TF_VERSION with TensorFlow version."
24         echo "For example, 'export TF_VERSION=1.5'"
25         exit 1
26     else
27         TF_VERSION=$(echo $TF_VERSION | sed 's/v\([0-9]\+\.[0-9]\+\).*/\1/')
28         echo "Automatically determined version of TensorFlow as: $TF_VERSION"
29     fi
30 }
31
32 determine_layer_BUILD_file()
33 {
34     if [ $TF_VERSION_MAJOR -gt 1 ]; then
35         LAYER_BUILD_FILE=BUILD_1_4_higher
36         echo "TensorFlow major version is higher than 1, so use BUILD configuraion file as for 1.4."
37         echo "WARNING: this TensorFlow version has not been tested!"
38     else
39         if [ $TF_VERSION_MINOR -ge 4 ]; then
40             echo "TensorFlow minor version is higher than 3, so use BUILD configuraion file as for 1.4"
41             LAYER_BUILD_FILE=BUILD_1_4_higher
42         else
43             if [ $TF_VERSION_MINOR -ge 2 ]; then
44                 echo "TensorFlow minor version is between 2 and 3, so use BUILD configuraion file as for 1.2"
45                 LAYER_BUILD_FILE=BUILD_1_2_to_1_3
46             else
47                 echo "ERROR: TensorFlow version is not supported. Versions 1.2-1.5 are supported"
48                 exit 1
49             fi
50         fi
51     fi
52 }
53
54 THIS_DIR=`dirname "$0"`
55 if echo "$THIS_DIR" | grep -q -s ^/ || echo "$THIS_DIR" | grep -q -s ^~ ; then
56    THIS_ABSOLUTE_DIR="$THIS_DIR"
57 else
58    THIS_ABSOLUTE_DIR="`pwd`/$THIS_DIR"
59 fi
60
61 set -e # exit if something goes wrong
62 if [ "x$INTEL_OPENVINO_DIR" = "x" ]; then
63     echo "ERROR: INTEL_OPENVINO_DIR environment variable is not set"
64     echo "Please, run the 'source <OpenVINO_install_dir>/bin/setupvars.sh'"
65     exit 1
66 fi
67
68 if [ "x$TF_ROOT_DIR" == 'x' ]; then
69     echo "ERROR: TF_ROOT_DIR environment variable is not set"
70     echo "Please, set TF_ROOT_DIR environment variable which points to directory with cloned TF"
71     exit 1
72 fi
73
74 IE_HEADERS_SRC_DIR=$INTEL_OPENVINO_DIR/inference_engine/include
75 if [ ! -e $IE_HEADERS_SRC_DIR ]; then
76     echo "ERROR: Inference Engine headers files '$IE_HEADERS_SRC_DIR' doesn't exist"
77     exit 1
78 fi
79
80 IE_HEADERS_DEST_DIR=$TF_ROOT_DIR/third_party/inference_engine
81 if [ -e $IE_HEADERS_DEST_DIR ]; then
82     echo "Removing old version of IE headers files from '$IE_HEADERS_DEST_DIR'"
83     rm -rf $IE_HEADERS_DEST_DIR
84 fi
85
86 if [ "x$TF_VERSION" = "x" ]; then
87     get_tf_version $TF_ROOT_DIR
88 fi
89
90 TF_VERSION_MINOR=$(echo $TF_VERSION | awk -F. '{print $2}')
91 TF_VERSION_MAJOR=$(echo $TF_VERSION | awk -F. '{print $1}')
92 echo "TensorFlow major version: $TF_VERSION_MAJOR"
93 echo "TensorFlow minor version: $TF_VERSION_MINOR"
94
95 determine_layer_BUILD_file
96
97 mkdir -p $IE_HEADERS_DEST_DIR
98 cp -r ${IE_HEADERS_SRC_DIR} ${IE_HEADERS_DEST_DIR}
99 cp $THIS_ABSOLUTE_DIR/inference_engine_BUILD ${IE_HEADERS_DEST_DIR}/BUILD
100
101 IE_LAYER_SOURCES_DIR=$THIS_ABSOLUTE_DIR/layer_sources
102 IE_LAYER_SOURCES_DEST_DIR=$TF_ROOT_DIR/tensorflow/cc/inference_engine_layer
103
104 if [ -e $IE_LAYER_SOURCES_DEST_DIR ]; then
105     echo "Removing old version of IE plugin source files from '$IE_LAYER_SOURCES_DEST_DIR'"
106     rm -rf $IE_LAYER_SOURCES_DEST_DIR
107 fi
108 cp -r ${IE_LAYER_SOURCES_DIR} ${IE_LAYER_SOURCES_DEST_DIR}
109 cp $IE_LAYER_SOURCES_DEST_DIR/$LAYER_BUILD_FILE $IE_LAYER_SOURCES_DEST_DIR/BUILD
110
111 OLD_DIR=`pwd`
112 cd $TF_ROOT_DIR
113 # refer to https://github.com/allenlavoie/tensorflow/commit/4afd28316f467ac3aaf600162020637c91c0c2b7 for info about --config=monolithic
114 bazel build --config=monolithic //tensorflow/cc/inference_engine_layer:libtensorflow_call_layer.so
115 cd $OLD_DIR