benfogle / crossenv

Cross-compiling virtualenv for Python
MIT License
112 stars 22 forks source link

Help cross-compiling grpcio #101

Closed PataviniMa closed 2 years ago

PataviniMa commented 2 years ago

Hello, first of all, I'd like to express all my gratitude for such an amazing tool. Keep up the good work!

I'm desperately trying to cross compile grpcio from ubuntu (x86_64) to android (aarch64). Both build python and host python are version 3.10.5. The build seems to proceed without issues, until I get this error:

/home/marco/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -D_WIN32_WINNT=1536 -DGRPC_XDS_USER_AGENT_NAME_SUFFIX="Python" -DGRPC_XDS_USER_AGENT_VERSION_SUFFIX="1.49.1" -DGPR_BACKWARDS_COMPATIBILITY_MODE=1 -DHAVE_CONFIG_H=1 -DGRPC_ENABLE_FORK_SUPPORT=1 -DPyMODINIT_FUNC=extern "C" attribute((visibility ("default"))) PyObject* -DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK=1 -Isrc/python/grpcio -Iinclude -I. -Ithird_party/abseil-cpp -Ithird_party/address_sorting/include -Ithird_party/cares/cares/include -Ithird_party/cares -Ithird_party/cares/cares -Ithird_party/cares/config_linux -Ithird_party/re2 -Ithird_party/boringssl-with-bazel/src/include -Ithird_party/upb -Isrc/core/ext/upb-generated -Isrc/core/ext/upbdefs-generated -Ithird_party/xxhash -Ithird_party/zlib -I/home/marco/venv/cross/include -I/home/marco/python3-android/build/usr/include/python3.10 -c src/core/ext/upb-generated/envoy/admin/v3/certs.upb.c -o python_build/temp.linux-aarch64-3.10/src/core/ext/upb-generated/envoy/admin/v3/certs.upb.o -std=c++14 -fvisibility=hidden -fno-wrapv -fno-exceptions -pthread error: invalid argument '-std=c++14' not allowed with 'C'

Full building log

So, evidently, the compiler is complaining because of the '-std=c++14' used for a C source file. The question is: how can I remove that flag for C source files ONLY, leaving it for C++ files? Any help is highly appreciated. Thank you!

benfogle commented 2 years ago

This seems like an upstream issue. From their setup.py, it looks like they're building a ton of C and C++ with identical flags. For gcc, compiling a C file with -std=c++14 only emits a warning, but to clang that's an error.

It seems like you're using NDK r25b, so switching to gcc probably isn't an option.

Maybe you can just remove the -std flag completely and let clang use its default dialect for each type? The default dialects for that version of clang should be gnu17 for C and gnu++14 for C++, which should be a more-or-less superset of what's required. No guarantees, of course, but that seems like the least painful solution.

I'm looking at this part of their setup.py, and it looks like you might be able to set the environment variable GRPC_PYTHON_CFLAGS to the correct value. In this case, you might be able to set it to -fvisibility=hidden -fno-wrapv -fno-exceptions.

PataviniMa commented 2 years ago

It took some hours of "compile - get error - find solution" looping, but I've finally managed to cross-compile grpcio using crossenv. Thanks for pointing me in the right direction. I'll leave my findings here for future reference. Python version (for both build and host): 3.10.5 grpcio source code version: 1.49.1 COMPILE_ARGS: I removed "-std=c++14", nothing else. LINK_ARGS: I removed "-lpthread" and "-lrt" (they don't have an Android counterpart as they are integrated in libc), and I added "-llog" and "-static-libstdc++" (without those two, I was getting runtime "cannot locate symbol" errors). Then, I had to set the "GRPC_PYTHON_DISABLE_LIBC_COMPATIBILITY" flag to true to get past other compile issues (see here). All this modifications were made to the {source}/setup.py file. Last but not least, I had to comment a line in this header file: {source}/third_party/cares/config_linux/ares_config.h to solve another runtime issue (see here). Now, I have a .whl feel I can import to my Android device, install it via pip, and it's working like a charm! If anyone is attempting the same, I'll leave all the edits I've made (in the form of .patch files) attached to this post. I'll also add the full build log.

build_grpcio.log patch.zip