CMakeLists.txt 7.3 KB

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