Home | History | Annotate | Download | only in pip_package
      1 #!/usr/bin/env bash
      2 # Copyright 2015 The TensorFlow Authors. 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 # ==============================================================================
     16 
     17 
     18 set -e
     19 
     20 function real_path() {
     21   [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
     22 }
     23 
     24 function cp_external() {
     25   local src_dir=$1
     26   local dest_dir=$2
     27   for f in `find "$src_dir" -maxdepth 1 -mindepth 1 ! -name '*local_config_cuda*' ! -name '*org_tensorflow*'`; do
     28     cp -R "$f" "$dest_dir"
     29   done
     30   mkdir -p "${dest_dir}/local_config_cuda/cuda/cuda/"
     31   cp "${src_dir}/local_config_cuda/cuda/cuda/cuda_config.h" "${dest_dir}/local_config_cuda/cuda/cuda/"
     32 }
     33 
     34 PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
     35 function is_windows() {
     36   # On windows, the shell script is actually running in msys
     37   if [[ "${PLATFORM}" =~ msys_nt* ]]; then
     38     true
     39   else
     40     false
     41   fi
     42 }
     43 
     44 function main() {
     45   if [ $# -lt 1 ] ; then
     46     echo "No destination dir provided"
     47     exit 1
     48   fi
     49 
     50   DEST=$(real_path $1)
     51   TMPDIR=$(mktemp -d -t tmp.XXXXXXXXXX)
     52 
     53   PKG_NAME_FLAG=""
     54   GPU_BUILD=0
     55   NIGHTLY_BUILD=0
     56   while true; do
     57     if [[ "$1" == "--nightly_flag" ]]; then
     58       NIGHTLY_BUILD=1
     59     elif [[ "$1" == "--gpu" ]]; then
     60       GPU_BUILD=1
     61     elif [[ "$1" == "--gpudirect" ]]; then
     62       PKG_NAME_FLAG="--project_name tensorflow_gpudirect"
     63     fi
     64     shift
     65 
     66     if [[ -z "$1" ]]; then
     67       break
     68     fi
     69   done
     70 
     71   if [[ ${NIGHTLY_BUILD} == "1" && ${GPU_BUILD} == "1" ]]; then
     72     PKG_NAME_FLAG="--project_name tf_nightly_gpu"
     73   elif [[ ${NIGHTLY_BUILD} == "1" ]]; then
     74     PKG_NAME_FLAG="--project_name tf_nightly"
     75   elif [[ ${GPU_BUILD} == "1" ]]; then
     76     PKG_NAME_FLAG="--project_name tensorflow_gpu"
     77   fi
     78 
     79   echo $(date) : "=== Using tmpdir: ${TMPDIR}"
     80 
     81   if [ ! -d bazel-bin/tensorflow ]; then
     82     echo "Could not find bazel-bin.  Did you run from the root of the build tree?"
     83     exit 1
     84   fi
     85 
     86   if is_windows; then
     87     rm -rf ./bazel-bin/tensorflow/tools/pip_package/simple_console_for_window_unzip
     88     mkdir -p ./bazel-bin/tensorflow/tools/pip_package/simple_console_for_window_unzip
     89     echo "Unzipping simple_console_for_windows.zip to create runfiles tree..."
     90     unzip -o -q ./bazel-bin/tensorflow/tools/pip_package/simple_console_for_windows.zip -d ./bazel-bin/tensorflow/tools/pip_package/simple_console_for_window_unzip
     91     echo "Unzip finished."
     92     # runfiles structure after unzip the python binary
     93     cp -R \
     94       bazel-bin/tensorflow/tools/pip_package/simple_console_for_window_unzip/runfiles/org_tensorflow/tensorflow \
     95       "${TMPDIR}"
     96     mkdir "${TMPDIR}/external"
     97     cp_external \
     98       bazel-bin/tensorflow/tools/pip_package/simple_console_for_window_unzip/runfiles \
     99       "${TMPDIR}/external"
    100     RUNFILES=bazel-bin/tensorflow/tools/pip_package/simple_console_for_window_unzip/runfiles/org_tensorflow
    101   else
    102     RUNFILES=bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow
    103     if [ -d bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/external ]; then
    104       # Old-style runfiles structure (--legacy_external_runfiles).
    105       cp -R \
    106         bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/tensorflow \
    107         "${TMPDIR}"
    108       mkdir "${TMPDIR}/external"
    109       cp_external \
    110         bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/external \
    111         "${TMPDIR}/external"
    112       # Copy MKL libs over so they can be loaded at runtime
    113       so_lib_dir=$(ls $RUNFILES | grep solib) || true
    114       if [ -n "${so_lib_dir}" ]; then
    115         mkl_so_dir=$(ls ${RUNFILES}/${so_lib_dir} | grep mkl) || true
    116         if [ -n "${mkl_so_dir}" ]; then
    117           mkdir "${TMPDIR}/${so_lib_dir}"
    118           cp -R ${RUNFILES}/${so_lib_dir}/${mkl_so_dir} "${TMPDIR}/${so_lib_dir}"
    119         fi
    120       fi
    121     else
    122       # New-style runfiles structure (--nolegacy_external_runfiles).
    123       cp -R \
    124         bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/tensorflow \
    125         "${TMPDIR}"
    126       mkdir "${TMPDIR}/external"
    127       cp_external \
    128         bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles \
    129         "${TMPDIR}/external"
    130       # Copy MKL libs over so they can be loaded at runtime
    131       so_lib_dir=$(ls $RUNFILES | grep solib) || true
    132       if [ -n "${so_lib_dir}" ]; then
    133         mkl_so_dir=$(ls ${RUNFILES}/${so_lib_dir} | grep mkl) || true
    134         if [ -n "${mkl_so_dir}" ]; then
    135           mkdir "${TMPDIR}/${so_lib_dir}"
    136           cp -R ${RUNFILES}/${so_lib_dir}/${mkl_so_dir} "${TMPDIR}/${so_lib_dir}"
    137         fi
    138       fi
    139     fi
    140     mkdir "${TMPDIR}/tensorflow/aux-bin"
    141     # Install toco as a binary in aux-bin.
    142     cp bazel-bin/tensorflow/contrib/lite/toco/toco ${TMPDIR}/tensorflow/aux-bin/
    143   fi
    144 
    145   # protobuf pip package doesn't ship with header files. Copy the headers
    146   # over so user defined ops can be compiled.
    147   mkdir -p ${TMPDIR}/google
    148   mkdir -p ${TMPDIR}/third_party
    149   pushd ${RUNFILES%org_tensorflow}
    150   for header in $(find protobuf_archive -name \*.h); do
    151     mkdir -p "${TMPDIR}/google/$(dirname ${header})"
    152     cp "$header" "${TMPDIR}/google/$(dirname ${header})/"
    153   done
    154   popd
    155   cp -R $RUNFILES/third_party/eigen3 ${TMPDIR}/third_party
    156 
    157   cp tensorflow/tools/pip_package/MANIFEST.in ${TMPDIR}
    158   cp tensorflow/tools/pip_package/README ${TMPDIR}
    159   cp tensorflow/tools/pip_package/setup.py ${TMPDIR}
    160 
    161   # Before we leave the top-level directory, make sure we know how to
    162   # call python.
    163   source tools/python_bin_path.sh
    164 
    165   pushd ${TMPDIR}
    166   rm -f MANIFEST
    167   echo $(date) : "=== Building wheel"
    168   "${PYTHON_BIN_PATH:-python}" setup.py bdist_wheel ${PKG_NAME_FLAG} >/dev/null
    169   mkdir -p ${DEST}
    170   cp dist/* ${DEST}
    171   popd
    172   rm -rf ${TMPDIR}
    173   echo $(date) : "=== Output wheel file is in: ${DEST}"
    174 }
    175 
    176 main "$@"
    177