2

I'm trying to use thrift for C++ and javascript running on my ARM device, and the project is built on a Linux host machine with a IDE for this ARM device.

My question is that when and how should I use cross compile?

There are two steps possible:

  1. when I install thrift(there is a host config option, but I don't know should I simply put 'arm' or the toolchain).

2 When I generate the source using thrift command with some option.

I'm confused here.

Thanks.

j.young
  • 41
  • 4

1 Answers1

0

Regarding your two points:

  1. Yes, you need to cross-compile the thrift library for your target platform. When you cross-compile your server it has to link against this library.

  2. No, you do not need to cross-compile the thrift-compiler. The thrift compiler is a source-to-source compiler. It only has to run on your development platform to translate the protocol definition into C++ code (or JavaScript code). This code is platform independent, so the thrift compiler does not need to know about your target platform. You would then take the generated code and, in case of C++, cross-compile it for your target platform as you would for your hand-written code.

To run your thrift server on the target device, you would therefore have take the following steps:

Obtain a cross-compiled version of the thrift library

Requirements:

  • The toolchain for your target platform
  • Cross-compiled version of boost
  • Cross-compiled version of openssl

Recent versions of thrift come with a cmake build script. To cross-compile with cmake, you would write a toolchain.cmake file that points to your toolchain (link, link). The toolchain definition can be passed to cmake with -DCMAKE_TOOLCHAIN_FILE:PATH=...

For the remaining cmake options, you can turn almost everything off except for the C++ library (or the C library if you prefer this):

cmake -S . -B out \
  -DBUILD_CPP=on \
  -DBUILD_COMPILER=off \
  -DBUILD_TESTING=off \
  -DBUILD_C_GLIB=off \
  -DBUILD_AS3=off \
  -DBUILD_JAVA=off \
  -DBUILD_JAVASCRIPT=off \
  -DBUILD_NODEJS=off \
  -DBUILD_PYTHON=off \
  -DBUILD_HASKELL=off \
  -DWITH_QT5=off \
  -DWITH_OPENSSL=on \
  -DCMAKE_CXX_STANDARD=14 \
  -DCMAKE_TOOLCHAIN_FILE:PATH="path/to/your/toolchain.cmake"

(Depending on your setup, some additional options may be required to point to boost and openssl, but I cannot tell without knowing more about your toolchain.)

The build process results in the target version of the thrift library:

./out/lib/libthriftd.so

Use the host thrift compiler for code generation

Run the thrift compiler on your host system to generate C++ code from your protocol specification (either use a pre-compiled version of the thrift compiler provided by your host system or compile thrift again for your host system, with the BUILD_COMPILER flag activated).

thrift --gen cpp path/to/your/protocol.thrift

This results in C++ files in the folder ./gen-cpp.

Cross-compile your code

Cross-compile your hand-written code for the server, along with the generated code in ./gen-cpp.

So for example if you have a Server.cpp, you could write the following CMakeLists.txt:

project( my-server CXX )

set( THRIFT_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/gen-cpp/HelloWorld.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gen-cpp/HelloWorld.h
    ${CMAKE_CURRENT_SOURCE_DIR}/gen-cpp/protocol_constants.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gen-cpp/protocol_constants.h
    ${CMAKE_CURRENT_SOURCE_DIR}/gen-cpp/protocol_types.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gen-cpp/protocol_types.h
)

add_library(
    my-generated-code
    OBJECT ${THRIFT_FILES}
)

add_executable(${PROJECT_NAME}
    ${CMAKE_CURRENT_SOURCE_DIR}/Server.cpp
    $<TARGET_OBJECTS:my-generated-code>
)

target_link_libraries(${PROJECT_NAME} thriftd)

You would cross-compile the server with:

cmake -S . -B out -DCMAKE_TOOLCHAIN_FILE:PATH="path/to/toolchain.cmake"

Depending on your setup, you might need additional options to point cmake to the cross-compiled libthriftd.so obtained in the first section. In the out folder, run make. This gives you a target binary ./out/my-server.

You can then deploy the my-server binary and the libthriftd.so to your target system. This should allow you to run my-server on the target system and to connect to the server with a matching client running on your host system.