Home | History | Annotate | Download | only in builds
      1 #!/usr/bin/env bash
      2 # Copyright 2016 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 # Script to generate tarballs:
     18 # (1) The TensorFlow C-library: Containing C API header files and libtensorflow.so
     19 # (2) Native library for the TensorFlow Java API: Containing libtensorflow_jni.so
     20 # And jars:
     21 # (3) Java API .jar
     22 # (4) Java API sources .jar
     23 #
     24 # These binary distributions will allow use of TensorFlow in various languages
     25 # without having to compile the TensorFlow framework from sources, which takes
     26 # a while and also introduces many other dependencies.
     27 #
     28 # Usage:
     29 # - Source this file in another bash script
     30 # - Execute build_libtensorflow_tarball SUFFIX
     31 #
     32 # Produces:
     33 # - lib_package/libtensorflow${SUFFIX}.tar.gz
     34 # - lib_package/libtensorflow_jni${SUFFIX}.tar.gz
     35 # - lib_package/libtensorflow.jar
     36 # - lib_package/libtensorflow-src.jar
     37 # - lib_package/libtensorflow_proto.zip
     38 #
     39 # ASSUMPTIONS:
     40 # - build_libtensorflow_tarball is invoked from the root of the git tree.
     41 # - Any environment variables needed by the "configure" script have been set.
     42 
     43 function build_libtensorflow_tarball() {
     44   # Sanity check that this is being run from the root of the git repository.
     45   if [ ! -e "WORKSPACE" ]; then
     46     echo "Must run this from the root of the bazel workspace"
     47     exit 1
     48   fi
     49   # Delete any leftovers from previous builds in this workspace.
     50   DIR=lib_package
     51   rm -rf ${DIR}
     52 
     53   TARBALL_SUFFIX="${1}"
     54   BAZEL_OPTS="-c opt --cxxopt=-D_GLIBCXX_USE_CXX11_ABI=0"
     55   export CC_OPT_FLAGS='-mavx'
     56   if [ "${TF_NEED_CUDA}" == "1" ]; then
     57     BAZEL_OPTS="${BAZEL_OPTS} --config=cuda"
     58   fi
     59   bazel clean --expunge
     60   yes "" | ./configure
     61 
     62   # Remove this test call when
     63   # https://github.com/bazelbuild/bazel/issues/2352
     64   # and https://github.com/bazelbuild/bazel/issues/1580
     65   # have been resolved and the "manual" tags on the BUILD targets
     66   # in tensorflow/tools/lib_package/BUILD are removed.
     67   # Till then, must manually run the test since these tests are
     68   # not covered by the continuous integration.
     69   bazel test ${BAZEL_OPTS} \
     70     //tensorflow/tools/lib_package:libtensorflow_test \
     71     //tensorflow/tools/lib_package:libtensorflow_java_test
     72 
     73   bazel build ${BAZEL_OPTS} \
     74     //tensorflow/tools/lib_package:libtensorflow.tar.gz \
     75     //tensorflow/tools/lib_package:libtensorflow_jni.tar.gz \
     76     //tensorflow/java:libtensorflow.jar \
     77     //tensorflow/java:libtensorflow-src.jar \
     78     //tensorflow/tools/lib_package:libtensorflow_proto.zip
     79 
     80   mkdir -p ${DIR}
     81 
     82   cp bazel-bin/tensorflow/tools/lib_package/libtensorflow.tar.gz ${DIR}/libtensorflow${TARBALL_SUFFIX}.tar.gz
     83   cp bazel-bin/tensorflow/tools/lib_package/libtensorflow_jni.tar.gz ${DIR}/libtensorflow_jni${TARBALL_SUFFIX}.tar.gz
     84   cp bazel-bin/tensorflow/java/libtensorflow.jar ${DIR}
     85   cp_normalized_srcjar bazel-bin/tensorflow/java/libtensorflow-src.jar ${DIR}/libtensorflow-src.jar
     86   cp bazel-genfiles/tensorflow/tools/lib_package/libtensorflow_proto.zip ${DIR}
     87   chmod -x ${DIR}/*
     88 }
     89 
     90 # Helper function to copy a srcjar after moving any source files
     91 # directly under the root to the "maven-style" src/main/java layout
     92 #
     93 # Source files generated by annotation processors appear directly
     94 # under the root of srcjars jars created by bazel, rather than under
     95 # the maven-style src/main/java subdirectory.
     96 #
     97 # Bazel manages annotation generated source as follows: First, it
     98 # calls javac with options that create generated files under a
     99 # bazel-out directory. Next, it archives the generated source files
    100 # into a srcjar directly under the root. There doesn't appear to be a
    101 # simple way to parameterize this from bazel, hence this helper to
    102 # "normalize" the srcjar layout.
    103 #
    104 # Arguments:
    105 #   src_jar - path to the original srcjar
    106 #   dest_jar - path to the destination
    107 # Returns:
    108 #   None
    109 function cp_normalized_srcjar() {
    110   local src_jar="$1"
    111   local dest_jar="$2"
    112   if [[ -z "${src_jar}" || -z "${dest_jar}" ]]; then
    113     echo "Unexpected: missing arguments" >&2
    114     exit 2
    115   fi
    116   local tmp_dir
    117   tmp_dir=$(mktemp -d)
    118   cp "${src_jar}" "${tmp_dir}/orig.jar"
    119   pushd "${tmp_dir}"
    120   # Extract any src/ files
    121   jar -xf "${tmp_dir}/orig.jar" src/
    122   # Extract any org/ files under src/main/java
    123   (mkdir -p src/main/java && cd src/main/java && jar -xf "${tmp_dir}/orig.jar" org/)
    124   # Repackage src/
    125   jar -cMf "${tmp_dir}/new.jar" src
    126   popd
    127   cp "${tmp_dir}/new.jar" "${dest_jar}"
    128   rm -rf "${tmp_dir}"
    129 }
    130