CMakeLists.txt 7.4 KB

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