Skip to content

Commit

Permalink
build: bazel 5.0.0 (#496)
Browse files Browse the repository at this point in the history
* build: use bazel 5.0.0

* build: configure Android at runtime

* build: pass api_level from build.py

* build: remove maven and unity dependencies

* build: use remote JDK

* build: remove JDK from Docker containers

* build: JDK is used internally

* wip: fix assembly file compilation error

* wip: workaround for C compilation errors

* allow >= 4.2.1
  • Loading branch information
homuler authored Mar 20, 2022
1 parent 4210673 commit b1c7b61
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 208 deletions.
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.1
5.0.0
81 changes: 5 additions & 76 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bazel_skylib_workspace()

load("@bazel_skylib//lib:versions.bzl", "versions")

versions.check(minimum_bazel_version = "3.7.2")
versions.check(minimum_bazel_version = "4.2.1")

http_archive(
name = "rules_pkg",
Expand Down Expand Up @@ -51,12 +51,6 @@ load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")

emsdk_emscripten_deps(emscripten_version = "2.0.22")

new_local_repository(
name = "unity",
build_file = "@//third_party:unity.BUILD",
path = "/path/to/unity/2020.3.30f1",
)

# mediapipe
http_archive(
name = "com_google_mediapipe",
Expand Down Expand Up @@ -323,18 +317,11 @@ http_archive(
urls = ["https://github.com/nothings/stb/archive/b42009b3b9d4ca35bc703f5310eedc74f584be58.tar.gz"],
)

# You may run setup_android.sh to install Android SDK and NDK.
android_ndk_repository(
name = "androidndk",
# If you need to support older versions of Android, please specify the API Level.
# Otherwise, some symbols in libmediapipe_jni.so cannot be resolved and `DllNotFoundException` will be thrown.
load("//third_party:android_configure.bzl", "android_configure")
android_configure(name = "local_config_android")

# api_level = 21,
)

android_sdk_repository(
name = "androidsdk",
)
load("@local_config_android//:android_configure.bzl", "android_workspace")
android_workspace()

# iOS basic build deps.

Expand Down Expand Up @@ -391,64 +378,6 @@ http_archive(
url = "https://github.com/google/google-toolbox-for-mac/archive/v2.2.1.zip",
)

# Maven dependencies.

RULES_JVM_EXTERNAL_TAG = "4.0"

RULES_JVM_EXTERNAL_SHA = "31701ad93dbfe544d597dbe62c9a1fdd76d81d8a9150c2bf1ecf928ecdf97169"

http_archive(
name = "rules_jvm_external",
sha256 = RULES_JVM_EXTERNAL_SHA,
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

# Important: there can only be one maven_install rule. Add new maven deps here.
maven_install(
artifacts = [
"androidx.concurrent:concurrent-futures:1.0.0-alpha03",
"androidx.lifecycle:lifecycle-common:2.3.1",
"androidx.activity:activity:1.2.2",
"androidx.exifinterface:exifinterface:1.3.3",
"androidx.fragment:fragment:1.3.4",
"androidx.annotation:annotation:aar:1.1.0",
"androidx.appcompat:appcompat:aar:1.1.0-rc01",
"androidx.camera:camera-core:1.0.0-beta10",
"androidx.camera:camera-camera2:1.0.0-beta10",
"androidx.camera:camera-lifecycle:1.0.0-beta10",
"androidx.constraintlayout:constraintlayout:aar:1.1.3",
"androidx.core:core:aar:1.1.0-rc03",
"androidx.legacy:legacy-support-v4:aar:1.0.0",
"androidx.recyclerview:recyclerview:aar:1.1.0-beta02",
"androidx.test.espresso:espresso-core:3.1.1",
"com.github.bumptech.glide:glide:4.11.0",
"com.google.android.material:material:aar:1.0.0-rc01",
"com.google.auto.value:auto-value:1.8.1",
"com.google.auto.value:auto-value-annotations:1.8.1",
"com.google.code.findbugs:jsr305:latest.release",
"com.google.android.datatransport:transport-api:3.0.0",
"com.google.android.datatransport:transport-backend-cct:3.1.0",
"com.google.android.datatransport:transport-runtime:3.1.0",
"com.google.flogger:flogger-system-backend:0.6",
"com.google.flogger:flogger:0.6",
"com.google.guava:guava:27.0.1-android",
"com.google.guava:listenablefuture:1.0",
"junit:junit:4.12",
"org.hamcrest:hamcrest-library:1.3",
],
fetch_sources = True,
repositories = [
"https://maven.google.com",
"https://dl.google.com/dl/android/maven2",
"https://repo1.maven.org/maven2",
"https://jcenter.bintray.com",
],
version_conflict_policy = "pinned",
)

# Needed by TensorFlow
http_archive(
name = "io_bazel_rules_closure",
Expand Down
6 changes: 6 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(self, command_args):
self.system = platform.system()
self.desktop = command_args.args.desktop
self.android = command_args.args.android
self.android_ndk_api_level = command_args.args.android_ndk_api_level
self.ios= command_args.args.ios
self.resources = command_args.args.resources
self.analyzers = command_args.args.analyzers
Expand Down Expand Up @@ -198,6 +199,9 @@ def _build_common_commands(self):
commands += ['build', '-c', self.compilation_mode]
commands += self._build_linkopt()

if self.android_ndk_api_level:
commands += ['--action_env', f'ANDROID_NDK_API_LEVEL="{self.android_ndk_api_level}"']

if self._is_windows():
python_bin_path = sys.executable.replace('\\', '//')
commands += ['--action_env', f'PYTHON_BIN_PATH="{python_bin_path}"']
Expand Down Expand Up @@ -282,6 +286,7 @@ def _build_android_commands(self):

commands = self._build_common_commands()
commands.append(f'--config=android_{self.android}')
commands.append(f'--java_runtime_version=remotejdk_11')
commands.append('//mediapipe_api/java/com/github/homuler/mediapipe:mediapipe_android')
return commands

Expand Down Expand Up @@ -410,6 +415,7 @@ def __init__(self):
build_command_parser = subparsers.add_parser('build', help='Build and install native libraries')
build_command_parser.add_argument('--desktop', choices=['cpu', 'gpu'])
build_command_parser.add_argument('--android', choices=['armv7', 'arm64', 'fat'])
build_command_parser.add_argument('--android_ndk_api_level', type=int, choices=range(16, 31))
build_command_parser.add_argument('--ios', choices=['arm64'])
build_command_parser.add_argument('--resources', action=argparse.BooleanOptionalAction, default=True)
build_command_parser.add_argument('--analyzers', action=argparse.BooleanOptionalAction, default=False, help='Install Roslyn Analyzers')
Expand Down
8 changes: 2 additions & 6 deletions docker/windows/x86_64/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# escape=`

ARG NODE_VERSION=16.13.0
ARG BAZEL_VERSION=4.2.1
ARG PYTHON_VERSION=3.9.9
ARG ANDROID_SDK_VERSION=30
ARG ANDROID_NDK_VERSION=21.4.7075529
Expand Down Expand Up @@ -106,24 +105,21 @@ RUN powershell -command "for($i=0;$i -lt 30;$i++) { $response += """y`n""" }; $r
FROM builder

ARG ANDROID_NDK_VERSION
ARG BAZEL_VERSION

ENV ANDROID_HOME C:\Android
ENV ANDROID_NDK_HOME ${ANDROID_HOME}\ndk\${ANDROID_NDK_VERSION}
ENV JAVA_HOME=C:\Java\jdk-11.0.13+8
ENV PYTHON_INSTALL_PATH=C:\Python
ENV PYTHON_BIN_PATH=${PYTHON_INSTALL_PATH}\python.exe

# Install Bazel and NuGet
RUN setx path "C:\bin;%PYTHON_INSTALL_PATH%;%PYTHON_INSTALL_PATH%\Scripts;%JAVA_HOME%\bin;%PATH%" && `
RUN setx path "C:\bin;%PYTHON_INSTALL_PATH%;%PYTHON_INSTALL_PATH%\Scripts;%PATH%" && `
mkdir C:\bin && `
curl -L https://github.com/bazelbuild/bazel/releases/download/%BAZEL_VERSION%/bazel-%BAZEL_VERSION%-windows-x86_64.exe -o C:\bin\bazel.exe && `
curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.11.0/bazelisk-windows-amd64.exe -o C:\bin\bazel.exe && `
curl -L https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -o C:\bin\nuget.exe

COPY --from=python C:\Python C:\Python
COPY --from=opencv C:\opencv C:\opencv
COPY --from=android C:\Android C:\Android
COPY --from=android C:\Java C:\Java

ENV WS_LONG_PATHS_VERSION 3.4.1
ENV WS_LONG_PATHS_BASE ndk-wsls-${WS_LONG_PATHS_VERSION}
Expand Down
5 changes: 1 addition & 4 deletions mediapipe_api/java/com/github/homuler/mediapipe/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ load("//mediapipe_api/java/com/google/mediapipe:mediapipe_aar.bzl", "mediapipe_a
# A sample aar to extend UnityPlayerActivity
mediapipe_aar(
name = "mediapipe_android",
srcs = [
# "MediaPipeUnityPlayerActivity.java",
],
srcs = [],
assets = [
# If preferable, model files can be included to aar
],
jni_deps = [
"//mediapipe_api:mediapipe_c",
],
deps = [
# "@unity//:activity",
"@opencv",
],
)
64 changes: 14 additions & 50 deletions mediapipe_api/java/com/google/mediapipe/mediapipe_aar.bzl
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
# Copyright 2019-2020 The MediaPipe Authors.
# Copyright (c) 2019 Adam Michael
# Copyright (c) 2021 homuler
#
# 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.
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

# CHANGES:
#
# - remove the usage comment
# - package name can be specified
# - add `srcs` option
# - add `deps` option
# - remove `calculators` option
# - not to generate *Proto.java
# - .so files are placed under lib/
# Based on https://github.com/aj-michael/aar_with_jni/blob/b284343108b1e4cbfdbbf155bbcfa0b06e878f79/aar_with_jni.bzl

"""AAR Generator
Expand All @@ -29,7 +14,7 @@ Macro to generate AAR, including libmediapipe_jni.so

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

def mediapipe_aar(name, package = "com.github.homuler.mediapipe", srcs = [], deps = [], jni_deps = [], assets = [], assets_dir = ""):
def mediapipe_aar(name, package = "com.github.homuler.mediapipe", srcs = [], deps = [], jni_deps = [], assets = [], assets_dir = "", target_sdk_version = 27, min_sdk_version = 21):
"""Generate MediaPipeUnityPlugin AAR.
Args:
Expand All @@ -40,14 +25,14 @@ def mediapipe_aar(name, package = "com.github.homuler.mediapipe", srcs = [], dep
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
target_sdk_version: AAR's target SDK version
min_sdk_version: AAR's min SDK version
"""
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,
deps = jni_deps,
)

native.cc_library(
Expand All @@ -65,41 +50,20 @@ cat > $(OUTS) <<EOF
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="{}">
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="27" />
android:minSdkVersion="{}"
android:targetSdkVersion="{}" />
<application />
</manifest>
EOF
""".format(package),
""".format(package, min_sdk_version, target_sdk_version),
)

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",
] + srcs,
srcs = 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",
"@com_google_protobuf//:protobuf_javalite",
"//third_party:androidx_annotation",
"//third_party:androidx_appcompat",
"//third_party:androidx_core",
"//third_party:androidx_legacy_support_v4",
"//third_party:camerax_core",
"//third_party:camerax_camera2",
"//third_party:camerax_lifecycle",
"@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,
Expand Down
70 changes: 0 additions & 70 deletions third_party/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -70,73 +70,3 @@ cc_library(
],
}),
)

android_library(
name = "androidx_annotation",
exports = [
"@maven//:androidx_annotation_annotation",
],
)

android_library(
name = "androidx_appcompat",
exports = [
"@maven//:androidx_appcompat_appcompat",
],
)

android_library(
name = "androidx_constraint_layout",
exports = [
"@maven//:androidx_constraintlayout_constraintlayout",
],
)

android_library(
name = "androidx_core",
exports = [
"@maven//:androidx_core_core",
],
)

android_library(
name = "androidx_legacy_support_v4",
exports = [
"@maven//:androidx_legacy_legacy_support_v4",
],
)

android_library(
name = "androidx_material",
exports = [
"@maven//:com_google_android_material_material",
],
)

android_library(
name = "androidx_recyclerview",
exports = [
"@maven//:androidx_recyclerview_recyclerview",
],
)

android_library(
name = "camerax_camera2",
exports = [
"@maven//:androidx_camera_camera_camera2",
],
)

android_library(
name = "camerax_core",
exports = [
"@maven//:androidx_camera_camera_core",
],
)

android_library(
name = "camerax_lifecycle",
exports = [
"@maven//:androidx_camera_camera_lifecycle",
],
)
Loading

0 comments on commit b1c7b61

Please sign in to comment.