|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2024 The DeepRec 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 | +# This script is used for packaging TensorFlow SDK files into a tarball. |
| 18 | +# The processing flow took 'tensorflow/tools/pip_package/build_pip_package.sh' |
| 19 | +# as the reference. |
| 20 | + |
| 21 | +set -e |
| 22 | + |
| 23 | +PLATFORM="$(uname -s | tr 'A-Z' 'a-z')" |
| 24 | +function is_windows() { |
| 25 | + # On windows, the shell script is actually running in msys |
| 26 | + if [[ "${PLATFORM}" =~ msys_nt* ]]; then |
| 27 | + true |
| 28 | + else |
| 29 | + false |
| 30 | + fi |
| 31 | +} |
| 32 | + |
| 33 | +function main() { |
| 34 | + if [ $# -lt 1 ] ; then |
| 35 | + echo "No destination dir provided" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + |
| 39 | + DEST=$1 |
| 40 | + TMPDIR=$(mktemp -d -t tmp.XXXXXXXXXX) |
| 41 | + mkdir -p "${TMPDIR}/sdk/bin" |
| 42 | + mkdir -p "${TMPDIR}/sdk/include" |
| 43 | + mkdir -p "${TMPDIR}/sdk/lib" |
| 44 | + |
| 45 | + echo $(date) : "=== Using tmpdir: ${TMPDIR}" |
| 46 | + |
| 47 | + if [ ! -d bazel-bin/tensorflow ]; then |
| 48 | + echo "Could not find bazel-bin. Did you run from the root of the build tree?" |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | + |
| 52 | + if is_windows; then |
| 53 | + echo "Windows version TensorFlow SDK not supported..." |
| 54 | + elif [ ! -d bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/org_tensorflow ]; then |
| 55 | + # Really old (0.2.1-) runfiles, without workspace name. |
| 56 | + echo "TensorFlow SDK does not support such old verions..." |
| 57 | + else |
| 58 | + RUNFILES=bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/org_tensorflow |
| 59 | + if [ -d ${RUNFILES}/external ]; then |
| 60 | + # Old-style runfiles structure (--legacy_external_runfiles). |
| 61 | + cp -RL ${RUNFILES}/tensorflow "${TMPDIR}/sdk/include" |
| 62 | + # Check LLVM headers for XLA support. |
| 63 | + if [ -d ${RUNFILES}/external/llvm_archive ]; then |
| 64 | + # Old-style runfiles structure (--legacy_external_runfiles). |
| 65 | + mkdir -p ${TMPDIR}/sdk/include/external/llvm/include |
| 66 | + cp -RL ${RUNFILES}/external/llvm_archive/include/llvm \ |
| 67 | + "${TMPDIR}/sdk/include/external/llvm/include" |
| 68 | + pushd ${TMPDIR}/sdk/include |
| 69 | + ln -s external/llvm/include/llvm llvm |
| 70 | + popd |
| 71 | + fi |
| 72 | + # Copy MKL libs over so they can be loaded at runtime |
| 73 | + so_lib_dir=$(ls $RUNFILES | grep solib) || true |
| 74 | + if [ -n "${so_lib_dir}" ]; then |
| 75 | + mkl_so_dir=$(ls ${RUNFILES}/${so_lib_dir} | grep mkl) || true |
| 76 | + if [ -n "${mkl_so_dir}" ]; then |
| 77 | + cp -L ${RUNFILES}/${so_lib_dir}/${mkl_so_dir}/*.so "${TMPDIR}/sdk/lib" |
| 78 | + fi |
| 79 | + fi |
| 80 | + else |
| 81 | + # New-style runfiles structure (--nolegacy_external_runfiles). |
| 82 | + cp -RL ${RUNFILES}/tensorflow "${TMPDIR}/sdk/include" |
| 83 | + # Check LLVM headers for XLA support. |
| 84 | + if [ -d bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/llvm_archive ]; then |
| 85 | + cp -RL \ |
| 86 | + bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/llvm_archive/include/llvm \ |
| 87 | + "${TMPDIR}/sdk/include" |
| 88 | + fi |
| 89 | + # Copy MKL libs over so they can be loaded at runtime |
| 90 | + so_lib_dir=$(ls $RUNFILES | grep solib) || true |
| 91 | + if [ -n "${so_lib_dir}" ]; then |
| 92 | + mkl_so_dir=$(ls ${RUNFILES}/${so_lib_dir} | grep mkl) || true |
| 93 | + if [ -n "${mkl_so_dir}" ]; then |
| 94 | + cp -L ${RUNFILES}/${so_lib_dir}/${mkl_so_dir}/*.so "${TMPDIR}/sdk/lib" |
| 95 | + fi |
| 96 | + fi |
| 97 | + fi |
| 98 | + fi |
| 99 | + |
| 100 | + # move and strip the dynamic library file for packaging. |
| 101 | + # at default the .so file was not writable for the owner, |
| 102 | + # so using a 'chmod +w' to perform the strip command. |
| 103 | + chmod +w ${TMPDIR}/sdk/include/tensorflow/libtensorflow_cc.so |
| 104 | + chmod +w ${TMPDIR}/sdk/include/tensorflow/libtensorflow_framework.so.1 |
| 105 | + strip ${TMPDIR}/sdk/include/tensorflow/libtensorflow_cc.so |
| 106 | + strip ${TMPDIR}/sdk/include/tensorflow/libtensorflow_framework.so.1 |
| 107 | + mv ${TMPDIR}/sdk/include/tensorflow/libtensorflow_*.so* ${TMPDIR}/sdk/lib |
| 108 | + |
| 109 | + # third party packages doesn't ship with header files. Copy the headers |
| 110 | + # over so user defined ops can be compiled. |
| 111 | + mkdir -p ${TMPDIR}/sdk/include/google |
| 112 | + mkdir -p ${TMPDIR}/sdk/include/third_party |
| 113 | + pushd ${RUNFILES%org_tensorflow}/com_google_protobuf/src/google |
| 114 | + for header in $(find protobuf -name \*.h); do |
| 115 | + mkdir -p "${TMPDIR}/sdk/include/google/$(dirname ${header})" |
| 116 | + cp -L "$header" "${TMPDIR}/sdk/include/google/$(dirname ${header})/" |
| 117 | + done |
| 118 | + popd |
| 119 | + cp -RL $RUNFILES/third_party/eigen3 ${TMPDIR}/sdk/include/third_party |
| 120 | + cp -RL ${RUNFILES%org_tensorflow}/eigen_archive/* ${TMPDIR}/sdk/include/ |
| 121 | + cp -RL ${RUNFILES%org_tensorflow}/nsync/public/* ${TMPDIR}/sdk/include |
| 122 | + cp -L ${RUNFILES%org_tensorflow}/com_google_protobuf/protoc ${TMPDIR}/sdk/bin |
| 123 | + |
| 124 | + # package all files into the target file. |
| 125 | + pushd ${TMPDIR} |
| 126 | + rm -f MANIFEST |
| 127 | + echo $(date) : "=== Building sdk package" |
| 128 | + tar czvf tensorflow_sdk.tar.gz sdk/ 1> /dev/null |
| 129 | + popd |
| 130 | + mkdir -p ${DEST} |
| 131 | + mv ${TMPDIR}/tensorflow_sdk.tar.gz ${DEST} |
| 132 | + rm -rf ${TMPDIR} |
| 133 | + echo $(date) : "=== Output sdk package file is: ${DEST}/tensorflow_sdk.tar.gz" |
| 134 | +} |
| 135 | + |
| 136 | +main "$@" |
0 commit comments