1 # Minimum CMake required. If available, accept the policy-controlled behavior up
3 cmake_minimum_required(VERSION 3.10...3.26)
5 # Revert to old behavior for MSVC debug symbols.
7 cmake_policy(SET CMP0141 OLD)
11 message(STATUS "Protocol Buffers Configuring...")
15 project(protobuf C CXX)
17 if(protobuf_DEPRECATED_CMAKE_SUBDIRECTORY_USAGE)
18 if(CMAKE_PROJECT_NAME STREQUAL "protobuf")
19 get_filename_component(CMAKE_SOURCE_DIR ${CMAKE_SOURCE_DIR} DIRECTORY)
21 get_filename_component(CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
22 get_filename_component(PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR} DIRECTORY)
23 get_filename_component(protobuf_SOURCE_DIR ${protobuf_SOURCE_DIR} DIRECTORY)
27 option(protobuf_INSTALL "Install protobuf binaries and files" ON)
28 option(protobuf_BUILD_TESTS "Build tests" ON)
29 option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
30 option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
31 option(protobuf_BUILD_PROTOBUF_BINARIES "Build protobuf libraries and protoc compiler" ON)
32 option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
33 option(protobuf_BUILD_LIBPROTOC "Build libprotoc" OFF)
34 option(protobuf_BUILD_LIBUPB "Build libupb" ON)
35 option(protobuf_DISABLE_RTTI "Remove runtime type information in the binaries" OFF)
36 option(protobuf_TEST_XML_OUTDIR "Output directory for XML logs from tests." "")
37 option(protobuf_ALLOW_CCACHE "Adjust build flags to allow for ccache support." OFF)
38 if (BUILD_SHARED_LIBS)
39 set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
40 else (BUILD_SHARED_LIBS)
41 set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
42 endif (BUILD_SHARED_LIBS)
43 option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
44 include(CMakeDependentOption)
45 cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON
46 "NOT protobuf_BUILD_SHARED_LIBS" OFF)
47 set(protobuf_WITH_ZLIB_DEFAULT ON)
48 option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
49 set(protobuf_DEBUG_POSTFIX "d"
50 CACHE STRING "Default debug postfix")
51 mark_as_advanced(protobuf_DEBUG_POSTFIX)
54 set(protobuf_PROTOC_EXE protoc)
55 set(protobuf_BUILD_PROTOC_BINARIES OFF)
56 add_executable(protoc IMPORTED GLOBAL)
57 add_executable(protobuf::protoc ALIAS protoc)
58 set_property(TARGET protoc PROPERTY IMPORTED_LOCATION ${WITH_PROTOC})
62 include(${protobuf_SOURCE_DIR}/cmake/protobuf-options.cmake)
64 if (protobuf_BUILD_SHARED_LIBS)
65 # This is necessary for linking in Abseil.
66 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
68 # Build Abseil as shared libraries to avoid ODR violations.
69 set(BUILD_SHARED_LIBS ON)
71 # Output directory is correct by default for most build setups. However, when
72 # building Protobuf as a DLL, it is important to have the DLL in the same
73 # directory as the executable using it. Thus, we put all binaries in a single
76 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
77 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
78 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
79 set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
84 set(protobuf_VERSION_STRING "5.28.0")
85 set(protobuf_DESCRIPTION "Protocol Buffers")
86 set(protobuf_CONTACT "protobuf@googlegroups.com")
88 # Overrides for option dependencies
89 if (protobuf_BUILD_PROTOC_BINARIES OR protobuf_BUILD_TESTS)
90 set(protobuf_BUILD_LIBPROTOC ON)
92 if (NOT protobuf_BUILD_PROTOBUF_BINARIES)
93 set(protobuf_INSTALL OFF)
95 # Parse version tweaks
96 set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)([-]rc[-]|\\.)?([0-9]*)$")
97 string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1"
98 protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
99 string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2"
100 protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
101 string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3"
102 protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
103 string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\5"
104 protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")
106 message(STATUS "${protobuf_VERSION_PRERELEASE}")
110 "${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
112 if(protobuf_VERSION_PRERELEASE)
113 set(protobuf_VERSION "${protobuf_VERSION}.${protobuf_VERSION_PRERELEASE}")
115 set(protobuf_VERSION "${protobuf_VERSION}.0")
117 message(STATUS "${protobuf_VERSION}")
120 message(STATUS "Configuration script parsing status [")
121 message(STATUS " Description : ${protobuf_DESCRIPTION}")
122 message(STATUS " Version : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
123 message(STATUS " Contact : ${protobuf_CONTACT}")
127 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map
134 # CheckLinkerFlag module available in CMake >=3.18.
135 if(${CMAKE_VERSION} VERSION_GREATER 3.18 OR ${CMAKE_VERSION} VERSION_EQUAL 3.18)
136 include(CheckLinkerFlag)
137 check_linker_flag(CXX -Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map protobuf_HAVE_LD_VERSION_SCRIPT)
139 include(CheckCXXSourceCompiles)
140 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
141 set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map)
142 check_cxx_source_compiles("
146 " protobuf_HAVE_LD_VERSION_SCRIPT)
147 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
149 file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map)
151 find_package(Threads REQUIRED)
153 # We can install dependencies from submodules if we're running
154 # CMake v3.13 or newer.
155 if(CMAKE_VERSION VERSION_LESS 3.13)
156 set(_protobuf_INSTALL_SUPPORTED_FROM_MODULE OFF)
158 set(_protobuf_INSTALL_SUPPORTED_FROM_MODULE ON)
161 set(_protobuf_FIND_ZLIB)
162 if (protobuf_WITH_ZLIB)
166 # FindZLIB module define ZLIB_INCLUDE_DIRS variable
167 # Set ZLIB_INCLUDE_DIRECTORIES for compatible
168 set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
169 # Using imported target if exists
170 if (TARGET ZLIB::ZLIB)
171 set(ZLIB_LIBRARIES ZLIB::ZLIB)
172 set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()")
173 endif (TARGET ZLIB::ZLIB)
176 # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
177 # complain when we use them later.
178 set(ZLIB_INCLUDE_DIRECTORIES)
181 endif (protobuf_WITH_ZLIB)
183 # We need to link with libatomic on systems that do not have builtin atomics, or
184 # don't have builtin support for 8 byte atomics
185 set(protobuf_LINK_LIBATOMIC false)
187 include(CheckCXXSourceCompiles)
188 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
189 set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -std=c++14)
190 check_cxx_source_compiles("
193 return static_cast<int>(std::atomic<int64_t>{});
195 " protobuf_HAVE_BUILTIN_ATOMICS)
196 if (NOT protobuf_HAVE_BUILTIN_ATOMICS)
197 set(protobuf_LINK_LIBATOMIC true)
198 endif (NOT protobuf_HAVE_BUILTIN_ATOMICS)
199 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
202 if (protobuf_BUILD_SHARED_LIBS)
203 set(protobuf_SHARED_OR_STATIC "SHARED")
204 else (protobuf_BUILD_SHARED_LIBS)
205 set(protobuf_SHARED_OR_STATIC "STATIC")
206 # The CMAKE_<LANG>_FLAGS(_<BUILD_TYPE>)? is meant to be user controlled.
207 # Prior to CMake 3.15, the MSVC runtime library was pushed into the same flags
208 # making programmatic control difficult. Prefer the functionality in newer
209 # CMake versions when available.
210 if(${CMAKE_VERSION} VERSION_GREATER 3.15 OR ${CMAKE_VERSION} VERSION_EQUAL 3.15)
211 if (protobuf_MSVC_STATIC_RUNTIME)
212 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)
214 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)
217 # In case we are building static libraries, link also the runtime library statically
218 # so that MSVCR*.DLL is not required at runtime.
219 # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
220 # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
221 # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
222 if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
224 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
225 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
226 if(${flag_var} MATCHES "/MD")
227 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
228 endif(${flag_var} MATCHES "/MD")
230 endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
232 endif (protobuf_BUILD_SHARED_LIBS)
234 # Export all symbols on Windows when building shared libraries
235 SET(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
238 string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
239 string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
240 string(REPLACE "." "," protobuf_RC_FILEVERSION "${protobuf_VERSION}")
242 if (protobuf_ALLOW_CCACHE)
243 # In order to support ccache, we need to remove the /Zi option because it
244 # puts debug symbols into separate pdb files (which in incompatible with
245 # ccache). This can be replaced with /Z7 to preserve debug symbols, which
246 # embeds debug symbols into the object files instead of creating a separate
247 # pdb file, which isn't currently supported by ccache. However, this bloats
248 # the ccache size by about a factor of 2x, making it very expensive in CI.
249 # Instead, we strip debug symbols to reduce this overhead.
252 CMAKE_CXX_FLAGS_DEBUG
253 CMAKE_C_FLAGS_RELWITHDEBINFO
254 CMAKE_CXX_FLAGS_RELWITHDEBINFO
256 string(REGEX REPLACE "[-/]Z[iI7]" "/DEBUG:NONE" ${v} "${${v}}")
260 # Suppress linker warnings about files with no symbols defined.
261 string(APPEND CMAKE_STATIC_LINKER_FLAGS " /ignore:4221")
263 # use English language (0x409) in resource compiler
264 string(APPEND CMAKE_RC_FLAGS " -l0x409")
266 # Generate the version.rc file used elsewhere.
267 configure_file(${protobuf_SOURCE_DIR}/cmake/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
268 set(protobuf_version_rc_file ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
270 # Add the "lib" prefix for generated .lib outputs.
273 # No version.rc file.
274 set(protobuf_version_rc_file)
276 # When building with "make", "lib" prefix will be added automatically by
282 ${ZLIB_INCLUDE_DIRECTORIES}
283 ${protobuf_BINARY_DIR}
284 # Support #include-ing other top-level directories, i.e. upb_generator.
285 ${protobuf_SOURCE_DIR}
286 ${protobuf_BINARY_DIR}/src
287 ${protobuf_SOURCE_DIR}/src)
289 set(protobuf_ABSL_PROVIDER "module" CACHE STRING "Provider of absl library")
290 set_property(CACHE protobuf_ABSL_PROVIDER PROPERTY STRINGS "module" "package")
292 set(protobuf_JSONCPP_PROVIDER "module" CACHE STRING "Provider of jsoncpp library")
293 set_property(CACHE protobuf_JSONCPP_PROVIDER PROPERTY STRINGS "module" "package")
295 if (protobuf_BUILD_TESTS)
296 include(${protobuf_SOURCE_DIR}/cmake/gtest.cmake)
297 endif (protobuf_BUILD_TESTS)
299 include(${protobuf_SOURCE_DIR}/cmake/abseil-cpp.cmake)
301 if (protobuf_BUILD_PROTOBUF_BINARIES)
302 include(${protobuf_SOURCE_DIR}/cmake/utf8_range.cmake)
303 include(${protobuf_SOURCE_DIR}/cmake/libprotobuf-lite.cmake)
304 if (NOT DEFINED protobuf_LIB_PROTOBUF_LITE)
305 set(protobuf_LIB_PROTOBUF_LITE libprotobuf-lite)
307 include(${protobuf_SOURCE_DIR}/cmake/libprotobuf.cmake)
308 if (NOT DEFINED protobuf_LIB_PROTOBUF)
309 set(protobuf_LIB_PROTOBUF libprotobuf)
311 if (protobuf_BUILD_LIBPROTOC)
312 include(${protobuf_SOURCE_DIR}/cmake/libprotoc.cmake)
313 if (NOT DEFINED protobuf_LIB_PROTOC)
314 set(protobuf_LIB_PROTOC libprotoc)
317 if (protobuf_BUILD_LIBUPB)
318 include(${protobuf_SOURCE_DIR}/cmake/libupb.cmake)
319 if (NOT DEFINED protobuf_LIB_UPB)
320 set(protobuf_LIB_UPB libupb)
322 include(${protobuf_SOURCE_DIR}/cmake/upb_generators.cmake)
324 if (protobuf_BUILD_PROTOC_BINARIES)
325 include(${protobuf_SOURCE_DIR}/cmake/protoc.cmake)
326 if (NOT DEFINED protobuf_PROTOC_EXE)
327 set(protobuf_PROTOC_EXE protoc)
331 find_package(Protobuf NO_MODULE)
333 set(protobuf_PROTOC_EXE protobuf::protoc)
334 set(protobuf_LIB_PROTOC protobuf::libprotoc)
335 set(protobuf_LIB_PROTOBUF protobuf::libprotobuf)
336 set(protobuf_LIB_PROTOBUF_LITE protobuf::libprotobuf-lite)
337 set(protobuf_LIB_UPB protobuf::libupb)
338 message(STATUS "CMake installation of Protobuf found.")
342 # Ensure we have a protoc executable and protobuf libraries if we need one
343 if (protobuf_BUILD_TESTS OR protobuf_BUILD_CONFORMANCE OR protobuf_BUILD_EXAMPLES)
344 if (NOT DEFINED protobuf_PROTOC_EXE)
345 find_program(protobuf_PROTOC_EXE protoc REQUIRED)
346 message(STATUS "Found system ${protobuf_PROTOC_EXE}.")
349 message(STATUS "Using protoc : ${protobuf_PROTOC_EXE}")
350 message(STATUS "Using libprotobuf : ${protobuf_LIB_PROTOBUF}")
351 message(STATUS "Using libprotobuf-lite : ${protobuf_LIB_PROTOBUF_LITE}")
352 message(STATUS "Using libprotoc : ${protobuf_LIB_PROTOC}")
353 message(STATUS "Using libupb : ${protobuf_LIB_UPB}")
354 endif(protobuf_VERBOSE)
357 if (protobuf_BUILD_TESTS)
359 include(${protobuf_SOURCE_DIR}/cmake/tests.cmake)
360 endif (protobuf_BUILD_TESTS)
362 if (protobuf_BUILD_CONFORMANCE)
363 include(${protobuf_SOURCE_DIR}/cmake/conformance.cmake)
364 endif (protobuf_BUILD_CONFORMANCE)
366 if (protobuf_INSTALL)
367 include(${protobuf_SOURCE_DIR}/cmake/install.cmake)
368 endif (protobuf_INSTALL)
370 if (protobuf_BUILD_EXAMPLES)
371 include(${protobuf_SOURCE_DIR}/cmake/examples.cmake)
372 endif (protobuf_BUILD_EXAMPLES)
375 message(STATUS "Protocol Buffers Configuring done")
376 endif(protobuf_VERBOSE)