blob: d5762ad4561bd1bbc3f57f11a2ad53fb114766c6 [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
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080029function main {
A. Unique TensorFlower8e0d6f12017-04-21 06:35:18 -080030 setup_python "$1"
31 exit 0
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080032}
33
Olivia Nordquist4959a122016-08-09 11:16:44 -080034function python_path {
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -080035 "$PYTHON_BIN_PATH" - <<END
Olivia Nordquist4959a122016-08-09 11:16:44 -080036from __future__ import print_function
37import site
38import os
39
40try:
41 input = raw_input
42except NameError:
43 pass
44
45python_paths = []
46if os.getenv('PYTHONPATH') is not None:
47 python_paths = os.getenv('PYTHONPATH').split(':')
Olivia Nordquist308e34f2016-08-23 09:27:27 -080048try:
Dan Mané54a71782016-09-09 16:07:46 -080049 library_paths = site.getsitepackages()
Olivia Nordquist308e34f2016-08-23 09:27:27 -080050except AttributeError:
51 from distutils.sysconfig import get_python_lib
Dan Mané54a71782016-09-09 16:07:46 -080052 library_paths = [get_python_lib()]
Olivia Nordquist308e34f2016-08-23 09:27:27 -080053all_paths = set(python_paths + library_paths)
Olivia Nordquist4959a122016-08-09 11:16:44 -080054
55paths = []
56for path in all_paths:
57 if os.path.isdir(path):
58 paths.append(path)
59
60if len(paths) == 1:
61 print(paths[0])
Benoit Steiner8b3d8c82016-08-16 14:01:13 -080062else:
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -080063 ret_paths = ",".join(paths)
Benoit Steiner8b3d8c82016-08-16 14:01:13 -080064 print(ret_paths)
Olivia Nordquist4959a122016-08-09 11:16:44 -080065END
66}
67
68function default_python_path {
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -080069 PYTHON_ARG="$1" "$PYTHON_BIN_PATH" - <<END
Olivia Nordquist4959a122016-08-09 11:16:44 -080070from __future__ import print_function
71import os
72
73default = os.getenv('PYTHON_ARG')
74default = str(default)
75print(default)
76END
77}
78
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080079function setup_python {
80 PYTHON_BIN_PATH="$1";
81
A. Unique TensorFlower8e0d6f12017-04-21 06:35:18 -080082 # TODO(ngiraldo): move most of these checks to root configure
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080083 if [ -z "$PYTHON_BIN_PATH" ]; then
84 echo "PYTHON_BIN_PATH was not provided. Did you run configure?"
85 exit 1
86 fi
87 if [ ! -x "$PYTHON_BIN_PATH" ] || [ -d "$PYTHON_BIN_PATH" ]; then
88 echo "PYTHON_BIN_PATH is not executable. Is it the python binary?"
89 exit 1
90 fi
91
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -080092 local python_major_version=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import sys; print(sys.version_info[0]);')
93 if [ "$python_major_version" == "" ]; then
94 echo -e "\n\nERROR: Problem getting python version. Is $PYTHON_BIN_PATH the correct python binary?"
95 exit 1
96 fi
97
A. Unique TensorFlower8e0d6f12017-04-21 06:35:18 -080098 # TODO(ngiraldo): confirm if these checks are really necessary, remove if not
Xiaoqiang Zhenge2d51a82016-10-28 10:29:28 -080099 if [ -z "$PYTHON_LIB_PATH" ]; then
100 local python_lib_path
101 # Split python_path into an array of paths, this allows path containing spaces
102 IFS=','
103 python_lib_path=($(python_path))
104 unset IFS
Benoit Steinera7715982016-11-09 13:14:03 -0800105
106 if [ 1 = "$USE_DEFAULT_PYTHON_LIB_PATH" ]; then
Xiaoqiang Zhenge2d51a82016-10-28 10:29:28 -0800107 PYTHON_LIB_PATH="$(default_python_path "${python_lib_path[0]}")"
Benoit Steinera7715982016-11-09 13:14:03 -0800108 echo "Using python library path: $PYTHON_LIB_PATH"
109
Olivia Nordquist4959a122016-08-09 11:16:44 -0800110 else
Benoit Steinera7715982016-11-09 13:14:03 -0800111 echo "Found possible Python library paths:"
112 for x in "${python_lib_path[@]}"; do
113 echo " $x"
114 done
115 set -- "${python_lib_path[@]}"
116 echo "Please input the desired Python library path to use. Default is ["$1"]"
117 read b || true
118 if [ "$b" == "" ]; then
119 PYTHON_LIB_PATH="$(default_python_path "${python_lib_path[0]}")"
120 echo "Using python library path: $PYTHON_LIB_PATH"
121 else
122 PYTHON_LIB_PATH="$b"
123 fi
Olivia Nordquist4959a122016-08-09 11:16:44 -0800124 fi
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800125 fi
Benoit Steinera7715982016-11-09 13:14:03 -0800126
Xiaoqiang Zhenge2d51a82016-10-28 10:29:28 -0800127 if test -d "$PYTHON_LIB_PATH" -a -x "$PYTHON_LIB_PATH"; then
128 python_lib="$PYTHON_LIB_PATH"
129 else
130 echo -e "\n\nERROR: Invalid python library path: ${PYTHON_LIB_PATH}."
131 exit 1
132 fi
Olivia Nordquist4959a122016-08-09 11:16:44 -0800133
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800134 # Convert python path to Windows style before writing into bazel.rc
135 if is_windows; then
136 PYTHON_BIN_PATH="$(cygpath -m "$PYTHON_BIN_PATH")"
137 fi
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -0800138
A. Unique TensorFlower8e0d6f12017-04-21 06:35:18 -0800139 # TODO(ngiraldo): move all below to root configure
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -0800140 # Write tools/bazel.rc
141 echo "# Autogenerated by configure: DO NOT EDIT" > tools/bazel.rc
142 sed -e "s/\$PYTHON_MAJOR_VERSION/$python_major_version/g" \
Martin Wickebc456e32017-03-23 12:31:16 -0800143 -e "s|\$PYTHON_BINARY|\"$PYTHON_BIN_PATH\"|g" \
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -0800144 tools/bazel.rc.template >> tools/bazel.rc
Vijay Vasudevan019c6792015-12-10 10:15:38 -0800145 # Write tools/python_bin_path.sh
Patrick Nguyenc5ab3dd2016-10-20 12:09:18 -0800146 echo "export PYTHON_BIN_PATH=\"$PYTHON_BIN_PATH\"" > tools/python_bin_path.sh
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800147}
148
Martin Wicke999b7942016-09-21 13:16:48 -0800149PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
150function is_windows() {
151 # On windows, the shell script is actually running in msys
152 if [[ "${PLATFORM}" =~ msys_nt* ]]; then
153 true
154 else
155 false
156 fi
157}
158
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -0800159main "$@"