CMakeLists.txt 8.3 KB

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