Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone libprotobuf.so cross compiled with Android NDK #18991

Open
milinddeore opened this issue Oct 24, 2024 · 0 comments
Open

Standalone libprotobuf.so cross compiled with Android NDK #18991

milinddeore opened this issue Oct 24, 2024 · 0 comments
Assignees
Labels

Comments

@milinddeore
Copy link

My build platform is ubuntu 22.4 and I have set the build configuration as suggested in the document, but it always pick the linux toolchain.

Android NDK: r25c
picked latest clone of protobuf on 25-oct-2024

My configuration looks as below:

diff --git a/BUILD.bazel b/BUILD.bazel
index 97d1ef84b..78bd10e40 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -647,3 +647,22 @@ filegroup(
     srcs = glob(["**/*.bzl"]),
     visibility = ["//visibility:public"],
 )
+
+
+platform(
+    name = "android_arm64",
+    constraint_values = [
+        "@platforms//os:android",
+        "@platforms//cpu:arm64",
+    ],
+)
+
+toolchain(
+    name = "my_android_toolchain",
+    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
+    toolchain = "@androidndk//:toolchain",
+    target_compatible_with = [
+        "@platforms//cpu:arm64",
+        "@platforms//os:android",
+    ],
+)
diff --git a/WORKSPACE b/WORKSPACE
index f84687db9..783310f46 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -1,5 +1,11 @@
 workspace(name = "com_google_protobuf")

+android_ndk_repository(
+    name = "androidndk",
+    path = "/home/ved/PROJECT/android-ndk-r21e",  # Path to NDK installation
+    api_level = 21,  # Minimum API level you are targeting
+)
+
 # An explicit self-reference to work around changes in Bazel 7.0
 # See https://github.com/bazelbuild/bazel/issues/19973#issuecomment-1787814450
 # buildifier: disable=duplicated-name
@@ -275,3 +281,4 @@ load("@rules_buf//buf:repositories.bzl", "rules_buf_dependencies", "rules_buf_to
 rules_buf_dependencies()

 rules_buf_toolchains(version = "v1.32.1")

Even at in the build command, explicitly specifying the toolchain but it never pick it.

$ bazel build //src/google/protobuf:protobuf --android_platforms=@androidndk//:android_arm64 --crosstool_top=@androidndk//:toolchain

Following commands returns empty result:

$ bazel query 'kind(android_platform, @androidndk//...)'
Starting local Bazel server and connecting to it...
DEBUG: /home/ved/.cache/bazel/_bazel_ved/59b4c1376de3e22977e2bd4801fb92a1/external/rules_ruby/ruby/private/toolchains/ruby_runtime.bzl:297:14: WARNING: no system ruby available, builds against system ruby will fail
DEBUG: /home/ved/.cache/bazel/_bazel_ved/59b4c1376de3e22977e2bd4801fb92a1/external/system_ruby/bundle.bzl:3:10: WARNING: no system ruby found for bundle
DEBUG: Rule 'com_google_protobuf_v25.0' indicated that a canonical reproducible form can be obtained by modifying arguments integrity = "sha256-e+7ZxRHWMs/3wirACU3Xcg5VAVMDnV2n4Fm8zrSIR0o="
DEBUG: Repository com_google_protobuf_v25.0 instantiated at:
  /home/ved/PROJECT/protobuf/WORKSPACE:242:13: in <toplevel>
Repository rule http_archive defined at:
  /home/ved/.cache/bazel/_bazel_ved/59b4c1376de3e22977e2bd4801fb92a1/external/bazel_tools/tools/build_defs/repo/http.bzl:387:31: in <toplevel>
INFO: Empty results

The resultant libraries is compiled using linux toolchain:

bazel build //src/google/protobuf:protobuf --android_platforms=@androidndk//:android_arm64 --crosstool_top=@androidndk//:toolchain
Starting local Bazel server and connecting to it...
DEBUG: /home/ved/.cache/bazel/_bazel_ved/59b4c1376de3e22977e2bd4801fb92a1/external/rules_ruby/ruby/private/toolchains/ruby_runtime.bzl:297:14: WARNING: no system ruby available, builds against system ruby will fail
DEBUG: /home/ved/.cache/bazel/_bazel_ved/59b4c1376de3e22977e2bd4801fb92a1/external/system_ruby/bundle.bzl:3:10: WARNING: no system ruby found for bundle
DEBUG: Rule 'com_google_protobuf_v25.0' indicated that a canonical reproducible form can be obtained by modifying arguments integrity = "sha256-e+7ZxRHWMs/3wirACU3Xcg5VAVMDnV2n4Fm8zrSIR0o="
DEBUG: Repository com_google_protobuf_v25.0 instantiated at:
  /home/ved/PROJECT/protobuf/WORKSPACE:242:13: in <toplevel>
Repository rule http_archive defined at:
  /home/ved/.cache/bazel/_bazel_ved/59b4c1376de3e22977e2bd4801fb92a1/external/bazel_tools/tools/build_defs/repo/http.bzl:387:31: in <toplevel>
DEBUG: Rule 'rules_buf_toolchains' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "f7e50b227bf171158ff4a80d22f274d0195487b618e0c4a57b3b0741a52453c2"
DEBUG: Repository rules_buf_toolchains instantiated at:
  /home/ved/PROJECT/protobuf/WORKSPACE:283:21: in <toplevel>
  /home/ved/.cache/bazel/_bazel_ved/59b4c1376de3e22977e2bd4801fb92a1/external/rules_buf/buf/internal/toolchain.bzl:197:26: in rules_buf_toolchains
Repository rule buf_download_releases defined at:
  /home/ved/.cache/bazel/_bazel_ved/59b4c1376de3e22977e2bd4801fb92a1/external/rules_buf/buf/internal/toolchain.bzl:170:40: in <toplevel>
INFO: Analyzed target //src/google/protobuf:protobuf (159 packages loaded, 1071 targets configured).
INFO: Found 1 target...
Target //src/google/protobuf:protobuf up-to-date:
  bazel-bin/src/google/protobuf/libprotobuf.a
  bazel-bin/src/google/protobuf/libprotobuf.so
INFO: Elapsed time: 119.992s, Critical Path: 24.67s
INFO: 284 processes: 93 internal, 191 linux-sandbox.
INFO: Build completed successfully, 284 total actions

ved@ved-Lenovo:~/PROJECT/protobuf$ file bazel-bin/src/google/protobuf/libprotobuf.so
bazel-bin/src/google/protobuf/libprotobuf.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=72c40c7ab934dc05d7bf3368d196c864a345bb8e, not stripped
@milinddeore milinddeore added the untriaged auto added to all issues by default when created. label Oct 24, 2024
@anandolee anandolee added bazel and removed untriaged auto added to all issues by default when created. labels Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants