THRIFT-2835 Add possibility to distribute generators separately from thrift core, and load them dynamically
Client: Compiler
Patch: Nobuaki Sukegawa, rebased by dtmuller

Also fixed by dtmuller:
* Add plugin namespace for erlang language binding
* Fix unit test test_const_value
* Don't clear type cache with every t_program conversion
* Type "wb" may not be supported by popen on non-Windows platforms
* Fix constness of AST type signatures
diff --git a/build/cmake/DefineOptions.cmake b/build/cmake/DefineOptions.cmake
index 6dd59e0..171c9fe 100644
--- a/build/cmake/DefineOptions.cmake
+++ b/build/cmake/DefineOptions.cmake
@@ -40,6 +40,13 @@
 # and enables the library if all are found. This means the default is to build as
 # much as possible but leaving out libraries if their dependencies are not met.
 
+CMAKE_DEPENDENT_OPTION(WITH_BOOST_STATIC "Build with Boost static link library" OFF "NOT MSVC" ON)
+set(Boost_USE_STATIC_LIBS ${WITH_BOOST_STATIC})
+if (NOT WITH_BOOST_STATIC)
+    add_definitions(-DBOOST_ALL_DYN_LINK)
+    add_definitions(-DBOOST_TEST_DYN_LINK)
+endif()
+
 # C++
 option(WITH_CPP "Build C++ Thrift library" ON)
 if(WITH_CPP)
@@ -77,6 +84,8 @@
 endif()
 CMAKE_DEPENDENT_OPTION(BUILD_CPP "Build C++ library" ON
                        "BUILD_LIBRARIES;WITH_CPP;Boost_FOUND" OFF)
+CMAKE_DEPENDENT_OPTION(WITH_PLUGIN "Build compiler plugin support" ON
+                       "BUILD_COMPILER;BUILD_CPP" OFF)
 
 # C GLib
 option(WITH_C_GLIB "Build C (GLib) Thrift library" ON)
@@ -86,6 +95,21 @@
 CMAKE_DEPENDENT_OPTION(BUILD_C_GLIB "Build C (GLib) library" ON
                        "BUILD_LIBRARIES;WITH_C_GLIB;GLIB_FOUND" OFF)
 
+if(BUILD_CPP)
+    set(boost_components)
+    if(WITH_BOOSTTHREADS OR BUILD_TESTING)
+        list(APPEND boost_components system thread)
+    endif()
+    if(BUILD_TESTING)
+        list(APPEND boost_components unit_test_framework filesystem chrono program_options)
+    endif()
+    if(boost_components)
+        find_package(Boost 1.53 REQUIRED COMPONENTS ${boost_components})
+    endif()
+elseif(BUILD_C_GLIB AND BUILD_TESTING)
+    find_package(Boost 1.53 REQUIRED)
+endif()
+
 # Java
 option(WITH_JAVA "Build Java Thrift library" ON)
 if(ANDROID)
@@ -120,8 +144,6 @@
     message(FATAL_ERROR "Cannot build with both shared and static outputs disabled!")
 endif()
 
-option(WITH_DYN_LINK_TEST "Build with Boost dynamic link test library" OFF)
-
 #NOTE: C++ compiler options are defined in the lib/cpp/CMakeLists.txt
 
 # Visual Studio only options
@@ -141,6 +163,7 @@
 message(STATUS "Thrift package version:                       ${PACKAGE_VERSION}")
 message(STATUS "Build configuration Summary")
 message(STATUS "  Build Thrift compiler:                      ${BUILD_COMPILER}")
+message(STATUS "  Build compiler plugin support:              ${WITH_PLUGIN}")
 message(STATUS "  Build with unit tests:                      ${BUILD_TESTING}")
 MESSAGE_DEP(HAVE_COMPILER "Disabled because BUILD_THRIFT=OFF and no valid THRIFT_COMPILER is given")
 message(STATUS "  Build examples:                             ${BUILD_EXAMPLES}")
@@ -178,6 +201,9 @@
 message(STATUS "  Build with OpenSSL support:                 ${WITH_OPENSSL}")
 message(STATUS "  Build with Boost thread support:            ${WITH_BOOSTTHREADS}")
 message(STATUS "  Build with C++ std::thread support:         ${WITH_STDTHREADS}")
-message(STATUS "  Build with Boost dynamic link test library: ${WITH_DYN_LINK_TEST}")
+message(STATUS "  Build with Boost static link library:       ${WITH_BOOST_STATIC}")
+if(MSVC)
+    message(STATUS "    - Enabled for Visual C++")
+endif()
 message(STATUS "----------------------------------------------------------")
 endmacro(PRINT_CONFIG_SUMMARY)
diff --git a/build/cmake/ThriftMacros.cmake b/build/cmake/ThriftMacros.cmake
index 2656598..f837f94 100644
--- a/build/cmake/ThriftMacros.cmake
+++ b/build/cmake/ThriftMacros.cmake
@@ -25,7 +25,6 @@
 
 if(WITH_SHARED_LIB)
     add_library(${name} SHARED ${ARGN})
-    #target_link_libraries(${name} ${SYSLIBS})
     set_target_properties(${name} PROPERTIES
         OUTPUT_NAME ${name}
         VERSION ${thrift_VERSION}
@@ -40,7 +39,6 @@
 
 if(WITH_STATIC_LIB)
     add_library(${name}_static STATIC ${ARGN})
-    #target_link_libraries(${name}_static ${SYSLIBS})
     set_target_properties(${name}_static PROPERTIES
         OUTPUT_NAME ${name}${STATIC_POSTFIX}
         VERSION ${thrift_VERSION}
@@ -55,6 +53,19 @@
 endmacro(ADD_LIBRARY_THRIFT)
 
 
+macro(TARGET_INCLUDE_DIRECTORIES_THRIFT name)
+
+if(WITH_SHARED_LIB)
+    target_include_directories(${name} ${ARGN})
+endif()
+
+if(WITH_STATIC_LIB)
+    target_include_directories(${name}_static ${ARGN})
+endif()
+
+endmacro(TARGET_INCLUDE_DIRECTORIES_THRIFT)
+
+
 macro(TARGET_LINK_LIBRARIES_THRIFT name)
 
 if(WITH_SHARED_LIB)
@@ -84,11 +95,11 @@
 macro(TARGET_LINK_LIBRARIES_THRIFT_AGAINST_THRIFT_LIBRARY target libname)
 
 if(WITH_SHARED_LIB)
-    target_link_libraries(${target} ${libname})
+    target_link_libraries(${target} ${ARGN} ${libname})
 endif()
 
 if(WITH_STATIC_LIB)
-    target_link_libraries(${target}_static ${libname}_static)
+    target_link_libraries(${target}_static ${ARGN} ${libname}_static)
 endif()
 
 endmacro(TARGET_LINK_LIBRARIES_THRIFT_AGAINST_THRIFT_LIBRARY)