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 { |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 49 | "$PYTHON_BIN_PATH" - <<END |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 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(':') |
Olivia Nordquist | 308e34f | 2016-08-23 09:27:27 -0800 | [diff] [blame] | 62 | try: |
Dan Mané | 54a7178 | 2016-09-09 16:07:46 -0800 | [diff] [blame] | 63 | library_paths = site.getsitepackages() |
Olivia Nordquist | 308e34f | 2016-08-23 09:27:27 -0800 | [diff] [blame] | 64 | except AttributeError: |
| 65 | from distutils.sysconfig import get_python_lib |
Dan Mané | 54a7178 | 2016-09-09 16:07:46 -0800 | [diff] [blame] | 66 | library_paths = [get_python_lib()] |
Olivia Nordquist | 308e34f | 2016-08-23 09:27:27 -0800 | [diff] [blame] | 67 | all_paths = set(python_paths + library_paths) |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 68 | |
| 69 | paths = [] |
| 70 | for path in all_paths: |
| 71 | if os.path.isdir(path): |
| 72 | paths.append(path) |
| 73 | |
| 74 | if len(paths) == 1: |
| 75 | print(paths[0]) |
Benoit Steiner | 8b3d8c8 | 2016-08-16 14:01:13 -0800 | [diff] [blame] | 76 | else: |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 77 | ret_paths = ",".join(paths) |
Benoit Steiner | 8b3d8c8 | 2016-08-16 14:01:13 -0800 | [diff] [blame] | 78 | print(ret_paths) |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 79 | END |
| 80 | } |
| 81 | |
| 82 | function default_python_path { |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 83 | PYTHON_ARG="$1" "$PYTHON_BIN_PATH" - <<END |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 84 | from __future__ import print_function |
| 85 | import os |
| 86 | |
| 87 | default = os.getenv('PYTHON_ARG') |
| 88 | default = str(default) |
| 89 | print(default) |
| 90 | END |
| 91 | } |
| 92 | |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 93 | function 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 Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame] | 105 | 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 Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 111 | local python_include="$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; from distutils import sysconfig; print(sysconfig.get_python_inc());')" |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 112 | if [ "$python_include" == "" ]; then |
| 113 | echo -e "\n\nERROR: Problem getting python include path. Is distutils installed?" |
| 114 | exit 1 |
| 115 | fi |
Xiaoqiang Zheng | e2d51a8 | 2016-10-28 10:29:28 -0800 | [diff] [blame] | 116 | |
| 117 | if [ -z "$PYTHON_LIB_PATH" ]; then |
| 118 | local python_lib_path |
| 119 | # Split python_path into an array of paths, this allows path containing spaces |
| 120 | IFS=',' |
| 121 | python_lib_path=($(python_path)) |
| 122 | unset IFS |
Benoit Steiner | a771598 | 2016-11-09 13:14:03 -0800 | [diff] [blame] | 123 | |
| 124 | if [ 1 = "$USE_DEFAULT_PYTHON_LIB_PATH" ]; then |
Xiaoqiang Zheng | e2d51a8 | 2016-10-28 10:29:28 -0800 | [diff] [blame] | 125 | PYTHON_LIB_PATH="$(default_python_path "${python_lib_path[0]}")" |
Benoit Steiner | a771598 | 2016-11-09 13:14:03 -0800 | [diff] [blame] | 126 | echo "Using python library path: $PYTHON_LIB_PATH" |
| 127 | |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 128 | else |
Benoit Steiner | a771598 | 2016-11-09 13:14:03 -0800 | [diff] [blame] | 129 | echo "Found possible Python library paths:" |
| 130 | for x in "${python_lib_path[@]}"; do |
| 131 | echo " $x" |
| 132 | done |
| 133 | set -- "${python_lib_path[@]}" |
| 134 | echo "Please input the desired Python library path to use. Default is ["$1"]" |
| 135 | read b || true |
| 136 | if [ "$b" == "" ]; then |
| 137 | PYTHON_LIB_PATH="$(default_python_path "${python_lib_path[0]}")" |
| 138 | echo "Using python library path: $PYTHON_LIB_PATH" |
| 139 | else |
| 140 | PYTHON_LIB_PATH="$b" |
| 141 | fi |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 142 | fi |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 143 | fi |
Benoit Steiner | a771598 | 2016-11-09 13:14:03 -0800 | [diff] [blame] | 144 | |
Xiaoqiang Zheng | e2d51a8 | 2016-10-28 10:29:28 -0800 | [diff] [blame] | 145 | if test -d "$PYTHON_LIB_PATH" -a -x "$PYTHON_LIB_PATH"; then |
| 146 | python_lib="$PYTHON_LIB_PATH" |
| 147 | else |
| 148 | echo -e "\n\nERROR: Invalid python library path: ${PYTHON_LIB_PATH}." |
| 149 | exit 1 |
| 150 | fi |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 151 | |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 152 | local numpy_include=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import numpy; print(numpy.get_include());') |
| 153 | if [ "$numpy_include" == "" ]; then |
| 154 | echo -e "\n\nERROR: Problem getting numpy include path. Is numpy installed?" |
| 155 | exit 1 |
| 156 | fi |
| 157 | |
| 158 | for x in $EXPECTED_PATHS; do |
| 159 | if [ -e "$x" ]; then |
Martin Wicke | 999b794 | 2016-09-21 13:16:48 -0800 | [diff] [blame] | 160 | rm -rf "$x" |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 161 | fi |
| 162 | done |
| 163 | |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 164 | # ln -sf is actually implemented as copying in msys since creating symbolic |
| 165 | # links is privileged on Windows. But copying is too slow, so invoke mklink |
| 166 | # to create junctions on Windows. |
| 167 | if is_windows; then |
| 168 | cmd /c "mklink /J util\\python\\python_include \"${python_include}\"" |
| 169 | cmd /c "mklink /J util\\python\\python_lib \"${python_lib}\"" |
| 170 | cmd /c "mklink /J third_party\\py\\numpy\\numpy_include \"${numpy_include}\"" |
| 171 | else |
| 172 | ln -sf "${python_include}" util/python/python_include |
| 173 | ln -sf "${python_lib}" util/python/python_lib |
| 174 | ln -sf "${numpy_include}" third_party/py/numpy/numpy_include |
| 175 | fi |
| 176 | # Convert python path to Windows style before writing into bazel.rc |
| 177 | if is_windows; then |
| 178 | PYTHON_BIN_PATH="$(cygpath -m "$PYTHON_BIN_PATH")" |
| 179 | fi |
Vijay Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame] | 180 | |
| 181 | # Write tools/bazel.rc |
| 182 | echo "# Autogenerated by configure: DO NOT EDIT" > tools/bazel.rc |
| 183 | sed -e "s/\$PYTHON_MAJOR_VERSION/$python_major_version/g" \ |
Martin Wicke | bc456e3 | 2017-03-23 12:31:16 -0800 | [diff] [blame^] | 184 | -e "s|\$PYTHON_BINARY|\"$PYTHON_BIN_PATH\"|g" \ |
Vijay Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame] | 185 | tools/bazel.rc.template >> tools/bazel.rc |
Vijay Vasudevan | 019c679 | 2015-12-10 10:15:38 -0800 | [diff] [blame] | 186 | # Write tools/python_bin_path.sh |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 187 | 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] | 188 | } |
| 189 | |
Martin Wicke | 999b794 | 2016-09-21 13:16:48 -0800 | [diff] [blame] | 190 | PLATFORM="$(uname -s | tr 'A-Z' 'a-z')" |
| 191 | function is_windows() { |
| 192 | # On windows, the shell script is actually running in msys |
| 193 | if [[ "${PLATFORM}" =~ msys_nt* ]]; then |
| 194 | true |
| 195 | else |
| 196 | false |
| 197 | fi |
| 198 | } |
| 199 | |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 200 | function check_python { |
| 201 | for x in $EXPECTED_PATHS; do |
| 202 | if [ ! -e "$x" ]; then |
| 203 | echo -e "\n\nERROR: Cannot find '${x}'. Did you run configure?\n\n" 1>&2 |
| 204 | exit 1 |
| 205 | fi |
Martin Wicke | 999b794 | 2016-09-21 13:16:48 -0800 | [diff] [blame] | 206 | # Don't check symbolic link on Windows |
| 207 | if ! is_windows && [ ! -L "${x}" ]; then |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 208 | echo -e "\n\nERROR: '${x}' is not a symbolic link. Internal error.\n\n" 1>&2 |
| 209 | exit 1 |
| 210 | fi |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 211 | if is_windows; then |
| 212 | # In msys, readlink <path> doesn't work, because no symbolic link on |
| 213 | # Windows. readlink -f <path> returns the real path of a junction. |
| 214 | true_path=$(readlink -f "${x}") |
| 215 | else |
| 216 | true_path=$(readlink "${x}") |
| 217 | fi |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 218 | if [ ! -d "${true_path}" ]; then |
| 219 | echo -e "\n\nERROR: '${x}' does not refer to an existing directory: ${true_path}. Do you need to rerun configure?\n\n" 1>&2 |
| 220 | exit 1 |
| 221 | fi |
| 222 | done |
| 223 | } |
| 224 | |
| 225 | main "$@" |