1 # Minimum CMake required 2 cmake_minimum_required(VERSION 2.8.12) 3 4 if(protobuf_VERBOSE) 5 message(STATUS "Protocol Buffers Configuring...") 6 endif() 7 8 # CMake policies 9 cmake_policy(SET CMP0022 NEW) 10 11 # Project 12 project(protobuf C CXX) 13 14 # Options 15 option(protobuf_BUILD_TESTS "Build tests" ON) 16 option(protobuf_BUILD_EXAMPLES "Build examples" OFF) 17 if (BUILD_SHARED_LIBS) 18 set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON) 19 else (BUILD_SHARED_LIBS) 20 set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF) 21 endif (BUILD_SHARED_LIBS) 22 option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT}) 23 include(CMakeDependentOption) 24 cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON 25 "NOT protobuf_BUILD_SHARED_LIBS" OFF) 26 if (MSVC) 27 set(protobuf_WITH_ZLIB_DEFAULT OFF) 28 else (MSVC) 29 set(protobuf_WITH_ZLIB_DEFAULT ON) 30 endif (MSVC) 31 option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT}) 32 set(protobuf_DEBUG_POSTFIX "d" 33 CACHE STRING "Default debug postfix") 34 mark_as_advanced(protobuf_DEBUG_POSTFIX) 35 # User options 36 include(protobuf-options.cmake) 37 38 # Path to main configure script 39 set(protobuf_CONFIGURE_SCRIPT "../configure.ac") 40 41 # Parse configure script 42 set(protobuf_AC_INIT_REGEX 43 "^AC_INIT\\(\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\]\\)$") 44 file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_AC_INIT_LINE 45 LIMIT_COUNT 1 REGEX "^AC_INIT") 46 # Description 47 string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\1" 48 protobuf_DESCRIPTION "${protobuf_AC_INIT_LINE}") 49 # Version 50 string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\2" 51 protobuf_VERSION_STRING "${protobuf_AC_INIT_LINE}") 52 # Contact 53 string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\3" 54 protobuf_CONTACT "${protobuf_AC_INIT_LINE}") 55 # Parse version tweaks 56 set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)-?(.*)$") 57 string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1" 58 protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}") 59 string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2" 60 protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}") 61 string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3" 62 protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}") 63 string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\4" 64 protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}") 65 66 # Package version 67 set(protobuf_VERSION 68 "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}") 69 70 if(protobuf_VERSION_PRERELEASE) 71 set(protobuf_VERSION "${protobuf_VERSION}-${protobuf_VERSION_PRERELEASE}") 72 endif() 73 74 if(protobuf_VERBOSE) 75 message(STATUS "Configuration script parsing status [") 76 message(STATUS " Description : ${protobuf_DESCRIPTION}") 77 message(STATUS " Version : ${protobuf_VERSION} (${protobuf_VERSION_STRING})") 78 message(STATUS " Contact : ${protobuf_CONTACT}") 79 message(STATUS "]") 80 endif() 81 82 add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD) 83 84 find_package(Threads REQUIRED) 85 if (CMAKE_USE_PTHREADS_INIT) 86 add_definitions(-DHAVE_PTHREAD) 87 endif (CMAKE_USE_PTHREADS_INIT) 88 89 if (protobuf_WITH_ZLIB) 90 find_package(ZLIB) 91 if (ZLIB_FOUND) 92 set(HAVE_ZLIB 1) 93 # FindZLIB module define ZLIB_INCLUDE_DIRS variable 94 # Set ZLIB_INCLUDE_DIRECTORIES for compatible 95 set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS}) 96 # Using imported target if exists 97 if (TARGET ZLIB::ZLIB) 98 set(ZLIB_LIBRARIES ZLIB::ZLIB) 99 endif (TARGET ZLIB::ZLIB) 100 else (ZLIB_FOUND) 101 set(HAVE_ZLIB 0) 102 # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't 103 # complain when we use them later. 104 set(ZLIB_INCLUDE_DIRECTORIES) 105 set(ZLIB_LIBRARIES) 106 endif (ZLIB_FOUND) 107 endif (protobuf_WITH_ZLIB) 108 109 if (HAVE_ZLIB) 110 add_definitions(-DHAVE_ZLIB) 111 endif (HAVE_ZLIB) 112 113 if (protobuf_BUILD_SHARED_LIBS) 114 set(protobuf_SHARED_OR_STATIC "SHARED") 115 else (protobuf_BUILD_SHARED_LIBS) 116 set(protobuf_SHARED_OR_STATIC "STATIC") 117 # In case we are building static libraries, link also the runtime library statically 118 # so that MSVCR*.DLL is not required at runtime. 119 # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx 120 # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd 121 # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F 122 if (MSVC AND protobuf_MSVC_STATIC_RUNTIME) 123 foreach(flag_var 124 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE 125 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) 126 if(${flag_var} MATCHES "/MD") 127 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") 128 endif(${flag_var} MATCHES "/MD") 129 endforeach(flag_var) 130 endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME) 131 endif (protobuf_BUILD_SHARED_LIBS) 132 133 if (MSVC) 134 # Build with multiple processes 135 add_definitions(/MP) 136 add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305) 137 # Allow big object 138 add_definitions(/bigobj) 139 string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR}) 140 string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR}) 141 configure_file(extract_includes.bat.in extract_includes.bat) 142 endif (MSVC) 143 144 get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH) 145 146 include_directories( 147 ${ZLIB_INCLUDE_DIRECTORIES} 148 ${protobuf_BINARY_DIR} 149 ${protobuf_source_dir}/src) 150 151 if (MSVC) 152 # Add the "lib" prefix for generated .lib outputs. 153 set(LIB_PREFIX lib) 154 else (MSVC) 155 # When building with "make", "lib" prefix will be added automatically by 156 # the build tool. 157 set(LIB_PREFIX) 158 endif (MSVC) 159 160 include(libprotobuf-lite.cmake) 161 include(libprotobuf.cmake) 162 include(libprotoc.cmake) 163 include(protoc.cmake) 164 165 if (protobuf_BUILD_TESTS) 166 include(tests.cmake) 167 endif (protobuf_BUILD_TESTS) 168 169 include(install.cmake) 170 171 if (protobuf_BUILD_EXAMPLES) 172 include(examples.cmake) 173 endif (protobuf_BUILD_EXAMPLES) 174 175 if(protobuf_VERBOSE) 176 message(STATUS "Protocol Buffers Configuring done") 177 endif() 178