Skip to content

Commit

Permalink
#1002: Switch to bzlmod for Android build
Browse files Browse the repository at this point in the history
This is a follow up to our previous commits [1][2], which enabled bzlmod
by default except for Android. With this commit, Android build actually
starts only bzlmod mode.

Basically the idea is to use transition [3] to specify required CPU and
platforms for each libmozc.so target. A bonus is that we no longer need
to rely on rules_android.

 [1]: e54fbcb
 [2]: 699c2ce
 [3]: https://bazel.build/rules/lib/builtins/transition

PiperOrigin-RevId: 680441153
  • Loading branch information
yukawa authored and hiroyuki-komatsu committed Sep 30, 2024
1 parent e06fa8c commit 6c61c4f
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 287 deletions.
33 changes: 5 additions & 28 deletions src/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ common:oss_windows --config=windows_env --define TARGET=oss_windows
common:prod_windows --config=windows_env --define TARGET=prod_windows
common:prod_windows_dev --config=prod_windows --config=dev_channel

common:android --config=android_env --config=android_env_workspace --define TARGET=oss_android
common:oss_android --config=android_env --config=android_env_workspace --define TARGET=oss_android
common:prod_android --config=android_env --config=android_env_workspace --define TARGET=prod_android
common:oss_android_bzlmod --config=android_env --config=android_env_bzlmod --define TARGET=oss_android
common:android --config=android_env --define TARGET=oss_android
common:oss_android --config=android_env --define TARGET=oss_android
common:prod_android --config=android_env --define TARGET=prod_android

## Bzlmod options: Bzlmod is the default except for Android.
common:linux_env --config=bzlmod
common:macos_env --config=bzlmod
common:windows_env --config=bzlmod
common:android_env_bzlmod --config=bzlmod
common:android_env_workspace --config=workspace
common:android_env --config=bzlmod

common:bzlmod --enable_bzlmod
common:workspace --noenable_bzlmod
Expand Down Expand Up @@ -78,28 +76,7 @@ build:android_env --copt "-DOS_ANDROID"
build:android_env --build_tag_filters=-noandroid
test:android_env --test_tag_filters=-noandroid

## Android workspace specific options
build:android_env_workspace --android_crosstool_top=@androidndk//:toolchain
# --fat_apk_cpu should be replaced with --android_platforms
# https://github.com/bazelbuild/bazel/commit/05bea52ed3159aa5d15d967f5f56fc084a2b6c73
build:android_env_workspace --fat_apk_cpu=armeabi-v7a,arm64-v8a,x86,x86_64 --incompatible_enable_android_toolchain_resolution=false
build:android_env_workspace --host_crosstool_top=@bazel_tools//tools/cpp:toolchain

## Android Blzmod specific options
### --experimental_google_legacy_api is required for all platforms.
build:bzlmod --experimental_google_legacy_api

build:android_env_bzlmod --experimental_enable_android_migration_apis
build:android_env_bzlmod --android_sdk=@androidsdk//:sdk
### arm64-v8a build (Arm 64bit)
build:android_env_bzlmod --android_platforms=//bazel/android:arm64-v8a --cpu=arm64
### x86_64 build (x86 64bit)
# build:android_env_bzlmod --android_platforms=//bazel/android:x86_64 --cpu=x86_64
### armeabi-v7a build (Arm 32bit)
# build:android_env_bzlmod --android_platforms=//bazel/android:armeabi-v7a --cpu=armv7
### x86 build (x86 32bit)
# build:android_env_bzlmod --android_platforms=//bazel/android:x86 --cpu=x86_32

# Note, although --android_platforms supports multiple platforms,
# Abseil doesn't support it yet, and requires the --cpu option.
# As the result, it is not possible to build both cpus with a single command.
build:android_env --experimental_enable_android_migration_apis
23 changes: 0 additions & 23 deletions src/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,6 @@ android_ndk_repository_extension = use_extension(
use_repo(android_ndk_repository_extension, "androidndk")
register_toolchains("@androidndk//:all")

# Android SDK rules (0.5.1 2024-08-06)
# https://github.com/bazelbuild/rules_android
bazel_dep(
name = "rules_android",
version = "0.5.1",
)
remote_android_extensions = use_extension(
"@rules_android//bzlmod_extensions:android_extensions.bzl",
"remote_android_tools_extensions",
)
use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")
register_toolchains(
"@rules_android//toolchains/android:android_default_toolchain",
"@rules_android//toolchains/android_sdk:android_sdk_tools",
)
android_sdk_repository_extension = use_extension(
"@rules_android//rules/android_sdk_repository:rule.bzl",
"android_sdk_repository_extension",
)
use_repo(android_sdk_repository_extension, "androidsdk")
register_toolchains("@androidsdk//:sdk-toolchain", "@androidsdk//:all")


new_local_repository = use_repo_rule(
"@bazel_tools//tools/build_defs/repo:local.bzl",
"new_local_repository",
Expand Down
50 changes: 0 additions & 50 deletions src/android/jni/AndroidManifest.xml

This file was deleted.

130 changes: 95 additions & 35 deletions src/android/jni/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# TODO(matsuzakit): Move JNI unrelated targets to inputtools directory. It might
# be better to move this BUILD file to mozc/android directory as these targets
# are not limited to jni.
#
# Usage:
# % blaze --blazerc android/blazerc build --config=android_arm android/jni:libmozc.so

load("@rules_android//android:rules.bzl", "android_binary")
load(
"//:build_defs.bzl",
"MOZC_TAGS",
"mozc_cc_binary",
"mozc_cc_library",
"mozc_py_binary",
"mozc_select",
)
load(":cross_build_binary.bzl", "cross_build_binary")

_TARGET_COMPATIBLE_WITH = [
"@platforms//os:android",
]

mozc_cc_library(
name = "mozcjni",
Expand Down Expand Up @@ -70,43 +68,105 @@ mozc_cc_library(
alwayslink = 1,
)

# Use an empty "libpthread.a" is needed to fix link error.
# https://github.com/bazelbuild/rules_android_ndk/issues/5#issuecomment-1381282580
# To reproduce "libpthread.a", run the following command.
# echo -n -e "create libpthread.a\nsave\nend" | ar -M
mozc_cc_library(
name = "fake_pthread",
srcs = ["libpthread.a"],
linkopts = ["-L" + package_name()],
)

# To build libmozc.so, this target needs to be named as "mozc".
android_binary(
mozc_cc_binary(
name = "mozc",
srcs = [],
manifest = "AndroidManifest.xml",
deps = mozc_select(
android = [
":fake_pthread",
":mozcjni",
],
),
linkshared = 1,
tags = MOZC_TAGS.ANDROID_ONLY,
target_compatible_with = _TARGET_COMPATIBLE_WITH,
visibility = ["//visibility:private"],
deps = [
":mozcjni",
],
)

platform(
name = "armeabi-v7a",
constraint_values = [
"@platforms//cpu:armv7",
"@platforms//os:android",
],
)

platform(
name = "arm64-v8a",
constraint_values = [
"@platforms//cpu:arm64",
"@platforms//os:android",
],
)

platform(
name = "x86_64",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:android",
],
)

platform(
name = "x86_32",
constraint_values = [
"@platforms//cpu:x86_32",
"@platforms//os:android",
],
)

cross_build_binary(
name = "mozc.arm64",
cpu = "arm64-v8a",
platform = ":arm64-v8a",
tags = MOZC_TAGS.ANDROID_ONLY,
target = ":mozc",
)

cross_build_binary(
name = "mozc.arm32",
cpu = "armv7",
platform = ":armeabi-v7a",
tags = MOZC_TAGS.ANDROID_ONLY,
target = ":mozc",
)

cross_build_binary(
name = "mozc.x86_64",
cpu = "x86_64",
platform = ":x86_64",
tags = MOZC_TAGS.ANDROID_ONLY,
target = ":mozc",
)

cross_build_binary(
name = "mozc.x86_32",
cpu = "x86_32",
platform = ":x86_32",
tags = MOZC_TAGS.ANDROID_ONLY,
target = ":mozc",
)

mozc_py_binary(
name = "extract_native_libs",
srcs = ["extract_native_libs.py"],
name = "make_archive",
srcs = ["make_archive.py"],
)

genrule(
name = "native_libs",
srcs = [
# See the following document about where '_unsigned.apk' comes from.
# https://bazel.build/reference/be/android#android_binary_implicit_outputs
":mozc_unsigned.apk",
":mozc.arm64",
":mozc.arm32",
":mozc.x86_64",
":mozc.x86_32",
],
outs = ["native_libs.zip"],
cmd = "$(location :extract_native_libs) --input $(SRCS) --output $@",
tools = [":extract_native_libs"],
visibility = ["//:__pkg__"],
cmd = " ".join([
"$(location :make_archive)",
"--arm64=$(location :mozc.arm64)",
"--arm32=$(location :mozc.arm32)",
"--x86_64=$(location :mozc.x86_64)",
"--x86_32=$(location :mozc.x86_32)",
"--output $@",
]),
tags = MOZC_TAGS.ANDROID_ONLY,
tools = [":make_archive"],
visibility = ["//:__subpackages__"],
)
98 changes: 98 additions & 0 deletions src/android/jni/cross_build_binary.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright 2010-2021, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Define a macro to cross build a target."""

load("//:config.bzl", "BAZEL_TOOLS_PREFIX")

def _cross_build_binary_transition_impl(
settings, # @unused
attr):
return [{
"//command_line_option:platforms": str(attr.platform),
"//command_line_option:cpu": attr.cpu,
}]

_cross_build_binary_transition = transition(
implementation = _cross_build_binary_transition_impl,
inputs = [],
outputs = ["//command_line_option:platforms", "//command_line_option:cpu"],
)

def _cross_build_binary_impl(ctx):
return DefaultInfo(files = depset(ctx.files.target))

_cross_build_binary = rule(
implementation = _cross_build_binary_impl,
attrs = {
"cpu": attr.string(),
"target": attr.label(cfg = _cross_build_binary_transition),
"platform": attr.label(),
"_allowlist_function_transition": attr.label(
default = BAZEL_TOOLS_PREFIX + "//tools/allowlists/function_transition_allowlist",
),
},
)

def cross_build_binary(
name,
target,
cpu,
platform,
target_compatible_with = [],
tags = [],
**kwargs):
"""Define a transition target with the given platform and CPU.
cross_build_binary(
name = "my_target",
target = "//path/to/target:my_target",
platform = ":my_platform",
cpu = "arm64-v8a",
)
Args:
name: name of the target.
target: the actual Bazel target to be built with the specified configurations.
cpu: CPU type of the target.
platform: the platform name to be used to build target.
target_compatible_with: optional. Visibility for the unit test target.
tags: optional. Tags for both the library and unit test targets.
**kwargs: other arguments passed to mozc_objc_library.
"""

_cross_build_binary(
name = name,
target = target,
cpu = cpu,
platform = platform,
target_compatible_with = target_compatible_with,
tags = tags,
**kwargs
)
1 change: 0 additions & 1 deletion src/android/jni/libpthread.a

This file was deleted.

Loading

0 comments on commit 6c61c4f

Please sign in to comment.