Advertisement
mattygrogy

protobuf_fixed

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