CMakeLists.txt 5.2 KB

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