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# The Intel compiler isn't able to deal with noinline member functions of 32# template classses defined in headers. As such it spams the output with 33# 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 39# Options 40if(WITH_PROTOC) 41 set(protobuf_PROTOC_EXE ${WITH_PROTOC} CACHE FILEPATH "Protocol Buffer Compiler executable" FORCE) 42endif() 43option(protobuf_BUILD_TESTS "Build tests" ON) 44option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF) 45option(protobuf_BUILD_EXAMPLES "Build examples" OFF) 46option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON) 47if (BUILD_SHARED_LIBS) 48 set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON) 49else (BUILD_SHARED_LIBS) 50 set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF) 51endif (BUILD_SHARED_LIBS) 52option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT}) 53include(CMakeDependentOption) 54cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON 55 "NOT protobuf_BUILD_SHARED_LIBS" OFF) 56set(protobuf_WITH_ZLIB_DEFAULT ON) 57option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT}) 58set(protobuf_DEBUG_POSTFIX "d" 59 CACHE STRING "Default debug postfix") 60mark_as_advanced(protobuf_DEBUG_POSTFIX) 61# User options 62include(protobuf-options.cmake) 63 64# Overrides for option dependencies 65if (protobuf_BUILD_PROTOC_BINARIES OR protobuf_BUILD_TESTS) 66 set(protobuf_BUILD_LIBPROTOC ON) 67else() 68 set(protobuf_BUILD_LIBPROTOC OFF) 69endif () 70# Path to main configure script 71set(protobuf_CONFIGURE_SCRIPT "../configure.ac") 72 73# 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}") 87# Parse version tweaks 88set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)([-]rc[-]|\\.)?([0-9]*)$") 89string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1" 90 protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}") 91string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2" 92 protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}") 93string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3" 94 protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}") 95string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\5" 96 protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}") 97 98message(STATUS "${protobuf_VERSION_PRERELEASE}") 99 100# Package version 101set(protobuf_VERSION 102 "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}") 103 104if(protobuf_VERSION_PRERELEASE) 105 set(protobuf_VERSION "${protobuf_VERSION}.${protobuf_VERSION_PRERELEASE}") 106else() 107 set(protobuf_VERSION "${protobuf_VERSION}.0") 108endif() 109message(STATUS "${protobuf_VERSION}") 110 111if(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 119add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD) 120 121find_package(Threads REQUIRED) 122if (CMAKE_USE_PTHREADS_INIT) 123 add_definitions(-DHAVE_PTHREAD) 124endif (CMAKE_USE_PTHREADS_INIT) 125 126set(_protobuf_FIND_ZLIB) 127if (protobuf_WITH_ZLIB) 128 find_package(ZLIB) 129 if (ZLIB_FOUND) 130 set(HAVE_ZLIB 1) 131 # FindZLIB module define ZLIB_INCLUDE_DIRS variable 132 # Set ZLIB_INCLUDE_DIRECTORIES for compatible 133 set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS}) 134 # Using imported target if exists 135 if (TARGET ZLIB::ZLIB) 136 set(ZLIB_LIBRARIES ZLIB::ZLIB) 137 set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()") 138 endif (TARGET ZLIB::ZLIB) 139 else (ZLIB_FOUND) 140 set(HAVE_ZLIB 0) 141 # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't 142 # complain when we use them later. 143 set(ZLIB_INCLUDE_DIRECTORIES) 144 set(ZLIB_LIBRARIES) 145 endif (ZLIB_FOUND) 146endif (protobuf_WITH_ZLIB) 147 148if (HAVE_ZLIB) 149 add_definitions(-DHAVE_ZLIB) 150endif (HAVE_ZLIB) 151 152# We need to link with libatomic on systems that do not have builtin atomics, or 153# don't have builtin support for 8 byte atomics 154set(protobuf_LINK_LIBATOMIC false) 155if (NOT MSVC) 156 include(CheckCXXSourceCompiles) 157 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 158 set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -std=c++11) 159 check_cxx_source_compiles(" 160 #include <atomic> 161 int main() { 162 return std::atomic<int64_t>{}; 163 } 164 " protobuf_HAVE_BUILTIN_ATOMICS) 165 if (NOT protobuf_HAVE_BUILTIN_ATOMICS) 166 set(protobuf_LINK_LIBATOMIC true) 167 endif (NOT protobuf_HAVE_BUILTIN_ATOMICS) 168 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 169endif (NOT MSVC) 170 171if (protobuf_BUILD_SHARED_LIBS) 172 set(protobuf_SHARED_OR_STATIC "SHARED") 173else (protobuf_BUILD_SHARED_LIBS) 174 set(protobuf_SHARED_OR_STATIC "STATIC") 175 # In case we are building static libraries, link also the runtime library statically 176 # so that MSVCR*.DLL is not required at runtime. 177 # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx 178 # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd 179 # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F 180 if (MSVC AND protobuf_MSVC_STATIC_RUNTIME) 181 foreach(flag_var 182 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE 183 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) 184 if(${flag_var} MATCHES "/MD") 185 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") 186 endif(${flag_var} MATCHES "/MD") 187 endforeach(flag_var) 188 endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME) 189endif (protobuf_BUILD_SHARED_LIBS) 190 191if (MSVC) 192 # Build with multiple processes 193 add_definitions(/MP) 194 # MSVC warning suppressions 195 add_definitions( 196 /wd4018 # 'expression' : signed/unsigned mismatch 197 /wd4065 # switch statement contains 'default' but no 'case' labels 198 /wd4146 # unary minus operator applied to unsigned type, result still unsigned 199 /wd4244 # 'conversion' conversion from 'type1' to 'type2', possible loss of data 200 /wd4251 # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2' 201 /wd4267 # 'var' : conversion from 'size_t' to 'type', possible loss of data 202 /wd4305 # 'identifier' : truncation from 'type1' to 'type2' 203 /wd4307 # 'operator' : integral constant overflow 204 /wd4309 # 'conversion' : truncation of constant value 205 /wd4334 # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?) 206 /wd4355 # 'this' : used in base member initializer list 207 /wd4506 # no definition for inline function 'function' 208 /wd4800 # 'type' : forcing value to bool 'true' or 'false' (performance warning) 209 /wd4996 # The compiler encountered a deprecated declaration. 210 ) 211 # Allow big object 212 add_definitions(/bigobj) 213 string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR}) 214 string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR}) 215 string(REPLACE "." "," protobuf_RC_FILEVERSION "${protobuf_VERSION}") 216 configure_file(extract_includes.bat.in extract_includes.bat) 217 218 # Suppress linker warnings about files with no symbols defined. 219 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4221") 220 221 # Configure Resource Compiler 222 enable_language(RC) 223 # use English language (0x409) in resource compiler 224 set(rc_flags "/l0x409") 225 # fix rc.exe invocations because of usage of add_definitions() 226 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_flags} <DEFINES> /fo<OBJECT> <SOURCE>") 227 228 configure_file(version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY) 229endif (MSVC) 230 231 232get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH) 233 234include_directories( 235 ${ZLIB_INCLUDE_DIRECTORIES} 236 ${protobuf_BINARY_DIR} 237 ${protobuf_source_dir}/src) 238 239if (MSVC) 240 # Add the "lib" prefix for generated .lib outputs. 241 set(LIB_PREFIX lib) 242else (MSVC) 243 # When building with "make", "lib" prefix will be added automatically by 244 # the build tool. 245 set(LIB_PREFIX) 246endif (MSVC) 247 248if (protobuf_UNICODE) 249 add_definitions(-DUNICODE -D_UNICODE) 250endif (protobuf_UNICODE) 251 252include(libprotobuf-lite.cmake) 253include(libprotobuf.cmake) 254if (protobuf_BUILD_LIBPROTOC) 255 include(libprotoc.cmake) 256endif (protobuf_BUILD_LIBPROTOC) 257if (protobuf_BUILD_PROTOC_BINARIES) 258 include(protoc.cmake) 259 if (NOT DEFINED protobuf_PROTOC_EXE) 260 set(protobuf_PROTOC_EXE protoc) 261 endif (NOT DEFINED protobuf_PROTOC_EXE) 262endif (protobuf_BUILD_PROTOC_BINARIES) 263 264# Ensure we have a protoc executable if we need one 265if (protobuf_BUILD_TESTS OR protobuf_BUILD_CONFORMANCE OR protobuf_BUILD_EXAMPLES) 266 if (NOT DEFINED protobuf_PROTOC_EXE) 267 find_program(protobuf_PROTOC_EXE protoc) 268 if (NOT protobuf_PROTOC_EXE) 269 message(FATAL "Build requires 'protoc' but binary not found and not building protoc.") 270 endif () 271 endif () 272 if(protobuf_VERBOSE) 273 message(STATUS "Using protoc : ${protobuf_PROTOC_EXE}") 274 endif(protobuf_VERBOSE) 275endif () 276 277if (protobuf_BUILD_TESTS) 278 include(tests.cmake) 279endif (protobuf_BUILD_TESTS) 280 281if (protobuf_BUILD_CONFORMANCE) 282 include(conformance.cmake) 283endif (protobuf_BUILD_CONFORMANCE) 284 285include(install.cmake) 286 287if (protobuf_BUILD_EXAMPLES) 288 include(examples.cmake) 289endif (protobuf_BUILD_EXAMPLES) 290 291if(protobuf_VERBOSE) 292 message(STATUS "Protocol Buffers Configuring done") 293endif(protobuf_VERBOSE) 294