• 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# Options
32option(protobuf_BUILD_TESTS "Build tests" ON)
33option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
34option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
35option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
36if (BUILD_SHARED_LIBS)
37  set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
38else (BUILD_SHARED_LIBS)
39  set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
40endif (BUILD_SHARED_LIBS)
41option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
42include(CMakeDependentOption)
43cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON
44  "NOT protobuf_BUILD_SHARED_LIBS" OFF)
45set(protobuf_WITH_ZLIB_DEFAULT ON)
46option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
47set(protobuf_DEBUG_POSTFIX "d"
48  CACHE STRING "Default debug postfix")
49mark_as_advanced(protobuf_DEBUG_POSTFIX)
50# User options
51include(protobuf-options.cmake)
52
53# Path to main configure script
54set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
55
56# Parse configure script
57set(protobuf_AC_INIT_REGEX
58  "^AC_INIT\\(\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\]\\)$")
59file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_AC_INIT_LINE
60  LIMIT_COUNT 1 REGEX "^AC_INIT")
61# Description
62string(REGEX REPLACE        "${protobuf_AC_INIT_REGEX}" "\\1"
63    protobuf_DESCRIPTION    "${protobuf_AC_INIT_LINE}")
64# Version
65string(REGEX REPLACE        "${protobuf_AC_INIT_REGEX}" "\\2"
66    protobuf_VERSION_STRING "${protobuf_AC_INIT_LINE}")
67# Contact
68string(REGEX REPLACE        "${protobuf_AC_INIT_REGEX}" "\\3"
69    protobuf_CONTACT        "${protobuf_AC_INIT_LINE}")
70# Parse version tweaks
71set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)([-]rc[-]|\\.)?([0-9]*)$")
72string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\1"
73  protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
74string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\2"
75  protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
76string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\3"
77  protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
78string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\5"
79  protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")
80
81message(STATUS "${protobuf_VERSION_PRERELEASE}")
82
83# Package version
84set(protobuf_VERSION
85  "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
86
87if(protobuf_VERSION_PRERELEASE)
88  set(protobuf_VERSION "${protobuf_VERSION}.${protobuf_VERSION_PRERELEASE}")
89else()
90  set(protobuf_VERSION "${protobuf_VERSION}.0")
91endif()
92message(STATUS "${protobuf_VERSION}")
93
94if(protobuf_VERBOSE)
95  message(STATUS "Configuration script parsing status [")
96  message(STATUS "  Description : ${protobuf_DESCRIPTION}")
97  message(STATUS "  Version     : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
98  message(STATUS "  Contact     : ${protobuf_CONTACT}")
99  message(STATUS "]")
100endif()
101
102add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
103
104find_package(Threads REQUIRED)
105if (CMAKE_USE_PTHREADS_INIT)
106  add_definitions(-DHAVE_PTHREAD)
107endif (CMAKE_USE_PTHREADS_INIT)
108
109set(_protobuf_FIND_ZLIB)
110if (protobuf_WITH_ZLIB)
111  find_package(ZLIB)
112  if (ZLIB_FOUND)
113    set(HAVE_ZLIB 1)
114    # FindZLIB module define ZLIB_INCLUDE_DIRS variable
115    # Set ZLIB_INCLUDE_DIRECTORIES for compatible
116    set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
117    # Using imported target if exists
118    if (TARGET ZLIB::ZLIB)
119      set(ZLIB_LIBRARIES ZLIB::ZLIB)
120      set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n  find_package(ZLIB)\nendif()")
121    endif (TARGET ZLIB::ZLIB)
122  else (ZLIB_FOUND)
123    set(HAVE_ZLIB 0)
124    # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
125    # complain when we use them later.
126    set(ZLIB_INCLUDE_DIRECTORIES)
127    set(ZLIB_LIBRARIES)
128  endif (ZLIB_FOUND)
129endif (protobuf_WITH_ZLIB)
130
131if (HAVE_ZLIB)
132  add_definitions(-DHAVE_ZLIB)
133endif (HAVE_ZLIB)
134
135if (protobuf_BUILD_SHARED_LIBS)
136  set(protobuf_SHARED_OR_STATIC "SHARED")
137else (protobuf_BUILD_SHARED_LIBS)
138  set(protobuf_SHARED_OR_STATIC "STATIC")
139  # In case we are building static libraries, link also the runtime library statically
140  # so that MSVCR*.DLL is not required at runtime.
141  # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
142  # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
143  # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
144  if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
145    foreach(flag_var
146        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
147        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
148      if(${flag_var} MATCHES "/MD")
149        string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
150      endif(${flag_var} MATCHES "/MD")
151    endforeach(flag_var)
152  endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
153endif (protobuf_BUILD_SHARED_LIBS)
154
155if (MSVC)
156  # Build with multiple processes
157  add_definitions(/MP)
158  # MSVC warning suppressions
159  add_definitions(
160    /wd4018 # 'expression' : signed/unsigned mismatch
161    /wd4065 # switch statement contains 'default' but no 'case' labels
162    /wd4146 # unary minus operator applied to unsigned type, result still unsigned
163    /wd4244 # 'conversion' conversion from 'type1' to 'type2', possible loss of data
164    /wd4251 # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
165    /wd4267 # 'var' : conversion from 'size_t' to 'type', possible loss of data
166    /wd4305 # 'identifier' : truncation from 'type1' to 'type2'
167    /wd4307 # 'operator' : integral constant overflow
168    /wd4309 # 'conversion' : truncation of constant value
169    /wd4334 # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
170    /wd4355 # 'this' : used in base member initializer list
171    /wd4506 # no definition for inline function 'function'
172    /wd4800 # 'type' : forcing value to bool 'true' or 'false' (performance warning)
173    /wd4996 # The compiler encountered a deprecated declaration.
174  )
175  # Allow big object
176  add_definitions(/bigobj)
177  string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
178  string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
179  string(REPLACE "." ","  protobuf_RC_FILEVERSION "${protobuf_VERSION}")
180  configure_file(extract_includes.bat.in extract_includes.bat)
181
182  # Suppress linker warnings about files with no symbols defined.
183  set(CMAKE_STATIC_LINKER_FLAGS /ignore:4221)
184
185  # Configure Resource Compiler
186  enable_language(RC)
187  # use English language (0x409) in resource compiler
188  set(rc_flags "/l0x409")
189  # fix rc.exe invocations because of usage of add_definitions()
190  set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_flags} <DEFINES> /fo<OBJECT> <SOURCE>")
191
192  configure_file(version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
193endif (MSVC)
194
195
196get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)
197
198include_directories(
199  ${ZLIB_INCLUDE_DIRECTORIES}
200  ${protobuf_BINARY_DIR}
201  ${protobuf_source_dir}/android
202  ${protobuf_source_dir}/src)
203
204if (MSVC)
205  # Add the "lib" prefix for generated .lib outputs.
206  set(LIB_PREFIX lib)
207else (MSVC)
208  # When building with "make", "lib" prefix will be added automatically by
209  # the build tool.
210  set(LIB_PREFIX)
211endif (MSVC)
212
213if (protobuf_UNICODE)
214  add_definitions(-DUNICODE -D_UNICODE)
215endif (protobuf_UNICODE)
216
217include(libprotobuf-lite.cmake)
218include(libprotobuf.cmake)
219if (protobuf_BUILD_PROTOC_BINARIES)
220  include(libprotoc.cmake)
221  include(protoc.cmake)
222endif (protobuf_BUILD_PROTOC_BINARIES)
223
224if (protobuf_BUILD_TESTS)
225  include(tests.cmake)
226endif (protobuf_BUILD_TESTS)
227
228if (protobuf_BUILD_CONFORMANCE)
229  include(conformance.cmake)
230endif (protobuf_BUILD_CONFORMANCE)
231
232include(install.cmake)
233
234if (protobuf_BUILD_EXAMPLES)
235  include(examples.cmake)
236endif (protobuf_BUILD_EXAMPLES)
237
238if(protobuf_VERBOSE)
239    message(STATUS "Protocol Buffers Configuring done")
240endif()
241