• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
43function 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  if [ "$(uname)" == "Darwin" ]; then
55    BAZEL_OPTS="${BAZEL_OPTS} --config=release_cpu_macos"
56  elif [ "${TF_NEED_CUDA}" == "1" ]; then
57    BAZEL_OPTS="${BAZEL_OPTS} --config=release_gpu_linux"
58  else
59    BAZEL_OPTS="${BAZEL_OPTS} --config=release_cpu_linux"
60  fi
61  export PYTHON_BIN_PATH="$(which python3.8)"
62  BAZEL_OPTS="${BAZEL_OPTS} --action_env=PYTHON_BIN_PATH=${PYTHON_BIN_PATH}"
63
64  # Remove this test call when
65  # https://github.com/bazelbuild/bazel/issues/2352
66  # and https://github.com/bazelbuild/bazel/issues/1580
67  # have been resolved and the "manual" tags on the BUILD targets
68  # in tensorflow/tools/lib_package/BUILD are removed.
69  # Till then, must manually run the test since these tests are
70  # not covered by the continuous integration.
71  bazel test ${BAZEL_OPTS} --test_output=errors \
72    //tensorflow/tools/lib_package:libtensorflow_test \
73    //tensorflow/tools/lib_package:libtensorflow_java_test
74
75  bazel build ${BAZEL_OPTS} \
76    //tensorflow/tools/lib_package:libtensorflow.tar.gz \
77    //tensorflow/tools/lib_package:libtensorflow_jni.tar.gz \
78    //tensorflow/java:libtensorflow.jar \
79    //tensorflow/java:libtensorflow-src.jar \
80    //tensorflow/tools/lib_package:libtensorflow_proto.zip
81
82  mkdir -p ${DIR}
83
84  cp bazel-bin/tensorflow/tools/lib_package/libtensorflow.tar.gz ${DIR}/libtensorflow${TARBALL_SUFFIX}.tar.gz
85  cp bazel-bin/tensorflow/tools/lib_package/libtensorflow_jni.tar.gz ${DIR}/libtensorflow_jni${TARBALL_SUFFIX}.tar.gz
86  cp bazel-bin/tensorflow/java/libtensorflow.jar ${DIR}
87  cp_normalized_srcjar bazel-bin/tensorflow/java/libtensorflow-src.jar ${DIR}/libtensorflow-src.jar
88  cp bazel-bin/tensorflow/tools/lib_package/libtensorflow_proto.zip ${DIR}
89  chmod -x ${DIR}/*
90}
91
92# Helper function to copy a srcjar after moving any source files
93# directly under the root to the "maven-style" src/main/java layout
94#
95# Source files generated by annotation processors appear directly
96# under the root of srcjars jars created by bazel, rather than under
97# the maven-style src/main/java subdirectory.
98#
99# Bazel manages annotation generated source as follows: First, it
100# calls javac with options that create generated files under a
101# bazel-out directory. Next, it archives the generated source files
102# into a srcjar directly under the root. There doesn't appear to be a
103# simple way to parameterize this from bazel, hence this helper to
104# "normalize" the srcjar layout.
105#
106# Arguments:
107#   src_jar - path to the original srcjar
108#   dest_jar - path to the destination
109# Returns:
110#   None
111function cp_normalized_srcjar() {
112  local src_jar="$1"
113  local dest_jar="$2"
114  if [[ -z "${src_jar}" || -z "${dest_jar}" ]]; then
115    echo "Unexpected: missing arguments" >&2
116    exit 2
117  fi
118  local tmp_dir
119  tmp_dir=$(mktemp -d)
120  cp "${src_jar}" "${tmp_dir}/orig.jar"
121  pushd "${tmp_dir}"
122  # Extract any src/ files
123  jar -xf "${tmp_dir}/orig.jar" src/
124  # Extract any org/ files under src/main/java
125  (mkdir -p src/main/java && cd src/main/java && jar -xf "${tmp_dir}/orig.jar" org/)
126  # Repackage src/
127  jar -cMf "${tmp_dir}/new.jar" src
128  popd
129  cp "${tmp_dir}/new.jar" "${dest_jar}"
130  rm -rf "${tmp_dir}"
131}
132