CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. # The Intel compiler isn't able to deal with noinline member functions of
  27. # template classes defined in headers. As such it spams the output with
  28. # warning #2196: routine is both "inline" and "noinline"
  29. # This silences that warning.
  30. if (CMAKE_CXX_COMPILER_ID MATCHES Intel)
  31. string(APPEND CMAKE_CXX_FLAGS " -diag-disable=2196")
  32. endif()
  33. # Options
  34. if(WITH_PROTOC)
  35. set(protobuf_PROTOC_EXE ${WITH_PROTOC} CACHE FILEPATH "Protocol Buffer Compiler executable" FORCE)
  36. endif()
  37. option(protobuf_BUILD_TESTS "Build tests" ON)
  38. option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
  39. option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
  40. option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
  41. option(protobuf_BUILD_LIBPROTOC "Build libprotoc" OFF)
  42. option(protobuf_DISABLE_RTTI "Remove runtime type information in the binaries" OFF)
  43. if (BUILD_SHARED_LIBS)
  44. set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
  45. else (BUILD_SHARED_LIBS)
  46. set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
  47. endif (BUILD_SHARED_LIBS)
  48. option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
  49. include(CMakeDependentOption)
  50. cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON
  51. "NOT protobuf_BUILD_SHARED_LIBS" OFF)
  52. set(protobuf_WITH_ZLIB_DEFAULT ON)
  53. option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
  54. set(protobuf_DEBUG_POSTFIX "d"
  55. CACHE STRING "Default debug postfix")
  56. mark_as_advanced(protobuf_DEBUG_POSTFIX)
  57. # User options
  58. include(protobuf-options.cmake)
  59. # Overrides for option dependencies
  60. if (protobuf_BUILD_PROTOC_BINARIES OR protobuf_BUILD_TESTS)
  61. set(protobuf_BUILD_LIBPROTOC ON)
  62. endif ()
  63. # Path to main configure script
  64. set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
  65. # Parse configure script
  66. set(protobuf_AC_INIT_REGEX
  67. "^AC_INIT\\(\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\]\\)$")
  68. file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_AC_INIT_LINE
  69. LIMIT_COUNT 1 REGEX "^AC_INIT")
  70. # Description
  71. string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\1"
  72. protobuf_DESCRIPTION "${protobuf_AC_INIT_LINE}")
  73. # Version
  74. string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\2"
  75. protobuf_VERSION_STRING "${protobuf_AC_INIT_LINE}")
  76. # Contact
  77. string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\3"
  78. protobuf_CONTACT "${protobuf_AC_INIT_LINE}")
  79. # Parse version tweaks
  80. set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)([-]rc[-]|\\.)?([0-9]*)$")
  81. string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1"
  82. protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
  83. string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2"
  84. protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
  85. string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3"
  86. protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
  87. string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\5"
  88. protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")
  89. message(STATUS "${protobuf_VERSION_PRERELEASE}")
  90. # Package version
  91. set(protobuf_VERSION
  92. "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
  93. if(protobuf_VERSION_PRERELEASE)
  94. set(protobuf_VERSION "${protobuf_VERSION}.${protobuf_VERSION_PRERELEASE}")
  95. else()
  96. set(protobuf_VERSION "${protobuf_VERSION}.0")
  97. endif()
  98. message(STATUS "${protobuf_VERSION}")
  99. if(protobuf_VERBOSE)
  100. message(STATUS "Configuration script parsing status [")
  101. message(STATUS " Description : ${protobuf_DESCRIPTION}")
  102. message(STATUS " Version : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
  103. message(STATUS " Contact : ${protobuf_CONTACT}")
  104. message(STATUS "]")
  105. endif()
  106. add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
  107. if (protobuf_DISABLE_RTTI)
  108. add_definitions(-DGOOGLE_PROTOBUF_NO_RTTI=1)
  109. endif()
  110. find_package(Threads REQUIRED)
  111. if (CMAKE_USE_PTHREADS_INIT)
  112. add_definitions(-DHAVE_PTHREAD)
  113. endif (CMAKE_USE_PTHREADS_INIT)
  114. set(_protobuf_FIND_ZLIB)
  115. if (protobuf_WITH_ZLIB)
  116. find_package(ZLIB)
  117. if (ZLIB_FOUND)
  118. set(HAVE_ZLIB 1)
  119. # FindZLIB module define ZLIB_INCLUDE_DIRS variable
  120. # Set ZLIB_INCLUDE_DIRECTORIES for compatible
  121. set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
  122. # Using imported target if exists
  123. if (TARGET ZLIB::ZLIB)
  124. set(ZLIB_LIBRARIES ZLIB::ZLIB)
  125. set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()")
  126. endif (TARGET ZLIB::ZLIB)
  127. else (ZLIB_FOUND)
  128. set(HAVE_ZLIB 0)
  129. # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
  130. # complain when we use them later.
  131. set(ZLIB_INCLUDE_DIRECTORIES)
  132. set(ZLIB_LIBRARIES)
  133. endif (ZLIB_FOUND)
  134. endif (protobuf_WITH_ZLIB)
  135. if (HAVE_ZLIB)
  136. add_definitions(-DHAVE_ZLIB)
  137. endif (HAVE_ZLIB)
  138. # We need to link with libatomic on systems that do not have builtin atomics, or
  139. # don't have builtin support for 8 byte atomics
  140. set(protobuf_LINK_LIBATOMIC false)
  141. if (NOT MSVC)
  142. include(CheckCXXSourceCompiles)
  143. set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  144. set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -std=c++11)
  145. check_cxx_source_compiles("
  146. #include <atomic>
  147. int main() {
  148. return std::atomic<int64_t>{};
  149. }
  150. " protobuf_HAVE_BUILTIN_ATOMICS)
  151. if (NOT protobuf_HAVE_BUILTIN_ATOMICS)
  152. set(protobuf_LINK_LIBATOMIC true)
  153. endif (NOT protobuf_HAVE_BUILTIN_ATOMICS)
  154. set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  155. endif (NOT MSVC)
  156. if (protobuf_BUILD_SHARED_LIBS)
  157. set(protobuf_SHARED_OR_STATIC "SHARED")
  158. else (protobuf_BUILD_SHARED_LIBS)
  159. set(protobuf_SHARED_OR_STATIC "STATIC")
  160. # In case we are building static libraries, link also the runtime library statically
  161. # so that MSVCR*.DLL is not required at runtime.
  162. # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
  163. # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
  164. # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
  165. if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
  166. foreach(flag_var
  167. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  168. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  169. if(${flag_var} MATCHES "/MD")
  170. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  171. endif(${flag_var} MATCHES "/MD")
  172. endforeach(flag_var)
  173. endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
  174. endif (protobuf_BUILD_SHARED_LIBS)
  175. if (MSVC)
  176. # Build with multiple processes
  177. add_definitions(/MP)
  178. # MSVC warning suppressions
  179. add_definitions(
  180. /wd4018 # 'expression' : signed/unsigned mismatch
  181. /wd4065 # switch statement contains 'default' but no 'case' labels
  182. /wd4146 # unary minus operator applied to unsigned type, result still unsigned
  183. /wd4244 # 'conversion' conversion from 'type1' to 'type2', possible loss of data
  184. /wd4251 # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
  185. /wd4267 # 'var' : conversion from 'size_t' to 'type', possible loss of data
  186. /wd4305 # 'identifier' : truncation from 'type1' to 'type2'
  187. /wd4307 # 'operator' : integral constant overflow
  188. /wd4309 # 'conversion' : truncation of constant value
  189. /wd4334 # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
  190. /wd4355 # 'this' : used in base member initializer list
  191. /wd4506 # no definition for inline function 'function'
  192. /wd4800 # 'type' : forcing value to bool 'true' or 'false' (performance warning)
  193. /wd4996 # The compiler encountered a deprecated declaration.
  194. )
  195. # Allow big object
  196. add_definitions(/bigobj)
  197. string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
  198. string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
  199. string(REPLACE "." "," protobuf_RC_FILEVERSION "${protobuf_VERSION}")
  200. configure_file(extract_includes.bat.in extract_includes.bat)
  201. # Suppress linker warnings about files with no symbols defined.
  202. set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4221")
  203. # Configure Resource Compiler
  204. enable_language(RC)
  205. # use English language (0x409) in resource compiler
  206. set(rc_flags "/l0x409")
  207. # fix rc.exe invocations because of usage of add_definitions()
  208. set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_flags} <DEFINES> /fo<OBJECT> <SOURCE>")
  209. configure_file(version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
  210. endif (MSVC)
  211. get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)
  212. include_directories(
  213. ${ZLIB_INCLUDE_DIRECTORIES}
  214. ${protobuf_BINARY_DIR}
  215. ${protobuf_source_dir}/src)
  216. if (MSVC)
  217. # Add the "lib" prefix for generated .lib outputs.
  218. set(LIB_PREFIX lib)
  219. else (MSVC)
  220. # When building with "make", "lib" prefix will be added automatically by
  221. # the build tool.
  222. set(LIB_PREFIX)
  223. endif (MSVC)
  224. if (protobuf_UNICODE)
  225. add_definitions(-DUNICODE -D_UNICODE)
  226. endif (protobuf_UNICODE)
  227. include(libprotobuf-lite.cmake)
  228. include(libprotobuf.cmake)
  229. if (protobuf_BUILD_LIBPROTOC)
  230. include(libprotoc.cmake)
  231. endif (protobuf_BUILD_LIBPROTOC)
  232. if (protobuf_BUILD_PROTOC_BINARIES)
  233. include(protoc.cmake)
  234. if (NOT DEFINED protobuf_PROTOC_EXE)
  235. set(protobuf_PROTOC_EXE protoc)
  236. endif (NOT DEFINED protobuf_PROTOC_EXE)
  237. endif (protobuf_BUILD_PROTOC_BINARIES)
  238. # Ensure we have a protoc executable if we need one
  239. if (protobuf_BUILD_TESTS OR protobuf_BUILD_CONFORMANCE OR protobuf_BUILD_EXAMPLES)
  240. if (NOT DEFINED protobuf_PROTOC_EXE)
  241. find_program(protobuf_PROTOC_EXE protoc)
  242. if (NOT protobuf_PROTOC_EXE)
  243. message(FATAL "Build requires 'protoc' but binary not found and not building protoc.")
  244. endif ()
  245. endif ()
  246. if(protobuf_VERBOSE)
  247. message(STATUS "Using protoc : ${protobuf_PROTOC_EXE}")
  248. endif(protobuf_VERBOSE)
  249. endif ()
  250. if (protobuf_BUILD_TESTS)
  251. include(tests.cmake)
  252. endif (protobuf_BUILD_TESTS)
  253. if (protobuf_BUILD_CONFORMANCE)
  254. include(conformance.cmake)
  255. endif (protobuf_BUILD_CONFORMANCE)
  256. include(install.cmake)
  257. if (protobuf_BUILD_EXAMPLES)
  258. include(examples.cmake)
  259. endif (protobuf_BUILD_EXAMPLES)
  260. if(protobuf_VERBOSE)
  261. message(STATUS "Protocol Buffers Configuring done")
  262. endif(protobuf_VERBOSE)