CMakeLists.txt 8.28 KB
Newer Older
1
# Minimum CMake required
2
cmake_minimum_required(VERSION 3.1.3)
Feng Xiao's avatar
Feng Xiao committed
3

4 5 6
if(protobuf_VERBOSE)
  message(STATUS "Protocol Buffers Configuring...")
endif()
Feng Xiao's avatar
Feng Xiao committed
7

8 9
# CMake policies
cmake_policy(SET CMP0022 NEW)
10 11 12 13 14
# On MacOS use @rpath/ for target's install name prefix path
if (POLICY CMP0042)
  cmake_policy(SET CMP0042 NEW)
endif ()
# Clear VERSION variables when no VERSION is given to project()
15 16 17 18
if(POLICY CMP0048)
  cmake_policy(SET CMP0048 NEW)
endif()

19 20 21
# Project
project(protobuf C CXX)

22
# Add c++11 flags
Ivan Shynkarenka's avatar
Ivan Shynkarenka committed
23 24 25 26 27 28 29
if (CYGWIN)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
else()
  set(CMAKE_CXX_STANDARD 11)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
  set(CMAKE_CXX_EXTENSIONS OFF)
endif()
30

31
# Options
32
option(protobuf_BUILD_TESTS "Build tests" ON)
33
option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
34
option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
35
option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
36 37 38 39 40 41
if (BUILD_SHARED_LIBS)
  set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
else (BUILD_SHARED_LIBS)
  set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
endif (BUILD_SHARED_LIBS)
option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
42 43 44
include(CMakeDependentOption)
cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON
  "NOT protobuf_BUILD_SHARED_LIBS" OFF)
45
set(protobuf_WITH_ZLIB_DEFAULT ON)
46
option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
47 48
set(protobuf_DEBUG_POSTFIX "d"
  CACHE STRING "Default debug postfix")
49 50
mark_as_advanced(protobuf_DEBUG_POSTFIX)
# User options
51
include(protobuf-options.cmake)
Feng Xiao's avatar
Feng Xiao committed
52

53 54
# Path to main configure script
set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69
# Parse configure script
set(protobuf_AC_INIT_REGEX
  "^AC_INIT\\(\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\]\\)$")
file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_AC_INIT_LINE
  LIMIT_COUNT 1 REGEX "^AC_INIT")
# Description
string(REGEX REPLACE        "${protobuf_AC_INIT_REGEX}" "\\1"
    protobuf_DESCRIPTION    "${protobuf_AC_INIT_LINE}")
# Version
string(REGEX REPLACE        "${protobuf_AC_INIT_REGEX}" "\\2"
    protobuf_VERSION_STRING "${protobuf_AC_INIT_LINE}")
# Contact
string(REGEX REPLACE        "${protobuf_AC_INIT_REGEX}" "\\3"
    protobuf_CONTACT        "${protobuf_AC_INIT_LINE}")
70
# Parse version tweaks
71
set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)-?(.*)$")
72
string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\1"
73
  protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
74
string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\2"
75
  protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
76
string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\3"
77
  protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
78 79 80
string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\4"
  protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")

81
# Package version
82
set(protobuf_VERSION
83
  "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
84

85 86 87 88
if(protobuf_VERSION_PRERELEASE)
  set(protobuf_VERSION "${protobuf_VERSION}-${protobuf_VERSION_PRERELEASE}")
endif()

89 90 91 92 93 94 95 96
if(protobuf_VERBOSE)
  message(STATUS "Configuration script parsing status [")
  message(STATUS "  Description : ${protobuf_DESCRIPTION}")
  message(STATUS "  Version     : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
  message(STATUS "  Contact     : ${protobuf_CONTACT}")
  message(STATUS "]")
endif()

97 98
add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)

Feng Xiao's avatar
Feng Xiao committed
99 100
find_package(Threads REQUIRED)
if (CMAKE_USE_PTHREADS_INIT)
101
  add_definitions(-DHAVE_PTHREAD)
Feng Xiao's avatar
Feng Xiao committed
102 103
endif (CMAKE_USE_PTHREADS_INIT)

104
set(_protobuf_FIND_ZLIB)
105
if (protobuf_WITH_ZLIB)
Feng Xiao's avatar
Feng Xiao committed
106 107 108
  find_package(ZLIB)
  if (ZLIB_FOUND)
    set(HAVE_ZLIB 1)
109 110 111 112 113 114
    # FindZLIB module define ZLIB_INCLUDE_DIRS variable
    # Set ZLIB_INCLUDE_DIRECTORIES for compatible
    set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
    # Using imported target if exists
    if (TARGET ZLIB::ZLIB)
      set(ZLIB_LIBRARIES ZLIB::ZLIB)
115
      set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n  find_package(ZLIB)\nendif()")
116
    endif (TARGET ZLIB::ZLIB)
Feng Xiao's avatar
Feng Xiao committed
117 118 119 120 121 122 123
  else (ZLIB_FOUND)
    set(HAVE_ZLIB 0)
    # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
    # complain when we use them later.
    set(ZLIB_INCLUDE_DIRECTORIES)
    set(ZLIB_LIBRARIES)
  endif (ZLIB_FOUND)
124
endif (protobuf_WITH_ZLIB)
Feng Xiao's avatar
Feng Xiao committed
125

126 127 128 129
if (HAVE_ZLIB)
  add_definitions(-DHAVE_ZLIB)
endif (HAVE_ZLIB)

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
if (protobuf_BUILD_SHARED_LIBS)
  set(protobuf_SHARED_OR_STATIC "SHARED")
else (protobuf_BUILD_SHARED_LIBS)
  set(protobuf_SHARED_OR_STATIC "STATIC")
  # In case we are building static libraries, link also the runtime library statically
  # so that MSVCR*.DLL is not required at runtime.
  # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
  # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
  # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
  if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
    foreach(flag_var
        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
      if(${flag_var} MATCHES "/MD")
        string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
      endif(${flag_var} MATCHES "/MD")
    endforeach(flag_var)
  endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
endif (protobuf_BUILD_SHARED_LIBS)
Feng Xiao's avatar
Feng Xiao committed
149

150
if (MSVC)
151 152
  # Build with multiple processes
  add_definitions(/MP)
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  # MSVC warning suppressions
  add_definitions(
    /wd4018 # 'expression' : signed/unsigned mismatch
    /wd4065 # switch statement contains 'default' but no 'case' labels
    /wd4146 # unary minus operator applied to unsigned type, result still unsigned
    /wd4244 # 'conversion' conversion from 'type1' to 'type2', possible loss of data
    /wd4251 # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
    /wd4267 # 'var' : conversion from 'size_t' to 'type', possible loss of data
    /wd4305 # 'identifier' : truncation from 'type1' to 'type2'
    /wd4307 # 'operator' : integral constant overflow
    /wd4309 # 'conversion' : truncation of constant value
    /wd4334 # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
    /wd4355 # 'this' : used in base member initializer list
    /wd4506 # no definition for inline function 'function'
    /wd4800 # 'type' : forcing value to bool 'true' or 'false' (performance warning)
    /wd4996 # The compiler encountered a deprecated declaration.
  )
170 171
  # Allow big object
  add_definitions(/bigobj)
172 173
  string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
  string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
174
  string(REPLACE "." ","  protobuf_RC_FILEVERSION "${protobuf_VERSION}")
175
  configure_file(extract_includes.bat.in extract_includes.bat)
Ivan Shynkarenka's avatar
Ivan Shynkarenka committed
176

177 178
  # Suppress linker warnings about files with no symbols defined.
  set(CMAKE_STATIC_LINKER_FLAGS /ignore:4221)
179 180 181 182 183 184 185 186 187

  # Configure Resource Compiler
  enable_language(RC)
  # use English language (0x409) in resource compiler
  set(rc_flags "/l0x409")
  # fix rc.exe invocations because of usage of add_definitions()
  set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_flags} <DEFINES> /fo<OBJECT> <SOURCE>")

  configure_file(version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
188
endif (MSVC)
Feng Xiao's avatar
Feng Xiao committed
189

190

Feng Xiao's avatar
Feng Xiao committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)

include_directories(
  ${ZLIB_INCLUDE_DIRECTORIES}
  ${protobuf_BINARY_DIR}
  ${protobuf_source_dir}/src)

if (MSVC)
  # Add the "lib" prefix for generated .lib outputs.
  set(LIB_PREFIX lib)
else (MSVC)
  # When building with "make", "lib" prefix will be added automatically by
  # the build tool.
  set(LIB_PREFIX)
endif (MSVC)

207
if (protobuf_UNICODE)
208
  add_definitions(-DUNICODE -D_UNICODE)
209
endif (protobuf_UNICODE)
210

Feng Xiao's avatar
Feng Xiao committed
211 212
include(libprotobuf-lite.cmake)
include(libprotobuf.cmake)
213 214 215 216
if (protobuf_BUILD_PROTOC_BINARIES)
  include(libprotoc.cmake)
  include(protoc.cmake)
endif (protobuf_BUILD_PROTOC_BINARIES)
217

218
if (protobuf_BUILD_TESTS)
219
  include(tests.cmake)
220
endif (protobuf_BUILD_TESTS)
221

222 223 224 225
if (protobuf_BUILD_CONFORMANCE)
  include(conformance.cmake)
endif (protobuf_BUILD_CONFORMANCE)

226
include(install.cmake)
227 228 229 230 231 232 233 234

if (protobuf_BUILD_EXAMPLES)
  include(examples.cmake)
endif (protobuf_BUILD_EXAMPLES)

if(protobuf_VERBOSE)
    message(STATUS "Protocol Buffers Configuring done")
endif()