blob: 50f6398f47ba3bc11e33d5582489bd5edb4bb114 [file] [log] [blame]
Geoffrey Irving4c85a082016-03-16 12:20:34 -08001#!/usr/bin/env bash
A. Unique TensorFlower122cdce2016-06-02 12:32:54 -08002# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Vijay Vasudevana4806a32015-12-03 10:26:25 -08003#
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# ==============================================================================
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080016
17set -e -o errexit
18
Vijay Vasudevan8cc567b2016-05-26 11:05:13 -080019if [ -d "../org_tensorflow" ]; then
20 script_path="../org_tensorflow"
21else
22 # Prefix expected paths with ./ locally and external/reponame/ for remote repos.
23 # TODO(kchodorow): remove once runfiles paths are fixed, see
24 # https://github.com/bazelbuild/bazel/issues/848.
25 script_path=$(dirname $(dirname $(dirname "$0")))
26 script_path=${script_path:-.}
27fi
28
Martin Wicke59f1eba2016-02-07 12:34:25 -080029EXPECTED_PATHS="$script_path/util/python/python_include"\
30" $script_path/util/python/python_lib"\
31" $script_path/third_party/py/numpy/numpy_include"
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080032
33function main {
34 argument="$1"
35 shift
36 case $argument in
37 --check)
38 check_python
39 exit 0
40 ;;
41 --setup)
42 setup_python "$1"
43 exit 0
44 ;;
45 esac
46}
47
Olivia Nordquist4959a122016-08-09 11:16:44 -080048function python_path {
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -080049 "$PYTHON_BIN_PATH" - <<END
Olivia Nordquist4959a122016-08-09 11:16:44 -080050from __future__ import print_function
51import site
52import os
53
54try:
55 input = raw_input
56except NameError:
57 pass
58
59python_paths = []
60if os.getenv('PYTHONPATH') is not None:
61 python_paths = os.getenv('PYTHONPATH').split(':')
Olivia Nordquist308e34f2016-08-23 09:27:27 -080062try:
Dan Mané54a71782016-09-09 16:07:46 -080063 library_paths = site.getsitepackages()
Olivia Nordquist308e34f2016-08-23 09:27:27 -080064except AttributeError:
65 from distutils.sysconfig import get_python_lib
Dan Mané54a71782016-09-09 16:07:46 -080066 library_paths = [get_python_lib()]
Olivia Nordquist308e34f2016-08-23 09:27:27 -080067all_paths = set(python_paths + library_paths)
Olivia Nordquist4959a122016-08-09 11:16:44 -080068
69paths = []
70for path in all_paths:
71 if os.path.isdir(path):
72 paths.append(path)
73
74if len(paths) == 1:
75 print(paths[0])
Benoit Steiner8b3d8c82016-08-16 14:01:13 -080076else:
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -080077 ret_paths = ",".join(paths)
Benoit Steiner8b3d8c82016-08-16 14:01:13 -080078 print(ret_paths)
Olivia Nordquist4959a122016-08-09 11:16:44 -080079END
80}
81
82function default_python_path {
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -080083 PYTHON_ARG="$1" "$PYTHON_BIN_PATH" - <<END
Olivia Nordquist4959a122016-08-09 11:16:44 -080084from __future__ import print_function
85import os
86
87default = os.getenv('PYTHON_ARG')
88default = str(default)
89print(default)
90END
91}
92
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080093function setup_python {
94 PYTHON_BIN_PATH="$1";
95
96 if [ -z "$PYTHON_BIN_PATH" ]; then
97 echo "PYTHON_BIN_PATH was not provided. Did you run configure?"
98 exit 1
99 fi
100 if [ ! -x "$PYTHON_BIN_PATH" ] || [ -d "$PYTHON_BIN_PATH" ]; then
101 echo "PYTHON_BIN_PATH is not executable. Is it the python binary?"
102 exit 1
103 fi
104
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -0800105 local python_major_version=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import sys; print(sys.version_info[0]);')
106 if [ "$python_major_version" == "" ]; then
107 echo -e "\n\nERROR: Problem getting python version. Is $PYTHON_BIN_PATH the correct python binary?"
108 exit 1
109 fi
110
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800111 local python_include="$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; from distutils import sysconfig; print(sysconfig.get_python_inc());')"
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800112 if [ "$python_include" == "" ]; then
113 echo -e "\n\nERROR: Problem getting python include path. Is distutils installed?"
114 exit 1
115 fi
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800116 local python_lib_path
117 # Split python_path into an array of paths, this allows path containing spaces
118 IFS=','
119 python_lib_path=($(python_path))
120 unset IFS
Olivia Nordquist4959a122016-08-09 11:16:44 -0800121 echo "Found possible Python library paths:"
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800122 for x in "${python_lib_path[@]}"; do
Olivia Nordquist4959a122016-08-09 11:16:44 -0800123 echo " $x"
124 done
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800125 set -- "${python_lib_path[@]}"
Olivia Nordquist4959a122016-08-09 11:16:44 -0800126 echo "Please input the desired Python library path to use. Default is ["$1"]"
A. Unique TensorFlower5c29a242016-08-12 06:21:36 -0800127 read b || true
Olivia Nordquist4959a122016-08-09 11:16:44 -0800128 if [ "$b" == "" ]; then
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800129 python_lib="$(default_python_path "${python_lib_path[0]}")"
Olivia Nordquist4959a122016-08-09 11:16:44 -0800130 echo $python_lib
131 else
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800132 if test -d "$b" -a -x "$b"; then
133 python_lib="$b"
Olivia Nordquist4959a122016-08-09 11:16:44 -0800134 else
135 echo -e "\n\nERROR: The path you have entered does not exist."
136 exit 1
137 fi
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800138 fi
Olivia Nordquist4959a122016-08-09 11:16:44 -0800139
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800140 local numpy_include=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import numpy; print(numpy.get_include());')
141 if [ "$numpy_include" == "" ]; then
142 echo -e "\n\nERROR: Problem getting numpy include path. Is numpy installed?"
143 exit 1
144 fi
145
146 for x in $EXPECTED_PATHS; do
147 if [ -e "$x" ]; then
Martin Wicke999b7942016-09-21 13:16:48 -0800148 rm -rf "$x"
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800149 fi
150 done
151
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800152# ln -sf is actually implemented as copying in msys since creating symbolic
153# links is privileged on Windows. But copying is too slow, so invoke mklink
154# to create junctions on Windows.
155 if is_windows; then
156 cmd /c "mklink /J util\\python\\python_include \"${python_include}\""
157 cmd /c "mklink /J util\\python\\python_lib \"${python_lib}\""
158 cmd /c "mklink /J third_party\\py\\numpy\\numpy_include \"${numpy_include}\""
159 else
160 ln -sf "${python_include}" util/python/python_include
161 ln -sf "${python_lib}" util/python/python_lib
162 ln -sf "${numpy_include}" third_party/py/numpy/numpy_include
163 fi
164 # Convert python path to Windows style before writing into bazel.rc
165 if is_windows; then
166 PYTHON_BIN_PATH="$(cygpath -m "$PYTHON_BIN_PATH")"
167 fi
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -0800168
169 # Write tools/bazel.rc
170 echo "# Autogenerated by configure: DO NOT EDIT" > tools/bazel.rc
171 sed -e "s/\$PYTHON_MAJOR_VERSION/$python_major_version/g" \
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800172 -e "s[\$PYTHON_BINARY[\"$PYTHON_BIN_PATH\"[g" \
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -0800173 tools/bazel.rc.template >> tools/bazel.rc
Vijay Vasudevan019c6792015-12-10 10:15:38 -0800174 # Write tools/python_bin_path.sh
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800175 echo "export PYTHON_BIN_PATH=\"$PYTHON_BIN_PATH\"" > tools/python_bin_path.sh
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800176}
177
Martin Wicke999b7942016-09-21 13:16:48 -0800178PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
179function is_windows() {
180 # On windows, the shell script is actually running in msys
181 if [[ "${PLATFORM}" =~ msys_nt* ]]; then
182 true
183 else
184 false
185 fi
186}
187
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800188function check_python {
189 for x in $EXPECTED_PATHS; do
190 if [ ! -e "$x" ]; then
191 echo -e "\n\nERROR: Cannot find '${x}'. Did you run configure?\n\n" 1>&2
192 exit 1
193 fi
Martin Wicke999b7942016-09-21 13:16:48 -0800194 # Don't check symbolic link on Windows
195 if ! is_windows && [ ! -L "${x}" ]; then
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800196 echo -e "\n\nERROR: '${x}' is not a symbolic link. Internal error.\n\n" 1>&2
197 exit 1
198 fi
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800199 if is_windows; then
200 # In msys, readlink <path> doesn't work, because no symbolic link on
201 # Windows. readlink -f <path> returns the real path of a junction.
202 true_path=$(readlink -f "${x}")
203 else
204 true_path=$(readlink "${x}")
205 fi
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800206 if [ ! -d "${true_path}" ]; then
207 echo -e "\n\nERROR: '${x}' does not refer to an existing directory: ${true_path}. Do you need to rerun configure?\n\n" 1>&2
208 exit 1
209 fi
210 done
211}
212
213main "$@"