CMakeLists.txt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Minimum CMake required
  2. cmake_minimum_required(VERSION 2.8)
  3. # Project
  4. project(protobuf C CXX)
  5. # Options
  6. option(BUILD_TESTING "Build tests" ON)
  7. option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
  8. if (MSVC)
  9. option(ZLIB "Build with zlib support" OFF)
  10. endif (MSVC)
  11. # Path to main configure script
  12. set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
  13. # Parse version from configure script
  14. file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_VERSION_LINE
  15. LIMIT_COUNT 1
  16. REGEX "^AC_INIT")
  17. # Replace special characters
  18. string(REPLACE "(" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE})
  19. string(REPLACE ")" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE})
  20. string(REPLACE "[" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE})
  21. string(REPLACE "]" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE})
  22. # Parse version string
  23. string(REGEX REPLACE "^AC_INIT__Protocol Buffers_,_([^_]+).*$" "\\1"
  24. protobuf_VERSION_STRING "${protobuf_VERSION_LINE}")
  25. # Parse version tweaks
  26. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+).*$" "\\1"
  27. protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
  28. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+).*$" "\\2"
  29. protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
  30. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+).*$" "\\3"
  31. protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
  32. # Package version
  33. set(protobuf_VERSION
  34. "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
  35. add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
  36. find_package(Threads REQUIRED)
  37. if (CMAKE_USE_PTHREADS_INIT)
  38. add_definitions(-DHAVE_PTHREAD)
  39. endif (CMAKE_USE_PTHREADS_INIT)
  40. if (MSVC)
  41. if (ZLIB)
  42. set(HAVE_ZLIB 1)
  43. find_path(ZLIB_INCLUDE_DIRECTORIES zlib.h ${protobuf_SOURCE_DIR})
  44. find_library(ZLIB_LIBRARIES zdll ${protobuf_SOURCE_DIR})
  45. else (ZLIB)
  46. set(HAVE_ZLIB 0)
  47. endif (ZLIB)
  48. else (MSVC)
  49. find_package(ZLIB)
  50. if (ZLIB_FOUND)
  51. set(HAVE_ZLIB 1)
  52. else (ZLIB_FOUND)
  53. set(HAVE_ZLIB 0)
  54. # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
  55. # complain when we use them later.
  56. set(ZLIB_INCLUDE_DIRECTORIES)
  57. set(ZLIB_LIBRARIES)
  58. endif (ZLIB_FOUND)
  59. endif (MSVC)
  60. if (HAVE_ZLIB)
  61. add_definitions(-DHAVE_ZLIB)
  62. endif (HAVE_ZLIB)
  63. if (MSVC)
  64. if (BUILD_SHARED_LIBS)
  65. add_definitions(-DPROTOBUF_USE_DLLS)
  66. else (BUILD_SHARED_LIBS)
  67. # In case we are building static libraries, link also the runtime library statically
  68. # so that MSVCR*.DLL is not required at runtime.
  69. # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
  70. # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
  71. # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
  72. foreach(flag_var
  73. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  74. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  75. if(${flag_var} MATCHES "/MD")
  76. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  77. endif(${flag_var} MATCHES "/MD")
  78. endforeach(flag_var)
  79. endif (BUILD_SHARED_LIBS)
  80. add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305)
  81. endif (MSVC)
  82. if (MSVC)
  83. string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
  84. string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
  85. configure_file(extract_includes.bat.in extract_includes.bat)
  86. endif (MSVC)
  87. get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)
  88. include_directories(
  89. ${ZLIB_INCLUDE_DIRECTORIES}
  90. ${protobuf_BINARY_DIR}
  91. ${protobuf_source_dir}/src)
  92. if (MSVC)
  93. # Add the "lib" prefix for generated .lib outputs.
  94. set(LIB_PREFIX lib)
  95. else (MSVC)
  96. # When building with "make", "lib" prefix will be added automatically by
  97. # the build tool.
  98. set(LIB_PREFIX)
  99. endif (MSVC)
  100. include(libprotobuf-lite.cmake)
  101. include(libprotobuf.cmake)
  102. include(libprotoc.cmake)
  103. include(protoc.cmake)
  104. if (BUILD_TESTING)
  105. include(tests.cmake)
  106. endif (BUILD_TESTING)
  107. include(install.cmake)