Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 1 | #!/bin/bash |
Vijay Vasudevan | a4806a3 | 2015-12-03 10:26:25 -0800 | [diff] [blame] | 2 | # Copyright 2015 Google Inc. All Rights Reserved. |
| 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 | |
| 19 | EXPECTED_PATHS="util/python/python_include util/python/python_lib third_party/py/numpy/numpy_include" |
| 20 | |
| 21 | function main { |
| 22 | argument="$1" |
| 23 | shift |
| 24 | case $argument in |
| 25 | --check) |
| 26 | check_python |
| 27 | exit 0 |
| 28 | ;; |
| 29 | --setup) |
| 30 | setup_python "$1" |
| 31 | exit 0 |
| 32 | ;; |
| 33 | esac |
| 34 | } |
| 35 | |
| 36 | function setup_python { |
| 37 | PYTHON_BIN_PATH="$1"; |
| 38 | |
| 39 | if [ -z "$PYTHON_BIN_PATH" ]; then |
| 40 | echo "PYTHON_BIN_PATH was not provided. Did you run configure?" |
| 41 | exit 1 |
| 42 | fi |
| 43 | if [ ! -x "$PYTHON_BIN_PATH" ] || [ -d "$PYTHON_BIN_PATH" ]; then |
| 44 | echo "PYTHON_BIN_PATH is not executable. Is it the python binary?" |
| 45 | exit 1 |
| 46 | fi |
| 47 | |
Vijay Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame^] | 48 | local python_major_version=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import sys; print(sys.version_info[0]);') |
| 49 | if [ "$python_major_version" == "" ]; then |
| 50 | echo -e "\n\nERROR: Problem getting python version. Is $PYTHON_BIN_PATH the correct python binary?" |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 54 | local python_include=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; from distutils import sysconfig; print(sysconfig.get_python_inc());') |
| 55 | if [ "$python_include" == "" ]; then |
| 56 | echo -e "\n\nERROR: Problem getting python include path. Is distutils installed?" |
| 57 | exit 1 |
| 58 | fi |
| 59 | local python_lib=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; from distutils import sysconfig; print(sysconfig.get_python_lib());') |
| 60 | if [ "$python_lib" == "" ]; then |
| 61 | echo -e "\n\nERROR: Problem getting python lib path. Is distutils installed?" |
| 62 | exit 1 |
| 63 | fi |
| 64 | local numpy_include=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import numpy; print(numpy.get_include());') |
| 65 | if [ "$numpy_include" == "" ]; then |
| 66 | echo -e "\n\nERROR: Problem getting numpy include path. Is numpy installed?" |
| 67 | exit 1 |
| 68 | fi |
| 69 | |
| 70 | for x in $EXPECTED_PATHS; do |
| 71 | if [ -e "$x" ]; then |
| 72 | rm "$x" |
| 73 | fi |
| 74 | done |
| 75 | |
| 76 | ln -s "${python_include}" util/python/python_include |
| 77 | ln -s "${python_lib}" util/python/python_lib |
| 78 | ln -s "${numpy_include}" third_party/py/numpy/numpy_include |
Vijay Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame^] | 79 | |
| 80 | # Write tools/bazel.rc |
| 81 | echo "# Autogenerated by configure: DO NOT EDIT" > tools/bazel.rc |
| 82 | sed -e "s/\$PYTHON_MAJOR_VERSION/$python_major_version/g" \ |
| 83 | -e "s[\$PYTHON_BINARY[$PYTHON_BIN_PATH[g" \ |
| 84 | tools/bazel.rc.template >> tools/bazel.rc |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | function check_python { |
| 88 | for x in $EXPECTED_PATHS; do |
| 89 | if [ ! -e "$x" ]; then |
| 90 | echo -e "\n\nERROR: Cannot find '${x}'. Did you run configure?\n\n" 1>&2 |
| 91 | exit 1 |
| 92 | fi |
| 93 | if [ ! -L "${x}" ]; then |
| 94 | echo -e "\n\nERROR: '${x}' is not a symbolic link. Internal error.\n\n" 1>&2 |
| 95 | exit 1 |
| 96 | fi |
| 97 | true_path=$(readlink "${x}") |
| 98 | if [ ! -d "${true_path}" ]; then |
| 99 | echo -e "\n\nERROR: '${x}' does not refer to an existing directory: ${true_path}. Do you need to rerun configure?\n\n" 1>&2 |
| 100 | exit 1 |
| 101 | fi |
| 102 | done |
| 103 | } |
| 104 | |
| 105 | main "$@" |