blob: a5666c2f7eeb2cec904521f46990c2282756a7cb [file] [log] [blame]
Vijay Vasudevan9a4878c2016-03-15 18:45:27 -08001#!/bin/bash
Vijay Vasudevana4806a32015-12-03 10:26:25 -08002# 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 Vasudevanbf6b5362015-12-02 15:04:40 -080016
17set -e -o errexit
18
Martin Wicke59f1eba2016-02-07 12:34:25 -080019# Prefix expected paths with ./ locally and external/reponame/ for remote repos.
20# TODO(kchodorow): remove once runfiles paths are fixed, see
21# https://github.com/bazelbuild/bazel/issues/848.
22script_path=$(dirname $(dirname $(dirname "$0")))
23script_path=${script_path:-.}
24EXPECTED_PATHS="$script_path/util/python/python_include"\
25" $script_path/util/python/python_lib"\
26" $script_path/third_party/py/numpy/numpy_include"
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080027
28function main {
29 argument="$1"
30 shift
31 case $argument in
32 --check)
33 check_python
34 exit 0
35 ;;
36 --setup)
37 setup_python "$1"
38 exit 0
39 ;;
40 esac
41}
42
43function setup_python {
44 PYTHON_BIN_PATH="$1";
45
46 if [ -z "$PYTHON_BIN_PATH" ]; then
47 echo "PYTHON_BIN_PATH was not provided. Did you run configure?"
48 exit 1
49 fi
50 if [ ! -x "$PYTHON_BIN_PATH" ] || [ -d "$PYTHON_BIN_PATH" ]; then
51 echo "PYTHON_BIN_PATH is not executable. Is it the python binary?"
52 exit 1
53 fi
54
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -080055 local python_major_version=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import sys; print(sys.version_info[0]);')
56 if [ "$python_major_version" == "" ]; then
57 echo -e "\n\nERROR: Problem getting python version. Is $PYTHON_BIN_PATH the correct python binary?"
58 exit 1
59 fi
60
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080061 local python_include=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; from distutils import sysconfig; print(sysconfig.get_python_inc());')
62 if [ "$python_include" == "" ]; then
63 echo -e "\n\nERROR: Problem getting python include path. Is distutils installed?"
64 exit 1
65 fi
66 local python_lib=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; from distutils import sysconfig; print(sysconfig.get_python_lib());')
67 if [ "$python_lib" == "" ]; then
68 echo -e "\n\nERROR: Problem getting python lib path. Is distutils installed?"
69 exit 1
70 fi
71 local numpy_include=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import numpy; print(numpy.get_include());')
72 if [ "$numpy_include" == "" ]; then
73 echo -e "\n\nERROR: Problem getting numpy include path. Is numpy installed?"
74 exit 1
75 fi
76
77 for x in $EXPECTED_PATHS; do
78 if [ -e "$x" ]; then
79 rm "$x"
80 fi
81 done
82
Eugene Brevdo56f1d642016-03-10 17:18:30 -080083 ln -sf "${python_include}" util/python/python_include
84 ln -sf "${python_lib}" util/python/python_lib
85 ln -sf "${numpy_include}" third_party/py/numpy/numpy_include
Vijay Vasudevaneb5e56e2015-12-03 14:39:42 -080086
87 # Write tools/bazel.rc
88 echo "# Autogenerated by configure: DO NOT EDIT" > tools/bazel.rc
89 sed -e "s/\$PYTHON_MAJOR_VERSION/$python_major_version/g" \
90 -e "s[\$PYTHON_BINARY[$PYTHON_BIN_PATH[g" \
91 tools/bazel.rc.template >> tools/bazel.rc
Vijay Vasudevan019c6792015-12-10 10:15:38 -080092 # Write tools/python_bin_path.sh
93 echo "export PYTHON_BIN_PATH=$PYTHON_BIN_PATH" > tools/python_bin_path.sh
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080094}
95
96function check_python {
97 for x in $EXPECTED_PATHS; do
98 if [ ! -e "$x" ]; then
99 echo -e "\n\nERROR: Cannot find '${x}'. Did you run configure?\n\n" 1>&2
100 exit 1
101 fi
102 if [ ! -L "${x}" ]; then
103 echo -e "\n\nERROR: '${x}' is not a symbolic link. Internal error.\n\n" 1>&2
104 exit 1
105 fi
106 true_path=$(readlink "${x}")
107 if [ ! -d "${true_path}" ]; then
108 echo -e "\n\nERROR: '${x}' does not refer to an existing directory: ${true_path}. Do you need to rerun configure?\n\n" 1>&2
109 exit 1
110 fi
111 done
112}
113
114main "$@"