tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

Cross-compilation instructions #2772

Closed aplavin closed 9 months ago

aplavin commented 9 months ago

I'm trying to compile tdlib different platforms on the same host machine, but probably doing something wrong here. The end result always is the so library for the host machine...

What I do:

mkdir build
cd build
CC=$HOSTCC CXX=$HOSTCXX LD=$HOSTLD cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --target prepare_cross_compiling

CC=$TARGETCC CXX=$TARGETCXX LD=$TARGETLD cmake -DCMAKE_CROSSCOMPILING=True -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --target tdjson

What should be the correct sequence of steps?

levlam commented 9 months ago

You must use differrent build folders for these CMake invocations. Otherwise, the direction is correct.

aplavin commented 9 months ago

Indeed, that seems to be the case, now it does attempt to cross compile. Still, cannot make it to succeed. As an example, let's consider x64 linux as the host, and arm macos as the target. If I run the second cmake as-is, it errors with

[  0%] Linking CXX executable generate_mime_types_gperf
cd /workspace/srcdir/td/build/tdutils/generate && /usr/bin/cmake -E cmake_link_script CMakeFiles/generate_mime_types_gperf.dir/link.txt --verbose=1
ccache /opt/bin/aarch64-apple-darwin20-libgfortran5-cxx11/c++  -std=c++14 -fno-omit-frame-pointer -fno-exceptions -fno-rtti -ffunction-sections -fdata-sections -Wall -Wextra -Wpointer-arith -Wcast-qual -Wsign-compare -Wno-tautological-compare -Wpointer-arith -Wvla -Wnon-virtual-dtor -Wno-unused-parameter -Wconversion -Wno-sign-conversion -Wc++14-compat-pedantic -Wdeprecated -Wno-unused-command-line-argument -Qunused-arguments -Wno-unknown-warning-option -Wodr -O3 -DNDEBUG  -Wl,--gc-sections -Wl,--exclude-libs,ALL CMakeFiles/generate_mime_types_gperf.dir/generate_mime_types_gperf.cpp.o -o generate_mime_types_gperf 
ld64.lld: error: unknown argument '--gc-sections'
ld64.lld: error: unknown argument '--exclude-libs'
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)

and if I pass -DAPPLE=1, then the error is different

[  0%] Generating ../../../tdutils/generate/auto/mime_type_to_extension.gperf, ../../../tdutils/generate/auto/extension_to_mime_type.gperf
cd /workspace/srcdir/td/tdutils/generate && /workspace/srcdir/td/build/tdutils/generate/generate_mime_types_gperf mime_types.txt /workspace/srcdir/td/tdutils/generate/auto/mime_type_to_extension.gperf /workspace/srcdir/td/tdutils/generate/auto/extension_to_mime_type.gperf
/workspace/srcdir/td/build/tdutils/generate/generate_mime_types_gperf: line 1: syntax error: unexpected "("

Any ideas of what goes wrong?

levlam commented 9 months ago

CMake needs much more variables to be specified to be able correctly build the project for the target architecture. It is better to create a toolchain file that provides all needed variables. See https://cmake.org/cmake/help/book/mastering-cmake/chapter/Cross%20Compiling%20With%20CMake.html for examples of how to correctly cross-compile with CMake.