CMakeLists.txt 7.9 KB

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