Reputation: 1
recently I've been working on a flutter app, which uses libgit2 and I had no issues compiling the libgit2 repo for linux. Now, I would like to add support for android, so I tried to compile it using the Android NDK (r26) and the following toolchain.cmake
:
set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 34)
set(CMAKE_ANDROID_ARCH_ABI arm64-v8a)
set(CMAKE_ANDROID_NDK /opt/android-ndk)
set(OPENSSL_INCLUDE_DIR "/path/to/openssl/include")
set(OPENSSL_CRYPTO_LIBRARY "/path/to/openssl/libcrypto.so")
set(OPENSSL_SSL_LIBRARY "/path/to/openssl/libssl.so")
with the acutual path to the openssl repo instead of /path/to/openssl
.
I had no problem compiling openssl with the NDK. However, compiling libgit2, which uses cmake instead of ./Config
, gave me this error:
CMake Error at /usr/share/cmake/Modules/CMakeTestCCompiler.cmake:67 (message):
The C compiler
"/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang"
is not able to compile a simple test program.
The file /opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
is aparently a text file with the name of the acutal clang binary clang-17
. I have little experience with cmake and none with the NDK, but I tried to add this to the toolchain file, which didn't do anything:
set(CMAKE_C_COMPILER /opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang-17)
set(CMAKE_CXX_COMPILER /opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang-17)
I even tried to replace the text files with symlinks, which also didn't work, but gave a different error:
CMake Error at /usr/share/cmake/Modules/CMakeTestCCompiler.cmake:67 (message):
The C compiler
"/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: '/home/jakub/Projekty/dpc/lib/libgit/build/arm64/CMakeFiles/CMakeScratch/TryCompile-03LVGf'
Run Build Command(s): /usr/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_f226c/fast
/usr/bin/gmake -f CMakeFiles/cmTC_f226c.dir/build.make CMakeFiles/cmTC_f226c.dir/build
gmake[1]: Vstupuje se do adresáře „/media/data/Všechny Projekty/dpc/lib/libgit/build/arm64/CMakeFiles/CMakeScratch/TryCompile-03LVGf“
Building C object CMakeFiles/cmTC_f226c.dir/testCCompiler.c.o
/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=aarch64-none-linux-android34 -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fexceptions -O2 -g -DNDEBUG -MD -MT CMakeFiles/cmTC_f226c.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_f226c.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_f226c.dir/testCCompiler.c.o -c /home/jakub/Projekty/dpc/lib/libgit/build/arm64/CMakeFiles/CMakeScratch/TryCompile-03LVGf/testCCompiler.c
Linking C executable cmTC_f226c
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_f226c.dir/link.txt --verbose=1
/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=aarch64-none-linux-android34 -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fexceptions -O2 -g -DNDEBUG -Wl,--build-id=sha1 -Wl,--no-undefined-version -Wl,--fatal-warnings -Wl,--gc-sections -Qunused-arguments -Wl,--no-undefined -Wl,--gc-sections CMakeFiles/cmTC_f226c.dir/testCCompiler.c.o -o cmTC_f226c -latomic -lm
clang: error: unable to execute command: posix_spawn failed: Exec format error
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Output of /opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --version
is:
Android (10087095, +pgo, +bolt, +lto, -mlgo, based on r487747c) clang version 17.0.2 (https://android.googlesource.com/toolchain/llvm-project d9f89f4d16663d5012e5c09495f3b30ece3d2362)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin
I also tried using different NDK versions and with r22b
(or older) it configured itself and almost compiled, where the clang version was 11.0.5
. Then, it had problems with it's own header files, not recognising the inline
keyword:
/opt/android-ndk-r22/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/linux/swab.h:40:8: error: unknown type name 'inline'
static inline __attribute__((__const__)) __u32 __fswahw32(__u32 val) {
^
/opt/android-ndk-r22/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/linux/swab.h:40:47: error: expected ';' after top level declarator
static inline __attribute__((__const__)) __u32 __fswahw32(__u32 val) {
Thank you so much in advance for any help.
Upvotes: 0
Views: 252
Reputation: 106
It seems the project is configured to use C90, while C99 is required to build for Android.
There's an issue open about it, with some workaround provided, run this on the libgit2 folder to fix the CMakeLists:
find . -name 'CMakeLists.txt' -exec sed -i 's|C_STANDARD 90|C_STANDARD 99|' {} \;
The same person that found the workaround, also has a Github Workflow that makes way easier to build ndk libs: https://github.com/leleliu008/ndk-pkg-package-manually-build. Just fork and follow the steps described there.
You can see how the libgit2 ndk-pkg's formula was made here: https://github.com/leleliu008/ndk-pkg-formula-repository-official-core/blob/b121bf843f980ff9dd87868884c4cea15a2762ae/formula/libgit2.yml
Upvotes: 0