CMakeLists.txt 11 KB

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