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