CMakeLists.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Minimum CMake required
  2. cmake_minimum_required(VERSION 2.8.12)
  3. if(protobuf_VERBOSE)
  4. message(STATUS "Protocol Buffers Configuring...")
  5. endif()
  6. # CMake policies
  7. cmake_policy(SET CMP0022 NEW)
  8. # Project
  9. project(protobuf C CXX)
  10. # Options
  11. option(protobuf_BUILD_TESTS "Build tests" ON)
  12. option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
  13. if (BUILD_SHARED_LIBS)
  14. set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
  15. else (BUILD_SHARED_LIBS)
  16. set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
  17. endif (BUILD_SHARED_LIBS)
  18. option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
  19. include(CMakeDependentOption)
  20. cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON
  21. "NOT protobuf_BUILD_SHARED_LIBS" OFF)
  22. if (MSVC)
  23. set(protobuf_WITH_ZLIB_DEFAULT OFF)
  24. else (MSVC)
  25. set(protobuf_WITH_ZLIB_DEFAULT ON)
  26. endif (MSVC)
  27. option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
  28. set(protobuf_DEBUG_POSTFIX "d"
  29. CACHE STRING "Default debug postfix")
  30. mark_as_advanced(protobuf_DEBUG_POSTFIX)
  31. # User options
  32. include(protobuf-options.cmake)
  33. # Path to main configure script
  34. set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
  35. # Parse configure script
  36. set(protobuf_AC_INIT_REGEX
  37. "^AC_INIT\\(\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\]\\)$")
  38. file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_AC_INIT_LINE
  39. LIMIT_COUNT 1 REGEX "^AC_INIT")
  40. # Description
  41. string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\1"
  42. protobuf_DESCRIPTION "${protobuf_AC_INIT_LINE}")
  43. # Version
  44. string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\2"
  45. protobuf_VERSION_STRING "${protobuf_AC_INIT_LINE}")
  46. # Contact
  47. string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\3"
  48. protobuf_CONTACT "${protobuf_AC_INIT_LINE}")
  49. # Parse version tweaks
  50. set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)-?(.*)$")
  51. string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1"
  52. protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
  53. string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2"
  54. protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
  55. string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3"
  56. protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
  57. string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\4"
  58. protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")
  59. # Package version
  60. set(protobuf_VERSION
  61. "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
  62. if(protobuf_VERSION_PRERELEASE)
  63. set(protobuf_VERSION "${protobuf_VERSION}-${protobuf_VERSION_PRERELEASE}")
  64. endif()
  65. if(protobuf_VERBOSE)
  66. message(STATUS "Configuration script parsing status [")
  67. message(STATUS " Description : ${protobuf_DESCRIPTION}")
  68. message(STATUS " Version : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
  69. message(STATUS " Contact : ${protobuf_CONTACT}")
  70. message(STATUS "]")
  71. endif()
  72. add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
  73. find_package(Threads REQUIRED)
  74. if (CMAKE_USE_PTHREADS_INIT)
  75. add_definitions(-DHAVE_PTHREAD)
  76. endif (CMAKE_USE_PTHREADS_INIT)
  77. set(_protobuf_FIND_ZLIB)
  78. if (protobuf_WITH_ZLIB)
  79. find_package(ZLIB)
  80. if (ZLIB_FOUND)
  81. set(HAVE_ZLIB 1)
  82. # FindZLIB module define ZLIB_INCLUDE_DIRS variable
  83. # Set ZLIB_INCLUDE_DIRECTORIES for compatible
  84. set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
  85. # Using imported target if exists
  86. if (TARGET ZLIB::ZLIB)
  87. set(ZLIB_LIBRARIES ZLIB::ZLIB)
  88. set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()")
  89. endif (TARGET ZLIB::ZLIB)
  90. else (ZLIB_FOUND)
  91. set(HAVE_ZLIB 0)
  92. # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
  93. # complain when we use them later.
  94. set(ZLIB_INCLUDE_DIRECTORIES)
  95. set(ZLIB_LIBRARIES)
  96. endif (ZLIB_FOUND)
  97. endif (protobuf_WITH_ZLIB)
  98. if (HAVE_ZLIB)
  99. add_definitions(-DHAVE_ZLIB)
  100. endif (HAVE_ZLIB)
  101. if (protobuf_BUILD_SHARED_LIBS)
  102. set(protobuf_SHARED_OR_STATIC "SHARED")
  103. else (protobuf_BUILD_SHARED_LIBS)
  104. set(protobuf_SHARED_OR_STATIC "STATIC")
  105. # In case we are building static libraries, link also the runtime library statically
  106. # so that MSVCR*.DLL is not required at runtime.
  107. # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
  108. # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
  109. # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
  110. if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
  111. foreach(flag_var
  112. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  113. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  114. if(${flag_var} MATCHES "/MD")
  115. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  116. endif(${flag_var} MATCHES "/MD")
  117. endforeach(flag_var)
  118. endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
  119. endif (protobuf_BUILD_SHARED_LIBS)
  120. if (MSVC)
  121. # Build with multiple processes
  122. add_definitions(/MP)
  123. add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305)
  124. # Allow big object
  125. add_definitions(/bigobj)
  126. string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
  127. string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
  128. configure_file(extract_includes.bat.in extract_includes.bat)
  129. endif (MSVC)
  130. get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)
  131. include_directories(
  132. ${ZLIB_INCLUDE_DIRECTORIES}
  133. ${protobuf_BINARY_DIR}
  134. ${protobuf_source_dir}/src)
  135. if (MSVC)
  136. # Add the "lib" prefix for generated .lib outputs.
  137. set(LIB_PREFIX lib)
  138. else (MSVC)
  139. # When building with "make", "lib" prefix will be added automatically by
  140. # the build tool.
  141. set(LIB_PREFIX)
  142. endif (MSVC)
  143. if (protobuf_UNICODE)
  144. add_definitions(-DUNICODE -D_UNICODE)
  145. endif (protobuf_UNICODE)
  146. include(libprotobuf-lite.cmake)
  147. include(libprotobuf.cmake)
  148. include(libprotoc.cmake)
  149. include(protoc.cmake)
  150. if (protobuf_BUILD_TESTS)
  151. include(tests.cmake)
  152. endif (protobuf_BUILD_TESTS)
  153. include(install.cmake)
  154. if (protobuf_BUILD_EXAMPLES)
  155. include(examples.cmake)
  156. endif (protobuf_BUILD_EXAMPLES)
  157. if(protobuf_VERBOSE)
  158. message(STATUS "Protocol Buffers Configuring done")
  159. endif()