blob: 76ad559f02c9b2f7f48ce3d27a1bdf2e57c5ec09 [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 {
49 python - <<END
50from __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(':')
62
63all_paths = set(python_paths + site.getsitepackages())
64
65paths = []
66for path in all_paths:
67 if os.path.isdir(path):
68 paths.append(path)
69
70if len(paths) == 1:
71 print(paths[0])
Benoit Steiner8b3d8c82016-08-16 14:01:13 -080072else:
73 ret_paths = " ".join(paths)
74 print(ret_paths)
Olivia Nordquist4959a122016-08-09 11:16:44 -080075END
76}
77
78function default_python_path {
79 PYTHON_ARG="$1" python - <<END
80from __future__ import print_function
81import os
82
83default = os.getenv('PYTHON_ARG')
84default = str(default)
85print(default)
86END
87}
88
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080089function setup_python {
90 PYTHON_BIN_PATH="$1";
91
92 if [ -z "$PYTHON_BIN_PATH" ]; then
93 echo "PYTHON_BIN_PATH was not provided. Did you run configure?"
94 exit 1
95 fi
96 if [ ! -x "$PYTHON_BIN_PATH" ] || [ -d "$PYTHON_BIN_PATH" ]; then
97 echo "PYTHON_BIN_PATH is not executable. Is it the python binary?"
98 exit 1
99 fi
100
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -0800101 local python_major_version=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import sys; print(sys.version_info[0]);')
102 if [ "$python_major_version" == "" ]; then
103 echo -e "\n\nERROR: Problem getting python version. Is $PYTHON_BIN_PATH the correct python binary?"
104 exit 1
105 fi
106
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800107 local python_include=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; from distutils import sysconfig; print(sysconfig.get_python_inc());')
108 if [ "$python_include" == "" ]; then
109 echo -e "\n\nERROR: Problem getting python include path. Is distutils installed?"
110 exit 1
111 fi
Olivia Nordquist4959a122016-08-09 11:16:44 -0800112
113 local python_lib_path=$(python_path)
114 echo "Found possible Python library paths:"
115 for x in $python_lib_path; do
116 echo " $x"
117 done
118 set -- $python_lib_path
119 echo "Please input the desired Python library path to use. Default is ["$1"]"
A. Unique TensorFlower5c29a242016-08-12 06:21:36 -0800120 read b || true
Olivia Nordquist4959a122016-08-09 11:16:44 -0800121 if [ "$b" == "" ]; then
122 python_lib="$(default_python_path $python_lib_path)"
123 echo $python_lib
124 else
125 if test -d $b -a -x $b; then
126 python_lib=$b
127 else
128 echo -e "\n\nERROR: The path you have entered does not exist."
129 exit 1
130 fi
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800131 fi
Olivia Nordquist4959a122016-08-09 11:16:44 -0800132
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800133 local numpy_include=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import numpy; print(numpy.get_include());')
134 if [ "$numpy_include" == "" ]; then
135 echo -e "\n\nERROR: Problem getting numpy include path. Is numpy installed?"
136 exit 1
137 fi
138
139 for x in $EXPECTED_PATHS; do
140 if [ -e "$x" ]; then
141 rm "$x"
142 fi
143 done
144
Eugene Brevdo56f1d642016-03-10 17:18:30 -0800145 ln -sf "${python_include}" util/python/python_include
146 ln -sf "${python_lib}" util/python/python_lib
147 ln -sf "${numpy_include}" third_party/py/numpy/numpy_include
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -0800148
149 # Write tools/bazel.rc
150 echo "# Autogenerated by configure: DO NOT EDIT" > tools/bazel.rc
151 sed -e "s/\$PYTHON_MAJOR_VERSION/$python_major_version/g" \
152 -e "s[\$PYTHON_BINARY[$PYTHON_BIN_PATH[g" \
153 tools/bazel.rc.template >> tools/bazel.rc
Vijay Vasudevan019c6792015-12-10 10:15:38 -0800154 # Write tools/python_bin_path.sh
155 echo "export PYTHON_BIN_PATH=$PYTHON_BIN_PATH" > tools/python_bin_path.sh
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800156}
157
158function check_python {
159 for x in $EXPECTED_PATHS; do
160 if [ ! -e "$x" ]; then
161 echo -e "\n\nERROR: Cannot find '${x}'. Did you run configure?\n\n" 1>&2
162 exit 1
163 fi
164 if [ ! -L "${x}" ]; then
165 echo -e "\n\nERROR: '${x}' is not a symbolic link. Internal error.\n\n" 1>&2
166 exit 1
167 fi
168 true_path=$(readlink "${x}")
169 if [ ! -d "${true_path}" ]; then
170 echo -e "\n\nERROR: '${x}' does not refer to an existing directory: ${true_path}. Do you need to rerun configure?\n\n" 1>&2
171 exit 1
172 fi
173 done
174}
175
176main "$@"