From 076788ceec1ef8d78af6175d09cbe1d28ff1cdf3 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Fri, 27 Oct 2023 06:06:22 +0000 Subject: [PATCH] Attempt to build libmozc.so --- .github/workflows/android.yaml | 18 ++------ src/.bazelrc | 4 +- src/WORKSPACE.bazel | 30 ++++++++----- src/android/jni/AndroidManifest.xml | 56 ++++++++++++++++++++++++ src/android/jni/BUILD.bazel | 21 +++++++++ src/android/jni/FakeActivity.java | 40 +++++++++++++++++ src/android/jni/libpthread.a | 1 + src/bazel/android_repository.bzl | 66 ----------------------------- 8 files changed, 143 insertions(+), 93 deletions(-) create mode 100644 src/android/jni/AndroidManifest.xml create mode 100644 src/android/jni/FakeActivity.java create mode 100644 src/android/jni/libpthread.a delete mode 100644 src/bazel/android_repository.bzl diff --git a/.github/workflows/android.yaml b/.github/workflows/android.yaml index 12874b89f4..71f054afc7 100644 --- a/.github/workflows/android.yaml +++ b/.github/workflows/android.yaml @@ -30,23 +30,11 @@ jobs: - name: build working-directory: ./src run: | - mkdir android_artifacts - mkdir android_artifacts/armeabi-v7a - mkdir android_artifacts/arm64-v8a - mkdir android_artifacts/x86 - mkdir android_artifacts/x86_64 - bazel build --config oss_android package --cpu=armeabi-v7a - cp bazel-bin/android/jni/libmozcjni.so android_artifacts/armeabi-v7a/ - bazel build --config oss_android package --cpu=arm64-v8a - cp bazel-bin/android/jni/libmozcjni.so android_artifacts/arm64-v8a/ - bazel build --config oss_android package --cpu=x86 - cp bazel-bin/android/jni/libmozcjni.so android_artifacts/x86/ - bazel build --config oss_android package --cpu=x86_64 - cp bazel-bin/android/jni/libmozcjni.so android_artifacts/x86_64/ + bazel build --config oss_android android/jni:mozc - name: upload artifacts uses: actions/upload-artifact@v3 with: - name: libmozcjni.so - path: src/android_artifacts/ + name: mozc.apk + path: src/bazel-bin/android/jni/mozc.apk if-no-files-found: warn diff --git a/src/.bazelrc b/src/.bazelrc index 1bb27a6147..cfa30bda21 100644 --- a/src/.bazelrc +++ b/src/.bazelrc @@ -34,12 +34,12 @@ build:prod_windows --define TARGET=prod_windows --build_tag_filters=-nowin # Android / OSS Android (same configurations) build:android --define TARGET=oss_android --copt "-DOS_ANDROID" -build:android --crosstool_top=@androidndk//:toolchain --cpu=armeabi-v7a +build:android --android_crosstool_top=@androidndk//:toolchain --fat_apk_cpu=armeabi-v7a,arm64-v8a,x86,x86_64 build:android --host_crosstool_top=@bazel_tools//tools/cpp:toolchain build:android --build_tag_filters=-noandroid test:android --test_tag_filters=-noandroid build:oss_android --define TARGET=oss_android --copt "-DOS_ANDROID" -build:oss_android --crosstool_top=@androidndk//:toolchain --cpu=armeabi-v7a +build:oss_android --android_crosstool_top=@androidndk//:toolchain --fat_apk_cpu=armeabi-v7a,arm64-v8a,x86,x86_64 build:oss_android --host_crosstool_top=@bazel_tools//tools/cpp:toolchain build:oss_android --build_tag_filters=-noandroid test:oss_android --test_tag_filters=-noandroid diff --git a/src/WORKSPACE.bazel b/src/WORKSPACE.bazel index e02cba73af..9085a4b1f3 100644 --- a/src/WORKSPACE.bazel +++ b/src/WORKSPACE.bazel @@ -74,24 +74,34 @@ apple_rules_dependencies() # Android NDK setup -RULES_ANDROID_NDK_COMMIT= "44dcd014f4b126f8941c29ff1b25e1584bd3eb26" -RULES_ANDROID_NDK_SHA = "79b1857e8e05e3007ad090a3269d2932e988b3ed176d7abd042719d45eb51500" +RULES_ANDROID_NDK_COMMIT= "877c68ef34c9f3353028bf490d269230c1990483" +RULES_ANDROID_NDK_SHA = "b1a5ddd784e6ed915c2035c0db536a278b5f50c64412128c06877115991391ef" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "rules_android_ndk", url = "https://github.com/bazelbuild/rules_android_ndk/archive/%s.zip" % RULES_ANDROID_NDK_COMMIT, sha256 = RULES_ANDROID_NDK_SHA, strip_prefix = "rules_android_ndk-%s" % RULES_ANDROID_NDK_COMMIT, ) +load("@rules_android_ndk//:rules.bzl", "android_ndk_repository") +android_ndk_repository( + name = "androidndk", + api_level = 31, +) + +http_archive( + name = "build_bazel_rules_android", + sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806", + strip_prefix = "rules_android-0.1.1", + urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"], +) -# Check the ANDROID_NDK_HOME envvar, if it is specified set up NDK as follows -# load("@rules_android_ndk//:rules.bzl", "android_ndk_repository") -# android_ndk_repository(name="androidndk") -# If it is empty, do nothing. -load("@//bazel:android_repository.bzl", "android_repository") -android_repository(name = "android_repository") -load("@android_repository//:setup.bzl", "android_ndk_setup") -android_ndk_setup() +load("@build_bazel_rules_android//android:rules.bzl", "android_sdk_repository") +android_sdk_repository( + name = "androidsdk", +) # Windows Implementation Library (WIL) # https://github.com/microsoft/wil/ diff --git a/src/android/jni/AndroidManifest.xml b/src/android/jni/AndroidManifest.xml new file mode 100644 index 0000000000..0e382db015 --- /dev/null +++ b/src/android/jni/AndroidManifest.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + diff --git a/src/android/jni/BUILD.bazel b/src/android/jni/BUILD.bazel index 8882a3c7bb..0193b799ae 100644 --- a/src/android/jni/BUILD.bazel +++ b/src/android/jni/BUILD.bazel @@ -36,6 +36,15 @@ load("//:build_defs.bzl", "MOZC_TAGS", "mozc_cc_library") +# 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 +cc_library( + name = "fake_pthread", + srcs = ["libpthread.a"], + linkopts = ["-L"+package_name()], +) + mozc_cc_library( name = "mozcjni", srcs = ["mozcjni.cc"], @@ -56,6 +65,18 @@ mozc_cc_library( "@com_google_absl//absl/random", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", + ":fake_pthread", ], alwayslink = 1, ) + +android_binary( + name = "mozc", + srcs = glob([ + "NoOpActivity.java", + ]), + manifest = "AndroidManifest.xml", + deps = [ + ":mozcjni", + ], +) diff --git a/src/android/jni/FakeActivity.java b/src/android/jni/FakeActivity.java new file mode 100644 index 0000000000..00f5e46e60 --- /dev/null +++ b/src/android/jni/FakeActivity.java @@ -0,0 +1,40 @@ +// Copyright 2010-2023, 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. + +package org.mozc.android.build; + +import andorid.app.Activity; + +public class FakeActivity extends Activity { + @Override + public void OnCreate() { + super.OnCreate(); + System.loadLibrary("mozc"); + } +} diff --git a/src/android/jni/libpthread.a b/src/android/jni/libpthread.a new file mode 100644 index 0000000000..8b277f0dd5 --- /dev/null +++ b/src/android/jni/libpthread.a @@ -0,0 +1 @@ +! diff --git a/src/bazel/android_repository.bzl b/src/bazel/android_repository.bzl deleted file mode 100644 index 25a4f2c775..0000000000 --- a/src/bazel/android_repository.bzl +++ /dev/null @@ -1,66 +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. - -"""Repository rule for Android build system. - -If ANDROID_NDK_HOME env variable is set, android_ndk_repository is called. -Otherwise do nothing. - -## Usage example in WORKSPACE.bazel -``` -load("//:android_repository.bzl", "android_repository") -android_repository(name = "android_repository") -load("@android_repository//:setup.bzl", "android_ndk_setup") -android_ndk_setup() -``` - -## Reference -* https://github.com/google/mozc/issues/544 -* https://github.com/bazelbuild/bazel/issues/14260 -""" - -NDK_SETUP_TEMPLATE = """ -load("@rules_android_ndk//:rules.bzl", "android_ndk_repository") - -def android_ndk_setup(): - {rule} -""" - -def _android_repository_impl(repository_ctx): - if repository_ctx.os.environ.get("ANDROID_NDK_HOME"): - rule = "android_ndk_repository(name=\"androidndk\")" - else: - rule = "pass" - repository_ctx.file("BUILD.bazel", "") - repository_ctx.file("setup.bzl", NDK_SETUP_TEMPLATE.format(rule = rule)) - -android_repository = repository_rule( - implementation = _android_repository_impl, - environ = ["ANDROID_NDK_HOME"], -)