Apache Thrift on Windows

How to build and run the compiler, C++ library and tutorial

Like many open source software, Apache Thrift is better aligned to build and run on Linux. Though it is supported on Windows, the instructions are very minimal and much has to be “explored”. Here I will provide some details about the setup from source.

Source

The official Apache Thrift source is available as a tar/zip file or git repository. This example is based on Thrift version 0.10.0, though it should work equally well for other versions.

Quick reference to git commands.

git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift
git checkout tags/0.10.0 -b 0.10.0

Dependencies

Thrift has some 3rd party dependencies on libraries which needs to be downloaded from external sites. I have included them in the git repository https://github.com/cognitivewaves/thrift under 3rdparty for convenience.

Boost 1.53.0
It can be downloaded from www.boost.org. Since this is just source code, you will have to build some of the required libraries. Building all of boost is quite large and time consuming, so I prefer to target only the required libraries (atomic, date_time and thread).

The bootstrap.bat prepares the Boost.Build system for use.

D:\thrift\3rdparty\boost_1_53_0> bootstrap.bat

This command invokes Boost.Build to build the separately-compiled Boost libraries. Note the various options used which can be tweaked based on your requirement.

D:\thrift\3rdparty\boost_1_53_0> b2 install --prefix=D:\thrift\3rdparty\boost_1_53_0\_install toolset=msvc-10.0 address-model=64 --with-atomic --with-date_time --with-thread > build.log 2>&1

Flex and Bison (WinFlexBison)
Win flex-bison is a port of Flex (the fast lexical analyser) & Bison (GNU parser generator) tools to the Windows platform. see Lex & Yacc page for more details.

Build

The setup is for Visual Studio 10 Win64 and C++ target language library. If you are using a different configuration make appropriate changes.

The following components needs to be built.

  1. Thrift Compiler
    The code is under compiler\cpp. Though the code is under a directory cpp, it is a language independent compiler. That is, it produces an exe which can generate code for any language from the Thrift IDL.
  2. Language Libraries
    The code is under lib\cpp for C++. Similarly other language bindings are under their respective directories.
  3. Tutorial (optional)
    The code is under tutorial\cpp for C++. Similarly other language tutorials are under their respective directories.

There are two methods to build

  1. Using existing Visual Studio C++ projects which is the recommended approach on the official site
  2. CMake generated Visual Studio C++ projects which is not documented on the official site but turns out to be more convenient

Using existing Visual Studio C++ projects

The source has some pre-defined Visual Studio project files which have to be fixed because they are either incomplete or incorrect.

Thrift Compiler (compiler\cpp\compiler.vcproj)

Create a x64 configuration
Though it is common to have 64-bit build these days, the project file by default does not have this configuration. So add it to the project settings.

Project -> Properties
Configuration Manager -> Active Solution Platform -> 
Type = x64; Copy Settings from = Win32

Once the new configuration is created, adjust these properties only for the x64 configuration.

Properties -> C/C++ -> General -> Debug Information Format = Program Database (Zi)
Properties -> Linker -> Advanced -> Target Machine = Machinex64 (/MACHINE:X64)

Adjust the Build Events
Change the path/name to flex and bison. Note that Windows has a win_ prefix to the executable.

Project -> Properties -> Build Events -> Pre-Build Event -> Command Line

D:\thrift\3rdparty\flex_bison\win_flex  -o "src\\thrift\\thriftl.cc" src/thrift/thriftl.ll && D:\thrift\3rdparty\flex_bison\win_bison -y -o "src\\thrift\\thrifty.cc" --defines="src\\thrift\\thrifty.hh" src/thrift/thrifty.yy

Thrift C++ Language Library (lib\cpp\libthrift.vcxproj)

Set path to 3rdparty libraries
Open the Property Manager tab. Under each configuration (e.g. Debug|x64) there will be a 3rdparty property sheet.

3rdparty -> Properties -> Common Properties -> User Macros

Add macro THIRD_PARTY=D:\thrift\3rdparty and move to top
Modify BOOST_ROOT=$(THIRD_PARTY)\boost_1_53_0
Leave OPENSSL_ROOT_DIR since it is only used by libthriftnb.vcxproj
Leave LIBEVENT_ROOT since it is only used by libthriftnb.vcxproj

Add missing source files to the project
src/thrift/server/TServerFramework.cpp and h
src/thrift/server/TConnectedClient.cpp and h

Thrift C++ Tutorial (tutorial\cpp)

Unfortunately, there is no project file for the tutorial.

CMake generated Visual Studio C++ projects

If you are not familiar with CMake, refer to CMake and Visual Studio.

Edit tutorial\cpp\CMakeLists.txt to make Zlib conditional as it is not mandatory for the tutorial.

add_executable(TutorialServer CppServer.cpp)
target_link_libraries(TutorialServer tutorialgencpp)
LINK_AGAINST_THRIFT_LIBRARY(TutorialServer thrift)
if (ZLIB_FOUND)
  target_link_libraries(TutorialServer ${ZLIB_LIBRARIES})
endif ()

add_executable(TutorialClient CppClient.cpp)
target_link_libraries(TutorialClient tutorialgencpp)
LINK_AGAINST_THRIFT_LIBRARY(TutorialClient thrift)
if (ZLIB_FOUND)
  target_link_libraries(TutorialClient ${ZLIB_LIBRARIES})
endif ()

Set the environment variables for CMake to find the correct paths to 3rd party libraries.

D:\thrift> set BOOST_ROOT=D:\thrift\3rdparty\boost_1_53_0\_install

Create an out of source build directory. Note that a directory called build already exists in the sources.

D:\thrift> mkdir _build
D:\thrift> cd _build

Generate the Visual Studio projects. The CMake options are used to keep the build to a minimum.

D:\thrift\_build> cmake .. -G "Visual Studio 10 Win64" -DCMAKE_INSTALL_PREFIX=D:/thrift/_install -DCMAKE_PREFIX_PATH=D:/thrift/3rdparty/flex_bison -DWITH_SHARED_LIB=off -DWITH_CPP=on -DWITH_BOOSTTHREADS=on -DWITH_PLUGIN=off -DBUILD_TESTING=off -DBUILD_TUTORIALS=on 

Open the solution file in :\thrift\_build\Apache Thrift.sln and build.

Thrift Visual Studio Explorer View

Thrift Compiler – libparse and thrift-compiler
Language Library – thrift_static
Tutorial – TutorialClient, tutorialgencpp and TutorialServer

Conclusion

As you can see, though Thrift works well on Windows, getting a minimal setup is quite a task. Hope these instructions come in handy.

Advertisements