Skip to content

Commit

Permalink
feat: build aar
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Oct 5, 2020
1 parent e41ce14 commit cc6879b
Show file tree
Hide file tree
Showing 12 changed files with 462 additions and 15 deletions.
21 changes: 21 additions & 0 deletions C/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,24 @@ build --enable_platform_specific_config
build:linux --cxxopt=-std=c++14
build:linux --host_cxxopt=-std=c++14
build:linux --copt=-w

# Android configs.
# Note: the documentation tells us to use @androidndk//:default_crosstool, but
# the automatic configuration transition uses //external:android/crosstool.
# Using it here works and spares us from having two different config_settings
# for Android.
build:android --crosstool_top=//external:android/crosstool
build:android --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
build:android --linkopt=-landroid
build:android --linkopt=-ldl
build:android --linkopt=-llog
build:android --linkopt=-lm
build:android --linkopt=-Wl,--gc-sections

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
3 changes: 2 additions & 1 deletion C/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ workspace(name = "mediapipe_api")
#
# - com_google_mediapipe dependency
# - @//third_party -> @com_google_mediapipe//third_party
# - exception: opencv_android, opencv_linux
# - exception: opencv_linux
# - android_opencv sha256 is added
# - unity dependency

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

Expand Down
12 changes: 10 additions & 2 deletions C/mediapipe_api/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_pkg//:pkg.bzl", "pkg_zip")

cc_binary(
cc_library(
name = "mediapipe_c",
deps = [
":calculators",
Expand All @@ -22,8 +22,16 @@ cc_binary(
"//mediapipe_api/gpu:gpu_shared_data_internal",
],
}),
visibility = None,
visibility = ["//visibility:public"],
alwayslink = True,
)

cc_binary(
name = "libmediapipe_c.so",
deps = [":mediapipe_c"],
linkshared = True,
linkstatic = True,
visibility = ["//visibility:public"],
)

# It is recommended that you include only neccessary calculators.
Expand Down
36 changes: 36 additions & 0 deletions C/mediapipe_api/java/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
android_library(
name = "mediapipe_android",
manifest = "AndroidManifest.xml",
assets = [
"@com_google_mediapipe//mediapipe/models:face_detection_front.tflite",
"@com_google_mediapipe//mediapipe/models:face_detection_front_labelmap.txt",
"@com_google_mediapipe//mediapipe/models:face_landmark.tflite",
"@com_google_mediapipe//mediapipe/models:hair_segmentation.tflite",
"@com_google_mediapipe//mediapipe/models:hand_landmark.tflite",
"@com_google_mediapipe//mediapipe/models:handedness.txt",
"@com_google_mediapipe//mediapipe/models:iris_landmark.tflite",
"@com_google_mediapipe//mediapipe/models:palm_detection.tflite",
"@com_google_mediapipe//mediapipe/models:palm_detection_labelmap.txt",
"@com_google_mediapipe//mediapipe/models:ssdlite_object_detection.tflite",
"@com_google_mediapipe//mediapipe/models:ssdlite_object_detection_labelmap.txt",
"@com_google_mediapipe//mediapipe/modules/pose_detection:pose_detection.tflite",
"@com_google_mediapipe//mediapipe/modules/pose_landmark:pose_landmark_upper_body.tflite",
],
assets_dir = "",
visibility = ["//visibility:public"],
deps = [
"@com_google_mediapipe//mediapipe/java/com/google/mediapipe/framework:android_framework",
"@com_google_mediapipe//mediapipe/java/com/google/mediapipe/glutil",
"//third_party:opencv",
],
)

android_binary(
name = "helloworld",
manifest = "AndroidManifest.xml",
multidex = "native",
deps = [
":mediapipe_android",
"//mediapipe_api:mediapipe_test",
],
)
1 change: 1 addition & 0 deletions C/mediapipe_api/java/com/google/mediapipe/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
licenses(["notice"])
168 changes: 168 additions & 0 deletions C/mediapipe_api/java/com/google/mediapipe/mediapipe_aar.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Copyright 2019-2020 The MediaPipe Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# CHANGES:
#
# - remove the usage comment
# - package name can be specified
# - add `srcs` option
# - add `deps` option
# - remove `calculators` option
# - add a `--proto_path` option
# - not to generate *Proto.java other than CalculatorProto.java
# - .so files are placed under lib/

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

def mediapipe_aar(name, package = "org.homuler.mediapipe.unity", srcs = [], deps = [], jni_deps = [], assets = [], assets_dir = ""):
"""Generate MediaPipeUnityPlugin AAR.
Args:
name: the name of the AAR.
srcs: java source files
deps: aar's dependencies (e.g. .so files)
jni_deps: additional dependencies that will be linked to libmediapipe_jni.so
assets: additional assets to be included into the archive.
assets_dir: path where the assets will the packaged.
"""
native.cc_binary(
name = "libmediapipe_jni.so",
linkshared = 1,
linkstatic = 1,
deps = [
"@com_google_mediapipe//mediapipe/java/com/google/mediapipe/framework/jni:mediapipe_framework_jni",
] + jni_deps,
)

native.cc_library(
name = name + "_mediapipe_jni_lib",
srcs = [":libmediapipe_jni.so"],
alwayslink = 1,
)

native.genrule(
name = name + "_aar_manifest_generator",
outs = ["AndroidManifest.xml"],
cmd = """
cat > $(OUTS) <<EOF
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="{}">
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="27" />
<application />
</manifest>
""".format(package),
)

_proto_java_src_generator(
name = "calculator_proto",
proto_src = "mediapipe/framework/calculator.proto",
java_lite_out = "com/google/mediapipe/proto/CalculatorProto.java",
srcs = ["@com_google_mediapipe//mediapipe/framework:protos_src"],
)

android_library(
name = name + "_android_lib",
srcs = [
"@com_google_mediapipe//mediapipe/java/com/google/mediapipe/components:java_src",
"@com_google_mediapipe//mediapipe/java/com/google/mediapipe/framework:java_src",
"@com_google_mediapipe//mediapipe/java/com/google/mediapipe/glutil:java_src",
"com/google/mediapipe/proto/CalculatorProto.java",
] + srcs,
manifest = "AndroidManifest.xml",
proguard_specs = ["@com_google_mediapipe//mediapipe/java/com/google/mediapipe/framework:proguard.pgcfg"],
deps = [
":" + name + "_mediapipe_jni_lib",
"@com_google_mediapipe//mediapipe/framework:calculator_java_proto_lite",
"@com_google_mediapipe//mediapipe/framework:calculator_profile_java_proto_lite",
"@com_google_mediapipe//mediapipe/framework/tool:calculator_graph_template_java_proto_lite",
"//third_party:androidx_annotation",
"//third_party:androidx_appcompat",
"//third_party:androidx_core",
"//third_party:androidx_legacy_support_v4",
"//third_party:camerax_core",
"//third_party:camera2",
"@maven//:com_google_code_findbugs_jsr305",
"@maven//:com_google_flogger_flogger",
"@maven//:com_google_flogger_flogger_system_backend",
"@maven//:com_google_guava_guava",
"@maven//:androidx_lifecycle_lifecycle_common",
] + deps,
assets = assets,
assets_dir = assets_dir,
)

_aar_with_jni(name, name + "_android_lib")

def _proto_java_src_generator(name, proto_src, java_lite_out, srcs = []):
native.genrule(
name = name,
srcs = srcs + [
"@com_google_protobuf//:well_known_protos",
],
outs = [java_lite_out],
cmd = "$(location @com_google_protobuf//:protoc) " +
"--proto_path=. --proto_path=$(GENDIR) " +
"--proto_path=$$(pwd)/external/com_google_protobuf/src " +
"--proto_path=$$(pwd)/external/com_google_mediapipe " +
"--java_out=lite:$(GENDIR) " + proto_src + " && " +
"mv $(GENDIR)/" + java_lite_out + " $$(dirname $(location " + java_lite_out + "))",
tools = [
"@com_google_protobuf//:protoc",
],
)

def _aar_with_jni(name, android_library):
# Generate dummy AndroidManifest.xml for dummy apk usage
# (dummy apk is generated by <name>_dummy_app target below)
native.genrule(
name = name + "_binary_manifest_generator",
outs = [name + "_generated_AndroidManifest.xml"],
cmd = """
cat > $(OUTS) <<EOF
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="dummy.package.for.so">
<uses-sdk android:minSdkVersion="21"/>
</manifest>
EOF
""",
)

# Generate dummy apk including .so files.
# We extract out .so files and throw away the apk.
android_binary(
name = name + "_dummy_app",
manifest = name + "_generated_AndroidManifest.xml",
custom_package = "dummy.package.for.so",
deps = [android_library],
)

native.genrule(
name = name,
srcs = [android_library + ".aar", name + "_dummy_app_unsigned.apk"],
outs = [name + ".aar"],
tags = ["manual"],
cmd = """
cp $(location {}.aar) $(location :{}.aar)
chmod +w $(location :{}.aar)
origdir=$$PWD
cd $$(mktemp -d)
unzip $$origdir/$(location :{}_dummy_app_unsigned.apk) "lib/*"
cp -r lib jni
zip -r $$origdir/$(location :{}.aar) jni/*/*.so
""".format(android_library, name, name, name, name),
)
30 changes: 30 additions & 0 deletions C/mediapipe_api/java/org/homuler/mediapipe/unity/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("//mediapipe_api/java/com/google/mediapipe:mediapipe_aar.bzl", "mediapipe_aar")

mediapipe_aar(
name = "mediapipe_android",
srcs = [
"MediaPipeUnityPlayerActivity.java",
],
deps = [
"@unity//:activity",
"//third_party:opencv",
],
jni_deps = [
"//mediapipe_api:mediapipe_c",
],
assets = [
"@com_google_mediapipe//mediapipe/models:face_detection_front.tflite",
"@com_google_mediapipe//mediapipe/models:face_detection_front_labelmap.txt",
"@com_google_mediapipe//mediapipe/models:face_landmark.tflite",
"@com_google_mediapipe//mediapipe/models:hair_segmentation.tflite",
"@com_google_mediapipe//mediapipe/models:hand_landmark.tflite",
"@com_google_mediapipe//mediapipe/models:handedness.txt",
"@com_google_mediapipe//mediapipe/models:iris_landmark.tflite",
"@com_google_mediapipe//mediapipe/models:palm_detection.tflite",
"@com_google_mediapipe//mediapipe/models:palm_detection_labelmap.txt",
"@com_google_mediapipe//mediapipe/models:ssdlite_object_detection.tflite",
"@com_google_mediapipe//mediapipe/models:ssdlite_object_detection_labelmap.txt",
"@com_google_mediapipe//mediapipe/modules/pose_detection:pose_detection.tflite",
"@com_google_mediapipe//mediapipe/modules/pose_landmark:pose_landmark_upper_body.tflite",
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.homuler.mediapipe.unity;

import android.os.Bundle;
import android.util.Log;
import com.google.mediapipe.framework.AndroidAssetUtil;
import com.unity3d.player.UnityPlayerActivity;

public class MediaPipeUnityPlayerActivity extends UnityPlayerActivity {
static {
// Load all native libraries needed by the app.
System.loadLibrary("mediapipe_jni");
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.d("MediaPipeUnityPlayerActivity", "onCreate called!");
// Initialize asset manager so that MediaPipe native libraries can access the app assets, e.g.,
// binary graphs.
boolean res = AndroidAssetUtil.initializeNativeAssetManager(this);
Log.d("MediaPipeUnityPalyerActivity", "Asset Manager Initialized: " + res);
}
}
4 changes: 2 additions & 2 deletions C/mediapipe_api/protobuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extern inline MpSerializedProto* MpSerializedProtoInitialize(const T& proto) {
auto bytes = new char[length];
memcpy(bytes, str.c_str(), length);

return new MpSerializedProto { bytes, length };
return new MpSerializedProto { bytes, static_cast<int>(length) };
}

template<class T>
Expand All @@ -58,7 +58,7 @@ extern inline MpSerializedProtoVector* MpSerializedProtoVectorInitialize(const s
data[i] = MpSerializedProtoInitialize(proto_vec[i]);
}

return new MpSerializedProtoVector { data, size };
return new MpSerializedProtoVector { data, static_cast<int>(size) };
}

#endif // C_MEDIAPIPE_API_PROTOBUF_H_
Loading

0 comments on commit cc6879b

Please sign in to comment.