Skip to content

Commit

Permalink
Add third party tools for java and android build.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 315088594
  • Loading branch information
xunkai55 authored and tflite-support-robot committed Jun 9, 2020
1 parent 1f0609d commit 77254a0
Show file tree
Hide file tree
Showing 14 changed files with 497 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This file is based on tensorflow's (v2.2.0) .bazelrc found here:
# https://github.com/tensorflow/tensorflow/blob/v2.2.0/.bazelrc

# Sets the default Apple platform to macOS.

build --apple_platform_type=macos

# Enable using platform specific build settings
build --enable_platform_specific_config

# Flag to enable remote config. Required starting from TF 2.2.
common --experimental_repo_remote_exec

# For workaround https://github.com/bazelbuild/bazel/issues/8772 with Bazel >= 0.29.1
build --java_toolchain=//third_party/toolchains/java:tf_java_toolchain
build --host_java_toolchain=//third_party/toolchains/java:tf_java_toolchain

# Suppress C++ compiler warnings, otherwise build logs become 10s of MBs.
build:android --copt=-w
build:linux --copt=-w
build:macos --copt=-w
build:windows --copt=/w

# Android workspace configurations. Should be replaced by an interative configure in the future.
build --action_env ANDROID_NDK_HOME
build --action_env ANDROID_NDK_API_LEVEL
build --action_env ANDROID_BUILD_TOOLS_VERSION
build --action_env ANDROID_SDK_API_LEVEL
build --action_env ANDROID_SDK_HOME

# Android configs. Bazel needs to have --cpu and --fat_apk_cpu both set to the
# target CPU to build transient dependencies correctly. See
# https://docs.bazel.build/versions/master/user-manual.html#flag--fat_apk_cpu

build:android --crosstool_top=//external:android/crosstool
build:android --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
build:android_arm --config=android
build:android_arm --cpu=armeabi-v7a
build:android_arm --fat_apk_cpu=armeabi-v7a
build:android_arm64 --config=android
build:android_arm64 --cpu=arm64-v8a
build:android_arm64 --fat_apk_cpu=arm64-v8a
build:android_x86 --config=android
build:android_x86 --cpu=x86
build:android_x86 --fat_apk_cpu=x86
build:android_x86_64 --config=android
build:android_x86_64 --cpu=x86_64
build:android_x86_64 --fat_apk_cpu=x86_64

# By default, build TF in C++ 14 mode.
build:android --cxxopt=-std=c++14
build:android --host_cxxopt=-std=c++14
build:ios --cxxopt=-std=c++14
build:ios --host_cxxopt=-std=c++14
build:linux --cxxopt=-std=c++14
build:linux --host_cxxopt=-std=c++14
build:macos --cxxopt=-std=c++14
build:macos --host_cxxopt=-std=c++14
build:windows --cxxopt=/std:c++14
build:windows --host_cxxopt=/std:c++14

# Config to use a mostly-static build and disable modular op registration
# support (this will revert to loading TensorFlow with RTLD_GLOBAL in Python).
# By default, TensorFlow will build with a dependence on
# //tensorflow:libtensorflow_framework.so.
build:monolithic --define framework_shared_object=false
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ TFLite Support targets at:
structures to perform pre/post processing and data conversion. It is also
designed to match the behavior of TensorFlow modules, such as TF.Image and
TF.text, ensuring consistency from training to inferencing.

## Build Instructions

We use Bazel to build the project. When you're building the Java (Android)
Utils, you need set up following env variables correctly:

* `ANDROID_NDK_HOME`
* `ANDROID_SDK_HOME`
* `ANDROID_NDK_API_LEVEL`
* `ANDROID_SDK_API_LEVEL`
* `ANDROID_BUILD_TOOLS_VERSION`
41 changes: 41 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
workspace(name = "org_tensorflow_lite_support")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_closure",
sha256 = "5b00383d08dd71f28503736db0500b6fb4dda47489ff5fc6bed42557c07c6ba9",
strip_prefix = "rules_closure-308b05b2419edb5c8ee0471b67a40403df940149",
urls = [
"https://storage.googleapis.com/mirror.tensorflow.org/github.com/bazelbuild/rules_closure/archive/308b05b2419edb5c8ee0471b67a40403df940149.tar.gz",
"https://github.com/bazelbuild/rules_closure/archive/308b05b2419edb5c8ee0471b67a40403df940149.tar.gz", # 2019-06-13
],
)

http_archive(
name = "org_tensorflow",
strip_prefix = "tensorflow-2.2.0",
sha256 = "fd3e6580cfe2035aa80d569b76bba5f33119362907f3d77039b6bedf76172712",
urls = [
"https://github.com/tensorflow/tensorflow/archive/v2.2.0.zip"
],
)

# Configure TF.
load("@org_tensorflow//tensorflow:workspace.bzl", "tf_workspace")

tf_workspace(tf_repo_name="@org_tensorflow")

load("//third_party/tensorflow:tf_configure.bzl", "tf_configure")

tf_configure(name = "local_config_tf")


# Configure Android.
load("//third_party/android:android_configure.bzl", "android_configure")

android_configure(name="local_config_android")

load("@local_config_android//:android.bzl", "android_workspace")

android_workspace()
5 changes: 5 additions & 0 deletions tensorflow_lite_support/java/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.tensorflow.lite.support">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="29"/>
</manifest>
18 changes: 18 additions & 0 deletions tensorflow_lite_support/java/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Description:
# TensorFlow Lite Support API in Java.

load("@build_bazel_rules_android//android:rules.bzl", "android_library")

package(
default_visibility = ["//visibility:public"],
licenses = ["notice"], # Apache 2.0
)

android_library(
name = "tensorflow-lite-support",
srcs = [],
manifest = "AndroidManifest.xml",
deps = [
"@org_tensorflow//tensorflow/lite/java:tensorflowlite",
],
)
6 changes: 6 additions & 0 deletions tensorflow_lite_support/opensource/opensource_only.files
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tensorflow_lite_support/opensource/WORKSPACE
tensorflow_lite_support/third_party/android/BUILD
tensorflow_lite_support/third_party/android/android.bzl.tpl
tensorflow_lite_support/third_party/android/android_configure.BUILD.tpl
tensorflow_lite_support/third_party/android/android_configure.bzl
tensorflow_lite_support/third_party/toolchains/java/BUILD
1 change: 1 addition & 0 deletions third_party/android/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Placeholder to make bazel treat it as a package.
9 changes: 9 additions & 0 deletions third_party/android/android.bzl.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Set up configurable Android SDK and NDK dependencies."""

def android_workspace():
# String for replacement in Bazel template.
# These will either be replaced by android_sdk_repository if various ENV
# variables are set when `local_config_android` repo_rule is run, or they
# will be replaced by noops otherwise.
MAYBE_ANDROID_SDK_REPOSITORY
MAYBE_ANDROID_NDK_REPOSITORY
Empty file.
95 changes: 95 additions & 0 deletions third_party/android/android_configure.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""Repository rule for Android SDK and NDK autoconfiguration.
`android_configure` depends on the following environment variables:
* `ANDROID_NDK_HOME`: Location of Android NDK root.
* `ANDROID_SDK_HOME`: Location of Android SDK root.
* `ANDROID_SDK_API_LEVEL`: Desired Android SDK API version.
* `ANDROID_NDK_API_LEVEL`: Desired Android NDK API version.
* `ANDROID_BUILD_TOOLS_VERSION`: Desired Android build tools version.
Writes Android SDK and NDK rules.
Add the following to your WORKSPACE FILE:
```python
android_configure(name = "local_config_android")
```
Args:
name: A unique name for this workspace rule.
"""

_ANDROID_NDK_HOME = "ANDROID_NDK_HOME"
_ANDROID_SDK_HOME = "ANDROID_SDK_HOME"
_ANDROID_NDK_API_VERSION = "ANDROID_NDK_API_LEVEL"
_ANDROID_SDK_API_VERSION = "ANDROID_SDK_API_LEVEL"
_ANDROID_BUILD_TOOLS_VERSION = "ANDROID_BUILD_TOOLS_VERSION"

_ANDROID_SDK_REPO_TEMPLATE = """
native.android_sdk_repository(
name="androidsdk",
path="%s",
api_level=%s,
build_tools_version="%s",
)
"""

_ANDROID_NDK_REPO_TEMPLATE = """
native.android_ndk_repository(
name="androidndk",
path="%s",
api_level=%s,
)
"""

def _android_autoconf_impl(repository_ctx):
"""Implementation of the android_autoconf repository rule."""
sdk_home = repository_ctx.os.environ.get(_ANDROID_SDK_HOME)
sdk_api_level = repository_ctx.os.environ.get(_ANDROID_SDK_API_VERSION)
build_tools_version = repository_ctx.os.environ.get(
_ANDROID_BUILD_TOOLS_VERSION,
)
ndk_home = repository_ctx.os.environ.get(_ANDROID_NDK_HOME)
ndk_api_level = repository_ctx.os.environ.get(_ANDROID_NDK_API_VERSION)

sdk_rule = ""
if all([sdk_home, sdk_api_level, build_tools_version]):
sdk_rule = _ANDROID_SDK_REPO_TEMPLATE % (
sdk_home,
sdk_api_level,
build_tools_version,
)

ndk_rule = ""
if all([ndk_home, ndk_api_level]):
ndk_rule = _ANDROID_NDK_REPO_TEMPLATE % (ndk_home, ndk_api_level)

if ndk_rule == "" and sdk_rule == "":
sdk_rule = "pass"
# TODO(xunkai): Add interactive configure script.

repository_ctx.template(
"BUILD",
Label("//third_party/android:android_configure.BUILD.tpl"),
)
repository_ctx.template(
"android.bzl",
Label("//third_party/android:android.bzl.tpl"),
substitutions = {
"MAYBE_ANDROID_SDK_REPOSITORY": sdk_rule,
"MAYBE_ANDROID_NDK_REPOSITORY": ndk_rule,
},
)

android_configure = repository_rule(
implementation = _android_autoconf_impl,
environ = [
_ANDROID_SDK_API_VERSION,
_ANDROID_NDK_API_VERSION,
_ANDROID_BUILD_TOOLS_VERSION,
_ANDROID_NDK_HOME,
_ANDROID_SDK_HOME,
],
)
1 change: 1 addition & 0 deletions third_party/tensorflow/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# placeholder to make the directory a bazel package.
18 changes: 18 additions & 0 deletions third_party/tensorflow/BUILD.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "tf_header_lib",
hdrs = [":tf_header_include"],
includes = ["include"],
visibility = ["//visibility:public"],
)

cc_library(
name = "libtensorflow_framework",
srcs = [":libtensorflow_framework_so"],
visibility = ["//visibility:public"],
)

%{TF_HEADER_GENRULE}
%{TF_SHARED_LIBRARY_GENRULE}

Loading

0 comments on commit 77254a0

Please sign in to comment.