CMakeLists.txt 9.5 KB

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