blob: 5d6397da57de273e9576d97702deb498a8e8ba5c [file] [log] [blame]
Geoffrey Irving4c85a082016-03-16 12:20:34 -08001#!/usr/bin/env bash
Manjunath Kudlurf41959c2015-11-06 16:27:58 -08002
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -08003DO_NOT_SUBMIT_WARNING="Unofficial setting. DO NOT SUBMIT!!!"
4
Shanqing Caide6d48c2016-04-28 10:34:56 -08005## Verify that the submodule google/protobuf is available
6# TODO(cais): Remove this check once protobuf is no longer depended upon
7if [[ ! -f "google/protobuf/protobuf.bzl" ]]; then
8 echo "ERROR: It appears that the required submodule google/protobuf is not "\
9"available in this TensorFlow git clone."
10 echo "Please be sure to use the --recurse-submodules flag when performing "\
11"git clone of TensorFlow."
12
13 exit 1
14fi
15
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080016## Set up python-related environment settings
17while true; do
18 fromuser=""
19 if [ -z "$PYTHON_BIN_PATH" ]; then
20 default_python_bin_path=$(which python)
21 read -p "Please specify the location of python. [Default is $default_python_bin_path]: " PYTHON_BIN_PATH
22 fromuser="1"
23 if [ -z "$PYTHON_BIN_PATH" ]; then
24 PYTHON_BIN_PATH=$default_python_bin_path
25 fi
26 fi
27 if [ -e "$PYTHON_BIN_PATH" ]; then
28 break
29 fi
30 echo "Invalid python path. ${PYTHON_BIN_PATH} cannot be found" 1>&2
31 if [ -z "$fromuser" ]; then
32 exit 1
33 fi
34 PYTHON_BIN_PATH=""
35 # Retry
36done
37
A. Unique TensorFlowered65e692016-05-11 21:03:48 -080038while [ "$TF_NEED_GCP" == "" ]; do
39 read -p "Do you wish to build TensorFlow with "\
40"Google Cloud Platform support? [y/N] " INPUT
41 case $INPUT in
42 [Yy]* ) echo "Google Cloud Platform support will be enabled for "\
43"TensorFlow"; TF_NEED_GCP=1;;
44 [Nn]* ) echo "No Google Cloud Platform support will be enabled for "\
45"TensorFlow"; TF_NEED_GCP=0;;
46 "" ) echo "No Google Cloud Platform support will be enabled for "\
47"TensorFlow"; TF_NEED_GCP=0;;
48 * ) echo "Invalid selection: " $INPUT;;
49 esac
50done
51
52if [ "$TF_NEED_GCP" == "1" ]; then
53
54 ## Verify that libcurl header files are available.
55 # Only check Linux, since on MacOS the header files are installed with XCode.
56 if [[ $(uname -a) =~ Linux ]] && [[ ! -f "/usr/include/curl/curl.h" ]]; then
57 echo "ERROR: It appears that the development version of libcurl is not "\
58"available. Please install the libcurl3-dev package."
59 exit 1
60 fi
61
62 # Update Bazel build configuration.
63 perl -pi -e "s,WITH_GCP_SUPPORT = (False|True),WITH_GCP_SUPPORT = True,s" tensorflow/core/platform/default/build_config.bzl
64else
65 # Update Bazel build configuration.
66 perl -pi -e "s,WITH_GCP_SUPPORT = (False|True),WITH_GCP_SUPPORT = False,s" tensorflow/core/platform/default/build_config.bzl
67fi
68
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -080069## Find swig path
70if [ -z "$SWIG_PATH" ]; then
71 SWIG_PATH=`type -p swig 2> /dev/null`
72fi
73if [[ ! -e "$SWIG_PATH" ]]; then
74 echo "Can't find swig. Ensure swig is in \$PATH or set \$SWIG_PATH."
75 exit 1
76fi
77echo "$SWIG_PATH" > tensorflow/tools/swig/swig_path
78
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080079# Invoke python_config and set up symlinks to python includes
80(./util/python/python_config.sh --setup "$PYTHON_BIN_PATH";) || exit -1
81
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080082## Set up Cuda-related environment settings
83
84while [ "$TF_NEED_CUDA" == "" ]; do
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080085 read -p "Do you wish to build TensorFlow with GPU support? [y/N] " INPUT
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080086 case $INPUT in
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080087 [Yy]* ) echo "GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=1;;
88 [Nn]* ) echo "No GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=0;;
89 "" ) echo "No GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=0;;
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080090 * ) echo "Invalid selection: " $INPUT;;
91 esac
92done
93
94if [ "$TF_NEED_CUDA" == "0" ]; then
95 echo "Configuration finished"
96 exit
97fi
98
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -080099# Set up which gcc nvcc should use as the host compiler
100while true; do
101 fromuser=""
102 if [ -z "$GCC_HOST_COMPILER_PATH" ]; then
103 default_gcc_host_compiler_path=$(which gcc)
104 read -p "Please specify which gcc nvcc should use as the host compiler. [Default is $default_gcc_host_compiler_path]: " GCC_HOST_COMPILER_PATH
105 fromuser="1"
106 if [ -z "$GCC_HOST_COMPILER_PATH" ]; then
107 GCC_HOST_COMPILER_PATH=$default_gcc_host_compiler_path
108 fi
109 fi
110 if [ -e "$GCC_HOST_COMPILER_PATH" ]; then
111 break
112 fi
113 echo "Invalid gcc path. ${GCC_HOST_COMPILER_PATH} cannot be found" 1>&2
114 if [ -z "$fromuser" ]; then
115 exit 1
116 fi
117 GCC_HOST_COMPILER_PATH=""
118 # Retry
119done
120
121
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800122# Find out where the CUDA toolkit is installed
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800123OSNAME=`uname -s`
124
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800125while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800126 # Configure the Cuda SDK version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800127 if [ -z "$TF_CUDA_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800128 read -p "Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: " TF_CUDA_VERSION
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800129 fi
130
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800131 fromuser=""
132 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
133 default_cuda_path=/usr/local/cuda
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800134 read -p "Please specify the location where CUDA $TF_CUDA_VERSION toolkit is installed. Refer to README.md for more details. [Default is $default_cuda_path]: " CUDA_TOOLKIT_PATH
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800135 fromuser="1"
136 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
137 CUDA_TOOLKIT_PATH=$default_cuda_path
138 fi
139 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800140
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800141 if [[ -z "$TF_CUDA_VERSION" ]]; then
142 TF_CUDA_EXT=""
143 else
144 TF_CUDA_EXT=".$TF_CUDA_VERSION"
145 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800146
147 if [ "$OSNAME" == "Linux" ]; then
148 CUDA_RT_LIB_PATH="lib64/libcudart.so${TF_CUDA_EXT}"
149 elif [ "$OSNAME" == "Darwin" ]; then
150 CUDA_RT_LIB_PATH="lib/libcudart${TF_CUDA_EXT}.dylib"
151 fi
152
153 if [ -e "${CUDA_TOOLKIT_PATH}/${CUDA_RT_LIB_PATH}" ]; then
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800154 break
155 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800156 echo "Invalid path to CUDA $TF_CUDA_VERSION toolkit. ${CUDA_TOOLKIT_PATH}/${CUDA_RT_LIB_PATH} cannot be found"
157
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800158 if [ -z "$fromuser" ]; then
159 exit 1
160 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800161 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800162 TF_CUDA_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800163 CUDA_TOOLKIT_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800164done
165
Martin Wicke916776a2016-01-14 07:30:00 -0800166# Find out where the cuDNN library is installed
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800167while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800168 # Configure the Cudnn version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800169 if [ -z "$TF_CUDNN_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800170 read -p "Please specify the Cudnn version you want to use. [Leave empty to use system default]: " TF_CUDNN_VERSION
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800171 fi
172
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800173 fromuser=""
174 if [ -z "$CUDNN_INSTALL_PATH" ]; then
175 default_cudnn_path=${CUDA_TOOLKIT_PATH}
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800176 read -p "Please specify the location where cuDNN $TF_CUDNN_VERSION library is installed. Refer to README.md for more details. [Default is $default_cudnn_path]: " CUDNN_INSTALL_PATH
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800177 fromuser="1"
178 if [ -z "$CUDNN_INSTALL_PATH" ]; then
179 CUDNN_INSTALL_PATH=$default_cudnn_path
180 fi
181 # Result returned from "read" will be used unexpanded. That make "~" unuseable.
182 # Going through one more level of expansion to handle that.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800183 CUDNN_INSTALL_PATH=`${PYTHON_BIN_PATH} -c "import os; print(os.path.realpath(os.path.expanduser('${CUDNN_INSTALL_PATH}')))"`
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800184 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800185
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800186 if [[ -z "$TF_CUDNN_VERSION" ]]; then
187 TF_CUDNN_EXT=""
188 else
189 TF_CUDNN_EXT=".$TF_CUDNN_VERSION"
190 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800191
192 if [ "$OSNAME" == "Linux" ]; then
193 CUDA_DNN_LIB_PATH="lib64/libcudnn.so${TF_CUDNN_EXT}"
194 CUDA_DNN_LIB_ALT_PATH="libcudnn.so${TF_CUDNN_EXT}"
195 elif [ "$OSNAME" == "Darwin" ]; then
196 CUDA_DNN_LIB_PATH="lib/libcudnn${TF_CUDNN_EXT}.dylib"
197 CUDA_DNN_LIB_ALT_PATH="libcudnn${TF_CUDNN_EXT}.dylib"
198 fi
199
200 if [ -e "$CUDNN_INSTALL_PATH/${CUDA_DNN_LIB_ALT_PATH}" -o -e "$CUDNN_INSTALL_PATH/${CUDA_DNN_LIB_PATH}" ]; then
Eugene Brevdo56f1d642016-03-10 17:18:30 -0800201 break
202 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800203
204 if [ "$OSNAME" == "Linux" ]; then
205 CUDNN_PATH_FROM_LDCONFIG="$(ldconfig -p | sed -n 's/.*libcudnn.so .* => \(.*\)/\1/p')"
206 if [ -e "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}" ]; then
207 CUDNN_INSTALL_PATH="$(dirname ${CUDNN_PATH_FROM_LDCONFIG})"
208 break
209 fi
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800210 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800211 echo "Invalid path to cuDNN ${CUDNN_VERSION} toolkit. Neither of the following two files can be found:"
212 echo "${CUDNN_INSTALL_PATH}/${CUDA_DNN_LIB_PATH}"
213 echo "${CUDNN_INSTALL_PATH}/${CUDA_DNN_LIB_ALT_PATH}"
214 if [ "$OSNAME" == "Linux" ]; then
215 echo "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}"
216 fi
217
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800218 if [ -z "$fromuser" ]; then
219 exit 1
220 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800221 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800222 TF_CUDNN_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800223 CUDNN_INSTALL_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800224done
225
226cat > third_party/gpus/cuda/cuda.config <<EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800227# CUDA_TOOLKIT_PATH refers to the CUDA toolkit.
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800228CUDA_TOOLKIT_PATH="$CUDA_TOOLKIT_PATH"
Martin Wicke916776a2016-01-14 07:30:00 -0800229# CUDNN_INSTALL_PATH refers to the cuDNN toolkit. The cuDNN header and library
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800230# files can be either in this directory, or under include/ and lib64/
231# directories separately.
232CUDNN_INSTALL_PATH="$CUDNN_INSTALL_PATH"
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800233
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800234# The Cuda SDK version that should be used in this build (empty to use libcudart.so symlink)
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800235TF_CUDA_VERSION=$TF_CUDA_VERSION
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800236
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800237# The Cudnn version that should be used in this build
238TF_CUDNN_VERSION=$TF_CUDNN_VERSION
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800239EOF
240
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800241# Configure the gcc host compiler to use
242export WARNING=$DO_NOT_SUBMIT_WARNING
243perl -pi -e "s,CPU_COMPILER = \('.*'\),# \$ENV{WARNING}\nCPU_COMPILER = ('$GCC_HOST_COMPILER_PATH'),s" third_party/gpus/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc
244perl -pi -e "s,GCC_HOST_COMPILER_PATH = \('.*'\),# \$ENV{WARNING}\nGCC_HOST_COMPILER_PATH = ('$GCC_HOST_COMPILER_PATH'),s" third_party/gpus/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc
245
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800246# Configure the platform name.
247perl -pi -e "s,PLATFORM = \".*\",PLATFORM = \"$OSNAME\",s" third_party/gpus/cuda/platform.bzl
248
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800249# Configure the Cuda toolkit version to work with.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800250perl -pi -e "s,(GetCudaVersion.*return )\"[0-9\.]*\",\1\"$TF_CUDA_VERSION\",s" tensorflow/stream_executor/dso_loader.cc
251perl -pi -e "s,CUDA_VERSION = \"[0-9\.]*\",CUDA_VERSION = \"$TF_CUDA_VERSION\",s" third_party/gpus/cuda/platform.bzl
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800252
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800253# Configure the Cudnn version to work with.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800254perl -pi -e "s,(GetCudnnVersion.*return )\"[0-9\.]*\",\1\"$TF_CUDNN_VERSION\",s" tensorflow/stream_executor/dso_loader.cc
255perl -pi -e "s,CUDNN_VERSION = \"[0-9\.]*\",CUDNN_VERSION = \"$TF_CUDNN_VERSION\",s" third_party/gpus/cuda/platform.bzl
256
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800257
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800258# Configure the compute capabilities that TensorFlow builds for.
259# Since Cuda toolkit is not backward-compatible, this is not guaranteed to work.
260while true; do
261 fromuser=""
262 if [ -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800263cat << EOF
264Please specify a list of comma-separated Cuda compute capabilities you want to build with.
265You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
266Please note that each additional compute capability significantly increases your build time and binary size.
267EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800268 read -p "[Default is: \"3.5,5.2\"]: " TF_CUDA_COMPUTE_CAPABILITIES
269 fromuser=1
270 fi
271 # Check whether all capabilities from the input is valid
272 COMPUTE_CAPABILITIES=${TF_CUDA_COMPUTE_CAPABILITIES//,/ }
273 ALL_VALID=1
274 for CAPABILITY in $COMPUTE_CAPABILITIES; do
275 if [[ ! "$CAPABILITY" =~ [0-9]+.[0-9]+ ]]; then
276 echo "Invalid compute capability: " $CAPABILITY
277 ALL_VALID=0
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800278 break
279 fi
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800280 done
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800281 if [ "$ALL_VALID" == "0" ]; then
282 if [ -z "$fromuser" ]; then
283 exit 1
284 fi
285 else
286 break
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800287 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800288 TF_CUDA_COMPUTE_CAPABILITIES=""
289done
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800290
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800291if [ ! -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800292 export WARNING=$DO_NOT_SUBMIT_WARNING
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800293 function CudaGenCodeOpts() {
294 OUTPUT=""
295 for CAPABILITY in $@; do
296 OUTPUT=${OUTPUT}" \"${CAPABILITY}\", "
297 done
298 echo $OUTPUT
299 }
300 export CUDA_GEN_CODES_OPTS=$(CudaGenCodeOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
301 perl -pi -0 -e 's,\n( *)([^\n]*supported_cuda_compute_capabilities\s*=\s*\[).*?(\]),\n\1# $ENV{WARNING}\n\1\2$ENV{CUDA_GEN_CODES_OPTS}\3,s' third_party/gpus/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc
302 function CudaVersionOpts() {
303 OUTPUT=""
304 for CAPABILITY in $@; do
305 OUTPUT=$OUTPUT"CudaVersion(\"${CAPABILITY}\"), "
306 done
307 echo $OUTPUT
308 }
309 export CUDA_VERSION_OPTS=$(CudaVersionOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
310 perl -pi -0 -e 's,\n( *)([^\n]*supported_cuda_compute_capabilities\s*=\s*\{).*?(\}),\n\1// $ENV{WARNING}\n\1\2$ENV{CUDA_VERSION_OPTS}\3,s' tensorflow/core/common_runtime/gpu/gpu_device.cc
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800311fi
312
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800313# Invoke the cuda_config.sh and set up the TensorFlow's canonical view of the Cuda libraries
314(cd third_party/gpus/cuda; ./cuda_config.sh;) || exit -1
315
316echo "Configuration finished"