Geoffrey Irving | 4c85a08 | 2016-03-16 12:20:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
A. Unique TensorFlower | 122cdce | 2016-06-02 12:32:54 -0800 | [diff] [blame] | 2 | # Copyright 2015 The TensorFlow Authors. All Rights Reserved. |
Vijay Vasudevan | a4806a3 | 2015-12-03 10:26:25 -0800 | [diff] [blame] | 3 | # |
| 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 Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 16 | |
| 17 | set -e -o errexit |
| 18 | |
Vijay Vasudevan | 8cc567b | 2016-05-26 11:05:13 -0800 | [diff] [blame] | 19 | if [ -d "../org_tensorflow" ]; then |
| 20 | script_path="../org_tensorflow" |
| 21 | else |
| 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:-.} |
| 27 | fi |
| 28 | |
Martin Wicke | 59f1eba | 2016-02-07 12:34:25 -0800 | [diff] [blame] | 29 | EXPECTED_PATHS="$script_path/util/python/python_include"\ |
| 30 | " $script_path/util/python/python_lib"\ |
| 31 | " $script_path/third_party/py/numpy/numpy_include" |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 32 | |
| 33 | function 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 Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 48 | function python_path { |
| 49 | python - <<END |
| 50 | from __future__ import print_function |
| 51 | import site |
| 52 | import os |
| 53 | |
| 54 | try: |
| 55 | input = raw_input |
| 56 | except NameError: |
| 57 | pass |
| 58 | |
| 59 | python_paths = [] |
| 60 | if os.getenv('PYTHONPATH') is not None: |
| 61 | python_paths = os.getenv('PYTHONPATH').split(':') |
| 62 | |
| 63 | all_paths = set(python_paths + site.getsitepackages()) |
| 64 | |
| 65 | paths = [] |
| 66 | for path in all_paths: |
| 67 | if os.path.isdir(path): |
| 68 | paths.append(path) |
| 69 | |
| 70 | if len(paths) == 1: |
| 71 | print(paths[0]) |
| 72 | ret_paths = "" |
| 73 | for path in paths: |
| 74 | ret_paths += path + " " |
| 75 | print(ret_paths) |
| 76 | END |
| 77 | } |
| 78 | |
| 79 | function default_python_path { |
| 80 | PYTHON_ARG="$1" python - <<END |
| 81 | from __future__ import print_function |
| 82 | import os |
| 83 | |
| 84 | default = os.getenv('PYTHON_ARG') |
| 85 | default = str(default) |
| 86 | print(default) |
| 87 | END |
| 88 | } |
| 89 | |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 90 | function setup_python { |
| 91 | PYTHON_BIN_PATH="$1"; |
| 92 | |
| 93 | if [ -z "$PYTHON_BIN_PATH" ]; then |
| 94 | echo "PYTHON_BIN_PATH was not provided. Did you run configure?" |
| 95 | exit 1 |
| 96 | fi |
| 97 | if [ ! -x "$PYTHON_BIN_PATH" ] || [ -d "$PYTHON_BIN_PATH" ]; then |
| 98 | echo "PYTHON_BIN_PATH is not executable. Is it the python binary?" |
| 99 | exit 1 |
| 100 | fi |
| 101 | |
Vijay Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame] | 102 | local python_major_version=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import sys; print(sys.version_info[0]);') |
| 103 | if [ "$python_major_version" == "" ]; then |
| 104 | echo -e "\n\nERROR: Problem getting python version. Is $PYTHON_BIN_PATH the correct python binary?" |
| 105 | exit 1 |
| 106 | fi |
| 107 | |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 108 | local python_include=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; from distutils import sysconfig; print(sysconfig.get_python_inc());') |
| 109 | if [ "$python_include" == "" ]; then |
| 110 | echo -e "\n\nERROR: Problem getting python include path. Is distutils installed?" |
| 111 | exit 1 |
| 112 | fi |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 113 | |
| 114 | local python_lib_path=$(python_path) |
| 115 | echo "Found possible Python library paths:" |
| 116 | for x in $python_lib_path; do |
| 117 | echo " $x" |
| 118 | done |
| 119 | set -- $python_lib_path |
| 120 | echo "Please input the desired Python library path to use. Default is ["$1"]" |
A. Unique TensorFlower | 5c29a24 | 2016-08-12 06:21:36 -0800 | [diff] [blame] | 121 | read b || true |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 122 | if [ "$b" == "" ]; then |
| 123 | python_lib="$(default_python_path $python_lib_path)" |
| 124 | echo $python_lib |
| 125 | else |
| 126 | if test -d $b -a -x $b; then |
| 127 | python_lib=$b |
| 128 | else |
| 129 | echo -e "\n\nERROR: The path you have entered does not exist." |
| 130 | exit 1 |
| 131 | fi |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 132 | fi |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 133 | |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 134 | local numpy_include=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import numpy; print(numpy.get_include());') |
| 135 | if [ "$numpy_include" == "" ]; then |
| 136 | echo -e "\n\nERROR: Problem getting numpy include path. Is numpy installed?" |
| 137 | exit 1 |
| 138 | fi |
| 139 | |
| 140 | for x in $EXPECTED_PATHS; do |
| 141 | if [ -e "$x" ]; then |
| 142 | rm "$x" |
| 143 | fi |
| 144 | done |
| 145 | |
Eugene Brevdo | 56f1d64 | 2016-03-10 17:18:30 -0800 | [diff] [blame] | 146 | ln -sf "${python_include}" util/python/python_include |
| 147 | ln -sf "${python_lib}" util/python/python_lib |
| 148 | ln -sf "${numpy_include}" third_party/py/numpy/numpy_include |
Vijay Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame] | 149 | |
| 150 | # Write tools/bazel.rc |
| 151 | echo "# Autogenerated by configure: DO NOT EDIT" > tools/bazel.rc |
| 152 | sed -e "s/\$PYTHON_MAJOR_VERSION/$python_major_version/g" \ |
| 153 | -e "s[\$PYTHON_BINARY[$PYTHON_BIN_PATH[g" \ |
| 154 | tools/bazel.rc.template >> tools/bazel.rc |
Vijay Vasudevan | 019c679 | 2015-12-10 10:15:38 -0800 | [diff] [blame] | 155 | # Write tools/python_bin_path.sh |
| 156 | echo "export PYTHON_BIN_PATH=$PYTHON_BIN_PATH" > tools/python_bin_path.sh |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | function check_python { |
| 160 | for x in $EXPECTED_PATHS; do |
| 161 | if [ ! -e "$x" ]; then |
| 162 | echo -e "\n\nERROR: Cannot find '${x}'. Did you run configure?\n\n" 1>&2 |
| 163 | exit 1 |
| 164 | fi |
| 165 | if [ ! -L "${x}" ]; then |
| 166 | echo -e "\n\nERROR: '${x}' is not a symbolic link. Internal error.\n\n" 1>&2 |
| 167 | exit 1 |
| 168 | fi |
| 169 | true_path=$(readlink "${x}") |
| 170 | if [ ! -d "${true_path}" ]; then |
| 171 | echo -e "\n\nERROR: '${x}' does not refer to an existing directory: ${true_path}. Do you need to rerun configure?\n\n" 1>&2 |
| 172 | exit 1 |
| 173 | fi |
| 174 | done |
| 175 | } |
| 176 | |
| 177 | main "$@" |