Protobuf: -lpthread in BUILD breaks Android projects that depend on protobuf

Created on 1 Apr 2016  路  6Comments  路  Source: protocolbuffers/protobuf

Hi,
Android supports pthreads, but does not provide a libpthread.so library to link against. The functionality is provided in other system libraries.

Since protobuf/BUILD specifies "-lpthread", I've had to add some hacks to get around this to get the Android Tensorflow demo working by creating a dummy pthread library, which is "linked" against and bundled in the APK: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/BUILD

Now that I'm trying to make a standalone Android executable it's becoming even more problematic as the binary expects to find the fake libpthread.so lib somewhere on its library path.

Is there a better way to build protobuf for Android? Or can the -lpthread flag be made selective on build platform, so that these workarounds are not necessary?

thanks!

bazel question

All 6 comments

You should be able to use bazel selectors to selectively include "-lpthread" on non android platforms. See an example of the selector:
https://github.com/google/protobuf/blob/master/BUILD#L588

You probably need to contact the bazel guys about how to select on android though.

The problem isn't how to include "-lpthread" for non-Android -- we don't need to do anything for this.

The question is how can we negate the "-lpthread" in protobuf/BUILD? It seems like this requires an edit to the protobuf source. I am not aware of any way to do this in the dependent project unfortunately.

Are there any examples of other Android projects that build with Bazel and use protobuf that you know of?

The comment there says "Bazel should provide portable link_opts for pthread". It's not clear to me if this is a TODO, or a note to users about how to build for mobile. Googling "bazel portable link_opts" just takes me back to that file.

What I am proposing is:

    linkopts = select({
        "//conditions:default": ["-pthread"],
        "<android>": [],
    }),

though I don't know how to select android in bazel.

Ah, gotcha (I'd thought you were talking about modifying Tensorflow somehow).

We select for Android in Tensorflow like this. //tensorflow:android is defined here.

There may be a better way to do it, but if nothing else we could just copy that config_setting into protobuf/BUILD and select against it when defining LINK_OPTS. Happy to do this myself if it sounds good with you.

Was this page helpful?
0 / 5 - 0 ratings