THRIFT-2571 Simplify cross compilation using CMake
Patch: Pascal Bach
This closes #137
----
diff --git a/.travis.yml b/.travis.yml
index 9bd9dfa..d54acc4 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -71,5 +71,7 @@
- make -j2 && make dist
- make cross -j2
- sh bootstrap.sh ; sh contrib/mingw-cross-compile.sh
+ - mkdir build_native && cd build_native && cmake ../compiler/cpp/ && make; cd ..
+ - mkdir build_mingw32 && cd build_mingw32 && cmake -DCMAKE_TOOLCHAIN_FILE=../contrib/mingw32-toolchain.cmake ../compiler/cpp/ && make; cd ..
# TODO: add these steps
# - sh bootstrap.sh ; dpkg-buildpackage -tc
diff --git a/compiler/cpp/CMakeLists.txt b/compiler/cpp/CMakeLists.txt
new file mode 100644
index 0000000..152e5db
--- /dev/null
+++ b/compiler/cpp/CMakeLists.txt
@@ -0,0 +1,152 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+
+cmake_minimum_required(VERSION 2.8)
+
+project(thrift-compiler)
+
+# Read the version information from the Autoconf file
+file (STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/../../configure.ac" CONFIGURE_AC REGEX "AC_INIT\\(.*\\)" )
+
+# The following variable is used in the version.h.in file
+string(REGEX REPLACE "AC_INIT\\(\\[.*\\], \\[([0-9]+\\.[0-9]+\\.[0-9]+(-dev)?)\\]\\)" "\\1" PACKAGE_VERSION ${CONFIGURE_AC})
+message(STATUS "Thrift package version: ${PACKAGE_VERSION}")
+
+# These are internal to CMake
+string(REGEX REPLACE "([0-9]+\\.[0-9]+\\.[0-9]+)(-dev)?" "\\1" thrift_VERSION ${PACKAGE_VERSION})
+string(REGEX REPLACE "([0-9]+)\\.[0-9]+\\.[0-9]+" "\\1" thrift_VERSION_MAJOR ${thrift_VERSION})
+string(REGEX REPLACE "[0-9]+\\.([0-9])+\\.[0-9]+" "\\1" thrift_VERSION_MINOR ${thrift_VERSION})
+string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" thrift_VERSION_PATCH ${thrift_VERSION})
+message(STATUS "Thrift version: ${thrift_VERSION} (${thrift_VERSION_MAJOR}.${thrift_VERSION_MINOR}.${thrift_VERSION_PATCH})")
+
+# Windows has a different header
+if(MSVC)
+ set(FLEX_FLAGS "--wincompat") # Don't use unistd.h on windows
+ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/windows/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h)
+else()
+ set(FLEX_FLAGS " ")
+ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h)
+endif()
+
+find_package(FLEX REQUIRED)
+find_package(BISON REQUIRED)
+
+# Create flex and bison files and build the lib parse static library
+BISON_TARGET(thrifty ${CMAKE_CURRENT_SOURCE_DIR}/src/thrifty.yy ${CMAKE_CURRENT_BINARY_DIR}/thrifty.cc)
+FLEX_TARGET(thriftl ${CMAKE_CURRENT_SOURCE_DIR}/src/thriftl.ll ${CMAKE_CURRENT_BINARY_DIR}/thriftl.cc COMPILE_FLAGS ${FLEX_FLAGS})
+ADD_FLEX_BISON_DEPENDENCY(thriftl thrifty)
+
+# HACK: Work around the fact that bison crates a .hh file but we need a .h file
+add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/thrifty.h
+ COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/thrifty.hh ${CMAKE_CURRENT_BINARY_DIR}/thrifty.h
+ DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/thrifty.hh
+ )
+
+set(libparse_SOURCES
+ ${CMAKE_CURRENT_BINARY_DIR}/thrifty.cc
+ ${CMAKE_CURRENT_BINARY_DIR}/thriftl.cc
+ ${CMAKE_CURRENT_BINARY_DIR}/thrifty.h
+)
+
+add_library(libparse STATIC ${libparse_SOURCES})
+
+# Create the thrift compiler
+set( thrift_SOURCES
+ src/main.cc
+ src/md5.c
+ src/generate/t_generator.cc
+ src/generate/t_generator_registry.h
+ src/globals.h
+ src/main.h
+ src/platform.h
+ src/md5.h
+ src/parse/t_doc.h
+ src/parse/t_type.h
+ src/parse/t_base_type.h
+ src/parse/t_enum.h
+ src/parse/t_enum_value.h
+ src/parse/t_typedef.h
+ src/parse/t_typedef.cc
+ src/parse/t_container.h
+ src/parse/t_list.h
+ src/parse/t_set.h
+ src/parse/t_map.h
+ src/parse/t_struct.h
+ src/parse/t_field.h
+ src/parse/t_service.h
+ src/parse/t_function.h
+ src/parse/t_program.h
+ src/parse/t_scope.h
+ src/parse/t_const.h
+ src/parse/t_const_value.h
+ src/parse/parse.cc
+ src/generate/t_generator.h
+ src/generate/t_oop_generator.h
+ src/generate/t_html_generator.h
+ src/windows/config.h
+ version.h
+)
+
+# This macro adds an option THRIFT_COMPILER_${NAME}
+# that allows enabling or disabling certain languages
+macro(THRIFT_ADD_COMPILER name description initial)
+ string(TOUPPER "THRIFT_COMPILER_${name}" enabler)
+ set(src "src/generate/t_${name}_generator.cc")
+ option(${enabler} ${description} ${initial})
+ if(${enabler})
+ list(APPEND thrift_SOURCES ${src})
+ endif()
+endmacro()
+
+# The following compiler can be enabled or disabled
+THRIFT_ADD_COMPILER(c_glib "Enable compiler for C with Glib" ON)
+THRIFT_ADD_COMPILER(cpp "Enable compiler for C++" ON)
+THRIFT_ADD_COMPILER(java "Enable compiler for Java" ON)
+THRIFT_ADD_COMPILER(as3 "Enable compiler for ActionScript 3" ON)
+THRIFT_ADD_COMPILER(csharp "Enable compiler for C#" ON)
+THRIFT_ADD_COMPILER(py "Enable compiler for Python 2.0" ON)
+THRIFT_ADD_COMPILER(rb "Enable compiler for Ruby" ON)
+THRIFT_ADD_COMPILER(perl "Enable compiler for Perl" ON)
+THRIFT_ADD_COMPILER(php "Enable compiler for PHP" ON)
+THRIFT_ADD_COMPILER(erl "Enable compiler for Erlang" ON)
+THRIFT_ADD_COMPILER(cocoa "Enable compiler for Cocoa Objective-C" ON)
+THRIFT_ADD_COMPILER(st "Enable compiler for Smalltalk" ON)
+THRIFT_ADD_COMPILER(ocaml "Enable compiler for OCaml" ON)
+THRIFT_ADD_COMPILER(hs "Enable compiler for Haskell" ON)
+THRIFT_ADD_COMPILER(xsd "Enable compiler for XSD" ON)
+THRIFT_ADD_COMPILER(html "Enable compiler for HTML Documentation" ON)
+THRIFT_ADD_COMPILER(js "Enable compiler for JavaScript" ON)
+THRIFT_ADD_COMPILER(javame "Enable compiler for Java ME" ON)
+THRIFT_ADD_COMPILER(delphi "Enable compiler for Delphi" ON)
+THRIFT_ADD_COMPILER(go "Enable compiler for Go" ON)
+THRIFT_ADD_COMPILER(d "Enable compiler for D" ON)
+THRIFT_ADD_COMPILER(lua "Enable compiler for Lua" ON)
+
+# Thrift is looking for include files in the src directory
+# we also add the current binary directory for generated files
+include_directories(${CMAKE_CURRENT_BINARY_DIR} src)
+
+add_executable(thrift ${thrift_SOURCES})
+
+target_link_libraries(thrift libparse)
+
+if(NOT WIN32)
+ install(TARGETS thrift DESTINATION bin)
+endif()
diff --git a/compiler/cpp/README.md b/compiler/cpp/README.md
index e69de29..452a49c 100644
--- a/compiler/cpp/README.md
+++ b/compiler/cpp/README.md
@@ -0,0 +1,19 @@
+# Build compiler using CMake
+
+Use the following steps to build using cmake
+
+mkdir build
+cd build
+cmake ..
+make
+
+## Build on windows
+
+In order to build on windows a few additional steps are necessary
+
+1. Download winflexbison from http://sourceforge.net/projects/winflexbison/
+2. Extract the winflex bison files to for e.g. C:\winflexbison
+3. Make the CMake variables point to the correct binaries.
+ * FLEX_EXECUTBALE = C:/winbuild/win_flex.exe
+ * BISON_EXECUTBALE = C:/winbuild/win_bison.exe
+
diff --git a/contrib/mingw32-toolchain.cmake b/contrib/mingw32-toolchain.cmake
new file mode 100644
index 0000000..3dfb1a5
--- /dev/null
+++ b/contrib/mingw32-toolchain.cmake
@@ -0,0 +1,19 @@
+# CMake mingw32 cross compile toolchain file
+
+# the name of the target operating system
+SET(CMAKE_SYSTEM_NAME Windows)
+
+# which compilers to use for C and C++
+SET(CMAKE_C_COMPILER i586-mingw32msvc-gcc)
+SET(CMAKE_CXX_COMPILER i586-mingw32msvc-g++)
+SET(CMAKE_RC_COMPILER i586-mingw32msvc-windres)
+
+# here is the target environment located
+SET(CMAKE_FIND_ROOT_PATH /usr/i586-mingw32msvc)
+
+# adjust the default behaviour of the FIND_XXX() commands:
+# search headers and libraries in the target environment, search
+# programs in the host environment
+set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)