blob: 98048ba91db4801d6be31239edf53ecabe67fe95 [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=""
177 else
178 TF_CUDNN_EXT=".$TF_CUDNN_VERSION"
179 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800180
181 if [ "$OSNAME" == "Linux" ]; then
182 CUDA_DNN_LIB_PATH="lib64/libcudnn.so${TF_CUDNN_EXT}"
183 CUDA_DNN_LIB_ALT_PATH="libcudnn.so${TF_CUDNN_EXT}"
184 elif [ "$OSNAME" == "Darwin" ]; then
185 CUDA_DNN_LIB_PATH="lib/libcudnn${TF_CUDNN_EXT}.dylib"
186 CUDA_DNN_LIB_ALT_PATH="libcudnn${TF_CUDNN_EXT}.dylib"
187 fi
188
189 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 -0800190 break
191 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800192
193 if [ "$OSNAME" == "Linux" ]; then
194 CUDNN_PATH_FROM_LDCONFIG="$(ldconfig -p | sed -n 's/.*libcudnn.so .* => \(.*\)/\1/p')"
195 if [ -e "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}" ]; then
196 CUDNN_INSTALL_PATH="$(dirname ${CUDNN_PATH_FROM_LDCONFIG})"
197 break
198 fi
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800199 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800200 echo "Invalid path to cuDNN ${CUDNN_VERSION} toolkit. Neither of the following two files can be found:"
201 echo "${CUDNN_INSTALL_PATH}/${CUDA_DNN_LIB_PATH}"
202 echo "${CUDNN_INSTALL_PATH}/${CUDA_DNN_LIB_ALT_PATH}"
203 if [ "$OSNAME" == "Linux" ]; then
204 echo "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}"
205 fi
206
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800207 if [ -z "$fromuser" ]; then
208 exit 1
209 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800210 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800211 TF_CUDNN_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800212 CUDNN_INSTALL_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800213done
214
215cat > third_party/gpus/cuda/cuda.config <<EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800216# CUDA_TOOLKIT_PATH refers to the CUDA toolkit.
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800217CUDA_TOOLKIT_PATH="$CUDA_TOOLKIT_PATH"
Martin Wicke916776a2016-01-14 07:30:00 -0800218# CUDNN_INSTALL_PATH refers to the cuDNN toolkit. The cuDNN header and library
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800219# files can be either in this directory, or under include/ and lib64/
220# directories separately.
221CUDNN_INSTALL_PATH="$CUDNN_INSTALL_PATH"
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800222
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800223# 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 -0800224TF_CUDA_VERSION=$TF_CUDA_VERSION
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800225
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800226# The Cudnn version that should be used in this build
227TF_CUDNN_VERSION=$TF_CUDNN_VERSION
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800228EOF
229
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800230# Configure the gcc host compiler to use
231export WARNING=$DO_NOT_SUBMIT_WARNING
232perl -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
233perl -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
234
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800235# Configure the platform name.
236perl -pi -e "s,PLATFORM = \".*\",PLATFORM = \"$OSNAME\",s" third_party/gpus/cuda/platform.bzl
237
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800238# Configure the Cuda toolkit version to work with.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800239perl -pi -e "s,(GetCudaVersion.*return )\"[0-9\.]*\",\1\"$TF_CUDA_VERSION\",s" tensorflow/stream_executor/dso_loader.cc
240perl -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 -0800241
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800242# Configure the Cudnn version to work with.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800243perl -pi -e "s,(GetCudnnVersion.*return )\"[0-9\.]*\",\1\"$TF_CUDNN_VERSION\",s" tensorflow/stream_executor/dso_loader.cc
244perl -pi -e "s,CUDNN_VERSION = \"[0-9\.]*\",CUDNN_VERSION = \"$TF_CUDNN_VERSION\",s" third_party/gpus/cuda/platform.bzl
245
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800246
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800247# Configure the compute capabilities that TensorFlow builds for.
248# Since Cuda toolkit is not backward-compatible, this is not guaranteed to work.
249while true; do
250 fromuser=""
251 if [ -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800252cat << EOF
253Please specify a list of comma-separated Cuda compute capabilities you want to build with.
254You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
255Please note that each additional compute capability significantly increases your build time and binary size.
256EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800257 read -p "[Default is: \"3.5,5.2\"]: " TF_CUDA_COMPUTE_CAPABILITIES
258 fromuser=1
259 fi
260 # Check whether all capabilities from the input is valid
261 COMPUTE_CAPABILITIES=${TF_CUDA_COMPUTE_CAPABILITIES//,/ }
262 ALL_VALID=1
263 for CAPABILITY in $COMPUTE_CAPABILITIES; do
264 if [[ ! "$CAPABILITY" =~ [0-9]+.[0-9]+ ]]; then
265 echo "Invalid compute capability: " $CAPABILITY
266 ALL_VALID=0
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800267 break
268 fi
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800269 done
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800270 if [ "$ALL_VALID" == "0" ]; then
271 if [ -z "$fromuser" ]; then
272 exit 1
273 fi
274 else
275 break
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800276 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800277 TF_CUDA_COMPUTE_CAPABILITIES=""
278done
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800279
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800280if [ ! -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800281 export WARNING=$DO_NOT_SUBMIT_WARNING
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800282 function CudaGenCodeOpts() {
283 OUTPUT=""
284 for CAPABILITY in $@; do
285 OUTPUT=${OUTPUT}" \"${CAPABILITY}\", "
286 done
287 echo $OUTPUT
288 }
289 export CUDA_GEN_CODES_OPTS=$(CudaGenCodeOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
290 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
291 function CudaVersionOpts() {
292 OUTPUT=""
293 for CAPABILITY in $@; do
294 OUTPUT=$OUTPUT"CudaVersion(\"${CAPABILITY}\"), "
295 done
296 echo $OUTPUT
297 }
298 export CUDA_VERSION_OPTS=$(CudaVersionOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
299 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 -0800300fi
301
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800302# Invoke the cuda_config.sh and set up the TensorFlow's canonical view of the Cuda libraries
303(cd third_party/gpus/cuda; ./cuda_config.sh;) || exit -1
304
305echo "Configuration finished"