CMakeLists.txt 10 KB

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