1#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10#   http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
20
21include(CMakeDependentOption)
22
23set(THRIFT_COMPILER "" CACHE FILEPATH "External Thrift compiler to use during build")
24
25# Additional components
26option(BUILD_COMPILER "Build Thrift compiler" ON)
27
28if(BUILD_COMPILER OR EXISTS ${THRIFT_COMPILER})
29    set(HAVE_COMPILER ON)
30endif()
31CMAKE_DEPENDENT_OPTION(BUILD_TESTING "Build with unit tests" ON "HAVE_COMPILER" OFF)
32CMAKE_DEPENDENT_OPTION(BUILD_TUTORIALS "Build Thrift tutorials" ON "HAVE_COMPILER" OFF)
33option(BUILD_LIBRARIES "Build Thrift libraries" ON)
34
35# Libraries to build
36
37# Each language library can be enabled or disabled using the WITH_<LANG> flag.
38# By default CMake checks if the required dependencies for a language are present
39# and enables the library if all are found. This means the default is to build as
40# much as possible but leaving out libraries if their dependencies are not met.
41
42if (NOT Boost_USE_STATIC_LIBS)
43    add_definitions(-DBOOST_ALL_DYN_LINK)
44    add_definitions(-DBOOST_TEST_DYN_LINK)
45endif()
46
47# as3
48option(WITH_AS3 "Build ActionScript 3 Thrift Library" ON)
49if (WITH_AS3)
50    set(POSSIBLE_PATHS "${FLEX_HOME}/bin" "$ENV{FLEX_HOME}/bin")
51    find_program(HAVE_COMPC NAMES compc HINTS ${POSSIBLE_PATHS})
52endif ()
53CMAKE_DEPENDENT_OPTION(BUILD_AS3 "Build as3 library" ON
54                       "BUILD_LIBRARIES;WITH_AS3;HAVE_COMPC" OFF)
55
56# C++
57option(WITH_CPP "Build C++ Thrift library" ON)
58if(WITH_CPP)
59    # NOTE: Currently the following options are C++ specific,
60    # but in future other libraries might reuse them.
61    # So they are not dependent on WITH_CPP but setting them without WITH_CPP currently
62    # has no effect.
63    if(ZLIB_LIBRARY)
64        # FindZLIB.cmake does not normalize path so we need to do it ourselves.
65        file(TO_CMAKE_PATH ${ZLIB_LIBRARY} ZLIB_LIBRARY)
66    endif()
67    find_package(ZLIB QUIET)
68    CMAKE_DEPENDENT_OPTION(WITH_ZLIB "Build with ZLIB support" ON
69                           "ZLIB_FOUND" OFF)
70    find_package(Libevent QUIET)
71    CMAKE_DEPENDENT_OPTION(WITH_LIBEVENT "Build with libevent support" ON
72                           "Libevent_FOUND" OFF)
73    find_package(Qt5 QUIET COMPONENTS Core Network)
74    CMAKE_DEPENDENT_OPTION(WITH_QT5 "Build with Qt5 support" ON
75                           "Qt5_FOUND" OFF)
76endif()
77CMAKE_DEPENDENT_OPTION(BUILD_CPP "Build C++ library" ON
78                       "BUILD_LIBRARIES;WITH_CPP" OFF)
79
80# C GLib
81option(WITH_C_GLIB "Build C (GLib) Thrift library" ON)
82if(WITH_C_GLIB)
83    find_package(GLIB QUIET COMPONENTS gobject)
84endif()
85CMAKE_DEPENDENT_OPTION(BUILD_C_GLIB "Build C (GLib) library" ON
86                       "BUILD_LIBRARIES;WITH_C_GLIB;GLIB_FOUND" OFF)
87
88# OpenSSL
89if(WITH_CPP OR WITH_C_GLIB)
90    find_package(OpenSSL QUIET)
91    CMAKE_DEPENDENT_OPTION(WITH_OPENSSL "Build with OpenSSL support" ON
92                        "OPENSSL_FOUND" OFF)
93endif()
94
95# Java
96option(WITH_JAVA "Build Java Thrift library" ON)
97if(ANDROID)
98    find_package(Gradle QUIET)
99    CMAKE_DEPENDENT_OPTION(BUILD_JAVA "Build Java library" ON
100                           "BUILD_LIBRARIES;WITH_JAVA;GRADLE_FOUND" OFF)
101else()
102    find_package(Gradle QUIET)
103    find_package(Java QUIET)
104    CMAKE_DEPENDENT_OPTION(BUILD_JAVA "Build Java library" ON
105                           "BUILD_LIBRARIES;WITH_JAVA;JAVA_FOUND;GRADLE_FOUND" OFF)
106endif()
107
108# Javascript
109option(WITH_JAVASCRIPT "Build Javascript Thrift library" ON)
110CMAKE_DEPENDENT_OPTION(BUILD_JAVASCRIPT "Build Javascript library" ON
111                       "BUILD_LIBRARIES;WITH_JAVASCRIPT;NOT WIN32; NOT CYGWIN" OFF)
112
113# NodeJS
114option(WITH_NODEJS "Build NodeJS Thrift library" ON)
115CMAKE_DEPENDENT_OPTION(BUILD_NODEJS "Build NodeJS library" ON
116                       "BUILD_LIBRARIES;WITH_NODEJS" OFF)
117
118# Python
119option(WITH_PYTHON "Build Python Thrift library" ON)
120find_package(PythonInterp QUIET) # for Python executable
121find_package(PythonLibs QUIET) # for Python.h
122CMAKE_DEPENDENT_OPTION(BUILD_PYTHON "Build Python library" ON
123                       "BUILD_LIBRARIES;WITH_PYTHON;PYTHONINTERP_FOUND;PYTHONLIBS_FOUND" OFF)
124
125# Common library options
126# https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html
127# Default on Windows is static, shared mode library support needs work...
128if(WIN32)
129    set(DEFAULT_BUILD_SHARED_LIBS ON)
130else()
131    set(DEFAULT_BUILD_SHARED_LIBS OFF)
132endif()
133option(BUILD_SHARED_LIBS "Build shared libraries" ${DEFAULT_BUILD_SHARED_LIBS})
134
135if (WITH_SHARED_LIB)
136    message(WARNING "WITH_SHARED_LIB is deprecated; use -DBUILD_SHARED_LIBS=ON instead")
137    set(BUILD_SHARED_LIBS ON)
138elseif (WITH_STATIC_LIB)
139    if (WITH_SHARED_LIB)
140        message(FATAL_ERROR "Cannot build shared and static together; set BUILD_SHARED_LIBS instead.")
141    endif ()
142    message(WARNING "WITH_STATIC_LIB is deprecated; use -DBUILD_SHARED_LIBS=OFF instead")
143    set(BUILD_SHARED_LIBS OFF)
144endif ()
145
146# Visual Studio only options
147if(MSVC)
148    option(WITH_MT "Build using the static runtime 'MT' instead of the shared DLL-specific runtime 'MD' (MSVC only)" OFF)
149endif(MSVC)
150
151macro(MESSAGE_DEP flag summary)
152if(NOT ${flag})
153    message(STATUS "   - ${summary}")
154endif()
155endmacro(MESSAGE_DEP flag summary)
156
157macro(PRINT_CONFIG_SUMMARY)
158message(STATUS "----------------------------------------------------------")
159message(STATUS "Thrift version:                               ${thrift_VERSION} (${thrift_VERSION_MAJOR}.${thrift_VERSION_MINOR}.${thrift_VERSION_PATCH})")
160message(STATUS "Thrift package version:                       ${PACKAGE_VERSION}")
161message(STATUS)
162message(STATUS "Build configuration summary")
163message(STATUS "  Build compiler:                             ${BUILD_COMPILER}")
164message(STATUS "  Build libraries:                            ${BUILD_LIBRARIES}")
165message(STATUS "  Build tests:                                ${BUILD_TESTING}")
166MESSAGE_DEP(HAVE_COMPILER "Disabled because BUILD_COMPILER=OFF and no valid THRIFT_COMPILER is given")
167message(STATUS "  Build type:                                 ${CMAKE_BUILD_TYPE}")
168message(STATUS)
169message(STATUS "Language libraries:")
170message(STATUS)
171message(STATUS "  Build as3 library:                          ${BUILD_AS3}")
172MESSAGE_DEP(WITH_AS3 "Disabled by WITH_AS3=OFF")
173MESSAGE_DEP(HAVE_COMPC "Adobe Flex compc was not found - did you set env var FLEX_HOME?")
174message(STATUS)
175message(STATUS "  Build with OpenSSL:                         ${WITH_OPENSSL}")
176if(WITH_OPENSSL)
177    message(STATUS "    Version:                                  ${OPENSSL_VERSION}")
178endif()
179message(STATUS)
180message(STATUS "  Build C++ library:                          ${BUILD_CPP}")
181MESSAGE_DEP(WITH_CPP "Disabled by WITH_CPP=OFF")
182if (BUILD_CPP)
183    message(STATUS "    C++ Language Level:                       ${CXX_LANGUAGE_LEVEL}")
184    message(STATUS "    Build shared libraries:                   ${BUILD_SHARED_LIBS}")
185    message(STATUS "    Build with libevent support:              ${WITH_LIBEVENT}")
186    message(STATUS "    Build with Qt5 support:                   ${WITH_QT5}")
187    message(STATUS "    Build with ZLIB support:                  ${WITH_ZLIB}")
188endif ()
189message(STATUS)
190message(STATUS "  Build C (GLib) library:                     ${BUILD_C_GLIB}")
191MESSAGE_DEP(WITH_C_GLIB "Disabled by WITH_C_GLIB=OFF")
192MESSAGE_DEP(GLIB_FOUND "GLib missing")
193message(STATUS)
194message(STATUS "  Build Java library:                         ${BUILD_JAVA}")
195MESSAGE_DEP(WITH_JAVA "Disabled by WITH_JAVA=OFF")
196if(ANDROID)
197    MESSAGE_DEP(GRADLE_FOUND "Gradle missing")
198else()
199    MESSAGE_DEP(JAVA_FOUND "Java Runtime missing")
200    MESSAGE_DEP(GRADLE_FOUND "Gradle missing")
201endif()
202message(STATUS "  Build Javascript library:                   ${BUILD_JAVASCRIPT}")
203MESSAGE_DEP(WITH_JAVASCRIPT "Disabled by WITH_JAVASCRIPT=OFF")
204message(STATUS "  Build NodeJS library:                       ${BUILD_NODEJS}")
205MESSAGE_DEP(WITH_NODEJS "Disabled by WITH_NODEJS=OFF")
206message(STATUS)
207message(STATUS "  Build Python library:                       ${BUILD_PYTHON}")
208MESSAGE_DEP(WITH_PYTHON "Disabled by WITH_PYTHON=OFF")
209MESSAGE_DEP(PYTHONLIBS_FOUND "Python libraries missing")
210if(MSVC)
211    message(STATUS "  Using static runtime library:               ${WITH_MT}")
212endif(MSVC)
213message(STATUS)
214message(STATUS)
215message(STATUS "----------------------------------------------------------")
216endmacro(PRINT_CONFIG_SUMMARY)
217