CMakeLists.txt 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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]+)([-]rc[-]|\\.)?([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}" "\\5"
  72. protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")
  73. message(STATUS "${protobuf_VERSION_PRERELEASE}")
  74. # Package version
  75. set(protobuf_VERSION
  76. "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
  77. if(protobuf_VERSION_PRERELEASE)
  78. set(protobuf_VERSION "${protobuf_VERSION}.${protobuf_VERSION_PRERELEASE}")
  79. else()
  80. set(protobuf_VERSION "${protobuf_VERSION}.0")
  81. endif()
  82. message(STATUS "${protobuf_VERSION}")
  83. if(protobuf_VERBOSE)
  84. message(STATUS "Configuration script parsing status [")
  85. message(STATUS " Description : ${protobuf_DESCRIPTION}")
  86. message(STATUS " Version : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
  87. message(STATUS " Contact : ${protobuf_CONTACT}")
  88. message(STATUS "]")
  89. endif()
  90. add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
  91. find_package(Threads REQUIRED)
  92. if (CMAKE_USE_PTHREADS_INIT)
  93. add_definitions(-DHAVE_PTHREAD)
  94. endif (CMAKE_USE_PTHREADS_INIT)
  95. set(_protobuf_FIND_ZLIB)
  96. if (protobuf_WITH_ZLIB)
  97. find_package(ZLIB)
  98. if (ZLIB_FOUND)
  99. set(HAVE_ZLIB 1)
  100. # FindZLIB module define ZLIB_INCLUDE_DIRS variable
  101. # Set ZLIB_INCLUDE_DIRECTORIES for compatible
  102. set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
  103. # Using imported target if exists
  104. if (TARGET ZLIB::ZLIB)
  105. set(ZLIB_LIBRARIES ZLIB::ZLIB)
  106. set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()")
  107. endif (TARGET ZLIB::ZLIB)
  108. else (ZLIB_FOUND)
  109. set(HAVE_ZLIB 0)
  110. # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
  111. # complain when we use them later.
  112. set(ZLIB_INCLUDE_DIRECTORIES)
  113. set(ZLIB_LIBRARIES)
  114. endif (ZLIB_FOUND)
  115. endif (protobuf_WITH_ZLIB)
  116. if (HAVE_ZLIB)
  117. add_definitions(-DHAVE_ZLIB)
  118. endif (HAVE_ZLIB)
  119. # We need to link with libatomic on systems that do not have builtin atomics, or
  120. # don't have builtin support for 8 byte atomics
  121. set(protobuf_LINK_LIBATOMIC false)
  122. if (NOT MSVC)
  123. include(CheckCXXSourceCompiles)
  124. set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  125. set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -std=c++11)
  126. check_cxx_source_compiles("
  127. #include <atomic>
  128. int main() {
  129. return std::atomic<int64_t>{};
  130. }
  131. " protobuf_HAVE_BUILTIN_ATOMICS)
  132. if (NOT protobuf_HAVE_BUILTIN_ATOMICS)
  133. set(protobuf_LINK_LIBATOMIC true)
  134. endif (NOT protobuf_HAVE_BUILTIN_ATOMICS)
  135. set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  136. endif (NOT MSVC)
  137. if (protobuf_BUILD_SHARED_LIBS)
  138. set(protobuf_SHARED_OR_STATIC "SHARED")
  139. else (protobuf_BUILD_SHARED_LIBS)
  140. set(protobuf_SHARED_OR_STATIC "STATIC")
  141. # In case we are building static libraries, link also the runtime library statically
  142. # so that MSVCR*.DLL is not required at runtime.
  143. # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
  144. # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
  145. # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
  146. if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
  147. foreach(flag_var
  148. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  149. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  150. if(${flag_var} MATCHES "/MD")
  151. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  152. endif(${flag_var} MATCHES "/MD")
  153. endforeach(flag_var)
  154. endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
  155. endif (protobuf_BUILD_SHARED_LIBS)
  156. if (MSVC)
  157. # Build with multiple processes
  158. add_definitions(/MP)
  159. # MSVC warning suppressions
  160. add_definitions(
  161. /wd4018 # 'expression' : signed/unsigned mismatch
  162. /wd4065 # switch statement contains 'default' but no 'case' labels
  163. /wd4146 # unary minus operator applied to unsigned type, result still unsigned
  164. /wd4244 # 'conversion' conversion from 'type1' to 'type2', possible loss of data
  165. /wd4251 # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
  166. /wd4267 # 'var' : conversion from 'size_t' to 'type', possible loss of data
  167. /wd4305 # 'identifier' : truncation from 'type1' to 'type2'
  168. /wd4307 # 'operator' : integral constant overflow
  169. /wd4309 # 'conversion' : truncation of constant value
  170. /wd4334 # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
  171. /wd4355 # 'this' : used in base member initializer list
  172. /wd4506 # no definition for inline function 'function'
  173. /wd4800 # 'type' : forcing value to bool 'true' or 'false' (performance warning)
  174. /wd4996 # The compiler encountered a deprecated declaration.
  175. )
  176. # Allow big object
  177. add_definitions(/bigobj)
  178. string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
  179. string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
  180. string(REPLACE "." "," protobuf_RC_FILEVERSION "${protobuf_VERSION}")
  181. configure_file(extract_includes.bat.in extract_includes.bat)
  182. # Suppress linker warnings about files with no symbols defined.
  183. set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4221")
  184. # Configure Resource Compiler
  185. enable_language(RC)
  186. # use English language (0x409) in resource compiler
  187. set(rc_flags "/l0x409")
  188. # fix rc.exe invocations because of usage of add_definitions()
  189. set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_flags} <DEFINES> /fo<OBJECT> <SOURCE>")
  190. configure_file(version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
  191. endif (MSVC)
  192. get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)
  193. include_directories(
  194. ${ZLIB_INCLUDE_DIRECTORIES}
  195. ${protobuf_BINARY_DIR}
  196. ${protobuf_source_dir}/src)
  197. if (MSVC)
  198. # Add the "lib" prefix for generated .lib outputs.
  199. set(LIB_PREFIX lib)
  200. else (MSVC)
  201. # When building with "make", "lib" prefix will be added automatically by
  202. # the build tool.
  203. set(LIB_PREFIX)
  204. endif (MSVC)
  205. if (protobuf_UNICODE)
  206. add_definitions(-DUNICODE -D_UNICODE)
  207. endif (protobuf_UNICODE)
  208. include(libprotobuf-lite.cmake)
  209. include(libprotobuf.cmake)
  210. if (protobuf_BUILD_PROTOC_BINARIES)
  211. include(libprotoc.cmake)
  212. include(protoc.cmake)
  213. endif (protobuf_BUILD_PROTOC_BINARIES)
  214. if (protobuf_BUILD_TESTS)
  215. include(tests.cmake)
  216. endif (protobuf_BUILD_TESTS)
  217. if (protobuf_BUILD_CONFORMANCE)
  218. include(conformance.cmake)
  219. endif (protobuf_BUILD_CONFORMANCE)
  220. include(install.cmake)
  221. if (protobuf_BUILD_EXAMPLES)
  222. include(examples.cmake)
  223. endif (protobuf_BUILD_EXAMPLES)
  224. if(protobuf_VERBOSE)
  225. message(STATUS "Protocol Buffers Configuring done")
  226. endif()