github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/toolchains/preconfig/generate/generate.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright 2018 The TensorFlow Authors. All Rights Reserved.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  # ==============================================================================
    17  
    18  TARGET="$1"
    19  OUTPUT="$2"
    20  
    21  if [[ -z "${TARGET}" || -z "${OUTPUT}" ]]; then
    22    echo "Usage:"
    23    echo "$0 <target> <output>"
    24    exit 1
    25  fi
    26  
    27  TEMPDIR="$(mktemp -d)"
    28  ROOT="${PWD}"
    29  PKG="third_party/toolchains/preconfig"
    30  IFS='-' read -ra PLATFORM <<< "${TARGET}"
    31  OS="${PLATFORM[0]}"
    32  PY_VERSION="${PLATFORM[1]}"
    33  COMPILER="${PLATFORM[2]}"
    34  GPU_VERSION="${PLATFORM[3]}"
    35  CUDNN_VERSION="${PLATFORM[4]}"
    36  TENSORRT_VERSION="${PLATFORM[5]}"
    37  
    38  # TODO(klimek): Put this into the name.
    39  
    40  if [[ "${GPU_VERSION}" == "rocm" ]]; then
    41    COMPILER="${COMPILER}"
    42  elif [[ -n "${GPU_VERSION}" ]]; then
    43    if [[ "${COMPILER}" == gcc* ]]; then
    44      COMPILER="${COMPILER}-nvcc-${GPU_VERSION}"
    45    fi
    46    # Currently we create a special toolchain for clang when compiling with
    47    # cuda enabled. We can get rid of this once the default toolchain bazel
    48    # provides supports cuda.
    49    if [[ "${COMPILER}" == "clang" ]]; then
    50      COMPILER="cuda-clang"
    51    fi
    52  fi
    53  
    54  echo "OS: ${OS}"
    55  echo "Python: ${PY_VERSION}"
    56  echo "Compiler: ${COMPILER}"
    57  echo "CUDA/ROCm: ${GPU_VERSION}"
    58  echo "CUDNN: ${CUDNN_VERSION}"
    59  echo "TensorRT: ${TENSORRT_VERSION}"
    60  
    61  bazel build --host_force_python=PY2 --define=mount_project="${PWD}" \
    62    "${PKG}/generate:${TARGET}"
    63  cd "${TEMPDIR}"
    64  tar xvf "${ROOT}/bazel-bin/${PKG}/generate/${TARGET}_outputs.tar"
    65  
    66  # Delete all empty files: configurations leave empty files around when they are
    67  # unnecessary.
    68  find . -empty -delete
    69  
    70  # We build up the following directory structure with preconfigured packages:
    71  # <OS>/
    72  #   <CUDA>-<CUDNN>/
    73  #   <COMPILER>/
    74  #   <PYTHON>/
    75  #   <TENSORRT>/
    76  
    77  # Create our toplevel output directory for the OS.
    78  mkdir "${OS}"
    79  
    80  # Python:
    81  mv local_config_python "${OS}/${PY_VERSION}"
    82  
    83  if [[ "${GPU_VERSION}" == "rocm" ]]; then
    84    # Compiler:
    85    mv local_config_rocm/crosstool "${OS}/${COMPILER}-${GPU_VERSION}"
    86  
    87    # ROCm:
    88    mv local_config_rocm "${OS}/${GPU_VERSION}"
    89  elif [[ -n "${GPU_VERSION}" ]]; then
    90    # Compiler:
    91    mv local_config_cuda/crosstool "${OS}/${COMPILER}"
    92  
    93    # CUDA:
    94    mv local_config_cuda "${OS}/${GPU_VERSION}-${CUDNN_VERSION}"
    95  
    96    # TensorRT:
    97    mv local_config_tensorrt "${OS}/${TENSORRT_VERSION}"
    98  else
    99    # Compiler:
   100    mv local_config_cc "${OS}/${COMPILER}"
   101  fi
   102  
   103  # Cleanup for copybara.
   104  find "${OS}" -name '*.h' |xargs clang-format -i
   105  find "${OS}" -name 'BUILD' -o -name '*.bzl' |xargs buildifier
   106  find "${OS}" -name 'BUILD' -o -name '*.bzl' |xargs -I {} mv {} {}.oss
   107  
   108  # Tar it up:
   109  tar cvf "${OUTPUT}" "${OS}"
   110