blob: 20bbf123abcee2d68d3d6364101cafeac98dc047 [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
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -08005## Set up python-related environment settings
6while true; do
7 fromuser=""
8 if [ -z "$PYTHON_BIN_PATH" ]; then
9 default_python_bin_path=$(which python)
10 read -p "Please specify the location of python. [Default is $default_python_bin_path]: " PYTHON_BIN_PATH
11 fromuser="1"
12 if [ -z "$PYTHON_BIN_PATH" ]; then
13 PYTHON_BIN_PATH=$default_python_bin_path
14 fi
15 fi
16 if [ -e "$PYTHON_BIN_PATH" ]; then
17 break
18 fi
19 echo "Invalid python path. ${PYTHON_BIN_PATH} cannot be found" 1>&2
20 if [ -z "$fromuser" ]; then
21 exit 1
22 fi
23 PYTHON_BIN_PATH=""
24 # Retry
25done
26
A. Unique TensorFlowered65e692016-05-11 21:03:48 -080027while [ "$TF_NEED_GCP" == "" ]; do
28 read -p "Do you wish to build TensorFlow with "\
29"Google Cloud Platform support? [y/N] " INPUT
30 case $INPUT in
31 [Yy]* ) echo "Google Cloud Platform support will be enabled for "\
32"TensorFlow"; TF_NEED_GCP=1;;
33 [Nn]* ) echo "No Google Cloud Platform support will be enabled for "\
34"TensorFlow"; TF_NEED_GCP=0;;
35 "" ) echo "No Google Cloud Platform support will be enabled for "\
36"TensorFlow"; TF_NEED_GCP=0;;
37 * ) echo "Invalid selection: " $INPUT;;
38 esac
39done
40
41if [ "$TF_NEED_GCP" == "1" ]; then
42
43 ## Verify that libcurl header files are available.
44 # Only check Linux, since on MacOS the header files are installed with XCode.
45 if [[ $(uname -a) =~ Linux ]] && [[ ! -f "/usr/include/curl/curl.h" ]]; then
46 echo "ERROR: It appears that the development version of libcurl is not "\
47"available. Please install the libcurl3-dev package."
48 exit 1
49 fi
50
51 # Update Bazel build configuration.
52 perl -pi -e "s,WITH_GCP_SUPPORT = (False|True),WITH_GCP_SUPPORT = True,s" tensorflow/core/platform/default/build_config.bzl
53else
54 # Update Bazel build configuration.
55 perl -pi -e "s,WITH_GCP_SUPPORT = (False|True),WITH_GCP_SUPPORT = False,s" tensorflow/core/platform/default/build_config.bzl
56fi
57
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -080058## Find swig path
59if [ -z "$SWIG_PATH" ]; then
60 SWIG_PATH=`type -p swig 2> /dev/null`
61fi
62if [[ ! -e "$SWIG_PATH" ]]; then
63 echo "Can't find swig. Ensure swig is in \$PATH or set \$SWIG_PATH."
64 exit 1
65fi
66echo "$SWIG_PATH" > tensorflow/tools/swig/swig_path
67
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080068# Invoke python_config and set up symlinks to python includes
69(./util/python/python_config.sh --setup "$PYTHON_BIN_PATH";) || exit -1
70
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080071## Set up Cuda-related environment settings
72
73while [ "$TF_NEED_CUDA" == "" ]; do
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080074 read -p "Do you wish to build TensorFlow with GPU support? [y/N] " INPUT
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080075 case $INPUT in
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080076 [Yy]* ) echo "GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=1;;
77 [Nn]* ) echo "No GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=0;;
78 "" ) echo "No GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=0;;
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080079 * ) echo "Invalid selection: " $INPUT;;
80 esac
81done
82
83if [ "$TF_NEED_CUDA" == "0" ]; then
84 echo "Configuration finished"
85 exit
86fi
87
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -080088# Set up which gcc nvcc should use as the host compiler
89while true; do
90 fromuser=""
91 if [ -z "$GCC_HOST_COMPILER_PATH" ]; then
92 default_gcc_host_compiler_path=$(which gcc)
93 read -p "Please specify which gcc nvcc should use as the host compiler. [Default is $default_gcc_host_compiler_path]: " GCC_HOST_COMPILER_PATH
94 fromuser="1"
95 if [ -z "$GCC_HOST_COMPILER_PATH" ]; then
96 GCC_HOST_COMPILER_PATH=$default_gcc_host_compiler_path
97 fi
98 fi
99 if [ -e "$GCC_HOST_COMPILER_PATH" ]; then
100 break
101 fi
102 echo "Invalid gcc path. ${GCC_HOST_COMPILER_PATH} cannot be found" 1>&2
103 if [ -z "$fromuser" ]; then
104 exit 1
105 fi
106 GCC_HOST_COMPILER_PATH=""
107 # Retry
108done
109
110
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800111# Find out where the CUDA toolkit is installed
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800112OSNAME=`uname -s`
113
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800114while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800115 # Configure the Cuda SDK version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800116 if [ -z "$TF_CUDA_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800117 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 -0800118 fi
119
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800120 fromuser=""
121 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
122 default_cuda_path=/usr/local/cuda
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800123 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 -0800124 fromuser="1"
125 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
126 CUDA_TOOLKIT_PATH=$default_cuda_path
127 fi
128 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800129
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800130 if [[ -z "$TF_CUDA_VERSION" ]]; then
131 TF_CUDA_EXT=""
132 else
133 TF_CUDA_EXT=".$TF_CUDA_VERSION"
134 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800135
136 if [ "$OSNAME" == "Linux" ]; then
137 CUDA_RT_LIB_PATH="lib64/libcudart.so${TF_CUDA_EXT}"
138 elif [ "$OSNAME" == "Darwin" ]; then
139 CUDA_RT_LIB_PATH="lib/libcudart${TF_CUDA_EXT}.dylib"
140 fi
141
142 if [ -e "${CUDA_TOOLKIT_PATH}/${CUDA_RT_LIB_PATH}" ]; then
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800143 break
144 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800145 echo "Invalid path to CUDA $TF_CUDA_VERSION toolkit. ${CUDA_TOOLKIT_PATH}/${CUDA_RT_LIB_PATH} cannot be found"
146
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800147 if [ -z "$fromuser" ]; then
148 exit 1
149 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800150 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800151 TF_CUDA_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800152 CUDA_TOOLKIT_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800153done
154
Martin Wicke916776a2016-01-14 07:30:00 -0800155# Find out where the cuDNN library is installed
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800156while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800157 # Configure the Cudnn version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800158 if [ -z "$TF_CUDNN_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800159 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 -0800160 fi
161
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800162 fromuser=""
163 if [ -z "$CUDNN_INSTALL_PATH" ]; then
164 default_cudnn_path=${CUDA_TOOLKIT_PATH}
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800165 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 -0800166 fromuser="1"
167 if [ -z "$CUDNN_INSTALL_PATH" ]; then
168 CUDNN_INSTALL_PATH=$default_cudnn_path
169 fi
170 # Result returned from "read" will be used unexpanded. That make "~" unuseable.
171 # Going through one more level of expansion to handle that.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800172 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 -0800173 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800174
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800175 if [[ -z "$TF_CUDNN_VERSION" ]]; then
176 TF_CUDNN_EXT=""
A. Unique TensorFlower533d8912016-06-30 12:10:50 -0800177 # Resolve to the SONAME of the symlink. Use readlink without -f
178 # to resolve exactly once to the SONAME. E.g, libcudnn.so ->
179 # libcudnn.so.4
180 REALVAL=`readlink ${CUDNN_INSTALL_PATH}/lib64/libcudnn.so`
181
182 # Extract the version of the SONAME, if it was indeed symlinked to
183 # the SONAME version of the file.
184 if [[ "$REALVAL" =~ .so[.]+([0-9]*) ]];
185 then
186 TF_CUDNN_EXT="."${BASH_REMATCH[1]}
187 TF_CUDNN_VERSION=${BASH_REMATCH[1]}
188 echo "libcudnn.so resolves to libcudnn${TF_CUDNN_EXT}"
189 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800190 else
191 TF_CUDNN_EXT=".$TF_CUDNN_VERSION"
192 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800193
194 if [ "$OSNAME" == "Linux" ]; then
195 CUDA_DNN_LIB_PATH="lib64/libcudnn.so${TF_CUDNN_EXT}"
196 CUDA_DNN_LIB_ALT_PATH="libcudnn.so${TF_CUDNN_EXT}"
197 elif [ "$OSNAME" == "Darwin" ]; then
198 CUDA_DNN_LIB_PATH="lib/libcudnn${TF_CUDNN_EXT}.dylib"
199 CUDA_DNN_LIB_ALT_PATH="libcudnn${TF_CUDNN_EXT}.dylib"
200 fi
201
202 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 -0800203 break
204 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800205
206 if [ "$OSNAME" == "Linux" ]; then
207 CUDNN_PATH_FROM_LDCONFIG="$(ldconfig -p | sed -n 's/.*libcudnn.so .* => \(.*\)/\1/p')"
208 if [ -e "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}" ]; then
209 CUDNN_INSTALL_PATH="$(dirname ${CUDNN_PATH_FROM_LDCONFIG})"
210 break
211 fi
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800212 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800213 echo "Invalid path to cuDNN ${CUDNN_VERSION} toolkit. Neither of the following two files can be found:"
214 echo "${CUDNN_INSTALL_PATH}/${CUDA_DNN_LIB_PATH}"
215 echo "${CUDNN_INSTALL_PATH}/${CUDA_DNN_LIB_ALT_PATH}"
216 if [ "$OSNAME" == "Linux" ]; then
217 echo "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}"
218 fi
219
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800220 if [ -z "$fromuser" ]; then
221 exit 1
222 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800223 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800224 TF_CUDNN_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800225 CUDNN_INSTALL_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800226done
227
228cat > third_party/gpus/cuda/cuda.config <<EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800229# CUDA_TOOLKIT_PATH refers to the CUDA toolkit.
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800230CUDA_TOOLKIT_PATH="$CUDA_TOOLKIT_PATH"
Martin Wicke916776a2016-01-14 07:30:00 -0800231# CUDNN_INSTALL_PATH refers to the cuDNN toolkit. The cuDNN header and library
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800232# files can be either in this directory, or under include/ and lib64/
233# directories separately.
234CUDNN_INSTALL_PATH="$CUDNN_INSTALL_PATH"
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800235
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800236# 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 -0800237TF_CUDA_VERSION=$TF_CUDA_VERSION
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800238
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800239# The Cudnn version that should be used in this build
240TF_CUDNN_VERSION=$TF_CUDNN_VERSION
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800241EOF
242
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800243# Configure the gcc host compiler to use
244export WARNING=$DO_NOT_SUBMIT_WARNING
245perl -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
246perl -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
247
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800248# Configure the platform name.
249perl -pi -e "s,PLATFORM = \".*\",PLATFORM = \"$OSNAME\",s" third_party/gpus/cuda/platform.bzl
250
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800251# Configure the Cuda toolkit version to work with.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800252perl -pi -e "s,(GetCudaVersion.*return )\"[0-9\.]*\",\1\"$TF_CUDA_VERSION\",s" tensorflow/stream_executor/dso_loader.cc
253perl -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 -0800254
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800255# Configure the Cudnn version to work with.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800256perl -pi -e "s,(GetCudnnVersion.*return )\"[0-9\.]*\",\1\"$TF_CUDNN_VERSION\",s" tensorflow/stream_executor/dso_loader.cc
257perl -pi -e "s,CUDNN_VERSION = \"[0-9\.]*\",CUDNN_VERSION = \"$TF_CUDNN_VERSION\",s" third_party/gpus/cuda/platform.bzl
258
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800259
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800260# Configure the compute capabilities that TensorFlow builds for.
261# Since Cuda toolkit is not backward-compatible, this is not guaranteed to work.
262while true; do
263 fromuser=""
264 if [ -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800265cat << EOF
266Please specify a list of comma-separated Cuda compute capabilities you want to build with.
267You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
268Please note that each additional compute capability significantly increases your build time and binary size.
269EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800270 read -p "[Default is: \"3.5,5.2\"]: " TF_CUDA_COMPUTE_CAPABILITIES
271 fromuser=1
272 fi
273 # Check whether all capabilities from the input is valid
274 COMPUTE_CAPABILITIES=${TF_CUDA_COMPUTE_CAPABILITIES//,/ }
275 ALL_VALID=1
276 for CAPABILITY in $COMPUTE_CAPABILITIES; do
277 if [[ ! "$CAPABILITY" =~ [0-9]+.[0-9]+ ]]; then
278 echo "Invalid compute capability: " $CAPABILITY
279 ALL_VALID=0
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800280 break
281 fi
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800282 done
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800283 if [ "$ALL_VALID" == "0" ]; then
284 if [ -z "$fromuser" ]; then
285 exit 1
286 fi
287 else
288 break
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800289 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800290 TF_CUDA_COMPUTE_CAPABILITIES=""
291done
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800292
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800293if [ ! -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800294 export WARNING=$DO_NOT_SUBMIT_WARNING
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800295 function CudaGenCodeOpts() {
296 OUTPUT=""
297 for CAPABILITY in $@; do
298 OUTPUT=${OUTPUT}" \"${CAPABILITY}\", "
299 done
300 echo $OUTPUT
301 }
302 export CUDA_GEN_CODES_OPTS=$(CudaGenCodeOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
303 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
304 function CudaVersionOpts() {
305 OUTPUT=""
306 for CAPABILITY in $@; do
307 OUTPUT=$OUTPUT"CudaVersion(\"${CAPABILITY}\"), "
308 done
309 echo $OUTPUT
310 }
311 export CUDA_VERSION_OPTS=$(CudaVersionOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
312 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 -0800313fi
314
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800315# Invoke the cuda_config.sh and set up the TensorFlow's canonical view of the Cuda libraries
316(cd third_party/gpus/cuda; ./cuda_config.sh;) || exit -1
317
318echo "Configuration finished"