CMakeLists.txt 5.89 KB
Newer Older
1
# Minimum CMake required
2
cmake_minimum_required(VERSION 2.8.12)
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 10
# CMake policies
cmake_policy(SET CMP0022 NEW)

11 12 13
# Project
project(protobuf C CXX)

14
# Options
15
option(protobuf_BUILD_TESTS "Build tests" ON)
16
option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
17 18 19 20 21 22
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})
23 24 25
include(CMakeDependentOption)
cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON
  "NOT protobuf_BUILD_SHARED_LIBS" OFF)
Feng Xiao's avatar
Feng Xiao committed
26
if (MSVC)
27
  set(protobuf_WITH_ZLIB_DEFAULT OFF)
28
else (MSVC)
29
  set(protobuf_WITH_ZLIB_DEFAULT ON)
Feng Xiao's avatar
Feng Xiao committed
30
endif (MSVC)
31
option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
32 33
set(protobuf_DEBUG_POSTFIX "d"
  CACHE STRING "Default debug postfix")
34 35
mark_as_advanced(protobuf_DEBUG_POSTFIX)
# User options
36
include(protobuf-options.cmake)
Feng Xiao's avatar
Feng Xiao committed
37

38 39
# Path to main configure script
set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54
# 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}")
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 64 65
string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\4"
  protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")

66
# Package version
67
set(protobuf_VERSION
68
  "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
69

70 71 72 73
if(protobuf_VERSION_PRERELEASE)
  set(protobuf_VERSION "${protobuf_VERSION}-${protobuf_VERSION_PRERELEASE}")
endif()

74 75 76 77 78 79 80 81
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()

82 83
add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)

Feng Xiao's avatar
Feng Xiao committed
84 85
find_package(Threads REQUIRED)
if (CMAKE_USE_PTHREADS_INIT)
86
  add_definitions(-DHAVE_PTHREAD)
Feng Xiao's avatar
Feng Xiao committed
87 88
endif (CMAKE_USE_PTHREADS_INIT)

89
if (protobuf_WITH_ZLIB)
Feng Xiao's avatar
Feng Xiao committed
90 91 92
  find_package(ZLIB)
  if (ZLIB_FOUND)
    set(HAVE_ZLIB 1)
93 94 95 96 97 98 99
    # 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)
    endif (TARGET ZLIB::ZLIB)
Feng Xiao's avatar
Feng Xiao committed
100 101 102 103 104 105 106
  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)
107
endif (protobuf_WITH_ZLIB)
Feng Xiao's avatar
Feng Xiao committed
108

109 110 111 112
if (HAVE_ZLIB)
  add_definitions(-DHAVE_ZLIB)
endif (HAVE_ZLIB)

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
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
132

133
if (MSVC)
134 135
  # Build with multiple processes
  add_definitions(/MP)
136
  add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305)
137 138
  # Allow big object
  add_definitions(/bigobj)
139 140 141 142
  string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
  string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
  configure_file(extract_includes.bat.in extract_includes.bat)
endif (MSVC)
Feng Xiao's avatar
Feng Xiao committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

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)

include(libprotobuf-lite.cmake)
include(libprotobuf.cmake)
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 172 173 174 175 176 177

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

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