diff --git a/src/.bazelrc b/src/.bazelrc index 2df6a7127..2f8a716a4 100644 --- a/src/.bazelrc +++ b/src/.bazelrc @@ -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 @@ -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 diff --git a/src/MODULE.bazel b/src/MODULE.bazel index e22a9acff..71b789fae 100644 --- a/src/MODULE.bazel +++ b/src/MODULE.bazel @@ -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", diff --git a/src/android/jni/AndroidManifest.xml b/src/android/jni/AndroidManifest.xml deleted file mode 100644 index dcb709e03..000000000 --- a/src/android/jni/AndroidManifest.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - diff --git a/src/android/jni/BUILD.bazel b/src/android/jni/BUILD.bazel index 4123a12a3..ba3268f95 100644 --- a/src/android/jni/BUILD.bazel +++ b/src/android/jni/BUILD.bazel @@ -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", @@ -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__"], ) diff --git a/src/android/jni/cross_build_binary.bzl b/src/android/jni/cross_build_binary.bzl new file mode 100644 index 000000000..4920c88dd --- /dev/null +++ b/src/android/jni/cross_build_binary.bzl @@ -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 + ) diff --git a/src/android/jni/libpthread.a b/src/android/jni/libpthread.a deleted file mode 100644 index 8b277f0dd..000000000 --- a/src/android/jni/libpthread.a +++ /dev/null @@ -1 +0,0 @@ -! diff --git a/src/android/jni/extract_native_libs.py b/src/android/jni/make_archive.py similarity index 66% rename from src/android/jni/extract_native_libs.py rename to src/android/jni/make_archive.py index fdbdfa422..529520ba3 100644 --- a/src/android/jni/extract_native_libs.py +++ b/src/android/jni/make_archive.py @@ -28,40 +28,41 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -"""A helper script to extract native libraries from an *.apk file.""" +"""Script to make a zip file of libmozc.so files for muchtiple archs.""" import argparse import pathlib -import zipfile - - -def extract_native_libs(apk: pathlib.Path, dest: pathlib.Path) -> None: - """Extract native libraries from an APK. - - Args: - apk: APK file from which native libraries will be extracted. - dest: ZIP file to which native libraries will be extracted. - """ - with zipfile.ZipFile(apk) as z: - with zipfile.ZipFile(dest, mode='w') as output: - for info in z.infolist(): - paths = info.filename.split('/') - if '..' in paths: - continue - if len(paths) < 1: - continue - if paths[0] != 'lib': - continue - output.writestr(info, z.read(info)) +import shutil +import tempfile def main(): parser = argparse.ArgumentParser() - parser.add_argument('--input', help='A path to APK', type=str) + parser.add_argument('--arm64', type=str) + parser.add_argument('--arm32', type=str) + parser.add_argument('--x86_32', type=str) + parser.add_argument('--x86_64', type=str) parser.add_argument('--output', help='A path to output ZIP file', type=str) args = parser.parse_args() - extract_native_libs(pathlib.Path(args.input), pathlib.Path(args.output)) + mappings = { + 'arm64-v8a': pathlib.Path(args.arm64), + 'armeabi-v7a': pathlib.Path(args.arm32), + 'x86': pathlib.Path(args.x86_32), + 'x86_64': pathlib.Path(args.x86_64), + } + + output = pathlib.Path(args.output) + + with tempfile.TemporaryDirectory() as tmp_dir: + root = pathlib.Path(tmp_dir) + for arch, src in mappings.items(): + dest_dir = root.joinpath('libs').joinpath(arch) + dest_dir.mkdir(parents=True, exist_ok=True) + shutil.copy(src, dest_dir.joinpath('libmozc.so')) + shutil.make_archive( + str(output.with_suffix('.zip')), format='zip', root_dir=tmp_dir + ) if __name__ == '__main__': diff --git a/src/bazel/android/BUILD.bazel b/src/bazel/android/BUILD.bazel deleted file mode 100644 index ea07803b8..000000000 --- a/src/bazel/android/BUILD.bazel +++ /dev/null @@ -1,90 +0,0 @@ -# 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. - -# platforms for --android_platforms option. -# In .bazelrc, the values are specified as --android_platforms=//bazel/android:arm64-v8a -# Reference: https://blog.bazel.build/2023/11/15/android-platforms.html - -load("//bazel:stubs.bzl", "bzl_library") - -package(default_visibility = [ - "//:__subpackages__", -]) - -# Platform names are used for the directory names of the JNI library. -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 = "riscv64", - constraint_values = - [ - "@platforms//cpu:riscv64", - "@platforms//os:android", - ], -) - -platform( - name = "x86", - constraint_values = - [ - "@platforms//cpu:x86_32", - "@platforms//os:android", - ], -) - -platform( - name = "x86_64", - constraint_values = [ - "@platforms//cpu:x86_64", - "@platforms//os:android", - ], -) - -bzl_library( - name = "rules_bzl", - srcs = ["rules.bzl"], - parse_tests = False, - visibility = ["//visibility:private"], -) diff --git a/src/bazel/android/rules.bzl b/src/bazel/android/rules.bzl deleted file mode 100644 index f7aeeb239..000000000 --- a/src/bazel/android/rules.bzl +++ /dev/null @@ -1,36 +0,0 @@ -# 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. - -"""Stub of rules_android forWORKSPACE.bazel.""" - -# This file should be removed once we migrate to Bzlmod from WORKSPACE. -# https://github.com/google/mozc/issues/1002 - -def android_binary(**kwargs): - native.android_binary(**kwargs)