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