• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Minimum CMake required
2cmake_minimum_required(VERSION 3.1.3)
3
4if(protobuf_VERBOSE)
5  message(STATUS "Protocol Buffers Configuring...")
6endif()
7
8# CMake policies
9cmake_policy(SET CMP0022 NEW)
10# On MacOS use @rpath/ for target's install name prefix path
11if (POLICY CMP0042)
12  cmake_policy(SET CMP0042 NEW)
13endif ()
14# Clear VERSION variables when no VERSION is given to project()
15if(POLICY CMP0048)
16  cmake_policy(SET CMP0048 NEW)
17endif()
18
19# Project
20project(protobuf C CXX)
21
22# Add c++11 flags
23if (CYGWIN)
24  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
25else()
26  set(CMAKE_CXX_STANDARD 11)
27  set(CMAKE_CXX_STANDARD_REQUIRED ON)
28  set(CMAKE_CXX_EXTENSIONS OFF)
29endif()
30
31# The Intel compiler isn't able to deal with noinline member functions of
32# template classes defined in headers.  As such it spams the output with
33#   warning #2196: routine is both "inline" and "noinline"
34# This silences that warning.
35if (CMAKE_CXX_COMPILER_ID MATCHES Intel)
36  string(APPEND CMAKE_CXX_FLAGS " -diag-disable=2196")
37endif()
38
39# Options
40if(WITH_PROTOC)
41  set(protobuf_PROTOC_EXE ${WITH_PROTOC} CACHE FILEPATH "Protocol Buffer Compiler executable" FORCE)
42endif()
43option(protobuf_BUILD_TESTS "Build tests" ON)
44option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
45option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
46option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
47option(protobuf_BUILD_LIBPROTOC "Build libprotoc" OFF)
48if (BUILD_SHARED_LIBS)
49  set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
50else (BUILD_SHARED_LIBS)
51  set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
52endif (BUILD_SHARED_LIBS)
53option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
54include(CMakeDependentOption)
55cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON
56  "NOT protobuf_BUILD_SHARED_LIBS" OFF)
57set(protobuf_WITH_ZLIB_DEFAULT ON)
58option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
59set(protobuf_DEBUG_POSTFIX "d"
60  CACHE STRING "Default debug postfix")
61mark_as_advanced(protobuf_DEBUG_POSTFIX)
62# User options
63include(protobuf-options.cmake)
64
65# Overrides for option dependencies
66if (protobuf_BUILD_PROTOC_BINARIES OR protobuf_BUILD_TESTS)
67  set(protobuf_BUILD_LIBPROTOC ON)
68endif ()
69# Path to main configure script
70set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
71
72# Parse configure script
73set(protobuf_AC_INIT_REGEX
74  "^AC_INIT\\(\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\]\\)$")
75file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_AC_INIT_LINE
76  LIMIT_COUNT 1 REGEX "^AC_INIT")
77# Description
78string(REGEX REPLACE        "${protobuf_AC_INIT_REGEX}" "\\1"
79    protobuf_DESCRIPTION    "${protobuf_AC_INIT_LINE}")
80# Version
81string(REGEX REPLACE        "${protobuf_AC_INIT_REGEX}" "\\2"
82    protobuf_VERSION_STRING "${protobuf_AC_INIT_LINE}")
83# Contact
84string(REGEX REPLACE        "${protobuf_AC_INIT_REGEX}" "\\3"
85    protobuf_CONTACT        "${protobuf_AC_INIT_LINE}")
86# Parse version tweaks
87set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)([-]rc[-]|\\.)?([0-9]*)$")
88string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\1"
89  protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
90string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\2"
91  protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
92string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\3"
93  protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
94string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\5"
95  protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")
96
97message(STATUS "${protobuf_VERSION_PRERELEASE}")
98
99# Package version
100set(protobuf_VERSION
101  "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
102
103if(protobuf_VERSION_PRERELEASE)
104  set(protobuf_VERSION "${protobuf_VERSION}.${protobuf_VERSION_PRERELEASE}")
105else()
106  set(protobuf_VERSION "${protobuf_VERSION}.0")
107endif()
108message(STATUS "${protobuf_VERSION}")
109
110if(protobuf_VERBOSE)
111  message(STATUS "Configuration script parsing status [")
112  message(STATUS "  Description : ${protobuf_DESCRIPTION}")
113  message(STATUS "  Version     : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
114  message(STATUS "  Contact     : ${protobuf_CONTACT}")
115  message(STATUS "]")
116endif()
117
118add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
119
120find_package(Threads REQUIRED)
121if (CMAKE_USE_PTHREADS_INIT)
122  add_definitions(-DHAVE_PTHREAD)
123endif (CMAKE_USE_PTHREADS_INIT)
124
125set(_protobuf_FIND_ZLIB)
126if (protobuf_WITH_ZLIB)
127  find_package(ZLIB)
128  if (ZLIB_FOUND)
129    set(HAVE_ZLIB 1)
130    # FindZLIB module define ZLIB_INCLUDE_DIRS variable
131    # Set ZLIB_INCLUDE_DIRECTORIES for compatible
132    set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
133    # Using imported target if exists
134    if (TARGET ZLIB::ZLIB)
135      set(ZLIB_LIBRARIES ZLIB::ZLIB)
136      set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n  find_package(ZLIB)\nendif()")
137    endif (TARGET ZLIB::ZLIB)
138  else (ZLIB_FOUND)
139    set(HAVE_ZLIB 0)
140    # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
141    # complain when we use them later.
142    set(ZLIB_INCLUDE_DIRECTORIES)
143    set(ZLIB_LIBRARIES)
144  endif (ZLIB_FOUND)
145endif (protobuf_WITH_ZLIB)
146
147if (HAVE_ZLIB)
148  add_definitions(-DHAVE_ZLIB)
149endif (HAVE_ZLIB)
150
151# We need to link with libatomic on systems that do not have builtin atomics, or
152# don't have builtin support for 8 byte atomics
153set(protobuf_LINK_LIBATOMIC false)
154if (NOT MSVC)
155  include(CheckCXXSourceCompiles)
156  set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
157  set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -std=c++11)
158  check_cxx_source_compiles("
159    #include <atomic>
160    int main() {
161      return std::atomic<int64_t>{};
162    }
163  " protobuf_HAVE_BUILTIN_ATOMICS)
164  if (NOT protobuf_HAVE_BUILTIN_ATOMICS)
165    set(protobuf_LINK_LIBATOMIC true)
166  endif (NOT protobuf_HAVE_BUILTIN_ATOMICS)
167  set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
168endif (NOT MSVC)
169
170if (protobuf_BUILD_SHARED_LIBS)
171  set(protobuf_SHARED_OR_STATIC "SHARED")
172else (protobuf_BUILD_SHARED_LIBS)
173  set(protobuf_SHARED_OR_STATIC "STATIC")
174  # In case we are building static libraries, link also the runtime library statically
175  # so that MSVCR*.DLL is not required at runtime.
176  # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
177  # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
178  # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
179  if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
180    foreach(flag_var
181        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
182        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
183      if(${flag_var} MATCHES "/MD")
184        string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
185      endif(${flag_var} MATCHES "/MD")
186    endforeach(flag_var)
187  endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
188endif (protobuf_BUILD_SHARED_LIBS)
189
190if (MSVC)
191  # Build with multiple processes
192  add_definitions(/MP)
193  # MSVC warning suppressions
194  add_definitions(
195    /wd4018 # 'expression' : signed/unsigned mismatch
196    /wd4065 # switch statement contains 'default' but no 'case' labels
197    /wd4146 # unary minus operator applied to unsigned type, result still unsigned
198    /wd4244 # 'conversion' conversion from 'type1' to 'type2', possible loss of data
199    /wd4251 # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
200    /wd4267 # 'var' : conversion from 'size_t' to 'type', possible loss of data
201    /wd4305 # 'identifier' : truncation from 'type1' to 'type2'
202    /wd4307 # 'operator' : integral constant overflow
203    /wd4309 # 'conversion' : truncation of constant value
204    /wd4334 # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
205    /wd4355 # 'this' : used in base member initializer list
206    /wd4506 # no definition for inline function 'function'
207    /wd4800 # 'type' : forcing value to bool 'true' or 'false' (performance warning)
208    /wd4996 # The compiler encountered a deprecated declaration.
209  )
210  # Allow big object
211  add_definitions(/bigobj)
212  string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
213  string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
214  string(REPLACE "." ","  protobuf_RC_FILEVERSION "${protobuf_VERSION}")
215  configure_file(extract_includes.bat.in extract_includes.bat)
216
217  # Suppress linker warnings about files with no symbols defined.
218  set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4221")
219
220  # Configure Resource Compiler
221  enable_language(RC)
222  # use English language (0x409) in resource compiler
223  set(rc_flags "/l0x409")
224  # fix rc.exe invocations because of usage of add_definitions()
225  set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_flags} <DEFINES> /fo<OBJECT> <SOURCE>")
226
227  configure_file(version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
228endif (MSVC)
229
230
231get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)
232
233include_directories(
234  ${ZLIB_INCLUDE_DIRECTORIES}
235  ${protobuf_BINARY_DIR}
236  ${protobuf_source_dir}/src)
237
238if (MSVC)
239  # Add the "lib" prefix for generated .lib outputs.
240  set(LIB_PREFIX lib)
241else (MSVC)
242  # When building with "make", "lib" prefix will be added automatically by
243  # the build tool.
244  set(LIB_PREFIX)
245endif (MSVC)
246
247if (protobuf_UNICODE)
248  add_definitions(-DUNICODE -D_UNICODE)
249endif (protobuf_UNICODE)
250
251include(libprotobuf-lite.cmake)
252include(libprotobuf.cmake)
253if (protobuf_BUILD_LIBPROTOC)
254  include(libprotoc.cmake)
255endif (protobuf_BUILD_LIBPROTOC)
256if (protobuf_BUILD_PROTOC_BINARIES)
257  include(protoc.cmake)
258  if (NOT DEFINED protobuf_PROTOC_EXE)
259    set(protobuf_PROTOC_EXE protoc)
260  endif (NOT DEFINED protobuf_PROTOC_EXE)
261endif (protobuf_BUILD_PROTOC_BINARIES)
262
263# Ensure we have a protoc executable if we need one
264if (protobuf_BUILD_TESTS OR protobuf_BUILD_CONFORMANCE OR protobuf_BUILD_EXAMPLES)
265  if (NOT DEFINED protobuf_PROTOC_EXE)
266    find_program(protobuf_PROTOC_EXE protoc)
267    if (NOT protobuf_PROTOC_EXE)
268      message(FATAL "Build requires 'protoc' but binary not found and not building protoc.")
269    endif ()
270  endif ()
271  if(protobuf_VERBOSE)
272    message(STATUS "Using protoc : ${protobuf_PROTOC_EXE}")
273  endif(protobuf_VERBOSE)
274endif ()
275
276if (protobuf_BUILD_TESTS)
277  include(tests.cmake)
278endif (protobuf_BUILD_TESTS)
279
280if (protobuf_BUILD_CONFORMANCE)
281  include(conformance.cmake)
282endif (protobuf_BUILD_CONFORMANCE)
283
284include(install.cmake)
285
286if (protobuf_BUILD_EXAMPLES)
287  include(examples.cmake)
288endif (protobuf_BUILD_EXAMPLES)
289
290if(protobuf_VERBOSE)
291  message(STATUS "Protocol Buffers Configuring done")
292endif(protobuf_VERBOSE)
293