blob: 5c3b6e45129a20937da8c6febef10c813fb74975 [file] [log] [blame]
Konstantin Podsvirov82983432015-07-31 23:36:00 +03001# Minimum CMake required
Corentin Le Molgatc3b152c2018-04-24 17:13:15 +02002cmake_minimum_required(VERSION 3.1.3)
Feng Xiao4333edb2015-05-31 02:28:34 -07003
Konstantin Podsvirov71556292016-06-01 17:00:08 +03004if(protobuf_VERBOSE)
5 message(STATUS "Protocol Buffers Configuring...")
6endif()
Feng Xiao4333edb2015-05-31 02:28:34 -07007
Konstantin Podsvirov905f4642015-09-01 16:44:48 +03008# CMake policies
9cmake_policy(SET CMP0022 NEW)
Mizux7306f542018-05-22 21:52:07 +020010# 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()
Corentin Le Molgat1ec9beb2018-01-29 15:31:06 +010015if(POLICY CMP0048)
16 cmake_policy(SET CMP0048 NEW)
17endif()
18
Konstantin Podsvirov71556292016-06-01 17:00:08 +030019# Project
20project(protobuf C CXX)
21
Corentin Le Molgatc3b152c2018-04-24 17:13:15 +020022# Add c++11 flags
Ivan Shynkarenkaf80a8862018-05-02 02:51:08 +030023if (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()
Silver Chand3e8a542018-04-04 10:31:05 +080030
Chuck Atkins10c48c92020-02-11 10:22:38 -050031# The Intel compiler isn't able to deal with noinline member functions of
Peter Newmane2cc2de2020-08-10 19:08:25 +010032# template classes defined in headers. As such it spams the output with
Chuck Atkins10c48c92020-02-11 10:22:38 -050033# 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
Konstantin Podsvirov82983432015-07-31 23:36:00 +030039# Options
Michael WERLE976f85b2020-05-01 16:50:53 +090040if(WITH_PROTOC)
41 set(protobuf_PROTOC_EXE ${WITH_PROTOC} CACHE FILEPATH "Protocol Buffer Compiler executable" FORCE)
42endif()
Konstantin Podsvirov673d32e2015-09-15 15:30:02 +030043option(protobuf_BUILD_TESTS "Build tests" ON)
Josh Haberman42496922018-08-24 12:27:21 -070044option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
Konstantin Podsvirov71556292016-06-01 17:00:08 +030045option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
Yangqing Jiacba18ef2017-11-13 15:15:39 -080046option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
Daniel Joosc8f76332020-10-10 00:54:39 +020047option(protobuf_BUILD_LIBPROTOC "Build libprotoc" OFF)
Florian Simonb9a036b2021-03-04 11:39:28 -050048option(protobuf_DISABLE_RTTI "Remove runtime type information in the binaries" OFF)
Konstantin Podsvirovc3aa4c22015-10-15 02:56:48 +030049if (BUILD_SHARED_LIBS)
50 set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
51else (BUILD_SHARED_LIBS)
52 set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
53endif (BUILD_SHARED_LIBS)
54option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
Walter Gray4150a912016-06-09 13:25:27 -070055include(CMakeDependentOption)
Rafi Kamalde756512019-11-20 18:03:29 -080056cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON
57 "NOT protobuf_BUILD_SHARED_LIBS" OFF)
Corentin Le Molgat8dd0f4e2018-01-29 15:16:40 +010058set(protobuf_WITH_ZLIB_DEFAULT ON)
Konstantin Podsvirov620bd742015-09-15 15:31:25 +030059option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
Konstantin Podsvirovf397ede2015-09-17 13:00:12 +030060set(protobuf_DEBUG_POSTFIX "d"
61 CACHE STRING "Default debug postfix")
Konstantin Podsvirov71556292016-06-01 17:00:08 +030062mark_as_advanced(protobuf_DEBUG_POSTFIX)
63# User options
Walter Gray23fef562016-06-06 10:59:58 -070064include(protobuf-options.cmake)
Feng Xiao4333edb2015-05-31 02:28:34 -070065
Michael WERLE976f85b2020-05-01 16:50:53 +090066# Overrides for option dependencies
67if (protobuf_BUILD_PROTOC_BINARIES OR protobuf_BUILD_TESTS)
68 set(protobuf_BUILD_LIBPROTOC ON)
Michael WERLE976f85b2020-05-01 16:50:53 +090069endif ()
Konstantin Podsvirov743ec442015-08-01 02:01:42 +030070# Path to main configure script
71set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
Konstantin Podsvirov82983432015-07-31 23:36:00 +030072
Konstantin Podsvirovdb014602015-08-31 15:20:18 +030073# Parse configure script
74set(protobuf_AC_INIT_REGEX
75 "^AC_INIT\\(\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\]\\)$")
76file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_AC_INIT_LINE
77 LIMIT_COUNT 1 REGEX "^AC_INIT")
78# Description
79string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\1"
80 protobuf_DESCRIPTION "${protobuf_AC_INIT_LINE}")
81# Version
82string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\2"
83 protobuf_VERSION_STRING "${protobuf_AC_INIT_LINE}")
84# Contact
85string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\3"
86 protobuf_CONTACT "${protobuf_AC_INIT_LINE}")
Konstantin Podsvirov743ec442015-08-01 02:01:42 +030087# Parse version tweaks
Paul Yang5b4ac532019-02-01 18:43:55 -080088set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)([-]rc[-]|\\.)?([0-9]*)$")
Konstantin Podsvirovdb014602015-08-31 15:20:18 +030089string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1"
Konstantin Podsvirov743ec442015-08-01 02:01:42 +030090 protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
Konstantin Podsvirovdb014602015-08-31 15:20:18 +030091string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2"
Konstantin Podsvirov743ec442015-08-01 02:01:42 +030092 protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
Konstantin Podsvirovdb014602015-08-31 15:20:18 +030093string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3"
Konstantin Podsvirov743ec442015-08-01 02:01:42 +030094 protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
Paul Yang5b4ac532019-02-01 18:43:55 -080095string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\5"
Walter Grayf1091ab2016-05-27 22:48:24 -070096 protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")
97
Paul Yang5b4ac532019-02-01 18:43:55 -080098message(STATUS "${protobuf_VERSION_PRERELEASE}")
99
Konstantin Podsvirov743ec442015-08-01 02:01:42 +0300100# Package version
Konstantin Podsvirov82983432015-07-31 23:36:00 +0300101set(protobuf_VERSION
Konstantin Podsvirov743ec442015-08-01 02:01:42 +0300102 "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
Konstantin Podsvirov82983432015-07-31 23:36:00 +0300103
Walter Grayf1091ab2016-05-27 22:48:24 -0700104if(protobuf_VERSION_PRERELEASE)
Paul Yang5b4ac532019-02-01 18:43:55 -0800105 set(protobuf_VERSION "${protobuf_VERSION}.${protobuf_VERSION_PRERELEASE}")
106else()
107 set(protobuf_VERSION "${protobuf_VERSION}.0")
Walter Grayf1091ab2016-05-27 22:48:24 -0700108endif()
Paul Yang5b4ac532019-02-01 18:43:55 -0800109message(STATUS "${protobuf_VERSION}")
Walter Grayf1091ab2016-05-27 22:48:24 -0700110
Konstantin Podsvirovdb014602015-08-31 15:20:18 +0300111if(protobuf_VERBOSE)
112 message(STATUS "Configuration script parsing status [")
113 message(STATUS " Description : ${protobuf_DESCRIPTION}")
114 message(STATUS " Version : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
115 message(STATUS " Contact : ${protobuf_CONTACT}")
116 message(STATUS "]")
117endif()
118
Jisi Liu78d470c2015-06-12 15:49:21 -0700119add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
120
Florian Simon1add7a72021-02-25 15:11:40 -0500121if (protobuf_DISABLE_RTTI)
122 add_definitions(-DGOOGLE_PROTOBUF_NO_RTTI=1)
123endif()
124
Feng Xiao4333edb2015-05-31 02:28:34 -0700125find_package(Threads REQUIRED)
126if (CMAKE_USE_PTHREADS_INIT)
Jisi Liu78d470c2015-06-12 15:49:21 -0700127 add_definitions(-DHAVE_PTHREAD)
Feng Xiao4333edb2015-05-31 02:28:34 -0700128endif (CMAKE_USE_PTHREADS_INIT)
129
Konstantin Podsviroveefd1fd2016-08-02 22:37:36 +0300130set(_protobuf_FIND_ZLIB)
Konstantin Podsvirov620bd742015-09-15 15:31:25 +0300131if (protobuf_WITH_ZLIB)
Feng Xiao4333edb2015-05-31 02:28:34 -0700132 find_package(ZLIB)
133 if (ZLIB_FOUND)
134 set(HAVE_ZLIB 1)
Konstantin Podsvirov20b882d2015-08-31 16:23:40 +0300135 # FindZLIB module define ZLIB_INCLUDE_DIRS variable
136 # Set ZLIB_INCLUDE_DIRECTORIES for compatible
137 set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
138 # Using imported target if exists
139 if (TARGET ZLIB::ZLIB)
140 set(ZLIB_LIBRARIES ZLIB::ZLIB)
Konstantin Podsviroveefd1fd2016-08-02 22:37:36 +0300141 set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()")
Konstantin Podsvirov20b882d2015-08-31 16:23:40 +0300142 endif (TARGET ZLIB::ZLIB)
Feng Xiao4333edb2015-05-31 02:28:34 -0700143 else (ZLIB_FOUND)
144 set(HAVE_ZLIB 0)
145 # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
146 # complain when we use them later.
147 set(ZLIB_INCLUDE_DIRECTORIES)
148 set(ZLIB_LIBRARIES)
149 endif (ZLIB_FOUND)
Konstantin Podsvirov620bd742015-09-15 15:31:25 +0300150endif (protobuf_WITH_ZLIB)
Feng Xiao4333edb2015-05-31 02:28:34 -0700151
Jisi Liu78d470c2015-06-12 15:49:21 -0700152if (HAVE_ZLIB)
153 add_definitions(-DHAVE_ZLIB)
154endif (HAVE_ZLIB)
155
boscosiu55ed1d42019-07-03 06:34:09 -0700156# We need to link with libatomic on systems that do not have builtin atomics, or
157# don't have builtin support for 8 byte atomics
158set(protobuf_LINK_LIBATOMIC false)
159if (NOT MSVC)
160 include(CheckCXXSourceCompiles)
Ben Bader56e7bdf2019-07-08 11:13:53 -0700161 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
162 set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -std=c++11)
boscosiu55ed1d42019-07-03 06:34:09 -0700163 check_cxx_source_compiles("
164 #include <atomic>
165 int main() {
166 return std::atomic<int64_t>{};
167 }
168 " protobuf_HAVE_BUILTIN_ATOMICS)
169 if (NOT protobuf_HAVE_BUILTIN_ATOMICS)
170 set(protobuf_LINK_LIBATOMIC true)
171 endif (NOT protobuf_HAVE_BUILTIN_ATOMICS)
Ben Bader56e7bdf2019-07-08 11:13:53 -0700172 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
boscosiu55ed1d42019-07-03 06:34:09 -0700173endif (NOT MSVC)
174
Konstantin Podsvirovc3aa4c22015-10-15 02:56:48 +0300175if (protobuf_BUILD_SHARED_LIBS)
176 set(protobuf_SHARED_OR_STATIC "SHARED")
177else (protobuf_BUILD_SHARED_LIBS)
178 set(protobuf_SHARED_OR_STATIC "STATIC")
Rafi Kamalde756512019-11-20 18:03:29 -0800179 # In case we are building static libraries, link also the runtime library statically
180 # so that MSVCR*.DLL is not required at runtime.
181 # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
182 # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
183 # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
184 if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
185 foreach(flag_var
186 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
187 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
188 if(${flag_var} MATCHES "/MD")
189 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
190 endif(${flag_var} MATCHES "/MD")
191 endforeach(flag_var)
192 endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
Konstantin Podsvirovc3aa4c22015-10-15 02:56:48 +0300193endif (protobuf_BUILD_SHARED_LIBS)
Feng Xiao4333edb2015-05-31 02:28:34 -0700194
Feng Xiaodffd5422015-06-05 17:59:09 -0700195if (MSVC)
Benjamin Bennett9fc4d902015-12-07 13:54:02 -0600196 # Build with multiple processes
197 add_definitions(/MP)
Tim Ebringera23669c2017-10-21 09:40:32 -0400198 # MSVC warning suppressions
199 add_definitions(
200 /wd4018 # 'expression' : signed/unsigned mismatch
201 /wd4065 # switch statement contains 'default' but no 'case' labels
202 /wd4146 # unary minus operator applied to unsigned type, result still unsigned
203 /wd4244 # 'conversion' conversion from 'type1' to 'type2', possible loss of data
204 /wd4251 # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
205 /wd4267 # 'var' : conversion from 'size_t' to 'type', possible loss of data
206 /wd4305 # 'identifier' : truncation from 'type1' to 'type2'
207 /wd4307 # 'operator' : integral constant overflow
208 /wd4309 # 'conversion' : truncation of constant value
209 /wd4334 # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
210 /wd4355 # 'this' : used in base member initializer list
211 /wd4506 # no definition for inline function 'function'
212 /wd4800 # 'type' : forcing value to bool 'true' or 'false' (performance warning)
213 /wd4996 # The compiler encountered a deprecated declaration.
214 )
Jisi Liu3b6df062016-04-01 15:02:45 -0700215 # Allow big object
216 add_definitions(/bigobj)
Feng Xiaodffd5422015-06-05 17:59:09 -0700217 string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
218 string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
Jozef Izso34152012018-04-27 23:44:38 +0200219 string(REPLACE "." "," protobuf_RC_FILEVERSION "${protobuf_VERSION}")
Feng Xiaodffd5422015-06-05 17:59:09 -0700220 configure_file(extract_includes.bat.in extract_includes.bat)
Ivan Shynkarenkaf80a8862018-05-02 02:51:08 +0300221
Tim Ebringer2a728402017-10-06 14:26:43 -0400222 # Suppress linker warnings about files with no symbols defined.
Changming Sun95a1c4f2019-06-04 16:39:10 -0700223 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4221")
Jozef Izso34152012018-04-27 23:44:38 +0200224
225 # Configure Resource Compiler
226 enable_language(RC)
227 # use English language (0x409) in resource compiler
228 set(rc_flags "/l0x409")
229 # fix rc.exe invocations because of usage of add_definitions()
230 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_flags} <DEFINES> /fo<OBJECT> <SOURCE>")
231
232 configure_file(version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
Feng Xiaodffd5422015-06-05 17:59:09 -0700233endif (MSVC)
Feng Xiao4333edb2015-05-31 02:28:34 -0700234
Jozef Izso34152012018-04-27 23:44:38 +0200235
Feng Xiao4333edb2015-05-31 02:28:34 -0700236get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)
237
238include_directories(
239 ${ZLIB_INCLUDE_DIRECTORIES}
240 ${protobuf_BINARY_DIR}
241 ${protobuf_source_dir}/src)
242
243if (MSVC)
244 # Add the "lib" prefix for generated .lib outputs.
245 set(LIB_PREFIX lib)
246else (MSVC)
247 # When building with "make", "lib" prefix will be added automatically by
248 # the build tool.
249 set(LIB_PREFIX)
250endif (MSVC)
251
Wei-Yin Chen (陳威尹)a7eaf362016-09-01 17:12:49 -0700252if (protobuf_UNICODE)
Wei-Yin Chen (陳威尹)588a8032016-08-19 15:25:54 -0700253 add_definitions(-DUNICODE -D_UNICODE)
Wei-Yin Chen (陳威尹)a7eaf362016-09-01 17:12:49 -0700254endif (protobuf_UNICODE)
Wei-Yin Chen (陳威尹)588a8032016-08-19 15:25:54 -0700255
Feng Xiao4333edb2015-05-31 02:28:34 -0700256include(libprotobuf-lite.cmake)
257include(libprotobuf.cmake)
Michael WERLE976f85b2020-05-01 16:50:53 +0900258if (protobuf_BUILD_LIBPROTOC)
Yangqing Jiacba18ef2017-11-13 15:15:39 -0800259 include(libprotoc.cmake)
Michael WERLE976f85b2020-05-01 16:50:53 +0900260endif (protobuf_BUILD_LIBPROTOC)
261if (protobuf_BUILD_PROTOC_BINARIES)
Yangqing Jiacba18ef2017-11-13 15:15:39 -0800262 include(protoc.cmake)
Michael WERLE976f85b2020-05-01 16:50:53 +0900263 if (NOT DEFINED protobuf_PROTOC_EXE)
264 set(protobuf_PROTOC_EXE protoc)
265 endif (NOT DEFINED protobuf_PROTOC_EXE)
Yangqing Jiacba18ef2017-11-13 15:15:39 -0800266endif (protobuf_BUILD_PROTOC_BINARIES)
Konstantin Podsvirov82983432015-07-31 23:36:00 +0300267
Michael WERLE976f85b2020-05-01 16:50:53 +0900268# Ensure we have a protoc executable if we need one
269if (protobuf_BUILD_TESTS OR protobuf_BUILD_CONFORMANCE OR protobuf_BUILD_EXAMPLES)
270 if (NOT DEFINED protobuf_PROTOC_EXE)
271 find_program(protobuf_PROTOC_EXE protoc)
272 if (NOT protobuf_PROTOC_EXE)
273 message(FATAL "Build requires 'protoc' but binary not found and not building protoc.")
274 endif ()
275 endif ()
276 if(protobuf_VERBOSE)
277 message(STATUS "Using protoc : ${protobuf_PROTOC_EXE}")
278 endif(protobuf_VERBOSE)
279endif ()
280
Konstantin Podsvirov673d32e2015-09-15 15:30:02 +0300281if (protobuf_BUILD_TESTS)
Feng Xiaoe9839ff2015-06-05 21:24:23 -0700282 include(tests.cmake)
Konstantin Podsvirov673d32e2015-09-15 15:30:02 +0300283endif (protobuf_BUILD_TESTS)
Konstantin Podsvirov82983432015-07-31 23:36:00 +0300284
Josh Haberman42496922018-08-24 12:27:21 -0700285if (protobuf_BUILD_CONFORMANCE)
286 include(conformance.cmake)
287endif (protobuf_BUILD_CONFORMANCE)
288
Konstantin Podsvirov82983432015-07-31 23:36:00 +0300289include(install.cmake)
Konstantin Podsvirov71556292016-06-01 17:00:08 +0300290
291if (protobuf_BUILD_EXAMPLES)
292 include(examples.cmake)
293endif (protobuf_BUILD_EXAMPLES)
294
295if(protobuf_VERBOSE)
Michael WERLE976f85b2020-05-01 16:50:53 +0900296 message(STATUS "Protocol Buffers Configuring done")
297endif(protobuf_VERBOSE)