[CDRIVER-2407] Android NDK support Created: 25/Nov/17  Updated: 28/Oct/23  Resolved: 05/Dec/17

Status: Closed
Project: C Driver
Component/s: cmake, libbson
Affects Version/s: 1.8.2
Fix Version/s: 1.9.0

Type: New Feature Priority: Major - P3
Reporter: Jean-Marc Le Roux Assignee: A. Jesse Jiryu Davis
Resolution: Fixed Votes: 0
Labels: feature
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Android NDK r13b+


Issue Links:
Related
related to CDRIVER-2440 BSON_HAVE_RAND_R is not defined for A... Closed
Backwards Compatibility: Fully Compatible

 Description   

libbson doesn't build with clang from the Android NDK r13b because of the missing `rand_r` symbol.

How to reproduce

docker run -it alexisduque/android-ndk-gradle-docker:latest bash
# following commands must be ran in the docker bash
git clone https://github.com/mongodb/libbson.git
cd libbson
mkdir bin && cd bin
cmake .. -DCMAKE_TOOLCHAIN_FILE:STRING=/opt/android-ndk-r13b/build/cmake/android.toolchain.cmake
make

Fails with error:

bson-context.c:359: error: undefined reference to 'rand_r'

Fix

Working fix available here:

https://github.com/aerys/libbson/tree/fix/android-undefined-rand_r



 Comments   
Comment by A. Jesse Jiryu Davis [ 05/Dec/17 ]

I'm marking this fixed. We'll release these changes in libbson 1.9.0 within the next few days. Let me know if there's any further changes required for Android.

Comment by Githook User [ 05/Dec/17 ]

Author:

{'username': 'ajdavis', 'email': 'jesse@mongodb.com', 'name': 'A. Jesse Jiryu Davis'}

Message: CDRIVER-2407 avoid conflict with system gettid()
Branch: master
https://github.com/mongodb/libbson/commit/113c4296f05b24d999ce8677910f58d9d85613dd

Comment by Jean-Marc Le Roux [ 04/Dec/17 ]

Jean-Marc, here's a branch that renames libbson's own gettid implementation to "bson_gettid", let me know if this works for you:

It builds fine with the Android NDK.
It's indeed a better solution than my pull request.

Comment by A. Jesse Jiryu Davis [ 04/Dec/17 ]

Jean-Marc, here's a branch that renames libbson's own gettid implementation to "bson_gettid", let me know if this works for you:

https://github.com/mongodb/libbson/tree/CDRIVER-2407-gettid

Comment by Githook User [ 26/Nov/17 ]

Author:

{'name': 'Jean-Marc Le Roux', 'username': 'promethe42', 'email': 'jeanmarc.leroux@aerys.in'}

Message: CDRIVER-2407 fix rand_r check in CMakeLists
Branch: master
https://github.com/mongodb/libbson/commit/8f4013f0e0b34788fbc810dda0560c2e28a6e017

Comment by Githook User [ 26/Nov/17 ]

Author:

{'name': 'Jean-Marc Le Roux', 'username': 'promethe42', 'email': 'jeanmarc.leroux@aerys.in'}

Message: CDRIVER-2407 polyfill rand_r in tests
Branch: master
https://github.com/mongodb/libbson/commit/5264bd53aa7e5d090f7d65045b3aec06c9a5d899

Comment by A. Jesse Jiryu Davis [ 26/Nov/17 ]

Great, I’ll look again when I’m at my computer. I think our time with Autotools is coming to an end and it’s ok not to add Android support there.

I think we shouldn’t have named our own function, gettid, it should be bson_gettid and it should have a consistent declaration.

Comment by Jean-Marc Le Roux [ 25/Nov/17 ]

I followed the `OR APPLE` existing code here: https://github.com/mongodb/libbson/blob/master/CMakeLists.txt#L138
And it now build for Android with NDK r16.

PR is here: https://github.com/mongodb/libbson/pull/210

Fix is specific to CMake. But I don't know how to cross-compile to Android with autotools.
Is it ok to have Android NDK compatibility for CMake only?

Comment by Jean-Marc Le Roux [ 25/Nov/17 ]

Removing the `static` qualifier for `gettid()` is not enough: clang still complains because `gettid()` is declared as :

  • pid_t gettid(void) _attribute_const_ in unistd.h (Android NDK)
  • uint16_t gettid (void) in bson-context.c (libbson)

Are those supposed to be the same `gettid()` ?

Comment by Jean-Marc Le Roux [ 25/Nov/17 ]

New PR here for the same `rand_r` issues in the tests : https://github.com/mongodb/libbson/pull/209

Another issue now with Android NDK r16 is this one :

/srv/libbson/src/bson/bson-context.c:51:1: error: static declaration of 'gettid' follows non-static declaration
gettid (void)
^
/opt/android-ndk-linux/sysroot/usr/include/unistd.h:84:8: note: previous declaration is here
pid_t  gettid(void) __attribute_const__;
       ^

Problematic code is in bson-context.c: https://github.com/mongodb/libbson/blob/master/src/bson/bson-context.c#L50

#ifdef BSON_HAVE_SYSCALL_TID
static uint16_t
gettid (void)
{
   return syscall (SYS_gettid);
}
#endif

Comment by A. Jesse Jiryu Davis [ 25/Nov/17 ]

I've merged the PR, which uses "rand" on Android where "rand_r" is unavailable. Jean-Marc is investigating if we need additional changes to handle BSON_HAVE_SYSCALL_TID properly.

Comment by Jean-Marc Le Roux [ 25/Nov/17 ]

Here you go: https://github.com/mongodb/libbson/pull/208

Comment by A. Jesse Jiryu Davis [ 25/Nov/17 ]

Thanks, would you like to submit a pull request?

Generated at Wed Feb 07 21:15:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.