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